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

feat: AssemblyScript Automatic Object-Type JSON Serialization #846

Merged
merged 15 commits into from
May 11, 2022
Merged
Show file tree
Hide file tree
Changes from 8 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
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,6 @@ RUN ./node_modules/.bin/asc {{dir}}/w3/entry.ts \
--use abort={{dir}}/w3/entry/w3Abort \
--optimize --debug --importMemory \
--runtime stub \
--runPasses asyncify
--runPasses asyncify \
--transform @serial-as/transform
{{/web3api_modules}}
90 changes: 26 additions & 64 deletions packages/js/client/src/__tests__/Web3ApiClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1434,8 +1434,6 @@ describe("Web3ApiClient", () => {
});

it("JSON-type", async () => {
type Json = string;

const client = await getClient();

const api = await buildAndDeployApi({
Expand All @@ -1448,78 +1446,42 @@ describe("Web3ApiClient", () => {
});
const ensUri = `ens/testnet/${api.ensDomain}`;

const value = { foo: "bar", bar: "baz" };
const parseResponse = await client.query<{
parse: Json;
}>({
const parse = await client.invoke<{ x: number; y: number }>({
uri: ensUri,
query: `query {
parse(value: $value)
}`,
variables: {
value: JSON.stringify(value),
module: "query",
method: "parse",
input: {
str: JSON.stringify({ x: 1, y: 2 }),
},
});

expect(parseResponse.data?.parse).toEqual(JSON.stringify(value));

const values = [
JSON.stringify({ bar: "foo" }),
JSON.stringify({ baz: "fuz" }),
];
const stringifyResponse = await client.query<{
stringify: Json;
}>({
uri: ensUri,
query: `query {
stringify(
values: $values
)
}`,
variables: {
values,
},
expect(parse.error).toBeFalsy();
expect(parse.data).toBeTruthy();
expect(parse.data).toMatchObject({
x: 1,
y: 2,
});

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

const object = {
jsonA: JSON.stringify({ foo: "bar" }),
jsonB: JSON.stringify({ fuz: "baz" }),
};
const stringifyObjectResponse = await client.query<{
stringifyObject: string;
}>({
const stringify = await client.invoke<{ str: string }>({
uri: ensUri,
query: `query {
stringifyObject(
object: $object
)
}`,
variables: {
object,
module: "query",
method: "stringify",
input: {
pair: {
x: 1,
y: 2,
},
},
});

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

const methodJSONResponse = await client.query<{
methodJSON: Json;
}>({
uri: ensUri,
query: `query {
methodJSON(valueA: 5, valueB: "foo", valueC: true)
}`,
});

const methodJSONResult = JSON.stringify({
valueA: 5,
valueB: "foo",
valueC: true,
});
expect(methodJSONResponse.data?.methodJSON).toEqual(methodJSONResult);
});

it("bytes-type", async () => {
Expand Down Expand Up @@ -2180,7 +2142,7 @@ describe("Web3ApiClient", () => {
const schema: string = await client.getSchema(
"w3://ens/js-logger.web3api.eth"
);

expect(schema).toStrictEqual(
`### Web3API Header START ###
scalar UInt
Expand Down
1 change: 0 additions & 1 deletion packages/schema/bind/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ MessagePack encoded data is sent between module boundaries. Decoding of the mess
| Bytes | bin 8/16/32 | array of 8-bit unsigned integer. |
| BigInt | fixstr or str 8/16/32 | UTF-8 string. |
| BigNumber | fixstr or str 8/16/32 | UTF-8 string. |
| JSON | fixstr or str 8/16/32 | UTF-8 string. |
dOrgJelli marked this conversation as resolved.
Show resolved Hide resolved
| [Type] | fixarray or array 16/32 | Array of elements. |
| Map | Msgpack extention type | Map of key-value pairs. |
| type CustomObject {<br/>&nbsp;&nbsp;prop: Type<br/>} | fixmap or map 16/32 | Structured object. |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,6 @@ export const toWasmInit: MustacheFn = () => {
return `BigInt.fromUInt16(0)`;
case "BigNumber":
return `new BigNumber(BigInt.fromUInt16(0), 0, 0)`;
case "JSON":
return `JSON.Value.Null()`;
default:
if (type.includes("Enum_")) {
return "0";
Expand Down Expand Up @@ -164,9 +162,6 @@ export const toWasm: MustacheFn = () => {
case "BigNumber":
type = "BigNumber";
break;
case "JSON":
type = "JSON.Value";
break;
default:
if (type.includes("Enum_")) {
type = `Types.${type.replace("Enum_", "")}`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
Nullable,
BigInt,
BigNumber,
JSON,
Result
} from "@web3api/wasm-as";
{{#methods.length}}
Expand All @@ -23,6 +22,7 @@ import {
import * as Types from "../..";

{{^isInterface}}
@serializable
export class {{type}} {

public static uri: string = "{{uri}}";
Expand Down Expand Up @@ -56,6 +56,7 @@ export class {{type}} {
}
{{/isInterface}}
{{#isInterface}}
@serializable
export class {{type}} {

public static interfaceUri: string = "{{uri}}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import * as Types from "../..";

{{#methods}}
@serializable
export class Input_{{name}} {
{{#arguments}}
{{#handleKeywords}}{{name}}{{/handleKeywords}}: {{#toWasm}}{{toGraphQLType}}{{/toWasm}};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
Nullable,
BigInt,
BigNumber,
JSON
} from "@web3api/wasm-as";
import {
serialize{{type}},
Expand All @@ -18,6 +17,7 @@ import {
} from "./serialization";
import * as Types from "../..";

@serializable
export class {{type}} {

public static uri: string = "{{uri}}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
{{/getImplementations}}
{{/capabilities}}

@serializable
export class {{namespace}} {
static uri: string = "{{uri}}"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import * as Types from "..";
{{#methods}}

@serializable
export class Input_{{name}} {
{{#arguments}}
{{#handleKeywords}}{{name}}{{/handleKeywords}}: {{#toWasm}}{{toGraphQLType}}{{/toWasm}};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
Nullable,
BigInt,
BigNumber,
JSON
} from "@web3api/wasm-as";
import {
serialize{{type}},
Expand All @@ -18,6 +17,7 @@ import {
} from "./serialization";
import * as Types from "..";

@serializable
export class {{type}} {
{{#properties}}
{{#handleKeywords}}{{name}}{{/handleKeywords}}: {{#toWasm}}{{toGraphQLType}}{{/toWasm}};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@ import {
Nullable,
BigInt,
BigNumber,
JSON,
Context
} from "@web3api/wasm-as";
14 changes: 4 additions & 10 deletions packages/schema/bind/src/bindings/typescript/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,10 @@ const _toTypescript = (
return toTypescriptMap(type, nullable);
}

switch (type) {
case "JSON":
type = "Json";
break;
default:
if (type.includes("Enum_")) {
type = `Types.${type.replace("Enum_", "")}`;
} else {
type = `Types.${type}`;
}
if (type.includes("Enum_")) {
type = `Types.${type.replace("Enum_", "")}`;
} else {
type = `Types.${type}`;
}

return undefinable
Expand Down
4 changes: 2 additions & 2 deletions packages/test-cases/cases/apis/json-type/package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "test-case-json-type",
"name": "test-case-new-json-type",
"private": true,
"dependencies": {
"@web3api/wasm-as": "0.0.1-prealpha.58",
"assemblyscript": "0.19.1"
}
}
}
30 changes: 4 additions & 26 deletions packages/test-cases/cases/apis/json-type/query/index.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,14 @@
import {
Input_parse,
Input_stringify,
Input_stringifyObject,
Input_methodJSON
Pair
} from "./w3";
import { JSON } from "@web3api/wasm-as";

export function parse(input: Input_parse): JSON.Value {
return JSON.parse(input.value);
export function parse(input: Input_parse): Pair {
return JSON.parse<Pair>(input.str);
}

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;
return JSON.stringify(input.pair);
}
25 changes: 5 additions & 20 deletions packages/test-cases/cases/apis/json-type/query/schema.graphql
Original file line number Diff line number Diff line change
@@ -1,24 +1,9 @@
type Query {
parse(
value: String!
): JSON!

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

stringifyObject(
object: Object!
): String!

methodJSON(
valueA: Int!
valueB: String!
valueC: Boolean!
): JSON!
parse(str: String!): Pair!
stringify(pair: Pair!): String!
}

type Object {
jsonA: JSON!
jsonB: JSON!
type Pair {
x: Int!
y: Int!
}
2 changes: 1 addition & 1 deletion packages/test-cases/cases/apis/json-type/web3api.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
format: 0.0.1-prealpha.7
name: JsonType
name: NewJsonType
build: ./web3api.build.yaml
language: wasm/assemblyscript
modules:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ type CustomType {
optBigint: BigInt
bignumber: BigNumber!
optBignumber: BigNumber
json: JSON!
dOrgJelli marked this conversation as resolved.
Show resolved Hide resolved
optJson: JSON
bytes: Bytes!
optBytes: Bytes
boolean: Boolean!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,6 @@ type CustomType {
optBigint: BigInt
bignumber: BigNumber!
optBignumber: BigNumber
json: JSON!
optJson: JSON
bytes: Bytes!
optBytes: Bytes
boolean: Boolean!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ export interface CustomType {
optBigint?: Types.BigInt | null;
bignumber: Types.BigNumber;
optBignumber?: Types.BigNumber | null;
json: Json;
optJson?: Json | null;
bytes: Types.Bytes;
optBytes?: Types.Bytes | null;
boolean: Types.Boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,6 @@ type CustomType {
optBigint: BigInt
bignumber: BigNumber!
optBignumber: BigNumber
json: JSON!
optJson: JSON
bytes: Bytes!
optBytes: Bytes
boolean: Boolean!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ export interface CustomType {
optBigint?: Types.BigInt | null;
bignumber: Types.BigNumber;
optBignumber?: Types.BigNumber | null;
json: Json;
optJson?: Json | null;
bytes: Types.Bytes;
optBytes?: Types.Bytes | null;
boolean: Types.Boolean;
Expand Down
Loading