encode.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. package yaml
  2. import (
  3. "encoding"
  4. "fmt"
  5. "io"
  6. "reflect"
  7. "regexp"
  8. "sort"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. type encoder struct {
  14. emitter yaml_emitter_t
  15. event yaml_event_t
  16. out []byte
  17. flow bool
  18. // doneInit holds whether the initial stream_start_event has been
  19. // emitted.
  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. yaml_stream_start_event_initialize(&e.event, yaml_UTF8_ENCODING)
  41. e.emit()
  42. e.doneInit = true
  43. }
  44. func (e *encoder) finish() {
  45. e.emitter.open_ended = false
  46. yaml_stream_end_event_initialize(&e.event)
  47. e.emit()
  48. }
  49. func (e *encoder) destroy() {
  50. yaml_emitter_delete(&e.emitter)
  51. }
  52. func (e *encoder) emit() {
  53. // This will internally delete the e.event value.
  54. e.must(yaml_emitter_emit(&e.emitter, &e.event))
  55. }
  56. func (e *encoder) must(ok bool) {
  57. if !ok {
  58. msg := e.emitter.problem
  59. if msg == "" {
  60. msg = "unknown problem generating YAML content"
  61. }
  62. failf("%s", msg)
  63. }
  64. }
  65. func (e *encoder) marshalDoc(tag string, in reflect.Value) {
  66. e.init()
  67. yaml_document_start_event_initialize(&e.event, nil, nil, true)
  68. e.emit()
  69. e.marshal(tag, in)
  70. yaml_document_end_event_initialize(&e.event, true)
  71. e.emit()
  72. }
  73. func (e *encoder) marshal(tag string, in reflect.Value) {
  74. if !in.IsValid() {
  75. e.nilv()
  76. return
  77. }
  78. iface := in.Interface()
  79. if m, ok := iface.(Marshaler); ok {
  80. v, err := m.MarshalYAML()
  81. if err != nil {
  82. fail(err)
  83. }
  84. if v == nil {
  85. e.nilv()
  86. return
  87. }
  88. in = reflect.ValueOf(v)
  89. } else if m, ok := iface.(encoding.TextMarshaler); ok {
  90. text, err := m.MarshalText()
  91. if err != nil {
  92. fail(err)
  93. }
  94. in = reflect.ValueOf(string(text))
  95. }
  96. switch in.Kind() {
  97. case reflect.Interface:
  98. if in.IsNil() {
  99. e.nilv()
  100. } else {
  101. e.marshal(tag, in.Elem())
  102. }
  103. case reflect.Map:
  104. e.mapv(tag, in)
  105. case reflect.Ptr:
  106. if in.IsNil() {
  107. e.nilv()
  108. } else {
  109. e.marshal(tag, in.Elem())
  110. }
  111. case reflect.Struct:
  112. e.structv(tag, in)
  113. case reflect.Slice:
  114. if in.Type().Elem() == mapItemType {
  115. e.itemsv(tag, in)
  116. } else {
  117. e.slicev(tag, in)
  118. }
  119. case reflect.String:
  120. e.stringv(tag, in)
  121. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  122. if in.Type() == durationType {
  123. e.stringv(tag, reflect.ValueOf(iface.(time.Duration).String()))
  124. } else {
  125. e.intv(tag, in)
  126. }
  127. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  128. e.uintv(tag, in)
  129. case reflect.Float32, reflect.Float64:
  130. e.floatv(tag, in)
  131. case reflect.Bool:
  132. e.boolv(tag, in)
  133. default:
  134. panic("cannot marshal type: " + in.Type().String())
  135. }
  136. }
  137. func (e *encoder) mapv(tag string, in reflect.Value) {
  138. e.mappingv(tag, func() {
  139. keys := keyList(in.MapKeys())
  140. sort.Sort(keys)
  141. for _, k := range keys {
  142. e.marshal("", k)
  143. e.marshal("", in.MapIndex(k))
  144. }
  145. })
  146. }
  147. func (e *encoder) itemsv(tag string, in reflect.Value) {
  148. e.mappingv(tag, func() {
  149. slice := in.Convert(reflect.TypeOf([]MapItem{})).Interface().([]MapItem)
  150. for _, item := range slice {
  151. e.marshal("", reflect.ValueOf(item.Key))
  152. e.marshal("", reflect.ValueOf(item.Value))
  153. }
  154. })
  155. }
  156. func (e *encoder) structv(tag string, in reflect.Value) {
  157. sinfo, err := getStructInfo(in.Type())
  158. if err != nil {
  159. panic(err)
  160. }
  161. e.mappingv(tag, func() {
  162. for _, info := range sinfo.FieldsList {
  163. var value reflect.Value
  164. if info.Inline == nil {
  165. value = in.Field(info.Num)
  166. } else {
  167. value = in.FieldByIndex(info.Inline)
  168. }
  169. if info.OmitEmpty && isZero(value) {
  170. continue
  171. }
  172. e.marshal("", reflect.ValueOf(info.Key))
  173. e.flow = info.Flow
  174. e.marshal("", value)
  175. }
  176. if sinfo.InlineMap >= 0 {
  177. m := in.Field(sinfo.InlineMap)
  178. if m.Len() > 0 {
  179. e.flow = false
  180. keys := keyList(m.MapKeys())
  181. sort.Sort(keys)
  182. for _, k := range keys {
  183. if _, found := sinfo.FieldsMap[k.String()]; found {
  184. panic(fmt.Sprintf("Can't have key %q in inlined map; conflicts with struct field", k.String()))
  185. }
  186. e.marshal("", k)
  187. e.flow = false
  188. e.marshal("", m.MapIndex(k))
  189. }
  190. }
  191. }
  192. })
  193. }
  194. func (e *encoder) mappingv(tag string, f func()) {
  195. implicit := tag == ""
  196. style := yaml_BLOCK_MAPPING_STYLE
  197. if e.flow {
  198. e.flow = false
  199. style = yaml_FLOW_MAPPING_STYLE
  200. }
  201. yaml_mapping_start_event_initialize(&e.event, nil, []byte(tag), implicit, style)
  202. e.emit()
  203. f()
  204. yaml_mapping_end_event_initialize(&e.event)
  205. e.emit()
  206. }
  207. func (e *encoder) slicev(tag string, in reflect.Value) {
  208. implicit := tag == ""
  209. style := yaml_BLOCK_SEQUENCE_STYLE
  210. if e.flow {
  211. e.flow = false
  212. style = yaml_FLOW_SEQUENCE_STYLE
  213. }
  214. e.must(yaml_sequence_start_event_initialize(&e.event, nil, []byte(tag), implicit, style))
  215. e.emit()
  216. n := in.Len()
  217. for i := 0; i < n; i++ {
  218. e.marshal("", in.Index(i))
  219. }
  220. e.must(yaml_sequence_end_event_initialize(&e.event))
  221. e.emit()
  222. }
  223. // isBase60 returns whether s is in base 60 notation as defined in YAML 1.1.
  224. //
  225. // The base 60 float notation in YAML 1.1 is a terrible idea and is unsupported
  226. // in YAML 1.2 and by this package, but these should be marshalled quoted for
  227. // the time being for compatibility with other parsers.
  228. func isBase60Float(s string) (result bool) {
  229. // Fast path.
  230. if s == "" {
  231. return false
  232. }
  233. c := s[0]
  234. if !(c == '+' || c == '-' || c >= '0' && c <= '9') || strings.IndexByte(s, ':') < 0 {
  235. return false
  236. }
  237. // Do the full match.
  238. return base60float.MatchString(s)
  239. }
  240. // From http://yaml.org/type/float.html, except the regular expression there
  241. // is bogus. In practice parsers do not enforce the "\.[0-9_]*" suffix.
  242. var base60float = regexp.MustCompile(`^[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+(?:\.[0-9_]*)?$`)
  243. func (e *encoder) stringv(tag string, in reflect.Value) {
  244. var style yaml_scalar_style_t
  245. s := in.String()
  246. rtag, rs := resolve("", s)
  247. if rtag == yaml_BINARY_TAG {
  248. if tag == "" || tag == yaml_STR_TAG {
  249. tag = rtag
  250. s = rs.(string)
  251. } else if tag == yaml_BINARY_TAG {
  252. failf("explicitly tagged !!binary data must be base64-encoded")
  253. } else {
  254. failf("cannot marshal invalid UTF-8 data as %s", shortTag(tag))
  255. }
  256. }
  257. if tag == "" && (rtag != yaml_STR_TAG || isBase60Float(s)) {
  258. style = yaml_DOUBLE_QUOTED_SCALAR_STYLE
  259. } else if strings.Contains(s, "\n") {
  260. style = yaml_LITERAL_SCALAR_STYLE
  261. } else {
  262. style = yaml_PLAIN_SCALAR_STYLE
  263. }
  264. e.emitScalar(s, "", tag, style)
  265. }
  266. func (e *encoder) boolv(tag string, in reflect.Value) {
  267. var s string
  268. if in.Bool() {
  269. s = "true"
  270. } else {
  271. s = "false"
  272. }
  273. e.emitScalar(s, "", tag, yaml_PLAIN_SCALAR_STYLE)
  274. }
  275. func (e *encoder) intv(tag string, in reflect.Value) {
  276. s := strconv.FormatInt(in.Int(), 10)
  277. e.emitScalar(s, "", tag, yaml_PLAIN_SCALAR_STYLE)
  278. }
  279. func (e *encoder) uintv(tag string, in reflect.Value) {
  280. s := strconv.FormatUint(in.Uint(), 10)
  281. e.emitScalar(s, "", tag, yaml_PLAIN_SCALAR_STYLE)
  282. }
  283. func (e *encoder) floatv(tag string, in reflect.Value) {
  284. // FIXME: Handle 64 bits here.
  285. s := strconv.FormatFloat(float64(in.Float()), 'g', -1, 32)
  286. switch s {
  287. case "+Inf":
  288. s = ".inf"
  289. case "-Inf":
  290. s = "-.inf"
  291. case "NaN":
  292. s = ".nan"
  293. }
  294. e.emitScalar(s, "", tag, yaml_PLAIN_SCALAR_STYLE)
  295. }
  296. func (e *encoder) nilv() {
  297. e.emitScalar("null", "", "", yaml_PLAIN_SCALAR_STYLE)
  298. }
  299. func (e *encoder) emitScalar(value, anchor, tag string, style yaml_scalar_style_t) {
  300. implicit := tag == ""
  301. e.must(yaml_scalar_event_initialize(&e.event, []byte(anchor), []byte(tag), []byte(value), implicit, implicit, style))
  302. e.emit()
  303. }