Skip to content

Commit

Permalink
Merge pull request #1891 from polywrap/wrap-golang-patches
Browse files Browse the repository at this point in the history
fix: various fixes in golang bindings such that wrap-test-harness passes
  • Loading branch information
dOrgJelli authored Sep 7, 2023
2 parents 195c1b4 + b2a2cb9 commit c206ea9
Show file tree
Hide file tree
Showing 35 changed files with 788 additions and 2,570 deletions.
7 changes: 6 additions & 1 deletion packages/cli/src/lib/codegen/CodeGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,12 @@ export class CodeGenerator {
intlMsg.lib_codeGenerator_genCodeError(),
intlMsg.lib_codeGenerator_genCodeWarning(),
async () => {
return this.runCodegen(bindLanguage, overrides);
try {
return await this.runCodegen(bindLanguage, overrides);
} catch (err) {
console.error(err);
throw err;
}
}
);

Expand Down
23 changes: 22 additions & 1 deletion packages/schema/bind/src/bindings/golang/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ export const stopIter: MustacheFn = () => {
};
};

export const indexIter: MustacheFn = () => {
return (text: string, render: (template: string) => string): string => {
const rendered: string = render(text);
if (num !== -1) {
return `${rendered}[${num}]`;
}
return rendered;
};
};

export const currIter: MustacheFn = () => {
return (text: string, render: (template: string) => string): string => {
const rendered: string = render(text);
Expand All @@ -34,7 +44,7 @@ export const nextIter: MustacheFn = () => {
export const prevFullIter: MustacheFn = () => {
return (text: string, render: (template: string) => string): string => {
const rendered: string = render(text);
if (rendered == "stop") {
if (rendered == "stop" || num === -1) {
return "";
}
return Array(num)
Expand Down Expand Up @@ -132,6 +142,17 @@ export const makeImports: MustacheFn = () => {
};
};

export const enumConstants: MustacheFn = () => {
return (text: string, render: (template: string) => string): string => {
let lines = render(text).split("\n");
if (lines[0] === "") {
lines = lines.slice(1);
}
lines[0] = lines[0] + " = iota";
return lines.join("\n");
};
};

export const stuctProps: MustacheFn = () => {
return (text: string, render: (template: string) => string): string => {
const props: [string, string][] = render(text)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/* eslint-disable @typescript-eslint/naming-convention */
/* eslint-disable @typescript-eslint/no-explicit-any */
import {
Abi,
EnumRef,
ObjectRef,
ModuleDefinition,
ObjectDefinition,
} from "@polywrap/wrap-manifest-types-js";
import { AbiTransforms } from "@polywrap/schema-parse";

interface State {
importedTypes: Map<string, string>;
moduleDef?: ModuleDefinition;
objectDef?: ObjectDefinition;
}

export function appendImportedTypes(): AbiTransforms {
const state: State = {
importedTypes: new Map(),
};

const addImportedTypeRef = (def: EnumRef | ObjectRef) => {
const importType = state.importedTypes.get(def.type);

if (importType) {
if (state.moduleDef) {
const importedTypes: string[] =
(state.moduleDef as any).importedTypes || [];
if (importedTypes.indexOf(importType) === -1) {
importedTypes.push(importType);
}
state.moduleDef = {
...state.moduleDef,
importedTypes,
} as ModuleDefinition;
}

if (state.objectDef) {
const importedTypes: string[] =
(state.objectDef as any).importedTypes || [];
if (importedTypes.indexOf(importType) === -1) {
importedTypes.push(importType);
}
state.objectDef = {
...state.objectDef,
importedTypes,
} as ObjectDefinition;
}
}

return def;
};

return {
enter: {
Abi: (abi: Abi) => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
state.importedTypes = abi.importedTypes;
return abi;
},
ModuleDefinition: (def: ModuleDefinition) => {
state.moduleDef = def;
return def;
},
ObjectDefinition: (def: ObjectDefinition) => {
if (!state.moduleDef) {
state.objectDef = def;
}

return def;
},
EnumRef: (def: EnumRef) => {
return addImportedTypeRef(def);
},
ObjectRef: (def: ObjectRef) => {
return addImportedTypeRef(def);
},
},
leave: {
ModuleDefinition: (def: ModuleDefinition) => {
const newDef = state.moduleDef || def;
state.moduleDef = undefined;
return newDef;
},
ObjectDefinition: (def: ObjectDefinition) => {
const newDef = state.objectDef || def;
state.objectDef = undefined;
return newDef;
},
},
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* eslint-disable @typescript-eslint/naming-convention */
import {
Abi,
ImportedEnumDefinition,
ImportedEnvDefinition,
ImportedModuleDefinition,
ImportedObjectDefinition,
} from "@polywrap/wrap-manifest-types-js";
import { AbiTransforms } from "@polywrap/schema-parse";

interface State {
importedTypes: Map<string, string>;
}

export function extractImportedTypes(): AbiTransforms {
const state: State = {
importedTypes: new Map(),
};

return {
enter: {
ImportedEnumDefinition: (def: ImportedEnumDefinition) => {
state.importedTypes = state.importedTypes.set(def.type, def.namespace);
return def;
},
ImportedEnvDefinition: (def: ImportedEnvDefinition) => {
state.importedTypes = state.importedTypes.set(def.type, def.namespace);
return def;
},
ImportedModuleDefinition: (def: ImportedModuleDefinition) => {
state.importedTypes = state.importedTypes.set(def.type, def.namespace);
return def;
},
ImportedObjectDefinition: (def: ImportedObjectDefinition) => {
state.importedTypes = state.importedTypes.set(def.type, def.namespace);
return def;
},
},
leave: {
Abi(abi: Abi) {
return {
...abi,
importedTypes: state.importedTypes,
};
},
},
};
}
3 changes: 2 additions & 1 deletion packages/schema/bind/src/bindings/golang/transforms/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./appendImportedTypes";
export * from "./extractImportedTypes";
export * from "./moduleNeedsTypes";
export * from "./moduleNeedsImportedTypes";

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ import {
} from "@polywrap/wrap-manifest-types-js";
import { AbiTransforms } from "@polywrap/schema-parse";

interface ModuleNeedsTypesState {
interface State {
moduleDefinition?: ModuleDefinition;
needsTypes?: boolean;
importedTypes?: Map<string, string>;
}

export function moduleNeedsTypes(): AbiTransforms {
const state: ModuleNeedsTypesState = {};
const state: State = {};

return {
enter: {
Abi: (abi) => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
state.importedTypes = abi._importedTypes;
state.importedTypes = abi.importedTypes;
return abi;
},
ModuleDefinition: (def: ModuleDefinition) => {
Expand Down
Loading

0 comments on commit c206ea9

Please sign in to comment.