-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Add type alias support #2637
Add type alias support #2637
Conversation
@acheroncrypto is attempting to deploy a commit to the coral-xyz Team on Vercel. A member of the Team first needs to authorize 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.
This is awesome! I left a couple comments but implementation is solid.
Having this capability in Anchor is going to be super helpful.
Some(Ok(IdlTypeDefinition { | ||
name, | ||
generics: None, | ||
docs: doc, | ||
ty: IdlTypeDefinitionTy::Alias { value }, | ||
})) |
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 wouldn't support type aliases with lifetimes right?
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.
It supports the types that have FromStr
implementation. It wouldn't support lifetimes because the IDL implementation itself doesn't support it.
pub fn type_alias( | ||
ctx: Context<TypeAlias>, | ||
type_alias_u8: TypeAliasU8, | ||
type_alias_u8_array: TypeAliasU8Array, | ||
type_alias_struct: TypeAliasStruct, | ||
) -> Result<()> { | ||
ctx.accounts.account.type_alias_u8 = type_alias_u8; | ||
ctx.accounts.account.type_alias_u8_array = type_alias_u8_array; | ||
ctx.accounts.account.type_alias_struct = type_alias_struct; | ||
Ok(()) | ||
} |
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.
Nice 🤌🏼
case "alias": { | ||
return IdlCoder.fieldLayout( | ||
{ type: typeDef.type.value, name: typeDef.name }, | ||
types | ||
); | ||
} |
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.
Looks like you dropped the catch-all else
but didn't include a default
case? ie:
default: {
throw new Error(`Unknown type kind: ${typeDef}`);
}
The original is also spelled wrong lol (type kint
)
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.
Yes, I've dropped it because it should be unreachable, i.e. the value we are checking is generated from the IDL so it should never be outside of these specific cases.
Problem
Type aliases are not supported.
Summary of Changes
Example
Program:
IDL:
TypeScript:
Closes #1300, resolves #1632.