Source File
encode.go
Belonging Package
gopkg.in/yaml.v2
package yaml
import (
)
type jsonNumber interface {
Float64() (float64, error)
Int64() (int64, error)
String() string
}
type encoder struct {
emitter yaml_emitter_t
event yaml_event_t
out []byte
doneInit bool
}
func () *encoder {
:= &encoder{}
yaml_emitter_initialize(&.emitter)
yaml_emitter_set_output_string(&.emitter, &.out)
yaml_emitter_set_unicode(&.emitter, true)
return
}
func ( io.Writer) *encoder {
:= &encoder{}
yaml_emitter_initialize(&.emitter)
yaml_emitter_set_output_writer(&.emitter, )
yaml_emitter_set_unicode(&.emitter, true)
return
}
func ( *encoder) () {
if .doneInit {
return
}
yaml_stream_start_event_initialize(&.event, yaml_UTF8_ENCODING)
.emit()
.doneInit = true
}
func ( *encoder) () {
.emitter.open_ended = false
yaml_stream_end_event_initialize(&.event)
.emit()
}
func ( *encoder) () {
yaml_emitter_delete(&.emitter)
}
.must(yaml_emitter_emit(&.emitter, &.event))
}
func ( *encoder) ( bool) {
if ! {
:= .emitter.problem
if == "" {
= "unknown problem generating YAML content"
}
failf("%s", )
}
}
func ( *encoder) ( string, reflect.Value) {
.init()
yaml_document_start_event_initialize(&.event, nil, nil, true)
.emit()
.marshal(, )
yaml_document_end_event_initialize(&.event, true)
.emit()
}
func ( *encoder) ( string, reflect.Value) {
if !.IsValid() || .Kind() == reflect.Ptr && .IsNil() {
.nilv()
return
}
:= .Interface()
switch m := .(type) {
case jsonNumber:
, := .Int64()
case Marshaler:
, := .MarshalYAML()
if != nil {
fail()
}
if == nil {
.nilv()
return
}
= reflect.ValueOf()
case encoding.TextMarshaler:
, := .MarshalText()
if != nil {
fail()
}
= reflect.ValueOf(string())
case nil:
.nilv()
return
}
switch .Kind() {
case reflect.Interface:
.(, .Elem())
case reflect.Map:
.mapv(, )
case reflect.Ptr:
if .Type() == ptrTimeType {
.timev(, .Elem())
} else {
.(, .Elem())
}
case reflect.Struct:
if .Type() == timeType {
.timev(, )
} else {
.structv(, )
}
case reflect.Slice, reflect.Array:
if .Type().Elem() == mapItemType {
.itemsv(, )
} else {
.slicev(, )
}
case reflect.String:
.stringv(, )
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if .Type() == durationType {
.stringv(, reflect.ValueOf(.(time.Duration).String()))
} else {
.intv(, )
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
.uintv(, )
case reflect.Float32, reflect.Float64:
.floatv(, )
case reflect.Bool:
.boolv(, )
default:
panic("cannot marshal type: " + .Type().String())
}
}
func ( *encoder) ( string, reflect.Value) {
.mappingv(, func() {
:= keyList(.MapKeys())
sort.Sort()
for , := range {
.marshal("", )
.marshal("", .MapIndex())
}
})
}
func ( *encoder) ( string, reflect.Value) {
.mappingv(, func() {
:= .Convert(reflect.TypeOf([]MapItem{})).Interface().([]MapItem)
for , := range {
.marshal("", reflect.ValueOf(.Key))
.marshal("", reflect.ValueOf(.Value))
}
})
}
func ( *encoder) ( string, reflect.Value) {
, := getStructInfo(.Type())
if != nil {
panic()
}
.mappingv(, func() {
for , := range .FieldsList {
var reflect.Value
if .Inline == nil {
= .Field(.Num)
} else {
= .FieldByIndex(.Inline)
}
if .OmitEmpty && isZero() {
continue
}
.marshal("", reflect.ValueOf(.Key))
.flow = .Flow
.marshal("", )
}
if .InlineMap >= 0 {
:= .Field(.InlineMap)
if .Len() > 0 {
.flow = false
:= keyList(.MapKeys())
sort.Sort()
for , := range {
if , := .FieldsMap[.String()]; {
panic(fmt.Sprintf("Can't have key %q in inlined map; conflicts with struct field", .String()))
}
.marshal("", )
.flow = false
.marshal("", .MapIndex())
}
}
}
})
}
func ( *encoder) ( string, func()) {
:= == ""
:= yaml_BLOCK_MAPPING_STYLE
if .flow {
.flow = false
= yaml_FLOW_MAPPING_STYLE
}
yaml_mapping_start_event_initialize(&.event, nil, []byte(), , )
.emit()
()
yaml_mapping_end_event_initialize(&.event)
.emit()
}
func ( *encoder) ( string, reflect.Value) {
:= == ""
:= yaml_BLOCK_SEQUENCE_STYLE
if .flow {
.flow = false
= yaml_FLOW_SEQUENCE_STYLE
}
.must(yaml_sequence_start_event_initialize(&.event, nil, []byte(), , ))
.emit()
:= .Len()
for := 0; < ; ++ {
.marshal("", .Index())
}
.must(yaml_sequence_end_event_initialize(&.event))
.emit()
}
return base60float.MatchString()
}
var base60float = regexp.MustCompile(`^[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+(?:\.[0-9_]*)?$`)
func ( *encoder) ( string, reflect.Value) {
var yaml_scalar_style_t
:= .String()
:= true
switch {
case !utf8.ValidString():
if == yaml_BINARY_TAG {
failf("explicitly tagged !!binary data must be base64-encoded")
}
if != "" {
failf("cannot marshal invalid UTF-8 data as %s", shortTag())
= yaml_BINARY_TAG
= encodeBase64()
, := resolve("", )
= == yaml_STR_TAG && !isBase60Float()
switch {
case strings.Contains(, "\n"):
= yaml_LITERAL_SCALAR_STYLE
case :
= yaml_PLAIN_SCALAR_STYLE
default:
= yaml_DOUBLE_QUOTED_SCALAR_STYLE
}
.emitScalar(, "", , )
}
func ( *encoder) ( string, reflect.Value) {
var string
if .Bool() {
= "true"
} else {
= "false"
}
.emitScalar(, "", , yaml_PLAIN_SCALAR_STYLE)
}
func ( *encoder) ( string, reflect.Value) {
:= strconv.FormatInt(.Int(), 10)
.emitScalar(, "", , yaml_PLAIN_SCALAR_STYLE)
}
func ( *encoder) ( string, reflect.Value) {
:= strconv.FormatUint(.Uint(), 10)
.emitScalar(, "", , yaml_PLAIN_SCALAR_STYLE)
}
func ( *encoder) ( string, reflect.Value) {
:= .Interface().(time.Time)
:= .Format(time.RFC3339Nano)
.emitScalar(, "", , yaml_PLAIN_SCALAR_STYLE)
}
:= 64
if .Kind() == reflect.Float32 {
= 32
}
:= strconv.FormatFloat(.Float(), 'g', -1, )
switch {
case "+Inf":
= ".inf"
case "-Inf":
= "-.inf"
case "NaN":
= ".nan"
}
.emitScalar(, "", , yaml_PLAIN_SCALAR_STYLE)
}
func ( *encoder) () {
.emitScalar("null", "", "", yaml_PLAIN_SCALAR_STYLE)
}
func ( *encoder) (, , string, yaml_scalar_style_t) {
:= == ""
.must(yaml_scalar_event_initialize(&.event, []byte(), []byte(), []byte(), , , ))
.emit()
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |