encode.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. yaml_document_start_event_initialize(&e.event, nil, nil, true)
  69. e.emit()
  70. e.marshal(tag, in)
  71. yaml_document_end_event_initialize(&e.event, true)
  72. e.emit()
  73. }
  74. func (e *encoder) marshal(tag string, in reflect.Value) {
  75. if !in.IsValid() || in.Kind() == reflect.Ptr && in.IsNil() {
  76. e.nilv()
  77. return
  78. }
  79. iface := in.Interface()
  80. switch m := iface.(type) {
  81. case time.Time, *time.Time:
  82. // Although time.Time implements TextMarshaler,
  83. // we don't want to treat it as a string for YAML
  84. // purposes because YAML has special support for
  85. // timestamps.
  86. case Marshaler:
  87. v, err := m.MarshalYAML()
  88. if err != nil {
  89. fail(err)
  90. }
  91. if v == nil {
  92. e.nilv()
  93. return
  94. }
  95. in = reflect.ValueOf(v)
  96. case encoding.TextMarshaler:
  97. text, err := m.MarshalText()
  98. if err != nil {
  99. fail(err)
  100. }
  101. in = reflect.ValueOf(string(text))
  102. case nil:
  103. e.nilv()
  104. return
  105. }
  106. switch in.Kind() {
  107. case reflect.Interface:
  108. e.marshal(tag, in.Elem())
  109. case reflect.Map:
  110. e.mapv(tag, in)
  111. case reflect.Ptr:
  112. if in.Type() == ptrTimeType {
  113. e.timev(tag, in.Elem())
  114. } else {
  115. e.marshal(tag, in.Elem())
  116. }
  117. case reflect.Struct:
  118. switch in.Type() {
  119. case timeType:
  120. e.timev(tag, in)
  121. case mergeTagType:
  122. e.mergeTagv(tag)
  123. default:
  124. e.structv(tag, in)
  125. }
  126. case reflect.Slice:
  127. if in.Type().Elem() == mapItemType {
  128. e.itemsv(tag, in)
  129. } else {
  130. e.slicev(tag, in)
  131. }
  132. case reflect.String:
  133. e.stringv(tag, in)
  134. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  135. if in.Type() == durationType {
  136. e.stringv(tag, reflect.ValueOf(iface.(time.Duration).String()))
  137. } else {
  138. e.intv(tag, in)
  139. }
  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) itemsv(tag string, in reflect.Value) {
  161. e.mappingv(tag, func() {
  162. slice := in.Convert(reflect.TypeOf([]MapItem{})).Interface().([]MapItem)
  163. for _, item := range slice {
  164. e.marshal("", reflect.ValueOf(item.Key))
  165. e.marshal("", reflect.ValueOf(item.Value))
  166. }
  167. })
  168. }
  169. func (e *encoder) structv(tag string, in reflect.Value) {
  170. sinfo, err := getStructInfo(in.Type())
  171. if err != nil {
  172. panic(err)
  173. }
  174. e.mappingv(tag, func() {
  175. for _, info := range sinfo.FieldsList {
  176. var value reflect.Value
  177. if info.Inline == nil {
  178. value = in.Field(info.Num)
  179. } else {
  180. value = in.FieldByIndex(info.Inline)
  181. }
  182. if info.OmitEmpty && isZero(value) {
  183. continue
  184. }
  185. e.marshal("", reflect.ValueOf(info.Key))
  186. e.flow = info.Flow
  187. e.marshal("", value)
  188. }
  189. if sinfo.InlineMap >= 0 {
  190. m := in.Field(sinfo.InlineMap)
  191. if m.Len() > 0 {
  192. e.flow = false
  193. keys := keyList(m.MapKeys())
  194. sort.Sort(keys)
  195. for _, k := range keys {
  196. if _, found := sinfo.FieldsMap[k.String()]; found {
  197. panic(fmt.Sprintf("Can't have key %q in inlined map; conflicts with struct field", k.String()))
  198. }
  199. e.marshal("", k)
  200. e.flow = false
  201. e.marshal("", m.MapIndex(k))
  202. }
  203. }
  204. }
  205. })
  206. }
  207. func (e *encoder) mappingv(tag string, f func()) {
  208. implicit := tag == ""
  209. style := yaml_BLOCK_MAPPING_STYLE
  210. if e.flow {
  211. e.flow = false
  212. style = yaml_FLOW_MAPPING_STYLE
  213. }
  214. yaml_mapping_start_event_initialize(&e.event, nil, []byte(tag), implicit, style)
  215. e.emit()
  216. f()
  217. yaml_mapping_end_event_initialize(&e.event)
  218. e.emit()
  219. }
  220. func (e *encoder) slicev(tag string, in reflect.Value) {
  221. implicit := tag == ""
  222. style := yaml_BLOCK_SEQUENCE_STYLE
  223. if e.flow {
  224. e.flow = false
  225. style = yaml_FLOW_SEQUENCE_STYLE
  226. }
  227. e.must(yaml_sequence_start_event_initialize(&e.event, nil, []byte(tag), implicit, style))
  228. e.emit()
  229. n := in.Len()
  230. for i := 0; i < n; i++ {
  231. e.marshal("", in.Index(i))
  232. }
  233. e.must(yaml_sequence_end_event_initialize(&e.event))
  234. e.emit()
  235. }
  236. // isBase60 returns whether s is in base 60 notation as defined in YAML 1.1.
  237. //
  238. // The base 60 float notation in YAML 1.1 is a terrible idea and is unsupported
  239. // in YAML 1.2 and by this package, but these should be marshalled quoted for
  240. // the time being for compatibility with other parsers.
  241. func isBase60Float(s string) (result bool) {
  242. // Fast path.
  243. if s == "" {
  244. return false
  245. }
  246. c := s[0]
  247. if !(c == '+' || c == '-' || c >= '0' && c <= '9') || strings.IndexByte(s, ':') < 0 {
  248. return false
  249. }
  250. // Do the full match.
  251. return base60float.MatchString(s)
  252. }
  253. // From http://yaml.org/type/float.html, except the regular expression there
  254. // is bogus. In practice parsers do not enforce the "\.[0-9_]*" suffix.
  255. var base60float = regexp.MustCompile(`^[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+(?:\.[0-9_]*)?$`)
  256. func (e *encoder) stringv(tag string, in reflect.Value) {
  257. var style yaml_scalar_style_t
  258. s := in.String()
  259. canUsePlain := true
  260. switch {
  261. case !utf8.ValidString(s):
  262. if tag == yaml_BINARY_TAG {
  263. failf("explicitly tagged !!binary data must be base64-encoded")
  264. }
  265. if tag != "" {
  266. failf("cannot marshal invalid UTF-8 data as %s", shortTag(tag))
  267. }
  268. // It can't be encoded directly as YAML so use a binary tag
  269. // and encode it as base64.
  270. tag = yaml_BINARY_TAG
  271. s = encodeBase64(s)
  272. case tag == "":
  273. // Check to see if it would resolve to a specific
  274. // tag when encoded unquoted. If it doesn't,
  275. // there's no need to quote it.
  276. rtag, _ := resolve("", s)
  277. canUsePlain = rtag == yaml_STR_TAG && !isBase60Float(s)
  278. }
  279. // Note: it's possible for user code to emit invalid YAML
  280. // if they explicitly specify a tag and a string containing
  281. // text that's incompatible with that tag.
  282. switch {
  283. case strings.Contains(s, "\n"):
  284. style = yaml_LITERAL_SCALAR_STYLE
  285. case canUsePlain:
  286. style = yaml_PLAIN_SCALAR_STYLE
  287. default:
  288. style = yaml_DOUBLE_QUOTED_SCALAR_STYLE
  289. }
  290. e.emitScalar(s, "", tag, style)
  291. }
  292. func (e *encoder) boolv(tag string, in reflect.Value) {
  293. var s string
  294. if in.Bool() {
  295. s = "true"
  296. } else {
  297. s = "false"
  298. }
  299. e.emitScalar(s, "", tag, yaml_PLAIN_SCALAR_STYLE)
  300. }
  301. func (e *encoder) intv(tag string, in reflect.Value) {
  302. s := strconv.FormatInt(in.Int(), 10)
  303. e.emitScalar(s, "", tag, yaml_PLAIN_SCALAR_STYLE)
  304. }
  305. func (e *encoder) uintv(tag string, in reflect.Value) {
  306. s := strconv.FormatUint(in.Uint(), 10)
  307. e.emitScalar(s, "", tag, yaml_PLAIN_SCALAR_STYLE)
  308. }
  309. func (e *encoder) timev(tag string, in reflect.Value) {
  310. t := in.Interface().(time.Time)
  311. if tag == "" {
  312. tag = yaml_TIMESTAMP_TAG
  313. }
  314. e.emitScalar(t.Format(time.RFC3339Nano), "", tag, yaml_PLAIN_SCALAR_STYLE)
  315. }
  316. func (e *encoder) mergeTagv(tag string) {
  317. e.emitScalar("<<", "", tag, yaml_PLAIN_SCALAR_STYLE)
  318. }
  319. func (e *encoder) floatv(tag string, in reflect.Value) {
  320. s := strconv.FormatFloat(in.Float(), 'g', -1, 64)
  321. switch s {
  322. case "+Inf":
  323. s = ".inf"
  324. case "-Inf":
  325. s = "-.inf"
  326. case "NaN":
  327. s = ".nan"
  328. }
  329. e.emitScalar(s, "", tag, yaml_PLAIN_SCALAR_STYLE)
  330. }
  331. func (e *encoder) nilv() {
  332. e.emitScalar("null", "", "", yaml_PLAIN_SCALAR_STYLE)
  333. }
  334. func (e *encoder) emitScalar(value, anchor, tag string, style yaml_scalar_style_t) {
  335. implicit := tag == ""
  336. e.must(yaml_scalar_event_initialize(&e.event, []byte(anchor), []byte(tag), []byte(value), implicit, implicit, style))
  337. e.emit()
  338. }