yaml/encode.go

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

2011-01-10 21:03:42 +03:00
package goyaml
// #include "helpers.h"
import "C"
import (
2011-04-14 01:20:44 +04:00
"reflect"
"strconv"
2011-11-24 23:47:20 +04:00
"unsafe"
2011-01-10 21:03:42 +03:00
)
type encoder struct {
2011-04-14 01:20:44 +04:00
emitter C.yaml_emitter_t
event C.yaml_event_t
out []byte
tmp []byte
tmph *reflect.SliceHeader
flow bool
2011-01-10 21:03:42 +03:00
}
//export outputHandler
func outputHandler(data unsafe.Pointer, buffer *C.uchar, size C.size_t) C.int {
2011-04-14 01:20:44 +04:00
e := (*encoder)(data)
e.tmph.Data = uintptr(unsafe.Pointer(buffer))
e.tmph.Len = int(size)
e.tmph.Cap = int(size)
e.out = append(e.out, e.tmp...)
return 1
2011-01-10 21:03:42 +03:00
}
func newEncoder() (e *encoder) {
2011-04-14 01:20:44 +04:00
e = &encoder{}
e.tmph = (*reflect.SliceHeader)(unsafe.Pointer(&e.tmp))
if C.yaml_emitter_initialize(&e.emitter) == 0 {
panic("Failed to initialize YAML emitter")
}
C.set_output_handler(&e.emitter)
C.yaml_stream_start_event_initialize(&e.event, C.YAML_UTF8_ENCODING)
e.emit()
C.yaml_document_start_event_initialize(&e.event, nil, nil, nil, 1)
e.emit()
return e
2011-01-10 21:03:42 +03:00
}
func (e *encoder) finish() {
2011-04-14 01:20:44 +04:00
C.yaml_document_end_event_initialize(&e.event, 1)
e.emit()
e.emitter.open_ended = 0
C.yaml_stream_end_event_initialize(&e.event)
e.emit()
2011-01-10 21:03:42 +03:00
}
func (e *encoder) destroy() {
2011-04-14 01:20:44 +04:00
C.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.
if C.yaml_emitter_emit(&e.emitter, &e.event) == 0 &&
e.event._type != C.YAML_DOCUMENT_END_EVENT &&
e.event._type != C.YAML_STREAM_END_EVENT {
if e.emitter.error == C.YAML_EMITTER_ERROR {
// XXX TESTME
panic("YAML emitter error: " + C.GoString(e.emitter.problem))
} else {
// XXX TESTME
panic("Unknown YAML emitter error")
}
}
2011-01-10 21:03:42 +03:00
}
func (e *encoder) fail(msg string) {
2011-04-14 01:20:44 +04:00
if msg == "" {
if e.emitter.problem != nil {
msg = C.GoString(e.emitter.problem)
} else {
msg = "Unknown problem generating YAML content"
}
}
panic(msg)
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:
2011-04-14 01:20:44 +04:00
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() {
2011-04-15 08:23:17 +04:00
for _, k := range in.MapKeys() {
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) {
fields, err := getStructFields(in.Type())
2011-04-14 01:20:44 +04:00
if err != nil {
panic(err)
}
e.mappingv(tag, func() {
for i, info := range fields.List {
value := in.Field(i)
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()) {
2011-04-14 01:20:44 +04:00
var ctag *C.yaml_char_t
var free func()
cimplicit := C.int(1)
if tag != "" {
ctag, free = ystr(tag)
defer free()
cimplicit = 0
}
cstyle := C.yaml_mapping_style_t(C.YAML_BLOCK_MAPPING_STYLE)
if e.flow {
e.flow = false
cstyle = C.YAML_FLOW_MAPPING_STYLE
}
C.yaml_mapping_start_event_initialize(&e.event, nil, ctag, cimplicit,
cstyle)
e.emit()
f()
C.yaml_mapping_end_event_initialize(&e.event)
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) {
2011-04-14 01:20:44 +04:00
var ctag *C.yaml_char_t
var free func()
var cimplicit C.int
if tag != "" {
ctag, free = ystr(tag)
defer free()
cimplicit = 0
} else {
cimplicit = 1
}
2011-04-14 01:20:44 +04:00
cstyle := C.yaml_sequence_style_t(C.YAML_BLOCK_SEQUENCE_STYLE)
if e.flow {
e.flow = false
cstyle = C.YAML_FLOW_SEQUENCE_STYLE
}
C.yaml_sequence_start_event_initialize(&e.event, nil, ctag, cimplicit,
cstyle)
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
}
C.yaml_sequence_end_event_initialize(&e.event)
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) {
2011-04-14 01:20:44 +04:00
var style C.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" {
style = C.YAML_DOUBLE_QUOTED_SCALAR_STYLE
} else {
style = C.YAML_PLAIN_SCALAR_STYLE
}
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"
}
e.emitScalar(s, "", tag, C.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) {
s := strconv.Itoa64(in.Int())
2011-04-14 01:20:44 +04:00
e.emitScalar(s, "", tag, C.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) {
s := strconv.Uitoa64(in.Uint())
2011-04-14 01:20:44 +04:00
e.emitScalar(s, "", tag, C.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-04-15 08:23:17 +04:00
s := strconv.Ftoa32(float32(in.Float()), 'g', -1)
2011-04-14 01:20:44 +04:00
switch s {
case "+Inf":
s = ".inf"
case "-Inf":
s = "-.inf"
case "NaN":
s = ".nan"
}
e.emitScalar(s, "", tag, C.YAML_PLAIN_SCALAR_STYLE)
2011-01-10 21:03:42 +03:00
}
func (e *encoder) nilv() {
2011-04-14 01:20:44 +04:00
e.emitScalar("null", "", "", C.YAML_PLAIN_SCALAR_STYLE)
2011-01-10 21:03:42 +03:00
}
func (e *encoder) emitScalar(value, anchor, tag string,
2011-11-24 23:47:20 +04:00
style C.yaml_scalar_style_t) {
2011-04-14 01:20:44 +04:00
var canchor, ctag, cvalue *C.yaml_char_t
var cimplicit C.int
var free func()
if anchor != "" {
canchor, free = ystr(anchor)
defer free()
}
if tag != "" {
ctag, free = ystr(tag)
defer free()
cimplicit = 0
style = C.YAML_PLAIN_SCALAR_STYLE
} else {
cimplicit = 1
}
cvalue, free = ystr(value)
defer free()
size := C.int(len(value))
if C.yaml_scalar_event_initialize(&e.event, canchor, ctag, cvalue, size,
cimplicit, cimplicit, style) == 0 {
e.fail("")
}
e.emit()
2011-01-10 21:03:42 +03:00
}
func ystr(s string) (ys *C.yaml_char_t, free func()) {
2011-04-14 01:20:44 +04:00
up := unsafe.Pointer(C.CString(s))
ys = (*C.yaml_char_t)(up)
free = func() { C.free(up) }
return ys, free
2011-01-10 21:03:42 +03:00
}