yaml/encode.go

227 строки
5.1 KiB
Go
Исходник Обычный вид История

2014-03-05 22:48:33 +04:00
package yaml
2011-01-10 21:03:42 +03:00
import (
2011-04-14 01:20:44 +04:00
"reflect"
2012-09-20 13:00:32 +04:00
"sort"
2011-04-14 01:20:44 +04:00
"strconv"
2014-04-06 17:56:05 +04:00
"time"
2011-01-10 21:03:42 +03:00
)
type encoder struct {
2013-04-28 08:28:40 +04:00
emitter yaml_emitter_t
event yaml_event_t
2011-04-14 01:20:44 +04:00
out []byte
flow bool
2011-01-10 21:03:42 +03:00
}
func newEncoder() (e *encoder) {
2011-04-14 01:20:44 +04:00
e = &encoder{}
2013-04-28 08:28:40 +04:00
e.must(yaml_emitter_initialize(&e.emitter))
yaml_emitter_set_output_string(&e.emitter, &e.out)
e.must(yaml_stream_start_event_initialize(&e.event, yaml_UTF8_ENCODING))
2011-04-14 01:20:44 +04:00
e.emit()
2013-04-28 08:28:40 +04:00
e.must(yaml_document_start_event_initialize(&e.event, nil, nil, true))
2011-04-14 01:20:44 +04:00
e.emit()
return e
2011-01-10 21:03:42 +03:00
}
func (e *encoder) finish() {
2013-04-28 08:28:40 +04:00
e.must(yaml_document_end_event_initialize(&e.event, true))
2011-04-14 01:20:44 +04:00
e.emit()
2013-04-28 08:28:40 +04:00
e.emitter.open_ended = false
e.must(yaml_stream_end_event_initialize(&e.event))
2011-04-14 01:20:44 +04:00
e.emit()
2011-01-10 21:03:42 +03:00
}
func (e *encoder) destroy() {
2013-04-28 08:28:40 +04:00
yaml_emitter_delete(&e.emitter)
2011-01-10 21:03:42 +03:00
}
func (e *encoder) emit() {
2011-04-14 01:20:44 +04:00
// This will internally delete the e.event value.
2013-04-28 08:28:40 +04:00
if !yaml_emitter_emit(&e.emitter, &e.event) && e.event.typ != yaml_DOCUMENT_END_EVENT && e.event.typ != yaml_STREAM_END_EVENT {
e.must(false)
2011-04-14 01:20:44 +04:00
}
2011-01-10 21:03:42 +03:00
}
2013-04-28 08:28:40 +04:00
func (e *encoder) must(ok bool) {
if !ok {
msg := e.emitter.problem
if msg == "" {
2011-04-14 01:20:44 +04:00
msg = "Unknown problem generating YAML content"
}
2013-04-28 08:28:40 +04:00
panic(msg)
2011-04-14 01:20:44 +04:00
}
2011-01-10 21:03:42 +03:00
}
func (e *encoder) marshal(tag string, in reflect.Value) {
2011-04-14 01:20:44 +04:00
var value interface{}
if getter, ok := in.Interface().(Getter); ok {
tag, value = getter.GetYAML()
if value == nil {
e.nilv()
return
}
2011-04-28 16:55:23 +04:00
in = reflect.ValueOf(value)
2011-04-14 01:20:44 +04:00
}
2011-04-15 08:23:17 +04:00
switch in.Kind() {
case reflect.Interface:
2011-04-14 01:20:44 +04:00
if in.IsNil() {
e.nilv()
} else {
e.marshal(tag, in.Elem())
}
2011-04-15 08:23:17 +04:00
case reflect.Map:
2011-04-14 01:20:44 +04:00
e.mapv(tag, in)
2011-04-15 08:23:17 +04:00
case reflect.Ptr:
2011-04-14 01:20:44 +04:00
if in.IsNil() {
e.nilv()
} else {
e.marshal(tag, in.Elem())
}
2011-04-15 08:23:17 +04:00
case reflect.Struct:
2011-04-14 01:20:44 +04:00
e.structv(tag, in)
2011-04-15 08:23:17 +04:00
case reflect.Slice:
2011-04-14 01:20:44 +04:00
e.slicev(tag, in)
2011-04-15 08:23:17 +04:00
case reflect.String:
2011-04-14 01:20:44 +04:00
e.stringv(tag, in)
2011-04-15 08:23:17 +04:00
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
2014-04-06 17:56:05 +04:00
if in.Type() == durationType {
e.stringv(tag, reflect.ValueOf(in.Interface().(time.Duration).String()))
} else {
e.intv(tag, in)
}
2011-04-15 08:23:17 +04:00
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
2011-04-14 01:20:44 +04:00
e.uintv(tag, in)
2011-04-15 08:23:17 +04:00
case reflect.Float32, reflect.Float64:
2011-04-14 01:20:44 +04:00
e.floatv(tag, in)
2011-04-15 08:23:17 +04:00
case reflect.Bool:
2011-04-14 01:20:44 +04:00
e.boolv(tag, in)
default:
panic("Can't marshal type yet: " + in.Type().String())
}
2011-01-10 21:03:42 +03:00
}
2011-04-15 08:23:17 +04:00
func (e *encoder) mapv(tag string, in reflect.Value) {
2011-04-14 01:20:44 +04:00
e.mappingv(tag, func() {
2012-09-20 13:00:32 +04:00
keys := keyList(in.MapKeys())
sort.Sort(keys)
for _, k := range keys {
2011-04-14 01:20:44 +04:00
e.marshal("", k)
2011-04-15 08:23:17 +04:00
e.marshal("", in.MapIndex(k))
2011-04-14 01:20:44 +04:00
}
})
2011-01-10 21:03:42 +03:00
}
2011-04-15 08:23:17 +04:00
func (e *encoder) structv(tag string, in reflect.Value) {
sinfo, err := getStructInfo(in.Type())
2011-04-14 01:20:44 +04:00
if err != nil {
panic(err)
}
e.mappingv(tag, func() {
for _, info := range sinfo.FieldsList {
var value reflect.Value
if info.Inline == nil {
value = in.Field(info.Num)
} else {
value = in.FieldByIndex(info.Inline)
}
if info.OmitEmpty && isZero(value) {
2011-04-14 01:20:44 +04:00
continue
}
2011-04-28 16:55:23 +04:00
e.marshal("", reflect.ValueOf(info.Key))
2011-04-14 01:20:44 +04:00
e.flow = info.Flow
e.marshal("", value)
}
})
}
func (e *encoder) mappingv(tag string, f func()) {
2013-04-28 08:28:40 +04:00
implicit := tag == ""
style := yaml_BLOCK_MAPPING_STYLE
2011-04-14 01:20:44 +04:00
if e.flow {
e.flow = false
2013-04-28 08:28:40 +04:00
style = yaml_FLOW_MAPPING_STYLE
2011-04-14 01:20:44 +04:00
}
2013-04-28 08:28:40 +04:00
e.must(yaml_mapping_start_event_initialize(&e.event, nil, []byte(tag), implicit, style))
2011-04-14 01:20:44 +04:00
e.emit()
f()
2013-04-28 08:28:40 +04:00
e.must(yaml_mapping_end_event_initialize(&e.event))
2011-04-14 01:20:44 +04:00
e.emit()
2011-01-10 21:03:42 +03:00
}
2011-04-15 08:23:17 +04:00
func (e *encoder) slicev(tag string, in reflect.Value) {
2013-04-28 08:28:40 +04:00
implicit := tag == ""
style := yaml_BLOCK_SEQUENCE_STYLE
2011-04-14 01:20:44 +04:00
if e.flow {
e.flow = false
2013-04-28 08:28:40 +04:00
style = yaml_FLOW_SEQUENCE_STYLE
2011-04-14 01:20:44 +04:00
}
2013-04-28 08:28:40 +04:00
e.must(yaml_sequence_start_event_initialize(&e.event, nil, []byte(tag), implicit, style))
2011-04-14 01:20:44 +04:00
e.emit()
n := in.Len()
for i := 0; i < n; i++ {
2011-04-15 08:23:17 +04:00
e.marshal("", in.Index(i))
2011-04-14 01:20:44 +04:00
}
2013-04-28 08:28:40 +04:00
e.must(yaml_sequence_end_event_initialize(&e.event))
2011-04-14 01:20:44 +04:00
e.emit()
2011-01-10 21:03:42 +03:00
}
2011-04-15 08:23:17 +04:00
func (e *encoder) stringv(tag string, in reflect.Value) {
2013-04-28 08:28:40 +04:00
var style yaml_scalar_style_t
2011-04-15 08:23:17 +04:00
s := in.String()
2011-04-14 01:20:44 +04:00
if rtag, _ := resolve("", s); rtag != "!!str" {
2013-04-28 08:28:40 +04:00
style = yaml_DOUBLE_QUOTED_SCALAR_STYLE
2011-04-14 01:20:44 +04:00
} else {
2013-04-28 08:28:40 +04:00
style = yaml_PLAIN_SCALAR_STYLE
2011-04-14 01:20:44 +04:00
}
e.emitScalar(s, "", tag, style)
2011-01-10 21:03:42 +03:00
}
2011-04-15 08:23:17 +04:00
func (e *encoder) boolv(tag string, in reflect.Value) {
2011-04-14 01:20:44 +04:00
var s string
2011-04-15 08:23:17 +04:00
if in.Bool() {
2011-04-14 01:20:44 +04:00
s = "true"
} else {
s = "false"
}
2013-04-28 08:28:40 +04:00
e.emitScalar(s, "", tag, yaml_PLAIN_SCALAR_STYLE)
2011-01-10 21:03:42 +03:00
}
2011-04-15 08:23:17 +04:00
func (e *encoder) intv(tag string, in reflect.Value) {
2011-12-15 19:22:49 +04:00
s := strconv.FormatInt(in.Int(), 10)
2013-04-28 08:28:40 +04:00
e.emitScalar(s, "", tag, yaml_PLAIN_SCALAR_STYLE)
2011-01-10 21:03:42 +03:00
}
2011-04-15 08:23:17 +04:00
func (e *encoder) uintv(tag string, in reflect.Value) {
2011-12-15 19:22:49 +04:00
s := strconv.FormatUint(in.Uint(), 10)
2013-04-28 08:28:40 +04:00
e.emitScalar(s, "", tag, yaml_PLAIN_SCALAR_STYLE)
2011-01-10 21:03:42 +03:00
}
2011-04-15 08:23:17 +04:00
func (e *encoder) floatv(tag string, in reflect.Value) {
2011-04-14 01:20:44 +04:00
// FIXME: Handle 64 bits here.
2011-12-15 19:22:49 +04:00
s := strconv.FormatFloat(float64(in.Float()), 'g', -1, 32)
2011-04-14 01:20:44 +04:00
switch s {
case "+Inf":
s = ".inf"
case "-Inf":
s = "-.inf"
case "NaN":
s = ".nan"
}
2013-04-28 08:28:40 +04:00
e.emitScalar(s, "", tag, yaml_PLAIN_SCALAR_STYLE)
2011-01-10 21:03:42 +03:00
}
func (e *encoder) nilv() {
2013-04-28 08:28:40 +04:00
e.emitScalar("null", "", "", yaml_PLAIN_SCALAR_STYLE)
2011-01-10 21:03:42 +03:00
}
2013-04-28 08:28:40 +04:00
func (e *encoder) emitScalar(value, anchor, tag string, style yaml_scalar_style_t) {
implicit := tag == ""
if !implicit {
style = yaml_PLAIN_SCALAR_STYLE
2011-04-14 01:20:44 +04:00
}
2013-04-28 08:28:40 +04:00
e.must(yaml_scalar_event_initialize(&e.event, []byte(anchor), []byte(tag), []byte(value), implicit, implicit, style))
2011-04-14 01:20:44 +04:00
e.emit()
2011-01-10 21:03:42 +03:00
}