Skip to content
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

Add "func" Decoder #257

Merged
merged 8 commits into from Jun 24, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 12 additions & 2 deletions decode_test.go
Expand Up @@ -152,6 +152,7 @@ func Test_Decoder(t *testing.T) {
B string `json:"str"`
C bool
D *T
E func()
}
content := []byte(`
{
Expand All @@ -162,7 +163,8 @@ func Test_Decoder(t *testing.T) {
"aa": 2,
"bb": "world",
"cc": true
}
},
"e" : null
}`)
assertErr(t, json.Unmarshal(content, &v))
assertEq(t, "struct.A", 123, v.A)
Expand All @@ -171,6 +173,7 @@ func Test_Decoder(t *testing.T) {
assertEq(t, "struct.D.AA", 2, v.D.AA)
assertEq(t, "struct.D.BB", "world", v.D.BB)
assertEq(t, "struct.D.CC", true, v.D.CC)
assertEq(t, "struct.E", nil, v.E)
t.Run("struct.field null", func(t *testing.T) {
var v struct {
A string
Expand All @@ -179,8 +182,9 @@ func Test_Decoder(t *testing.T) {
D map[string]interface{}
E [2]string
F interface{}
G func()
}
assertErr(t, json.Unmarshal([]byte(`{"a":null,"b":null,"c":null,"d":null,"e":null,"f":null}`), &v))
assertErr(t, json.Unmarshal([]byte(`{"a":null,"b":null,"c":null,"d":null,"e":null,"f":null,"g":null}`), &v))
assertEq(t, "string", v.A, "")
assertNeq(t, "[]string", v.B, nil)
assertEq(t, "[]string", len(v.B), 0)
Expand All @@ -191,6 +195,7 @@ func Test_Decoder(t *testing.T) {
assertNeq(t, "array", v.E, nil)
assertEq(t, "array", len(v.E), 2)
assertEq(t, "interface{}", v.F, nil)
assertEq(t, "func", v.G, nil)
})
})
t.Run("interface", func(t *testing.T) {
Expand Down Expand Up @@ -239,6 +244,11 @@ func Test_Decoder(t *testing.T) {
assertEq(t, "interface", nil, v)
})
})
t.Run("func", func(t *testing.T) {
var v func()
assertErr(t, json.Unmarshal([]byte(`null`), &v))
assertEq(t, "func", nil, v)
})
}

func TestIssue98(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion helper_test.go
Expand Up @@ -11,7 +11,7 @@ func assertErr(t *testing.T, err error) {

func assertEq(t *testing.T, msg string, exp interface{}, act interface{}) {
t.Helper()
if exp != act {
if exp != act && exp != nil && act != nil {
Kiraub marked this conversation as resolved.
Show resolved Hide resolved
t.Fatalf("failed to test for %s. exp=[%v] but act=[%v]", msg, exp, act)
}
}
Expand Down
10 changes: 10 additions & 0 deletions internal/decoder/compile.go
Expand Up @@ -123,11 +123,15 @@ func compile(typ *runtime.Type, structName, fieldName string, structTypeToDecode
return compileFloat32(structName, fieldName)
case reflect.Float64:
return compileFloat64(structName, fieldName)
case reflect.Func:
return compileFunc(typ, structName, fieldName)
}
return nil, &errors.UnmarshalTypeError{
Value: "object",
Type: runtime.RType2Type(typ),
Offset: 0,
Struct: structName,
Field: fieldName,
}
}

Expand Down Expand Up @@ -178,6 +182,8 @@ ERROR:
Value: "object",
Type: runtime.RType2Type(typ),
Offset: 0,
Struct: structName,
Field: fieldName,
}
}

Expand Down Expand Up @@ -312,6 +318,10 @@ func compileInterface(typ *runtime.Type, structName, fieldName string) (Decoder,
return newInterfaceDecoder(typ, structName, fieldName), nil
}

func compileFunc(typ *runtime.Type, strutName, fieldName string) (Decoder, error) {
return newFuncDecoder(typ, strutName, fieldName), nil
}

func removeConflictFields(fieldMap map[string]*structFieldSet, conflictedMap map[string]struct{}, dec *structDecoder, field reflect.StructField) {
for k, v := range dec.fieldMap {
if _, exists := conflictedMap[k]; exists {
Expand Down
108 changes: 108 additions & 0 deletions internal/decoder/func.go
@@ -0,0 +1,108 @@
package decoder

import (
"bytes"
"unsafe"

"github.com/goccy/go-json/internal/errors"
"github.com/goccy/go-json/internal/runtime"
)

type funcDecoder struct {
typ *runtime.Type
structName string
fieldName string
}

func newFuncDecoder(typ *runtime.Type, structName, fieldName string) *funcDecoder {
fnDecoder := &funcDecoder{typ, structName, fieldName}
return fnDecoder
}

func (d *funcDecoder) DecodeStream(s *Stream, depth int64, p unsafe.Pointer) error {
s.skipWhiteSpace()
start := s.cursor
if err := s.skipValue(depth); err != nil {
return err
}
src := s.buf[start:s.cursor]
if len(src) > 0 {
switch src[0] {
Copy link
Owner

Choose a reason for hiding this comment

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

It seems better to be able to make an error even if the values ​​are different (e.g. true or false or other characters).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've added the cases for true and false.

The stdjson returns an error invalid character 'a' looking for beginning of value. This could be added to internal/errors/errors.go for other unexpected characters.

case '"':
return &errors.UnmarshalTypeError{
Value: "string",
Type: runtime.RType2Type(d.typ),
Offset: s.totalOffset(),
}
case '[':
return &errors.UnmarshalTypeError{
Value: "array",
Type: runtime.RType2Type(d.typ),
Offset: s.totalOffset(),
}
case '{':
return &errors.UnmarshalTypeError{
Value: "object",
Type: runtime.RType2Type(d.typ),
Offset: s.totalOffset(),
}
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
return &errors.UnmarshalTypeError{
Value: "number",
Type: runtime.RType2Type(d.typ),
Offset: s.totalOffset(),
}
case 'n':
if bytes.Equal(src, nullbytes) {
*(*unsafe.Pointer)(p) = nil
return nil
}
}
}
return errors.ErrNotAtBeginningOfValue(start)
}

func (d *funcDecoder) Decode(ctx *RuntimeContext, cursor, depth int64, p unsafe.Pointer) (int64, error) {
buf := ctx.Buf
cursor = skipWhiteSpace(buf, cursor)
start := cursor
end, err := skipValue(buf, cursor, depth)
if err != nil {
return 0, err
}
src := buf[start:end]
if len(src) > 0 {
switch src[0] {
case '"':
return 0, &errors.UnmarshalTypeError{
Value: "string",
Type: runtime.RType2Type(d.typ),
Offset: start,
}
case '[':
return 0, &errors.UnmarshalTypeError{
Value: "array",
Type: runtime.RType2Type(d.typ),
Offset: start,
}
case '{':
return 0, &errors.UnmarshalTypeError{
Value: "object",
Type: runtime.RType2Type(d.typ),
Offset: start,
}
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
return 0, &errors.UnmarshalTypeError{
Value: "number",
Type: runtime.RType2Type(d.typ),
Offset: start,
}
case 'n':
if bytes.Equal(src, nullbytes) {
*(*unsafe.Pointer)(p) = nil
return end, nil
}
}
}
return 0, errors.ErrNotAtBeginningOfValue(start)
}