Documentation ¶
Overview ¶
Package yson implements encoding and decoding of YSON.
See https://wiki.yandex-team.ru/yt/userdoc/yson/ for introduction to YSON.
Package provides interface similar to encoding/json package.
type myValue struct { A int `yson:"a"` B string `yson:"b"` } func Example() error { var v myValue b := []byte("{a=1;b=c;}") if err := yson.Unmarshal(b, &v); err != nil { return err } if err, b = yson.Marshal(v); err != nil { return err } return nil }
Index ¶
- Variables
- func AttrsOf(value any) map[string]any
- func InferAttrs(v any) ([]string, error)
- func IsTrue(value any) bool
- func Marshal(value any) ([]byte, error)
- func MarshalFormat(value any, format Format) ([]byte, error)
- func MarshalOptions(value any, opts *EncoderOptions) ([]byte, error)
- func MarshalTime(t Time) (s string, err error)
- func MustInferAttrs(v any) []string
- func RegisterInterfaceDecoder(iface any, decoder DecoderFn)
- func SliceYPathAttrs(ypath []byte) (n int, err error)
- func SliceYPathString(ypath []byte, str *[]byte) (n int, err error)
- func SliceYPathValue(ypath []byte, value *any) (n int, err error)
- func Unmarshal(data []byte, v any) error
- func UnmarshalOptions(data []byte, v any, opts *DecoderOptions) error
- func Valid(data []byte) error
- func ValidListFragment(data []byte) error
- func ValidMapFragment(data []byte) error
- func ValueOf(value any) any
- type Decoder
- type DecoderFn
- type DecoderOptions
- type Duration
- type Encoder
- type EncoderOptions
- type Event
- type Format
- type Marshaler
- type RawValue
- type Reader
- func (r *Reader) Bool() bool
- func (r *Reader) Bytes() []byte
- func (r *Reader) CheckFinish() error
- func (r *Reader) Float64() float64
- func (r *Reader) Int64() int64
- func (r *Reader) Next(skipAttributes bool) (Event, error)
- func (r *Reader) NextKey() (ok bool, err error)
- func (r *Reader) NextListItem() (ok bool, err error)
- func (r *Reader) NextRawValue() ([]byte, error)
- func (r *Reader) String() string
- func (r *Reader) Type() Type
- func (r *Reader) Uint64() uint64
- func (r *Reader) Undo(event Event)
- type StreamKind
- type StreamMarshaler
- type StreamUnmarshaler
- type SyntaxError
- type Tag
- type Time
- type Type
- type TypeError
- type Unmarshaler
- type UnsupportedTypeError
- type ValueWithAttrs
- type Writer
- func (w *Writer) Any(v any)
- func (w *Writer) BeginAttrs()
- func (w *Writer) BeginList()
- func (w *Writer) BeginMap()
- func (w *Writer) Bool(b bool)
- func (w *Writer) Bytes(b []byte)
- func (w *Writer) EndAttrs()
- func (w *Writer) EndList()
- func (w *Writer) EndMap()
- func (w *Writer) Entity()
- func (w *Writer) Err() error
- func (w *Writer) Finish() error
- func (w *Writer) Float64(f float64)
- func (w *Writer) Int64(i int64)
- func (w *Writer) MapKeyBytes(k []byte)
- func (w *Writer) MapKeyString(k string)
- func (w *Writer) RawNode(raw []byte)
- func (w *Writer) String(s string)
- func (w *Writer) Uint64(u uint64)
- type WriterConfig
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrIntegerOverflow = errors.New("yson: integer overflow")
var ErrInvalidNesting = errors.New("invalid YSON nesting")
Functions ¶
func AttrsOf ¶
AttrsOf is a helper function to work with ValueWithAttrs. If value is a pointer to ValueWithAttrs, returns attributes. Otherwise returns nil.
func InferAttrs ¶
InferAttrs infers attribute names from struct.
Function returns attribute names of struct fields including exported fields of yson-untagged anonymous fields
func IsTrue ¶
IsTrue is a helper function for compatibility with C++ ConvertTo<bool>().
It returns true, if value is bool(true) or string("true").
Early versions of YSON was missing separate wire type for bool. Instead, string "true" was used to specify true value. C++ code still accepts string value where bool is expected, for compatibility reasons.
func Marshal ¶
Marshal returns YSON encoding of value.
Marshal traverses value recursively.
If value implements Marshaler interface, Marshal calls its MarshalYSON method to produce YSON.
If value implements StreamMarshaler interface, Marshal calls its MarshalYSON method to produce YSON.
Otherwise, the following default encoding is used.
Boolean values are encoded as YSON booleans.
Floating point values are encoded as YSON float64.
Unsigned integer types uint8, uint16, uint32, uint64 and uint are encoded as unsigned YSON integers.
Signed integer types int8, int16, int32, int64 and int are encoded as signed YSON integers.
string and []byte values are encoded as YSON strings. Note that YSON strings are always binary.
Slice values are encoded as YSON lists (with exception of []byte).
Struct values are encoded as YSON maps by default. Encoding of each struct field can be customized by format string stored under the "yson" key in the field's tag.
// Field appears in YSON under key "my_field". Field int `yson:"my_field"` // Field appears as attribute with name "my_attr". Field int `yson:"my_attr,attr"` // Field encoding completely replaces encoding of the whole struct. // Other fields annotated as ",attr" are encoded as attributes preceding the value. // All other fields are ignored. Field int `yson:",value"` // Field is skipped if empty. Field int `yson:",omitempty"' // Field is ignored by this package Field int `yson:"-"` // Field appears in YSON under key "-" Field int `yson:"-,"`
Map values are encoded as YSON maps. The map's key type must either be a string, implement encoding.TextMarshaler, or implement encoding.BinaryMarshaler. The map keys are sorted and used as YSON map keys by applying the following rules:
- keys of any string type are used directly
- encoding.TextMarshalers are marshaled
- encoding.BinaryMarshalers are marshaled
Pointer values are encoded as the value pointed to. A nil pointer encodes as the YSON entity value.
Values implementing encoding.TextMarshaler and encoding.BinaryMarshaler interface are encoded as YSON strings.
Interface values are encoded as the value contained in the interface. A nil interface value encodes as the YSON entity value.
func MarshalOptions ¶
func MarshalOptions(value any, opts *EncoderOptions) ([]byte, error)
func MarshalTime ¶
MarshalTime encodes time to YT-specific time format.
Zero time is encoded into entity.
func MustInferAttrs ¶
MustInferAttrs is like InferAttrs but panics in case of an error.
Example ¶
package main import ( "fmt" "strings" "go.ytsaurus.tech/yt/go/yson" ) type Inner struct { G bool `yson:"g,attr"` } type anonymous struct { F *int `yson:"f,attr"` *Inner } type Anonymous struct { F *int `yson:"f,attr"` *Inner } type MyStruct struct { Value string `yson:",value"` Attr int64 `yson:"attr,attr"` NoName float64 `yson:",attr"` NoAttr string `yson:"no_attr"` NoTag string Inner *Inner `yson:"inner,attr"` anonymous Anonymous `yson:"a,attr"` } var Attrs = yson.MustInferAttrs(&MyStruct{}) func main() { fmt.Println(strings.Join(Attrs, ",")) }
Output: attr,NoName,inner,f,g,a
func SliceYPathAttrs ¶
SliceYPathAttrs splits ypath into attributes and path.
SliceYPathAttrs does not validate that path is a correct ypath.
func SliceYPathValue ¶
SliceYPathValue decodes single YSON value from beginning of ypath.
Returned value might point into the buffer.
func Unmarshal ¶
Unmarshal parses YSON-encoded data and stores result in the value pointed by v. If v is nil or not a pointer, Unmarshal returns UnsupportedTypeError.
Unmarshal works similar to encoding/json package.
Mapping between YSON types and go objects is the same as in Marshal().
func UnmarshalOptions ¶
func UnmarshalOptions(data []byte, v any, opts *DecoderOptions) error
func ValidListFragment ¶
ValidListFragment checks that byte sequence is a valid YSON list fragment.
func ValidMapFragment ¶
ValidMapFragment checks that byte sequence is a valid YSON map fragment.
Types ¶
type Decoder ¶
type Decoder struct { R *Reader // contains filtered or unexported fields }
func NewDecoder ¶
func NewDecoderFromBytes ¶
func (*Decoder) CheckFinish ¶
type DecoderOptions ¶
type DecoderOptions struct {
SupportYPAPIMaps bool
}
type Encoder ¶
type Encoder struct {
// contains filtered or unexported fields
}
Encoder writes YSON to output stream.
func NewEncoder ¶
func NewEncoderWriter ¶
type EncoderOptions ¶
type EncoderOptions struct {
SupportYPAPIMaps bool
}
type Reader ¶
type Reader struct {
// contains filtered or unexported fields
}
Reader provides streaming access to YSON node.
func NewReaderFromBytes ¶
func NewReaderKind ¶
func NewReaderKind(r io.Reader, kind StreamKind) *Reader
func NewReaderKindFromBytes ¶
func NewReaderKindFromBytes(b []byte, kind StreamKind) *Reader
func (*Reader) CheckFinish ¶
CheckFinish consumes the rest of input.
func (*Reader) NextKey ¶
NextKey returns true if next event in the stream is a key.
If next event is a key, NextKey consumes it.
func (*Reader) NextListItem ¶
NextListItem returns true if next event in the stream is end of list.
func (*Reader) NextRawValue ¶
NextRawValue reads next YSON value from stream.
Returned slice is valid only until next call to the reader.
type StreamKind ¶
type StreamKind int
StreamKind is kind of top level value.
const ( StreamNode StreamKind = iota StreamListFragment StreamMapFragment )
func (StreamKind) String ¶
func (s StreamKind) String() string
type StreamMarshaler ¶
StreamMarshaler is an interface implemented by types that can encode themselves to YSON.
type StreamUnmarshaler ¶
StreamUnmarshaler is an interface implemented by types that can unmarshal themselves from YSON reader.
type SyntaxError ¶
type SyntaxError struct {
Message string
}
func (*SyntaxError) Error ¶
func (e *SyntaxError) Error() string
type Time ¶
Time is an alias for time.Time with YT specific time representation format.
func UnmarshalTime ¶
UnmarshalTime decodes time from YT-specific time format.
Entity is decoded into zero time.
func (Time) MarshalText ¶
func (*Time) UnmarshalText ¶
type Unmarshaler ¶
Unmarshaler is an interface implemented by types that can unmarshal themselves from YSON. Input can be assumed to be a valid YSON value.
type UnsupportedTypeError ¶
func (*UnsupportedTypeError) Error ¶
func (e *UnsupportedTypeError) Error() string
type ValueWithAttrs ¶
ValueWithAttrs is a generic representation of YSON node with attached attributes.
type Writer ¶
type Writer struct {
// contains filtered or unexported fields
}
Writer writes YSON stream to underlying byte stream.
Example usage:
var buf bytes.Buffer w := yson.NewWriter(&buf) w.BeginMap() w.MapKeyString("a") w.Bool(false) w.EndMap() if err := w.Flush(); err != nil { return err } yson := buf.Bytes()
Most users should not use Writer directly. Use higher level Marshal() function instead.
Writer ignores all calls after the first error.
func NewWriterConfig ¶
func NewWriterConfig(inner io.Writer, c WriterConfig) (w *Writer)
func (*Writer) BeginAttrs ¶
func (w *Writer) BeginAttrs()
func (*Writer) MapKeyBytes ¶
func (*Writer) MapKeyString ¶
type WriterConfig ¶
type WriterConfig struct { Format Format Kind StreamKind }