-
Notifications
You must be signed in to change notification settings - Fork 12
/
parse.go
58 lines (51 loc) · 1.27 KB
/
parse.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package variable
import (
"encoding/json"
"github.com/eolinker/eosc"
"reflect"
)
func NewParse(variables eosc.IVariable) *Parse {
return &Parse{variables: variables}
}
type Parse struct {
variables eosc.IVariable
}
func (p *Parse) Unmarshal(buf []byte, typ reflect.Type) (interface{}, []string, error) {
o := newOrg(typ, p.variables)
err := json.Unmarshal(buf, o)
return o.target, o.usedVariable, err
}
type org struct {
typ reflect.Type
variables eosc.IVariable
target interface{}
usedVariable []string
}
func newOrg(typ reflect.Type, variables eosc.IVariable) *org {
return &org{typ: typ, variables: variables}
}
func (o *org) UnmarshalJSON(bytes []byte) error {
var origin interface{}
err := json.Unmarshal(bytes, &origin)
if err != nil {
return err
}
target := reflect.New(o.typ)
variables, err := recurseReflect(reflect.ValueOf(origin), target, o.variables)
if err != nil {
return err
}
o.target = target.Elem().Interface()
variableMap := make(map[string]bool)
for _, v := range variables {
if _, ok := variableMap[v]; !ok {
variableMap[v] = true
}
}
usedVariables := make([]string, 0, len(variableMap))
for key := range variableMap {
usedVariables = append(usedVariables, key)
}
o.usedVariable = usedVariables
return nil
}