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(bind): map of array weren't generating invalid bindings #924

Merged
merged 23 commits into from
Jul 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
2c62cb9
fix: test-cases
Niraj-Kamdar Jun 13, 2022
7f36dbd
fix: issue
Niraj-Kamdar Jun 13, 2022
0183fe7
fix: rust binding
Niraj-Kamdar Jun 15, 2022
a39eb66
fix(bind): tests
Niraj-Kamdar Jun 16, 2022
71b246c
Merge remote-tracking branch 'upstream/prealpha-dev' into nk/fix-map-…
Niraj-Kamdar Jun 20, 2022
6fb954c
Merge branch 'nk/cli-workflow' into nk/fix-map-binding
Niraj-Kamdar Jun 20, 2022
31868c1
Merge branch 'nk/cli-workflow' into nk/fix-map-binding
Niraj-Kamdar Jun 20, 2022
cca6d3f
Merge branch 'nk/cli-workflow' into nk/fix-map-binding
Niraj-Kamdar Jun 20, 2022
a990fd5
Merge branch 'nk/cli-workflow' into nk/fix-map-binding
Niraj-Kamdar Jun 20, 2022
baa6159
Merge branch 'nk/cli-workflow' into nk/fix-map-binding
Niraj-Kamdar Jun 20, 2022
8ce4763
Merge branch 'prealpha-dev' into nk/fix-map-binding
dOrgJelli Jun 23, 2022
5e8d698
Merge branch 'prealpha-dev' into nk/fix-map-binding
Niraj-Kamdar Jul 4, 2022
3cfb3f0
chore: bump yarn.lock
Niraj-Kamdar Jul 4, 2022
fe9dc3d
Merge branch 'nk/fix-map-binding' of https://github.com/polywrap/mono…
Niraj-Kamdar Jul 4, 2022
97fc8d3
fix(schema-bind): tests
Niraj-Kamdar Jul 4, 2022
2da6798
very WIP
dOrgJelli Jul 5, 2022
db48aee
Merge branch 'nk/fix-map-binding' into dorgjelli/fix-map-binding
Niraj-Kamdar Jul 6, 2022
aa6aae2
Merge pull request #1030 from polywrap/dorgjelli/fix-map-binding
Niraj-Kamdar Jul 6, 2022
77d53ee
Merge remote-tracking branch 'upstream/origin-dev' into nk/fix-map-bi…
Niraj-Kamdar Jul 6, 2022
431f1ca
Merge remote-tracking branch 'upstream/nk/fix-map-binding' into nk/fi…
Niraj-Kamdar Jul 6, 2022
c2d37f6
fix(schema-parse): map type issues
Niraj-Kamdar Jul 6, 2022
af8dc17
fix(schema-bind): map type issues
Niraj-Kamdar Jul 6, 2022
26afc3e
refactor(schema-parse): lint fix
Niraj-Kamdar Jul 6, 2022
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
2 changes: 1 addition & 1 deletion packages/cli/bin/polywrap
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ if (['v', 'version', '-v', '--v', '-version', '--version'].includes(process.argv
}

var sourceDir = __dirname + '/../build'
require(sourceDir + '/cli').run(process.argv)
require(sourceDir + '/cli').run(process.argv)
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ return reader.read{{#toMsgPack}}{{toGraphQLType}}{{/toMsgPack}}((reader: Read):
return reader.read{{#toMsgPack}}{{toGraphQLType}}{{/toMsgPack}}((reader: Read): {{#key}}{{#toWasm}}{{toGraphQLType}}{{/toWasm}}{{/key}} => {
return reader.read{{#key}}{{#toMsgPack}}{{toGraphQLType}}{{/toMsgPack}}{{/key}}();
}, (reader: Read): {{#value}}{{#toWasm}}{{toGraphQLType}}{{/toWasm}}{{/value}} => {
{{#value}}
{{> deserialize_map_value}}
{{/value}}
});
{{/map}}
{{#enum}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ export function deserialize{{name}}Args(argsBuf: ArrayBuffer): Args_{{name}} {
_{{name}} = reader.read{{#toMsgPack}}{{toGraphQLType}}{{/toMsgPack}}((reader: Read): {{#key}}{{#toWasm}}{{toGraphQLType}}{{/toWasm}}{{/key}} => {
return reader.read{{#key}}{{#toMsgPack}}{{toGraphQLType}}{{/toMsgPack}}{{/key}}();
}, (reader: Read): {{#value}}{{#toWasm}}{{toGraphQLType}}{{/toWasm}}{{/value}} => {
{{#value}}
{{> deserialize_map_value}}
{{/value}}
});
{{/map}}
{{#enum}}
Expand Down Expand Up @@ -120,9 +118,7 @@ export function write{{name}}Result(writer: Write, result: {{#return}}{{#toWasm}
writer.write{{#toMsgPack}}{{toGraphQLType}}{{/toMsgPack}}(result, (writer: Write, key: {{#key}}{{#toWasm}}{{toGraphQLType}}{{/toWasm}}{{/key}}) => {
writer.write{{#key}}{{#toMsgPack}}{{toGraphQLType}}{{/toMsgPack}}{{/key}}(key);
}, (writer: Write, value: {{#value}}{{#toWasm}}{{toGraphQLType}}{{/toWasm}}{{/value}}): void => {
{{#value}}
{{> serialize_map_value}}
{{/value}}
});
{{/map}}
{{#enum}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ export function write{{type}}(writer: Write, type: {{type}}): void {
writer.write{{#toMsgPack}}{{toGraphQLType}}{{/toMsgPack}}(type.{{#handleKeywords}}{{name}}{{/handleKeywords}}, (writer: Write, key: {{#key}}{{#toWasm}}{{toGraphQLType}}{{/toWasm}}{{/key}}) => {
writer.write{{#key}}{{#toMsgPack}}{{toGraphQLType}}{{/toMsgPack}}{{/key}}(key);
}, (writer: Write, value: {{#value}}{{#toWasm}}{{toGraphQLType}}{{/toWasm}}{{/value}}): void => {
{{#value}}
{{> serialize_map_value}}
{{/value}}
});
{{/map}}
{{#object}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ reader.read_{{#toLower}}{{#toMsgPack}}{{toGraphQLType}}{{/toMsgPack}}{{/toLower}
reader.read_{{#toLower}}{{#toMsgPack}}{{toGraphQLType}}{{/toMsgPack}}{{/toLower}}(|reader| {
reader.read_{{#key}}{{#toLower}}{{#toMsgPack}}{{toGraphQLType}}{{/toMsgPack}}{{/toLower}}{{/key}}()
}, |reader| {
{{#value}}
{{> deserialize_map_value}}
{{/value}}
})
{{/map}}
{{#enum}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ reader.read_{{#toLower}}{{#toMsgPack}}{{toGraphQLType}}{{/toMsgPack}}{{/toLower}
reader.read_{{#toLower}}{{#toMsgPack}}{{toGraphQLType}}{{/toMsgPack}}{{/toLower}}(|reader| {
reader.read_{{#key}}{{#toLower}}{{#toMsgPack}}{{toGraphQLType}}{{/toMsgPack}}{{/toLower}}{{/key}}()
}, |reader| {
{{#value}}
{{> deserialize_map_value_nobox}}
{{/value}}
})
{{/map}}
{{#enum}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,7 @@ pub fn deserialize_{{#toLower}}{{name}}{{/toLower}}_args(args: &[u8]) -> Result<
_{{#toLower}}{{name}}{{/toLower}} = reader.read_{{#toLower}}{{#toMsgPack}}{{toGraphQLType}}{{/toMsgPack}}{{/toLower}}(|reader| {
reader.read_{{#key}}{{#toLower}}{{#toMsgPack}}{{toGraphQLType}}{{/toMsgPack}}{{/toLower}}{{/key}}()
}, |reader| {
{{#value}}
{{> deserialize_map_value_nobox}}
{{/value}}
})?;
{{/map}}
{{#enum}}
Expand Down Expand Up @@ -144,9 +142,7 @@ pub fn write_{{#toLower}}{{name}}{{/toLower}}_result<W: Write>(result: {{#return
writer.write_{{#toLower}}{{#toMsgPack}}{{toGraphQLType}}{{/toMsgPack}}{{/toLower}}(&result, |writer, key| {
writer.write_{{#key}}{{#toLower}}{{#toMsgPack}}{{toGraphQLType}}{{/toMsgPack}}{{/toLower}}{{/key}}(key)
}, |writer, value| {
{{#value}}
{{> serialize_map_value}}
{{/value}}
})?;
{{/map}}
{{#enum}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ pub fn write_{{#toLower}}{{type}}{{/toLower}}<W: Write>(args: &{{#toUpper}}{{typ
writer.write_{{#toLower}}{{#toMsgPack}}{{toGraphQLType}}{{/toMsgPack}}{{/toLower}}(&args.{{#detectKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/detectKeyword}}, |writer, key| {
writer.write_{{#key}}{{#toLower}}{{#toMsgPack}}{{toGraphQLType}}{{/toMsgPack}}{{/toLower}}{{/key}}(key)
}, |writer, value| {
{{#value}}
{{> serialize_map_value}}
{{/value}}
})?;
{{/map}}
{{#object}}
Expand Down Expand Up @@ -130,9 +128,7 @@ pub fn read_{{#toLower}}{{type}}{{/toLower}}<R: Read>(reader: &mut R) -> Result<
_{{#toLower}}{{name}}{{/toLower}} = reader.read_{{#toLower}}{{#toMsgPack}}{{toGraphQLType}}{{/toMsgPack}}{{/toLower}}(|reader| {
reader.read_{{#key}}{{#toLower}}{{#toMsgPack}}{{toGraphQLType}}{{/toMsgPack}}{{/toLower}}{{/key}}()?
}, |reader| {
{{#value}}
{{> deserialize_map_value_nobox}}
{{/value}}
})?;
{{/map}}
{{#enum}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ writer.write_{{#toLower}}{{#toMsgPack}}{{toGraphQLType}}{{/toMsgPack}}{{/toLower
writer.write_{{#toLower}}{{#toMsgPack}}{{toGraphQLType}}{{/toMsgPack}}{{/toLower}}(value, |writer, key| {
writer.write_{{#key}}{{#toLower}}{{#toMsgPack}}{{toGraphQLType}}{{/toMsgPack}}{{/toLower}}{{/key}}(key)
}, |writer, value| {
{{#value}}
{{> serialize_map_value}}
{{/value}}
})
{{/map}}
{{#enum}}
Expand Down
66 changes: 38 additions & 28 deletions packages/schema/parse/src/__tests__/parse-map-type.spec.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,42 @@
import { parseCurrentType, parseMapType, toGraphQLType } from "../extract/utils/map-utils";
import {
createMapDefinition,
createMapKeyDefinition,
createScalarDefinition,
createUnresolvedObjectOrEnumRef,
} from "../abi";
import {
parseCurrentType,
parseMapType,
toGraphQLType,
} from "../extract/utils/map-utils";

describe("parseMapType", () => {
test("Map<String, Int>", () => {
const result = parseMapType("Map<String, Int>");
expect(result).toMatchObject({
type: "Map<String, Int>",
key: {
type: "String",
},
value: {
type: "Int",
},
});
expect(result).toMatchObject(
createMapDefinition({
type: "Map<String, Int>",
key: createMapKeyDefinition({ type: "String", required: true }),
value: createScalarDefinition({ type: "Int" }),
})
);
});

test("Map<String, CustomType!>", () => {
const result = parseMapType("Map<String, CustomType!>");
expect(result).toMatchObject({
type: "Map<String, CustomType>",
key: {
type: "String",
},
value: {
type: "CustomType",
required: true,
},
});
expect(result).toMatchObject(
createMapDefinition({
type: "Map<String, CustomType>",
key: createMapKeyDefinition({
type: "String",
required: true,
}),
value: createUnresolvedObjectOrEnumRef({
type: "CustomType",
required: true,
}),
})
);
});

test("Map<Int, Array<String>!>", () => {
Expand All @@ -44,7 +55,7 @@ describe("parseMapType", () => {
type: "String",
},
required: true,
}
},
});
});

Expand All @@ -61,7 +72,7 @@ describe("parseMapType", () => {
type: "String",
},
required: true,
}
},
});
});

Expand All @@ -85,7 +96,7 @@ describe("parseMapType", () => {
},
required: true,
});
})
});

test("Map<CustomType, String!>", () => {
expect(() => parseMapType("Map<CustomType, String!>")).toThrow(
Expand All @@ -94,7 +105,6 @@ describe("parseMapType", () => {
});
});


describe("toGraphQLType", () => {
test("Map<String, Int>", () => {
const result = toGraphQLType("Map<String, Int>");
Expand All @@ -115,7 +125,7 @@ describe("toGraphQLType", () => {
const result = toGraphQLType("Array<String!>!");
expect(result).toBe("[String]");
});
})
});

describe("parseCurrentType", () => {
test("Map<String, Int>", () => {
Expand All @@ -132,7 +142,7 @@ describe("parseCurrentType", () => {
expect(result).toMatchObject({
currentType: "Map",
subType: "String, CustomType!",
required: false
required: false,
});
});

Expand Down Expand Up @@ -161,7 +171,7 @@ describe("parseCurrentType", () => {
subType: "String!, Map<String!, Int!>!",
required: true,
});
})
});

test("CustomType!", () => {
const result = parseCurrentType("CustomType!");
Expand All @@ -180,4 +190,4 @@ describe("parseCurrentType", () => {
required: false,
});
});
});
});
13 changes: 12 additions & 1 deletion packages/schema/parse/src/abi/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export function createAnyDefinition(args: {
scalar?: ScalarDefinition;
object?: ObjectRef;
enum?: EnumRef;
unresolvedObjectOrEnum?: UnresolvedObjectOrEnumRef;
}): AnyDefinition {
return {
...createGenericDefinition(args),
Expand All @@ -114,7 +115,9 @@ export function createAnyDefinition(args: {
scalar: args.scalar ? args.scalar : null,
object: args.object ? args.object : null,
enum: args.enum ? args.enum : null,
unresolvedObjectOrEnum: null,
unresolvedObjectOrEnum: args.unresolvedObjectOrEnum
? args.unresolvedObjectOrEnum
: null,
kind: DefinitionKind.Any,
};
}
Expand Down Expand Up @@ -237,6 +240,10 @@ export function createMapDefinition(args: {
args.value && isKind(args.value, DefinitionKind.EnumRef)
? (args.value as EnumRef)
: undefined,
unresolvedObjectOrEnum:
args.value && isKind(args.value, DefinitionKind.UnresolvedObjectOrEnum)
? (args.value as UnresolvedObjectOrEnumRef)
: undefined,
}),
key: args.key ? args.key : null,
value: args.value ? args.value : null,
Expand Down Expand Up @@ -276,6 +283,10 @@ export function createArrayDefinition(args: {
args.item && isKind(args.item, DefinitionKind.EnumRef)
? (args.item as EnumRef)
: undefined,
unresolvedObjectOrEnum:
args.item && isKind(args.item, DefinitionKind.UnresolvedObjectOrEnum)
? (args.item as UnresolvedObjectOrEnumRef)
: undefined,
}),
item: args.item ? args.item : null,
kind: DefinitionKind.Array,
Expand Down
51 changes: 51 additions & 0 deletions packages/schema/parse/src/transform/finalizePropertyDef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
GenericDefinition,
PropertyDefinition,
Abi,
MapDefinition,
AnyDefinition,
} from "../abi";

export const finalizePropertyDef = (abi: Abi): AbiTransforms => {
Expand Down Expand Up @@ -37,6 +39,7 @@ export function populatePropertyType(
} else if (property.enum) {
propertyType = property.enum;
} else if (property.map) {
populateMapType(property.map, abi);
propertyType = property.map;
} else {
throw Error("Property type is undefined, this should never happen.");
Expand All @@ -46,6 +49,54 @@ export function populatePropertyType(
property.required = propertyType.required;
}

function populateMapType(map: MapDefinition, abi: Abi) {
let baseTypeFound = false;

let currentType: AnyDefinition = map;
while (!baseTypeFound) {
if (currentType.map) {
currentType = currentType.map;
populateMapType(currentType as MapDefinition, abi);
} else if (currentType.array) {
currentType = currentType.array;
populateArrayType(currentType as ArrayDefinition, abi);
} else if (
currentType.scalar ||
currentType.object ||
currentType.enum ||
currentType.unresolvedObjectOrEnum
) {
baseTypeFound = true;
} else {
throw Error(
`This should never happen, MapDefinition is malformed.\n${JSON.stringify(
map,
null,
2
)}`
);
}
}

if (map.array) {
map.value = map.array;
} else if (map.unresolvedObjectOrEnum) {
map.value = resolveObjectOrEnumKind(map, abi);
} else if (map.scalar) {
map.value = map.scalar;
} else if (map.enum) {
map.value = map.enum;
} else if (map.map) {
map.value = map.map;
} else {
map.value = map.object;
}

if (!map.value) {
throw Error("Map isn't valid.");
}
}

function populateArrayType(array: ArrayDefinition, abi: Abi) {
let baseTypeFound = false;

Expand Down
5 changes: 4 additions & 1 deletion packages/schema/parse/src/transform/toGraphQLType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ export function toGraphQL(def: GenericDefinition, prefixed = false): string {
);
}
return applyRequired(
`Map<${toGraphQL(map.key, prefixed)}, ${anyToGraphQL(map, prefixed)}>`,
`Map<${toGraphQL(map.key, prefixed)}, ${toGraphQL(
map.value,
prefixed
)}>`,
map.required
);
}
Expand Down
7 changes: 7 additions & 0 deletions packages/test-cases/cases/bind/sanity/input/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ type Module @imports(
enumArray: [CustomEnum!]!
optEnumArray: [CustomEnum]
map: Map! @annotate(type: "Map<String!, Int!>!")
mapOfArr: Map! @annotate(type: "Map<String!, [Int!]!>!")
mapOfObj: Map! @annotate(type: "Map<String!, AnotherType!>!")
mapOfArrOfObj: Map! @annotate(type: "Map<String!, [AnotherType!]!>!")
): Int!

objectMethod(
Expand Down Expand Up @@ -116,6 +119,10 @@ type CustomType {
optEnum: CustomEnum
enumArray: [CustomEnum!]!
optEnumArray: [CustomEnum]
map: Map! @annotate(type: "Map<String!, Int!>!")
mapOfArr: Map! @annotate(type: "Map<String!, [Int!]!>!")
mapOfObj: Map! @annotate(type: "Map<String!, AnotherType!>!")
mapOfArrOfObj: Map! @annotate(type: "Map<String!, [AnotherType!]!>!")
}

type AnotherType {
Expand Down
Loading