github.com/tomwright/dasel@v1.27.3/storage/plain.go (about)

     1  package storage
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  )
     7  
     8  func init() {
     9  	registerWriteParser([]string{"-", "plain"}, []string{}, &PlainParser{})
    10  }
    11  
    12  // PlainParser is a Parser implementation to handle plain files.
    13  type PlainParser struct {
    14  }
    15  
    16  // ErrPlainParserNotImplemented is returned when you try to use the PlainParser.FromBytes func.
    17  var ErrPlainParserNotImplemented = fmt.Errorf("PlainParser.FromBytes not implemented")
    18  
    19  // FromBytes returns some data that is represented by the given bytes.
    20  func (p *PlainParser) FromBytes(byteData []byte) (interface{}, error) {
    21  	return nil, ErrPlainParserNotImplemented
    22  }
    23  
    24  // ToBytes returns a slice of bytes that represents the given value.
    25  func (p *PlainParser) ToBytes(value interface{}, options ...ReadWriteOption) ([]byte, error) {
    26  	buf := new(bytes.Buffer)
    27  	switch val := value.(type) {
    28  	case SingleDocument:
    29  		buf.Write([]byte(fmt.Sprintf("%v\n", val.Document())))
    30  	case MultiDocument:
    31  		for _, doc := range val.Documents() {
    32  			buf.Write([]byte(fmt.Sprintf("%v\n", doc)))
    33  		}
    34  	default:
    35  		buf.Write([]byte(fmt.Sprintf("%v\n", val)))
    36  	}
    37  	return buf.Bytes(), nil
    38  }