-
Notifications
You must be signed in to change notification settings - Fork 4
/
or.go
166 lines (145 loc) · 3.74 KB
/
or.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
package types
import (
"errors"
"fmt"
"github.com/dave/jennifer/jen"
"github.com/dipdup-net/go-lib/tzkt/api"
"github.com/iancoleman/strcase"
)
// Object -
type Or struct{}
// AsField -
func (Or) AsField(name, path string, schema api.JSONSchema, isRequired bool, result *ContractTypeResult) (jen.Code, error) {
tags := map[string]string{
"json": name,
}
if isRequired {
tags["validate"] = TagRequired
}
return jen.Id(fieldName("Or", name)).Add(jen.Id(result.GetName("Or", name))).Tag(tags), nil
}
// AsCode -
func (obj Or) AsCode(name, path string, schema api.JSONSchema, result *ContractTypeResult) (Code, error) {
code, err := obj.createOr(name, path, schema, result)
if err != nil {
return code, err
}
return Code{
Statement: jen.Comment(code.Name).Line().Type().Id(code.Name).Add(code.Statement),
Name: code.Name,
}, nil
}
// AsType -
func (obj Or) AsType(name, path string, schema api.JSONSchema, result *ContractTypeResult) (Code, error) {
return obj.createOr(name, path, schema, result)
}
func (obj Or) createOr(name, path string, schema api.JSONSchema, result *ContractTypeResult) (Code, error) {
if len(schema.OneOf) != 2 {
return Code{}, errors.New("invalid oneOf key for or type")
}
leftField, leftStatement, err := obj.createSide(name, "left", path, schema.OneOf[0], result)
if err != nil {
return Code{}, err
}
rightField, rightStatement, err := obj.createSide(name, "right", path, schema.OneOf[1], result)
if err != nil {
return Code{}, err
}
typName := result.GetName("Or", name)
unmarshalJSON := obj.getUnmarshalJSON(typName)
marshalJSON := obj.getMarshalJSON(typName)
statement := jen.Struct(
leftField,
rightField,
).Line().Line().Add(unmarshalJSON).Line().Add(marshalJSON).Line()
if leftStatement != nil {
statement.Add(leftStatement)
}
if rightStatement != nil {
statement.Add(rightStatement)
}
return Code{
Statement: statement,
Name: typName,
}, nil
}
func (obj Or) createSide(name, side, path string, schema api.JSONSchema, result *ContractTypeResult) (jen.Code, jen.Code, error) {
typ, err := selectType(schema)
if err != nil {
return nil, nil, err
}
if isSimpleType(schema.Comment) {
code, err := typ.AsField(side, path, schema, false, result)
return code, nil, err
}
sideName := fmt.Sprintf("%s_%s", side, name)
newPath := getPath(path, name)
code, err := typ.AsCode(sideName, newPath, schema, result)
if err != nil {
return nil, nil, err
}
field := jen.Id(strcase.ToCamel(side)).Op("*").Id(code.Name)
return field, code.Statement, nil
}
func (obj Or) getUnmarshalJSON(typName string) jen.Code {
return jen.Comment("UnmarshalJSON").Line().
Func().Params(
jen.Id("or").Op("*").Id(typName),
).Id("UnmarshalJSON").Params(
jen.Id("data").Op("[]").Byte(),
).Error().Block(
jen.If(
jen.Err().Op(":=").Id("json.Unmarshal").
Call(
jen.Id("data"),
jen.Id("or.Left"),
),
jen.Err().Op("!=").Nil(),
).Block(
jen.Return(jen.Nil()),
),
jen.Return(
jen.Id("json.Unmarshal").
Call(
jen.Id("data"),
jen.Id("or.Right"),
),
),
)
}
func (obj Or) getMarshalJSON(typName string) jen.Code {
return jen.Comment("MarshalJSON").Line().
Func().Params(
jen.Id("or").Op("*").Id(typName),
).Id("MarshalJSON").Params().Params(
jen.Op("[]").Byte(),
jen.Error(),
).Block(
jen.If(
jen.Id("or.Left").Op("!=").Nil().Block(
jen.Return(
jen.Id("json.Marshal").
Call(
jen.Id("or.Left"),
),
),
),
),
jen.If(
jen.Id("or.Right").Op("!=").Nil().Block(
jen.Return(
jen.Id("json.Marshal").
Call(
jen.Id("or.Right"),
),
),
),
),
jen.Return(
jen.Nil(),
jen.Qual("errors", "New").Call(
jen.Lit("left and right in or type are not initialized"),
),
),
)
}