encode.go 12 KB

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