-
Notifications
You must be signed in to change notification settings - Fork 27
/
guessstruct.go
152 lines (134 loc) · 3.48 KB
/
guessstruct.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"bytes"
"fmt"
"strconv"
"strings"
"github.com/denkhaus/bitshares/gen/data"
"github.com/denkhaus/bitshares/types"
"github.com/denkhaus/bitshares/util"
"github.com/denkhaus/gojson"
"github.com/juju/errors"
"github.com/pquerna/ffjson/ffjson"
)
var (
// do not change order here
knownTypes = []types.Unmarshalable{
//&types.AccountOptions{},
// &types.Asset{},
&types.AssetAmount{},
&types.AssetFeed{},
// &types.AssetOptions{},
&types.ObjectID{},
//&types.BitAssetDataOptions{},
&types.Authority{},
//&types.Memo{},
&types.Price{},
&types.PriceFeed{},
//&types.Votes{},
&types.Time{},
//&types.PublicKey{},
//&types.Account{},
}
)
func generateOpData(d GenData) error {
samples, err := data.GetSamplesByType(d.Type)
if err != nil {
return errors.Annotate(err, "GetSampleByType")
}
for _, s := range samples {
sample, err := strconv.Unquote(s)
if err != nil {
return errors.Annotate(err, "Unquote")
}
//fmt.Printf("generate struct by sample %+v\n", sample)
buf, err := gojson.GenerateWithTypeGuessing(
strings.NewReader(sample),
gojson.ParseJson, d.Type.OperationName(),
"operations", []string{"json"}, true, true,
guessStructType,
)
if err != nil {
return errors.Annotate(err, "GenerateWithTypeGuessing")
}
fmt.Println("generated struct ", string(buf))
}
return nil
}
func guessStructType(value interface{}, suggestedType string) (string, error) {
//util.Dump("valueToGuess", value)
// util.DumpJSON("suggestedType", suggestedType)
for _, t := range knownTypes {
v, err := ffjson.Marshal(value)
if err != nil {
return "", errors.Annotate(err, "Marshal")
}
//make local copy of known type
typ := t
// util.Dump("data", v)
if err := typ.UnmarshalJSON(v); err == nil {
// util.Dump("compare-1", typ)
// util.Dump("compare-2", value)
switch o := typ.(type) {
case *types.ObjectID:
if value.(string) == o.String() {
return "types.ObjectID", nil
}
case *types.Time:
if value.(string) == o.String() {
return "types.Time", nil
}
// case *types.PublicKey:
// if value.(string) == o.String() {
// return "types.PublicKey", nil
// }
case *types.AccountOptions:
if bytes.Equal(v, util.ToBytes(typ)) {
return "types.AccountOptions", nil
}
case *types.AssetAmount:
if bytes.Equal(v, util.ToBytes(typ)) {
return "types.AssetAmount", nil
}
case *types.Authority:
if bytes.Equal(v, util.ToBytes(typ)) {
return "types.Authority", nil
}
case *types.Memo:
if bytes.Equal(v, util.ToBytes(typ)) {
return "types.Memo", nil
}
// case *types.Votes:
// if bytes.Equal(v, util.ToBytes(typ)) {
// return "types.Votes", nil
// }
case *types.Price:
if bytes.Equal(v, util.ToBytes(typ)) {
return "types.Price", nil
}
case *types.PriceFeed:
if bytes.Equal(v, util.ToBytes(typ)) {
return "types.PriceFeed", nil
}
case *types.AssetFeed:
if bytes.Equal(v, util.ToBytes(typ)) {
return "types.AssetFeed", nil
}
}
// if val1, ok := value.(map[string]interface{}); ok {
// val2 := util.ToMap(typ)
// util.Dump("compare-1", val1)
// util.Dump("compare-2", val2)
// if reflect.DeepEqual(val1, val2) {
// util.Dump("solved", typ)
// return "lala", nil
// }
// }
// name := reflect.ValueOf(typ).Type().Name()
// util.Dump("solved", typ)
// util.Dump("typeName", name)
//o1 := util.ToMap(value)
}
}
return suggestedType, nil
}