Skip to content

Commit

Permalink
Fix enum size import issue in JS renderers (#179)
Browse files Browse the repository at this point in the history
When setting a custom size on a data enum, the size codec/serializer was being generated but not imported.
  • Loading branch information
lorisleiva authored Mar 20, 2024
1 parent 92c9b5a commit dc8dea7
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/violet-clocks-pull.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@metaplex-foundation/kinobi": patch
---

Fix enum size import issue in JS renderers
2 changes: 2 additions & 0 deletions src/renderers/js-experimental/getTypeManifestVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,13 @@ export function getTypeManifestVisitor(input: {
.mapRender(
(r) => `getDataEnumEncoder([${r}]${encoderOptionsAsString})`
)
.mergeImportsWith(encoderImports)
.addImports('solanaCodecsDataStructures', ['getDataEnumEncoder']);
mergedManifest.decoder
.mapRender(
(r) => `getDataEnumDecoder([${r}]${decoderOptionsAsString})`
)
.mergeImportsWith(decoderImports)
.addImports('solanaCodecsDataStructures', ['getDataEnumDecoder']);
return mergedManifest;
},
Expand Down
13 changes: 8 additions & 5 deletions src/renderers/js/getTypeManifestVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,7 @@ export function getTypeManifestVisitor(input: {
visitEnumType(enumType, { self }) {
const strictImports = new JavaScriptImportMap();
const looseImports = new JavaScriptImportMap();
const serializerImports = new JavaScriptImportMap().add(
'umiSerializers',
'scalarEnum'
);
const serializerImports = new JavaScriptImportMap();

const variantNames = enumType.variants.map((variant) =>
pascalCase(variant.name)
Expand Down Expand Up @@ -176,7 +173,10 @@ export function getTypeManifestVisitor(input: {
serializer:
`scalarEnum<${currentParentName.strict}>` +
`(${currentParentName.strict + optionsAsString})`,
serializerImports,
serializerImports: serializerImports.add(
'umiSerializers',
'scalarEnum'
),
};
}

Expand All @@ -194,6 +194,9 @@ export function getTypeManifestVisitor(input: {
});

const mergedManifest = mergeManifests(variants);
mergedManifest.strictImports.mergeWith(strictImports);
mergedManifest.looseImports.mergeWith(looseImports);
mergedManifest.serializerImports.mergeWith(serializerImports);
const variantSerializers = variants
.map((variant) => variant.serializer)
.join(', ');
Expand Down
1 change: 1 addition & 0 deletions test/mpl_token_metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -4229,6 +4229,7 @@
"name": "DelegateArgs",
"type": {
"kind": "enum",
"size": "u16",
"variants": [
{
"name": "CollectionV1"
Expand Down
28 changes: 18 additions & 10 deletions test/packages/js-experimental/src/generated/types/delegateArgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import {
getDataEnumEncoder,
getStructDecoder,
getStructEncoder,
getU16Decoder,
getU16Encoder,
getU64Decoder,
getU64Encoder,
getUnitDecoder,
Expand All @@ -34,19 +36,25 @@ export type DelegateArgsArgs =
| { __kind: 'TransferV1'; amount: number | bigint };

export function getDelegateArgsEncoder(): Encoder<DelegateArgsArgs> {
return getDataEnumEncoder([
['CollectionV1', getUnitEncoder()],
['SaleV1', getStructEncoder([['amount', getU64Encoder()]])],
['TransferV1', getStructEncoder([['amount', getU64Encoder()]])],
]);
return getDataEnumEncoder(
[
['CollectionV1', getUnitEncoder()],
['SaleV1', getStructEncoder([['amount', getU64Encoder()]])],
['TransferV1', getStructEncoder([['amount', getU64Encoder()]])],
],
{ size: getU16Encoder() }
);
}

export function getDelegateArgsDecoder(): Decoder<DelegateArgs> {
return getDataEnumDecoder([
['CollectionV1', getUnitDecoder()],
['SaleV1', getStructDecoder([['amount', getU64Decoder()]])],
['TransferV1', getStructDecoder([['amount', getU64Decoder()]])],
]);
return getDataEnumDecoder(
[
['CollectionV1', getUnitDecoder()],
['SaleV1', getStructDecoder([['amount', getU64Decoder()]])],
['TransferV1', getStructDecoder([['amount', getU64Decoder()]])],
],
{ size: getU16Decoder() }
);
}

export function getDelegateArgsCodec(): Codec<DelegateArgsArgs, DelegateArgs> {
Expand Down
3 changes: 2 additions & 1 deletion test/packages/js/src/generated/types/delegateArgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
Serializer,
dataEnum,
struct,
u16,
u64,
unit,
} from '@metaplex-foundation/umi/serializers';
Expand Down Expand Up @@ -47,7 +48,7 @@ export function getDelegateArgsSerializer(): Serializer<
]),
],
],
{ description: 'DelegateArgs' }
{ size: u16(), description: 'DelegateArgs' }
) as Serializer<DelegateArgsArgs, DelegateArgs>;
}

Expand Down

0 comments on commit dc8dea7

Please sign in to comment.