encode.go 15 KB

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