Skip to content

Commit

Permalink
Better json func, and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wexder committed Jun 4, 2022
1 parent 0425e88 commit 11b0a68
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
9 changes: 7 additions & 2 deletions postgres/literal.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,13 @@ func String(value string) StringExpression {
}

// Json creates new json literal expression
func Json(value string) StringExpression {
return StringExp(CAST(jet.String(value)).AS("json"))
func Json(value interface{}) StringExpression {
switch value.(type) {
case string, []byte:
default:
panic("Bytea parameter value has to be of the type string or []byte")
}
return StringExp(CAST(jet.Literal(value)).AS("json"))
}

// UUID is a helper function to create string literal expression from uuid object
Expand Down
5 changes: 5 additions & 0 deletions postgres/literal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ func TestBytea(t *testing.T) {
assertSerialize(t, Bytea([]byte("Some byte array")), `$1::bytea`, []byte("Some byte array"))
}

func TestJson(t *testing.T) {
assertSerialize(t, Json("{\"key\": \"value\"}"), `$1::json`, "{\"key\": \"value\"}")
assertSerialize(t, Json([]byte("{\"key\": \"value\"}")), `$1::json`, []byte("{\"key\": \"value\"}"))
}

func TestDate(t *testing.T) {
assertSerialize(t, Date(2014, time.January, 2), `$1::date`, "2014-01-02")
assertSerialize(t, DateT(time.Now()), `$1::date`)
Expand Down

0 comments on commit 11b0a68

Please sign in to comment.