-
Notifications
You must be signed in to change notification settings - Fork 242
/
enum.ts
76 lines (62 loc) · 1.97 KB
/
enum.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
import { CodeMaker } from 'codemaker';
import { EnumType, EnumMember } from 'jsii-reflect';
import { SpecialDependencies } from '../dependencies';
import { EmitContext } from '../emit-context';
import { Package } from '../package';
import { JSII_RT_ALIAS } from '../runtime';
import { GoType } from './go-type';
export class Enum extends GoType<EnumType> {
private readonly members: readonly GoEnumMember[];
public constructor(pkg: Package, type: EnumType) {
super(pkg, type);
this.members = type.members.map((mem) => new GoEnumMember(this, mem));
}
public emit(context: EmitContext) {
this.emitDocs(context);
const { code } = context;
// TODO figure out the value type -- probably a string in most cases
const valueType = 'string';
code.line(`type ${this.name} ${valueType}`);
code.line();
code.open(`const (`);
// Const values are prefixed by the wrapped value type
for (const member of this.members) {
member.emit(code);
}
code.close(`)`);
code.line();
}
public emitRegistration(code: CodeMaker): void {
code.open(`${JSII_RT_ALIAS}.RegisterEnum(`);
code.line(`"${this.fqn}",`);
code.line(`reflect.TypeOf((*${this.name})(nil)).Elem(),`);
code.open(`map[string]interface{}{`);
for (const member of this.members) {
code.line(`"${member.rawValue}": ${member.name},`);
}
code.close(`},`);
code.close(')');
}
public get dependencies(): Package[] {
return [];
}
public get specialDependencies(): SpecialDependencies {
return {
runtime: false,
init: false,
internal: false,
time: false,
};
}
}
class GoEnumMember {
public readonly name: string;
public readonly rawValue: string;
public constructor(private readonly parent: Enum, entry: EnumMember) {
this.name = `${parent.name}_${entry.name}`;
this.rawValue = entry.name;
}
public emit(code: CodeMaker) {
code.line(`${this.name} ${this.parent.name} = "${this.rawValue}"`);
}
}