-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstct.go
185 lines (159 loc) · 3.44 KB
/
stct.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package main
import (
"bytes"
"encoding/json"
"io"
//"encoding/json"
"fmt"
//"io"
"github.com/go-playground/validator/v10"
)
var EnableDecoderUseNumber = false
// EnableDecoderDisallowUnknownFields is used to call the DisallowUnknownFields method
// on the JSON Decoder instance. DisallowUnknownFields causes the Decoder to
// return an error when the destination is a struct and the input contains object
// keys which do not match any non-ignored, exported fields in the destination.
var EnableDecoderDisallowUnknownFields = false
type A struct {
X, Y int
}
func (a *A) Tes() int {
return 100
}
type AI interface {
Tes() int
}
type B struct {
Z, T int
}
func (b *B) Ig() int {
return 200
}
type BI interface {
Ig() int
}
type CI interface {
AI
BI
}
type C struct {
A
B
O int
}
type D struct {
G CI
}
type Inner struct {
A *float64 `json:"a" validate:"required"`
AA *int `json:"aa" validate:"required"`
}
type Outer struct {
Inner []Inner `json:"inner" validate:"required,dive"`
}
type OOuter struct {
Outer Outer `json:"outer" validate:"required"`
}
func BindJsonBody(body []byte, obj interface{}) error {
return decodeJSON(bytes.NewReader(body), obj)
}
func Validate(obj interface{}) error {
return validate(obj)
}
func decodeJSON(r io.Reader, obj interface{}) error {
decoder := json.NewDecoder(r)
if err := decoder.Decode(obj); err != nil {
return err
}
return validate(obj)
}
func validate(obj interface{}) error {
validate := validator.New()
err := validate.Struct(obj)
if err != nil {
return err
}
return nil
}
//type AppInfo struct {
// AppID string `json:"AppId" validate:"required"`
// AppVer string `json:"AppVer" validate:"required"`
//}
//
//type BaseInfo struct {
// BizId int `json:"bizid" validate:"required"`
// UserId int `json:"userid" validate:"required"`
//}
//
//type FeeRequest struct {
// Data BaseInfo `json:"Data" validate:"required,dive"`
// Info AppInfo `json:"Info" validate:"required"`
//}
//
//var Validator StructValidator = &defaultValidator{}
//
//func validate(obj interface{}) error {
// if Validator == nil {
// return nil
// }
// return Validator.ValidateStruct(obj)
//}
//
//func decodeJSON(r io.Reader, obj interface{}) error {
// decoder := json.NewDecoder(r)
// if EnableDecoderUseNumber {
// decoder.UseNumber()
// }
// if EnableDecoderDisallowUnknownFields {
// decoder.DisallowUnknownFields()
// }
// if err := decoder.Decode(obj); err != nil {
// return err
// }
// return validate(obj)
//}
func main() {
//c := C{
// //A: A{},
// //B: B{},
// O: 300,
//}
//
//d := D{G: &c}
//a := A{1, 2}
//b := B{3, 4}
//var c C
//c.B = new(B)
//c.AA.X = 123
//c.AA.Y = 456
//c.B.Z = 789
//c.B.T = 000
//outName := jsoniter.Config{TagKey: "outname"}.Froze()
//
//v := new(User)
//s := `{"user_id": 111}`
//s1 := []byte(s)
//err := binding.JSON.BindBody(s1, v)
//fmt.Println(err)
//fmt.Printf("%+v", v)
//ret, _ := outName.Marshal(v)
//fmt.Printf("%s", ret)
o := new(OOuter)
s := `{"outer": {"inner": [{"a": 1.22222222222222, "aa": 0}]}}`
body := []byte(s)
//decoder := json.NewDecoder(bytes.NewReader(body))
//err := decoder.Decode(o)
//err := binding.JSON.BindBody(body, o)
//s := new(FeeRequest)
//s.Info.AppID=""
//validate := validator.New()
//err = validate.Struct(*o)
err := BindJsonBody(body, o)
if err != nil {
fmt.Println(err)
}
r, err := json.Marshal(o)
//cal := *o.Outer.Inner[0].A / 2
fmt.Printf("data is %s", r)
fmt.Printf("data is %s", *o.Outer.Inner[0].A)
}