-
Notifications
You must be signed in to change notification settings - Fork 242
/
go-type-reference.ts
231 lines (199 loc) · 6.03 KB
/
go-type-reference.ts
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
import { TypeReference } from 'jsii-reflect';
import * as log from '../../../logging';
import { SpecialDependencies } from '../dependencies';
import { Package } from '../package';
import { GoType } from './go-type';
/*
* Maps names of JS primitives to corresponding Go types as strings
*/
class PrimitiveMapper {
private readonly MAP: { [key: string]: string } = {
number: 'float64',
boolean: 'bool',
any: 'interface{}',
date: 'time.Time',
string: 'string',
json: `map[string]interface{}`,
};
public constructor(private readonly name: string) {}
public get goPrimitive(): string {
const val = this.MAP[this.name];
if (!val) {
log.debug(`Unmapped primitive type: ${this.name}`);
}
return val ?? this.name;
}
}
/**
* TypeMap used to recursively resolve interfaces in nested types for use in
* resolving scoped type names and implementation maps.
*/
type TypeMap =
| { readonly type: 'primitive'; readonly value: string }
| { readonly type: 'array'; readonly value: GoTypeRef }
| { readonly type: 'map'; readonly value: GoTypeRef }
| { readonly type: 'union'; readonly value: readonly GoTypeRef[] }
| { readonly type: 'interface'; readonly value: GoTypeRef }
| { readonly type: 'void' };
/*
* Accepts a JSII TypeReference and Go Package and can resolve the GoType within the module tree.
*/
export class GoTypeRef {
private _typeMap?: TypeMap;
public constructor(
public readonly root: Package,
public readonly reference: TypeReference,
) {}
public get type(): GoType | undefined {
if (this.reference.fqn) {
return this.root.findType(this.reference.fqn);
}
return undefined;
}
public get specialDependencies(): SpecialDependencies {
return {
runtime: false,
init: false,
internal: false,
time: containsDate(this.reference),
};
function containsDate(ref: TypeReference): boolean {
if (ref.primitive === 'date') {
return true;
}
if (ref.arrayOfType) {
return containsDate(ref.arrayOfType);
}
if (ref.mapOfType) {
return containsDate(ref.mapOfType);
}
if (ref.unionOfTypes) {
return ref.unionOfTypes.some(containsDate);
}
return false;
}
}
public get primitiveType() {
if (this.reference.primitive) {
return new PrimitiveMapper(this.reference.primitive).goPrimitive;
}
return undefined;
}
public get name() {
return this.type?.name;
}
public get datatype() {
const reflectType = this.type?.type;
return reflectType?.isInterfaceType() && reflectType.datatype;
}
public get namespace() {
return this.type?.namespace;
}
public get void() {
return this.reference.void;
}
public get typeMap(): TypeMap {
if (!this._typeMap) {
this._typeMap = this.buildTypeMap(this);
}
return this._typeMap;
}
/**
* The go `import`s required in order to be able to use this type in code.
*/
public get dependencies(): readonly Package[] {
const ret = new Array<Package>();
switch (this.typeMap.type) {
case 'interface':
if (this.type?.pkg) {
ret.push(this.type.pkg);
}
break;
case 'array':
case 'map':
ret.push(...this.typeMap.value.dependencies);
break;
case 'union':
for (const t of this.typeMap.value) {
ret.push(...t.dependencies);
}
break;
case 'void':
case 'primitive':
break;
}
return ret;
}
/*
* Return the name of a type for reference from the `Package` passed in
*/
public scopedName(scope: Package): string {
return this.scopedTypeName(this.typeMap, scope);
}
public scopedReference(scope: Package): string {
return this.scopedTypeName(this.typeMap, scope, true);
}
private buildTypeMap(ref: GoTypeRef): TypeMap {
if (ref.primitiveType) {
return { type: 'primitive', value: ref.primitiveType };
} else if (ref.reference.arrayOfType) {
return {
type: 'array',
value: new GoTypeRef(this.root, ref.reference.arrayOfType),
};
} else if (ref.reference.mapOfType) {
return {
type: 'map',
value: new GoTypeRef(this.root, ref.reference.mapOfType),
};
} else if (ref.reference.unionOfTypes) {
return {
type: 'union',
value: ref.reference.unionOfTypes.map(
(typeRef) => new GoTypeRef(this.root, typeRef),
),
};
} else if (ref.reference.void) {
return { type: 'void' };
}
return { type: 'interface', value: ref };
}
public scopedTypeName(
typeMap: TypeMap,
scope: Package,
asRef = false,
): string {
if (typeMap.type === 'primitive') {
const { value } = typeMap;
const prefix = asRef && value !== 'interface{}' ? '*' : '';
return `${prefix}${value}`;
} else if (typeMap.type === 'array' || typeMap.type === 'map') {
const prefix = asRef ? '*' : '';
const wrapper = typeMap.type === 'array' ? '[]' : 'map[string]';
const innerName =
this.scopedTypeName(typeMap.value.typeMap, scope, asRef) ??
'interface{}';
return `${prefix}${wrapper}${innerName}`;
} else if (typeMap.type === 'interface') {
const prefix = asRef && typeMap.value.datatype ? '*' : '';
const baseName = typeMap.value.name;
// type is defined in the same scope as the current one, no namespace required
if (scope.packageName === typeMap.value.namespace && baseName) {
// if the current scope is the same as the types scope, return without a namespace
return `${prefix}${baseName}`;
}
// type is defined in another module and requires a namespace and import
if (baseName) {
return `${prefix}${typeMap.value.namespace}.${baseName}`;
}
} else if (typeMap.type === 'union') {
return 'interface{}';
} else if (typeMap.type === 'void') {
return '';
}
// type isn't handled
throw new Error(
`Type ${typeMap.value?.name} does not resolve to a known Go type.`,
);
}
}