Skip to content

Commit

Permalink
Add discriminator field to enum variant nodes (#209)
Browse files Browse the repository at this point in the history
  • Loading branch information
lorisleiva authored Apr 11, 2024
1 parent cf6b54a commit c528819
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/poor-waves-matter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@metaplex-foundation/kinobi": minor
---

Add discriminator field to enum variant nodes
10 changes: 8 additions & 2 deletions src/nodes/typeNodes/EnumEmptyVariantTypeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,23 @@ export interface EnumEmptyVariantTypeNode {

// Data.
readonly name: MainCaseString;
readonly discriminator?: number;
}

export function enumEmptyVariantTypeNode(
name: string
name: string,
discriminator?: number
): EnumEmptyVariantTypeNode {
if (!name) {
throw new InvalidKinobiTreeError(
'EnumEmptyVariantTypeNode must have a name.'
);
}
return { kind: 'enumEmptyVariantTypeNode', name: mainCase(name) };
return {
kind: 'enumEmptyVariantTypeNode',
name: mainCase(name),
discriminator,
};
}

export function enumEmptyVariantTypeNodeFromIdl(
Expand Down
14 changes: 12 additions & 2 deletions src/nodes/typeNodes/EnumStructVariantTypeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,27 @@ export interface EnumStructVariantTypeNode<

// Data.
readonly name: MainCaseString;
readonly discriminator?: number;
}

export function enumStructVariantTypeNode<
TStruct extends NestedTypeNode<StructTypeNode>,
>(name: string, struct: TStruct): EnumStructVariantTypeNode<TStruct> {
>(
name: string,
struct: TStruct,
discriminator?: number
): EnumStructVariantTypeNode<TStruct> {
if (!name) {
throw new InvalidKinobiTreeError(
'EnumStructVariantTypeNode must have a name.'
);
}
return { kind: 'enumStructVariantTypeNode', name: mainCase(name), struct };
return {
kind: 'enumStructVariantTypeNode',
name: mainCase(name),
struct,
discriminator,
};
}

export function enumStructVariantTypeNodeFromIdl(
Expand Down
14 changes: 12 additions & 2 deletions src/nodes/typeNodes/EnumTupleVariantTypeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,27 @@ export interface EnumTupleVariantTypeNode<

// Data.
readonly name: MainCaseString;
readonly discriminator?: number;
}

export function enumTupleVariantTypeNode<
TTuple extends NestedTypeNode<TupleTypeNode>,
>(name: string, tuple: TTuple): EnumTupleVariantTypeNode<TTuple> {
>(
name: string,
tuple: TTuple,
discriminator?: number
): EnumTupleVariantTypeNode<TTuple> {
if (!name) {
throw new InvalidKinobiTreeError(
'EnumTupleVariantTypeNode must have a name.'
);
}
return { kind: 'enumTupleVariantTypeNode', name: mainCase(name), tuple };
return {
kind: 'enumTupleVariantTypeNode',
name: mainCase(name),
tuple,
discriminator,
};
}

export function enumTupleVariantTypeNodeFromIdl(
Expand Down

0 comments on commit c528819

Please sign in to comment.