-
-
Notifications
You must be signed in to change notification settings - Fork 76
Reconstructable deterministic constrids #272
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
Reconstructable deterministic constrids #272
Conversation
a5b2245
to
955bab8
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A small conflict otherwise lgtm.
pycardano/plutus.py
Outdated
def id_map(cls, skip_constructor=False): | ||
""" | ||
Constructs a unique representation of a PlutusData type definition. | ||
Intended for automatic constructor generation. | ||
""" | ||
if cls == bytes: | ||
return "bytes" | ||
if cls == int: | ||
return "int" | ||
if cls == RawCBOR or cls == RawPlutusData or cls == Datum: | ||
return "any" | ||
if cls == IndefiniteList: | ||
return "list" | ||
if hasattr(cls, "__origin__"): | ||
origin = getattr(cls, "__origin__") | ||
if origin == list: | ||
prefix = "list" | ||
elif origin == dict: | ||
prefix = "map" | ||
elif origin == typing.Union: | ||
prefix = "union" | ||
else: | ||
raise TypeError( | ||
f"Unexpected parameterized type for automatic constructor generation: {cls}" | ||
) | ||
return prefix + "<" + ",".join(id_map(a) for a in cls.__args__) + ">" | ||
if issubclass(cls, PlutusData): | ||
return ( | ||
"cons[" | ||
+ cls.__name__ | ||
+ "](" | ||
+ (str(cls.CONSTR_ID) if not skip_constructor else "_") | ||
+ ";" | ||
+ ",".join(f.name + ":" + id_map(f.type) for f in fields(cls)) | ||
+ ")" | ||
) | ||
raise TypeError(f"Unexpected type for automatic constructor generation: {cls}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is nice. Is there a language-agnostic documentation for it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No. Basically, this is the reference implementation for this kind of constructor IDs. I could write up a documentation, either in this document (where?), OpShin or in a dedicated CIP
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will leave you to decide. For me, because this can potentially get adopted by other languages, I think a dedicated CIP is the best choice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have added a CIP here: cardano-foundation/CIPs#608
Tried merging from main branch, but failed with this error:
Looks like |
798c1e3
to
2809b34
Compare
I have added support for |
Codecov Report
❗ Your organization needs to install the Codecov GitHub app to enable full functionality. @@ Coverage Diff @@
## main #272 +/- ##
==========================================
- Coverage 84.75% 84.74% -0.02%
==========================================
Files 26 26
Lines 3044 3067 +23
Branches 740 750 +10
==========================================
+ Hits 2580 2599 +19
- Misses 348 350 +2
- Partials 116 118 +2
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks!
This resolves #271
Please reach out if there are any remaining questions as to the necessity or implementation of this.