Skip to content

Commit

Permalink
First pass of omitEmpty support.
Browse files Browse the repository at this point in the history
Structs now inspect the value before each key, because yielding of the key
must of course be skipped if the value is to be skipped.

And yet, we're not done here, and that test is commented out for a reason!
This is more complicated than for e.g. stdlib json.Marshal -- we have to
emit length information at the beginning of an object.

And this, in turn, is capital-H Hard.  Emitting the correct length information
*up front* will require significantly more code changes, and they're a tad
controvertial.  We have to inspect *all* the fields to see if they're going
to be skipped.  And strangely, I think we're going to have to do that twice.
Checking for the fields to skip must happen at the top, that much is clear;
but then to remember which ones we already know will be skipped would require
O(n) memory in the length of the struct... which would imply a heap allocation
to track!  (Worrying about heap allocs is not news in the refmt project because
of our stepfunc design, but it's interesting to note we'd be in trouble anyway:
Go actually always lets runtime-sized slice creation escape to heap:
golang/go#20533 .)  So.  An O(2n) runtime is going
to be a better trade than slipping from constant to O(n) memory.  Hrmph.
Anyway, that bit will be in the next commits.

Signed-off-by: Eric Myhre <[email protected]>
  • Loading branch information
warpfork committed Mar 3, 2018
1 parent 852c21d commit 45a9eaa
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 12 deletions.
42 changes: 42 additions & 0 deletions obj/empty.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package obj

import "reflect"

// The missing definition of 'reflect.IsZero' you've always wanted.
//
// This definition always considers structs non-empty
// (checking struct emptiness can be a relatively costly operation, O(size_of_struct);
// all other kinds can define emptiness in constant time).
func isEmptyValue(v reflect.Value) bool {
switch v.Kind() {
case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
return v.Len() == 0
case reflect.Bool:
return !v.Bool()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return v.Int() == 0
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return v.Uint() == 0
case reflect.Float32, reflect.Float64:
return v.Float() == 0
case reflect.Interface, reflect.Ptr:
return v.IsNil()
}
return false
}

// zeroishness checks on a struct are possible in theory but we're passing for now.
// There's no easy route exposed from the reflect package, due to a variety of reasons:
//
// - The `DeepEqual` method would almost suffice, but we'd need one that takes
// `reflect.Value` instead of `interface{}` params, because we already have
// the former, and un/re-boxing those into two `interface{}` values is two
// heap mallocs and that's a wildly unacceptable performance overhead for this.
// `DeepEqual` calls `deepValueEqual`, which does what we want, but...
// - We can't easily copy the `deepValueEqual` method. It imports the unsafe
// package. This is undesirable because it would reduce the portability of refmt.
//
// It's possible we could produce a struct isZero method which is *simpler* than
// `deepValueEqual`, because we can actually just halt on any non-zero pointer,
// and without any need for following pointers we also need no cycle detection.
// But this is an exercise I'm leaving for later. PRs welcome if someone wants it.
36 changes: 24 additions & 12 deletions obj/marshalStruct.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,22 @@ import (
type marshalMachineStructAtlas struct {
cfg *atlas.AtlasEntry // set on initialization

rv reflect.Value
index int // Progress marker
value bool // Progress marker
target_rv reflect.Value
index int // Progress marker
value_rv reflect.Value // Next value (or nil if next step is key).
}

func (mach *marshalMachineStructAtlas) Reset(_ *marshalSlab, rv reflect.Value, _ reflect.Type) error {
mach.rv = rv
mach.target_rv = rv
mach.index = -1
mach.value = false
mach.value_rv = reflect.Value{}
return nil
}

func (mach *marshalMachineStructAtlas) Step(driver *Marshaller, slab *marshalSlab, tok *Token) (done bool, err error) {
//fmt.Printf("--step on %#v: i=%d/%d v=%v\n", mach.rv, mach.index, len(mach.cfg.Fields), mach.value)
//fmt.Printf("--step on %#v: i=%d/%d v=%v\n", mach.target_rv, mach.index, len(mach.cfg.Fields), mach.value)

// Check boundaries and do the special steps or either start or end.
nEntries := len(mach.cfg.StructMap.Fields)
if mach.index < 0 {
tok.Type = TMapOpen
Expand All @@ -46,21 +48,31 @@ func (mach *marshalMachineStructAtlas) Step(driver *Marshaller, slab *marshalSla
return true, fmt.Errorf("invalid state: entire struct (%d fields) already consumed", nEntries)
}

if mach.value {
fieldEntry := mach.cfg.StructMap.Fields[mach.index]
child_rv := fieldEntry.ReflectRoute.TraverseToValue(mach.rv)
// If value loaded from last step, recurse into handling that.
fieldEntry := mach.cfg.StructMap.Fields[mach.index]
if mach.value_rv != (reflect.Value{}) {
child_rv := mach.value_rv
mach.index++
mach.value = false
mach.value_rv = reflect.Value{}
return false, driver.Recurse(
tok,
child_rv,
fieldEntry.Type,
slab.requisitionMachine(fieldEntry.Type),
)
}

// If value was nil, that indicates we're supposed to pick the value and yield a key.
// We have to look ahead to the value because if it's zero and tagged as
// omitEmpty, then we have to skip emitting the key as well.
mach.value_rv = fieldEntry.ReflectRoute.TraverseToValue(mach.target_rv)
if fieldEntry.OmitEmpty && isEmptyValue(mach.value_rv) {
mach.value_rv = reflect.Value{}
mach.index++
return mach.Step(driver, slab, tok)
}
tok.Type = TString
tok.Str = mach.cfg.StructMap.Fields[mach.index].SerialName
mach.value = true
tok.Str = fieldEntry.SerialName
if mach.index > 0 {
slab.release()
}
Expand Down
12 changes: 12 additions & 0 deletions obj/objFixtures_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,18 @@ var objFixtures = []struct {
valueFn: func() interface{} { return tObjStr{""} }},
},
},
// {title: "omitEmpty resulting in empty map",
// sequence: fixtures.SequenceMap["empty map"],
// atlas: atlas.MustBuild(
// atlas.BuildEntry(tObjStr{}).StructMap().
// AddField("X", atlas.StructMapEntry{SerialName: "key", OmitEmpty: true}).
// Complete(),
// ),
// marshalResults: []marshalResults{
// {title: "from tObjStr",
// valueFn: func() interface{} { var v tObjStr; return v }},
// },
// },
{title: "nulls in map values and struct fields",
sequence: fixtures.SequenceMap["null in map"],
atlas: atlas.MustBuild(
Expand Down

0 comments on commit 45a9eaa

Please sign in to comment.