Skip to content

Commit d923275

Browse files
Merge pull request #37 from Stebalien/fix/scratch-map
Always allocate scratch space when marshalling into a map.
2 parents 211df3b + fdf2ae9 commit d923275

File tree

5 files changed

+99
-2
lines changed

5 files changed

+99
-2
lines changed

gen.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,9 +1085,8 @@ func emitCborMarshalStructMap(w io.Writer, gti *GenTypeInfo) error {
10851085
if _, err := w.Write({{ .MapHeaderAsByteString }}); err != nil {
10861086
return err
10871087
}
1088-
{{ if .NeedsScratch }}
1088+
10891089
scratch := make([]byte, 9)
1090-
{{ end }}
10911090
`)
10921091
if err != nil {
10931092
return err

testgen/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ func main() {
1818

1919
if err := cbg.WriteMapEncodersToFile("testing/cbor_map_gen.go", "testing",
2020
types.SimpleTypeTree{},
21+
types.NeedScratchForMap{},
2122
); err != nil {
2223
panic(err)
2324
}

testing/cbor_map_gen.go

Lines changed: 88 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

testing/roundtrip_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ func TestSimpleTypeTree(t *testing.T) {
3737
testTypeRoundtrips(t, reflect.TypeOf(SimpleTypeTree{}))
3838
}
3939

40+
func TestNeedScratchForMap(t *testing.T) {
41+
testTypeRoundtrips(t, reflect.TypeOf(NeedScratchForMap{}))
42+
}
43+
4044
func testValueRoundtrip(t *testing.T, obj cbg.CBORMarshaler, nobj cbg.CBORUnmarshaler) {
4145

4246
buf := new(bytes.Buffer)

testing/types.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,8 @@ type FixedArrays struct {
5454
Uint8 [20]uint8
5555
Uint64 [20]uint64
5656
}
57+
58+
// Do not add fields to this type.
59+
type NeedScratchForMap struct {
60+
Thing bool
61+
}

0 commit comments

Comments
 (0)