yaml.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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)
  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. // --------------------------------------------------------------------------
  141. // Maintain a mapping of keys to structure field indexes
  142. // The code in this section was copied from mgo/bson.
  143. // structInfo holds details for the serialization of fields of
  144. // a given struct.
  145. type structInfo struct {
  146. FieldsMap map[string]fieldInfo
  147. FieldsList []fieldInfo
  148. // InlineMap is the number of the field in the struct that
  149. // contains an ,inline map, or -1 if there's none.
  150. InlineMap int
  151. }
  152. type fieldInfo struct {
  153. Key string
  154. Num int
  155. OmitEmpty bool
  156. Flow bool
  157. // Inline holds the field index if the field is part of an inlined struct.
  158. Inline []int
  159. }
  160. var structMap = make(map[reflect.Type]*structInfo)
  161. var fieldMapMutex sync.RWMutex
  162. func getStructInfo(st reflect.Type) (*structInfo, error) {
  163. fieldMapMutex.RLock()
  164. sinfo, found := structMap[st]
  165. fieldMapMutex.RUnlock()
  166. if found {
  167. return sinfo, nil
  168. }
  169. n := st.NumField()
  170. fieldsMap := make(map[string]fieldInfo)
  171. fieldsList := make([]fieldInfo, 0, n)
  172. inlineMap := -1
  173. for i := 0; i != n; i++ {
  174. field := st.Field(i)
  175. if field.PkgPath != "" {
  176. continue // Private field
  177. }
  178. info := fieldInfo{Num: i}
  179. tag := field.Tag.Get("yaml")
  180. if tag == "" && strings.Index(string(field.Tag), ":") < 0 {
  181. tag = string(field.Tag)
  182. }
  183. if tag == "-" {
  184. continue
  185. }
  186. inline := false
  187. fields := strings.Split(tag, ",")
  188. if len(fields) > 1 {
  189. for _, flag := range fields[1:] {
  190. switch flag {
  191. case "omitempty":
  192. info.OmitEmpty = true
  193. case "flow":
  194. info.Flow = true
  195. case "inline":
  196. inline = true
  197. default:
  198. return nil, errors.New(fmt.Sprintf("Unsupported flag %q in tag %q of type %s", flag, tag, st))
  199. }
  200. }
  201. tag = fields[0]
  202. }
  203. if inline {
  204. switch field.Type.Kind() {
  205. // TODO: Implement support for inline maps.
  206. //case reflect.Map:
  207. // if inlineMap >= 0 {
  208. // return nil, errors.New("Multiple ,inline maps in struct " + st.String())
  209. // }
  210. // if field.Type.Key() != reflect.TypeOf("") {
  211. // return nil, errors.New("Option ,inline needs a map with string keys in struct " + st.String())
  212. // }
  213. // inlineMap = info.Num
  214. case reflect.Struct:
  215. sinfo, err := getStructInfo(field.Type)
  216. if err != nil {
  217. return nil, err
  218. }
  219. for _, finfo := range sinfo.FieldsList {
  220. if _, found := fieldsMap[finfo.Key]; found {
  221. msg := "Duplicated key '" + finfo.Key + "' in struct " + st.String()
  222. return nil, errors.New(msg)
  223. }
  224. if finfo.Inline == nil {
  225. finfo.Inline = []int{i, finfo.Num}
  226. } else {
  227. finfo.Inline = append([]int{i}, finfo.Inline...)
  228. }
  229. fieldsMap[finfo.Key] = finfo
  230. fieldsList = append(fieldsList, finfo)
  231. }
  232. default:
  233. //return nil, errors.New("Option ,inline needs a struct value or map field")
  234. return nil, errors.New("Option ,inline needs a struct value field")
  235. }
  236. continue
  237. }
  238. if tag != "" {
  239. info.Key = tag
  240. } else {
  241. info.Key = strings.ToLower(field.Name)
  242. }
  243. if _, found = fieldsMap[info.Key]; found {
  244. msg := "Duplicated key '" + info.Key + "' in struct " + st.String()
  245. return nil, errors.New(msg)
  246. }
  247. fieldsList = append(fieldsList, info)
  248. fieldsMap[info.Key] = info
  249. }
  250. sinfo = &structInfo{fieldsMap, fieldsList, inlineMap}
  251. fieldMapMutex.Lock()
  252. structMap[st] = sinfo
  253. fieldMapMutex.Unlock()
  254. return sinfo, nil
  255. }
  256. func isZero(v reflect.Value) bool {
  257. switch v.Kind() {
  258. case reflect.String:
  259. return len(v.String()) == 0
  260. case reflect.Interface, reflect.Ptr:
  261. return v.IsNil()
  262. case reflect.Slice:
  263. return v.Len() == 0
  264. case reflect.Map:
  265. return v.Len() == 0
  266. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  267. return v.Int() == 0
  268. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  269. return v.Uint() == 0
  270. case reflect.Bool:
  271. return !v.Bool()
  272. }
  273. return false
  274. }