Skip to content

Commit 0dcec8f

Browse files
elianddbJames Cor
authored andcommitted
refactor to ret conversion instead
1 parent a655e11 commit 0dcec8f

File tree

1 file changed

+15
-27
lines changed

1 file changed

+15
-27
lines changed

sql/types/json.go

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,9 @@ func (t JsonType) Compare(ctx context.Context, a interface{}, b interface{}) (in
4545
return CompareJSON(ctx, a, b)
4646
}
4747

48-
// convertJSONValue parses JSON-encoded data if the input is a string or []byte, returning the resulting Go value. For
49-
// other types, the value is returned as-is. The returned value is the raw, unwrapped JSON representation and is later
50-
// wrapped in a JSONDocument by JsonType.Convert.
51-
func convertJSONValue(v interface{}) (val interface{}, inRange sql.ConvertInRange, err error) {
48+
// convertJSONValue parses JSON-encoded data if the input is a string or []byte, returning the resulting JSONDocument. For
49+
// other types, the value is returned if it can be marshalled.
50+
func convertJSONValue(v interface{}) (interface{}, sql.ConvertInRange, error) {
5251
var data []byte
5352
var charsetMaxLength int64 = 1
5453
switch x := v.(type) {
@@ -62,46 +61,39 @@ func convertJSONValue(v interface{}) (val interface{}, inRange sql.ConvertInRang
6261
// a valid JSON document representation
6362
if b, berr := json.Marshal(v); berr == nil {
6463
data = b
65-
}
64+
} else {
65+
return JSONDocument{Val: nil}, sql.InRange, nil
66+
}
6667
}
6768

6869
if int64(len(data))*charsetMaxLength > MaxJsonFieldByteLength {
69-
return nil, sql.InRange, ErrLengthTooLarge.New(len(data), MaxJsonFieldByteLength)
70+
return JSONDocument{Val: nil}, sql.InRange, ErrLengthTooLarge.New(len(data), MaxJsonFieldByteLength)
7071
}
7172

73+
var val interface{}
7274
if err := json.Unmarshal(data, &val); err != nil {
73-
return nil, sql.OutOfRange, sql.ErrInvalidJson.New(err.Error())
75+
return JSONDocument{Val: nil}, sql.OutOfRange, sql.ErrInvalidJson.New(err.Error())
7476
}
7577

76-
return val, sql.InRange, nil
78+
return JSONDocument{Val: val}, sql.InRange, nil
7779
}
7880

7981
// Convert implements Type interface.
80-
func (t JsonType) Convert(c context.Context, v interface{}) (doc interface{}, inRange sql.ConvertInRange, err error) {
81-
docVal := v
82+
func (t JsonType) Convert(c context.Context, v interface{}) (interface{}, sql.ConvertInRange, error) {
8283
switch v := v.(type) {
8384
case sql.JSONWrapper:
8485
return v, sql.InRange, nil
8586
case []byte:
86-
docVal, inRange, err = convertJSONValue(v)
87-
if err != nil {
88-
return nil, inRange, err
89-
}
87+
return convertJSONValue(v)
9088
case string:
91-
docVal, inRange, err = convertJSONValue(v)
92-
if err != nil {
93-
return nil, inRange, err
94-
}
89+
return convertJSONValue(v)
9590
// Text values may be stored in wrappers (e.g. Dolt's TextStorage), so unwrap to the raw string before decoding.
9691
case sql.StringWrapper:
9792
str, err := v.Unwrap(c)
9893
if err != nil {
9994
return nil, sql.OutOfRange, err
10095
}
101-
docVal, inRange, err = convertJSONValue(str)
102-
if err != nil {
103-
return nil, inRange, err
104-
}
96+
return convertJSONValue(str)
10597
case int8:
10698
return JSONDocument{Val: int64(v)}, sql.InRange, nil
10799
case int16:
@@ -125,12 +117,8 @@ func (t JsonType) Convert(c context.Context, v interface{}) (doc interface{}, in
125117
case decimal.Decimal:
126118
return JSONDocument{Val: v}, sql.InRange, nil
127119
default:
128-
docVal, inRange, err = convertJSONValue(v)
129-
}
130-
if err != nil {
131-
return nil, sql.OutOfRange, err
120+
return convertJSONValue(v)
132121
}
133-
return JSONDocument{Val: docVal}, sql.InRange, nil
134122
}
135123

136124
// Equals implements the Type interface.

0 commit comments

Comments
 (0)