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: binding issues for map type #1186

Merged
merged 2 commits into from
Aug 26, 2022
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
54 changes: 37 additions & 17 deletions packages/schema/bind/src/bindings/assemblyscript/functions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isBaseType, isKeyword } from "./types";
import { MustacheFn } from "../types";
import { isBaseType, isKeyword } from "./types";

// check if any of the keywords match the property name;
// if there's a match, insert `_` at the beginning of the property name.
Expand Down Expand Up @@ -67,13 +67,29 @@ export const toWasmInit: MustacheFn = () => {
}

if (type.startsWith("Map<")) {
const openBracketIdx = type.indexOf("<");
const closeBracketIdx = type.lastIndexOf(">");
const [key, value] = type
.substring(openBracketIdx + 1, closeBracketIdx)
.split(",")
.map((x) => toWasm()(x.trim(), render));
return `new Map<${key}, ${value}>()`;
const firstOpenBracketIdx = type.indexOf("<");
const lastCloseBracketIdx = type.lastIndexOf(">");
if (firstOpenBracketIdx === -1 || lastCloseBracketIdx === -1) {
throw new Error(`Invalid Map: ${type}`);
}

const keyValTypes = type.substring(
firstOpenBracketIdx + 1,
lastCloseBracketIdx
);

const firstCommaIdx = keyValTypes.indexOf(",");
if (firstCommaIdx === -1) {
throw new Error(`Invalid Map: ${type}`);
}

const keyType = keyValTypes.substring(0, firstCommaIdx).trim();
const valType = keyValTypes.substring(firstCommaIdx + 1).trim();

const wasmKeyType = toWasm()(keyType, (str) => str);
const wasmValType = toWasm()(valType, (str) => str);

return `new Map<${wasmKeyType}, ${wasmValType}>()`;
}

switch (type) {
Expand Down Expand Up @@ -198,23 +214,27 @@ const toWasmMap = (type: string, optional: boolean): string => {
const firstOpenBracketIdx = type.indexOf("<");
const lastCloseBracketIdx = type.lastIndexOf(">");

if (!(firstOpenBracketIdx !== -1 && lastCloseBracketIdx !== -1)) {
if (firstOpenBracketIdx === -1 || lastCloseBracketIdx === -1) {
throw new Error(`Invalid Map: ${type}`);
}

const keyValTypes = type
.substring(firstOpenBracketIdx + 1, lastCloseBracketIdx)
.split(",")
.map((x) => x.trim());
const keyValTypes = type.substring(
firstOpenBracketIdx + 1,
lastCloseBracketIdx
);

if (keyValTypes.length !== 2 || !keyValTypes[0] || !keyValTypes[1]) {
const firstCommaIdx = keyValTypes.indexOf(",");
if (firstCommaIdx === -1) {
throw new Error(`Invalid Map: ${type}`);
}

const keyType = toWasm()(keyValTypes[0], (str) => str);
const valType = toWasm()(keyValTypes[1], (str) => str);
const keyType = keyValTypes.substring(0, firstCommaIdx).trim();
const valType = keyValTypes.substring(firstCommaIdx + 1).trim();

const wasmKeyType = toWasm()(keyType, (str) => str);
const wasmValType = toWasm()(valType, (str) => str);

return applyOptional(`Map<${keyType}, ${valType}>`, optional, false);
return applyOptional(`Map<${wasmKeyType}, ${wasmValType}>`, optional, false);
};

const applyOptional = (
Expand Down
53 changes: 37 additions & 16 deletions packages/schema/bind/src/bindings/rust/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,30 @@ export const toWasmInit: MustacheFn = () => {
}

if (type.startsWith("Map<")) {
const openBracketIdx = type.indexOf("<");
const closeBracketIdx = type.lastIndexOf(">");
const [key, value] = type
.substring(openBracketIdx + 1, closeBracketIdx)
.split(",")
.map((x) => toWasm()(x.trim(), render));
return optionalModifier(`Map::<${key}, ${value}>::new()`);
const firstOpenBracketIdx = type.indexOf("<");
const lastCloseBracketIdx = type.lastIndexOf(">");

if (firstOpenBracketIdx === -1 || lastCloseBracketIdx === -1) {
throw new Error(`Invalid Map: ${type}`);
}

const keyValTypes = type.substring(
firstOpenBracketIdx + 1,
lastCloseBracketIdx
);

const firstCommaIdx = keyValTypes.indexOf(",");
if (firstCommaIdx === -1) {
throw new Error(`Invalid Map: ${type}`);
}

const keyType = keyValTypes.substring(0, firstCommaIdx).trim();
const valType = keyValTypes.substring(firstCommaIdx + 1).trim();

const wasmKeyType = toWasm()(keyType, (str) => str);
const wasmValType = toWasm()(valType, (str) => str);

return optionalModifier(`Map::<${wasmKeyType}, ${wasmValType}>::new()`);
}

switch (type) {
Expand Down Expand Up @@ -302,23 +319,27 @@ const toWasmMap = (type: string, optional: boolean): string => {
const firstOpenBracketIdx = type.indexOf("<");
const lastCloseBracketIdx = type.lastIndexOf(">");

if (!(firstOpenBracketIdx !== -1 && lastCloseBracketIdx !== -1)) {
if (firstOpenBracketIdx === -1 || lastCloseBracketIdx === -1) {
throw new Error(`Invalid Map: ${type}`);
}

const keyValTypes = type
.substring(firstOpenBracketIdx + 1, lastCloseBracketIdx)
.split(",")
.map((x) => x.trim());
const keyValTypes = type.substring(
firstOpenBracketIdx + 1,
lastCloseBracketIdx
);

if (keyValTypes.length !== 2 || !keyValTypes[0] || !keyValTypes[1]) {
const firstCommaIdx = keyValTypes.indexOf(",");
if (firstCommaIdx === -1) {
throw new Error(`Invalid Map: ${type}`);
}

const keyType = toWasm()(keyValTypes[0], (str) => str);
const valType = toWasm()(keyValTypes[1], (str) => str);
const keyType = keyValTypes.substring(0, firstCommaIdx).trim();
const valType = keyValTypes.substring(firstCommaIdx + 1).trim();

const wasmKeyType = toWasm()(keyType, (str) => str);
const wasmValType = toWasm()(valType, (str) => str);

return applyOptional(`Map<${keyType}, ${valType}>`, optional);
return applyOptional(`Map<${wasmKeyType}, ${wasmValType}>`, optional);
};

const applyOptional = (type: string, optional: boolean): string => {
Expand Down
14 changes: 9 additions & 5 deletions packages/schema/bind/src/bindings/typescript/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,17 @@ const toTypescriptMap = (type: string, optional: boolean): string => {
const openAngleBracketIdx = type.indexOf("<");
const closeAngleBracketIdx = type.lastIndexOf(">");

const [keyType, valtype] = type
.substring(openAngleBracketIdx + 1, closeAngleBracketIdx)
.split(",")
.map((x) => x.trim());
const keyValTypes = type.substring(
openAngleBracketIdx + 1,
closeAngleBracketIdx
);

const firstCommaIdx = keyValTypes.indexOf(",");
const keyType = keyValTypes.substring(0, firstCommaIdx).trim();
const valType = keyValTypes.substring(firstCommaIdx + 1).trim();

const tsKeyType = _toTypescript(keyType, (str) => str);
const tsValType = _toTypescript(valtype, (str) => str, true);
const tsValType = _toTypescript(valType, (str) => str, true);

return applyOptional(`Map<${tsKeyType}, ${tsValType}>`, optional);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type Module @imports(
optEnumArray: [CustomEnum]
map: Map! @annotate(type: "Map<String!, Int!>!")
mapOfArr: Map! @annotate(type: "Map<String!, [Int!]!>!")
mapOfMap: Map! @annotate(type: "Map<String!, Map<String!, Int!>!>!")
mapOfObj: Map! @annotate(type: "Map<String!, AnotherType!>!")
mapOfArrOfObj: Map! @annotate(type: "Map<String!, [AnotherType!]!>!")
): Int!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface Args_moduleMethod extends Record<string, unknown> {
optEnumArray?: Array<Types.CustomEnum | null> | null;
map: Map<Types.String, Types.Int>;
mapOfArr: Map<Types.String, Array<Types.Int>>;
mapOfMap: Map<Types.String, Map<Types.String, Types.Int>>;
mapOfObj: Map<Types.String, Types.AnotherType>;
mapOfArrOfObj: Map<Types.String, Array<Types.AnotherType>>;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,72 @@ export const manifest: WrapManifest = {
"required": true,
"type": "Map<String, [Int]>"
},
{
"kind": 34,
"map": {
"key": {
"kind": 4,
"name": "mapOfMap",
"required": true,
"type": "String"
},
"kind": 262146,
"map": {
"key": {
"kind": 4,
"name": "mapOfMap",
"required": true,
"type": "String"
},
"kind": 262146,
"name": "mapOfMap",
"required": true,
"scalar": {
"kind": 4,
"name": "mapOfMap",
"required": true,
"type": "Int"
},
"type": "Map<String, Int>",
"value": {
"kind": 4,
"name": "mapOfMap",
"required": true,
"type": "Int"
}
},
"name": "mapOfMap",
"required": true,
"type": "Map<String, Map<String, Int>>",
"value": {
"key": {
"kind": 4,
"name": "mapOfMap",
"required": true,
"type": "String"
},
"kind": 262146,
"name": "mapOfMap",
"required": true,
"scalar": {
"kind": 4,
"name": "mapOfMap",
"required": true,
"type": "Int"
},
"type": "Map<String, Int>",
"value": {
"kind": 4,
"name": "mapOfMap",
"required": true,
"type": "Int"
}
}
},
"name": "mapOfMap",
"required": true,
"type": "Map<String, Map<String, Int>>"
},
{
"kind": 34,
"map": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export class Args_moduleMethod {
optEnumArray: Array<Option<Types.CustomEnum>> | null;
map: Map<string, i32>;
mapOfArr: Map<string, Array<i32>>;
mapOfMap: Map<string, Map<string, i32>>;
mapOfObj: Map<string, Types.AnotherType>;
mapOfArrOfObj: Map<string, Array<Types.AnotherType>>;
}
Expand All @@ -43,6 +44,8 @@ export function deserializemoduleMethodArgs(argsBuf: ArrayBuffer): Args_moduleMe
let _mapSet: bool = false;
let _mapOfArr: Map<string, Array<i32>> = new Map<string, Array<i32>>();
let _mapOfArrSet: bool = false;
let _mapOfMap: Map<string, Map<string, i32>> = new Map<string, Map<string, i32>>();
let _mapOfMapSet: bool = false;
let _mapOfObj: Map<string, Types.AnotherType> = new Map<string, Types.AnotherType>();
let _mapOfObjSet: bool = false;
let _mapOfArrOfObj: Map<string, Array<Types.AnotherType>> = new Map<string, Array<Types.AnotherType>>();
Expand Down Expand Up @@ -156,6 +159,20 @@ export function deserializemoduleMethodArgs(argsBuf: ArrayBuffer): Args_moduleMe
_mapOfArrSet = true;
reader.context().pop();
}
else if (field == "mapOfMap") {
reader.context().push(field, "Map<string, Map<string, i32>>", "type found, reading property");
_mapOfMap = reader.readExtGenericMap((reader: Read): string => {
return reader.readString();
}, (reader: Read): Map<string, i32> => {
return reader.readExtGenericMap((reader: Read): string => {
return reader.readString();
}, (reader: Read): i32 => {
return reader.readInt32();
});
});
_mapOfMapSet = true;
reader.context().pop();
}
else if (field == "mapOfObj") {
reader.context().push(field, "Map<string, Types.AnotherType>", "type found, reading property");
_mapOfObj = reader.readExtGenericMap((reader: Read): string => {
Expand Down Expand Up @@ -198,6 +215,9 @@ export function deserializemoduleMethodArgs(argsBuf: ArrayBuffer): Args_moduleMe
if (!_mapOfArrSet) {
throw new Error(reader.context().printWithContext("Missing required argument: 'mapOfArr: Map<String, [Int]>'"));
}
if (!_mapOfMapSet) {
throw new Error(reader.context().printWithContext("Missing required argument: 'mapOfMap: Map<String, Map<String, Int>>'"));
}
if (!_mapOfObjSet) {
throw new Error(reader.context().printWithContext("Missing required argument: 'mapOfObj: Map<String, AnotherType>'"));
}
Expand All @@ -214,6 +234,7 @@ export function deserializemoduleMethodArgs(argsBuf: ArrayBuffer): Args_moduleMe
optEnumArray: _optEnumArray,
map: _map,
mapOfArr: _mapOfArr,
mapOfMap: _mapOfMap,
mapOfObj: _mapOfObj,
mapOfArrOfObj: _mapOfArrOfObj
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export function moduleMethodWrapped(argsBuf: ArrayBuffer, env_size: u32): ArrayB
optEnumArray: args.optEnumArray,
map: args.map,
mapOfArr: args.mapOfArr,
mapOfMap: args.mapOfMap,
mapOfObj: args.mapOfObj,
mapOfArrOfObj: args.mapOfArrOfObj
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub struct ArgsModuleMethod {
pub opt_enum_array: Option<Vec<Option<CustomEnum>>>,
pub map: Map<String, i32>,
pub map_of_arr: Map<String, Vec<i32>>,
pub map_of_map: Map<String, Map<String, i32>>,
pub map_of_obj: Map<String, AnotherType>,
pub map_of_arr_of_obj: Map<String, Vec<AnotherType>>,
}
Expand All @@ -56,6 +57,8 @@ pub fn deserialize_module_method_args(args: &[u8]) -> Result<ArgsModuleMethod, D
let mut _map_set = false;
let mut _map_of_arr: Map<String, Vec<i32>> = Map::<String, Vec<i32>>::new();
let mut _map_of_arr_set = false;
let mut _map_of_map: Map<String, Map<String, i32>> = Map::<String, Map<String, i32>>::new();
let mut _map_of_map_set = false;
let mut _map_of_obj: Map<String, AnotherType> = Map::<String, AnotherType>::new();
let mut _map_of_obj_set = false;
let mut _map_of_arr_of_obj: Map<String, Vec<AnotherType>> = Map::<String, Vec<AnotherType>>::new();
Expand Down Expand Up @@ -161,6 +164,20 @@ pub fn deserialize_module_method_args(args: &[u8]) -> Result<ArgsModuleMethod, D
_map_of_arr_set = true;
reader.context().pop();
}
"mapOfMap" => {
reader.context().push(&field, "Map<String, Map<String, i32>>", "type found, reading argument");
_map_of_map = reader.read_ext_generic_map(|reader| {
reader.read_string()
}, |reader| {
reader.read_ext_generic_map(|reader| {
reader.read_string()
}, |reader| {
reader.read_i32()
})
})?;
_map_of_map_set = true;
reader.context().pop();
}
"mapOfObj" => {
reader.context().push(&field, "Map<String, AnotherType>", "type found, reading argument");
_map_of_obj = reader.read_ext_generic_map(|reader| {
Expand Down Expand Up @@ -203,6 +220,9 @@ pub fn deserialize_module_method_args(args: &[u8]) -> Result<ArgsModuleMethod, D
if !_map_of_arr_set {
return Err(DecodeError::MissingField("mapOfArr: Map<String, [Int]>.".to_string()));
}
if !_map_of_map_set {
return Err(DecodeError::MissingField("mapOfMap: Map<String, Map<String, Int>>.".to_string()));
}
if !_map_of_obj_set {
return Err(DecodeError::MissingField("mapOfObj: Map<String, AnotherType>.".to_string()));
}
Expand All @@ -219,6 +239,7 @@ pub fn deserialize_module_method_args(args: &[u8]) -> Result<ArgsModuleMethod, D
opt_enum_array: _opt_enum_array,
map: _map,
map_of_arr: _map_of_arr,
map_of_map: _map_of_map,
map_of_obj: _map_of_obj,
map_of_arr_of_obj: _map_of_arr_of_obj,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub fn module_method_wrapped(args: &[u8], env_size: u32) -> Vec<u8> {
opt_enum_array: args.opt_enum_array,
map: args.map,
map_of_arr: args.map_of_arr,
map_of_map: args.map_of_map,
map_of_obj: args.map_of_obj,
map_of_arr_of_obj: args.map_of_arr_of_obj,
});
Expand Down