-
Notifications
You must be signed in to change notification settings - Fork 97
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
feat: Allow to decode t as opt t #716
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -914,10 +914,25 @@ export class OptClass<T> extends ConstructType<[T] | []> { | |
typeTable.add(this, concat(opCode, buffer)); | ||
} | ||
|
||
public checkType(t: Type): Type { | ||
try { | ||
return super.checkType(t); | ||
} catch (e) { | ||
// try to coerce t to opt t | ||
if (!(t instanceof OptClass) && !(this._type instanceof OptClass)) { | ||
if (this._type.checkType(t)) { | ||
return t; | ||
} | ||
} | ||
// rethrow if opt coercion is not applicable | ||
throw e; | ||
} | ||
} | ||
|
||
public decodeValue(b: Pipe, t: Type): [T] | [] { | ||
const opt = this.checkType(t); | ||
if (!(opt instanceof OptClass)) { | ||
throw new Error('Not an option type'); | ||
return [this._type.decodeValue(b, opt)]; | ||
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. I did some tests, and this should be a little bit different
Otherwise this will throw another error 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. Thanks. Could you provide a full test-case of an input the crashes with the current code but succeeds using your suggested change? |
||
} | ||
switch (safeReadUint8(b)) { | ||
case 0: | ||
|
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.
The formatter insists on this change.