Skip to content
This repository was archived by the owner on Jan 28, 2021. It is now read-only.

sql: fix SQL method for arrays of JSON #790

Merged
merged 1 commit into from
Jul 29, 2019
Merged
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
73 changes: 71 additions & 2 deletions sql/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -897,12 +897,17 @@ func (t arrayT) SQL(v interface{}) (sqltypes.Value, error) {
return sqltypes.NULL, nil
}

v, err := t.Convert(v)
v, err := convertForJSON(t, v)
if err != nil {
return sqltypes.Value{}, err
}

return JSON.SQL(v)
val, err := json.Marshal(v)
if err != nil {
return sqltypes.Value{}, err
}

return sqltypes.MakeTrusted(sqltypes.TypeJSON, val), nil
}

func (t arrayT) Convert(v interface{}) (interface{}, error) {
Expand Down Expand Up @@ -1015,6 +1020,7 @@ func IsText(t Type) bool {
return t == Text || t == Blob || t == JSON || IsVarChar(t)
}

// IsVarChar checks if t is a varchar type.
func IsVarChar(t Type) bool {
_, ok := t.(varCharT)
return ok
Expand Down Expand Up @@ -1096,3 +1102,66 @@ func UnderlyingType(t Type) Type {

return a.underlying
}

func convertForJSON(t Type, v interface{}) (interface{}, error) {
switch t := t.(type) {
case jsonT:
val, err := t.Convert(v)
if err != nil {
return nil, err
}

var doc interface{}
err = json.Unmarshal(val.([]byte), &doc)
if err != nil {
return nil, err
}

return doc, nil
case arrayT:
return convertArrayForJSON(t, v)
default:
return t.Convert(v)
}
}

func convertArrayForJSON(t arrayT, v interface{}) (interface{}, error) {
switch v := v.(type) {
case []interface{}:
var result = make([]interface{}, len(v))
for i, v := range v {
var err error
result[i], err = convertForJSON(t.underlying, v)
if err != nil {
return nil, err
}
}
return result, nil
case Generator:
var values []interface{}
for {
val, err := v.Next()
if err != nil {
if err == io.EOF {
break
}
return nil, err
}

val, err = convertForJSON(t.underlying, val)
if err != nil {
return nil, err
}

values = append(values, val)
}

if err := v.Close(); err != nil {
return nil, err
}

return values, nil
default:
return nil, ErrNotArray.New(v)
}
}
16 changes: 16 additions & 0 deletions sql/type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,22 @@ func TestUnderlyingType(t *testing.T) {
require.Equal(t, Text, UnderlyingType(Text))
}

type testJSONStruct struct {
A int
B string
}

func TestJSONArraySQL(t *testing.T) {
require := require.New(t)
val, err := Array(JSON).SQL([]interface{}{
testJSONStruct{1, "foo"},
testJSONStruct{2, "bar"},
})
require.NoError(err)
expected := `[{"A":1,"B":"foo"},{"A":2,"B":"bar"}]`
require.Equal(expected, string(val.Raw()))
}

func eq(t *testing.T, typ Type, a, b interface{}) {
t.Helper()
cmp, err := typ.Compare(a, b)
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy