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

support omitting empty fields on map encoders #77

Merged
merged 1 commit into from
Dec 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 69 additions & 10 deletions gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ type Field struct {
Type reflect.Type
Pkg string

OmitEmpty bool
IterLabel string

MaxLen int
Expand Down Expand Up @@ -203,13 +204,16 @@ func ParseTypeInfo(i interface{}) (*GenTypeInfo, error) {
usrMaxLen = val
}

_, omitempty := tags["omitempty"]

out.Fields = append(out.Fields, Field{
Name: f.Name,
MapKey: mapk,
Pointer: pointer,
Type: ft,
Pkg: pkg,
MaxLen: usrMaxLen,
Name: f.Name,
MapKey: mapk,
Pointer: pointer,
Type: ft,
Pkg: pkg,
OmitEmpty: omitempty,
MaxLen: usrMaxLen,
})
}

Expand All @@ -231,6 +235,8 @@ func tagparse(v string) (map[string]string, error) {
}

out[strings.TrimSpace(parts[0])] = strings.TrimSpace(parts[1])
} else if elem == "omitempty" {
out["omitempty"] = "true"
} else {
out["name"] = elem
}
Expand All @@ -245,7 +251,10 @@ func (gti GenTypeInfo) TupleHeader() []byte {
}

func (gti GenTypeInfo) TupleHeaderAsByteString() string {
h := gti.TupleHeader()
return MakeByteString(gti.TupleHeader())
}

func MakeByteString(h []byte) string {
s := "[]byte{"
for _, b := range h {
s += fmt.Sprintf("%d,", b)
Expand Down Expand Up @@ -1199,33 +1208,77 @@ func GenTupleEncodersForType(gti *GenTypeInfo, w io.Writer) error {
}

func emitCborMarshalStructMap(w io.Writer, gti *GenTypeInfo) error {
var hasOmitEmpty bool
for _, f := range gti.Fields {
if f.OmitEmpty {
hasOmitEmpty = true
}
}

err := doTemplate(w, gti, `func (t *{{ .Name }}) MarshalCBOR(w io.Writer) error {
if t == nil {
_, err := w.Write(cbg.CborNull)
return err
}

cw := cbg.NewCborWriter(w)
`)
if err != nil {
return err
}

if hasOmitEmpty {
fmt.Fprintln(w, "var emptyFieldCount int")
for _, f := range gti.Fields {
if f.OmitEmpty {
err := doTemplate(w, f, `
if t.{{ .Name }} == nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't go's definition of "empty".

The "omitempty" option specifies that the field should be omitted from the encoding if the field has an empty value, defined as false, 0, a nil pointer, a nil interface value, and any empty array, slice, map, or string.

You can:

  • Do nothing if you don't care.
  • Use reflect.Value.IsZero (some runtime cost).
  • Have a bunch of logic to define the "zeros" for different kinds.
  • Define per-field zero values as "variables" in codegen (using reflect.Zero).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, so, there's an easier way.

var {{.Name}}Zero {{.Type}}
if t.{{.Name}} == {{.Name}}Zero {
    ...
}

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah... I was getting kinda lazy since this fix was three layers down the rabbit hole. I'd like to avoid runtime costs so i think generator time type checking is the right thing to do

emptyFieldCount++
}
`)
if err != nil {
return err
}
}
}

fmt.Fprintf(w, `
if _, err := cw.Write(cbg.CborEncodeMajorType(cbg.MajMap, uint64(%d - emptyFieldCount))); err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: define fieldCount, then subtract one by one for every empty field.

return err
}
`, len(gti.Fields))
if err != nil {
return err
}

} else {

err = doTemplate(w, gti, `
if _, err := cw.Write({{ .MapHeaderAsByteString }}); err != nil {
return err
}
`)
if err != nil {
return err
if err != nil {
return err
}
}

for _, f := range gti.Fields {
fmt.Fprintf(w, "\n\t// t.%s (%s) (%s)", f.Name, f.Type, f.Type.Kind())

if f.OmitEmpty {
if err := doTemplate(w, f, "\nif t.{{.Name}} != nil {\n"); err != nil {
return err
}
}

if err := emitCborMarshalStringField(w, Field{
Name: `"` + f.MapKey + `"`,
}); err != nil {
return err
}

f.Name = "t." + f.Name

switch f.Type.Kind() {
case reflect.String:
if err := emitCborMarshalStringField(w, f); err != nil {
Expand Down Expand Up @@ -1264,6 +1317,12 @@ func emitCborMarshalStructMap(w io.Writer, gti *GenTypeInfo) error {
default:
return fmt.Errorf("field %q of %q has unsupported kind %q", f.Name, gti.Name, f.Type.Kind())
}

if f.OmitEmpty {
if err := doTemplate(w, f, "}\n"); err != nil {
return err
}
}
}

fmt.Fprintf(w, "\treturn nil\n}\n\n")
Expand Down
1 change: 1 addition & 0 deletions testgen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func main() {
types.SimpleStructV1{},
types.SimpleStructV2{},
types.RenamedFields{},
types.TestEmpty{},
); err != nil {
panic(err)
}
Expand Down
167 changes: 167 additions & 0 deletions testing/cbor_map_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions testing/roundtrip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,4 +378,14 @@ func TestLargeField(t *testing.T) {
}
}

func TestOmitEmpty(t *testing.T) {
et := TestEmpty{
Cat: 167,
}

recepticle := TestEmpty{}

testValueRoundtrip(t, &et, &recepticle)
}

//TODO same for strings
5 changes: 5 additions & 0 deletions testing/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,8 @@ type RenamedFields struct {
type BigField struct {
LargeBytes []byte `cborgen:"maxlen=10000000"`
}

type TestEmpty struct {
Foo *string `cborgen:"omitempty"`
Cat int64
}