Skip to content

Commit

Permalink
Merge pull request #294 from kopera/feature/rename-struct-field
Browse files Browse the repository at this point in the history
Add support for renaming struct fields
  • Loading branch information
taniabogatsch authored Oct 28, 2024
2 parents 5177403 + 0c93fef commit 970d79d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
19 changes: 12 additions & 7 deletions appender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,15 @@ import (
)

type simpleStruct struct {
A int32
A int32 `db:"a"`
B string
}

type duplicateKeyStruct struct {
A int64 `db:"Duplicate"`
Duplicate int64
}

type wrappedSimpleStruct struct {
A string
B simpleStruct
Expand Down Expand Up @@ -321,7 +326,7 @@ func TestAppenderNullStruct(t *testing.T) {
t.Parallel()
c, con, a := prepareAppender(t, `
CREATE TABLE test (
simple_struct STRUCT(A INT, B VARCHAR)
simple_struct STRUCT(a INT, B VARCHAR)
)`)

require.NoError(t, a.AppendRow(simpleStruct{1, "hello"}))
Expand Down Expand Up @@ -358,7 +363,7 @@ func TestAppenderNestedNullStruct(t *testing.T) {
Y STRUCT(
N VARCHAR,
M STRUCT(
A INT,
a INT,
B VARCHAR
)
)
Expand Down Expand Up @@ -773,19 +778,19 @@ const createNestedDataTableSQL = `
int_list INT[],
nested_int_list INT[][],
triple_nested_int_list INT[][][],
simple_struct STRUCT(A INT, B VARCHAR),
wrapped_struct STRUCT(N VARCHAR, M STRUCT(A INT, B VARCHAR)),
simple_struct STRUCT(a INT, B VARCHAR),
wrapped_struct STRUCT(N VARCHAR, M STRUCT(a INT, B VARCHAR)),
double_wrapped_struct STRUCT(
X VARCHAR,
Y STRUCT(
N VARCHAR,
M STRUCT(
A INT,
a INT,
B VARCHAR
)
)
),
struct_list STRUCT(A INT, B VARCHAR)[],
struct_list STRUCT(a INT, B VARCHAR)[],
struct_with_list STRUCT(L INT[]),
mix STRUCT(
A STRUCT(L VARCHAR[]),
Expand Down
15 changes: 13 additions & 2 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,21 @@ func TestErrAppendSimpleStruct(t *testing.T) {
cleanupAppender(t, c, con, a)
}

func TestErrAppendDuplicateStruct(t *testing.T) {
c, con, a := prepareAppender(t, `
CREATE TABLE test (
duplicate_struct STRUCT(Duplicate INT)
)`)

err := a.AppendRow(duplicateKeyStruct{1, 2})
testError(t, err, errAppenderAppendRow.Error(), duplicateNameErrMsg)
cleanupAppender(t, c, con, a)
}

func TestErrAppendStruct(t *testing.T) {
c, con, a := prepareAppender(t, `
CREATE TABLE test (
mix STRUCT(A STRUCT(L VARCHAR[]), B STRUCT(L INT[])[])
mix STRUCT(a STRUCT(L VARCHAR[]), B STRUCT(L INT[])[])
)`)

err := a.AppendRow(simpleStruct{1, "hello"})
Expand Down Expand Up @@ -274,7 +285,7 @@ func TestErrAppendStructWithList(t *testing.T) {
func TestErrAppendNestedStruct(t *testing.T) {
c, con, a := prepareAppender(t, `
CREATE TABLE test (
wrapped_simple_struct STRUCT(A VARCHAR, B STRUCT(A INT, B VARCHAR)),
wrapped_simple_struct STRUCT(a VARCHAR, B STRUCT(A INT, B VARCHAR)),
)`)

err := a.AppendRow(simpleStruct{1, "hello"})
Expand Down
6 changes: 6 additions & 0 deletions vector_setters.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,12 @@ func setStruct[S any](vec *vector, rowIdx C.idx_t, val S) error {
continue
}
fieldName := structType.Field(i).Name
if name, ok := structType.Field(i).Tag.Lookup("db"); ok {
fieldName = name
}
if _, ok := m[fieldName]; ok {
return duplicateNameError(fieldName)
}
m[fieldName] = rv.Field(i).Interface()
}
}
Expand Down

0 comments on commit 970d79d

Please sign in to comment.