Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: interface module bindings #1219

Merged
merged 10 commits into from
Sep 13, 2022
55 changes: 38 additions & 17 deletions packages/js/client/src/__tests__/e2e/test-cases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,20 +329,33 @@ export const runImplementationsTest = async (

export const runGetImplementationsTest = async (
client: PolywrapClient,
aggregatorUri: string,
interfaceUri: string,
implementationUri: string
) => {
let implUri = new Uri(implementationUri);
expect(client.getImplementations(interfaceUri)).toEqual([implUri.uri]);

const result = await client.invoke({
uri: implUri.uri,
uri: aggregatorUri,
method: "moduleImplementations",
});

expect(result.error).toBeFalsy();
expect(result.data).toBeTruthy();
expect(result.data).toEqual([implUri.uri]);

const moduleMethodResult = await client.invoke({
uri: aggregatorUri,
method: "abstractModuleMethod",
args: {
arg: {
str: "Test String 2",
},
},
});
expect(moduleMethodResult.error).toBeFalsy();
expect(moduleMethodResult.data).toEqual("Test String 2");
};

export const runInvalidTypesTest = async (
Expand Down Expand Up @@ -755,13 +768,16 @@ export const runObjectTypesTest = async (

export const runMapTypeTest = async (client: PolywrapClient, uri: string) => {
const mapClass = new Map<string, number>().set("Hello", 1).set("Heyo", 50);
const nestedMapClass = new Map<string, Map<string, number>>().set("Nested", mapClass);
const nestedMapClass = new Map<string, Map<string, number>>().set(
"Nested",
mapClass
);
const mapRecord: Record<string, number> = {
Hello: 1,
Heyo: 50,
};
const nestedMapRecord: Record<string, Record<string, number>> = {
Nested: mapRecord
Nested: mapRecord,
};

const returnMapResponse1 = await client.invoke<Map<string, number>>({
Expand Down Expand Up @@ -790,7 +806,7 @@ export const runMapTypeTest = async (client: PolywrapClient, uri: string) => {
args: {
foo: {
map: mapClass,
nestedMap: nestedMapClass
nestedMap: nestedMapClass,
},
key: "Hello",
},
Expand All @@ -804,7 +820,7 @@ export const runMapTypeTest = async (client: PolywrapClient, uri: string) => {
args: {
foo: {
map: mapRecord,
nestedMap: nestedMapRecord
nestedMap: nestedMapRecord,
},
key: "Heyo",
},
Expand All @@ -813,28 +829,33 @@ export const runMapTypeTest = async (client: PolywrapClient, uri: string) => {
expect(getKeyResponse2.data).toEqual(mapRecord.Heyo);

const returnCustomMap = await client.invoke<{
map: Map<string, number>,
nestedMap: Map<string, Map<string, number>>
map: Map<string, number>;
nestedMap: Map<string, Map<string, number>>;
}>({
uri,
method: "returnCustomMap",
args: {
foo: {
map: mapRecord,
nestedMap: nestedMapClass
}
nestedMap: nestedMapClass,
},
},
});
expect(returnCustomMap.error).toBeUndefined();
expect(returnCustomMap.data).toEqual({ map: mapClass, nestedMap: nestedMapClass });

const returnNestedMap = await client.invoke<Map<string, Map<string, number>>>({
uri,
method: "returnNestedMap",
args: {
foo: nestedMapClass
},
expect(returnCustomMap.data).toEqual({
map: mapClass,
nestedMap: nestedMapClass,
});

const returnNestedMap = await client.invoke<Map<string, Map<string, number>>>(
{
uri,
method: "returnNestedMap",
args: {
foo: nestedMapClass,
},
}
);
expect(returnNestedMap.error).toBeUndefined();
expect(returnNestedMap.data).toEqual(nestedMapClass);
};
Expand Down
13 changes: 10 additions & 3 deletions packages/js/client/src/__tests__/e2e/wasm-as.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { GetPathToTestWrappers } from "@polywrap/test-cases";
import { getClientWithEnsAndIpfs } from "../utils/getClientWithEnsAndIpfs";
import { getClient } from "../utils/getClient";

jest.setTimeout(200000);
jest.setTimeout(300000);

describe("wasm-as test cases", () => {
beforeAll(async () => {
Expand Down Expand Up @@ -137,12 +137,18 @@ describe("wasm-as test cases", () => {
});

it("implementations - getImplementations", async () => {
const interfacePath = `${GetPathToTestWrappers()}/wasm-as/implementations/test-interface`;
const interfaceUri = "wrap://ens/interface.eth";

const implementationPath = `${GetPathToTestWrappers()}/wasm-as/implementations/test-use-getImpl`;
const implementationUri = `wrap://fs/${implementationPath}/build`;
const implementationPath = `${GetPathToTestWrappers()}/wasm-as/implementations/test-wrapper`;
const implementationUri = `fs/${implementationPath}/build`;

const aggregatorPath = `${GetPathToTestWrappers()}/wasm-as/implementations/test-use-getImpl`;
const aggregatorUri = `fs/${aggregatorPath}/build`;

await buildWrapper(interfacePath);
await buildWrapper(implementationPath);
await buildWrapper(aggregatorPath);

const client = await getClient({
interfaces: [
Expand All @@ -155,6 +161,7 @@ describe("wasm-as test cases", () => {

await TestCases.runGetImplementationsTest(
client,
aggregatorUri,
interfaceUri,
implementationUri
);
Expand Down
12 changes: 9 additions & 3 deletions packages/js/client/src/__tests__/e2e/wasm-rs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,18 @@ describe("wasm-rs test cases", () => {
});

it("implementations - getImplementations", async () => {
const interfacePath = `${GetPathToTestWrappers()}/wasm-rs/implementations/test-interface`;
const interfaceUri = "wrap://ens/interface.eth";

const implementationPath = `${GetPathToTestWrappers()}/wasm-rs/implementations/test-use-getImpl`;
const implementationPath = `${GetPathToTestWrappers()}/wasm-rs/implementations/test-wrapper`;
const implementationUri = `fs/${implementationPath}/build`;

await buildWrapper(implementationPath);
const aggregatorPath = `${GetPathToTestWrappers()}/wasm-rs/implementations/test-use-getImpl`;
const aggregatorUri = `fs/${aggregatorPath}/build`;

const implementationUri = `fs/${implementationPath}/build`;
await buildWrapper(interfacePath);
await buildWrapper(implementationPath);
await buildWrapper(aggregatorPath);

const client = await getClient({
interfaces: [
Expand All @@ -134,6 +139,7 @@ describe("wasm-rs test cases", () => {

await TestCases.runGetImplementationsTest(
client,
aggregatorUri,
interfaceUri,
implementationUri
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use {{crate}}::{{#detectKeyword}}{{#toUpper}}{{type}}{{/toUpper}}{{/detectKeywor
{{/propertyDeps}}
{{/propertyDeps.length}}

{{^isInterface}}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct {{#detectKeyword}}{{#toUpper}}{{type}}{{/toUpper}}{{/detectKeyword}} {}

Expand All @@ -51,3 +52,34 @@ impl {{#detectKeyword}}{{#toUpper}}{{type}}{{/toUpper}}{{/detectKeyword}} {
{{/last}}
{{/methods}}
}
{{/isInterface}}
{{#isInterface}}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct {{#detectKeyword}}{{#toUpper}}{{type}}{{/toUpper}}{{/detectKeyword}}<'a> {
{{#isInterface}}uri: &'a str{{/isInterface}}
}

impl<'a> {{#detectKeyword}}{{#toUpper}}{{type}}{{/toUpper}}{{/detectKeyword}}<'a> {
pub const INTERFACE_URI: &'static str = "{{uri}}";

pub fn new(uri: &'a str) -> {{#detectKeyword}}{{#toUpper}}{{type}}{{/toUpper}}{{/detectKeyword}}<'a> {
{{#detectKeyword}}{{#toUpper}}{{type}}{{/toUpper}}{{/detectKeyword}} { uri: uri }
}

{{#methods}}
pub fn {{#toLower}}{{name}}{{/toLower}}(&self, args: &Args{{#toUpper}}{{name}}{{/toUpper}}) -> Result<{{#return}}{{#toWasm}}{{toGraphQLType}}{{/toWasm}}{{/return}}, String> {
let uri = self.uri;
let args = serialize_{{#toLower}}{{name}}{{/toLower}}_args(args).map_err(|e| e.to_string())?;
let result = subinvoke::wrap_subinvoke(
uri,
"{{name}}",
args,
)?;
deserialize_{{#toLower}}{{name}}{{/toLower}}_result(result.as_slice()).map_err(|e| e.to_string())
}
{{^last}}

{{/last}}
{{/methods}}
}
{{/isInterface}}
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,19 @@ use crate::TestImportObject;
use crate::TestImportEnum;

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct TestImportModule {}
pub struct TestImportModule<'a> {
uri: &'a str
}

impl TestImportModule {
pub const URI: &'static str = "testimport.uri.eth";
impl<'a> TestImportModule<'a> {
pub const INTERFACE_URI: &'static str = "testimport.uri.eth";

pub fn new() -> TestImportModule {
TestImportModule {}
pub fn new(uri: &'a str) -> TestImportModule<'a> {
TestImportModule { uri: uri }
}

pub fn imported_method(args: &ArgsImportedMethod) -> Result<Option<TestImportObject>, String> {
let uri = TestImportModule::URI;
pub fn imported_method(&self, args: &ArgsImportedMethod) -> Result<Option<TestImportObject>, String> {
let uri = self.uri;
let args = serialize_imported_method_args(args).map_err(|e| e.to_string())?;
let result = subinvoke::wrap_subinvoke(
uri,
Expand All @@ -42,8 +44,8 @@ impl TestImportModule {
deserialize_imported_method_result(result.as_slice()).map_err(|e| e.to_string())
}

pub fn another_method(args: &ArgsAnotherMethod) -> Result<i32, String> {
let uri = TestImportModule::URI;
pub fn another_method(&self, args: &ArgsAnotherMethod) -> Result<i32, String> {
let uri = self.uri;
let args = serialize_another_method_args(args).map_err(|e| e.to_string())?;
let result = subinvoke::wrap_subinvoke(
uri,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
pub mod wrap;
pub use wrap::*;
pub use wrap::imported::interface_module;
pub use wrap::imported::interface_argument;

pub fn module_implementations(args: ArgsModuleImplementations) -> Vec<String> {
Interface::get_implementations()
Interface::get_implementations()
}

pub fn module_method(args: ArgsModuleMethod) -> ImplementationType {
args.arg
}

pub fn abstract_module_method(args: ArgsAbstractModuleMethod) -> String {
args.arg.str
let impls = Interface::get_implementations();
let uri: String = impls[0].to_owned();
let module = InterfaceModule::new(&uri);
let method_args = interface_module::serialization::ArgsAbstractModuleMethod {
arg: interface_argument::InterfaceArgument {
str: args.arg.str
}
};
module.abstract_module_method(&method_args).unwrap()
}