15 emitter yaml_emitter_t
21 func newEncoder() (e *encoder) {
23 e.must(yaml_emitter_initialize(&e.emitter))
24 yaml_emitter_set_output_string(&e.emitter, &e.out)
25 yaml_emitter_set_unicode(&e.emitter, true)
26 e.must(yaml_stream_start_event_initialize(&e.event, yaml_UTF8_ENCODING))
28 e.must(yaml_document_start_event_initialize(&e.event, nil, nil, true))
33 func (e *encoder) finish() {
34 e.must(yaml_document_end_event_initialize(&e.event, true))
36 e.emitter.open_ended = false
37 e.must(yaml_stream_end_event_initialize(&e.event))
41 func (e *encoder) destroy() {
42 yaml_emitter_delete(&e.emitter)
45 func (e *encoder) emit() {
46 // This will internally delete the e.event value.
47 if !yaml_emitter_emit(&e.emitter, &e.event) && e.event.typ != yaml_DOCUMENT_END_EVENT && e.event.typ != yaml_STREAM_END_EVENT {
52 func (e *encoder) must(ok bool) {
54 msg := e.emitter.problem
56 msg = "unknown problem generating YAML content"
62 func (e *encoder) marshal(tag string, in reflect.Value) {
67 iface := in.Interface()
68 if m, ok := iface.(Marshaler); ok {
69 v, err := m.MarshalYAML()
77 in = reflect.ValueOf(v)
78 } else if m, ok := iface.(encoding.TextMarshaler); ok {
79 text, err := m.MarshalText()
83 in = reflect.ValueOf(string(text))
86 case reflect.Interface:
90 e.marshal(tag, in.Elem())
98 e.marshal(tag, in.Elem())
103 if in.Type().Elem() == mapItemType {
110 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
111 if in.Type() == durationType {
112 e.stringv(tag, reflect.ValueOf(iface.(time.Duration).String()))
116 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
118 case reflect.Float32, reflect.Float64:
123 panic("cannot marshal type: " + in.Type().String())
127 func (e *encoder) mapv(tag string, in reflect.Value) {
128 e.mappingv(tag, func() {
129 keys := keyList(in.MapKeys())
131 for _, k := range keys {
133 e.marshal("", in.MapIndex(k))
138 func (e *encoder) itemsv(tag string, in reflect.Value) {
139 e.mappingv(tag, func() {
140 slice := in.Convert(reflect.TypeOf([]MapItem{})).Interface().([]MapItem)
141 for _, item := range slice {
142 e.marshal("", reflect.ValueOf(item.Key))
143 e.marshal("", reflect.ValueOf(item.Value))
148 func (e *encoder) structv(tag string, in reflect.Value) {
149 sinfo, err := getStructInfo(in.Type())
153 e.mappingv(tag, func() {
154 for _, info := range sinfo.FieldsList {
155 var value reflect.Value
156 if info.Inline == nil {
157 value = in.Field(info.Num)
159 value = in.FieldByIndex(info.Inline)
161 if info.OmitEmpty && isZero(value) {
164 e.marshal("", reflect.ValueOf(info.Key))
168 if sinfo.InlineMap >= 0 {
169 m := in.Field(sinfo.InlineMap)
172 keys := keyList(m.MapKeys())
174 for _, k := range keys {
175 if _, found := sinfo.FieldsMap[k.String()]; found {
176 panic(fmt.Sprintf("Can't have key %q in inlined map; conflicts with struct field", k.String()))
180 e.marshal("", m.MapIndex(k))
187 func (e *encoder) mappingv(tag string, f func()) {
188 implicit := tag == ""
189 style := yaml_BLOCK_MAPPING_STYLE
192 style = yaml_FLOW_MAPPING_STYLE
194 e.must(yaml_mapping_start_event_initialize(&e.event, nil, []byte(tag), implicit, style))
197 e.must(yaml_mapping_end_event_initialize(&e.event))
201 func (e *encoder) slicev(tag string, in reflect.Value) {
202 implicit := tag == ""
203 style := yaml_BLOCK_SEQUENCE_STYLE
206 style = yaml_FLOW_SEQUENCE_STYLE
208 e.must(yaml_sequence_start_event_initialize(&e.event, nil, []byte(tag), implicit, style))
211 for i := 0; i < n; i++ {
212 e.marshal("", in.Index(i))
214 e.must(yaml_sequence_end_event_initialize(&e.event))
218 // isBase60 returns whether s is in base 60 notation as defined in YAML 1.1.
220 // The base 60 float notation in YAML 1.1 is a terrible idea and is unsupported
221 // in YAML 1.2 and by this package, but these should be marshalled quoted for
222 // the time being for compatibility with other parsers.
223 func isBase60Float(s string) (result bool) {
229 if !(c == '+' || c == '-' || c >= '0' && c <= '9') || strings.IndexByte(s, ':') < 0 {
232 // Do the full match.
233 return base60float.MatchString(s)
236 // From http://yaml.org/type/float.html, except the regular expression there
237 // is bogus. In practice parsers do not enforce the "\.[0-9_]*" suffix.
238 var base60float = regexp.MustCompile(`^[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+(?:\.[0-9_]*)?$`)
240 func (e *encoder) stringv(tag string, in reflect.Value) {
241 var style yaml_scalar_style_t
243 rtag, rs := resolve("", s)
244 if rtag == yaml_BINARY_TAG {
245 if tag == "" || tag == yaml_STR_TAG {
248 } else if tag == yaml_BINARY_TAG {
249 failf("explicitly tagged !!binary data must be base64-encoded")
251 failf("cannot marshal invalid UTF-8 data as %s", shortTag(tag))
254 if tag == "" && (rtag != yaml_STR_TAG || isBase60Float(s)) {
255 style = yaml_DOUBLE_QUOTED_SCALAR_STYLE
256 } else if strings.Contains(s, "\n") {
257 style = yaml_LITERAL_SCALAR_STYLE
259 style = yaml_PLAIN_SCALAR_STYLE
261 e.emitScalar(s, "", tag, style)
264 func (e *encoder) boolv(tag string, in reflect.Value) {
271 e.emitScalar(s, "", tag, yaml_PLAIN_SCALAR_STYLE)
274 func (e *encoder) intv(tag string, in reflect.Value) {
275 s := strconv.FormatInt(in.Int(), 10)
276 e.emitScalar(s, "", tag, yaml_PLAIN_SCALAR_STYLE)
279 func (e *encoder) uintv(tag string, in reflect.Value) {
280 s := strconv.FormatUint(in.Uint(), 10)
281 e.emitScalar(s, "", tag, yaml_PLAIN_SCALAR_STYLE)
284 func (e *encoder) floatv(tag string, in reflect.Value) {
285 // FIXME: Handle 64 bits here.
286 s := strconv.FormatFloat(float64(in.Float()), 'g', -1, 32)
295 e.emitScalar(s, "", tag, yaml_PLAIN_SCALAR_STYLE)
298 func (e *encoder) nilv() {
299 e.emitScalar("null", "", "", yaml_PLAIN_SCALAR_STYLE)
302 func (e *encoder) emitScalar(value, anchor, tag string, style yaml_scalar_style_t) {
303 implicit := tag == ""
304 e.must(yaml_scalar_event_initialize(&e.event, []byte(anchor), []byte(tag), []byte(value), implicit, implicit, style))