encode.go 9.8 KB

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