forked from goadesign/goa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgo_transform_union_test.go
195 lines (176 loc) · 5.42 KB
/
go_transform_union_test.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
186
187
188
189
190
191
192
193
194
195
package codegen
import (
"testing"
"goa.design/goa/v3/codegen/testdata"
"goa.design/goa/v3/expr"
)
func TestGoTransformUnion(t *testing.T) {
root := RunDSL(t, testdata.TestUnionDSL)
var (
scope = NewNameScope()
// types to test
unionString = root.UserType("Container").Attribute().Find("UnionString").Find("UnionString")
unionString2 = root.UserType("Container").Attribute().Find("UnionString2").Find("UnionString2")
unionStringInt = root.UserType("Container").Attribute().Find("UnionStringInt").Find("UnionStringInt")
unionStringInt2 = root.UserType("Container").Attribute().Find("UnionStringInt2").Find("UnionStringInt2")
unionSomeType = root.UserType("Container").Attribute().Find("UnionSomeType").Find("UnionSomeType")
userType = &expr.AttributeExpr{Type: root.UserType("UnionUserType")}
defaultCtx = NewAttributeContext(false, false, true, "", scope)
)
tc := []struct {
Name string
Source *expr.AttributeExpr
Target *expr.AttributeExpr
Expected string
}{
{"UnionString to UnionString2", unionString, unionString2, unionToUnionCode},
{"UnionStringInt to UnionStringInt2", unionStringInt, unionStringInt2, unionMultiToUnionMultiCode},
{"UnionString to User Type", unionString, userType, unionStringToUserTypeCode},
{"UnionStringInt to User Type", unionStringInt, userType, unionStringIntToUserTypeCode},
{"UnionSomeType to User Type", unionSomeType, userType, unionSomeTypeToUserTypeCode},
{"User Type to UnionString", userType, unionString, userTypeToUnionStringCode},
{"User Type to UnionStringInt", userType, unionStringInt, userTypeToUnionStringIntCode},
{"User Type to UnionSomeType", userType, unionSomeType, userTypeToUnionSomeTypeCode},
}
for _, c := range tc {
t.Run(c.Name, func(t *testing.T) {
code, _, err := GoTransform(c.Source, c.Target, "source", "target", defaultCtx, defaultCtx, "", true)
if err != nil {
t.Errorf("unexpected error %s", err)
return
}
code = FormatTestCode(t, "package foo\nfunc transform(){\n"+code+"}")
if code != c.Expected {
t.Errorf("invalid code, got:\n%s\ngot vs. expected:\n%s", code, Diff(t, code, c.Expected))
}
})
}
}
func TestGoTransformUnionError(t *testing.T) {
root := RunDSL(t, testdata.TestUnionDSL)
var (
scope = NewNameScope()
// types to test
unionString = root.UserType("Container").Attribute().Find("UnionString").Find("UnionString")
unionStringInt = root.UserType("Container").Attribute().Find("UnionStringInt").Find("UnionStringInt")
unionSomeType = root.UserType("Container").Attribute().Find("UnionSomeType").Find("UnionSomeType")
defaultCtx = NewAttributeContext(false, false, true, "", scope)
)
tc := []struct {
Name string
Source *expr.AttributeExpr
Target *expr.AttributeExpr
Error string
}{
{"UnionString to UnionStringInt", unionString, unionStringInt, "cannot transform union: number of union types differ (UnionString has 1, UnionStringInt has 2)"},
{"UnionString to UnionSomeType", unionString, unionSomeType, "cannot transform union UnionString to UnionSomeType: type at index 0: source is a string but target type is object"},
}
for _, c := range tc {
t.Run(c.Name, func(t *testing.T) {
_, _, err := GoTransform(c.Source, c.Target, "source", "target", defaultCtx, defaultCtx, "", true)
if err == nil {
t.Errorf("unexpected success")
return
}
if err.Error() != c.Error {
t.Errorf("unexpected error, got: %s, expected: %s", err, c.Error)
}
})
}
}
const unionToUnionCode = `func transform() {
var target *UnionString2
switch actual := source.(type) {
case UnionStringString:
target = UnionString2String(actual)
}
}
`
const unionMultiToUnionMultiCode = `func transform() {
var target *UnionStringInt2
switch actual := source.(type) {
case UnionStringIntString:
target = UnionStringInt2String(actual)
case UnionStringIntInt:
target = UnionStringInt2Int(actual)
}
}
`
const unionStringToUserTypeCode = `func transform() {
var target *UnionUserType
js, _ := json.Marshal(source)
var name string
switch source.(type) {
case UnionStringString:
name = "String"
}
target = &UnionUserType{
Type: name,
Value: string(js),
}
}
`
const unionStringIntToUserTypeCode = `func transform() {
var target *UnionUserType
js, _ := json.Marshal(source)
var name string
switch source.(type) {
case UnionStringIntString:
name = "String"
case UnionStringIntInt:
name = "Int"
}
target = &UnionUserType{
Type: name,
Value: string(js),
}
}
`
const unionSomeTypeToUserTypeCode = `func transform() {
var target *UnionUserType
js, _ := json.Marshal(source)
var name string
switch source.(type) {
case *SomeType:
name = "SomeType"
}
target = &UnionUserType{
Type: name,
Value: string(js),
}
}
`
const userTypeToUnionStringCode = `func transform() {
var target *UnionString
switch source.Type {
case "String":
var val UnionStringString
json.Unmarshal([]byte(source.Value), &val)
target = val
}
}
`
const userTypeToUnionStringIntCode = `func transform() {
var target *UnionStringInt
switch source.Type {
case "String":
var val UnionStringIntString
json.Unmarshal([]byte(source.Value), &val)
target = val
case "Int":
var val UnionStringIntInt
json.Unmarshal([]byte(source.Value), &val)
target = val
}
}
`
const userTypeToUnionSomeTypeCode = `func transform() {
var target *UnionSomeType
switch source.Type {
case "SomeType":
var val *SomeType
json.Unmarshal([]byte(source.Value), &val)
target = val
}
}
`