Skip to content

Commit 6cd6a73

Browse files
authored
feat(spanner): SelectAll struct spanner tag annotation match should be case-insensitive (#9460)
* feat(spanner): SelectAll struct spanner tag annotation match should be case-insensitive * feat(spanner): Add a test case (#9460)
1 parent 0676670 commit 6cd6a73

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

spanner/row.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -566,8 +566,8 @@ func initFieldTag(sliceItem reflect.Value, fieldTagMap *map[string]reflect.Value
566566
continue
567567
}
568568
if name == "" {
569-
name = strings.ToLower(fieldType.Name)
569+
name = fieldType.Name
570570
}
571-
(*fieldTagMap)[name] = sliceItem.Field(i)
571+
(*fieldTagMap)[strings.ToLower(name)] = sliceItem.Field(i)
572572
}
573573
}

spanner/row_test.go

+40-1
Original file line numberDiff line numberDiff line change
@@ -2008,6 +2008,12 @@ func TestSelectAll(t *testing.T) {
20082008
Col3 string
20092009
Col4 time.Time
20102010
}
2011+
type testStructWithTag struct {
2012+
Col1 int64 `spanner:"tag1"`
2013+
Col2 float64 `spanner:"Tag2"`
2014+
Col3 string `spanner:"taG3"`
2015+
Col4 time.Time `spanner:"TAG4"`
2016+
}
20112017
tests := []struct {
20122018
name string
20132019
args args
@@ -2175,7 +2181,40 @@ func TestSelectAll(t *testing.T) {
21752181
{Col1: 3, COL2: 3.3, Col3: "value3"},
21762182
{Col1: 1, COL2: 1.1, Col3: "value"},
21772183
{Col1: 2, COL2: 2.2, Col3: "value2"},
2178-
}},
2184+
},
2185+
},
2186+
{
2187+
name: "success: using slice of structs with spanner tag annotations",
2188+
args: args{
2189+
destination: &[]testStructWithTag{},
2190+
mock: func(mockIterator *mockRowIterator) {
2191+
mockIterator.On("Next").Once().Return(&Row{
2192+
[]*sppb.StructType_Field{
2193+
{Name: "Tag1", Type: intType()},
2194+
{Name: "Tag2", Type: floatType()},
2195+
{Name: "Tag3", Type: stringType()},
2196+
{Name: "Tag4", Type: timeType()},
2197+
},
2198+
[]*proto3.Value{intProto(1), floatProto(1.1), stringProto("value"), timeProto(tm)},
2199+
}, nil)
2200+
mockIterator.On("Next").Once().Return(&Row{
2201+
[]*sppb.StructType_Field{
2202+
{Name: "Tag1", Type: intType()},
2203+
{Name: "Tag2", Type: floatType()},
2204+
{Name: "Tag3", Type: stringType()},
2205+
{Name: "Tag4", Type: timeType()},
2206+
},
2207+
[]*proto3.Value{intProto(2), floatProto(2.2), stringProto("value2"), timeProto(tm.Add(24 * time.Hour))},
2208+
}, nil)
2209+
mockIterator.On("Next").Once().Return(nil, iterator.Done)
2210+
mockIterator.On("Stop").Once().Return(nil)
2211+
},
2212+
},
2213+
want: &[]testStructWithTag{
2214+
{Col1: 1, Col2: 1.1, Col3: "value", Col4: tm},
2215+
{Col1: 2, Col2: 2.2, Col3: "value2", Col4: tm.Add(24 * time.Hour)},
2216+
},
2217+
},
21792218
{
21802219
name: "failure: in case of error destination will have the partial result",
21812220
args: args{

0 commit comments

Comments
 (0)