encode.go 6.8 KB

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