-
Notifications
You must be signed in to change notification settings - Fork 18
/
typeshape.go
288 lines (256 loc) · 7.45 KB
/
typeshape.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// Copyright 2022, Pulumi Corporation. All rights reserved.
package java
import (
"encoding/json"
"fmt"
"strings"
"github.com/pulumi/pulumi-java/pkg/codegen/java/names"
)
type TypeShape struct {
Type names.FQN
Parameters []TypeShape
Annotations []string
}
type TypeShapeStringOptions struct {
// useful for type parameters because (most) annotations are not allowed on a generic parameter type
CommentOutAnnotations bool
// useful for annotation parameters and other contexts where comments or annotations are not allowed
SkipAnnotations bool
// useful for .class or [].class to skip the generic part of the type
GenericErasure bool
// useful to append .class
AppendClassLiteral bool
// useful to append [].class
AppendClassArrayLiteral bool
}
func (ts TypeShape) Equal(other TypeShape) bool {
if !ts.Type.Equal(other.Type) {
return false
}
if len(ts.Parameters) != len(other.Parameters) {
return false
}
for i := range ts.Parameters {
if !ts.Parameters[i].Equal(other.Parameters[i]) {
return false
}
}
if len(ts.Annotations) != len(other.Annotations) {
return false
}
for i := range ts.Annotations {
if ts.Annotations[i] != other.Annotations[i] {
return false
}
}
return true
}
func (ts TypeShape) WithoutAnnotations() TypeShape {
return TypeShape{Type: ts.Type, Parameters: ts.Parameters}
}
// ToCode converts to Java code, may add imports to use short names.
func (ts TypeShape) ToCode(imports *names.Imports) string {
return ts.ToCodeWithOptions(imports, TypeShapeStringOptions{})
}
// ToCodeErased converts to Java code with erased generics, may add imports to use short names.
func (ts TypeShape) ToCodeErased(imports *names.Imports) string {
return ts.ToCodeWithOptions(imports, TypeShapeStringOptions{
GenericErasure: true,
SkipAnnotations: true,
})
}
// ToCodeClassLiteral converts to Java class literal, may add imports to use short names.
func (ts TypeShape) ToCodeClassLiteral(imports *names.Imports) string {
return ts.ToCodeWithOptions(imports, TypeShapeStringOptions{
GenericErasure: true,
SkipAnnotations: true,
AppendClassLiteral: true,
})
}
func (ts TypeShape) ToCodeCommentedAnnotations(imports *names.Imports) string {
return ts.ToCodeWithOptions(imports, TypeShapeStringOptions{
CommentOutAnnotations: true,
})
}
func (ts TypeShape) ToCodeWithOptions(imports *names.Imports, opts TypeShapeStringOptions) string {
// guard against unexpected output, should not happen outside of tests
if ts.Type.String() == "" {
return ""
}
var annotationsString string
if !opts.SkipAnnotations {
annotationsString = strings.Join(ts.Annotations, " ")
if len(ts.Annotations) > 0 {
if opts.CommentOutAnnotations {
annotationsString = "/* " + annotationsString + " */"
}
annotationsString = annotationsString + " "
}
}
var parametersString string
if !opts.GenericErasure && len(ts.Parameters) > 0 {
parametersStrings := make([]string, len(ts.Parameters))
for i, parameter := range ts.Parameters {
parameterString := parameter.ToCodeWithOptions(imports, TypeShapeStringOptions{CommentOutAnnotations: true})
if len(parameterString) > 0 {
parametersStrings[i] = parameterString
}
}
if len(parametersStrings) > 0 {
parametersString = "<" + strings.Join(parametersStrings, ",") + ">"
}
}
var classLiteral string
if opts.AppendClassLiteral {
classLiteral = ".class"
}
if opts.AppendClassArrayLiteral {
classLiteral = "[].class"
}
return fmt.Sprintf("%s%s%s%s", annotationsString, imports.Ref(ts.Type), parametersString, classLiteral)
}
func (ts TypeShape) ToTree(imports *names.Imports) (string, string) {
refs := []string{}
refs, tree := ts.toTreeInternal(refs, imports)
treeJSON, err := json.Marshal(tree)
if err != nil {
panic(err)
}
return strings.Join(refs, ","), string(treeJSON)
}
func (ts TypeShape) toTreeInternal(refs []string, imports *names.Imports) ([]string, []interface{}) {
refIndex := func(classLiteral string) (int, bool) {
return sliceIndex(len(refs), func(i int) bool {
return refs[i] == classLiteral
})
}
requireRefIndex := func(classLiteral string) int {
classRef, ok := refIndex(classLiteral)
if !ok {
panic(fmt.Errorf(
"expected a class reference index for a class literal: '%s'", classLiteral,
))
}
return classRef
}
rootClassLiteral := ts.ToCodeClassLiteral(imports)
// guard against unexpected output, should not happen outside of tests
if rootClassLiteral == "" {
return []string{}, []interface{}{}
}
_, ok := refIndex(rootClassLiteral)
if !ok {
refs = append(refs, rootClassLiteral)
}
classRef := requireRefIndex(rootClassLiteral)
tree := []interface{}{classRef}
for _, param := range ts.Parameters {
subRefs, subTree := param.toTreeInternal(refs, imports)
refs = subRefs
if len(subTree) == 1 {
tree = append(tree, subTree[0])
} else {
tree = append(tree, subTree)
}
}
return refs, tree
}
func sliceIndex(limit int, predicate func(i int) bool) (int, bool) {
for i := 0; i < limit; i++ {
if predicate(i) {
return i, true
}
}
return -1, false
}
func (ts TypeShape) ParameterTypes(imports *names.Imports) []string {
return ts.ParameterTypesTransformed(func(ts TypeShape) string {
return imports.Ref(ts.Type)
})
}
func (ts TypeShape) ParameterTypesTransformed(f func(TypeShape) string) []string {
parameterTypes := make([]string, len(ts.Parameters))
for i, v := range ts.Parameters {
parameterTypes[i] = f(v)
}
return parameterTypes
}
func (ts TypeShape) ListType(ctx *classFileContext) string {
if ts.Type.String() == names.List.String() {
for _, a := range ts.Parameters {
if strings.HasPrefix(a.Type.String(), names.JavaUtil.Dot("").String()) {
continue
}
return a.ToCode(ctx.imports)
}
}
return ""
}
// StringJavaTypeShape returns a Java code representation.
func (ts TypeShape) StringJavaTypeShape(imports *names.Imports) string {
shape := fmt.Sprintf("%s.<%s>builder(%s)",
imports.Ref(names.TypeShape),
ts.ToCodeCommentedAnnotations(imports),
ts.ToCodeClassLiteral(imports),
)
for _, parameter := range ts.Parameters {
if len(parameter.Parameters) > 0 {
shape += fmt.Sprintf(".addParameter(%s)", parameter.StringJavaTypeShape(imports))
} else {
shape += fmt.Sprintf(".addParameter(%s)", parameter.ToCodeClassLiteral(imports))
}
}
shape += ".build()"
return shape
}
func (ts TypeShape) UnOutput() (bool, TypeShape) {
if ts.Type.Equal(names.Output) {
return true, ts.Parameters[0]
}
return false, ts
}
func (ts TypeShape) UnList() (bool, TypeShape) {
if ts.Type.Equal(names.List) {
return true, ts.Parameters[0]
}
return false, ts
}
func (ts TypeShape) Optional() TypeShape {
return TypeShape{
Type: names.Optional,
Parameters: []TypeShape{ts},
}
}
func (ts TypeShape) Output() TypeShape {
return TypeShape{
Type: names.Output,
Parameters: []TypeShape{ts},
}
}
func (ts TypeShape) UnOptional() (bool, TypeShape) {
if ts.Type.Equal(names.Optional) {
return true, ts.Parameters[0]
}
return false, ts
}
func (ts TypeShape) UnNullable() (bool, TypeShape) {
annotations := []string{}
isNullable := false
for _, a := range ts.Annotations {
if strings.Contains(a, "Nullable") {
isNullable = true
} else {
annotations = append(annotations, a)
}
}
if isNullable {
return true, TypeShape{ts.Type, ts.Parameters, annotations}
}
return false, ts
}
func (ts TypeShape) UnEither() (bool, TypeShape, TypeShape) {
if ts.Type.Equal(names.Either) {
return true, ts.Parameters[0], ts.Parameters[1]
}
return false, TypeShape{}, TypeShape{}
}