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: enum in python plugin bindings #1718

Merged
merged 6 commits into from
Apr 24, 2023
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
4 changes: 3 additions & 1 deletion packages/schema/bind/src/__tests__/test-cases.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
import fs from "fs";
import path from "path";

import { deepCopy } from "./utils";

describe("Polywrap Binding Test Suite", () => {
const cases = fetchTestCases();

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

const output = bindSchema({
...testCase.input,
...deepCopy(testCase.input),
bindLanguage: language as BindLanguage,
});

Expand Down
34 changes: 34 additions & 0 deletions packages/schema/bind/src/__tests__/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
export function deepCopy(obj: any): any {
var copy;

// Handle the 3 simple types, and null or undefined
if (null == obj || "object" != typeof obj) return obj;

// Handle Date
if (obj instanceof Date) {
copy = new Date();
copy.setTime(obj.getTime());
return copy;
}

// Handle Array
if (obj instanceof Array) {
copy = [];
for (var i = 0, len = obj.length; i < len; i++) {
copy[i] = deepCopy(obj[i]);
}
return copy;
}

// Handle Object
if (obj instanceof Object) {
copy = {};
for (var attr in obj) {
//@ts-ignore
if (obj.hasOwnProperty(attr)) copy[attr] = deepCopy(obj[attr]);
}
return copy;
}

throw new Error("Unable to copy obj! Its type isn't supported.");
}
4 changes: 3 additions & 1 deletion packages/schema/bind/src/bindings/python/plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import * as Functions from "../functions";
import { GenerateBindingFn, renderTemplates } from "../..";
import { BindOptions, BindOutput } from "../../..";
import { addEnumMembers } from "../transformers";

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

const sort = (obj: Record<string, unknown>) =>
Object.keys(obj)
Object.keys(obj || {})
.sort()
.reduce((map: Record<string, unknown>, key: string) => {
if (typeof obj[key] === "object") {
Expand Down Expand Up @@ -71,6 +72,7 @@ function applyTransforms(abi: WrapAbi): WrapAbi {
toPrefixedGraphQLType,
methodParentPointers(),
interfaceUris(),
addEnumMembers,
];

for (const transform of transforms) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from __future__ import annotations

from typing import TypedDict, Optional
from enum import IntEnum, auto
from enum import IntEnum

from polywrap_core import InvokerClient, Uri, UriPackageOrWrapper
from polywrap_msgpack import GenericMap
Expand Down Expand Up @@ -36,13 +36,13 @@ from polywrap_msgpack import GenericMap
### Enums START ###
{{#enumTypes}}
class {{#detectKeyword}}{{#toUpper}}{{type}}{{/toUpper}}{{/detectKeyword}}(IntEnum):
{{#constants}}
{{#detectKeyword}}{{.}}{{/detectKeyword}} = auto(), "{{.}}"
{{/constants}}
{{#members}}
{{#detectKeyword}}{{name}}{{/detectKeyword}} = {{value}}, "{{value}}", "{{name}}"
{{/members}}

def __new__(cls, value: int, *aliases: str):
obj = int.__new__(cls)
obj._value_ = value - 1
obj._value_ = value
for alias in aliases:
cls._value2member_map_[alias] = obj
return obj
Expand All @@ -68,13 +68,13 @@ class {{#detectKeyword}}{{#toUpper}}{{type}}{{/toUpper}}{{/detectKeyword}}(IntEn
{{#importedEnumTypes}}
# URI: "{{uri}}" #
class {{#detectKeyword}}{{#toUpper}}{{type}}{{/toUpper}}{{/detectKeyword}}(IntEnum):
{{#constants}}
{{#detectKeyword}}{{.}}{{/detectKeyword}} = auto()
{{/constants}}
{{#members}}
{{#detectKeyword}}{{name}}{{/detectKeyword}} = {{value}}, "{{value}}", "{{name}}"
{{/members}}

def __new__(cls, value: int, *aliases: str):
obj = int.__new__(cls)
obj._value_ = value - 1
obj._value_ = value
for alias in aliases:
cls._value2member_map_[alias] = obj
return obj
Expand Down
29 changes: 29 additions & 0 deletions packages/schema/bind/src/bindings/python/transformers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { AbiTransforms } from "@polywrap/schema-parse";
import { EnumDefinition } from "@polywrap/wrap-manifest-types-js";

export const addEnumMembers: AbiTransforms = {
enter: {
// eslint-disable-next-line @typescript-eslint/naming-convention
EnumDefinition: (def: EnumDefinition): EnumDefinition => {
if (!def.constants) {
return { ...def };
}

const members: Array<Record<string, unknown>> = [];
let value = 0;

for (const constant of def.constants) {
members.push({
name: constant,
value: value,
});
value += 1;
}

return {
...def,
members,
} as EnumDefinition;
},
},
};
26 changes: 13 additions & 13 deletions packages/test-cases/cases/bind/sanity/output/plugin-py/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from __future__ import annotations

from typing import TypedDict, Optional
from enum import IntEnum, auto
from enum import IntEnum

from polywrap_core import InvokerClient, Uri, UriPackageOrWrapper
from polywrap_msgpack import GenericMap
Expand Down Expand Up @@ -84,23 +84,23 @@

### Enums START ###
class CustomEnum(IntEnum):
STRING = auto(), "STRING"
BYTES = auto(), "BYTES"
STRING = 0, "0", "STRING"
BYTES = 1, "1", "BYTES"

def __new__(cls, value: int, *aliases: str):
obj = int.__new__(cls)
obj._value_ = value - 1
obj._value_ = value
for alias in aliases:
cls._value2member_map_[alias] = obj
return obj

class While(IntEnum):
r_for = auto(), "for"
r_in = auto(), "in"
r_for = 0, "0", "for"
r_in = 1, "1", "in"

def __new__(cls, value: int, *aliases: str):
obj = int.__new__(cls)
obj._value_ = value - 1
obj._value_ = value
for alias in aliases:
cls._value2member_map_[alias] = obj
return obj
Expand Down Expand Up @@ -132,24 +132,24 @@ def __new__(cls, value: int, *aliases: str):

# URI: "testimport.uri.eth" #
class TestImportEnum(IntEnum):
STRING = auto()
BYTES = auto()
STRING = 0, "0", "STRING"
BYTES = 1, "1", "BYTES"

def __new__(cls, value: int, *aliases: str):
obj = int.__new__(cls)
obj._value_ = value - 1
obj._value_ = value
for alias in aliases:
cls._value2member_map_[alias] = obj
return obj

# URI: "testimport.uri.eth" #
class TestImportEnumReturn(IntEnum):
STRING = auto()
BYTES = auto()
STRING = 0, "0", "STRING"
BYTES = 1, "1", "BYTES"

def __new__(cls, value: int, *aliases: str):
obj = int.__new__(cls)
obj._value_ = value - 1
obj._value_ = value
for alias in aliases:
cls._value2member_map_[alias] = obj
return obj
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from __future__ import annotations

from typing import TypedDict, Optional
from enum import IntEnum, auto
from enum import IntEnum

from polywrap_core import InvokerClient, Uri, UriPackageOrWrapper
from polywrap_msgpack import GenericMap
Expand Down
58 changes: 37 additions & 21 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2670,9 +2670,9 @@
integrity sha512-wH6Tu9mbiOt0n5EvdoWy0VGQaJMHfLIxY/6wS0xLC7CV1taM6gESEzcYy0ZlWvxxiiljYvfDIvz4hHbUUDRlhw==

"@types/node@*", "@types/node@^18.14.6":
version "18.15.11"
resolved "https://registry.yarnpkg.com/@types/node/-/node-18.15.11.tgz#b3b790f09cb1696cffcec605de025b088fa4225f"
integrity sha512-E5Kwq2n4SbMzQOn6wnmBjuK9ouqlURrcZDVfbo9ftDDTFt3nk7ZKK4GMOzoYgnpQJKcxwQw+lGaBvvlMo0qN/Q==
version "18.16.0"
resolved "https://registry.yarnpkg.com/@types/node/-/node-18.16.0.tgz#4668bc392bb6938637b47e98b1f2ed5426f33316"
integrity sha512-BsAaKhB+7X+H4GnSjGhJG9Qi8Tw+inU9nJDwmD5CgOmBLEI6ArdhikpLX7DjbjDRDTbqZzU2LSQNZg8WGPiSZQ==

"@types/normalize-package-data@^2.4.0":
version "2.4.1"
Expand Down Expand Up @@ -3650,9 +3650,9 @@ camelcase@^6.0.0:
integrity sha512-Gmy6FhYlCY7uOElZUSbxo2UCDH8owEk996gkbrpsgGtrJLM3J7jGxl9Ic7Qwwj4ivOE5AWZWRMecDdF7hqGjFA==

caniuse-lite@^1.0.30001449:
version "1.0.30001480"
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001480.tgz#9bbd35ee44c2480a1e3a3b9f4496f5066817164a"
integrity sha512-q7cpoPPvZYgtyC4VaBSN0Bt+PJ4c4EYRf0DrduInOz2SkFpHD5p3LnvEpqBp7UnJn+8x1Ogl1s38saUxe+ihQQ==
version "1.0.30001481"
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001481.tgz#f58a717afe92f9e69d0e35ff64df596bfad93912"
integrity sha512-KCqHwRnaa1InZBtqXzP98LPg0ajCVujMKjqKDhZEthIpAsJl/YEIa3YvXjGXPVqzZVguccuu7ga9KOE1J9rKPQ==

capture-exit@^2.0.0:
version "2.0.0"
Expand Down Expand Up @@ -4458,9 +4458,9 @@ electron-fetch@^1.7.2:
encoding "^0.1.13"

electron-to-chromium@^1.4.284:
version "1.4.368"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.368.tgz#75901f97d3e23da2e66feb1e61fbb8e70ac96430"
integrity sha512-e2aeCAixCj9M7nJxdB/wDjO6mbYX+lJJxSJCXDzlr5YPGYVofuJwGN9nKg2o6wWInjX6XmxRinn3AeJMK81ltw==
version "1.4.369"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.369.tgz#a98d838cdd79be4471cd04e9b4dffe891d037874"
integrity sha512-LfxbHXdA/S+qyoTEA4EbhxGjrxx7WK2h6yb5K2v0UCOufUKX+VZaHbl3svlzZfv9sGseym/g3Ne4DpsgRULmqg==

elliptic@6.5.4:
version "6.5.4"
Expand Down Expand Up @@ -4538,7 +4538,7 @@ error-ex@^1.2.0, error-ex@^1.3.1:
dependencies:
is-arrayish "^0.2.1"

es-abstract@^1.19.0, es-abstract@^1.20.4:
es-abstract@^1.19.0, es-abstract@^1.20.4, es-abstract@^1.21.2:
version "1.21.2"
resolved "https://registry.yarnpkg.com/es-abstract/-/es-abstract-1.21.2.tgz#a56b9695322c8a185dc25975aa3b8ec31d0e7eff"
integrity sha512-y/B5POM2iBnIxCiernH1G7rC9qQoM77lLIMQLuob0zhp8C56Po81+2Nj0WFKnd0pNReDTnkYryc+zhOzpEIROg==
Expand Down Expand Up @@ -6401,6 +6401,11 @@ isarray@1.0.0, isarray@^1.0.0, isarray@~1.0.0:
resolved "https://registry.yarnpkg.com/isarray/-/isarray-1.0.0.tgz#bb935d48582cba168c06834957a54a3e07124f11"
integrity sha512-VLghIWNM6ELQzo7zwmcg0NmTVyWKYjvIeM83yjp0wRDTmUnrM678fQbcKBo6n2CJEF0szoG//ytg+TKla89ALQ==

isarray@^2.0.5:
version "2.0.5"
resolved "https://registry.yarnpkg.com/isarray/-/isarray-2.0.5.tgz#8af1e4c1221244cc62459faf38940d4e644a5723"
integrity sha512-xHjhDr3cNBK0BzdUJSPXZntQUx/mwMS5Rw4A7lPJ90XGAO6ISP/ePDNuo0vhqOZU+UD5JoodwCAAoZQd3FeAKw==

isexe@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/isexe/-/isexe-2.0.0.tgz#e8fbf374dc556ff8947a10dcb0572d633f2cfa10"
Expand Down Expand Up @@ -8284,14 +8289,15 @@ object.assign@^4.1.4:
object-keys "^1.1.1"

object.getownpropertydescriptors@^2.0.3:
version "2.1.5"
resolved "https://registry.yarnpkg.com/object.getownpropertydescriptors/-/object.getownpropertydescriptors-2.1.5.tgz#db5a9002489b64eef903df81d6623c07e5b4b4d3"
integrity sha512-yDNzckpM6ntyQiGTik1fKV1DcVDRS+w8bvpWNCBanvH5LfRX9O8WTHqQzG4RZwRAM4I0oU7TV11Lj5v0g20ibw==
version "2.1.6"
resolved "https://registry.yarnpkg.com/object.getownpropertydescriptors/-/object.getownpropertydescriptors-2.1.6.tgz#5e5c384dd209fa4efffead39e3a0512770ccc312"
integrity sha512-lq+61g26E/BgHv0ZTFgRvi7NMEPuAxLkFU7rukXjc/AlwH4Am5xXVnIXy3un1bg/JPbXHrixRkK1itUzzPiIjQ==
dependencies:
array.prototype.reduce "^1.0.5"
call-bind "^1.0.2"
define-properties "^1.1.4"
es-abstract "^1.20.4"
define-properties "^1.2.0"
es-abstract "^1.21.2"
safe-array-concat "^1.0.0"

object.pick@^1.3.0:
version "1.3.0"
Expand Down Expand Up @@ -8710,9 +8716,9 @@ prettier@2.2.1:
integrity sha512-PqyhM2yCjg/oKkFPtTGUojv7gnZAoG80ttl45O6x2Ug/rMJw4wcc9k6aaf2hibP7BGVCCM33gZoGjyvt9mm16Q==

prettier@^2.6.2:
version "2.8.7"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.8.7.tgz#bb79fc8729308549d28fe3a98fce73d2c0656450"
integrity sha512-yPngTo3aXUUmyuTjeTUT75txrf+aMh9FiD7q9ZE/i6r0bPb22g4FsE6Y338PQX1bmfy08i9QQCB7/rcUAVntfw==
version "2.8.8"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.8.8.tgz#e8c5d7e98a4305ffe3de2e1fc4aca1a71c28b1da"
integrity sha512-tdN8qQGvNjw4CHbY+XXk0JgCXn9QiF21a55rBe5LJAU+kDyC4WQn4+awm2Xfk2lQMk5fKup9XgzTZtGkjBdP9Q==

pretty-format@^25.2.1, pretty-format@^25.5.0:
version "25.5.0"
Expand Down Expand Up @@ -9285,6 +9291,16 @@ rxjs@^6.6.0:
dependencies:
tslib "^1.9.0"

safe-array-concat@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/safe-array-concat/-/safe-array-concat-1.0.0.tgz#2064223cba3c08d2ee05148eedbc563cd6d84060"
integrity sha512-9dVEFruWIsnie89yym+xWTAYASdpw3CJV7Li/6zBewGf9z2i1j31rP6jnY0pHEO4QZh6N0K11bFjWmdR8UGdPQ==
dependencies:
call-bind "^1.0.2"
get-intrinsic "^1.2.0"
has-symbols "^1.0.3"
isarray "^2.0.5"

safe-buffer@^5.0.1, safe-buffer@^5.1.2, safe-buffer@^5.2.1, safe-buffer@~5.2.0:
version "5.2.1"
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.2.1.tgz#1eaf9fa9bdb1fdd4ec75f58f9cdb4e6b7827eec6"
Expand Down Expand Up @@ -10624,9 +10640,9 @@ which-boxed-primitive@^1.0.2:
is-symbol "^1.0.3"

which-module@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/which-module/-/which-module-2.0.0.tgz#d9ef07dce77b9902b8a3a8fa4b31c3e3f7e6e87a"
integrity sha512-B+enWhmw6cjfVC7kS8Pj9pCrKSc5txArRyaYGe088shv/FGWH+0Rjx/xPgtsWfsUtS27FkP697E4DDhgrgoc0Q==
version "2.0.1"
resolved "https://registry.yarnpkg.com/which-module/-/which-module-2.0.1.tgz#776b1fe35d90aebe99e8ac15eb24093389a4a409"
integrity sha512-iBdZ57RDvnOR9AGBhML2vFZf7h8vmBjhoaZqODJBFWHVtKkDmKuHai3cx5PgVMrX5YDNp27AofYbAwctSS+vhQ==

which-typed-array@^1.1.2, which-typed-array@^1.1.9:
version "1.1.9"
Expand Down