encode.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. package yaml
  2. import (
  3. "encoding"
  4. "fmt"
  5. "io"
  6. "reflect"
  7. "regexp"
  8. "sort"
  9. "strconv"
  10. "strings"
  11. "time"
  12. "unicode/utf8"
  13. )
  14. type encoder struct {
  15. emitter yaml_emitter_t
  16. event yaml_event_t
  17. out []byte
  18. flow bool
  19. // doneInit holds whether the initial stream_start_event has been
  20. // emitted.
  21. doneInit bool
  22. }
  23. func newEncoder() *encoder {
  24. e := &encoder{}
  25. yaml_emitter_initialize(&e.emitter)
  26. yaml_emitter_set_output_string(&e.emitter, &e.out)
  27. yaml_emitter_set_unicode(&e.emitter, true)
  28. return e
  29. }
  30. func newEncoderWithWriter(w io.Writer) *encoder {
  31. e := &encoder{}
  32. yaml_emitter_initialize(&e.emitter)
  33. yaml_emitter_set_output_writer(&e.emitter, w)
  34. yaml_emitter_set_unicode(&e.emitter, true)
  35. return e
  36. }
  37. func (e *encoder) init() {
  38. if e.doneInit {
  39. return
  40. }
  41. yaml_stream_start_event_initialize(&e.event, yaml_UTF8_ENCODING)
  42. e.emit()
  43. e.doneInit = true
  44. }
  45. func (e *encoder) finish() {
  46. e.emitter.open_ended = false
  47. yaml_stream_end_event_initialize(&e.event)
  48. e.emit()
  49. }
  50. func (e *encoder) destroy() {
  51. yaml_emitter_delete(&e.emitter)
  52. }
  53. func (e *encoder) emit() {
  54. // This will internally delete the e.event value.
  55. e.must(yaml_emitter_emit(&e.emitter, &e.event))
  56. }
  57. func (e *encoder) must(ok bool) {
  58. if !ok {
  59. msg := e.emitter.problem
  60. if msg == "" {
  61. msg = "unknown problem generating YAML content"
  62. }
  63. failf("%s", msg)
  64. }
  65. }
  66. func (e *encoder) marshalDoc(tag string, in reflect.Value) {
  67. e.init()
  68. if node, ok := in.Interface().(*Node); ok && node.Kind == DocumentNode {
  69. e.nodev(in)
  70. } else {
  71. yaml_document_start_event_initialize(&e.event, nil, nil, true)
  72. e.emit()
  73. e.marshal(tag, in)
  74. yaml_document_end_event_initialize(&e.event, true)
  75. e.emit()
  76. }
  77. }
  78. func (e *encoder) marshal(tag string, in reflect.Value) {
  79. if !in.IsValid() || in.Kind() == reflect.Ptr && in.IsNil() {
  80. e.nilv()
  81. return
  82. }
  83. iface := in.Interface()
  84. switch value := iface.(type) {
  85. case *Node:
  86. e.nodev(in)
  87. return
  88. case time.Time:
  89. e.timev(tag, in)
  90. return
  91. case *time.Time:
  92. e.timev(tag, in.Elem())
  93. return
  94. case time.Duration:
  95. e.stringv(tag, reflect.ValueOf(value.String()))
  96. return
  97. case Marshaler:
  98. v, err := value.MarshalYAML()
  99. if err != nil {
  100. fail(err)
  101. }
  102. if v == nil {
  103. e.nilv()
  104. return
  105. }
  106. in = reflect.ValueOf(v)
  107. case encoding.TextMarshaler:
  108. text, err := value.MarshalText()
  109. if err != nil {
  110. fail(err)
  111. }
  112. in = reflect.ValueOf(string(text))
  113. case nil:
  114. e.nilv()
  115. return
  116. }
  117. switch in.Kind() {
  118. case reflect.Interface:
  119. e.marshal(tag, in.Elem())
  120. case reflect.Map:
  121. e.mapv(tag, in)
  122. case reflect.Ptr:
  123. e.marshal(tag, in.Elem())
  124. case reflect.Struct:
  125. e.structv(tag, in)
  126. case reflect.Slice, reflect.Array:
  127. e.slicev(tag, in)
  128. case reflect.String:
  129. e.stringv(tag, in)
  130. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  131. e.intv(tag, in)
  132. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  133. e.uintv(tag, in)
  134. case reflect.Float32, reflect.Float64:
  135. e.floatv(tag, in)
  136. case reflect.Bool:
  137. e.boolv(tag, in)
  138. default:
  139. panic("cannot marshal type: " + in.Type().String())
  140. }
  141. }
  142. func (e *encoder) mapv(tag string, in reflect.Value) {
  143. e.mappingv(tag, func() {
  144. keys := keyList(in.MapKeys())
  145. sort.Sort(keys)
  146. for _, k := range keys {
  147. e.marshal("", k)
  148. e.marshal("", in.MapIndex(k))
  149. }
  150. })
  151. }
  152. func (e *encoder) itemsv(tag string, in reflect.Value) {
  153. e.mappingv(tag, func() {
  154. slice := in.Convert(reflect.TypeOf([]MapItem{})).Interface().([]MapItem)
  155. for _, item := range slice {
  156. e.marshal("", reflect.ValueOf(item.Key))
  157. e.marshal("", reflect.ValueOf(item.Value))
  158. }
  159. })
  160. }
  161. func (e *encoder) structv(tag string, in reflect.Value) {
  162. sinfo, err := getStructInfo(in.Type())
  163. if err != nil {
  164. panic(err)
  165. }
  166. e.mappingv(tag, func() {
  167. for _, info := range sinfo.FieldsList {
  168. var value reflect.Value
  169. if info.Inline == nil {
  170. value = in.Field(info.Num)
  171. } else {
  172. value = in.FieldByIndex(info.Inline)
  173. }
  174. if info.OmitEmpty && isZero(value) {
  175. continue
  176. }
  177. e.marshal("", reflect.ValueOf(info.Key))
  178. e.flow = info.Flow
  179. e.marshal("", value)
  180. }
  181. if sinfo.InlineMap >= 0 {
  182. m := in.Field(sinfo.InlineMap)
  183. if m.Len() > 0 {
  184. e.flow = false
  185. keys := keyList(m.MapKeys())
  186. sort.Sort(keys)
  187. for _, k := range keys {
  188. if _, found := sinfo.FieldsMap[k.String()]; found {
  189. panic(fmt.Sprintf("Can't have key %q in inlined map; conflicts with struct field", k.String()))
  190. }
  191. e.marshal("", k)
  192. e.flow = false
  193. e.marshal("", m.MapIndex(k))
  194. }
  195. }
  196. }
  197. })
  198. }
  199. func (e *encoder) mappingv(tag string, f func()) {
  200. implicit := tag == ""
  201. style := yaml_BLOCK_MAPPING_STYLE
  202. if e.flow {
  203. e.flow = false
  204. style = yaml_FLOW_MAPPING_STYLE
  205. }
  206. yaml_mapping_start_event_initialize(&e.event, nil, []byte(tag), implicit, style)
  207. e.emit()
  208. f()
  209. yaml_mapping_end_event_initialize(&e.event)
  210. e.emit()
  211. }
  212. func (e *encoder) slicev(tag string, in reflect.Value) {
  213. implicit := tag == ""
  214. style := yaml_BLOCK_SEQUENCE_STYLE
  215. if e.flow {
  216. e.flow = false
  217. style = yaml_FLOW_SEQUENCE_STYLE
  218. }
  219. e.must(yaml_sequence_start_event_initialize(&e.event, nil, []byte(tag), implicit, style))
  220. e.emit()
  221. n := in.Len()
  222. for i := 0; i < n; i++ {
  223. e.marshal("", in.Index(i))
  224. }
  225. e.must(yaml_sequence_end_event_initialize(&e.event))
  226. e.emit()
  227. }
  228. // isBase60 returns whether s is in base 60 notation as defined in YAML 1.1.
  229. //
  230. // The base 60 float notation in YAML 1.1 is a terrible idea and is unsupported
  231. // in YAML 1.2 and by this package, but these should be marshalled quoted for
  232. // the time being for compatibility with other parsers.
  233. func isBase60Float(s string) (result bool) {
  234. // Fast path.
  235. if s == "" {
  236. return false
  237. }
  238. c := s[0]
  239. if !(c == '+' || c == '-' || c >= '0' && c <= '9') || strings.IndexByte(s, ':') < 0 {
  240. return false
  241. }
  242. // Do the full match.
  243. return base60float.MatchString(s)
  244. }
  245. // From http://yaml.org/type/float.html, except the regular expression there
  246. // is bogus. In practice parsers do not enforce the "\.[0-9_]*" suffix.
  247. var base60float = regexp.MustCompile(`^[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+(?:\.[0-9_]*)?$`)
  248. func (e *encoder) stringv(tag string, in reflect.Value) {
  249. var style yaml_scalar_style_t
  250. s := in.String()
  251. canUsePlain := true
  252. switch {
  253. case !utf8.ValidString(s):
  254. if tag == yaml_BINARY_TAG {
  255. failf("explicitly tagged !!binary data must be base64-encoded")
  256. }
  257. if tag != "" {
  258. failf("cannot marshal invalid UTF-8 data as %s", shortTag(tag))
  259. }
  260. // It can't be encoded directly as YAML so use a binary tag
  261. // and encode it as base64.
  262. tag = yaml_BINARY_TAG
  263. s = encodeBase64(s)
  264. case tag == "":
  265. // Check to see if it would resolve to a specific
  266. // tag when encoded unquoted. If it doesn't,
  267. // there's no need to quote it.
  268. rtag, _ := resolve("", s)
  269. canUsePlain = rtag == yaml_STR_TAG && !isBase60Float(s)
  270. }
  271. // Note: it's possible for user code to emit invalid YAML
  272. // if they explicitly specify a tag and a string containing
  273. // text that's incompatible with that tag.
  274. switch {
  275. case strings.Contains(s, "\n"):
  276. style = yaml_LITERAL_SCALAR_STYLE
  277. case canUsePlain:
  278. style = yaml_PLAIN_SCALAR_STYLE
  279. default:
  280. style = yaml_DOUBLE_QUOTED_SCALAR_STYLE
  281. }
  282. e.emitScalar(s, "", tag, style, nil, nil, nil)
  283. }
  284. func (e *encoder) boolv(tag string, in reflect.Value) {
  285. var s string
  286. if in.Bool() {
  287. s = "true"
  288. } else {
  289. s = "false"
  290. }
  291. e.emitScalar(s, "", tag, yaml_PLAIN_SCALAR_STYLE, nil, nil, nil)
  292. }
  293. func (e *encoder) intv(tag string, in reflect.Value) {
  294. s := strconv.FormatInt(in.Int(), 10)
  295. e.emitScalar(s, "", tag, yaml_PLAIN_SCALAR_STYLE, nil, nil, nil)
  296. }
  297. func (e *encoder) uintv(tag string, in reflect.Value) {
  298. s := strconv.FormatUint(in.Uint(), 10)
  299. e.emitScalar(s, "", tag, yaml_PLAIN_SCALAR_STYLE, nil, nil, nil)
  300. }
  301. func (e *encoder) timev(tag string, in reflect.Value) {
  302. t := in.Interface().(time.Time)
  303. s := t.Format(time.RFC3339Nano)
  304. e.emitScalar(s, "", tag, yaml_PLAIN_SCALAR_STYLE, nil, nil, nil)
  305. }
  306. func (e *encoder) floatv(tag string, in reflect.Value) {
  307. // Issue #352: When formatting, use the precision of the underlying value
  308. precision := 64
  309. if in.Kind() == reflect.Float32 {
  310. precision = 32
  311. }
  312. s := strconv.FormatFloat(in.Float(), 'g', -1, precision)
  313. switch s {
  314. case "+Inf":
  315. s = ".inf"
  316. case "-Inf":
  317. s = "-.inf"
  318. case "NaN":
  319. s = ".nan"
  320. }
  321. e.emitScalar(s, "", tag, yaml_PLAIN_SCALAR_STYLE, nil, nil, nil)
  322. }
  323. func (e *encoder) nilv() {
  324. e.emitScalar("null", "", "", yaml_PLAIN_SCALAR_STYLE, nil, nil, nil)
  325. }
  326. func (e *encoder) emitScalar(value, anchor, tag string, style yaml_scalar_style_t, header, inline, footer []byte) {
  327. // TODO Kill this function. Replace all initialize calls by their underlining Go literals.
  328. implicit := tag == ""
  329. e.must(yaml_scalar_event_initialize(&e.event, []byte(anchor), []byte(tag), []byte(value), implicit, implicit, style))
  330. e.event.header_comment = header
  331. e.event.inline_comment = inline
  332. e.event.footer_comment = footer
  333. e.emit()
  334. }
  335. func (e *encoder) nodev(in reflect.Value) {
  336. e.node(in.Interface().(*Node))
  337. }
  338. func (e *encoder) node(node *Node) {
  339. switch node.Kind {
  340. case DocumentNode:
  341. yaml_document_start_event_initialize(&e.event, nil, nil, true)
  342. e.event.header_comment = []byte(node.Header)
  343. e.emit()
  344. for _, node := range node.Children {
  345. e.node(node)
  346. }
  347. yaml_document_end_event_initialize(&e.event, true)
  348. e.event.footer_comment = []byte(node.Footer)
  349. e.emit()
  350. case SequenceNode:
  351. style := yaml_BLOCK_SEQUENCE_STYLE
  352. if node.Style&FlowStyle != 0 {
  353. style = yaml_FLOW_SEQUENCE_STYLE
  354. }
  355. e.must(yaml_sequence_start_event_initialize(&e.event, nil, []byte(node.Tag), node.implicit(), style))
  356. e.event.header_comment = []byte(node.Header)
  357. e.emit()
  358. for _, node := range node.Children {
  359. e.node(node)
  360. }
  361. e.must(yaml_sequence_end_event_initialize(&e.event))
  362. e.event.inline_comment = []byte(node.Inline)
  363. e.event.footer_comment = []byte(node.Footer)
  364. e.emit()
  365. case MappingNode:
  366. style := yaml_BLOCK_MAPPING_STYLE
  367. if node.Style&FlowStyle != 0 {
  368. style = yaml_FLOW_MAPPING_STYLE
  369. }
  370. yaml_mapping_start_event_initialize(&e.event, nil, []byte(node.Tag), node.implicit(), style)
  371. e.event.header_comment = []byte(node.Header)
  372. e.emit()
  373. for i := 0; i+1 < len(node.Children); i += 2 {
  374. e.node(node.Children[i])
  375. e.node(node.Children[i+1])
  376. }
  377. yaml_mapping_end_event_initialize(&e.event)
  378. e.event.inline_comment = []byte(node.Inline)
  379. e.event.footer_comment = []byte(node.Footer)
  380. e.emit()
  381. case ScalarNode, AliasNode:
  382. style := yaml_PLAIN_SCALAR_STYLE
  383. switch {
  384. case node.Style&DoubleQuotedStyle != 0:
  385. style = yaml_DOUBLE_QUOTED_SCALAR_STYLE
  386. case node.Style&SingleQuotedStyle != 0:
  387. style = yaml_SINGLE_QUOTED_SCALAR_STYLE
  388. case node.Style&LiteralStyle != 0:
  389. style = yaml_LITERAL_SCALAR_STYLE
  390. case node.Style&FoldedStyle != 0:
  391. style = yaml_FOLDED_SCALAR_STYLE
  392. }
  393. if style == yaml_PLAIN_SCALAR_STYLE && strings.Contains(node.Value, "\n") {
  394. style = yaml_LITERAL_SCALAR_STYLE
  395. }
  396. e.emitScalar(node.Value, "", node.Tag, style, []byte(node.Header), []byte(node.Inline), []byte(node.Footer))
  397. // TODO Check if binaries are being decoded into node.Value or not.
  398. //switch {
  399. //if !utf8.ValidString(s) {
  400. // if tag == yaml_BINARY_TAG {
  401. // failf("explicitly tagged !!binary data must be base64-encoded")
  402. // }
  403. // if tag != "" {
  404. // failf("cannot marshal invalid UTF-8 data as %s", shortTag(tag))
  405. // }
  406. // // It can't be encoded directly as YAML so use a binary tag
  407. // // and encode it as base64.
  408. // tag = yaml_BINARY_TAG
  409. // s = encodeBase64(s)
  410. //}
  411. }
  412. }