-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use name transformers for enum variant and data enum discriminator (#140
- Loading branch information
1 parent
3bfeb8c
commit 69a9f3e
Showing
11 changed files
with
216 additions
and
193 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@metaplex-foundation/kinobi': patch | ||
--- | ||
|
||
Use name transformers for enum variant and data enum discriminator |
18 changes: 9 additions & 9 deletions
18
src/renderers/js-experimental/fragments/typeDataEnumHelpers.njk
This file contains 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 |
---|---|---|
@@ -1,23 +1,23 @@ | ||
// Data Enum Helpers. | ||
{% for variant in typeNode.variants %} | ||
{% if variant.kind === 'enumStructVariantTypeNode' %} | ||
export function {{ dataEnumFunction }}(kind: '{{ variant.name | pascalCase }}', data: GetDataEnumKindContent<{{ looseName }}, '{{ variant.name | pascalCase }}'>): GetDataEnumKind<{{ looseName }}, '{{ variant.name | pascalCase }}'>; | ||
export function {{ dataEnumFunction }}(kind: '{{ getVariant(variant.name) }}', data: GetDataEnumKindContent<{{ looseName }}, '{{ getVariant(variant.name) }}'>): GetDataEnumKind<{{ looseName }}, '{{ getVariant(variant.name) }}'>; | ||
{% elif variant.kind === 'enumTupleVariantTypeNode' %} | ||
export function {{ dataEnumFunction }}(kind: '{{ variant.name | pascalCase }}', data: GetDataEnumKindContent<{{ looseName }}, '{{ variant.name | pascalCase }}'>['fields']): GetDataEnumKind<{{ looseName }}, '{{ variant.name | pascalCase }}'>; | ||
export function {{ dataEnumFunction }}(kind: '{{ getVariant(variant.name) }}', data: GetDataEnumKindContent<{{ looseName }}, '{{ getVariant(variant.name) }}'>['fields']): GetDataEnumKind<{{ looseName }}, '{{ getVariant(variant.name) }}'>; | ||
{% else %} | ||
export function {{ dataEnumFunction }}(kind: '{{ variant.name | pascalCase }}'): GetDataEnumKind<{{ looseName }}, '{{ variant.name | pascalCase }}'>; | ||
export function {{ dataEnumFunction }}(kind: '{{ getVariant(variant.name) }}'): GetDataEnumKind<{{ looseName }}, '{{ getVariant(variant.name) }}'>; | ||
{% endif %} | ||
{% endfor %} | ||
export function {{ dataEnumFunction }}<K extends {{ looseName }}['__kind']>( | ||
export function {{ dataEnumFunction }}<K extends {{ looseName }}['{{ dataEnumDiscriminator }}']>( | ||
kind: K, | ||
data?: any, | ||
): Extract<{{ looseName }}, { __kind: K }> { | ||
return Array.isArray(data) ? { __kind: kind, fields: data } : { __kind: kind, ...(data ?? {}) }; | ||
): Extract<{{ looseName }}, { {{ dataEnumDiscriminator }}: K }> { | ||
return Array.isArray(data) ? { {{ dataEnumDiscriminator }}: kind, fields: data } : { {{ dataEnumDiscriminator }}: kind, ...(data ?? {}) }; | ||
} | ||
|
||
export function {{ isDataEnumFunction }}<K extends {{ strictName }}['__kind']>( | ||
export function {{ isDataEnumFunction }}<K extends {{ strictName }}['{{ dataEnumDiscriminator }}']>( | ||
kind: K, | ||
value: {{ strictName }}, | ||
): value is {{ strictName }} & { __kind: K } { | ||
return value.__kind === kind; | ||
): value is {{ strictName }} & { {{ dataEnumDiscriminator }}: K } { | ||
return value.{{ dataEnumDiscriminator }} === kind; | ||
}; |
This file contains 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 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 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 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 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 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
Oops, something went wrong.