Skip to content

Commit 8d59b34

Browse files
authored
Misc fixes (#53)
1 parent dfa50e4 commit 8d59b34

File tree

7 files changed

+213
-100
lines changed

7 files changed

+213
-100
lines changed

src/go/enum_visitor.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ limitations under the License.
1515
*/
1616

1717
import { Context, Writer } from "../../deps/@apexlang/core/model/mod.ts";
18-
import { formatComment, pascalCase } from "../utils/mod.ts";
18+
import { formatComment, pascalCase, snakeCase } from "../utils/mod.ts";
1919
import { IMPORTS } from "./constant.ts";
2020
import { getImporter, GoVisitor } from "./go_visitor.ts";
2121

@@ -56,8 +56,12 @@ export class EnumVisitor extends GoVisitor {
5656
context.enum.accept(context, toIDVisitor);
5757

5858
if (this.writeTypeInfo) {
59-
this.write(`func (e ${context.enum.name}) Type() string {
60-
return "${context.enum.name}"
59+
const constName = `ENUM_${snakeCase(context.enum.name).toUpperCase()}`;
60+
this.write(
61+
`const ${constName} = "${context.enum.name}"\n\n`,
62+
);
63+
this.write(`func (e ${context.enum.name}) GetType() string {
64+
return ${constName}
6165
}\n\n`);
6266
}
6367

src/go/interfaces_visitor.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,13 @@ export class InterfacesVisitor extends GoVisitor {
5151

5252
if (this.writeTypeInfo) {
5353
this.write(`\n\n
54+
55+
const NAMESPACE = "${ns.name}"
56+
5457
type ns struct{}
5558
56-
func (n *ns) Namespace() string {
57-
return "${ns.name}"
59+
func (n *ns) GetNamespace() string {
60+
return NAMESPACE
5861
}\n\n`);
5962

6063
ns.annotation("version", (a) => {

src/go/msgpack_decoder_visitor.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export class MsgPackDecoderVisitor extends GoVisitor {
3333
return err
3434
}
3535
36+
var _o ${type.name}
3637
for numFields > 0 {
3738
numFields--;
3839
${context.fields.length > 0 ? "field" : "_"}, err := decoder.ReadString()
@@ -52,7 +53,7 @@ export class MsgPackDecoderVisitor extends GoVisitor {
5253
msgpackRead(
5354
context,
5455
false,
55-
`o.${fieldName(field, field.name)}`,
56+
`_o.${fieldName(field, field.name)}`,
5657
true,
5758
"",
5859
field.type,
@@ -73,6 +74,7 @@ export class MsgPackDecoderVisitor extends GoVisitor {
7374
this.write(`if err != nil {
7475
return err
7576
}
77+
*o = _o
7678
}\n`);
7779
this.write(`
7880
return nil

src/go/struct_visitor.ts

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ limitations under the License.
1616

1717
import {
1818
Context,
19+
Field,
1920
Kind,
2021
Named,
2122
Writer,
2223
} from "../../deps/@apexlang/core/model/mod.ts";
23-
import { expandType, fieldName } from "./helpers.ts";
24+
import { defaultValueForType, expandType, fieldName } from "./helpers.ts";
2425
import { translateAlias } from "./alias_visitor.ts";
2526
import { formatComment } from "../utils/mod.ts";
2627
import { getImports, GoVisitor } from "./go_visitor.ts";
@@ -29,6 +30,7 @@ import {
2930
StringValue,
3031
Value,
3132
} from "../../deps/@apexlang/core/ast/mod.ts";
33+
import { snakeCase } from "../utils/utilities.ts";
3234

3335
interface Serialize {
3436
value: string;
@@ -45,14 +47,20 @@ export class StructVisitor extends GoVisitor {
4547
public override visitTypeBefore(context: Context): void {
4648
const { type } = context;
4749
super.triggerTypeBefore(context);
48-
this.write(formatComment("// ", type.description));
49-
this.write(`type ${type.name} struct {\n`);
50-
5150
let writeTypeInfo = context.config.writeTypeInfo as boolean;
5251
if (writeTypeInfo == undefined) {
5352
writeTypeInfo = this.writeTypeInfo;
5453
}
5554

55+
if (writeTypeInfo) {
56+
this.write(
57+
`const TYPE_${snakeCase(type.name).toUpperCase()} = "${type.name}"\n\n`,
58+
);
59+
}
60+
61+
this.write(formatComment("// ", type.description));
62+
this.write(`type ${type.name} struct {\n`);
63+
5664
if (writeTypeInfo) {
5765
this.write(` ns\n`);
5866
}
@@ -100,6 +108,7 @@ export class StructVisitor extends GoVisitor {
100108

101109
public override visitTypeAfter(context: Context): void {
102110
const { type } = context;
111+
const importer = getImports(context);
103112
const receiver = type.name.substring(0, 1).toLowerCase();
104113
this.write(`}\n\n`);
105114

@@ -108,8 +117,39 @@ export class StructVisitor extends GoVisitor {
108117
writeTypeInfo = this.writeTypeInfo;
109118
}
110119
if (writeTypeInfo) {
111-
this.write(`func (${receiver} *${type.name}) Type() string {
112-
return "${type.name}"
120+
const idField = type.fields.find((f: Field) => f.name == "id");
121+
if (idField) {
122+
importer.type(idField.type);
123+
const packageName = context.config.otherPackage;
124+
const idType = expandType(
125+
idField.type!,
126+
packageName,
127+
true,
128+
translateAlias(context),
129+
);
130+
const idDefault = defaultValueForType(
131+
context,
132+
idField.type,
133+
packageName,
134+
);
135+
this.write(`func (${receiver} *${type.name}) GetID() ${idType} {
136+
if ${receiver} == nil {
137+
return ${idDefault}
138+
}
139+
140+
return ${receiver}.ID
141+
}
142+
143+
func (${receiver} *${type.name}) SetID(id ${idType}) {
144+
if ${receiver} == nil {
145+
return
146+
}
147+
148+
${receiver}.ID = id
149+
}\n\n`);
150+
}
151+
this.write(`func (${receiver} *${type.name}) GetType() string {
152+
return TYPE_${snakeCase(type.name).toUpperCase()}
113153
}\n\n`);
114154
}
115155
super.triggerTypeAfter(context);

src/openapiv3/openapiv3.ts

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ export class OpenAPIV3Visitor extends BaseVisitor {
230230
}
231231

232232
const { operation, parameter } = context;
233-
let paramIn: string;
233+
let paramIn = "query";
234234

235235
let required = true;
236236
let t = parameter.type;
@@ -365,6 +365,7 @@ export class OpenAPIV3Visitor extends BaseVisitor {
365365
} as ParameterObject);
366366
return;
367367
}
368+
368369
case "query":
369370
switch (t.kind) {
370371
case Kind.List: {
@@ -401,7 +402,7 @@ export class OpenAPIV3Visitor extends BaseVisitor {
401402
...primitive,
402403
},
403404
} as ParameterObject);
404-
return;
405+
break;
405406
}
406407

407408
// query parameters encapsulated inside a type
@@ -423,16 +424,41 @@ export class OpenAPIV3Visitor extends BaseVisitor {
423424
} as ParameterObject);
424425
}
425426
});
426-
return;
427+
break;
427428
}
428429
throw Error(
429430
`query parameter "${parameter.name}" must be a built-type: found "${named.name}"`,
430431
);
431432
}
433+
434+
case Kind.Type: {
435+
const type = t as Type;
436+
type.fields.map((f) => {
437+
let value = f.type;
438+
if (value.kind == Kind.Optional) {
439+
value = (value as Optional).type;
440+
}
441+
if (value.kind != Kind.Primitive) {
442+
return;
443+
}
444+
const named = value as Named;
445+
const primitive = primitiveTypeMap.get(named.name);
446+
if (primitive) {
447+
this.operation!.parameters!.push({
448+
name: f.name,
449+
in: "query",
450+
description: f.description,
451+
required: f.type.kind != Kind.Optional,
452+
schema: {
453+
...primitive,
454+
},
455+
} as ParameterObject);
456+
}
457+
});
458+
break;
459+
}
432460
}
433461
}
434-
435-
this.operation!.parameters.push(p);
436462
}
437463

438464
public override visitOperationAfter(context: Context): void {

src/proto/proto_visitor.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,8 @@ const scalarTypeMap = new Map<string, string>([
298298
["boolean", "bool"],
299299
["date", "google.protobuf.Timestamp"],
300300
["datetime", "google.protobuf.Timestamp"],
301-
["any", "google.protobuf.Any"],
302-
["value", "google.protobuf.Any"],
301+
["any", "google.protobuf.Value"],
302+
["value", "google.protobuf.Value"],
303303
]);
304304

305305
function typeSignature(type: AnyType): string {
@@ -365,7 +365,8 @@ class ImportVisitor extends BaseVisitor {
365365
this.addImport("google/protobuf/timestamp.proto");
366366
break;
367367
case PrimitiveName.Any:
368-
this.addImport("google/protobuf/any.proto");
368+
case PrimitiveName.Value:
369+
this.addImport("google/protobuf/struct.proto");
369370
break;
370371
}
371372
break;
@@ -449,7 +450,8 @@ function primitiveMessageType(name: PrimitiveName): string {
449450
case PrimitiveName.Bytes:
450451
return `google.protobuf.BytesValue`;
451452
case PrimitiveName.Any:
452-
return `google.protobuf.Any`;
453+
case PrimitiveName.Value:
454+
return `google.protobuf.Value`;
453455
}
454456

455457
return "unknown";

0 commit comments

Comments
 (0)