Skip to content

Commit c66dd68

Browse files
committed
Write NS Info tweaks
1 parent b5203ae commit c66dd68

File tree

6 files changed

+99
-37
lines changed

6 files changed

+99
-37
lines changed

src/go/enum_visitor.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ import { Context, Writer } from "../../deps/@apexlang/core/model/mod.ts";
1818
import { formatComment, pascalCase, snakeCase } from "../utils/mod.ts";
1919
import { IMPORTS } from "./constant.ts";
2020
import { getImporter, GoVisitor } from "./go_visitor.ts";
21+
import {
22+
shouldWriteTypeInfo,
23+
writeNamespaceEmbeddedStruct,
24+
} from "./helpers.ts";
2125

2226
export class EnumVisitor extends GoVisitor {
2327
private writeTypeInfo: boolean;
@@ -29,6 +33,12 @@ export class EnumVisitor extends GoVisitor {
2933

3034
public override visitEnumBefore(context: Context): void {
3135
super.triggerEnumsBefore(context);
36+
37+
const writeTypeInfo = shouldWriteTypeInfo(context, this.writeTypeInfo);
38+
if (writeTypeInfo) {
39+
writeNamespaceEmbeddedStruct(context, this.writer);
40+
}
41+
3242
this.write(formatComment("// ", context.enum.description));
3343
this.write(`type ${context.enum.name} int32
3444

src/go/helpers.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
PrimitiveName,
3333
Stream,
3434
Valued,
35+
Writer,
3536
} from "../../deps/@apexlang/core/model/mod.ts";
3637
import { capitalize, renamed } from "../utils/mod.ts";
3738
import { Import } from "./alias_visitor.ts";
@@ -424,3 +425,38 @@ export function varAccessArg(
424425
export function receiver(iface: Interface): string {
425426
return iface.name[0].toLowerCase();
426427
}
428+
429+
export function shouldWriteTypeInfo(
430+
context: Context,
431+
defaultValue: boolean,
432+
): boolean {
433+
const writeTypeInfo = context.config.writeTypeInfo as boolean;
434+
if (writeTypeInfo == undefined) {
435+
return defaultValue;
436+
}
437+
return writeTypeInfo;
438+
}
439+
440+
export function writeNamespaceEmbeddedStruct(context: Context, writer: Writer) {
441+
if (context.config.nsWritten == true) {
442+
return;
443+
}
444+
445+
const { namespace: ns } = context;
446+
writer.write(`\n\n
447+
const NAMESPACE = "${ns.name}"
448+
449+
type ns struct{}
450+
451+
func (n *ns) GetNamespace() string {
452+
return NAMESPACE
453+
}\n\n`);
454+
455+
ns.annotation("version", (a) => {
456+
writer.write(`func (n *ns) Version() string {
457+
return "${a.arguments[0].value.getValue()}"
458+
}\n\n`);
459+
});
460+
461+
context.config.nsWritten = true;
462+
}

src/go/interfaces_visitor.ts

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -39,36 +39,10 @@ export class InterfacesVisitor extends GoVisitor {
3939
defaultsVisitor = (writer: Writer): Visitor => new DefaultsVisitor(writer);
4040
enumVisitor = (writer: Writer): Visitor =>
4141
new EnumVisitor(writer, this.writeTypeInfo);
42-
unionVisitor = (writer: Writer): Visitor => new UnionVisitor(writer);
42+
unionVisitor = (writer: Writer): Visitor =>
43+
new UnionVisitor(writer, this.writeTypeInfo);
4344
aliasVisitor = (writer: Writer): Visitor => new AliasVisitor(writer);
4445

45-
public override visitNamespaceBefore(context: Context): void {
46-
const { namespace: ns } = context;
47-
this.writeTypeInfo = context.config.writeTypeInfo as boolean;
48-
if (this.writeTypeInfo == undefined) {
49-
this.writeTypeInfo = false;
50-
}
51-
52-
if (this.writeTypeInfo) {
53-
this.write(`\n\n
54-
55-
const NAMESPACE = "${ns.name}"
56-
57-
type ns struct{}
58-
59-
func (n *ns) GetNamespace() string {
60-
return NAMESPACE
61-
}\n\n`);
62-
63-
ns.annotation("version", (a) => {
64-
this.write(`func (n *ns) Version() string {
65-
return "${a.arguments[0].value.getValue()}"
66-
}\n\n`);
67-
});
68-
}
69-
super.triggerNamespaceBefore(context);
70-
}
71-
7246
public override visitFunctionBefore(context: Context): void {
7347
const { operation } = context;
7448
const visitor = this.serviceVisitor(this.writer);

src/go/struct_visitor.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,13 @@ import {
2121
Named,
2222
Writer,
2323
} from "../../deps/@apexlang/core/model/mod.ts";
24-
import { defaultValueForType, expandType, fieldName } from "./helpers.ts";
24+
import {
25+
defaultValueForType,
26+
expandType,
27+
fieldName,
28+
shouldWriteTypeInfo,
29+
writeNamespaceEmbeddedStruct,
30+
} from "./helpers.ts";
2531
import { translateAlias } from "./alias_visitor.ts";
2632
import { formatComment } from "../utils/mod.ts";
2733
import { getImports, GoVisitor } from "./go_visitor.ts";
@@ -47,12 +53,10 @@ export class StructVisitor extends GoVisitor {
4753
public override visitTypeBefore(context: Context): void {
4854
const { type } = context;
4955
super.triggerTypeBefore(context);
50-
let writeTypeInfo = context.config.writeTypeInfo as boolean;
51-
if (writeTypeInfo == undefined) {
52-
writeTypeInfo = this.writeTypeInfo;
53-
}
5456

57+
const writeTypeInfo = shouldWriteTypeInfo(context, this.writeTypeInfo);
5558
if (writeTypeInfo) {
59+
writeNamespaceEmbeddedStruct(context, this.writer);
5660
this.write(
5761
`const TYPE_${snakeCase(type.name).toUpperCase()} = "${type.name}"\n\n`,
5862
);

src/go/union_visitor.ts

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,53 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
import { Annotated, Context } from "../../deps/@apexlang/core/model/mod.ts";
18-
import { formatComment, typeName } from "../utils/mod.ts";
17+
import {
18+
Annotated,
19+
Context,
20+
Writer,
21+
} from "../../deps/@apexlang/core/model/mod.ts";
22+
import { formatComment, snakeCase, typeName } from "../utils/mod.ts";
1923
import { getImports, GoVisitor } from "./go_visitor.ts";
20-
import { expandType, fieldName } from "./helpers.ts";
24+
import {
25+
expandType,
26+
fieldName,
27+
shouldWriteTypeInfo,
28+
writeNamespaceEmbeddedStruct,
29+
} from "./helpers.ts";
2130

2231
interface UnionKey {
2332
value: string;
2433
}
2534

2635
export class UnionVisitor extends GoVisitor {
36+
private writeTypeInfo: boolean;
37+
38+
constructor(writer: Writer, writeTypeInfo: boolean = false) {
39+
super(writer);
40+
this.writeTypeInfo = writeTypeInfo;
41+
}
42+
2743
public override visitUnion(context: Context): void {
2844
const tick = "`";
2945
const { union } = context;
3046
const imports = getImports(context);
47+
48+
const writeTypeInfo = shouldWriteTypeInfo(context, this.writeTypeInfo);
49+
if (writeTypeInfo) {
50+
writeNamespaceEmbeddedStruct(context, this.writer);
51+
52+
this.write(
53+
`const UNION_${
54+
snakeCase(union.name).toUpperCase()
55+
} = "${union.name}"\n\n`,
56+
);
57+
}
58+
3159
this.write(formatComment("// ", union.description));
3260
this.write(`type ${union.name} struct {\n`);
61+
if (writeTypeInfo) {
62+
this.write(`ns\n`);
63+
}
3364
union.members.forEach((member) => {
3465
let tname = typeName(member.type);
3566
member.annotation("unionKey", (a) => {
@@ -50,5 +81,12 @@ export class UnionVisitor extends GoVisitor {
5081
this.write(`"${tick}\n`);
5182
});
5283
this.write(`}\n\n`);
84+
85+
if (writeTypeInfo) {
86+
const receiver = union.name.substring(0, 1).toLowerCase();
87+
this.write(`func (${receiver} *${union.name}) GetType() string {
88+
return UNION_${snakeCase(union.name).toUpperCase()}
89+
}\n\n`);
90+
}
5391
}
5492
}

src/proto/proto_visitor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ package ${ns.name};\n\n`);
122122
this.write(
123123
` ${typeSignature(field.type)} ${
124124
snakeCase(field.name)
125-
} = ${fieldnum.value};\n`,
125+
} = ${fieldnum.value} [json_name="${field.name}"];\n`,
126126
);
127127
}
128128

0 commit comments

Comments
 (0)