forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/gopkg.in/yaml.v2/yaml.go

832 lines
14 KiB
Go
Raw Normal View History

2024-05-14 13:07:09 +00:00
// Package yaml implements YAML support for the Go language.
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// Source code and other details for the project are available at GitHub:
2024-05-14 13:07:09 +00:00
//
2024-06-14 08:41:36 +00:00
// https://github.com/go-yaml/yaml
2024-05-14 13:07:09 +00:00
package yaml
import (
"errors"
"fmt"
"io"
"reflect"
"strings"
"sync"
)
// MapSlice encodes and decodes as a YAML map.
2024-05-14 13:07:09 +00:00
// The order of keys is preserved when encoding and decoding.
2024-05-14 13:07:09 +00:00
type MapSlice []MapItem
// MapItem is an item in a MapSlice.
2024-05-14 13:07:09 +00:00
type MapItem struct {
Key, Value interface{}
}
// The Unmarshaler interface may be implemented by types to customize their
2024-05-14 13:07:09 +00:00
// behavior when being unmarshaled from a YAML document. The UnmarshalYAML
2024-05-14 13:07:09 +00:00
// method receives a function that may be called to unmarshal the original
2024-05-14 13:07:09 +00:00
// YAML value into a field or variable. It is safe to call the unmarshal
2024-05-14 13:07:09 +00:00
// function parameter more than once if necessary.
2024-05-14 13:07:09 +00:00
type Unmarshaler interface {
UnmarshalYAML(unmarshal func(interface{}) error) error
}
// The Marshaler interface may be implemented by types to customize their
2024-05-14 13:07:09 +00:00
// behavior when being marshaled into a YAML document. The returned value
2024-05-14 13:07:09 +00:00
// is marshaled in place of the original value implementing Marshaler.
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// If an error is returned by MarshalYAML, the marshaling procedure stops
2024-05-14 13:07:09 +00:00
// and returns with the provided error.
2024-05-14 13:07:09 +00:00
type Marshaler interface {
MarshalYAML() (interface{}, error)
}
// Unmarshal decodes the first document found within the in byte slice
2024-05-14 13:07:09 +00:00
// and assigns decoded values into the out value.
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// Maps and pointers (to a struct, string, int, etc) are accepted as out
2024-05-14 13:07:09 +00:00
// values. If an internal pointer within a struct is not initialized,
2024-05-14 13:07:09 +00:00
// the yaml package will initialize it if necessary for unmarshalling
2024-05-14 13:07:09 +00:00
// the provided data. The out parameter must not be nil.
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// The type of the decoded values should be compatible with the respective
2024-05-14 13:07:09 +00:00
// values in out. If one or more values cannot be decoded due to a type
2024-05-14 13:07:09 +00:00
// mismatches, decoding continues partially until the end of the YAML
2024-05-14 13:07:09 +00:00
// content, and a *yaml.TypeError is returned with details for all
2024-05-14 13:07:09 +00:00
// missed values.
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// Struct fields are only unmarshalled if they are exported (have an
2024-05-14 13:07:09 +00:00
// upper case first letter), and are unmarshalled using the field name
2024-05-14 13:07:09 +00:00
// lowercased as the default key. Custom keys may be defined via the
2024-05-14 13:07:09 +00:00
// "yaml" name in the field tag: the content preceding the first comma
2024-05-14 13:07:09 +00:00
// is used as the key, and the following comma-separated options are
2024-05-14 13:07:09 +00:00
// used to tweak the marshalling process (see Marshal).
2024-05-14 13:07:09 +00:00
// Conflicting names result in a runtime error.
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// For example:
2024-05-14 13:07:09 +00:00
//
2024-06-14 08:41:36 +00:00
// type T struct {
2024-06-14 08:41:36 +00:00
// F int `yaml:"a,omitempty"`
2024-06-14 08:41:36 +00:00
// B int
2024-06-14 08:41:36 +00:00
// }
2024-06-14 08:41:36 +00:00
// var t T
2024-06-14 08:41:36 +00:00
// yaml.Unmarshal([]byte("a: 1\nb: 2"), &t)
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// See the documentation of Marshal for the format of tags and a list of
2024-05-14 13:07:09 +00:00
// supported tag options.
2024-05-14 13:07:09 +00:00
func Unmarshal(in []byte, out interface{}) (err error) {
2024-05-14 13:07:09 +00:00
return unmarshal(in, out, false)
2024-05-14 13:07:09 +00:00
}
// UnmarshalStrict is like Unmarshal except that any fields that are found
2024-05-14 13:07:09 +00:00
// in the data that do not have corresponding struct members, or mapping
2024-05-14 13:07:09 +00:00
// keys that are duplicates, will result in
2024-05-14 13:07:09 +00:00
// an error.
2024-05-14 13:07:09 +00:00
func UnmarshalStrict(in []byte, out interface{}) (err error) {
2024-05-14 13:07:09 +00:00
return unmarshal(in, out, true)
2024-05-14 13:07:09 +00:00
}
// A Decoder reads and decodes YAML values from an input stream.
2024-05-14 13:07:09 +00:00
type Decoder struct {
strict bool
2024-05-14 13:07:09 +00:00
parser *parser
}
// NewDecoder returns a new decoder that reads from r.
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// The decoder introduces its own buffering and may read
2024-05-14 13:07:09 +00:00
// data from r beyond the YAML values requested.
2024-05-14 13:07:09 +00:00
func NewDecoder(r io.Reader) *Decoder {
2024-05-14 13:07:09 +00:00
return &Decoder{
2024-05-14 13:07:09 +00:00
parser: newParserFromReader(r),
}
2024-05-14 13:07:09 +00:00
}
// SetStrict sets whether strict decoding behaviour is enabled when
2024-05-14 13:07:09 +00:00
// decoding items in the data (see UnmarshalStrict). By default, decoding is not strict.
2024-05-14 13:07:09 +00:00
func (dec *Decoder) SetStrict(strict bool) {
2024-05-14 13:07:09 +00:00
dec.strict = strict
2024-05-14 13:07:09 +00:00
}
// Decode reads the next YAML-encoded value from its input
2024-05-14 13:07:09 +00:00
// and stores it in the value pointed to by v.
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// See the documentation for Unmarshal for details about the
2024-05-14 13:07:09 +00:00
// conversion of YAML into a Go value.
2024-05-14 13:07:09 +00:00
func (dec *Decoder) Decode(v interface{}) (err error) {
2024-05-14 13:07:09 +00:00
d := newDecoder(dec.strict)
2024-05-14 13:07:09 +00:00
defer handleErr(&err)
2024-05-14 13:07:09 +00:00
node := dec.parser.parse()
2024-05-14 13:07:09 +00:00
if node == nil {
2024-05-14 13:07:09 +00:00
return io.EOF
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
out := reflect.ValueOf(v)
2024-05-14 13:07:09 +00:00
if out.Kind() == reflect.Ptr && !out.IsNil() {
2024-05-14 13:07:09 +00:00
out = out.Elem()
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
d.unmarshal(node, out)
2024-05-14 13:07:09 +00:00
if len(d.terrors) > 0 {
2024-05-14 13:07:09 +00:00
return &TypeError{d.terrors}
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
return nil
2024-05-14 13:07:09 +00:00
}
func unmarshal(in []byte, out interface{}, strict bool) (err error) {
2024-05-14 13:07:09 +00:00
defer handleErr(&err)
2024-05-14 13:07:09 +00:00
d := newDecoder(strict)
2024-05-14 13:07:09 +00:00
p := newParser(in)
2024-05-14 13:07:09 +00:00
defer p.destroy()
2024-05-14 13:07:09 +00:00
node := p.parse()
2024-05-14 13:07:09 +00:00
if node != nil {
2024-05-14 13:07:09 +00:00
v := reflect.ValueOf(out)
2024-05-14 13:07:09 +00:00
if v.Kind() == reflect.Ptr && !v.IsNil() {
2024-05-14 13:07:09 +00:00
v = v.Elem()
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
d.unmarshal(node, v)
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if len(d.terrors) > 0 {
2024-05-14 13:07:09 +00:00
return &TypeError{d.terrors}
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
return nil
2024-05-14 13:07:09 +00:00
}
// Marshal serializes the value provided into a YAML document. The structure
2024-05-14 13:07:09 +00:00
// of the generated document will reflect the structure of the value itself.
2024-05-14 13:07:09 +00:00
// Maps and pointers (to struct, string, int, etc) are accepted as the in value.
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// Struct fields are only marshalled if they are exported (have an upper case
2024-05-14 13:07:09 +00:00
// first letter), and are marshalled using the field name lowercased as the
2024-05-14 13:07:09 +00:00
// default key. Custom keys may be defined via the "yaml" name in the field
2024-05-14 13:07:09 +00:00
// tag: the content preceding the first comma is used as the key, and the
2024-05-14 13:07:09 +00:00
// following comma-separated options are used to tweak the marshalling process.
2024-05-14 13:07:09 +00:00
// Conflicting names result in a runtime error.
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// The field tag format accepted is:
2024-05-14 13:07:09 +00:00
//
2024-06-14 08:41:36 +00:00
// `(...) yaml:"[<key>][,<flag1>[,<flag2>]]" (...)`
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// The following flags are currently supported:
2024-05-14 13:07:09 +00:00
//
2024-06-14 08:41:36 +00:00
// omitempty Only include the field if it's not set to the zero
2024-06-14 08:41:36 +00:00
// value for the type or to empty slices or maps.
2024-06-14 08:41:36 +00:00
// Zero valued structs will be omitted if all their public
2024-06-14 08:41:36 +00:00
// fields are zero, unless they implement an IsZero
2024-06-14 08:41:36 +00:00
// method (see the IsZeroer interface type), in which
2024-06-14 08:41:36 +00:00
// case the field will be excluded if IsZero returns true.
2024-05-14 13:07:09 +00:00
//
2024-06-14 08:41:36 +00:00
// flow Marshal using a flow style (useful for structs,
2024-06-14 08:41:36 +00:00
// sequences and maps).
2024-05-14 13:07:09 +00:00
//
2024-06-14 08:41:36 +00:00
// inline Inline the field, which must be a struct or a map,
2024-06-14 08:41:36 +00:00
// causing all of its fields or keys to be processed as if
2024-06-14 08:41:36 +00:00
// they were part of the outer struct. For maps, keys must
2024-06-14 08:41:36 +00:00
// not conflict with the yaml keys of other struct fields.
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// In addition, if the key is "-", the field is ignored.
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// For example:
2024-05-14 13:07:09 +00:00
//
2024-06-14 08:41:36 +00:00
// type T struct {
2024-06-14 08:41:36 +00:00
// F int `yaml:"a,omitempty"`
2024-06-14 08:41:36 +00:00
// B int
2024-06-14 08:41:36 +00:00
// }
2024-06-14 08:41:36 +00:00
// yaml.Marshal(&T{B: 2}) // Returns "b: 2\n"
2024-06-14 08:41:36 +00:00
// yaml.Marshal(&T{F: 1}} // Returns "a: 1\nb: 0\n"
2024-05-14 13:07:09 +00:00
func Marshal(in interface{}) (out []byte, err error) {
2024-05-14 13:07:09 +00:00
defer handleErr(&err)
2024-05-14 13:07:09 +00:00
e := newEncoder()
2024-05-14 13:07:09 +00:00
defer e.destroy()
2024-05-14 13:07:09 +00:00
e.marshalDoc("", reflect.ValueOf(in))
2024-05-14 13:07:09 +00:00
e.finish()
2024-05-14 13:07:09 +00:00
out = e.out
2024-05-14 13:07:09 +00:00
return
2024-05-14 13:07:09 +00:00
}
// An Encoder writes YAML values to an output stream.
2024-05-14 13:07:09 +00:00
type Encoder struct {
encoder *encoder
}
// NewEncoder returns a new encoder that writes to w.
2024-05-14 13:07:09 +00:00
// The Encoder should be closed after use to flush all data
2024-05-14 13:07:09 +00:00
// to w.
2024-05-14 13:07:09 +00:00
func NewEncoder(w io.Writer) *Encoder {
2024-05-14 13:07:09 +00:00
return &Encoder{
2024-05-14 13:07:09 +00:00
encoder: newEncoderWithWriter(w),
}
2024-05-14 13:07:09 +00:00
}
// Encode writes the YAML encoding of v to the stream.
2024-05-14 13:07:09 +00:00
// If multiple items are encoded to the stream, the
2024-05-14 13:07:09 +00:00
// second and subsequent document will be preceded
2024-05-14 13:07:09 +00:00
// with a "---" document separator, but the first will not.
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// See the documentation for Marshal for details about the conversion of Go
2024-05-14 13:07:09 +00:00
// values to YAML.
2024-05-14 13:07:09 +00:00
func (e *Encoder) Encode(v interface{}) (err error) {
2024-05-14 13:07:09 +00:00
defer handleErr(&err)
2024-05-14 13:07:09 +00:00
e.encoder.marshalDoc("", reflect.ValueOf(v))
2024-05-14 13:07:09 +00:00
return nil
2024-05-14 13:07:09 +00:00
}
// Close closes the encoder by writing any remaining data.
2024-05-14 13:07:09 +00:00
// It does not write a stream terminating string "...".
2024-05-14 13:07:09 +00:00
func (e *Encoder) Close() (err error) {
2024-05-14 13:07:09 +00:00
defer handleErr(&err)
2024-05-14 13:07:09 +00:00
e.encoder.finish()
2024-05-14 13:07:09 +00:00
return nil
2024-05-14 13:07:09 +00:00
}
func handleErr(err *error) {
2024-05-14 13:07:09 +00:00
if v := recover(); v != nil {
2024-05-14 13:07:09 +00:00
if e, ok := v.(yamlError); ok {
2024-05-14 13:07:09 +00:00
*err = e.err
2024-05-14 13:07:09 +00:00
} else {
2024-05-14 13:07:09 +00:00
panic(v)
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
type yamlError struct {
err error
}
func fail(err error) {
2024-05-14 13:07:09 +00:00
panic(yamlError{err})
2024-05-14 13:07:09 +00:00
}
func failf(format string, args ...interface{}) {
2024-05-14 13:07:09 +00:00
panic(yamlError{fmt.Errorf("yaml: "+format, args...)})
2024-05-14 13:07:09 +00:00
}
// A TypeError is returned by Unmarshal when one or more fields in
2024-05-14 13:07:09 +00:00
// the YAML document cannot be properly decoded into the requested
2024-05-14 13:07:09 +00:00
// types. When this error is returned, the value is still
2024-05-14 13:07:09 +00:00
// unmarshaled partially.
2024-05-14 13:07:09 +00:00
type TypeError struct {
Errors []string
}
func (e *TypeError) Error() string {
2024-05-14 13:07:09 +00:00
return fmt.Sprintf("yaml: unmarshal errors:\n %s", strings.Join(e.Errors, "\n "))
2024-05-14 13:07:09 +00:00
}
// --------------------------------------------------------------------------
2024-05-14 13:07:09 +00:00
// Maintain a mapping of keys to structure field indexes
// The code in this section was copied from mgo/bson.
// structInfo holds details for the serialization of fields of
2024-05-14 13:07:09 +00:00
// a given struct.
2024-05-14 13:07:09 +00:00
type structInfo struct {
FieldsMap map[string]fieldInfo
2024-05-14 13:07:09 +00:00
FieldsList []fieldInfo
// InlineMap is the number of the field in the struct that
2024-05-14 13:07:09 +00:00
// contains an ,inline map, or -1 if there's none.
2024-05-14 13:07:09 +00:00
InlineMap int
}
type fieldInfo struct {
Key string
Num int
2024-05-14 13:07:09 +00:00
OmitEmpty bool
Flow bool
2024-05-14 13:07:09 +00:00
// Id holds the unique field identifier, so we can cheaply
2024-05-14 13:07:09 +00:00
// check for field duplicates without maintaining an extra map.
2024-05-14 13:07:09 +00:00
Id int
// Inline holds the field index if the field is part of an inlined struct.
2024-05-14 13:07:09 +00:00
Inline []int
}
var structMap = make(map[reflect.Type]*structInfo)
2024-05-14 13:07:09 +00:00
var fieldMapMutex sync.RWMutex
func getStructInfo(st reflect.Type) (*structInfo, error) {
2024-05-14 13:07:09 +00:00
fieldMapMutex.RLock()
2024-05-14 13:07:09 +00:00
sinfo, found := structMap[st]
2024-05-14 13:07:09 +00:00
fieldMapMutex.RUnlock()
2024-05-14 13:07:09 +00:00
if found {
2024-05-14 13:07:09 +00:00
return sinfo, nil
2024-05-14 13:07:09 +00:00
}
n := st.NumField()
2024-05-14 13:07:09 +00:00
fieldsMap := make(map[string]fieldInfo)
2024-05-14 13:07:09 +00:00
fieldsList := make([]fieldInfo, 0, n)
2024-05-14 13:07:09 +00:00
inlineMap := -1
2024-05-14 13:07:09 +00:00
for i := 0; i != n; i++ {
2024-05-14 13:07:09 +00:00
field := st.Field(i)
2024-05-14 13:07:09 +00:00
if field.PkgPath != "" && !field.Anonymous {
2024-05-14 13:07:09 +00:00
continue // Private field
2024-05-14 13:07:09 +00:00
}
info := fieldInfo{Num: i}
tag := field.Tag.Get("yaml")
2024-05-14 13:07:09 +00:00
if tag == "" && strings.Index(string(field.Tag), ":") < 0 {
2024-05-14 13:07:09 +00:00
tag = string(field.Tag)
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if tag == "-" {
2024-05-14 13:07:09 +00:00
continue
2024-05-14 13:07:09 +00:00
}
inline := false
2024-05-14 13:07:09 +00:00
fields := strings.Split(tag, ",")
2024-05-14 13:07:09 +00:00
if len(fields) > 1 {
2024-05-14 13:07:09 +00:00
for _, flag := range fields[1:] {
2024-05-14 13:07:09 +00:00
switch flag {
2024-05-14 13:07:09 +00:00
case "omitempty":
2024-05-14 13:07:09 +00:00
info.OmitEmpty = true
2024-05-14 13:07:09 +00:00
case "flow":
2024-05-14 13:07:09 +00:00
info.Flow = true
2024-05-14 13:07:09 +00:00
case "inline":
2024-05-14 13:07:09 +00:00
inline = true
2024-05-14 13:07:09 +00:00
default:
2024-05-14 13:07:09 +00:00
return nil, errors.New(fmt.Sprintf("Unsupported flag %q in tag %q of type %s", flag, tag, st))
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
tag = fields[0]
2024-05-14 13:07:09 +00:00
}
if inline {
2024-05-14 13:07:09 +00:00
switch field.Type.Kind() {
2024-05-14 13:07:09 +00:00
case reflect.Map:
2024-05-14 13:07:09 +00:00
if inlineMap >= 0 {
2024-05-14 13:07:09 +00:00
return nil, errors.New("Multiple ,inline maps in struct " + st.String())
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if field.Type.Key() != reflect.TypeOf("") {
2024-05-14 13:07:09 +00:00
return nil, errors.New("Option ,inline needs a map with string keys in struct " + st.String())
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
inlineMap = info.Num
2024-05-14 13:07:09 +00:00
case reflect.Struct:
2024-05-14 13:07:09 +00:00
sinfo, err := getStructInfo(field.Type)
2024-05-14 13:07:09 +00:00
if err != nil {
2024-05-14 13:07:09 +00:00
return nil, err
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
for _, finfo := range sinfo.FieldsList {
2024-05-14 13:07:09 +00:00
if _, found := fieldsMap[finfo.Key]; found {
2024-05-14 13:07:09 +00:00
msg := "Duplicated key '" + finfo.Key + "' in struct " + st.String()
2024-05-14 13:07:09 +00:00
return nil, errors.New(msg)
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if finfo.Inline == nil {
2024-05-14 13:07:09 +00:00
finfo.Inline = []int{i, finfo.Num}
2024-05-14 13:07:09 +00:00
} else {
2024-05-14 13:07:09 +00:00
finfo.Inline = append([]int{i}, finfo.Inline...)
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
finfo.Id = len(fieldsList)
2024-05-14 13:07:09 +00:00
fieldsMap[finfo.Key] = finfo
2024-05-14 13:07:09 +00:00
fieldsList = append(fieldsList, finfo)
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
default:
2024-05-14 13:07:09 +00:00
//return nil, errors.New("Option ,inline needs a struct value or map field")
2024-05-14 13:07:09 +00:00
return nil, errors.New("Option ,inline needs a struct value field")
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
continue
2024-05-14 13:07:09 +00:00
}
if tag != "" {
2024-05-14 13:07:09 +00:00
info.Key = tag
2024-05-14 13:07:09 +00:00
} else {
2024-05-14 13:07:09 +00:00
info.Key = strings.ToLower(field.Name)
2024-05-14 13:07:09 +00:00
}
if _, found = fieldsMap[info.Key]; found {
2024-05-14 13:07:09 +00:00
msg := "Duplicated key '" + info.Key + "' in struct " + st.String()
2024-05-14 13:07:09 +00:00
return nil, errors.New(msg)
2024-05-14 13:07:09 +00:00
}
info.Id = len(fieldsList)
2024-05-14 13:07:09 +00:00
fieldsList = append(fieldsList, info)
2024-05-14 13:07:09 +00:00
fieldsMap[info.Key] = info
2024-05-14 13:07:09 +00:00
}
sinfo = &structInfo{
FieldsMap: fieldsMap,
2024-05-14 13:07:09 +00:00
FieldsList: fieldsList,
InlineMap: inlineMap,
2024-05-14 13:07:09 +00:00
}
fieldMapMutex.Lock()
2024-05-14 13:07:09 +00:00
structMap[st] = sinfo
2024-05-14 13:07:09 +00:00
fieldMapMutex.Unlock()
2024-05-14 13:07:09 +00:00
return sinfo, nil
2024-05-14 13:07:09 +00:00
}
// IsZeroer is used to check whether an object is zero to
2024-05-14 13:07:09 +00:00
// determine whether it should be omitted when marshaling
2024-05-14 13:07:09 +00:00
// with the omitempty flag. One notable implementation
2024-05-14 13:07:09 +00:00
// is time.Time.
2024-05-14 13:07:09 +00:00
type IsZeroer interface {
IsZero() bool
}
func isZero(v reflect.Value) bool {
2024-05-14 13:07:09 +00:00
kind := v.Kind()
2024-05-14 13:07:09 +00:00
if z, ok := v.Interface().(IsZeroer); ok {
2024-05-14 13:07:09 +00:00
if (kind == reflect.Ptr || kind == reflect.Interface) && v.IsNil() {
2024-05-14 13:07:09 +00:00
return true
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
return z.IsZero()
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
switch kind {
2024-05-14 13:07:09 +00:00
case reflect.String:
2024-05-14 13:07:09 +00:00
return len(v.String()) == 0
2024-05-14 13:07:09 +00:00
case reflect.Interface, reflect.Ptr:
2024-05-14 13:07:09 +00:00
return v.IsNil()
2024-05-14 13:07:09 +00:00
case reflect.Slice:
2024-05-14 13:07:09 +00:00
return v.Len() == 0
2024-05-14 13:07:09 +00:00
case reflect.Map:
2024-05-14 13:07:09 +00:00
return v.Len() == 0
2024-05-14 13:07:09 +00:00
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
2024-05-14 13:07:09 +00:00
return v.Int() == 0
2024-05-14 13:07:09 +00:00
case reflect.Float32, reflect.Float64:
2024-05-14 13:07:09 +00:00
return v.Float() == 0
2024-05-14 13:07:09 +00:00
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
2024-05-14 13:07:09 +00:00
return v.Uint() == 0
2024-05-14 13:07:09 +00:00
case reflect.Bool:
2024-05-14 13:07:09 +00:00
return !v.Bool()
2024-05-14 13:07:09 +00:00
case reflect.Struct:
2024-05-14 13:07:09 +00:00
vt := v.Type()
2024-05-14 13:07:09 +00:00
for i := v.NumField() - 1; i >= 0; i-- {
2024-05-14 13:07:09 +00:00
if vt.Field(i).PkgPath != "" {
2024-05-14 13:07:09 +00:00
continue // Private field
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if !isZero(v.Field(i)) {
2024-05-14 13:07:09 +00:00
return false
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
return true
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
return false
2024-05-14 13:07:09 +00:00
}
// FutureLineWrap globally disables line wrapping when encoding long strings.
2024-05-14 13:07:09 +00:00
// This is a temporary and thus deprecated method introduced to faciliate
2024-05-14 13:07:09 +00:00
// migration towards v3, which offers more control of line lengths on
2024-05-14 13:07:09 +00:00
// individual encodings, and has a default matching the behavior introduced
2024-05-14 13:07:09 +00:00
// by this function.
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// The default formatting of v2 was erroneously changed in v2.3.0 and reverted
2024-05-14 13:07:09 +00:00
// in v2.4.0, at which point this function was introduced to help migration.
2024-05-14 13:07:09 +00:00
func FutureLineWrap() {
2024-05-14 13:07:09 +00:00
disableLineWrapping = true
2024-05-14 13:07:09 +00:00
}