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

1216 lines
20 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Copyright (c) 2011-2019 Canonical Ltd
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Licensed under the Apache License, Version 2.0 (the "License");
2024-02-18 10:42:21 +00:00
// you may not use this file except in compliance with the License.
2024-02-18 10:42:21 +00:00
// You may obtain a copy of the License at
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// http://www.apache.org/licenses/LICENSE-2.0
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Unless required by applicable law or agreed to in writing, software
2024-02-18 10:42:21 +00:00
// distributed under the License is distributed on an "AS IS" BASIS,
2024-02-18 10:42:21 +00:00
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2024-02-18 10:42:21 +00:00
// See the License for the specific language governing permissions and
2024-02-18 10:42:21 +00:00
// limitations under the License.
// Package yaml implements YAML support for the Go language.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Source code and other details for the project are available at GitHub:
2024-02-18 10:42:21 +00:00
//
2024-06-14 08:41:36 +00:00
// https://github.com/go-yaml/yaml
2024-02-18 10:42:21 +00:00
package yaml
import (
"errors"
"fmt"
"io"
"reflect"
"strings"
"sync"
"unicode/utf8"
)
// The Unmarshaler interface may be implemented by types to customize their
2024-02-18 10:42:21 +00:00
// behavior when being unmarshaled from a YAML document.
2024-02-18 10:42:21 +00:00
type Unmarshaler interface {
UnmarshalYAML(value *Node) error
}
type obsoleteUnmarshaler interface {
UnmarshalYAML(unmarshal func(interface{}) error) error
}
// The Marshaler interface may be implemented by types to customize their
2024-02-18 10:42:21 +00:00
// behavior when being marshaled into a YAML document. The returned value
2024-02-18 10:42:21 +00:00
// is marshaled in place of the original value implementing Marshaler.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// If an error is returned by MarshalYAML, the marshaling procedure stops
2024-02-18 10:42:21 +00:00
// and returns with the provided error.
2024-02-18 10:42:21 +00:00
type Marshaler interface {
MarshalYAML() (interface{}, error)
}
// Unmarshal decodes the first document found within the in byte slice
2024-02-18 10:42:21 +00:00
// and assigns decoded values into the out value.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Maps and pointers (to a struct, string, int, etc) are accepted as out
2024-02-18 10:42:21 +00:00
// values. If an internal pointer within a struct is not initialized,
2024-02-18 10:42:21 +00:00
// the yaml package will initialize it if necessary for unmarshalling
2024-02-18 10:42:21 +00:00
// the provided data. The out parameter must not be nil.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// The type of the decoded values should be compatible with the respective
2024-02-18 10:42:21 +00:00
// values in out. If one or more values cannot be decoded due to a type
2024-02-18 10:42:21 +00:00
// mismatches, decoding continues partially until the end of the YAML
2024-02-18 10:42:21 +00:00
// content, and a *yaml.TypeError is returned with details for all
2024-02-18 10:42:21 +00:00
// missed values.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Struct fields are only unmarshalled if they are exported (have an
2024-02-18 10:42:21 +00:00
// upper case first letter), and are unmarshalled using the field name
2024-02-18 10:42:21 +00:00
// lowercased as the default key. Custom keys may be defined via the
2024-02-18 10:42:21 +00:00
// "yaml" name in the field tag: the content preceding the first comma
2024-02-18 10:42:21 +00:00
// is used as the key, and the following comma-separated options are
2024-02-18 10:42:21 +00:00
// used to tweak the marshalling process (see Marshal).
2024-02-18 10:42:21 +00:00
// Conflicting names result in a runtime error.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// For example:
2024-02-18 10:42:21 +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-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// See the documentation of Marshal for the format of tags and a list of
2024-02-18 10:42:21 +00:00
// supported tag options.
2024-02-18 10:42:21 +00:00
func Unmarshal(in []byte, out interface{}) (err error) {
2024-02-18 10:42:21 +00:00
return unmarshal(in, out, false)
2024-02-18 10:42:21 +00:00
}
// A Decoder reads and decodes YAML values from an input stream.
2024-02-18 10:42:21 +00:00
type Decoder struct {
parser *parser
2024-02-18 10:42:21 +00:00
knownFields bool
}
// NewDecoder returns a new decoder that reads from r.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// The decoder introduces its own buffering and may read
2024-02-18 10:42:21 +00:00
// data from r beyond the YAML values requested.
2024-02-18 10:42:21 +00:00
func NewDecoder(r io.Reader) *Decoder {
2024-02-18 10:42:21 +00:00
return &Decoder{
2024-02-18 10:42:21 +00:00
parser: newParserFromReader(r),
}
2024-02-18 10:42:21 +00:00
}
// KnownFields ensures that the keys in decoded mappings to
2024-02-18 10:42:21 +00:00
// exist as fields in the struct being decoded into.
2024-02-18 10:42:21 +00:00
func (dec *Decoder) KnownFields(enable bool) {
2024-02-18 10:42:21 +00:00
dec.knownFields = enable
2024-02-18 10:42:21 +00:00
}
// Decode reads the next YAML-encoded value from its input
2024-02-18 10:42:21 +00:00
// and stores it in the value pointed to by v.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// See the documentation for Unmarshal for details about the
2024-02-18 10:42:21 +00:00
// conversion of YAML into a Go value.
2024-02-18 10:42:21 +00:00
func (dec *Decoder) Decode(v interface{}) (err error) {
2024-02-18 10:42:21 +00:00
d := newDecoder()
2024-02-18 10:42:21 +00:00
d.knownFields = dec.knownFields
2024-02-18 10:42:21 +00:00
defer handleErr(&err)
2024-02-18 10:42:21 +00:00
node := dec.parser.parse()
2024-02-18 10:42:21 +00:00
if node == nil {
2024-02-18 10:42:21 +00:00
return io.EOF
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
out := reflect.ValueOf(v)
2024-02-18 10:42:21 +00:00
if out.Kind() == reflect.Ptr && !out.IsNil() {
2024-02-18 10:42:21 +00:00
out = out.Elem()
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
d.unmarshal(node, out)
2024-02-18 10:42:21 +00:00
if len(d.terrors) > 0 {
2024-02-18 10:42:21 +00:00
return &TypeError{d.terrors}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
// Decode decodes the node and stores its data into the value pointed to by v.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// See the documentation for Unmarshal for details about the
2024-02-18 10:42:21 +00:00
// conversion of YAML into a Go value.
2024-02-18 10:42:21 +00:00
func (n *Node) Decode(v interface{}) (err error) {
2024-02-18 10:42:21 +00:00
d := newDecoder()
2024-02-18 10:42:21 +00:00
defer handleErr(&err)
2024-02-18 10:42:21 +00:00
out := reflect.ValueOf(v)
2024-02-18 10:42:21 +00:00
if out.Kind() == reflect.Ptr && !out.IsNil() {
2024-02-18 10:42:21 +00:00
out = out.Elem()
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
d.unmarshal(n, out)
2024-02-18 10:42:21 +00:00
if len(d.terrors) > 0 {
2024-02-18 10:42:21 +00:00
return &TypeError{d.terrors}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
func unmarshal(in []byte, out interface{}, strict bool) (err error) {
2024-02-18 10:42:21 +00:00
defer handleErr(&err)
2024-02-18 10:42:21 +00:00
d := newDecoder()
2024-02-18 10:42:21 +00:00
p := newParser(in)
2024-02-18 10:42:21 +00:00
defer p.destroy()
2024-02-18 10:42:21 +00:00
node := p.parse()
2024-02-18 10:42:21 +00:00
if node != nil {
2024-02-18 10:42:21 +00:00
v := reflect.ValueOf(out)
2024-02-18 10:42:21 +00:00
if v.Kind() == reflect.Ptr && !v.IsNil() {
2024-02-18 10:42:21 +00:00
v = v.Elem()
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
d.unmarshal(node, v)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if len(d.terrors) > 0 {
2024-02-18 10:42:21 +00:00
return &TypeError{d.terrors}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
// Marshal serializes the value provided into a YAML document. The structure
2024-02-18 10:42:21 +00:00
// of the generated document will reflect the structure of the value itself.
2024-02-18 10:42:21 +00:00
// Maps and pointers (to struct, string, int, etc) are accepted as the in value.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Struct fields are only marshalled if they are exported (have an upper case
2024-02-18 10:42:21 +00:00
// first letter), and are marshalled using the field name lowercased as the
2024-02-18 10:42:21 +00:00
// default key. Custom keys may be defined via the "yaml" name in the field
2024-02-18 10:42:21 +00:00
// tag: the content preceding the first comma is used as the key, and the
2024-02-18 10:42:21 +00:00
// following comma-separated options are used to tweak the marshalling process.
2024-02-18 10:42:21 +00:00
// Conflicting names result in a runtime error.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// The field tag format accepted is:
2024-02-18 10:42:21 +00:00
//
2024-06-14 08:41:36 +00:00
// `(...) yaml:"[<key>][,<flag1>[,<flag2>]]" (...)`
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// The following flags are currently supported:
2024-02-18 10:42:21 +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-02-18 10:42:21 +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-02-18 10:42:21 +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-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// In addition, if the key is "-", the field is ignored.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// For example:
2024-02-18 10:42:21 +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-02-18 10:42:21 +00:00
func Marshal(in interface{}) (out []byte, err error) {
2024-02-18 10:42:21 +00:00
defer handleErr(&err)
2024-02-18 10:42:21 +00:00
e := newEncoder()
2024-02-18 10:42:21 +00:00
defer e.destroy()
2024-02-18 10:42:21 +00:00
e.marshalDoc("", reflect.ValueOf(in))
2024-02-18 10:42:21 +00:00
e.finish()
2024-02-18 10:42:21 +00:00
out = e.out
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
// An Encoder writes YAML values to an output stream.
2024-02-18 10:42:21 +00:00
type Encoder struct {
encoder *encoder
}
// NewEncoder returns a new encoder that writes to w.
2024-02-18 10:42:21 +00:00
// The Encoder should be closed after use to flush all data
2024-02-18 10:42:21 +00:00
// to w.
2024-02-18 10:42:21 +00:00
func NewEncoder(w io.Writer) *Encoder {
2024-02-18 10:42:21 +00:00
return &Encoder{
2024-02-18 10:42:21 +00:00
encoder: newEncoderWithWriter(w),
}
2024-02-18 10:42:21 +00:00
}
// Encode writes the YAML encoding of v to the stream.
2024-02-18 10:42:21 +00:00
// If multiple items are encoded to the stream, the
2024-02-18 10:42:21 +00:00
// second and subsequent document will be preceded
2024-02-18 10:42:21 +00:00
// with a "---" document separator, but the first will not.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// See the documentation for Marshal for details about the conversion of Go
2024-02-18 10:42:21 +00:00
// values to YAML.
2024-02-18 10:42:21 +00:00
func (e *Encoder) Encode(v interface{}) (err error) {
2024-02-18 10:42:21 +00:00
defer handleErr(&err)
2024-02-18 10:42:21 +00:00
e.encoder.marshalDoc("", reflect.ValueOf(v))
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
// Encode encodes value v and stores its representation in n.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// See the documentation for Marshal for details about the
2024-02-18 10:42:21 +00:00
// conversion of Go values into YAML.
2024-02-18 10:42:21 +00:00
func (n *Node) Encode(v interface{}) (err error) {
2024-02-18 10:42:21 +00:00
defer handleErr(&err)
2024-02-18 10:42:21 +00:00
e := newEncoder()
2024-02-18 10:42:21 +00:00
defer e.destroy()
2024-02-18 10:42:21 +00:00
e.marshalDoc("", reflect.ValueOf(v))
2024-02-18 10:42:21 +00:00
e.finish()
2024-02-18 10:42:21 +00:00
p := newParser(e.out)
2024-02-18 10:42:21 +00:00
p.textless = true
2024-02-18 10:42:21 +00:00
defer p.destroy()
2024-02-18 10:42:21 +00:00
doc := p.parse()
2024-02-18 10:42:21 +00:00
*n = *doc.Content[0]
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
// SetIndent changes the used indentation used when encoding.
2024-02-18 10:42:21 +00:00
func (e *Encoder) SetIndent(spaces int) {
2024-02-18 10:42:21 +00:00
if spaces < 0 {
2024-02-18 10:42:21 +00:00
panic("yaml: cannot indent to a negative number of spaces")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
e.encoder.indent = spaces
2024-02-18 10:42:21 +00:00
}
// Close closes the encoder by writing any remaining data.
2024-02-18 10:42:21 +00:00
// It does not write a stream terminating string "...".
2024-02-18 10:42:21 +00:00
func (e *Encoder) Close() (err error) {
2024-02-18 10:42:21 +00:00
defer handleErr(&err)
2024-02-18 10:42:21 +00:00
e.encoder.finish()
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
func handleErr(err *error) {
2024-02-18 10:42:21 +00:00
if v := recover(); v != nil {
2024-02-18 10:42:21 +00:00
if e, ok := v.(yamlError); ok {
2024-02-18 10:42:21 +00:00
*err = e.err
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
panic(v)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
type yamlError struct {
err error
}
func fail(err error) {
2024-02-18 10:42:21 +00:00
panic(yamlError{err})
2024-02-18 10:42:21 +00:00
}
func failf(format string, args ...interface{}) {
2024-02-18 10:42:21 +00:00
panic(yamlError{fmt.Errorf("yaml: "+format, args...)})
2024-02-18 10:42:21 +00:00
}
// A TypeError is returned by Unmarshal when one or more fields in
2024-02-18 10:42:21 +00:00
// the YAML document cannot be properly decoded into the requested
2024-02-18 10:42:21 +00:00
// types. When this error is returned, the value is still
2024-02-18 10:42:21 +00:00
// unmarshaled partially.
2024-02-18 10:42:21 +00:00
type TypeError struct {
Errors []string
}
func (e *TypeError) Error() string {
2024-02-18 10:42:21 +00:00
return fmt.Sprintf("yaml: unmarshal errors:\n %s", strings.Join(e.Errors, "\n "))
2024-02-18 10:42:21 +00:00
}
type Kind uint32
const (
DocumentNode Kind = 1 << iota
2024-02-18 10:42:21 +00:00
SequenceNode
2024-02-18 10:42:21 +00:00
MappingNode
2024-02-18 10:42:21 +00:00
ScalarNode
2024-02-18 10:42:21 +00:00
AliasNode
)
type Style uint32
const (
TaggedStyle Style = 1 << iota
2024-02-18 10:42:21 +00:00
DoubleQuotedStyle
2024-02-18 10:42:21 +00:00
SingleQuotedStyle
2024-02-18 10:42:21 +00:00
LiteralStyle
2024-02-18 10:42:21 +00:00
FoldedStyle
2024-02-18 10:42:21 +00:00
FlowStyle
)
// Node represents an element in the YAML document hierarchy. While documents
2024-02-18 10:42:21 +00:00
// are typically encoded and decoded into higher level types, such as structs
2024-02-18 10:42:21 +00:00
// and maps, Node is an intermediate representation that allows detailed
2024-02-18 10:42:21 +00:00
// control over the content being decoded or encoded.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// It's worth noting that although Node offers access into details such as
2024-02-18 10:42:21 +00:00
// line numbers, colums, and comments, the content when re-encoded will not
2024-02-18 10:42:21 +00:00
// have its original textual representation preserved. An effort is made to
2024-02-18 10:42:21 +00:00
// render the data plesantly, and to preserve comments near the data they
2024-02-18 10:42:21 +00:00
// describe, though.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Values that make use of the Node type interact with the yaml package in the
2024-02-18 10:42:21 +00:00
// same way any other type would do, by encoding and decoding yaml data
2024-02-18 10:42:21 +00:00
// directly or indirectly into them.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// For example:
2024-02-18 10:42:21 +00:00
//
2024-06-14 08:41:36 +00:00
// var person struct {
2024-06-14 08:41:36 +00:00
// Name string
2024-06-14 08:41:36 +00:00
// Address yaml.Node
2024-06-14 08:41:36 +00:00
// }
2024-06-14 08:41:36 +00:00
// err := yaml.Unmarshal(data, &person)
2024-02-18 10:42:21 +00:00
//
2024-06-14 08:41:36 +00:00
// Or by itself:
2024-05-14 13:07:09 +00:00
//
2024-06-14 08:41:36 +00:00
// var person Node
2024-06-14 08:41:36 +00:00
// err := yaml.Unmarshal(data, &person)
2024-02-18 10:42:21 +00:00
type Node struct {
2024-02-18 10:42:21 +00:00
// Kind defines whether the node is a document, a mapping, a sequence,
2024-02-18 10:42:21 +00:00
// a scalar value, or an alias to another node. The specific data type of
2024-02-18 10:42:21 +00:00
// scalar nodes may be obtained via the ShortTag and LongTag methods.
2024-06-14 08:41:36 +00:00
Kind Kind
2024-02-18 10:42:21 +00:00
// Style allows customizing the apperance of the node in the tree.
2024-02-18 10:42:21 +00:00
Style Style
// Tag holds the YAML tag defining the data type for the value.
2024-02-18 10:42:21 +00:00
// When decoding, this field will always be set to the resolved tag,
2024-02-18 10:42:21 +00:00
// even when it wasn't explicitly provided in the YAML content.
2024-02-18 10:42:21 +00:00
// When encoding, if this field is unset the value type will be
2024-02-18 10:42:21 +00:00
// implied from the node properties, and if it is set, it will only
2024-02-18 10:42:21 +00:00
// be serialized into the representation if TaggedStyle is used or
2024-02-18 10:42:21 +00:00
// the implicit tag diverges from the provided one.
2024-02-18 10:42:21 +00:00
Tag string
// Value holds the unescaped and unquoted represenation of the value.
2024-02-18 10:42:21 +00:00
Value string
// Anchor holds the anchor name for this node, which allows aliases to point to it.
2024-02-18 10:42:21 +00:00
Anchor string
// Alias holds the node that this alias points to. Only valid when Kind is AliasNode.
2024-02-18 10:42:21 +00:00
Alias *Node
// Content holds contained nodes for documents, mappings, and sequences.
2024-02-18 10:42:21 +00:00
Content []*Node
// HeadComment holds any comments in the lines preceding the node and
2024-02-18 10:42:21 +00:00
// not separated by an empty line.
2024-02-18 10:42:21 +00:00
HeadComment string
// LineComment holds any comments at the end of the line where the node is in.
2024-02-18 10:42:21 +00:00
LineComment string
// FootComment holds any comments following the node and before empty lines.
2024-02-18 10:42:21 +00:00
FootComment string
// Line and Column hold the node position in the decoded YAML text.
2024-02-18 10:42:21 +00:00
// These fields are not respected when encoding the node.
Line int
2024-02-18 10:42:21 +00:00
Column int
}
// IsZero returns whether the node has all of its fields unset.
2024-02-18 10:42:21 +00:00
func (n *Node) IsZero() bool {
2024-02-18 10:42:21 +00:00
return n.Kind == 0 && n.Style == 0 && n.Tag == "" && n.Value == "" && n.Anchor == "" && n.Alias == nil && n.Content == nil &&
2024-02-18 10:42:21 +00:00
n.HeadComment == "" && n.LineComment == "" && n.FootComment == "" && n.Line == 0 && n.Column == 0
2024-02-18 10:42:21 +00:00
}
// LongTag returns the long form of the tag that indicates the data type for
2024-02-18 10:42:21 +00:00
// the node. If the Tag field isn't explicitly defined, one will be computed
2024-02-18 10:42:21 +00:00
// based on the node properties.
2024-02-18 10:42:21 +00:00
func (n *Node) LongTag() string {
2024-02-18 10:42:21 +00:00
return longTag(n.ShortTag())
2024-02-18 10:42:21 +00:00
}
// ShortTag returns the short form of the YAML tag that indicates data type for
2024-02-18 10:42:21 +00:00
// the node. If the Tag field isn't explicitly defined, one will be computed
2024-02-18 10:42:21 +00:00
// based on the node properties.
2024-02-18 10:42:21 +00:00
func (n *Node) ShortTag() string {
2024-02-18 10:42:21 +00:00
if n.indicatedString() {
2024-02-18 10:42:21 +00:00
return strTag
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if n.Tag == "" || n.Tag == "!" {
2024-02-18 10:42:21 +00:00
switch n.Kind {
2024-02-18 10:42:21 +00:00
case MappingNode:
2024-02-18 10:42:21 +00:00
return mapTag
2024-02-18 10:42:21 +00:00
case SequenceNode:
2024-02-18 10:42:21 +00:00
return seqTag
2024-02-18 10:42:21 +00:00
case AliasNode:
2024-02-18 10:42:21 +00:00
if n.Alias != nil {
2024-02-18 10:42:21 +00:00
return n.Alias.ShortTag()
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
case ScalarNode:
2024-02-18 10:42:21 +00:00
tag, _ := resolve("", n.Value)
2024-02-18 10:42:21 +00:00
return tag
2024-02-18 10:42:21 +00:00
case 0:
2024-02-18 10:42:21 +00:00
// Special case to make the zero value convenient.
2024-02-18 10:42:21 +00:00
if n.IsZero() {
2024-02-18 10:42:21 +00:00
return nullTag
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return ""
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return shortTag(n.Tag)
2024-02-18 10:42:21 +00:00
}
func (n *Node) indicatedString() bool {
2024-02-18 10:42:21 +00:00
return n.Kind == ScalarNode &&
2024-02-18 10:42:21 +00:00
(shortTag(n.Tag) == strTag ||
2024-02-18 10:42:21 +00:00
(n.Tag == "" || n.Tag == "!") && n.Style&(SingleQuotedStyle|DoubleQuotedStyle|LiteralStyle|FoldedStyle) != 0)
2024-02-18 10:42:21 +00:00
}
// SetString is a convenience function that sets the node to a string value
2024-02-18 10:42:21 +00:00
// and defines its style in a pleasant way depending on its content.
2024-02-18 10:42:21 +00:00
func (n *Node) SetString(s string) {
2024-02-18 10:42:21 +00:00
n.Kind = ScalarNode
2024-02-18 10:42:21 +00:00
if utf8.ValidString(s) {
2024-02-18 10:42:21 +00:00
n.Value = s
2024-02-18 10:42:21 +00:00
n.Tag = strTag
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
n.Value = encodeBase64(s)
2024-02-18 10:42:21 +00:00
n.Tag = binaryTag
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if strings.Contains(n.Value, "\n") {
2024-02-18 10:42:21 +00:00
n.Style = LiteralStyle
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// --------------------------------------------------------------------------
2024-02-18 10:42:21 +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-02-18 10:42:21 +00:00
// a given struct.
2024-02-18 10:42:21 +00:00
type structInfo struct {
FieldsMap map[string]fieldInfo
2024-02-18 10:42:21 +00:00
FieldsList []fieldInfo
// InlineMap is the number of the field in the struct that
2024-02-18 10:42:21 +00:00
// contains an ,inline map, or -1 if there's none.
2024-02-18 10:42:21 +00:00
InlineMap int
// InlineUnmarshalers holds indexes to inlined fields that
2024-02-18 10:42:21 +00:00
// contain unmarshaler values.
2024-02-18 10:42:21 +00:00
InlineUnmarshalers [][]int
}
type fieldInfo struct {
Key string
Num int
2024-02-18 10:42:21 +00:00
OmitEmpty bool
Flow bool
2024-02-18 10:42:21 +00:00
// Id holds the unique field identifier, so we can cheaply
2024-02-18 10:42:21 +00:00
// check for field duplicates without maintaining an extra map.
2024-02-18 10:42:21 +00:00
Id int
// Inline holds the field index if the field is part of an inlined struct.
2024-02-18 10:42:21 +00:00
Inline []int
}
var structMap = make(map[reflect.Type]*structInfo)
2024-02-18 10:42:21 +00:00
var fieldMapMutex sync.RWMutex
2024-02-18 10:42:21 +00:00
var unmarshalerType reflect.Type
func init() {
2024-02-18 10:42:21 +00:00
var v Unmarshaler
2024-02-18 10:42:21 +00:00
unmarshalerType = reflect.ValueOf(&v).Elem().Type()
2024-02-18 10:42:21 +00:00
}
func getStructInfo(st reflect.Type) (*structInfo, error) {
2024-02-18 10:42:21 +00:00
fieldMapMutex.RLock()
2024-02-18 10:42:21 +00:00
sinfo, found := structMap[st]
2024-02-18 10:42:21 +00:00
fieldMapMutex.RUnlock()
2024-02-18 10:42:21 +00:00
if found {
2024-02-18 10:42:21 +00:00
return sinfo, nil
2024-02-18 10:42:21 +00:00
}
n := st.NumField()
2024-02-18 10:42:21 +00:00
fieldsMap := make(map[string]fieldInfo)
2024-02-18 10:42:21 +00:00
fieldsList := make([]fieldInfo, 0, n)
2024-02-18 10:42:21 +00:00
inlineMap := -1
2024-02-18 10:42:21 +00:00
inlineUnmarshalers := [][]int(nil)
2024-02-18 10:42:21 +00:00
for i := 0; i != n; i++ {
2024-02-18 10:42:21 +00:00
field := st.Field(i)
2024-02-18 10:42:21 +00:00
if field.PkgPath != "" && !field.Anonymous {
2024-02-18 10:42:21 +00:00
continue // Private field
2024-02-18 10:42:21 +00:00
}
info := fieldInfo{Num: i}
tag := field.Tag.Get("yaml")
2024-02-18 10:42:21 +00:00
if tag == "" && strings.Index(string(field.Tag), ":") < 0 {
2024-02-18 10:42:21 +00:00
tag = string(field.Tag)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if tag == "-" {
2024-02-18 10:42:21 +00:00
continue
2024-02-18 10:42:21 +00:00
}
inline := false
2024-02-18 10:42:21 +00:00
fields := strings.Split(tag, ",")
2024-02-18 10:42:21 +00:00
if len(fields) > 1 {
2024-02-18 10:42:21 +00:00
for _, flag := range fields[1:] {
2024-02-18 10:42:21 +00:00
switch flag {
2024-02-18 10:42:21 +00:00
case "omitempty":
2024-02-18 10:42:21 +00:00
info.OmitEmpty = true
2024-02-18 10:42:21 +00:00
case "flow":
2024-02-18 10:42:21 +00:00
info.Flow = true
2024-02-18 10:42:21 +00:00
case "inline":
2024-02-18 10:42:21 +00:00
inline = true
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
return nil, errors.New(fmt.Sprintf("unsupported flag %q in tag %q of type %s", flag, tag, st))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
tag = fields[0]
2024-02-18 10:42:21 +00:00
}
if inline {
2024-02-18 10:42:21 +00:00
switch field.Type.Kind() {
2024-02-18 10:42:21 +00:00
case reflect.Map:
2024-02-18 10:42:21 +00:00
if inlineMap >= 0 {
2024-02-18 10:42:21 +00:00
return nil, errors.New("multiple ,inline maps in struct " + st.String())
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if field.Type.Key() != reflect.TypeOf("") {
2024-02-18 10:42:21 +00:00
return nil, errors.New("option ,inline needs a map with string keys in struct " + st.String())
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
inlineMap = info.Num
2024-02-18 10:42:21 +00:00
case reflect.Struct, reflect.Ptr:
2024-02-18 10:42:21 +00:00
ftype := field.Type
2024-02-18 10:42:21 +00:00
for ftype.Kind() == reflect.Ptr {
2024-02-18 10:42:21 +00:00
ftype = ftype.Elem()
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if ftype.Kind() != reflect.Struct {
2024-02-18 10:42:21 +00:00
return nil, errors.New("option ,inline may only be used on a struct or map field")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if reflect.PtrTo(ftype).Implements(unmarshalerType) {
2024-02-18 10:42:21 +00:00
inlineUnmarshalers = append(inlineUnmarshalers, []int{i})
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
sinfo, err := getStructInfo(ftype)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
for _, index := range sinfo.InlineUnmarshalers {
2024-02-18 10:42:21 +00:00
inlineUnmarshalers = append(inlineUnmarshalers, append([]int{i}, index...))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
for _, finfo := range sinfo.FieldsList {
2024-02-18 10:42:21 +00:00
if _, found := fieldsMap[finfo.Key]; found {
2024-02-18 10:42:21 +00:00
msg := "duplicated key '" + finfo.Key + "' in struct " + st.String()
2024-02-18 10:42:21 +00:00
return nil, errors.New(msg)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if finfo.Inline == nil {
2024-02-18 10:42:21 +00:00
finfo.Inline = []int{i, finfo.Num}
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
finfo.Inline = append([]int{i}, finfo.Inline...)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
finfo.Id = len(fieldsList)
2024-02-18 10:42:21 +00:00
fieldsMap[finfo.Key] = finfo
2024-02-18 10:42:21 +00:00
fieldsList = append(fieldsList, finfo)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
return nil, errors.New("option ,inline may only be used on a struct or map field")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
continue
2024-02-18 10:42:21 +00:00
}
if tag != "" {
2024-02-18 10:42:21 +00:00
info.Key = tag
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
info.Key = strings.ToLower(field.Name)
2024-02-18 10:42:21 +00:00
}
if _, found = fieldsMap[info.Key]; found {
2024-02-18 10:42:21 +00:00
msg := "duplicated key '" + info.Key + "' in struct " + st.String()
2024-02-18 10:42:21 +00:00
return nil, errors.New(msg)
2024-02-18 10:42:21 +00:00
}
info.Id = len(fieldsList)
2024-02-18 10:42:21 +00:00
fieldsList = append(fieldsList, info)
2024-02-18 10:42:21 +00:00
fieldsMap[info.Key] = info
2024-02-18 10:42:21 +00:00
}
sinfo = &structInfo{
FieldsMap: fieldsMap,
FieldsList: fieldsList,
InlineMap: inlineMap,
2024-02-18 10:42:21 +00:00
InlineUnmarshalers: inlineUnmarshalers,
}
fieldMapMutex.Lock()
2024-02-18 10:42:21 +00:00
structMap[st] = sinfo
2024-02-18 10:42:21 +00:00
fieldMapMutex.Unlock()
2024-02-18 10:42:21 +00:00
return sinfo, nil
2024-02-18 10:42:21 +00:00
}
// IsZeroer is used to check whether an object is zero to
2024-02-18 10:42:21 +00:00
// determine whether it should be omitted when marshaling
2024-02-18 10:42:21 +00:00
// with the omitempty flag. One notable implementation
2024-02-18 10:42:21 +00:00
// is time.Time.
2024-02-18 10:42:21 +00:00
type IsZeroer interface {
IsZero() bool
}
func isZero(v reflect.Value) bool {
2024-02-18 10:42:21 +00:00
kind := v.Kind()
2024-02-18 10:42:21 +00:00
if z, ok := v.Interface().(IsZeroer); ok {
2024-02-18 10:42:21 +00:00
if (kind == reflect.Ptr || kind == reflect.Interface) && v.IsNil() {
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return z.IsZero()
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
switch kind {
2024-02-18 10:42:21 +00:00
case reflect.String:
2024-02-18 10:42:21 +00:00
return len(v.String()) == 0
2024-02-18 10:42:21 +00:00
case reflect.Interface, reflect.Ptr:
2024-02-18 10:42:21 +00:00
return v.IsNil()
2024-02-18 10:42:21 +00:00
case reflect.Slice:
2024-02-18 10:42:21 +00:00
return v.Len() == 0
2024-02-18 10:42:21 +00:00
case reflect.Map:
2024-02-18 10:42:21 +00:00
return v.Len() == 0
2024-02-18 10:42:21 +00:00
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
2024-02-18 10:42:21 +00:00
return v.Int() == 0
2024-02-18 10:42:21 +00:00
case reflect.Float32, reflect.Float64:
2024-02-18 10:42:21 +00:00
return v.Float() == 0
2024-02-18 10:42:21 +00:00
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
2024-02-18 10:42:21 +00:00
return v.Uint() == 0
2024-02-18 10:42:21 +00:00
case reflect.Bool:
2024-02-18 10:42:21 +00:00
return !v.Bool()
2024-02-18 10:42:21 +00:00
case reflect.Struct:
2024-02-18 10:42:21 +00:00
vt := v.Type()
2024-02-18 10:42:21 +00:00
for i := v.NumField() - 1; i >= 0; i-- {
2024-02-18 10:42:21 +00:00
if vt.Field(i).PkgPath != "" {
2024-02-18 10:42:21 +00:00
continue // Private field
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !isZero(v.Field(i)) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}