Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aathan combined -- all aathan PRs combined #981

Open
wants to merge 7 commits into
base: v3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,7 @@ func isStringMap(n *Node) bool {
}

func (d *decoder) mappingStruct(n *Node, out reflect.Value) (good bool) {
sinfo, err := getStructInfo(out.Type())
sinfo, err := getStructInfo(out.Type(), false)
if err != nil {
panic(err)
}
Expand Down
108 changes: 70 additions & 38 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,41 @@ import (
"sort"
"strconv"
"strings"
"time"
"unicode/utf8"
)

type encoder struct {
emitter yaml_emitter_t
event yaml_event_t
out []byte
flow bool
indent int
doneInit bool
emitter yaml_emitter_t
event yaml_event_t
out []byte
flow bool
indent int
doneInit bool
originalCase bool
arg any
}

func newEncoder() *encoder {
e := &encoder{}
func newEncoder_(arg any, opts ...MarshalOpt) (e *encoder) {
e = &encoder{}
e.arg = arg
for _, o := range opts {
if o == OriginalCase {
e.originalCase = true
}
}
return
}

func newEncoder(arg any, opts ...MarshalOpt) *encoder {
e := newEncoder_(arg, opts...)
yaml_emitter_initialize(&e.emitter)
yaml_emitter_set_output_string(&e.emitter, &e.out)
yaml_emitter_set_unicode(&e.emitter, true)
return e
}

func newEncoderWithWriter(w io.Writer) *encoder {
e := &encoder{}
func newEncoderWithWriter(w io.Writer, arg any, opts ...MarshalOpt) *encoder {
e := newEncoder_(arg, opts...)
yaml_emitter_initialize(&e.emitter)
yaml_emitter_set_output_writer(&e.emitter, w)
yaml_emitter_set_unicode(&e.emitter, true)
Expand Down Expand Up @@ -110,53 +122,66 @@ func (e *encoder) marshalDoc(tag string, in reflect.Value) {

func (e *encoder) marshal(tag string, in reflect.Value) {
tag = shortTag(tag)
if !in.IsValid() || in.Kind() == reflect.Ptr && in.IsNil() {
if !in.IsValid() { //|| in.Kind() == reflect.Ptr && in.IsNil() {
e.nilv()
return
}

iface := in.Interface()
switch value := iface.(type) {
case *Node:
e.nodev(in)
if iface == nil {
e.nilv()
return
case Node:
if !in.CanAddr() {
var n = reflect.New(in.Type()).Elem()
}

if in.Kind() != reflect.Ptr && in.Kind() != reflect.Interface {
if in.CanAddr() {
iface = in.Addr().Interface()
} else {
// uh, not sure about this but look at this project's original Node: case...
n := reflect.New(in.Type()).Elem()
n.Set(in)
in = n
iface = n.Addr().Interface()
}
e.nodev(in.Addr())
return
case time.Time:
e.timev(tag, in)
return
case *time.Time:
e.timev(tag, in.Elem())
return
case time.Duration:
e.stringv(tag, reflect.ValueOf(value.String()))
}

switch value := iface.(type) {
//case *time.Duration:
// e.stringv(tag, reflect.ValueOf(value.String()))
// return
case *Node:
e.nodev(in)
return
case Marshaler:
v, err := value.MarshalYAML()
v, err := value.MarshalYAML(e.arg)
if err != nil {
fail(err)
}
if v == nil {
e.nilv()
return
} else {
e.marshal(tag, reflect.ValueOf(v))
}
e.marshal(tag, reflect.ValueOf(v))
return
case encoding.TextMarshaler:
text, err := value.MarshalText()
if err != nil {
fail(err)
}
in = reflect.ValueOf(string(text))
e.rawstringv(tag, string(text))
return
case fmt.Stringer:
e.rawstringv(tag, value.String())
return
case nil:
e.nilv()
return
}

omitAllEmptyFields := false
if opter, ok := iface.(StructEncoderOpter); ok {
omitAllEmptyFields = opter.OmitAllEmptyFieldsOpt()
}

switch in.Kind() {
case reflect.Interface:
e.marshal(tag, in.Elem())
Expand All @@ -165,7 +190,7 @@ func (e *encoder) marshal(tag string, in reflect.Value) {
case reflect.Ptr:
e.marshal(tag, in.Elem())
case reflect.Struct:
e.structv(tag, in)
e.structv(tag, in, omitAllEmptyFields)
case reflect.Slice, reflect.Array:
e.slicev(tag, in)
case reflect.String:
Expand Down Expand Up @@ -211,8 +236,9 @@ func (e *encoder) fieldByIndex(v reflect.Value, index []int) (field reflect.Valu
return v
}

func (e *encoder) structv(tag string, in reflect.Value) {
sinfo, err := getStructInfo(in.Type())
func (e *encoder) structv(tag string, in reflect.Value, omitAllEmptyFields bool) {
sinfo, err := getStructInfo(in.Type(), e.originalCase)

if err != nil {
panic(err)
}
Expand All @@ -227,7 +253,7 @@ func (e *encoder) structv(tag string, in reflect.Value) {
continue
}
}
if info.OmitEmpty && isZero(value) {
if (omitAllEmptyFields || info.OmitEmpty) && isZero(info.Key, value) {
continue
}
e.marshal("", reflect.ValueOf(info.Key))
Expand Down Expand Up @@ -322,8 +348,12 @@ func isOldBool(s string) (result bool) {
}

func (e *encoder) stringv(tag string, in reflect.Value) {
var style yaml_scalar_style_t
s := in.String()
e.rawstringv(tag, s)
}

func (e *encoder) rawstringv(tag string, s string) {
var style yaml_scalar_style_t
canUsePlain := true
switch {
case !utf8.ValidString(s):
Expand Down Expand Up @@ -382,11 +412,13 @@ func (e *encoder) uintv(tag string, in reflect.Value) {
e.emitScalar(s, "", tag, yaml_PLAIN_SCALAR_STYLE, nil, nil, nil, nil)
}

/* Handled by TextMarshaler in time.Time
func (e *encoder) timev(tag string, in reflect.Value) {
t := in.Interface().(time.Time)
s := t.Format(time.RFC3339Nano)
e.emitScalar(s, "", tag, yaml_PLAIN_SCALAR_STYLE, nil, nil, nil, nil)
}
*/

func (e *encoder) floatv(tag string, in reflect.Value) {
// Issue #352: When formatting, use the precision of the underlying value
Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module "gopkg.in/yaml.v3"
module gopkg.in/yaml.v3

require (
"gopkg.in/check.v1" v0.0.0-20161208181325-20d25e280405
)
go 1.19

require gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405