Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
12 changes: 10 additions & 2 deletions schema/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,20 @@ func (dst *JSON) Set(src interface{}) error {

switch value := src.(type) {
case string:
*dst = JSON{Bytes: []byte(value), Status: Present}
if value == "" {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you test with *string and string cases removed from the switch? Just let it fall to default?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

If we do that, then all of those strings will just be turned into {} rather than ""... Depends on the behavior we want...

*dst = JSON{Bytes: []byte(`""`), Status: Present}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I believe JSON{Status: Null} would be more correct here.
Current implementation results in a "" value in the postgres column (which is kinda weird - because likely the policy is an object of some-sort like {"some-policy": "some-value"}).

Just null i think - no reason to distinguish between empty-string and nonexistent.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I was trying to keep the same behavior that go has when we call Marshal...

json.Marshal("") produces ""

} else {
*dst = JSON{Bytes: []byte(value), Status: Present}
}
case *string:
if value == nil {
*dst = JSON{Status: Null}
} else {
*dst = JSON{Bytes: []byte(*value), Status: Present}
if *value == "" {
*dst = JSON{Bytes: []byte(`""`), Status: Present}
} else {
*dst = JSON{Bytes: []byte(*value), Status: Present}
}
}
case []byte:
if value == nil {
Expand Down
4 changes: 4 additions & 0 deletions schema/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ type Foo struct {
}

func TestJSONSet(t *testing.T) {
emptyString := ""
successfulTests := []struct {
source interface{}
result JSON
}{

{source: emptyString, result: JSON{Bytes: []byte(`""`), Status: Present}},
{source: &emptyString, result: JSON{Bytes: []byte(`""`), Status: Present}},
{source: "{}", result: JSON{Bytes: []byte("{}"), Status: Present}},
{source: []byte("{}"), result: JSON{Bytes: []byte("{}"), Status: Present}},
{source: ([]byte)(nil), result: JSON{Status: Null}},
Expand Down