Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ bundle.js
!/testdata/*/actual/go.*
!/testdata/*/actual/package.*
!/testdata/*/actual/requirements.txt
npm
File renamed without changes.
46 changes: 46 additions & 0 deletions scripts/build_npm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// ex. scripts/build_npm.ts
import { build, emptyDir } from "https://deno.land/x/dnt@0.40.0/mod.ts";

await emptyDir("./npm");

await build({
entryPoints: [
"./src/cs/mod.ts",
"./src/go/mod.ts",
"./src/json-schema/mod.ts",
"./src/markdown/mod.ts",
"./src/openapiv3/mod.ts",
"./src/proto/mod.ts",
"./src/python/mod.ts",
"./src/rest/mod.ts",
"./src/rust/mod.ts",
"./src/typescript/mod.ts",
"./src/utils/mod.ts",
],
outDir: "./npm",
test: false,
shims: {
// see JS docs for overview and more options
deno: true,
},
package: {
// package.json properties
name: "@apexlang/core",
version: Deno.args[0],
description: "Apex language JavaScript support",
keywords: ["apex", "idl", "codegen"],
license: "Apache-2.0",
repository: {
type: "git",
url: "https://www.github.com/apexlang/apex-js",
},
bugs: {
url: "https://www.github.com/apexlang/apex-js/issues",
},
},
postBuild() {
// steps to run after building and before running the tests
Deno.copyFileSync("LICENSE", "npm/LICENSE");
Deno.copyFileSync("README.md", "npm/README.md");
},
});
4 changes: 2 additions & 2 deletions src/cs/union_visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ export class UnionVisitor extends BaseVisitor {
const { union } = context;
this.write(` ${formatComment("// ", union.description)}`);
this.write(`public record ${union.name} {\n`);
union.types.forEach((t) => {
const typeName = expandType(t);
union.members.forEach((member) => {
const typeName = expandType(member.type);
this.write(` public ${pascalCase(typeName)} ${typeName};`);
this.triggerCallbacks(context, "UnionStructTags");
this.write(`\n`);
Expand Down
2 changes: 1 addition & 1 deletion src/deps/core/ast.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from "https://deno.land/x/apex_core@v0.1.3/ast/mod.ts";
export * from "https://deno.land/x/apex_core@v0.1.5/ast/mod.ts";
2 changes: 1 addition & 1 deletion src/deps/core/model.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from "https://deno.land/x/apex_core@v0.1.3/model/mod.ts";
export * from "https://deno.land/x/apex_core@v0.1.5/model/mod.ts";
10 changes: 7 additions & 3 deletions src/go/grpc_visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,8 @@ func New${iface.name}GRPCWrapper(service ${iface.name}) *${iface.name}GRPCWrappe
}
switch {\n`,
);
union.types.forEach((ut) => {
union.members.forEach((member) => {
const ut = member.type;
this.write(`case from.${pascalCase(expandType(ut))} != nil:
return &pb.${union.name}{\n`);
switch (ut.kind) {
Expand Down Expand Up @@ -844,7 +845,8 @@ func New${iface.name}GRPCWrapper(service ${iface.name}) *${iface.name}GRPCWrappe
}
switch v := from.Value.(type) {\n`,
);
union.types.forEach((ut) => {
union.members.forEach((member) => {
const ut = member.type;
if (ut.kind == Kind.Type) {
const t = ut as Named;
this.write(`case *pb.${union.name}_${pascalCase(expandType(ut))}Value:
Expand Down Expand Up @@ -919,7 +921,9 @@ func New${iface.name}GRPCWrapper(service ${iface.name}) *${iface.name}GRPCWrappe
case Kind.Union: {
const u = a as Union;
m[u.name] = u;
u.types.forEach((t) => this.checkType(context, t, m, types));
u.members.forEach((member) =>
this.checkType(context, member.type, m, types)
);
break;
}
case Kind.Alias: {
Expand Down
17 changes: 7 additions & 10 deletions src/go/union_visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,14 @@ export class UnionVisitor extends GoVisitor {
const imports = getImports(context);
this.write(formatComment("// ", union.description));
this.write(`type ${union.name} struct {\n`);
union.types.forEach((t) => {
let tname = typeName(t);
const annotated = t as Annotated;
if (annotated.annotation) {
annotated.annotation("unionKey", (a) => {
tname = a.convert<UnionKey>().value;
});
}
union.members.forEach((member) => {
let tname = typeName(member.type);
member.annotation("unionKey", (a) => {
tname = a.convert<UnionKey>().value;
});

imports.type(t);
const expandedName = expandType(t);
imports.type(member.type);
const expandedName = expandType(member.type);
this.write(
`${
fieldName(
Expand Down
2 changes: 1 addition & 1 deletion src/json-schema/json-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ class UnionVisitor extends BaseVisitor {
const { union } = context;
const arr: SchemaObject[] = [];
convertArrayToObject(
union.types,
union.members.map((m) => m.type),
(t: AnyType) => {
switch (t.kind) {
case Kind.Union:
Expand Down
6 changes: 5 additions & 1 deletion src/markdown/markdown-visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ export class MarkdownVisitor extends BaseVisitor {
visitUnion(context: Context): void {
const u = context.union;
this.writeDefinitionName(u.name);
this.writeLn(`\`${u.name} = ${u.types.map(expandType).join(" | ")}\``);
this.writeLn(
`\`${u.name} = ${
u.members.map((m) => m.type).map(expandType).join(" | ")
}\``,
);
}
}
2 changes: 1 addition & 1 deletion src/openapiv3/openapiv3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ export class OpenAPIV3Visitor extends BaseVisitor {
type: Types.OBJECT,
description: union.description,
properties: convertArrayToObject(
union.types,
union.members.map((m) => m.type),
(t: AnyType) => {
switch (t.kind) {
case Kind.Union:
Expand Down
17 changes: 12 additions & 5 deletions src/proto/proto_visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,19 @@ package ${ns.name};\n\n`);
this.write(formatComment("// ", u.description));
this.write(`message ${pascalCase(u.name)} {\n`);
this.write(` oneof value {\n`);
let i = 0;
for (const t of u.types) {
const n = t as Named;
i++;

for (const member of u.members) {
const n = member.type as Named;

const fieldnumAnnotation = member.annotation("n");
if (!fieldnumAnnotation) {
throw new Error(`${u.name}.${n.name} requires a @n`);
}

this.write(
` ${typeSignature(t)} ${snakeCase(n.name)}_value = ${i};\n`,
` ${typeSignature(member.type)} ${
snakeCase(n.name)
}_value = ${n};\n`,
);
}
this.write(` }\n`);
Expand Down
4 changes: 2 additions & 2 deletions src/python/interfaces_visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ function tsort(context: Context): void {
}
case Kind.Union: {
const un = t as Union;
un.types.map((ty) => {
visitNamed(ty, (name: string) => {
un.members.map((member) => {
visitNamed(member.type, (name: string) => {
if (d.indexOf(name) == -1) {
d.push(name);
}
Expand Down
3 changes: 2 additions & 1 deletion src/rust/visitors/union_visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ export class UnionVisitor extends SourceGenerator<Union> {
}

getSource(): string {
const variants = this.root.types.map((t) => {
const variants = this.root.members.map((member) => {
const t = member.type;
const isRecursive = isRecursiveType(t);
const isHeapAllocated = t.kind === Kind.Map || t.kind === Kind.List;
const baseType = types.apexToRustType(t, this.config);
Expand Down
8 changes: 4 additions & 4 deletions src/typescript/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import { Configuration } from "https://deno.land/x/apex_cli@v0.0.18/src/config.ts";
import * as apex from "https://deno.land/x/apex_core@v0.1.3/mod.ts";
import * as ast from "https://deno.land/x/apex_core@v0.1.5/ast.ts";

const importUrl = new URL(".", import.meta.url);

function urlify(relpath: string): string {
function _urlify(relpath: string): string {
const url = new URL(relpath, importUrl).toString();
console.error(url);
return url;
}

export default function (
doc: apex.ast.Document,
_doc: ast.Document,
config: Configuration,
): Configuration {
config.generates ||= {};

config.config ||= {};
config.config.aliases ||= {};
(config.config.aliases as any).UUID = {
(config.config.aliases as Record<string, unknown>).UUID = {
type: true,
import: "v4 as uuidv4",
from: "uuid",
Expand Down
2 changes: 1 addition & 1 deletion src/utils/exposed_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class ExposedTypesVisitor extends BaseVisitor {
}
case Kind.Union: {
const u = any as Union;
u.types.forEach((t) => this.checkType(t));
u.members.forEach((member) => this.checkType(member.type));
break;
}
case Kind.Alias: {
Expand Down
9 changes: 5 additions & 4 deletions src/utils/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -602,9 +602,9 @@ export function convertOperationToType(
}

export function convertUnionToType(tr: TypeResolver, union: Union): Type {
const fields = union.types.map((param) => {
const n = typeName(param);
const t = modelToAST(param);
const fields = union.members.map((member) => {
const n = typeName(member.type);
const t = modelToAST(member.type);
return new FieldDefinition(
undefined,
new Name(undefined, n),
Expand Down Expand Up @@ -849,7 +849,8 @@ export function isRecursiveType(typ: AnyType, seen: NamedType[] = []): boolean {
case Kind.Union: {
const t = typ as Union;

return t.types.filter((t) => isRecursiveType(t, seen)).length > 0;
return t.members.filter((member) => isRecursiveType(member.type, seen))
.length > 0;
}

case Kind.Type: {
Expand Down
2 changes: 1 addition & 1 deletion templates/generator/src/default-visitor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
BaseVisitor,
Context,
} from "https://deno.land/x/apex_core@v0.1.3/model/mod.ts";
} from "https://deno.land/x/apex_core@v0.1.5/model/mod.ts";
import { TypeVisitor } from "./visitors/type-visitor.ts";
import { InterfaceVisitor } from "./visitors/interface-visitor.ts";
import { EnumVisitor } from "./visitors/enum-visitor.ts";
Expand Down
2 changes: 1 addition & 1 deletion templates/generator/src/utils/conversions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
ObjectMap,
Operation,
Parameter,
} from "https://deno.land/x/apex_core@v0.1.3/model/mod.ts";
} from "https://deno.land/x/apex_core@v0.1.5/model/mod.ts";
import * as utils from "https://deno.land/x/apex_codegen@v0.1.7/utils/mod.ts";
import { convertType } from "./types.ts";

Expand Down
2 changes: 1 addition & 1 deletion templates/generator/src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
Optional,
Primitive,
PrimitiveName,
} from "https://deno.land/x/apex_core@v0.1.3/model/mod.ts";
} from "https://deno.land/x/apex_core@v0.1.5/model/mod.ts";

/**
* Convert an Apex type to a type suitable for the destination format.
Expand Down
2 changes: 1 addition & 1 deletion templates/generator/src/visitors/alias-visitor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
Alias,
Context,
} from "https://deno.land/x/apex_core@v0.1.3/model/mod.ts";
} from "https://deno.land/x/apex_core@v0.1.5/model/mod.ts";
import { convertDescription } from "../utils/conversions.ts";
import { convertType } from "../utils/types.ts";

Expand Down
2 changes: 1 addition & 1 deletion templates/generator/src/visitors/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
Type,
Union,
Writer,
} from "https://deno.land/x/apex_core@v0.1.3/model/mod.ts";
} from "https://deno.land/x/apex_core@v0.1.5/model/mod.ts";

export type VisitorTypes = Alias | Type | Union | Enum | Interface;

Expand Down
2 changes: 1 addition & 1 deletion templates/generator/src/visitors/enum-visitor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
Context,
Enum,
} from "https://deno.land/x/apex_core@v0.1.3/model/mod.ts";
} from "https://deno.land/x/apex_core@v0.1.5/model/mod.ts";
import { convertDescription } from "../utils/conversions.ts";

import { SourceGenerator } from "./base.ts";
Expand Down
2 changes: 1 addition & 1 deletion templates/generator/src/visitors/interface-visitor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
Context,
Interface,
} from "https://deno.land/x/apex_core@v0.1.3/model/mod.ts";
} from "https://deno.land/x/apex_core@v0.1.5/model/mod.ts";
import { convertDescription, convertOperation } from "../utils/conversions.ts";

import { SourceGenerator } from "./base.ts";
Expand Down
2 changes: 1 addition & 1 deletion templates/generator/src/visitors/type-visitor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
Context,
Type,
} from "https://deno.land/x/apex_core@v0.1.3/model/mod.ts";
} from "https://deno.land/x/apex_core@v0.1.5/model/mod.ts";
import { convertDescription } from "../utils/conversions.ts";
import { convertType } from "../utils/types.ts";
import { SourceGenerator } from "./base.ts";
Expand Down
2 changes: 1 addition & 1 deletion templates/generator/src/visitors/union-visitor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
Context,
Union,
} from "https://deno.land/x/apex_core@v0.1.3/model/mod.ts";
} from "https://deno.land/x/apex_core@v0.1.5/model/mod.ts";
import { convertDescription } from "../utils/conversions.ts";
import { convertType } from "../utils/types.ts";

Expand Down
2 changes: 1 addition & 1 deletion templates/generator/test/default.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { parse } from "./parse.ts";
import { DefaultVisitor } from "../src/default-visitor.ts";
import { Writer } from "https://deno.land/x/apex_core@v0.1.3/model/mod.ts";
import { Writer } from "https://deno.land/x/apex_core@v0.1.5/model/mod.ts";
import { assert } from "https://deno.land/std@0.167.0/testing/asserts.ts";

Deno.test("should generate apex from apex", () => {
Expand Down
4 changes: 2 additions & 2 deletions templates/generator/test/parse.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { parse as parseApex } from "https://deno.land/x/apex_core@v0.1.3/mod.ts";
import { Context } from "https://deno.land/x/apex_core@v0.1.3/model/mod.ts";
import { parse as parseApex } from "https://deno.land/x/apex_core@v0.1.5/mod.ts";
import { Context } from "https://deno.land/x/apex_core@v0.1.5/model/mod.ts";

export function parse(src: string): Context {
const doc = parseApex(src);
Expand Down
6 changes: 3 additions & 3 deletions test/parse.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { parse as parseApex } from "https://deno.land/x/apex_core@v0.1.3/mod.ts";
import { parse as parseApex } from "https://deno.land/x/apex_core@v0.1.5/mod.ts";
import { Context, Namespace, Type } from "../src/deps/core/model.ts";

export function parse(src: string): Namespace {
Expand All @@ -9,6 +9,6 @@ export function parse(src: string): Namespace {

export function getTypes(namespace: Namespace, types: string[]): Type[] {
return Object.entries(namespace.types)
.filter(([name, type]) => (types.indexOf(name) === -1 ? false : true))
.map(([n, t]) => t);
.filter(([name, _type]) => (types.indexOf(name) === -1 ? false : true))
.map(([_n, t]) => t);
}
5 changes: 2 additions & 3 deletions test/utils/utilities.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { assert } from "https://deno.land/std@0.167.0/testing/asserts.ts";
import { assert } from "https://deno.land/std@0.213.0/assert/assert.ts";
import { isRecursiveType } from "../../src/utils/utilities.ts";
import { getTypes, parse } from "../parse.ts";
export * from "https://deno.land/std@0.167.0/testing/asserts.ts";

Deno.test("should not identify recursive types", () => {
const apex = `
Expand All @@ -16,7 +15,7 @@ Deno.test("should not identify recursive types", () => {
}
`;
const model = parse(apex);
const [parent, child] = getTypes(model, ["Parent"]);
const [parent, _child] = getTypes(model, ["Parent"]);
assert(isRecursiveType(parent));
});

Expand Down
2 changes: 1 addition & 1 deletion testdata/json-schema/expected/schema-test.json
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,4 @@
]
}
}
}
}
Loading