encode

package
v0.9.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 22, 2024 License: MIT Imports: 9 Imported by: 1

Documentation

Index

Constants

View Source
const Dashing = "-"

the key for sequences

Variables

This section is empty.

Functions

func ExtractString

func ExtractString(el r.Value) (ret string, err error)

a helper which, given a reflected string value returns that string.

Types

type Comment

type Comment struct {
	Header []string // before the key
	Prefix []string // the key comment, between the key and value
	Suffix []string // trailing comment, the value
}

type Commenting added in v0.8.0

type Commenting func(r.Value) (Comments, error)

turns a value representing one or more comments into a comment iterator. the encoder uses the iterator to generate comments for collections.

var NoComments Commenting = nil

a nil commenting implementation for readability

type Comments

type Comments interface {
	Next() bool          // called before every element, false if there are no more elements
	GetComment() Comment // valid after next returns true
}

comment access for collections

func CommentBlock

func CommentBlock(v r.Value) (ret Comments, err error)

expects that the value is a kind of string containing a standard tell comment block

func CommentSlice added in v0.8.0

func CommentSlice(els []Comment) Comments

an implementation of Commenting that walks the passed slice.

func DiscardComments

func DiscardComments(r.Value) (Comments, error)

implies that there are comments; the encoder should simply skip them.

type Encoder

type Encoder struct {
	Tabs              TabWriter
	Mapper, Sequencer StartCollection
	MapComments       Commenting
	SequenceComments  Commenting
}

func MakeCommentEncoder

func MakeCommentEncoder(w io.Writer) Encoder

use the "CommentBlock" encoder

func MakeEncoder

func MakeEncoder(w io.Writer) Encoder

an encoder that expects no comments

func (*Encoder) Encode

func (enc *Encoder) Encode(v any) (err error)

func (*Encoder) WriteMapping

func (enc *Encoder) WriteMapping(it Iterator, wasMaps bool) (err error)

func (*Encoder) WriteSequence

func (enc *Encoder) WriteSequence(it Iterator, wasMaps bool) (err error)

func (*Encoder) WriteValue

func (enc *Encoder) WriteValue(v r.Value, wasMaps bool) (err error)

writes a single value to the stream wrapped by tab writer if the parent was map, and there is a new sequence; then we want a newline

type GetReflectedValue

type GetReflectedValue interface {
	GetReflectedValue() r.Value
}

if implemented by one of the iterators will be used instead of GetValue()

type Iterator added in v0.8.0

type Iterator interface {
	// return true if there are elements left
	Next() bool
	// return "-" for sequences
	GetKey() string
	// can panic if there are no remaining elements
	GetValue() any
}

walk the elements of a collection

func MakeSequence added in v0.8.0

func MakeSequence(src r.Value) (ret Iterator, err error)

type MapTransform

type MapTransform struct {
	// contains filtered or unexported fields
}

customization for serializing native maps r.Value is guaranteed to a kind of reflect.Map

func (*MapTransform) KeyTransform

func (m *MapTransform) KeyTransform(t func(keys r.Value) string) *MapTransform

change a reflected key into an encodable string the default uses reflect Value.String()

func (*MapTransform) Mapper

func (m *MapTransform) Mapper() StartCollection

return a factory function for the encoder

func (*MapTransform) Sort

func (m *MapTransform) Sort(t func(a, b string) bool) *MapTransform

sort keys; by default keys are written sorted as per standard go string rules.

type StartCollection added in v0.8.0

type StartCollection func(r.Value) (Iterator, error)

for sequences, value is guaranteed to be a reflect.Slice for mappings, value is guaranteed to be a reflect.Map

type TabWriter

type TabWriter struct {
	Writer io.Writer
	// contains filtered or unexported fields
}

func (*TabWriter) Escape

func (tab *TabWriter) Escape(s string) bool

escape the contents of a string returns true if there were any escapes

func (*TabWriter) Indent

func (tab *TabWriter) Indent(inc bool)

inc: increases the current indent line: increases the current line

func (*TabWriter) Nextline added in v0.9.0

func (tab *TabWriter) Nextline()

func (*TabWriter) Quote

func (tab *TabWriter) Quote(s string)

quotes and escapes the passed string. fix? uses strconv, and strconv produces \x, \u, and \U escapes ( plus it requires multiple traversals over the string )

func (*TabWriter) Softline

func (tab *TabWriter) Softline()

func (*TabWriter) Space

func (tab *TabWriter) Space()

a soft space -- eaten if theres a newline

func (*TabWriter) Tab

func (tab *TabWriter) Tab()

func (*TabWriter) WriteRune

func (tab *TabWriter) WriteRune(q rune) (ret int, err error)

func (*TabWriter) WriteString

func (tab *TabWriter) WriteString(s string) (int, error)

type TellMapping

type TellMapping interface {
	TellMapping() Iterator
}

controls serialization when implemented by a value that's being encoded

type TellSequence

type TellSequence interface {
	TellSequence() Iterator
}

controls serialization when implemented by a value that's being encoded

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL