yaml.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. // Package yaml implements YAML support for the Go language.
  2. //
  3. // Source code and other details for the project are available at GitHub:
  4. //
  5. // https://github.com/go-yaml/yaml
  6. //
  7. package yaml
  8. import (
  9. "errors"
  10. "fmt"
  11. "reflect"
  12. "strings"
  13. "sync"
  14. )
  15. type yamlError string
  16. func fail(msg string) {
  17. panic(yamlError(msg))
  18. }
  19. func handleErr(err *error) {
  20. if r := recover(); r != nil {
  21. if e, ok := r.(yamlError); ok {
  22. *err = errors.New("YAML error: " + string(e))
  23. } else {
  24. panic(r)
  25. }
  26. }
  27. }
  28. // The Setter interface may be implemented by types to do their own custom
  29. // unmarshalling of YAML values, rather than being implicitly assigned by
  30. // the yaml package machinery. If setting the value works, the method should
  31. // return true. If it returns false, the value is considered unsupported
  32. // and is omitted from maps and slices.
  33. type Setter interface {
  34. SetYAML(tag string, value interface{}) bool
  35. }
  36. // The Getter interface is implemented by types to do their own custom
  37. // marshalling into a YAML tag and value.
  38. type Getter interface {
  39. GetYAML() (tag string, value interface{})
  40. }
  41. // Unmarshal decodes the first document found within the in byte slice
  42. // and assigns decoded values into the out value.
  43. //
  44. // Maps and pointers (to a struct, string, int, etc) are accepted as out
  45. // values. If an internal pointer within a struct is not initialized,
  46. // the yaml package will initialize it if necessary for unmarshalling
  47. // the provided data. The out parameter must not be nil.
  48. //
  49. // The type of the decoded values and the type of out will be considered,
  50. // and Unmarshal will do the best possible job to unmarshal values
  51. // appropriately. It is NOT considered an error, though, to skip values
  52. // because they are not available in the decoded YAML, or if they are not
  53. // compatible with the out value. To ensure something was properly
  54. // unmarshaled use a map or compare against the previous value for the
  55. // field (usually the zero value).
  56. //
  57. // Struct fields are only unmarshalled if they are exported (have an
  58. // upper case first letter), and are unmarshalled using the field name
  59. // lowercased as the default key. Custom keys may be defined via the
  60. // "yaml" name in the field tag: the content preceding the first comma
  61. // is used as the key, and the following comma-separated options are
  62. // used to tweak the marshalling process (see Marshal).
  63. // Conflicting names result in a runtime error.
  64. //
  65. // For example:
  66. //
  67. // type T struct {
  68. // F int `yaml:"a,omitempty"`
  69. // B int
  70. // }
  71. // var t T
  72. // yaml.Unmarshal([]byte("a: 1\nb: 2"), &t)
  73. //
  74. // See the documentation of Marshal for the format of tags and a list of
  75. // supported tag options.
  76. //
  77. func Unmarshal(in []byte, out interface{}) (err error) {
  78. defer handleErr(&err)
  79. d := newDecoder()
  80. p := newParser(in, UnmarshalMappingKeyTransform)
  81. defer p.destroy()
  82. node := p.parse()
  83. if node != nil {
  84. v := reflect.ValueOf(out)
  85. if v.Kind() == reflect.Ptr && !v.IsNil() {
  86. v = v.Elem()
  87. }
  88. d.unmarshal(node, v)
  89. }
  90. return nil
  91. }
  92. // Marshal serializes the value provided into a YAML document. The structure
  93. // of the generated document will reflect the structure of the value itself.
  94. // Maps and pointers (to struct, string, int, etc) are accepted as the in value.
  95. //
  96. // Struct fields are only unmarshalled if they are exported (have an upper case
  97. // first letter), and are unmarshalled using the field name lowercased as the
  98. // default key. Custom keys may be defined via the "yaml" name in the field
  99. // tag: the content preceding the first comma is used as the key, and the
  100. // following comma-separated options are used to tweak the marshalling process.
  101. // Conflicting names result in a runtime error.
  102. //
  103. // The field tag format accepted is:
  104. //
  105. // `(...) yaml:"[<key>][,<flag1>[,<flag2>]]" (...)`
  106. //
  107. // The following flags are currently supported:
  108. //
  109. // omitempty Only include the field if it's not set to the zero
  110. // value for the type or to empty slices or maps.
  111. // Does not apply to zero valued structs.
  112. //
  113. // flow Marshal using a flow style (useful for structs,
  114. // sequences and maps.
  115. //
  116. // inline Inline the struct it's applied to, so its fields
  117. // are processed as if they were part of the outer
  118. // struct.
  119. //
  120. // In addition, if the key is "-", the field is ignored.
  121. //
  122. // For example:
  123. //
  124. // type T struct {
  125. // F int "a,omitempty"
  126. // B int
  127. // }
  128. // yaml.Marshal(&T{B: 2}) // Returns "b: 2\n"
  129. // yaml.Marshal(&T{F: 1}} // Returns "a: 1\nb: 0\n"
  130. //
  131. func Marshal(in interface{}) (out []byte, err error) {
  132. defer handleErr(&err)
  133. e := newEncoder()
  134. defer e.destroy()
  135. e.marshal("", reflect.ValueOf(in))
  136. e.finish()
  137. out = e.out
  138. return
  139. }
  140. // UnmarshalMappingKeyTransform is a string transformation that is applied to
  141. // each mapping key in a YAML document before it is unmarshalled. By default,
  142. // UnmarshalMappingKeyTransform is an identity transform (no modification).
  143. var UnmarshalMappingKeyTransform transformString = identityTransform
  144. type transformString func(in string) (out string)
  145. func identityTransform(in string) (out string) {
  146. return in
  147. }
  148. // --------------------------------------------------------------------------
  149. // Maintain a mapping of keys to structure field indexes
  150. // The code in this section was copied from mgo/bson.
  151. // structInfo holds details for the serialization of fields of
  152. // a given struct.
  153. type structInfo struct {
  154. FieldsMap map[string]fieldInfo
  155. FieldsList []fieldInfo
  156. // InlineMap is the number of the field in the struct that
  157. // contains an ,inline map, or -1 if there's none.
  158. InlineMap int
  159. }
  160. type fieldInfo struct {
  161. Key string
  162. Num int
  163. OmitEmpty bool
  164. Flow bool
  165. // Inline holds the field index if the field is part of an inlined struct.
  166. Inline []int
  167. }
  168. var structMap = make(map[reflect.Type]*structInfo)
  169. var fieldMapMutex sync.RWMutex
  170. func getStructInfo(st reflect.Type) (*structInfo, error) {
  171. fieldMapMutex.RLock()
  172. sinfo, found := structMap[st]
  173. fieldMapMutex.RUnlock()
  174. if found {
  175. return sinfo, nil
  176. }
  177. n := st.NumField()
  178. fieldsMap := make(map[string]fieldInfo)
  179. fieldsList := make([]fieldInfo, 0, n)
  180. inlineMap := -1
  181. for i := 0; i != n; i++ {
  182. field := st.Field(i)
  183. if field.PkgPath != "" {
  184. continue // Private field
  185. }
  186. info := fieldInfo{Num: i}
  187. tag := field.Tag.Get("yaml")
  188. if tag == "" && strings.Index(string(field.Tag), ":") < 0 {
  189. tag = string(field.Tag)
  190. }
  191. if tag == "-" {
  192. continue
  193. }
  194. inline := false
  195. fields := strings.Split(tag, ",")
  196. if len(fields) > 1 {
  197. for _, flag := range fields[1:] {
  198. switch flag {
  199. case "omitempty":
  200. info.OmitEmpty = true
  201. case "flow":
  202. info.Flow = true
  203. case "inline":
  204. inline = true
  205. default:
  206. return nil, errors.New(fmt.Sprintf("Unsupported flag %q in tag %q of type %s", flag, tag, st))
  207. }
  208. }
  209. tag = fields[0]
  210. }
  211. if inline {
  212. switch field.Type.Kind() {
  213. // TODO: Implement support for inline maps.
  214. //case reflect.Map:
  215. // if inlineMap >= 0 {
  216. // return nil, errors.New("Multiple ,inline maps in struct " + st.String())
  217. // }
  218. // if field.Type.Key() != reflect.TypeOf("") {
  219. // return nil, errors.New("Option ,inline needs a map with string keys in struct " + st.String())
  220. // }
  221. // inlineMap = info.Num
  222. case reflect.Struct:
  223. sinfo, err := getStructInfo(field.Type)
  224. if err != nil {
  225. return nil, err
  226. }
  227. for _, finfo := range sinfo.FieldsList {
  228. if _, found := fieldsMap[finfo.Key]; found {
  229. msg := "Duplicated key '" + finfo.Key + "' in struct " + st.String()
  230. return nil, errors.New(msg)
  231. }
  232. if finfo.Inline == nil {
  233. finfo.Inline = []int{i, finfo.Num}
  234. } else {
  235. finfo.Inline = append([]int{i}, finfo.Inline...)
  236. }
  237. fieldsMap[finfo.Key] = finfo
  238. fieldsList = append(fieldsList, finfo)
  239. }
  240. default:
  241. //return nil, errors.New("Option ,inline needs a struct value or map field")
  242. return nil, errors.New("Option ,inline needs a struct value field")
  243. }
  244. continue
  245. }
  246. if tag != "" {
  247. info.Key = tag
  248. } else {
  249. info.Key = strings.ToLower(field.Name)
  250. }
  251. if _, found = fieldsMap[info.Key]; found {
  252. msg := "Duplicated key '" + info.Key + "' in struct " + st.String()
  253. return nil, errors.New(msg)
  254. }
  255. fieldsList = append(fieldsList, info)
  256. fieldsMap[info.Key] = info
  257. }
  258. sinfo = &structInfo{fieldsMap, fieldsList, inlineMap}
  259. fieldMapMutex.Lock()
  260. structMap[st] = sinfo
  261. fieldMapMutex.Unlock()
  262. return sinfo, nil
  263. }
  264. func isZero(v reflect.Value) bool {
  265. switch v.Kind() {
  266. case reflect.String:
  267. return len(v.String()) == 0
  268. case reflect.Interface, reflect.Ptr:
  269. return v.IsNil()
  270. case reflect.Slice:
  271. return v.Len() == 0
  272. case reflect.Map:
  273. return v.Len() == 0
  274. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  275. return v.Int() == 0
  276. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  277. return v.Uint() == 0
  278. case reflect.Bool:
  279. return !v.Bool()
  280. }
  281. return false
  282. }