Package yaml implements YAML support for the Go language. Source code and other details for the project are available at GitHub: https://github.com/go-yaml/yaml
package yaml

import (
	
	
	
	
	
	
)
MapSlice encodes and decodes as a YAML map. The order of keys is preserved when encoding and decoding.
MapItem is an item in a MapSlice.
type MapItem struct {
	Key, Value interface{}
}
The Unmarshaler interface may be implemented by types to customize their behavior when being unmarshaled from a YAML document. The UnmarshalYAML method receives a function that may be called to unmarshal the original YAML value into a field or variable. It is safe to call the unmarshal function parameter more than once if necessary.
type Unmarshaler interface {
	UnmarshalYAML(unmarshal func(interface{}) error) error
}
The Marshaler interface may be implemented by types to customize their behavior when being marshaled into a YAML document. The returned value is marshaled in place of the original value implementing Marshaler. If an error is returned by MarshalYAML, the marshaling procedure stops and returns with the provided error.
type Marshaler interface {
	MarshalYAML() (interface{}, error)
}
Unmarshal decodes the first document found within the in byte slice and assigns decoded values into the out value. Maps and pointers (to a struct, string, int, etc) are accepted as out values. If an internal pointer within a struct is not initialized, the yaml package will initialize it if necessary for unmarshalling the provided data. The out parameter must not be nil. The type of the decoded values should be compatible with the respective values in out. If one or more values cannot be decoded due to a type mismatches, decoding continues partially until the end of the YAML content, and a *yaml.TypeError is returned with details for all missed values. Struct fields are only unmarshalled if they are exported (have an upper case first letter), and are unmarshalled using the field name lowercased as the default key. Custom keys may be defined via the "yaml" name in the field tag: the content preceding the first comma is used as the key, and the following comma-separated options are used to tweak the marshalling process (see Marshal). Conflicting names result in a runtime error. For example: type T struct { F int `yaml:"a,omitempty"` B int } var t T yaml.Unmarshal([]byte("a: 1\nb: 2"), &t) See the documentation of Marshal for the format of tags and a list of supported tag options.
func ( []byte,  interface{}) ( error) {
	return unmarshal(, , false)
}
UnmarshalStrict is like Unmarshal except that any fields that are found in the data that do not have corresponding struct members, or mapping keys that are duplicates, will result in an error.
func ( []byte,  interface{}) ( error) {
	return unmarshal(, , true)
}
A Decoder reads and decodes YAML values from an input stream.
type Decoder struct {
	strict bool
	parser *parser
}
NewDecoder returns a new decoder that reads from r. The decoder introduces its own buffering and may read data from r beyond the YAML values requested.
SetStrict sets whether strict decoding behaviour is enabled when decoding items in the data (see UnmarshalStrict). By default, decoding is not strict.
func ( *Decoder) ( bool) {
	.strict = 
}
Decode reads the next YAML-encoded value from its input and stores it in the value pointed to by v. See the documentation for Unmarshal for details about the conversion of YAML into a Go value.
func ( *Decoder) ( interface{}) ( error) {
	 := newDecoder(.strict)
	defer handleErr(&)
	 := .parser.parse()
	if  == nil {
		return io.EOF
	}
	 := reflect.ValueOf()
	if .Kind() == reflect.Ptr && !.IsNil() {
		 = .Elem()
	}
	.unmarshal(, )
	if len(.terrors) > 0 {
		return &TypeError{.terrors}
	}
	return nil
}

func ( []byte,  interface{},  bool) ( error) {
	defer handleErr(&)
	 := newDecoder()
	 := newParser()
	defer .destroy()
	 := .parse()
	if  != nil {
		 := reflect.ValueOf()
		if .Kind() == reflect.Ptr && !.IsNil() {
			 = .Elem()
		}
		.unmarshal(, )
	}
	if len(.terrors) > 0 {
		return &TypeError{.terrors}
	}
	return nil
}
Marshal serializes the value provided into a YAML document. The structure of the generated document will reflect the structure of the value itself. Maps and pointers (to struct, string, int, etc) are accepted as the in value. Struct fields are only marshalled if they are exported (have an upper case first letter), and are marshalled using the field name lowercased as the default key. Custom keys may be defined via the "yaml" name in the field tag: the content preceding the first comma is used as the key, and the following comma-separated options are used to tweak the marshalling process. Conflicting names result in a runtime error. The field tag format accepted is: `(...) yaml:"[<key>][,<flag1>[,<flag2>]]" (...)` The following flags are currently supported: omitempty Only include the field if it's not set to the zero value for the type or to empty slices or maps. Zero valued structs will be omitted if all their public fields are zero, unless they implement an IsZero method (see the IsZeroer interface type), in which case the field will be included if that method returns true. flow Marshal using a flow style (useful for structs, sequences and maps). inline Inline the field, which must be a struct or a map, causing all of its fields or keys to be processed as if they were part of the outer struct. For maps, keys must not conflict with the yaml keys of other struct fields. In addition, if the key is "-", the field is ignored. For example: type T struct { F int `yaml:"a,omitempty"` B int } yaml.Marshal(&T{B: 2}) // Returns "b: 2\n" yaml.Marshal(&T{F: 1}} // Returns "a: 1\nb: 0\n"
func ( interface{}) ( []byte,  error) {
	defer handleErr(&)
	 := newEncoder()
	defer .destroy()
	.marshalDoc("", reflect.ValueOf())
	.finish()
	 = .out
	return
}
An Encoder writes YAML values to an output stream.
type Encoder struct {
	encoder *encoder
}
NewEncoder returns a new encoder that writes to w. The Encoder should be closed after use to flush all data to w.
Encode writes the YAML encoding of v to the stream. If multiple items are encoded to the stream, the second and subsequent document will be preceded with a "---" document separator, but the first will not. See the documentation for Marshal for details about the conversion of Go values to YAML.
func ( *Encoder) ( interface{}) ( error) {
	defer handleErr(&)
	.encoder.marshalDoc("", reflect.ValueOf())
	return nil
}
Close closes the encoder by writing any remaining data. It does not write a stream terminating string "...".
func ( *Encoder) () ( error) {
	defer handleErr(&)
	.encoder.finish()
	return nil
}

func ( *error) {
	if  := recover();  != nil {
		if ,  := .(yamlError);  {
			* = .err
		} else {
			panic()
		}
	}
}

type yamlError struct {
	err error
}

func ( error) {
	panic(yamlError{})
}

func ( string,  ...interface{}) {
	panic(yamlError{fmt.Errorf("yaml: "+, ...)})
}
A TypeError is returned by Unmarshal when one or more fields in the YAML document cannot be properly decoded into the requested types. When this error is returned, the value is still unmarshaled partially.
type TypeError struct {
	Errors []string
}

func ( *TypeError) () string {
	return fmt.Sprintf("yaml: unmarshal errors:\n  %s", strings.Join(.Errors, "\n  "))
}
-------------------------------------------------------------------------- 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 a given struct.
InlineMap is the number of the field in the struct that contains an ,inline map, or -1 if there's none.
Id holds the unique field identifier, so we can cheaply check for field duplicates without maintaining an extra map.
Inline holds the field index if the field is part of an inlined struct.
	Inline []int
}

var structMap = make(map[reflect.Type]*structInfo)
var fieldMapMutex sync.RWMutex

func ( reflect.Type) (*structInfo, error) {
	fieldMapMutex.RLock()
	,  := structMap[]
	fieldMapMutex.RUnlock()
	if  {
		return , nil
	}

	 := .NumField()
	 := make(map[string]fieldInfo)
	 := make([]fieldInfo, 0, )
	 := -1
	for  := 0;  != ; ++ {
		 := .Field()
		if .PkgPath != "" && !.Anonymous {
			continue // Private field
		}

		 := fieldInfo{Num: }

		 := .Tag.Get("yaml")
		if  == "" && strings.Index(string(.Tag), ":") < 0 {
			 = string(.Tag)
		}
		if  == "-" {
			continue
		}

		 := false
		 := strings.Split(, ",")
		if len() > 1 {
			for ,  := range [1:] {
				switch  {
				case "omitempty":
					.OmitEmpty = true
				case "flow":
					.Flow = true
				case "inline":
					 = true
				default:
					return nil, errors.New(fmt.Sprintf("Unsupported flag %q in tag %q of type %s", , , ))
				}
			}
			 = [0]
		}

		if  {
			switch .Type.Kind() {
			case reflect.Map:
				if  >= 0 {
					return nil, errors.New("Multiple ,inline maps in struct " + .String())
				}
				if .Type.Key() != reflect.TypeOf("") {
					return nil, errors.New("Option ,inline needs a map with string keys in struct " + .String())
				}
				 = .Num
			case reflect.Struct:
				,  := (.Type)
				if  != nil {
					return nil, 
				}
				for ,  := range .FieldsList {
					if ,  := [.Key];  {
						 := "Duplicated key '" + .Key + "' in struct " + .String()
						return nil, errors.New()
					}
					if .Inline == nil {
						.Inline = []int{, .Num}
					} else {
						.Inline = append([]int{}, .Inline...)
					}
					.Id = len()
					[.Key] = 
					 = append(, )
				}
return nil, errors.New("Option ,inline needs a struct value or map field")
				return nil, errors.New("Option ,inline needs a struct value field")
			}
			continue
		}

		if  != "" {
			.Key = 
		} else {
			.Key = strings.ToLower(.Name)
		}

		if _,  = [.Key];  {
			 := "Duplicated key '" + .Key + "' in struct " + .String()
			return nil, errors.New()
		}

		.Id = len()
		 = append(, )
		[.Key] = 
	}

	 = &structInfo{
		FieldsMap:  ,
		FieldsList: ,
		InlineMap:  ,
	}

	fieldMapMutex.Lock()
	structMap[] = 
	fieldMapMutex.Unlock()
	return , nil
}
IsZeroer is used to check whether an object is zero to determine whether it should be omitted when marshaling with the omitempty flag. One notable implementation is time.Time.
type IsZeroer interface {
	IsZero() bool
}

func ( reflect.Value) bool {
	 := .Kind()
	if ,  := .Interface().(IsZeroer);  {
		if ( == reflect.Ptr ||  == reflect.Interface) && .IsNil() {
			return true
		}
		return .IsZero()
	}
	switch  {
	case reflect.String:
		return len(.String()) == 0
	case reflect.Interface, reflect.Ptr:
		return .IsNil()
	case reflect.Slice:
		return .Len() == 0
	case reflect.Map:
		return .Len() == 0
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		return .Int() == 0
	case reflect.Float32, reflect.Float64:
		return .Float() == 0
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
		return .Uint() == 0
	case reflect.Bool:
		return !.Bool()
	case reflect.Struct:
		 := .Type()
		for  := .NumField() - 1;  >= 0; -- {
			if .Field().PkgPath != "" {
				continue // Private field
			}
			if !(.Field()) {
				return false
			}
		}
		return true
	}
	return false