Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SNOW-1669514] Honor the Valuer/Stringer methods to resolve #1209 #1211

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.idea/
.vscode/
parameters*.json
parameters*.bat
*.p8
Expand All @@ -11,6 +12,7 @@ wss-golang-agent.config
wss-unified-agent.jar
whitesource/
*.swp
cp.out

# exclude vendor
vendor
15 changes: 15 additions & 0 deletions converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,21 @@ func arrayToString(v driver.Value, tsmode snowflakeType, params map[string]*stri
return bindingValue{&res, "json", &schemaForBytes}, nil
} else if isSliceOfSlices(v) {
return bindingValue{}, errors.New("array of arrays is not supported")
} else if valuer, ok := v1.Interface().(driver.Valuer); ok { // check for driver.Valuer satisfaction and honor that first
value, err := valuer.Value()

if err != nil || value == nil {
return bindingValue{}, err
}

if v, ok := value.(string); ok {
return bindingValue{&v, "", nil}, nil
}

return bindingValue{}, nil
} else if stringer, ok := v1.Interface().(fmt.Stringer); ok { // alternate approach; check for stringer method. Guarantees it's String() and returns string
value := stringer.String()
return bindingValue{&value, "", nil}, nil
}
res, err := json.Marshal(v)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After this, the bindingValue is set to "json". Should that be the case, or should it be string? Since it is already Marshaled, why set it to json again? That will cause it to be marshaled a second time. It feels like it should not marshal here if it also sets the binding value to json.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to be clear This is in reference to the code on Lines 406-409; the added code will not return the "json" string as it's already been transformed

if err != nil {
Expand Down
22 changes: 22 additions & 0 deletions converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,12 @@ func (o *testValueToStringStructuredObject) Write(sowc StructuredObjectWriterCon
return nil
}

type testSqlUuid = UUID

func (uuid testSqlUuid) Value() (driver.Value, error) {
return uuid.String(), nil
}

func TestValueToString(t *testing.T) {
v := cmplx.Sqrt(-5 + 12i) // should never happen as Go sql package must have already validated.
_, err := valueToString(v, nullType, nil)
Expand Down Expand Up @@ -282,6 +288,22 @@ func TestValueToString(t *testing.T) {
assertEqualE(t, *bv.value, "[1,2]")
})

t.Run("UUID - should return string", func(t *testing.T) {
u := NewUUID()
bv, err := valueToString(u, textType, nil)
assertNilF(t, err)
assertEmptyStringE(t, bv.format)
assertEqualE(t, *bv.value, u.String())
})

t.Run("database/sql/driver - Valuer interface", func(t *testing.T) {
u := testSqlUuid(NewUUID())
bv, err := valueToString(u, textType, nil)
assertNilF(t, err)
assertEmptyStringE(t, bv.format)
assertEqualE(t, *bv.value, u.String())
})

bv, err = valueToString(&testValueToStringStructuredObject{s: "some string", i: 123, date: time.Date(2024, time.May, 24, 0, 0, 0, 0, time.UTC)}, timestampLtzType, params)
assertNilF(t, err)
assertEqualE(t, bv.format, "json")
Expand Down