Skip to content

added json unmarshaler for castTo #1825

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
* Added support for the `json.Unmarshaler` interface in the `CastTo` function for use in scanners, such as the `ScanStruct` method

## v3.111.3
* Fixed session closing in `ydb.WithExecuteDataQueryOverQueryClient(true)` scenario

Expand Down
131 changes: 131 additions & 0 deletions internal/value/cast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package value

import (
"database/sql/driver"
"encoding/json"
"errors"
"reflect"
"testing"
"time"
Expand All @@ -25,6 +27,28 @@ func unwrapPtr(v interface{}) interface{} {
return reflect.ValueOf(v).Elem().Interface()
}

type jsonUnmarshaller struct {
bytes []byte
}

func (json *jsonUnmarshaller) UnmarshalJSON(bytes []byte) error {
json.bytes = bytes

return nil
}

var _ json.Unmarshaler = &jsonUnmarshaller{}

type jsonUnmarshallerBroken struct {
bytes []byte
}

func (json *jsonUnmarshallerBroken) UnmarshalJSON(_ []byte) error {
return errors.New("unmarshal error")
}

var _ json.Unmarshaler = &jsonUnmarshallerBroken{}

func loadLocation(t *testing.T, name string) *time.Location {
loc, err := time.LoadLocation(name)
require.NoError(t, err)
Expand Down Expand Up @@ -138,6 +162,75 @@ func TestCastTo(t *testing.T) {
err: ErrCannotCast,
},

// JSONValue
{
name: xtest.CurrentFileLine(),
value: JSONValue(`{"test": "text"}"`),
dst: ptr[string](),
exp: `{"test": "text"}"`,
err: nil,
},
{
name: xtest.CurrentFileLine(),
value: JSONValue(`{"test":"text"}"`),
dst: ptr[Value](),
exp: JSONValue(`{"test":"text"}"`),
err: nil,
},
{
name: xtest.CurrentFileLine(),
value: OptionalValue(JSONValue(`{"test": "text"}"`)),
dst: ptr[*[]byte](),
exp: value2ptr([]byte(`{"test": "text"}"`)),
err: nil,
},
{
name: xtest.CurrentFileLine(),
value: JSONValue(`{"test":"text"}"`),
dst: ptr[[]byte](),
exp: []byte(`{"test":"text"}"`),
err: nil,
},
{
name: xtest.CurrentFileLine(),
value: JSONValue(`{"test": "text"}"`),
dst: ptr[jsonUnmarshaller](),
exp: jsonUnmarshaller{[]byte(`{"test": "text"}"`)},
err: nil,
},
{
name: xtest.CurrentFileLine(),
value: JSONValue(`{"test": "text"}"`),
dst: ptr[jsonUnmarshallerBroken](),
err: ErrCannotCast,
},
{
name: xtest.CurrentFileLine(),
value: OptionalValue(JSONValue(`{"test": "text"}"`)),
dst: ptr[jsonUnmarshaller](),
exp: jsonUnmarshaller{[]byte(`{"test": "text"}"`)},
err: nil,
},
{
name: xtest.CurrentFileLine(),
value: OptionalValue(JSONValue(`{"test": "text"}"`)),
dst: ptr[jsonUnmarshallerBroken](),
err: ErrCannotCast,
},
{
name: xtest.CurrentFileLine(),
value: JSONValue(`{"test": "text"}"`),
dst: ptr[int](),
err: ErrCannotCast,
},
{
name: xtest.CurrentFileLine(),
value: OptionalValue(JSONValue(`{"test": "text"}"`)),
dst: ptr[int](),
err: ErrCannotCast,
},

// JSONDocumentValue
{
name: xtest.CurrentFileLine(),
value: JSONDocumentValue(`{"test": "text"}"`),
Expand Down Expand Up @@ -166,6 +259,44 @@ func TestCastTo(t *testing.T) {
exp: []byte(`{"test":"text"}"`),
err: nil,
},
{
name: xtest.CurrentFileLine(),
value: JSONDocumentValue(`{"test": "text"}"`),
dst: ptr[jsonUnmarshaller](),
exp: jsonUnmarshaller{[]byte(`{"test": "text"}"`)},
err: nil,
},
{
name: xtest.CurrentFileLine(),
value: JSONDocumentValue(`{"test": "text"}"`),
dst: ptr[jsonUnmarshallerBroken](),
err: ErrCannotCast,
},
{
name: xtest.CurrentFileLine(),
value: OptionalValue(JSONDocumentValue(`{"test": "text"}"`)),
dst: ptr[jsonUnmarshaller](),
exp: jsonUnmarshaller{[]byte(`{"test": "text"}"`)},
err: nil,
},
{
name: xtest.CurrentFileLine(),
value: OptionalValue(JSONDocumentValue(`{"test": "text"}"`)),
dst: ptr[jsonUnmarshallerBroken](),
err: ErrCannotCast,
},
{
name: xtest.CurrentFileLine(),
value: JSONDocumentValue(`{"test": "text"}"`),
dst: ptr[int](),
err: ErrCannotCast,
},
{
name: xtest.CurrentFileLine(),
value: OptionalValue(JSONDocumentValue(`{"test": "text"}"`)),
dst: ptr[int](),
err: ErrCannotCast,
},

{
name: xtest.CurrentFileLine(),
Expand Down
21 changes: 21 additions & 0 deletions internal/value/value.go
Copy link
Member

Choose a reason for hiding this comment

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

Не хватает строчки в CHANGELOG.md

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package value
import (
"database/sql/driver"
"encoding/binary"
"encoding/json"
"fmt"
"math/big"
"reflect"
Expand Down Expand Up @@ -1369,6 +1370,16 @@ func (v jsonValue) castTo(dst any) error {
case *[]byte:
*vv = xstring.ToBytes(string(v))

return nil
case json.Unmarshaler:
Copy link
Member

Choose a reason for hiding this comment

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

Не хватает тестов на эти типы - хотя бы для того, чтобы в будущем не сломать эту функциональность

err := vv.UnmarshalJSON(xstring.ToBytes(string(v)))
if err != nil {
return xerrors.WithStackTrace(fmt.Errorf(
"%w '%s(%+v)' to '%T' destination: %w",
ErrCannotCast, v.Type().Yql(), v, vv, err,
))
}

return nil
default:
return xerrors.WithStackTrace(fmt.Errorf(
Expand Down Expand Up @@ -1413,6 +1424,16 @@ func (v jsonDocumentValue) castTo(dst any) error {
case *[]byte:
*vv = xstring.ToBytes(string(v))

return nil
case json.Unmarshaler:
err := vv.UnmarshalJSON(xstring.ToBytes(string(v)))
if err != nil {
return xerrors.WithStackTrace(fmt.Errorf(
"%w '%s(%+v)' to '%T' destination: %w",
ErrCannotCast, v.Type().Yql(), v, vv, err,
))
}

return nil
default:
return xerrors.WithStackTrace(fmt.Errorf(
Expand Down
Loading