Skip to content

Commit

Permalink
fix interface
Browse files Browse the repository at this point in the history
  • Loading branch information
goccy committed Nov 28, 2022
1 parent c604ca9 commit b55a676
Show file tree
Hide file tree
Showing 27 changed files with 881 additions and 209 deletions.
2 changes: 1 addition & 1 deletion benchmarks/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func Benchmark_Decode_SmallStruct_UnmarshalPath_GoJson(b *testing.B) {
path, err := gojson.PathString("st").Build()
path, err := gojson.CreatePath("$.st")
if err != nil {
b.Fatal(err)
}
Expand Down
30 changes: 9 additions & 21 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,21 +83,14 @@ func unmarshalContext(ctx context.Context, data []byte, v interface{}, optFuncs
return validateEndBuf(src, cursor)
}

func unmarshalPath(path *Path, data []byte, v interface{}, optFuncs ...DecodeOptionFunc) error {
var (
pathDecoder = decoder.NewPathDecoder()
)

func extractFromPath(path *Path, data []byte, optFuncs ...DecodeOptionFunc) ([][]byte, error) {
src := make([]byte, len(data)+1) // append nul byte to the end
copy(src, data)

header := (*emptyInterface)(unsafe.Pointer(&v))
typ := header.typ
typeptr := uintptr(unsafe.Pointer(typ))
if err := validateType(*(**runtime.Type)(unsafe.Pointer(&typeptr)), uintptr(header.ptr)); err != nil {
return err
}

dec, err := decoder.CompileToGetDecoder(runtime.PtrTo(decoder.EmptyInterfaceType))
if err != nil {
return err
}
ctx := decoder.TakeRuntimeContext()
ctx.Buf = src
ctx.Option.Flags = 0
Expand All @@ -106,21 +99,16 @@ func unmarshalPath(path *Path, data []byte, v interface{}, optFuncs ...DecodeOpt
for _, optFunc := range optFuncs {
optFunc(ctx.Option)
}
ptr := decoder.UnsafeNew(decoder.EmptyInterfaceType)
cursor, err := dec.Decode(ctx, 0, 0, ptr)
paths, cursor, err := pathDecoder.DecodePath(ctx, 0, 0)
if err != nil {
decoder.ReleaseRuntimeContext(ctx)
return err
return nil, err
}
decoder.ReleaseRuntimeContext(ctx)
if err := validateEndBuf(src, cursor); err != nil {
return err
return nil, err
}

return decoder.AssignValue(v, *(*interface{})(unsafe.Pointer(&emptyInterface{
typ: decoder.EmptyInterfaceType,
ptr: ptr,
})))
return paths, nil
}

func unmarshalNoEscape(data []byte, v interface{}, optFuncs ...DecodeOptionFunc) error {
Expand Down
4 changes: 4 additions & 0 deletions internal/decoder/anonymous_field.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@ func (d *anonymousFieldDecoder) Decode(ctx *RuntimeContext, cursor, depth int64,
p = *(*unsafe.Pointer)(p)
return d.dec.Decode(ctx, cursor, depth, unsafe.Pointer(uintptr(p)+d.offset))
}

func (d *anonymousFieldDecoder) DecodePath(ctx *RuntimeContext, cursor, depth int64) ([][]byte, int64, error) {
return d.dec.DecodePath(ctx, cursor, depth)
}
5 changes: 5 additions & 0 deletions internal/decoder/array.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package decoder

import (
"fmt"
"unsafe"

"github.com/goccy/go-json/internal/errors"
Expand Down Expand Up @@ -167,3 +168,7 @@ func (d *arrayDecoder) Decode(ctx *RuntimeContext, cursor, depth int64, p unsafe
}
}
}

func (d *arrayDecoder) DecodePath(ctx *RuntimeContext, cursor, depth int64) ([][]byte, int64, error) {
return nil, 0, fmt.Errorf("json: array decoder does not support decode path")
}

0 comments on commit b55a676

Please sign in to comment.