You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

encode.go 9.6KB

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