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

Rename fields when generating map encodings #46

Open
wants to merge 1 commit into
base: master
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,11 @@ var _ = xerrors.Errorf
}

type Field struct {
Name string
Pointer bool
Type reflect.Type
Pkg string
Name string
Serialized string
Pointer bool
Type reflect.Type
Pkg string

IterLabel string
}
Expand Down Expand Up @@ -189,11 +190,16 @@ func ParseTypeInfo(i interface{}) (*GenTypeInfo, error) {
pointer = true
}

serialized, hasSerialized := f.Tag.Lookup("json")
if !hasSerialized {
serialized = f.Name
}
out.Fields = append(out.Fields, Field{
Name: f.Name,
Pointer: pointer,
Type: ft,
Pkg: pkg,
Name: f.Name,
Serialized: serialized,
Pointer: pointer,
Type: ft,
Pkg: pkg,
})
}

Expand Down Expand Up @@ -1124,7 +1130,7 @@ func emitCborMarshalStructMap(w io.Writer, gti *GenTypeInfo) error {
fmt.Fprintf(w, "\n\t// t.%s (%s) (%s)", f.Name, f.Type, f.Type.Kind())

if err := emitCborMarshalStringField(w, Field{
Name: `"` + f.Name + `"`,
Name: `"` + f.Serialized + `"`,
}); err != nil {
return err
}
Expand Down Expand Up @@ -1219,7 +1225,7 @@ func (t *{{ .Name}}) UnmarshalCBOR(r io.Reader) error {
fmt.Fprintf(w, "// t.%s (%s) (%s)", f.Name, f.Type, f.Type.Kind())

err := doTemplate(w, f, `
case "{{ .Name }}":
case "{{ .Serialized }}":
`)
if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions testgen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func main() {
if err := cbg.WriteMapEncodersToFile("testing/cbor_map_gen.go", "testing",
types.SimpleTypeTree{},
types.NeedScratchForMap{},
types.MapWithRenames{},
); err != nil {
panic(err)
}
Expand Down
165 changes: 165 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.

4 changes: 4 additions & 0 deletions testing/roundtrip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ func TestNeedScratchForMap(t *testing.T) {
testTypeRoundtrips(t, reflect.TypeOf(NeedScratchForMap{}))
}

func TestMapWithRenames(t *testing.T) {
testTypeRoundtrips(t, reflect.TypeOf(MapWithRenames{}))
}

func testValueRoundtrip(t *testing.T, obj cbg.CBORMarshaler, nobj cbg.CBORUnmarshaler) {

buf := new(bytes.Buffer)
Expand Down
6 changes: 6 additions & 0 deletions testing/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,9 @@ type ThingWithSomeTime struct {
type NeedScratchForMap struct {
Thing bool
}

type MapWithRenames struct {
String string `json:"s"`
Int uint64 `json:"i"`
Bytes []byte `json:"b"`
}