Skip to content

Commit

Permalink
Merge pull request #37 from Stebalien/fix/scratch-map
Browse files Browse the repository at this point in the history
Always allocate scratch space when marshalling into a map.
  • Loading branch information
whyrusleeping authored Aug 11, 2020
2 parents 211df3b + fdf2ae9 commit d923275
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 2 deletions.
3 changes: 1 addition & 2 deletions gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -1085,9 +1085,8 @@ func emitCborMarshalStructMap(w io.Writer, gti *GenTypeInfo) error {
if _, err := w.Write({{ .MapHeaderAsByteString }}); err != nil {
return err
}
{{ if .NeedsScratch }}
scratch := make([]byte, 9)
{{ end }}
`)
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 @@ -18,6 +18,7 @@ func main() {

if err := cbg.WriteMapEncodersToFile("testing/cbor_map_gen.go", "testing",
types.SimpleTypeTree{},
types.NeedScratchForMap{},
); err != nil {
panic(err)
}
Expand Down
88 changes: 88 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 @@ -37,6 +37,10 @@ func TestSimpleTypeTree(t *testing.T) {
testTypeRoundtrips(t, reflect.TypeOf(SimpleTypeTree{}))
}

func TestNeedScratchForMap(t *testing.T) {
testTypeRoundtrips(t, reflect.TypeOf(NeedScratchForMap{}))
}

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

buf := new(bytes.Buffer)
Expand Down
5 changes: 5 additions & 0 deletions testing/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,8 @@ type FixedArrays struct {
Uint8 [20]uint8
Uint64 [20]uint64
}

// Do not add fields to this type.
type NeedScratchForMap struct {
Thing bool
}

0 comments on commit d923275

Please sign in to comment.