encode.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. package goyaml
  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. fields, err := getStructFields(in.Type())
  103. if err != nil {
  104. panic(err)
  105. }
  106. e.mappingv(tag, func() {
  107. for _, info := range fields.List {
  108. value := in.Field(info.Num)
  109. if info.OmitEmpty && isZero(value) {
  110. continue
  111. }
  112. e.marshal("", reflect.ValueOf(info.Key))
  113. e.flow = info.Flow
  114. e.marshal("", value)
  115. }
  116. })
  117. }
  118. func (e *encoder) mappingv(tag string, f func()) {
  119. implicit := tag == ""
  120. style := yaml_BLOCK_MAPPING_STYLE
  121. if e.flow {
  122. e.flow = false
  123. style = yaml_FLOW_MAPPING_STYLE
  124. }
  125. e.must(yaml_mapping_start_event_initialize(&e.event, nil, []byte(tag), implicit, style))
  126. e.emit()
  127. f()
  128. e.must(yaml_mapping_end_event_initialize(&e.event))
  129. e.emit()
  130. }
  131. func (e *encoder) slicev(tag string, in reflect.Value) {
  132. implicit := tag == ""
  133. style := yaml_BLOCK_SEQUENCE_STYLE
  134. if e.flow {
  135. e.flow = false
  136. style = yaml_FLOW_SEQUENCE_STYLE
  137. }
  138. e.must(yaml_sequence_start_event_initialize(&e.event, nil, []byte(tag), implicit, style))
  139. e.emit()
  140. n := in.Len()
  141. for i := 0; i < n; i++ {
  142. e.marshal("", in.Index(i))
  143. }
  144. e.must(yaml_sequence_end_event_initialize(&e.event))
  145. e.emit()
  146. }
  147. func (e *encoder) stringv(tag string, in reflect.Value) {
  148. var style yaml_scalar_style_t
  149. s := in.String()
  150. if rtag, _ := resolve("", s); rtag != "!!str" {
  151. style = yaml_DOUBLE_QUOTED_SCALAR_STYLE
  152. } else {
  153. style = yaml_PLAIN_SCALAR_STYLE
  154. }
  155. e.emitScalar(s, "", tag, style)
  156. }
  157. func (e *encoder) boolv(tag string, in reflect.Value) {
  158. var s string
  159. if in.Bool() {
  160. s = "true"
  161. } else {
  162. s = "false"
  163. }
  164. e.emitScalar(s, "", tag, yaml_PLAIN_SCALAR_STYLE)
  165. }
  166. func (e *encoder) intv(tag string, in reflect.Value) {
  167. s := strconv.FormatInt(in.Int(), 10)
  168. e.emitScalar(s, "", tag, yaml_PLAIN_SCALAR_STYLE)
  169. }
  170. func (e *encoder) uintv(tag string, in reflect.Value) {
  171. s := strconv.FormatUint(in.Uint(), 10)
  172. e.emitScalar(s, "", tag, yaml_PLAIN_SCALAR_STYLE)
  173. }
  174. func (e *encoder) floatv(tag string, in reflect.Value) {
  175. // FIXME: Handle 64 bits here.
  176. s := strconv.FormatFloat(float64(in.Float()), 'g', -1, 32)
  177. switch s {
  178. case "+Inf":
  179. s = ".inf"
  180. case "-Inf":
  181. s = "-.inf"
  182. case "NaN":
  183. s = ".nan"
  184. }
  185. e.emitScalar(s, "", tag, yaml_PLAIN_SCALAR_STYLE)
  186. }
  187. func (e *encoder) nilv() {
  188. e.emitScalar("null", "", "", yaml_PLAIN_SCALAR_STYLE)
  189. }
  190. func (e *encoder) emitScalar(value, anchor, tag string, style yaml_scalar_style_t) {
  191. implicit := tag == ""
  192. if !implicit {
  193. style = yaml_PLAIN_SCALAR_STYLE
  194. }
  195. e.must(yaml_scalar_event_initialize(&e.event, []byte(anchor), []byte(tag), []byte(value), implicit, implicit, style))
  196. e.emit()
  197. }