encode.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. package yaml
  2. import (
  3. "reflect"
  4. "regexp"
  5. "sort"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. type encoder struct {
  11. emitter yaml_emitter_t
  12. event yaml_event_t
  13. out []byte
  14. flow bool
  15. }
  16. func newEncoder() (e *encoder) {
  17. e = &encoder{}
  18. e.must(yaml_emitter_initialize(&e.emitter))
  19. yaml_emitter_set_output_string(&e.emitter, &e.out)
  20. yaml_emitter_set_unicode(&e.emitter, true)
  21. e.must(yaml_stream_start_event_initialize(&e.event, yaml_UTF8_ENCODING))
  22. e.emit()
  23. e.must(yaml_document_start_event_initialize(&e.event, nil, nil, true))
  24. e.emit()
  25. return e
  26. }
  27. func (e *encoder) finish() {
  28. e.must(yaml_document_end_event_initialize(&e.event, true))
  29. e.emit()
  30. e.emitter.open_ended = false
  31. e.must(yaml_stream_end_event_initialize(&e.event))
  32. e.emit()
  33. }
  34. func (e *encoder) destroy() {
  35. yaml_emitter_delete(&e.emitter)
  36. }
  37. func (e *encoder) emit() {
  38. // This will internally delete the e.event value.
  39. if !yaml_emitter_emit(&e.emitter, &e.event) && e.event.typ != yaml_DOCUMENT_END_EVENT && e.event.typ != yaml_STREAM_END_EVENT {
  40. e.must(false)
  41. }
  42. }
  43. func (e *encoder) must(ok bool) {
  44. if !ok {
  45. msg := e.emitter.problem
  46. if msg == "" {
  47. msg = "unknown problem generating YAML content"
  48. }
  49. failf("%s", msg)
  50. }
  51. }
  52. func (e *encoder) marshal(tag string, in reflect.Value) {
  53. if m, ok := in.Interface().(Marshaler); ok {
  54. v, err := m.MarshalYAML()
  55. if err != nil {
  56. fail(err)
  57. }
  58. if v == nil {
  59. e.nilv()
  60. return
  61. }
  62. in = reflect.ValueOf(v)
  63. }
  64. switch in.Kind() {
  65. case reflect.Interface:
  66. if in.IsNil() {
  67. e.nilv()
  68. } else {
  69. e.marshal(tag, in.Elem())
  70. }
  71. case reflect.Map:
  72. e.mapv(tag, in)
  73. case reflect.Ptr:
  74. if in.IsNil() {
  75. e.nilv()
  76. } else {
  77. e.marshal(tag, in.Elem())
  78. }
  79. case reflect.Struct:
  80. e.structv(tag, in)
  81. case reflect.Slice:
  82. if in.Type().Elem() == mapItemType {
  83. e.itemsv(tag, in)
  84. } else {
  85. e.slicev(tag, in)
  86. }
  87. case reflect.String:
  88. e.stringv(tag, in)
  89. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  90. if in.Type() == durationType {
  91. e.stringv(tag, reflect.ValueOf(in.Interface().(time.Duration).String()))
  92. } else {
  93. e.intv(tag, in)
  94. }
  95. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  96. e.uintv(tag, in)
  97. case reflect.Float32, reflect.Float64:
  98. e.floatv(tag, in)
  99. case reflect.Bool:
  100. e.boolv(tag, in)
  101. default:
  102. panic("cannot marshal type: " + in.Type().String())
  103. }
  104. }
  105. func (e *encoder) mapv(tag string, in reflect.Value) {
  106. e.mappingv(tag, func() {
  107. keys := keyList(in.MapKeys())
  108. sort.Sort(keys)
  109. for _, k := range keys {
  110. e.marshal("", k)
  111. e.marshal("", in.MapIndex(k))
  112. }
  113. })
  114. }
  115. func (e *encoder) itemsv(tag string, in reflect.Value) {
  116. e.mappingv(tag, func() {
  117. slice := in.Convert(reflect.TypeOf([]MapItem{})).Interface().([]MapItem)
  118. for _, item := range slice {
  119. e.marshal("", reflect.ValueOf(item.Key))
  120. e.marshal("", reflect.ValueOf(item.Value))
  121. }
  122. })
  123. }
  124. func (e *encoder) structv(tag string, in reflect.Value) {
  125. sinfo, err := getStructInfo(in.Type())
  126. if err != nil {
  127. panic(err)
  128. }
  129. e.mappingv(tag, func() {
  130. for _, info := range sinfo.FieldsList {
  131. var value reflect.Value
  132. if info.Inline == nil {
  133. value = in.Field(info.Num)
  134. } else {
  135. value = in.FieldByIndex(info.Inline)
  136. }
  137. if info.OmitEmpty && isZero(value) {
  138. continue
  139. }
  140. e.marshal("", reflect.ValueOf(info.Key))
  141. e.flow = info.Flow
  142. e.marshal("", value)
  143. }
  144. })
  145. }
  146. func (e *encoder) mappingv(tag string, f func()) {
  147. implicit := tag == ""
  148. style := yaml_BLOCK_MAPPING_STYLE
  149. if e.flow {
  150. e.flow = false
  151. style = yaml_FLOW_MAPPING_STYLE
  152. }
  153. e.must(yaml_mapping_start_event_initialize(&e.event, nil, []byte(tag), implicit, style))
  154. e.emit()
  155. f()
  156. e.must(yaml_mapping_end_event_initialize(&e.event))
  157. e.emit()
  158. }
  159. func (e *encoder) slicev(tag string, in reflect.Value) {
  160. implicit := tag == ""
  161. style := yaml_BLOCK_SEQUENCE_STYLE
  162. if e.flow {
  163. e.flow = false
  164. style = yaml_FLOW_SEQUENCE_STYLE
  165. }
  166. e.must(yaml_sequence_start_event_initialize(&e.event, nil, []byte(tag), implicit, style))
  167. e.emit()
  168. n := in.Len()
  169. for i := 0; i < n; i++ {
  170. e.marshal("", in.Index(i))
  171. }
  172. e.must(yaml_sequence_end_event_initialize(&e.event))
  173. e.emit()
  174. }
  175. // isBase60 returns whether s is in base 60 notation as defined in YAML 1.1.
  176. //
  177. // The base 60 float notation in YAML 1.1 is a terrible idea and is unsupported
  178. // in YAML 1.2 and by this package, but these should be marshalled quoted for
  179. // the time being for compatibility with other parsers.
  180. func isBase60Float(s string) (result bool) {
  181. // Fast path.
  182. if s == "" {
  183. return false
  184. }
  185. c := s[0]
  186. if !(c == '+' || c == '-' || c >= '0' && c <= '9') || strings.IndexByte(s, ':') < 0 {
  187. return false
  188. }
  189. // Do the full match.
  190. return base60float.MatchString(s)
  191. }
  192. // From http://yaml.org/type/float.html, except the regular expression there
  193. // is bogus. In practice parsers do not enforce the "\.[0-9_]*" suffix.
  194. var base60float = regexp.MustCompile(`^[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+(?:\.[0-9_]*)?$`)
  195. func (e *encoder) stringv(tag string, in reflect.Value) {
  196. var style yaml_scalar_style_t
  197. s := in.String()
  198. rtag, rs := resolve("", s)
  199. if rtag == yaml_BINARY_TAG {
  200. if tag == "" || tag == yaml_STR_TAG {
  201. tag = rtag
  202. s = rs.(string)
  203. } else if tag == yaml_BINARY_TAG {
  204. failf("explicitly tagged !!binary data must be base64-encoded")
  205. } else {
  206. failf("cannot marshal invalid UTF-8 data as %s", shortTag(tag))
  207. }
  208. }
  209. if tag == "" && (rtag != yaml_STR_TAG || isBase60Float(s)) {
  210. style = yaml_DOUBLE_QUOTED_SCALAR_STYLE
  211. } else if strings.Contains(s, "\n") {
  212. style = yaml_LITERAL_SCALAR_STYLE
  213. } else {
  214. style = yaml_PLAIN_SCALAR_STYLE
  215. }
  216. e.emitScalar(s, "", tag, style)
  217. }
  218. func (e *encoder) boolv(tag string, in reflect.Value) {
  219. var s string
  220. if in.Bool() {
  221. s = "true"
  222. } else {
  223. s = "false"
  224. }
  225. e.emitScalar(s, "", tag, yaml_PLAIN_SCALAR_STYLE)
  226. }
  227. func (e *encoder) intv(tag string, in reflect.Value) {
  228. s := strconv.FormatInt(in.Int(), 10)
  229. e.emitScalar(s, "", tag, yaml_PLAIN_SCALAR_STYLE)
  230. }
  231. func (e *encoder) uintv(tag string, in reflect.Value) {
  232. s := strconv.FormatUint(in.Uint(), 10)
  233. e.emitScalar(s, "", tag, yaml_PLAIN_SCALAR_STYLE)
  234. }
  235. func (e *encoder) floatv(tag string, in reflect.Value) {
  236. // FIXME: Handle 64 bits here.
  237. s := strconv.FormatFloat(float64(in.Float()), 'g', -1, 32)
  238. switch s {
  239. case "+Inf":
  240. s = ".inf"
  241. case "-Inf":
  242. s = "-.inf"
  243. case "NaN":
  244. s = ".nan"
  245. }
  246. e.emitScalar(s, "", tag, yaml_PLAIN_SCALAR_STYLE)
  247. }
  248. func (e *encoder) nilv() {
  249. e.emitScalar("null", "", "", yaml_PLAIN_SCALAR_STYLE)
  250. }
  251. func (e *encoder) emitScalar(value, anchor, tag string, style yaml_scalar_style_t) {
  252. implicit := tag == ""
  253. e.must(yaml_scalar_event_initialize(&e.event, []byte(anchor), []byte(tag), []byte(value), implicit, implicit, style))
  254. e.emit()
  255. }