Skip to content

Commit 52930d6

Browse files
authored
Merge pull request #1723 from polywrap/origin-0.10-dev
Origin 0.10.2 | /workflows/release-pr
2 parents 4eed65c + 089f62d commit 52930d6

13 files changed

Lines changed: 143 additions & 54 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# Polywrap Origin (0.10.2)
2+
## Bugs
3+
**`@polywrap/schema-bind`:**
4+
* [PR-1718](https://github.com/polywrap/toolchain/pull/1718) **`plugin/python` Enum Bindings Fix**
5+
* Enums are now properly displayed in Python plugins.
6+
17
# Polywrap Origin (0.10.1)
28
## Features
39
**`@polywrap/schema-bind`:**

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.10.1
1+
0.10.2

packages/cli/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
"rimraf": "3.0.2",
8282
"toml": "3.0.0",
8383
"typescript": "4.9.5",
84-
"yaml": "2.1.3",
84+
"yaml": "2.2.2",
8585
"yesno": "0.4.0"
8686
},
8787
"devDependencies": {

packages/js/manifests/polywrap/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"@polywrap/polywrap-manifest-schemas": "0.10.1",
2020
"jsonschema": "1.4.0",
2121
"semver": "7.5.0",
22-
"yaml": "2.1.3"
22+
"yaml": "2.2.2"
2323
},
2424
"devDependencies": {
2525
"@polywrap/os-js": "0.10.1",

packages/schema/bind/src/__tests__/test-cases.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import {
1515
import fs from "fs";
1616
import path from "path";
1717

18+
import { deepCopy } from "./utils";
19+
1820
describe("Polywrap Binding Test Suite", () => {
1921
const cases = fetchTestCases();
2022

@@ -38,7 +40,7 @@ describe("Polywrap Binding Test Suite", () => {
3840
};
3941

4042
const output = bindSchema({
41-
...testCase.input,
43+
...deepCopy(testCase.input),
4244
bindLanguage: language as BindLanguage,
4345
});
4446

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
export function deepCopy(obj: any): any {
2+
var copy;
3+
4+
// Handle the 3 simple types, and null or undefined
5+
if (null == obj || "object" != typeof obj) return obj;
6+
7+
// Handle Date
8+
if (obj instanceof Date) {
9+
copy = new Date();
10+
copy.setTime(obj.getTime());
11+
return copy;
12+
}
13+
14+
// Handle Array
15+
if (obj instanceof Array) {
16+
copy = [];
17+
for (var i = 0, len = obj.length; i < len; i++) {
18+
copy[i] = deepCopy(obj[i]);
19+
}
20+
return copy;
21+
}
22+
23+
// Handle Object
24+
if (obj instanceof Object) {
25+
copy = {};
26+
for (var attr in obj) {
27+
//@ts-ignore
28+
if (obj.hasOwnProperty(attr)) copy[attr] = deepCopy(obj[attr]);
29+
}
30+
return copy;
31+
}
32+
33+
throw new Error("Unable to copy obj! Its type isn't supported.");
34+
}

packages/schema/bind/src/bindings/python/plugin/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import * as Functions from "../functions";
33
import { GenerateBindingFn, renderTemplates } from "../..";
44
import { BindOptions, BindOutput } from "../../..";
5+
import { addEnumMembers } from "../transformers";
56

67
import {
78
transformAbi,
@@ -21,7 +22,7 @@ const templatePath = (subpath: string) =>
2122
path.join(__dirname, "./templates", subpath);
2223

2324
const sort = (obj: Record<string, unknown>) =>
24-
Object.keys(obj)
25+
Object.keys(obj || {})
2526
.sort()
2627
.reduce((map: Record<string, unknown>, key: string) => {
2728
if (typeof obj[key] === "object") {
@@ -71,6 +72,7 @@ function applyTransforms(abi: WrapAbi): WrapAbi {
7172
toPrefixedGraphQLType,
7273
methodParentPointers(),
7374
interfaceUris(),
75+
addEnumMembers,
7476
];
7577

7678
for (const transform of transforms) {

packages/schema/bind/src/bindings/python/plugin/templates/types-py.mustache

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from __future__ import annotations
44

55
from typing import TypedDict, Optional
6-
from enum import IntEnum, auto
6+
from enum import IntEnum
77

88
from polywrap_core import InvokerClient, Uri, UriPackageOrWrapper
99
from polywrap_msgpack import GenericMap
@@ -36,13 +36,13 @@ from polywrap_msgpack import GenericMap
3636
### Enums START ###
3737
{{#enumTypes}}
3838
class {{#detectKeyword}}{{#toUpper}}{{type}}{{/toUpper}}{{/detectKeyword}}(IntEnum):
39-
{{#constants}}
40-
{{#detectKeyword}}{{.}}{{/detectKeyword}} = auto(), "{{.}}"
41-
{{/constants}}
39+
{{#members}}
40+
{{#detectKeyword}}{{name}}{{/detectKeyword}} = {{value}}, "{{value}}", "{{name}}"
41+
{{/members}}
4242

4343
def __new__(cls, value: int, *aliases: str):
4444
obj = int.__new__(cls)
45-
obj._value_ = value - 1
45+
obj._value_ = value
4646
for alias in aliases:
4747
cls._value2member_map_[alias] = obj
4848
return obj
@@ -68,13 +68,13 @@ class {{#detectKeyword}}{{#toUpper}}{{type}}{{/toUpper}}{{/detectKeyword}}(IntEn
6868
{{#importedEnumTypes}}
6969
# URI: "{{uri}}" #
7070
class {{#detectKeyword}}{{#toUpper}}{{type}}{{/toUpper}}{{/detectKeyword}}(IntEnum):
71-
{{#constants}}
72-
{{#detectKeyword}}{{.}}{{/detectKeyword}} = auto()
73-
{{/constants}}
71+
{{#members}}
72+
{{#detectKeyword}}{{name}}{{/detectKeyword}} = {{value}}, "{{value}}", "{{name}}"
73+
{{/members}}
7474

7575
def __new__(cls, value: int, *aliases: str):
7676
obj = int.__new__(cls)
77-
obj._value_ = value - 1
77+
obj._value_ = value
7878
for alias in aliases:
7979
cls._value2member_map_[alias] = obj
8080
return obj
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { AbiTransforms } from "@polywrap/schema-parse";
2+
import { EnumDefinition } from "@polywrap/wrap-manifest-types-js";
3+
4+
export const addEnumMembers: AbiTransforms = {
5+
enter: {
6+
// eslint-disable-next-line @typescript-eslint/naming-convention
7+
EnumDefinition: (def: EnumDefinition): EnumDefinition => {
8+
if (!def.constants) {
9+
return { ...def };
10+
}
11+
12+
const members: Array<Record<string, unknown>> = [];
13+
let value = 0;
14+
15+
for (const constant of def.constants) {
16+
members.push({
17+
name: constant,
18+
value: value,
19+
});
20+
value += 1;
21+
}
22+
23+
return {
24+
...def,
25+
members,
26+
} as EnumDefinition;
27+
},
28+
},
29+
};

packages/test-cases/cases/bind/sanity/output/plugin-py/types.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from __future__ import annotations
44

55
from typing import TypedDict, Optional
6-
from enum import IntEnum, auto
6+
from enum import IntEnum
77

88
from polywrap_core import InvokerClient, Uri, UriPackageOrWrapper
99
from polywrap_msgpack import GenericMap
@@ -84,23 +84,23 @@
8484

8585
### Enums START ###
8686
class CustomEnum(IntEnum):
87-
STRING = auto(), "STRING"
88-
BYTES = auto(), "BYTES"
87+
STRING = 0, "0", "STRING"
88+
BYTES = 1, "1", "BYTES"
8989

9090
def __new__(cls, value: int, *aliases: str):
9191
obj = int.__new__(cls)
92-
obj._value_ = value - 1
92+
obj._value_ = value
9393
for alias in aliases:
9494
cls._value2member_map_[alias] = obj
9595
return obj
9696

9797
class While(IntEnum):
98-
r_for = auto(), "for"
99-
r_in = auto(), "in"
98+
r_for = 0, "0", "for"
99+
r_in = 1, "1", "in"
100100

101101
def __new__(cls, value: int, *aliases: str):
102102
obj = int.__new__(cls)
103-
obj._value_ = value - 1
103+
obj._value_ = value
104104
for alias in aliases:
105105
cls._value2member_map_[alias] = obj
106106
return obj
@@ -132,24 +132,24 @@ def __new__(cls, value: int, *aliases: str):
132132

133133
# URI: "testimport.uri.eth" #
134134
class TestImportEnum(IntEnum):
135-
STRING = auto()
136-
BYTES = auto()
135+
STRING = 0, "0", "STRING"
136+
BYTES = 1, "1", "BYTES"
137137

138138
def __new__(cls, value: int, *aliases: str):
139139
obj = int.__new__(cls)
140-
obj._value_ = value - 1
140+
obj._value_ = value
141141
for alias in aliases:
142142
cls._value2member_map_[alias] = obj
143143
return obj
144144

145145
# URI: "testimport.uri.eth" #
146146
class TestImportEnumReturn(IntEnum):
147-
STRING = auto()
148-
BYTES = auto()
147+
STRING = 0, "0", "STRING"
148+
BYTES = 1, "1", "BYTES"
149149

150150
def __new__(cls, value: int, *aliases: str):
151151
obj = int.__new__(cls)
152-
obj._value_ = value - 1
152+
obj._value_ = value
153153
for alias in aliases:
154154
cls._value2member_map_[alias] = obj
155155
return obj

0 commit comments

Comments
 (0)