Skip to content

Commit

Permalink
Merge pull request #938 from polywrap/chore/rollback-serial-as-json
Browse files Browse the repository at this point in the history
Chore: Rollback JSON Serialization in AS
  • Loading branch information
dOrgJelli authored Jun 22, 2022
2 parents 255caa0 + edb0226 commit 08144df
Show file tree
Hide file tree
Showing 17 changed files with 201 additions and 218 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,5 @@ RUN ./node_modules/.bin/asc {{dir}}/wrap/entry.ts \
--use abort={{dir}}/wrap/entry/wrapAbort \
--optimize --importMemory \
--runtime stub \
--runPasses asyncify \
--transform @serial-as/transform
--runPasses asyncify
{{/polywrap_module}}
76 changes: 52 additions & 24 deletions packages/js/client/src/__tests__/e2e/test-cases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -632,40 +632,68 @@ export const runJsonTypeTest = async (
client: PolywrapClient,
uri: string
) => {
const fromJson = await client.invoke<{ x: number; y: number }>({
type Json = string;
const value = JSON.stringify({ foo: "bar", bar: "bar" })
const parseResponse = await client.invoke<{ parse: Json }>({
uri,
method: "fromJson",
method: "parse",
input: {
json: JSON.stringify({ x: 1, y: 2 }),
},
});
value
}
})

expect(fromJson.error).toBeFalsy();
expect(fromJson.data).toBeTruthy();
expect(fromJson.data).toMatchObject({
x: 1,
y: 2,
});
expect(parseResponse.data).toEqual(value);

const values = [
JSON.stringify({ bar: "foo" }),
JSON.stringify({ baz: "fuz" }),
];

const toJson = await client.invoke<{ str: string }>({
const stringifyResponse = await client.invoke<{ stringify: Json}>({
uri,
method: "toJson",
method: "stringify",
input: {
pair: {
x: 1,
y: 2,
},
values
}
})

expect(stringifyResponse.data).toEqual(values.join(""));

const object = {
jsonA: JSON.stringify({ foo: "bar" }),
jsonB: JSON.stringify({ fuz: "baz" }),
};

const stringifyObjectResponse = await client.invoke<{
stringifyObject: string;
}>({
uri,
method: "stringifyObject",
input: {
object,
},
});

expect(toJson.error).toBeFalsy();
expect(toJson.data).toBeTruthy();
expect(toJson.data).toBe(
JSON.stringify({
x: 1,
y: 2,
})
expect(stringifyObjectResponse.data).toEqual(
object.jsonA + object.jsonB
);

const json = {
valueA: 5,
valueB: "foo",
valueC: true
}

const methodJSONResponse = await client.invoke<{
methodJSON: Json;
}>({
uri,
method: "methodJSON",
input: json
});

const methodJSONResult = JSON.stringify(json);
expect(methodJSONResponse.data).toEqual(methodJSONResult);
};

export const runLargeTypesTest = async (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import {
Nullable,
BigInt,
BigNumber,
JSON,
JSONSerializer,
JSONDeserializer,
JSON
} from "@polywrap/wasm-as";
import {
serialize{{type}},
Expand All @@ -16,7 +14,6 @@ import {
} from "./serialization";
import * as Types from "../..";

@serializable
export class {{type}} {
public static uri: string = "{{uri}}";
Expand All @@ -40,6 +37,4 @@ export class {{type}} {
static read(reader: Read): {{type}} {
return read{{type}}(reader);
}

{{> json_methods}}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import {
Nullable,
BigInt,
BigNumber,
JSON,
JSONSerializer,
JSONDeserializer,
JSON
} from "@polywrap/wasm-as";
import {
serialize{{type}},
Expand All @@ -16,7 +14,6 @@ import {
} from "./serialization";
import * as Types from "..";

@serializable
export class {{type}} {
{{#properties}}
{{#handleKeywords}}{{name}}{{/handleKeywords}}: {{#toWasm}}{{toGraphQLType}}{{/toWasm}};
Expand All @@ -37,6 +34,4 @@ export class {{type}} {
static read(reader: Read): {{type}} {
return read{{type}}(reader);
}

{{> json_methods}}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import {
Nullable,
BigInt,
BigNumber,
JSON,
JSONSerializer,
JSONDeserializer,
JSON
} from "@polywrap/wasm-as";
import {
serializeAnotherType,
Expand All @@ -16,7 +14,6 @@ import {
} from "./serialization";
import * as Types from "..";

@serializable
export class AnotherType {
prop: string | null;
circular: Types.CustomType | null;
Expand All @@ -37,12 +34,4 @@ export class AnotherType {
static read(reader: Read): AnotherType {
return readAnotherType(reader);
}

static toJson(type: AnotherType): JSON.Value {
return JSONSerializer.encode(type);
}

static fromJson(json: JSON.Value): AnotherType {
return (new JSONDeserializer(json)).decode<AnotherType>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import {
Nullable,
BigInt,
BigNumber,
JSON,
JSONSerializer,
JSONDeserializer,
JSON
} from "@polywrap/wasm-as";
import {
serializeCustomType,
Expand All @@ -16,7 +14,6 @@ import {
} from "./serialization";
import * as Types from "..";

@serializable
export class CustomType {
str: string;
optStr: string | null;
Expand Down Expand Up @@ -71,12 +68,4 @@ export class CustomType {
static read(reader: Read): CustomType {
return readCustomType(reader);
}

static toJson(type: CustomType): JSON.Value {
return JSONSerializer.encode(type);
}

static fromJson(json: JSON.Value): CustomType {
return (new JSONDeserializer(json)).decode<CustomType>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import {
Nullable,
BigInt,
BigNumber,
JSON,
JSONSerializer,
JSONDeserializer,
JSON
} from "@polywrap/wasm-as";
import {
serializeEnv,
Expand All @@ -16,7 +14,6 @@ import {
} from "./serialization";
import * as Types from "..";

@serializable
export class Env {
prop: string;
optProp: string | null;
Expand All @@ -37,12 +34,4 @@ export class Env {
static read(reader: Read): Env {
return readEnv(reader);
}

static toJson(type: Env): JSON.Value {
return JSONSerializer.encode(type);
}

static fromJson(json: JSON.Value): Env {
return (new JSONDeserializer(json)).decode<Env>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import {
Nullable,
BigInt,
BigNumber,
JSON,
JSONSerializer,
JSONDeserializer,
JSON
} from "@polywrap/wasm-as";
import {
serializeTestImport_AnotherObject,
Expand All @@ -16,7 +14,6 @@ import {
} from "./serialization";
import * as Types from "../..";

@serializable
export class TestImport_AnotherObject {

public static uri: string = "testimport.uri.eth";
Expand All @@ -38,12 +35,4 @@ export class TestImport_AnotherObject {
static read(reader: Read): TestImport_AnotherObject {
return readTestImport_AnotherObject(reader);
}

static toJson(type: TestImport_AnotherObject): JSON.Value {
return JSONSerializer.encode(type);
}

static fromJson(json: JSON.Value): TestImport_AnotherObject {
return (new JSONDeserializer(json)).decode<TestImport_AnotherObject>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import {
Nullable,
BigInt,
BigNumber,
JSON,
JSONSerializer,
JSONDeserializer,
JSON
} from "@polywrap/wasm-as";
import {
serializeTestImport_Object,
Expand All @@ -16,7 +14,6 @@ import {
} from "./serialization";
import * as Types from "../..";

@serializable
export class TestImport_Object {

public static uri: string = "testimport.uri.eth";
Expand Down Expand Up @@ -45,12 +42,4 @@ export class TestImport_Object {
static read(reader: Read): TestImport_Object {
return readTestImport_Object(reader);
}

static toJson(type: TestImport_Object): JSON.Value {
return JSONSerializer.encode(type);
}

static fromJson(json: JSON.Value): TestImport_Object {
return (new JSONDeserializer(json)).decode<TestImport_Object>();
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
type Module {
fromJson(json: JSON!): Pair!
toJson(pair: Pair!): JSON!
}
parse(
value: String!
): JSON!

stringify(
values: [JSON!]!
): String!

type Pair {
x: Int!
y: Int!
stringifyObject(
object: Object!
): String!

methodJSON(
valueA: Int!
valueB: String!
valueC: Boolean!
): JSON!
}

type Object {
jsonA: JSON!
jsonB: JSON!
}
36 changes: 29 additions & 7 deletions packages/test-cases/cases/wrappers/wasm-as/json-type/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,36 @@
import {
Input_fromJson,
Input_toJson,
Pair
Input_parse,
Input_stringify,
Input_stringifyObject,
Input_methodJSON
} from "./wrap";
import { JSON } from "@polywrap/wasm-as";

export function fromJson(input: Input_fromJson): Pair {
return Pair.fromJson(input.json);
export function parse(input: Input_parse): JSON.Value {
return JSON.parse(input.value);
}

export function toJson(input: Input_toJson): JSON.Value {
return Pair.toJson(input.pair);
export function stringify(input: Input_stringify): string {
let str = "";
for (let i = 0; i < input.values.length; ++i) {
const value = input.values[i];
str += value.stringify();
}
return str;
}

export function stringifyObject(input: Input_stringifyObject): string {
let str = "";
str += input.object.jsonA.stringify();
str += input.object.jsonB.stringify();
return str;
}

export function methodJSON(input: Input_methodJSON): JSON.Value {
const result = JSON.Value.Object();
result.set("valueA", JSON.from(input.valueA));
result.set("valueB", JSON.from(input.valueB));
result.set("valueC", JSON.from(input.valueC));

return result;
}
Loading

0 comments on commit 08144df

Please sign in to comment.