Skip to content

Commit

Permalink
Merge pull request #1487 from ydb-platform/allow-skip-sql-fields
Browse files Browse the repository at this point in the history
Allowed skip struct field in ScanStruct by tag -
  • Loading branch information
asmyasnikov authored Oct 2, 2024
2 parents eb7d205 + a760212 commit 05a2dde
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
* Allowed skip column for `ScanStruct` by tag `-`

## v3.81.4
* Returned `topicwriter.ErrQueueLimitExceed`, accidental removed at `v3.81.0`

Expand Down
4 changes: 4 additions & 0 deletions internal/query/scanner/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ func (s StructScanner) ScanStruct(dst interface{}, opts ...ScanStructOption) (er
existingFields := make(map[string]struct{}, tt.NumField())
for i := 0; i < tt.NumField(); i++ {
name := fieldName(tt.Field(i), settings.TagName)
if name == "-" {
continue
}

v, err := s.data.seekByName(name)
if err != nil {
missingColumns = append(missingColumns, name)
Expand Down
31 changes: 31 additions & 0 deletions internal/query/scanner/struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,37 @@ func TestStructNotFoundColumns(t *testing.T) {
require.ErrorIs(t, err, ErrColumnsNotFoundInRow)
}

func TestStructSkippedColumns(t *testing.T) {
scanner := Struct(Data(
[]*Ydb.Column{
{
Name: "A",
Type: &Ydb.Type{
Type: &Ydb.Type_TypeId{
TypeId: Ydb.Type_UTF8,
},
},
},
},
[]*Ydb.Value{
{
Value: &Ydb.Value_TextValue{
TextValue: "test-a",
},
},
},
))

var row struct {
A string
C string `sql:"-"`
}
err := scanner.ScanStruct(&row)
require.NoError(t, err)
require.Equal(t, "test-a", row.A)
require.Empty(t, row.C)
}

func TestStructWithAllowMissingColumnsFromSelect(t *testing.T) {
scanner := Struct(Data(
[]*Ydb.Column{
Expand Down

0 comments on commit 05a2dde

Please # to comment.