...
1 package json
2
3 import (
4 "reflect"
5
6 "github.com/goccy/go-json/internal/decoder"
7 )
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 func CreatePath(p string) (*Path, error) {
24 path, err := decoder.PathString(p).Build()
25 if err != nil {
26 return nil, err
27 }
28 return &Path{path: path}, nil
29 }
30
31
32 type Path struct {
33 path *decoder.Path
34 }
35
36
37 func (p *Path) RootSelectorOnly() bool {
38 return p.path.RootSelectorOnly
39 }
40
41
42 func (p *Path) UsedSingleQuotePathSelector() bool {
43 return p.path.SingleQuotePathSelector
44 }
45
46
47 func (p *Path) UsedDoubleQuotePathSelector() bool {
48 return p.path.DoubleQuotePathSelector
49 }
50
51
52 func (p *Path) Extract(data []byte, optFuncs ...DecodeOptionFunc) ([][]byte, error) {
53 return extractFromPath(p, data, optFuncs...)
54 }
55
56
57 func (p *Path) PathString() string {
58 return p.path.String()
59 }
60
61
62 func (p *Path) Unmarshal(data []byte, v interface{}, optFuncs ...DecodeOptionFunc) error {
63 contents, err := extractFromPath(p, data, optFuncs...)
64 if err != nil {
65 return err
66 }
67 results := make([]interface{}, 0, len(contents))
68 for _, content := range contents {
69 var result interface{}
70 if err := Unmarshal(content, &result); err != nil {
71 return err
72 }
73 results = append(results, result)
74 }
75 if err := decoder.AssignValue(reflect.ValueOf(results), reflect.ValueOf(v)); err != nil {
76 return err
77 }
78 return nil
79 }
80
81
82 func (p *Path) Get(src, dst interface{}) error {
83 return p.path.Get(reflect.ValueOf(src), reflect.ValueOf(dst))
84 }
85
View as plain text