-
Notifications
You must be signed in to change notification settings - Fork 100
feat: Allow deserialization of candid values with unknown types #555
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
Merged
Merged
Changes from 9 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
633dc60
Add IDL.Unknown for deserializing unknown types
9daf26e
Reformatting with prettier
f9bd596
More tests
8f27242
Add changelog entry
976fe65
Merge branch 'main' into frederik/idl-unknown
92531d2
Add test for serializing unknown
2a1705d
Move helper to top
570222b
Add test for service
7de395f
Add unit test for func
64ee3a6
Update changelog.html
krpeacock 11f50f0
Update packages/candid/src/idl.ts
krpeacock 278e1f7
Merge branch 'main' into frederik/idl-unknown
krpeacock 2c90af5
Merge branch 'main' into frederik/idl-unknown
krpeacock File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -280,6 +280,76 @@ export class EmptyClass extends PrimitiveType<never> { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Represents an IDL Unknown, a placeholder type for deserialization only. | ||
| * When decoding a value as Unknown, all fields will be retained but the names are only available in | ||
| * hashed form. | ||
| * A deserialized unknown will offer it's actual type by calling the `type()` function. | ||
| * Unknown cannot be serialized and attempting to do so will throw an error. | ||
| */ | ||
| export class UnknownClass extends Type { | ||
| public checkType(t: Type): Type { | ||
| throw new Error('Method not implemented for unknown.'); | ||
| } | ||
|
|
||
| public accept<D, R>(v: Visitor<D, R>, d: D): R { | ||
| throw v.visitType(this, d); | ||
| } | ||
|
|
||
| public covariant(x: any): x is any { | ||
| return false; | ||
| } | ||
|
|
||
| public encodeValue(): never { | ||
| throw new Error('Unknown cannot appear as a function argument'); | ||
| } | ||
|
|
||
| public valueToString(): never { | ||
| throw new Error('Unknown cannot appear as a value'); | ||
| } | ||
|
|
||
| public encodeType(): never { | ||
| throw new Error('Unknown cannot be serialized'); | ||
| } | ||
|
|
||
| public decodeValue(b: Pipe, t: Type): any { | ||
| let decodedValue = t.decodeValue(b, t); | ||
|
|
||
| if (Object(decodedValue) !== decodedValue) { | ||
| // decodedValue is primitive. Box it, otherwise we cannot add the type() function. | ||
| // The type() function is important for primitives because otherwise we cannot tell apart the | ||
| // different number types. | ||
| decodedValue = Object(decodedValue); | ||
| } | ||
|
|
||
| let typeFunc; | ||
| if (t instanceof RecClass) { | ||
| typeFunc = () => t.getType(); | ||
| } else { | ||
| typeFunc = () => t; | ||
| } | ||
| // Do not use 'decodedValue.type = typeFunc' because this would lead to an enumerable property | ||
| // 'type' which means it would be serialized if the value would be candid encoded again. | ||
| // This in turn leads to problems if the decoded value is a variant because these values are | ||
| // only allowed to have a single property. | ||
| Object.defineProperty(decodedValue, 'type', { | ||
| value: typeFunc, | ||
| writable: true, | ||
| enumerable: false, | ||
| configurable: true, | ||
|
Comment on lines
+337
to
+339
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are these properties used? |
||
| }); | ||
| return decodedValue; | ||
| } | ||
|
|
||
| protected _buildTypeTableImpl(): void { | ||
| throw new Error('Unknown cannot be serialized'); | ||
| } | ||
|
|
||
| get name() { | ||
| return 'Unknown'; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Represents an IDL Bool | ||
| */ | ||
|
|
@@ -1562,6 +1632,7 @@ export type InterfaceFactory = (idl: { | |
| IDL: { | ||
| Empty: EmptyClass; | ||
| Reserved: ReservedClass; | ||
| Unknown: UnknownClass; | ||
| Bool: BoolClass; | ||
| Null: NullClass; | ||
| Text: TextClass; | ||
|
|
@@ -1598,6 +1669,7 @@ export type InterfaceFactory = (idl: { | |
| // Export Types instances. | ||
| export const Empty = new EmptyClass(); | ||
| export const Reserved = new ReservedClass(); | ||
| export const Unknown = new UnknownClass(); | ||
|
krpeacock marked this conversation as resolved.
|
||
| export const Bool = new BoolClass(); | ||
| export const Null = new NullClass(); | ||
| export const Text = new TextClass(); | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.