encode.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. package yaml
  2. import (
  3. "reflect"
  4. "sort"
  5. "strconv"
  6. "time"
  7. )
  8. type encoder struct {
  9. emitter yaml_emitter_t
  10. event yaml_event_t
  11. out []byte
  12. flow bool
  13. }
  14. func newEncoder() (e *encoder) {
  15. e = &encoder{}
  16. e.must(yaml_emitter_initialize(&e.emitter))
  17. yaml_emitter_set_output_string(&e.emitter, &e.out)
  18. e.must(yaml_stream_start_event_initialize(&e.event, yaml_UTF8_ENCODING))
  19. e.emit()
  20. e.must(yaml_document_start_event_initialize(&e.event, nil, nil, true))
  21. e.emit()
  22. return e
  23. }
  24. func (e *encoder) finish() {
  25. e.must(yaml_document_end_event_initialize(&e.event, true))
  26. e.emit()
  27. e.emitter.open_ended = false
  28. e.must(yaml_stream_end_event_initialize(&e.event))
  29. e.emit()
  30. }
  31. func (e *encoder) destroy() {
  32. yaml_emitter_delete(&e.emitter)
  33. }
  34. func (e *encoder) emit() {
  35. // This will internally delete the e.event value.
  36. if !yaml_emitter_emit(&e.emitter, &e.event) && e.event.typ != yaml_DOCUMENT_END_EVENT && e.event.typ != yaml_STREAM_END_EVENT {
  37. e.must(false)
  38. }
  39. }
  40. func (e *encoder) must(ok bool) {
  41. if !ok {
  42. msg := e.emitter.problem
  43. if msg == "" {
  44. msg = "Unknown problem generating YAML content"
  45. }
  46. panic(msg)
  47. }
  48. }
  49. func (e *encoder) marshal(tag string, in reflect.Value) {
  50. var value interface{}
  51. if !in.IsValid() {
  52. e.nilv()
  53. return
  54. }
  55. if getter, ok := in.Interface().(Getter); ok {
  56. tag, value = getter.GetYAML()
  57. if value == nil {
  58. e.nilv()
  59. return
  60. }
  61. in = reflect.ValueOf(value)
  62. }
  63. switch in.Kind() {
  64. case reflect.Interface:
  65. if in.IsNil() {
  66. e.nilv()
  67. } else {
  68. e.marshal(tag, in.Elem())
  69. }
  70. case reflect.Map:
  71. e.mapv(tag, in)
  72. case reflect.Ptr:
  73. if in.IsNil() {
  74. e.nilv()
  75. } else {
  76. e.marshal(tag, in.Elem())
  77. }
  78. case reflect.Struct:
  79. e.structv(tag, in)
  80. case reflect.Slice:
  81. e.slicev(tag, in)
  82. case reflect.String:
  83. e.stringv(tag, in)
  84. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  85. if in.Type() == durationType {
  86. e.stringv(tag, reflect.ValueOf(in.Interface().(time.Duration).String()))
  87. } else {
  88. e.intv(tag, in)
  89. }
  90. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  91. e.uintv(tag, in)
  92. case reflect.Float32, reflect.Float64:
  93. e.floatv(tag, in)
  94. case reflect.Bool:
  95. e.boolv(tag, in)
  96. default:
  97. panic("Can't marshal type yet: " + in.Type().String())
  98. }
  99. }
  100. func (e *encoder) mapv(tag string, in reflect.Value) {
  101. e.mappingv(tag, func() {
  102. keys := keyList(in.MapKeys())
  103. sort.Sort(keys)
  104. for _, k := range keys {
  105. e.marshal("", k)
  106. e.marshal("", in.MapIndex(k))
  107. }
  108. })
  109. }
  110. func (e *encoder) structv(tag string, in reflect.Value) {
  111. sinfo, err := getStructInfo(in.Type())
  112. if err != nil {
  113. panic(err)
  114. }
  115. e.mappingv(tag, func() {
  116. for _, info := range sinfo.FieldsList {
  117. var value reflect.Value
  118. if info.Inline == nil {
  119. value = in.Field(info.Num)
  120. } else {
  121. value = in.FieldByIndex(info.Inline)
  122. }
  123. if info.OmitEmpty && isZero(value) {
  124. continue
  125. }
  126. e.marshal("", reflect.ValueOf(info.Key))
  127. e.flow = info.Flow
  128. e.marshal("", value)
  129. }
  130. })
  131. }
  132. func (e *encoder) mappingv(tag string, f func()) {
  133. implicit := tag == ""
  134. style := yaml_BLOCK_MAPPING_STYLE
  135. if e.flow {
  136. e.flow = false
  137. style = yaml_FLOW_MAPPING_STYLE
  138. }
  139. e.must(yaml_mapping_start_event_initialize(&e.event, nil, []byte(tag), implicit, style))
  140. e.emit()
  141. f()
  142. e.must(yaml_mapping_end_event_initialize(&e.event))
  143. e.emit()
  144. }
  145. func (e *encoder) slicev(tag string, in reflect.Value) {
  146. implicit := tag == ""
  147. style := yaml_BLOCK_SEQUENCE_STYLE
  148. if e.flow {
  149. e.flow = false
  150. style = yaml_FLOW_SEQUENCE_STYLE
  151. }
  152. e.must(yaml_sequence_start_event_initialize(&e.event, nil, []byte(tag), implicit, style))
  153. e.emit()
  154. n := in.Len()
  155. for i := 0; i < n; i++ {
  156. e.marshal("", in.Index(i))
  157. }
  158. e.must(yaml_sequence_end_event_initialize(&e.event))
  159. e.emit()
  160. }
  161. func (e *encoder) stringv(tag string, in reflect.Value) {
  162. var style yaml_scalar_style_t
  163. s := in.String()
  164. if rtag, _ := resolve("", s); rtag != "!!str" {
  165. style = yaml_DOUBLE_QUOTED_SCALAR_STYLE
  166. } else {
  167. style = yaml_PLAIN_SCALAR_STYLE
  168. }
  169. e.emitScalar(s, "", tag, style)
  170. }
  171. func (e *encoder) boolv(tag string, in reflect.Value) {
  172. var s string
  173. if in.Bool() {
  174. s = "true"
  175. } else {
  176. s = "false"
  177. }
  178. e.emitScalar(s, "", tag, yaml_PLAIN_SCALAR_STYLE)
  179. }
  180. func (e *encoder) intv(tag string, in reflect.Value) {
  181. s := strconv.FormatInt(in.Int(), 10)
  182. e.emitScalar(s, "", tag, yaml_PLAIN_SCALAR_STYLE)
  183. }
  184. func (e *encoder) uintv(tag string, in reflect.Value) {
  185. s := strconv.FormatUint(in.Uint(), 10)
  186. e.emitScalar(s, "", tag, yaml_PLAIN_SCALAR_STYLE)
  187. }
  188. func (e *encoder) floatv(tag string, in reflect.Value) {
  189. // FIXME: Handle 64 bits here.
  190. s := strconv.FormatFloat(float64(in.Float()), 'g', -1, 32)
  191. switch s {
  192. case "+Inf":
  193. s = ".inf"
  194. case "-Inf":
  195. s = "-.inf"
  196. case "NaN":
  197. s = ".nan"
  198. }
  199. e.emitScalar(s, "", tag, yaml_PLAIN_SCALAR_STYLE)
  200. }
  201. func (e *encoder) nilv() {
  202. e.emitScalar("null", "", "", yaml_PLAIN_SCALAR_STYLE)
  203. }
  204. func (e *encoder) emitScalar(value, anchor, tag string, style yaml_scalar_style_t) {
  205. implicit := tag == ""
  206. if !implicit {
  207. style = yaml_PLAIN_SCALAR_STYLE
  208. }
  209. e.must(yaml_scalar_event_initialize(&e.event, []byte(anchor), []byte(tag), []byte(value), implicit, implicit, style))
  210. e.emit()
  211. }