encode.go 7.3 KB

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