Skip to content

Commit 74d7464

Browse files
Merge pull request #101 from brianolson/fix-str-ptr-slice
fix handling of an []*string field
2 parents cdbc4c7 + 41fee79 commit 74d7464

File tree

5 files changed

+249
-3
lines changed

5 files changed

+249
-3
lines changed

gen.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,9 +1372,10 @@ func (g Gen) emitCborUnmarshalSliceField(w io.Writer, f Field) error {
13721372

13731373
case reflect.String:
13741374
subf := Field{
1375-
Type: e,
1376-
Pkg: f.Pkg,
1377-
Name: f.Name + "[" + f.IterLabel + "]",
1375+
Type: e,
1376+
Pkg: f.Pkg,
1377+
Pointer: pointer,
1378+
Name: f.Name + "[" + f.IterLabel + "]",
13781379
}
13791380
err := g.emitCborUnmarshalStringField(w, subf)
13801381
if err != nil {

testgen/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ func main() {
3737
types.TestCanonicalFieldOrder{},
3838
types.MapStringString{},
3939
types.TestSliceNilPreserve{},
40+
types.StringPtrSlices{},
4041
); err != nil {
4142
panic(err)
4243
}

testing/cbor_map_gen.go

Lines changed: 223 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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,22 @@ func TestOptionalInts(t *testing.T) {
770770
testValueRoundtrip(t, val, recepticle, WithGolden([]byte{0x84, 0xf6, 0x02, 0x03, 0xf6}))
771771
}
772772

773+
func TestStringPtrSlices(t *testing.T) {
774+
foo := "foo"
775+
bar := "bar"
776+
ob := StringPtrSlices{
777+
Strings: []string{
778+
"a", "b", "c",
779+
},
780+
StringPtrs: []*string{
781+
&foo, nil, &bar,
782+
},
783+
}
784+
ob2 := StringPtrSlices{}
785+
786+
testValueRoundtrip(t, &ob, &ob2)
787+
}
788+
773789
func ptr[T any](v T) *T {
774790
return &v
775791
}

testing/types.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,8 @@ func (ls LongString) Generate(rand *rand.Rand, size int) reflect.Value {
199199
type BigIntContainer struct {
200200
Int *big.Int
201201
}
202+
203+
type StringPtrSlices struct {
204+
Strings []string
205+
StringPtrs []*string
206+
}

0 commit comments

Comments
 (0)