encode.go 4.9 KB

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