forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/gopkg.in/yaml.v3/encode.go

1057 lines
15 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Copyright (c) 2011-2019 Canonical Ltd
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Licensed under the Apache License, Version 2.0 (the "License");
2024-02-18 10:42:21 +00:00
// you may not use this file except in compliance with the License.
2024-02-18 10:42:21 +00:00
// You may obtain a copy of the License at
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// http://www.apache.org/licenses/LICENSE-2.0
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Unless required by applicable law or agreed to in writing, software
2024-02-18 10:42:21 +00:00
// distributed under the License is distributed on an "AS IS" BASIS,
2024-02-18 10:42:21 +00:00
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2024-02-18 10:42:21 +00:00
// See the License for the specific language governing permissions and
2024-02-18 10:42:21 +00:00
// limitations under the License.
package yaml
import (
"encoding"
"fmt"
"io"
"reflect"
"regexp"
"sort"
"strconv"
"strings"
"time"
"unicode/utf8"
)
type encoder struct {
emitter yaml_emitter_t
event yaml_event_t
out []byte
flow bool
indent int
2024-02-18 10:42:21 +00:00
doneInit bool
}
func newEncoder() *encoder {
2024-02-18 10:42:21 +00:00
e := &encoder{}
2024-02-18 10:42:21 +00:00
yaml_emitter_initialize(&e.emitter)
2024-02-18 10:42:21 +00:00
yaml_emitter_set_output_string(&e.emitter, &e.out)
2024-02-18 10:42:21 +00:00
yaml_emitter_set_unicode(&e.emitter, true)
2024-02-18 10:42:21 +00:00
return e
2024-02-18 10:42:21 +00:00
}
func newEncoderWithWriter(w io.Writer) *encoder {
2024-02-18 10:42:21 +00:00
e := &encoder{}
2024-02-18 10:42:21 +00:00
yaml_emitter_initialize(&e.emitter)
2024-02-18 10:42:21 +00:00
yaml_emitter_set_output_writer(&e.emitter, w)
2024-02-18 10:42:21 +00:00
yaml_emitter_set_unicode(&e.emitter, true)
2024-02-18 10:42:21 +00:00
return e
2024-02-18 10:42:21 +00:00
}
func (e *encoder) init() {
2024-02-18 10:42:21 +00:00
if e.doneInit {
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if e.indent == 0 {
2024-02-18 10:42:21 +00:00
e.indent = 4
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
e.emitter.best_indent = e.indent
2024-02-18 10:42:21 +00:00
yaml_stream_start_event_initialize(&e.event, yaml_UTF8_ENCODING)
2024-02-18 10:42:21 +00:00
e.emit()
2024-02-18 10:42:21 +00:00
e.doneInit = true
2024-02-18 10:42:21 +00:00
}
func (e *encoder) finish() {
2024-02-18 10:42:21 +00:00
e.emitter.open_ended = false
2024-02-18 10:42:21 +00:00
yaml_stream_end_event_initialize(&e.event)
2024-02-18 10:42:21 +00:00
e.emit()
2024-02-18 10:42:21 +00:00
}
func (e *encoder) destroy() {
2024-02-18 10:42:21 +00:00
yaml_emitter_delete(&e.emitter)
2024-02-18 10:42:21 +00:00
}
func (e *encoder) emit() {
2024-02-18 10:42:21 +00:00
// This will internally delete the e.event value.
2024-02-18 10:42:21 +00:00
e.must(yaml_emitter_emit(&e.emitter, &e.event))
2024-02-18 10:42:21 +00:00
}
func (e *encoder) must(ok bool) {
2024-02-18 10:42:21 +00:00
if !ok {
2024-02-18 10:42:21 +00:00
msg := e.emitter.problem
2024-02-18 10:42:21 +00:00
if msg == "" {
2024-02-18 10:42:21 +00:00
msg = "unknown problem generating YAML content"
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
failf("%s", msg)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
func (e *encoder) marshalDoc(tag string, in reflect.Value) {
2024-02-18 10:42:21 +00:00
e.init()
2024-02-18 10:42:21 +00:00
var node *Node
2024-02-18 10:42:21 +00:00
if in.IsValid() {
2024-02-18 10:42:21 +00:00
node, _ = in.Interface().(*Node)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if node != nil && node.Kind == DocumentNode {
2024-02-18 10:42:21 +00:00
e.nodev(in)
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
yaml_document_start_event_initialize(&e.event, nil, nil, true)
2024-02-18 10:42:21 +00:00
e.emit()
2024-02-18 10:42:21 +00:00
e.marshal(tag, in)
2024-02-18 10:42:21 +00:00
yaml_document_end_event_initialize(&e.event, true)
2024-02-18 10:42:21 +00:00
e.emit()
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
func (e *encoder) marshal(tag string, in reflect.Value) {
2024-02-18 10:42:21 +00:00
tag = shortTag(tag)
2024-02-18 10:42:21 +00:00
if !in.IsValid() || in.Kind() == reflect.Ptr && in.IsNil() {
2024-02-18 10:42:21 +00:00
e.nilv()
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
iface := in.Interface()
2024-02-18 10:42:21 +00:00
switch value := iface.(type) {
2024-02-18 10:42:21 +00:00
case *Node:
2024-02-18 10:42:21 +00:00
e.nodev(in)
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
case Node:
2024-02-18 10:42:21 +00:00
if !in.CanAddr() {
2024-02-18 10:42:21 +00:00
var n = reflect.New(in.Type()).Elem()
2024-02-18 10:42:21 +00:00
n.Set(in)
2024-02-18 10:42:21 +00:00
in = n
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
e.nodev(in.Addr())
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
case time.Time:
2024-02-18 10:42:21 +00:00
e.timev(tag, in)
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
case *time.Time:
2024-02-18 10:42:21 +00:00
e.timev(tag, in.Elem())
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
case time.Duration:
2024-02-18 10:42:21 +00:00
e.stringv(tag, reflect.ValueOf(value.String()))
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
case Marshaler:
2024-02-18 10:42:21 +00:00
v, err := value.MarshalYAML()
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
fail(err)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if v == nil {
2024-02-18 10:42:21 +00:00
e.nilv()
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
e.marshal(tag, reflect.ValueOf(v))
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
case encoding.TextMarshaler:
2024-02-18 10:42:21 +00:00
text, err := value.MarshalText()
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
fail(err)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
in = reflect.ValueOf(string(text))
2024-02-18 10:42:21 +00:00
case nil:
2024-02-18 10:42:21 +00:00
e.nilv()
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
switch in.Kind() {
2024-02-18 10:42:21 +00:00
case reflect.Interface:
2024-02-18 10:42:21 +00:00
e.marshal(tag, in.Elem())
2024-02-18 10:42:21 +00:00
case reflect.Map:
2024-02-18 10:42:21 +00:00
e.mapv(tag, in)
2024-02-18 10:42:21 +00:00
case reflect.Ptr:
2024-02-18 10:42:21 +00:00
e.marshal(tag, in.Elem())
2024-02-18 10:42:21 +00:00
case reflect.Struct:
2024-02-18 10:42:21 +00:00
e.structv(tag, in)
2024-02-18 10:42:21 +00:00
case reflect.Slice, reflect.Array:
2024-02-18 10:42:21 +00:00
e.slicev(tag, in)
2024-02-18 10:42:21 +00:00
case reflect.String:
2024-02-18 10:42:21 +00:00
e.stringv(tag, in)
2024-02-18 10:42:21 +00:00
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
2024-02-18 10:42:21 +00:00
e.intv(tag, in)
2024-02-18 10:42:21 +00:00
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
2024-02-18 10:42:21 +00:00
e.uintv(tag, in)
2024-02-18 10:42:21 +00:00
case reflect.Float32, reflect.Float64:
2024-02-18 10:42:21 +00:00
e.floatv(tag, in)
2024-02-18 10:42:21 +00:00
case reflect.Bool:
2024-02-18 10:42:21 +00:00
e.boolv(tag, in)
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
panic("cannot marshal type: " + in.Type().String())
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
func (e *encoder) mapv(tag string, in reflect.Value) {
2024-02-18 10:42:21 +00:00
e.mappingv(tag, func() {
2024-02-18 10:42:21 +00:00
keys := keyList(in.MapKeys())
2024-02-18 10:42:21 +00:00
sort.Sort(keys)
2024-02-18 10:42:21 +00:00
for _, k := range keys {
2024-02-18 10:42:21 +00:00
e.marshal("", k)
2024-02-18 10:42:21 +00:00
e.marshal("", in.MapIndex(k))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
})
2024-02-18 10:42:21 +00:00
}
func (e *encoder) fieldByIndex(v reflect.Value, index []int) (field reflect.Value) {
2024-02-18 10:42:21 +00:00
for _, num := range index {
2024-02-18 10:42:21 +00:00
for {
2024-02-18 10:42:21 +00:00
if v.Kind() == reflect.Ptr {
2024-02-18 10:42:21 +00:00
if v.IsNil() {
2024-02-18 10:42:21 +00:00
return reflect.Value{}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
v = v.Elem()
2024-02-18 10:42:21 +00:00
continue
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
break
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
v = v.Field(num)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return v
2024-02-18 10:42:21 +00:00
}
func (e *encoder) structv(tag string, in reflect.Value) {
2024-02-18 10:42:21 +00:00
sinfo, err := getStructInfo(in.Type())
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
panic(err)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
e.mappingv(tag, func() {
2024-02-18 10:42:21 +00:00
for _, info := range sinfo.FieldsList {
2024-02-18 10:42:21 +00:00
var value reflect.Value
2024-02-18 10:42:21 +00:00
if info.Inline == nil {
2024-02-18 10:42:21 +00:00
value = in.Field(info.Num)
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
value = e.fieldByIndex(in, info.Inline)
2024-02-18 10:42:21 +00:00
if !value.IsValid() {
2024-02-18 10:42:21 +00:00
continue
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if info.OmitEmpty && isZero(value) {
2024-02-18 10:42:21 +00:00
continue
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
e.marshal("", reflect.ValueOf(info.Key))
2024-02-18 10:42:21 +00:00
e.flow = info.Flow
2024-02-18 10:42:21 +00:00
e.marshal("", value)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if sinfo.InlineMap >= 0 {
2024-02-18 10:42:21 +00:00
m := in.Field(sinfo.InlineMap)
2024-02-18 10:42:21 +00:00
if m.Len() > 0 {
2024-02-18 10:42:21 +00:00
e.flow = false
2024-02-18 10:42:21 +00:00
keys := keyList(m.MapKeys())
2024-02-18 10:42:21 +00:00
sort.Sort(keys)
2024-02-18 10:42:21 +00:00
for _, k := range keys {
2024-02-18 10:42:21 +00:00
if _, found := sinfo.FieldsMap[k.String()]; found {
2024-02-18 10:42:21 +00:00
panic(fmt.Sprintf("cannot have key %q in inlined map: conflicts with struct field", k.String()))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
e.marshal("", k)
2024-02-18 10:42:21 +00:00
e.flow = false
2024-02-18 10:42:21 +00:00
e.marshal("", m.MapIndex(k))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
})
2024-02-18 10:42:21 +00:00
}
func (e *encoder) mappingv(tag string, f func()) {
2024-02-18 10:42:21 +00:00
implicit := tag == ""
2024-02-18 10:42:21 +00:00
style := yaml_BLOCK_MAPPING_STYLE
2024-02-18 10:42:21 +00:00
if e.flow {
2024-02-18 10:42:21 +00:00
e.flow = false
2024-02-18 10:42:21 +00:00
style = yaml_FLOW_MAPPING_STYLE
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
yaml_mapping_start_event_initialize(&e.event, nil, []byte(tag), implicit, style)
2024-02-18 10:42:21 +00:00
e.emit()
2024-02-18 10:42:21 +00:00
f()
2024-02-18 10:42:21 +00:00
yaml_mapping_end_event_initialize(&e.event)
2024-02-18 10:42:21 +00:00
e.emit()
2024-02-18 10:42:21 +00:00
}
func (e *encoder) slicev(tag string, in reflect.Value) {
2024-02-18 10:42:21 +00:00
implicit := tag == ""
2024-02-18 10:42:21 +00:00
style := yaml_BLOCK_SEQUENCE_STYLE
2024-02-18 10:42:21 +00:00
if e.flow {
2024-02-18 10:42:21 +00:00
e.flow = false
2024-02-18 10:42:21 +00:00
style = yaml_FLOW_SEQUENCE_STYLE
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
e.must(yaml_sequence_start_event_initialize(&e.event, nil, []byte(tag), implicit, style))
2024-02-18 10:42:21 +00:00
e.emit()
2024-02-18 10:42:21 +00:00
n := in.Len()
2024-02-18 10:42:21 +00:00
for i := 0; i < n; i++ {
2024-02-18 10:42:21 +00:00
e.marshal("", in.Index(i))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
e.must(yaml_sequence_end_event_initialize(&e.event))
2024-02-18 10:42:21 +00:00
e.emit()
2024-02-18 10:42:21 +00:00
}
// isBase60 returns whether s is in base 60 notation as defined in YAML 1.1.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// The base 60 float notation in YAML 1.1 is a terrible idea and is unsupported
2024-02-18 10:42:21 +00:00
// in YAML 1.2 and by this package, but these should be marshalled quoted for
2024-02-18 10:42:21 +00:00
// the time being for compatibility with other parsers.
2024-02-18 10:42:21 +00:00
func isBase60Float(s string) (result bool) {
2024-02-18 10:42:21 +00:00
// Fast path.
2024-02-18 10:42:21 +00:00
if s == "" {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
c := s[0]
2024-02-18 10:42:21 +00:00
if !(c == '+' || c == '-' || c >= '0' && c <= '9') || strings.IndexByte(s, ':') < 0 {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
// Do the full match.
2024-02-18 10:42:21 +00:00
return base60float.MatchString(s)
2024-02-18 10:42:21 +00:00
}
// From http://yaml.org/type/float.html, except the regular expression there
2024-02-18 10:42:21 +00:00
// is bogus. In practice parsers do not enforce the "\.[0-9_]*" suffix.
2024-02-18 10:42:21 +00:00
var base60float = regexp.MustCompile(`^[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+(?:\.[0-9_]*)?$`)
// isOldBool returns whether s is bool notation as defined in YAML 1.1.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// We continue to force strings that YAML 1.1 would interpret as booleans to be
2024-02-18 10:42:21 +00:00
// rendered as quotes strings so that the marshalled output valid for YAML 1.1
2024-02-18 10:42:21 +00:00
// parsing.
2024-02-18 10:42:21 +00:00
func isOldBool(s string) (result bool) {
2024-02-18 10:42:21 +00:00
switch s {
2024-02-18 10:42:21 +00:00
case "y", "Y", "yes", "Yes", "YES", "on", "On", "ON",
2024-02-18 10:42:21 +00:00
"n", "N", "no", "No", "NO", "off", "Off", "OFF":
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
func (e *encoder) stringv(tag string, in reflect.Value) {
2024-02-18 10:42:21 +00:00
var style yaml_scalar_style_t
2024-02-18 10:42:21 +00:00
s := in.String()
2024-02-18 10:42:21 +00:00
canUsePlain := true
2024-02-18 10:42:21 +00:00
switch {
2024-02-18 10:42:21 +00:00
case !utf8.ValidString(s):
2024-02-18 10:42:21 +00:00
if tag == binaryTag {
2024-02-18 10:42:21 +00:00
failf("explicitly tagged !!binary data must be base64-encoded")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if tag != "" {
2024-02-18 10:42:21 +00:00
failf("cannot marshal invalid UTF-8 data as %s", shortTag(tag))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
// It can't be encoded directly as YAML so use a binary tag
2024-02-18 10:42:21 +00:00
// and encode it as base64.
2024-02-18 10:42:21 +00:00
tag = binaryTag
2024-02-18 10:42:21 +00:00
s = encodeBase64(s)
2024-02-18 10:42:21 +00:00
case tag == "":
2024-02-18 10:42:21 +00:00
// Check to see if it would resolve to a specific
2024-02-18 10:42:21 +00:00
// tag when encoded unquoted. If it doesn't,
2024-02-18 10:42:21 +00:00
// there's no need to quote it.
2024-02-18 10:42:21 +00:00
rtag, _ := resolve("", s)
2024-02-18 10:42:21 +00:00
canUsePlain = rtag == strTag && !(isBase60Float(s) || isOldBool(s))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
// Note: it's possible for user code to emit invalid YAML
2024-02-18 10:42:21 +00:00
// if they explicitly specify a tag and a string containing
2024-02-18 10:42:21 +00:00
// text that's incompatible with that tag.
2024-02-18 10:42:21 +00:00
switch {
2024-02-18 10:42:21 +00:00
case strings.Contains(s, "\n"):
2024-02-18 10:42:21 +00:00
if e.flow {
2024-02-18 10:42:21 +00:00
style = yaml_DOUBLE_QUOTED_SCALAR_STYLE
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
style = yaml_LITERAL_SCALAR_STYLE
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
case canUsePlain:
2024-02-18 10:42:21 +00:00
style = yaml_PLAIN_SCALAR_STYLE
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
style = yaml_DOUBLE_QUOTED_SCALAR_STYLE
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
e.emitScalar(s, "", tag, style, nil, nil, nil, nil)
2024-02-18 10:42:21 +00:00
}
func (e *encoder) boolv(tag string, in reflect.Value) {
2024-02-18 10:42:21 +00:00
var s string
2024-02-18 10:42:21 +00:00
if in.Bool() {
2024-02-18 10:42:21 +00:00
s = "true"
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
s = "false"
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
e.emitScalar(s, "", tag, yaml_PLAIN_SCALAR_STYLE, nil, nil, nil, nil)
2024-02-18 10:42:21 +00:00
}
func (e *encoder) intv(tag string, in reflect.Value) {
2024-02-18 10:42:21 +00:00
s := strconv.FormatInt(in.Int(), 10)
2024-02-18 10:42:21 +00:00
e.emitScalar(s, "", tag, yaml_PLAIN_SCALAR_STYLE, nil, nil, nil, nil)
2024-02-18 10:42:21 +00:00
}
func (e *encoder) uintv(tag string, in reflect.Value) {
2024-02-18 10:42:21 +00:00
s := strconv.FormatUint(in.Uint(), 10)
2024-02-18 10:42:21 +00:00
e.emitScalar(s, "", tag, yaml_PLAIN_SCALAR_STYLE, nil, nil, nil, nil)
2024-02-18 10:42:21 +00:00
}
func (e *encoder) timev(tag string, in reflect.Value) {
2024-02-18 10:42:21 +00:00
t := in.Interface().(time.Time)
2024-02-18 10:42:21 +00:00
s := t.Format(time.RFC3339Nano)
2024-02-18 10:42:21 +00:00
e.emitScalar(s, "", tag, yaml_PLAIN_SCALAR_STYLE, nil, nil, nil, nil)
2024-02-18 10:42:21 +00:00
}
func (e *encoder) floatv(tag string, in reflect.Value) {
2024-02-18 10:42:21 +00:00
// Issue #352: When formatting, use the precision of the underlying value
2024-02-18 10:42:21 +00:00
precision := 64
2024-02-18 10:42:21 +00:00
if in.Kind() == reflect.Float32 {
2024-02-18 10:42:21 +00:00
precision = 32
2024-02-18 10:42:21 +00:00
}
s := strconv.FormatFloat(in.Float(), 'g', -1, precision)
2024-02-18 10:42:21 +00:00
switch s {
2024-02-18 10:42:21 +00:00
case "+Inf":
2024-02-18 10:42:21 +00:00
s = ".inf"
2024-02-18 10:42:21 +00:00
case "-Inf":
2024-02-18 10:42:21 +00:00
s = "-.inf"
2024-02-18 10:42:21 +00:00
case "NaN":
2024-02-18 10:42:21 +00:00
s = ".nan"
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
e.emitScalar(s, "", tag, yaml_PLAIN_SCALAR_STYLE, nil, nil, nil, nil)
2024-02-18 10:42:21 +00:00
}
func (e *encoder) nilv() {
2024-02-18 10:42:21 +00:00
e.emitScalar("null", "", "", yaml_PLAIN_SCALAR_STYLE, nil, nil, nil, nil)
2024-02-18 10:42:21 +00:00
}
func (e *encoder) emitScalar(value, anchor, tag string, style yaml_scalar_style_t, head, line, foot, tail []byte) {
2024-02-18 10:42:21 +00:00
// TODO Kill this function. Replace all initialize calls by their underlining Go literals.
2024-02-18 10:42:21 +00:00
implicit := tag == ""
2024-02-18 10:42:21 +00:00
if !implicit {
2024-02-18 10:42:21 +00:00
tag = longTag(tag)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
e.must(yaml_scalar_event_initialize(&e.event, []byte(anchor), []byte(tag), []byte(value), implicit, implicit, style))
2024-02-18 10:42:21 +00:00
e.event.head_comment = head
2024-02-18 10:42:21 +00:00
e.event.line_comment = line
2024-02-18 10:42:21 +00:00
e.event.foot_comment = foot
2024-02-18 10:42:21 +00:00
e.event.tail_comment = tail
2024-02-18 10:42:21 +00:00
e.emit()
2024-02-18 10:42:21 +00:00
}
func (e *encoder) nodev(in reflect.Value) {
2024-02-18 10:42:21 +00:00
e.node(in.Interface().(*Node), "")
2024-02-18 10:42:21 +00:00
}
func (e *encoder) node(node *Node, tail string) {
2024-02-18 10:42:21 +00:00
// Zero nodes behave as nil.
2024-02-18 10:42:21 +00:00
if node.Kind == 0 && node.IsZero() {
2024-02-18 10:42:21 +00:00
e.nilv()
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
// If the tag was not explicitly requested, and dropping it won't change the
2024-02-18 10:42:21 +00:00
// implicit tag of the value, don't include it in the presentation.
2024-02-18 10:42:21 +00:00
var tag = node.Tag
2024-02-18 10:42:21 +00:00
var stag = shortTag(tag)
2024-02-18 10:42:21 +00:00
var forceQuoting bool
2024-02-18 10:42:21 +00:00
if tag != "" && node.Style&TaggedStyle == 0 {
2024-02-18 10:42:21 +00:00
if node.Kind == ScalarNode {
2024-02-18 10:42:21 +00:00
if stag == strTag && node.Style&(SingleQuotedStyle|DoubleQuotedStyle|LiteralStyle|FoldedStyle) != 0 {
2024-02-18 10:42:21 +00:00
tag = ""
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
rtag, _ := resolve("", node.Value)
2024-02-18 10:42:21 +00:00
if rtag == stag {
2024-02-18 10:42:21 +00:00
tag = ""
2024-02-18 10:42:21 +00:00
} else if stag == strTag {
2024-02-18 10:42:21 +00:00
tag = ""
2024-02-18 10:42:21 +00:00
forceQuoting = true
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
var rtag string
2024-02-18 10:42:21 +00:00
switch node.Kind {
2024-02-18 10:42:21 +00:00
case MappingNode:
2024-02-18 10:42:21 +00:00
rtag = mapTag
2024-02-18 10:42:21 +00:00
case SequenceNode:
2024-02-18 10:42:21 +00:00
rtag = seqTag
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if rtag == stag {
2024-02-18 10:42:21 +00:00
tag = ""
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
switch node.Kind {
2024-02-18 10:42:21 +00:00
case DocumentNode:
2024-02-18 10:42:21 +00:00
yaml_document_start_event_initialize(&e.event, nil, nil, true)
2024-02-18 10:42:21 +00:00
e.event.head_comment = []byte(node.HeadComment)
2024-02-18 10:42:21 +00:00
e.emit()
2024-02-18 10:42:21 +00:00
for _, node := range node.Content {
2024-02-18 10:42:21 +00:00
e.node(node, "")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
yaml_document_end_event_initialize(&e.event, true)
2024-02-18 10:42:21 +00:00
e.event.foot_comment = []byte(node.FootComment)
2024-02-18 10:42:21 +00:00
e.emit()
case SequenceNode:
2024-02-18 10:42:21 +00:00
style := yaml_BLOCK_SEQUENCE_STYLE
2024-02-18 10:42:21 +00:00
if node.Style&FlowStyle != 0 {
2024-02-18 10:42:21 +00:00
style = yaml_FLOW_SEQUENCE_STYLE
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
e.must(yaml_sequence_start_event_initialize(&e.event, []byte(node.Anchor), []byte(longTag(tag)), tag == "", style))
2024-02-18 10:42:21 +00:00
e.event.head_comment = []byte(node.HeadComment)
2024-02-18 10:42:21 +00:00
e.emit()
2024-02-18 10:42:21 +00:00
for _, node := range node.Content {
2024-02-18 10:42:21 +00:00
e.node(node, "")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
e.must(yaml_sequence_end_event_initialize(&e.event))
2024-02-18 10:42:21 +00:00
e.event.line_comment = []byte(node.LineComment)
2024-02-18 10:42:21 +00:00
e.event.foot_comment = []byte(node.FootComment)
2024-02-18 10:42:21 +00:00
e.emit()
case MappingNode:
2024-02-18 10:42:21 +00:00
style := yaml_BLOCK_MAPPING_STYLE
2024-02-18 10:42:21 +00:00
if node.Style&FlowStyle != 0 {
2024-02-18 10:42:21 +00:00
style = yaml_FLOW_MAPPING_STYLE
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
yaml_mapping_start_event_initialize(&e.event, []byte(node.Anchor), []byte(longTag(tag)), tag == "", style)
2024-02-18 10:42:21 +00:00
e.event.tail_comment = []byte(tail)
2024-02-18 10:42:21 +00:00
e.event.head_comment = []byte(node.HeadComment)
2024-02-18 10:42:21 +00:00
e.emit()
// The tail logic below moves the foot comment of prior keys to the following key,
2024-02-18 10:42:21 +00:00
// since the value for each key may be a nested structure and the foot needs to be
2024-02-18 10:42:21 +00:00
// processed only the entirety of the value is streamed. The last tail is processed
2024-02-18 10:42:21 +00:00
// with the mapping end event.
2024-02-18 10:42:21 +00:00
var tail string
2024-02-18 10:42:21 +00:00
for i := 0; i+1 < len(node.Content); i += 2 {
2024-02-18 10:42:21 +00:00
k := node.Content[i]
2024-02-18 10:42:21 +00:00
foot := k.FootComment
2024-02-18 10:42:21 +00:00
if foot != "" {
2024-02-18 10:42:21 +00:00
kopy := *k
2024-02-18 10:42:21 +00:00
kopy.FootComment = ""
2024-02-18 10:42:21 +00:00
k = &kopy
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
e.node(k, tail)
2024-02-18 10:42:21 +00:00
tail = foot
v := node.Content[i+1]
2024-02-18 10:42:21 +00:00
e.node(v, "")
2024-02-18 10:42:21 +00:00
}
yaml_mapping_end_event_initialize(&e.event)
2024-02-18 10:42:21 +00:00
e.event.tail_comment = []byte(tail)
2024-02-18 10:42:21 +00:00
e.event.line_comment = []byte(node.LineComment)
2024-02-18 10:42:21 +00:00
e.event.foot_comment = []byte(node.FootComment)
2024-02-18 10:42:21 +00:00
e.emit()
case AliasNode:
2024-02-18 10:42:21 +00:00
yaml_alias_event_initialize(&e.event, []byte(node.Value))
2024-02-18 10:42:21 +00:00
e.event.head_comment = []byte(node.HeadComment)
2024-02-18 10:42:21 +00:00
e.event.line_comment = []byte(node.LineComment)
2024-02-18 10:42:21 +00:00
e.event.foot_comment = []byte(node.FootComment)
2024-02-18 10:42:21 +00:00
e.emit()
case ScalarNode:
2024-02-18 10:42:21 +00:00
value := node.Value
2024-02-18 10:42:21 +00:00
if !utf8.ValidString(value) {
2024-02-18 10:42:21 +00:00
if stag == binaryTag {
2024-02-18 10:42:21 +00:00
failf("explicitly tagged !!binary data must be base64-encoded")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if stag != "" {
2024-02-18 10:42:21 +00:00
failf("cannot marshal invalid UTF-8 data as %s", stag)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
// It can't be encoded directly as YAML so use a binary tag
2024-02-18 10:42:21 +00:00
// and encode it as base64.
2024-02-18 10:42:21 +00:00
tag = binaryTag
2024-02-18 10:42:21 +00:00
value = encodeBase64(value)
2024-02-18 10:42:21 +00:00
}
style := yaml_PLAIN_SCALAR_STYLE
2024-02-18 10:42:21 +00:00
switch {
2024-02-18 10:42:21 +00:00
case node.Style&DoubleQuotedStyle != 0:
2024-02-18 10:42:21 +00:00
style = yaml_DOUBLE_QUOTED_SCALAR_STYLE
2024-02-18 10:42:21 +00:00
case node.Style&SingleQuotedStyle != 0:
2024-02-18 10:42:21 +00:00
style = yaml_SINGLE_QUOTED_SCALAR_STYLE
2024-02-18 10:42:21 +00:00
case node.Style&LiteralStyle != 0:
2024-02-18 10:42:21 +00:00
style = yaml_LITERAL_SCALAR_STYLE
2024-02-18 10:42:21 +00:00
case node.Style&FoldedStyle != 0:
2024-02-18 10:42:21 +00:00
style = yaml_FOLDED_SCALAR_STYLE
2024-02-18 10:42:21 +00:00
case strings.Contains(value, "\n"):
2024-02-18 10:42:21 +00:00
style = yaml_LITERAL_SCALAR_STYLE
2024-02-18 10:42:21 +00:00
case forceQuoting:
2024-02-18 10:42:21 +00:00
style = yaml_DOUBLE_QUOTED_SCALAR_STYLE
2024-02-18 10:42:21 +00:00
}
e.emitScalar(value, node.Anchor, tag, style, []byte(node.HeadComment), []byte(node.LineComment), []byte(node.FootComment), []byte(tail))
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
failf("cannot encode node with unknown kind %d", node.Kind)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}