yaml.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. package yaml
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "reflect"
  7. "strconv"
  8. "gopkg.in/yaml.v2"
  9. )
  10. // Marshals the object into JSON then converts JSON to YAML and returns the
  11. // YAML.
  12. func Marshal(o interface{}) ([]byte, error) {
  13. j, err := json.Marshal(o)
  14. if err != nil {
  15. return nil, fmt.Errorf("error marshaling into JSON: ", err)
  16. }
  17. y, err := JSONToYAML(j)
  18. if err != nil {
  19. return nil, fmt.Errorf("error converting JSON to YAML: ", err)
  20. }
  21. return y, nil
  22. }
  23. // Converts YAML to JSON then uses JSON to unmarshal into an object.
  24. func Unmarshal(y []byte, o interface{}) error {
  25. vo := reflect.ValueOf(o)
  26. j, err := yamlToJSON(y, &vo)
  27. if err != nil {
  28. return fmt.Errorf("error converting YAML to JSON: %v", err)
  29. }
  30. err = json.Unmarshal(j, o)
  31. if err != nil {
  32. return fmt.Errorf("error unmarshaling JSON: %v", err)
  33. }
  34. return nil
  35. }
  36. // Convert JSON to YAML.
  37. func JSONToYAML(j []byte) ([]byte, error) {
  38. // Convert the JSON to an object.
  39. var jsonObj interface{}
  40. // We are using yaml.Unmarshal here (instead of json.Unmarshal) because the
  41. // Go JSON library doesn't try to pick the right number type (int, float,
  42. // etc.) when unmarshling to interface{}, it just picks float64
  43. // universally. go-yaml does go through the effort of picking the right
  44. // number type, so we can preserve number type throughout this process.
  45. err := yaml.Unmarshal(j, &jsonObj)
  46. if err != nil {
  47. return nil, err
  48. }
  49. // Marshal this object into YAML.
  50. return yaml.Marshal(jsonObj)
  51. }
  52. // Convert YAML to JSON. Since JSON is a subset of YAML, passing JSON through
  53. // this method should be a no-op.
  54. //
  55. // Things YAML can do that are not supported by JSON:
  56. // * In YAML you can have binary and null keys in your maps. These are invalid
  57. // in JSON. (int and float keys are converted to strings.)
  58. // * Binary data in YAML with the !!binary tag is not supported. If you want to
  59. // use binary data with this library, encode the data as base64 as usual but do
  60. // not use the !!binary tag in your YAML. This will ensure the original base64
  61. // encoded data makes it all the way through to the JSON.
  62. func YAMLToJSON(y []byte) ([]byte, error) {
  63. return yamlToJSON(y, nil)
  64. }
  65. func yamlToJSON(y []byte, jsonTarget *reflect.Value) ([]byte, error) {
  66. // Convert the YAML to an object.
  67. var yamlObj interface{}
  68. err := yaml.Unmarshal(y, &yamlObj)
  69. if err != nil {
  70. return nil, err
  71. }
  72. // YAML objects are not completely compatible with JSON objects (e.g. you
  73. // can have non-string keys in YAML). So, convert the YAML-compatible object
  74. // to a JSON-compatible object, failing with an error if irrecoverable
  75. // incompatibilties happen along the way.
  76. jsonObj, err := convertToJSONableObject(yamlObj, jsonTarget)
  77. if err != nil {
  78. return nil, err
  79. }
  80. // Convert this object to JSON and return the data.
  81. return json.Marshal(jsonObj)
  82. }
  83. func convertToJSONableObject(yamlObj interface{}, jsonTarget *reflect.Value) (interface{}, error) {
  84. var err error
  85. // Resolve jsonTarget to a concrete value (i.e. not a pointer or an
  86. // interface). We pass decodingNull as false because we're not actually
  87. // decoding into the value, we're just checking if the ultimate target is a
  88. // string.
  89. if jsonTarget != nil {
  90. ju, tu, pv := indirect(*jsonTarget, false)
  91. // We have a JSON or Text Umarshaler at this level, so we can't be trying
  92. // to decode into a string.
  93. if ju != nil || tu != nil {
  94. jsonTarget = nil
  95. } else {
  96. jsonTarget = &pv
  97. }
  98. }
  99. // If yamlObj is a number or a boolean, check if jsonTarget is a string -
  100. // if so, coerce. Else return normal.
  101. // If yamlObj is a map or array, find the field that each key is
  102. // unmarshaling to, and when you recurse pass the reflect.Value for that
  103. // field back into this function.
  104. switch typedYAMLObj := yamlObj.(type) {
  105. case map[interface{}]interface{}:
  106. // JSON does not support arbitrary keys in a map, so we must convert
  107. // these keys to strings.
  108. //
  109. // From my reading of go-yaml v2 (specifically the resolve function),
  110. // keys can only have the types string, int, int64, float64, binary
  111. // (unsupported), or null (unsupported).
  112. strMap := make(map[string]interface{})
  113. for k, v := range typedYAMLObj {
  114. // Resolve the key to a string first.
  115. var keyString string
  116. switch typedKey := k.(type) {
  117. case string:
  118. keyString = typedKey
  119. case int:
  120. keyString = strconv.Itoa(typedKey)
  121. case int64:
  122. // go-yaml will only return an int64 as a key if the system
  123. // architecture is 32-bit and the key's value is between 32-bit
  124. // and 64-bit. Otherwise the key type will simply be int.
  125. keyString = strconv.FormatInt(typedKey, 10)
  126. case float64:
  127. // Stolen from go-yaml to use the same conversion to string as
  128. // the go-yaml library uses to convert float to string when
  129. // Marshaling.
  130. s := strconv.FormatFloat(typedKey, 'g', -1, 32)
  131. switch s {
  132. case "+Inf":
  133. s = ".inf"
  134. case "-Inf":
  135. s = "-.inf"
  136. case "NaN":
  137. s = ".nan"
  138. }
  139. keyString = s
  140. case bool:
  141. if typedKey {
  142. keyString = "true"
  143. } else {
  144. keyString = "false"
  145. }
  146. default:
  147. return nil, fmt.Errorf("Unsupported map key of type: %s, key: %+#v, value: %+#v",
  148. reflect.TypeOf(k), k, v)
  149. }
  150. // jsonTarget should be a struct or a map. If it's a struct, find
  151. // the field it's going to map to and pass its reflect.Value. If
  152. // it's a map, find the element type of the map and pass the
  153. // reflect.Value created from that type. If it's neither, just pass
  154. // nil - JSON conversion will error for us if it's a real issue.
  155. if jsonTarget != nil {
  156. t := *jsonTarget
  157. if t.Kind() == reflect.Struct {
  158. keyBytes := []byte(keyString)
  159. // Find the field that the JSON library would use.
  160. var f *field
  161. fields := cachedTypeFields(t.Type())
  162. for i := range fields {
  163. ff := &fields[i]
  164. if bytes.Equal(ff.nameBytes, keyBytes) {
  165. f = ff
  166. break
  167. }
  168. // Do case-insensitive comparison.
  169. if f == nil && ff.equalFold(ff.nameBytes, keyBytes) {
  170. f = ff
  171. }
  172. }
  173. if f != nil {
  174. // Find the reflect.Value of the most preferential
  175. // struct field.
  176. jtf := t.Field(f.index[0])
  177. strMap[keyString], err = convertToJSONableObject(v, &jtf)
  178. if err != nil {
  179. return nil, err
  180. }
  181. continue
  182. }
  183. } else if t.Kind() == reflect.Map {
  184. // Create a zero value of the map's element type to use as
  185. // the JSON target.
  186. jtv := reflect.Zero(t.Type().Elem())
  187. strMap[keyString], err = convertToJSONableObject(v, &jtv)
  188. if err != nil {
  189. return nil, err
  190. }
  191. continue
  192. }
  193. }
  194. strMap[keyString], err = convertToJSONableObject(v, nil)
  195. if err != nil {
  196. return nil, err
  197. }
  198. }
  199. return strMap, nil
  200. case []interface{}:
  201. // We need to recurse into arrays in case there are any
  202. // map[interface{}]interface{}'s inside and to convert any
  203. // numbers to strings.
  204. // If jsonTarget is a slice (which it really should be), find the
  205. // thing it's going to map to. If it's not a slice, just pass nil
  206. // - JSON conversion will error for us if it's a real issue.
  207. var jsonSliceElemValue *reflect.Value
  208. if jsonTarget != nil {
  209. t := *jsonTarget
  210. if t.Kind() == reflect.Slice {
  211. // By default slices point to nil, but we need a reflect.Value
  212. // pointing to a value of the slice type, so we create one here.
  213. ev := reflect.Indirect(reflect.New(t.Type().Elem()))
  214. jsonSliceElemValue = &ev
  215. }
  216. }
  217. // Make and use a new array.
  218. arr := make([]interface{}, len(typedYAMLObj))
  219. for i, v := range typedYAMLObj {
  220. arr[i], err = convertToJSONableObject(v, jsonSliceElemValue)
  221. if err != nil {
  222. return nil, err
  223. }
  224. }
  225. return arr, nil
  226. default:
  227. // If the target type is a string and the YAML type is a number,
  228. // convert the YAML type to a string.
  229. if jsonTarget != nil && (*jsonTarget).Kind() == reflect.String {
  230. // Based on my reading of go-yaml, it may return int, int64,
  231. // float64, or uint64.
  232. var s string
  233. switch typedVal := typedYAMLObj.(type) {
  234. case int:
  235. s = strconv.FormatInt(int64(typedVal), 10)
  236. case int64:
  237. s = strconv.FormatInt(typedVal, 10)
  238. case float64:
  239. s = strconv.FormatFloat(typedVal, 'g', -1, 32)
  240. case uint64:
  241. s = strconv.FormatUint(typedVal, 10)
  242. case bool:
  243. if typedVal {
  244. s = "true"
  245. } else {
  246. s = "false"
  247. }
  248. }
  249. if len(s) > 0 {
  250. yamlObj = interface{}(s)
  251. }
  252. }
  253. return yamlObj, nil
  254. }
  255. return nil, nil
  256. }