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
Add "func" Decoder #257
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
585ce46
add func decoder
d34d796
add func Unmarshal tests
a7d041a
gofmt -s
5f0b342
funcDecoder: handle cases of value being true or false
4fe8e6f
change assertEq check
e94f0cd
Merge branch 'goccy:master' into master
Kiraub 6a81ba1
reverted helper
Kiraub 9d9e5cd
inline nilfunc to nil comparison
Kiraub File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
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] { | ||
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 err := nullBytes(s); err != nil { | ||
return err | ||
} | ||
*(*unsafe.Pointer)(p) = nil | ||
return nil | ||
case 't': | ||
if err := trueBytes(s); err == nil { | ||
return &errors.UnmarshalTypeError{ | ||
Value: "boolean", | ||
Type: runtime.RType2Type(d.typ), | ||
Offset: s.totalOffset(), | ||
} | ||
} | ||
case 'f': | ||
if err := falseBytes(s); err == nil { | ||
return &errors.UnmarshalTypeError{ | ||
Value: "boolean", | ||
Type: runtime.RType2Type(d.typ), | ||
Offset: s.totalOffset(), | ||
} | ||
} | ||
} | ||
} | ||
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 | ||
} | ||
case 't': | ||
if err := validateTrue(buf, start); err == nil { | ||
return 0, &errors.UnmarshalTypeError{ | ||
Value: "boolean", | ||
Type: runtime.RType2Type(d.typ), | ||
Offset: start, | ||
} | ||
} | ||
case 'f': | ||
if err := validateFalse(buf, start); err == nil { | ||
return 0, &errors.UnmarshalTypeError{ | ||
Value: "boolean", | ||
Type: runtime.RType2Type(d.typ), | ||
Offset: start, | ||
} | ||
} | ||
} | ||
} | ||
return 0, errors.ErrNotAtBeginningOfValue(start) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
orfalse
or other characters).There was a problem hiding this comment.
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
andfalse
.The stdjson returns an error
invalid character 'a' looking for beginning of value
. This could be added tointernal/errors/errors.go
for other unexpected characters.