Skip to content

Commit

Permalink
Require StringTypeNode encoding explicitly
Browse files Browse the repository at this point in the history
  • Loading branch information
lorisleiva committed Apr 9, 2024
1 parent f74707d commit 3726e1f
Show file tree
Hide file tree
Showing 21 changed files with 48 additions and 43 deletions.
5 changes: 5 additions & 0 deletions .changeset/stale-suns-share.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@metaplex-foundation/kinobi": minor
---

Require StringTypeNode encoding explicitly
2 changes: 1 addition & 1 deletion src/nodes/PdaNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function pdaNodeFromIdl(idl: Partial<IdlPda>): PdaNode {
const seeds = (idl.seeds ?? []).map((seed): PdaSeedNode => {
if (seed.kind === 'constant') {
const type = (() => {
if (seed.type === 'string') return stringTypeNode();
if (seed.type === 'string') return stringTypeNode('utf8');
if (seed.type === 'bytes') return bytesTypeNode();
return createTypeNodeFromIdl(seed.type);
})();
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/pdaSeedNodes/ConstantPdaSeedNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ export function constantPdaSeedNode<
}

export function constantPdaSeedNodeFromString(value: string) {
return constantPdaSeedNode(stringTypeNode(), stringValueNode(value));
return constantPdaSeedNode(stringTypeNode('utf8'), stringValueNode(value));
}
9 changes: 3 additions & 6 deletions src/nodes/typeNodes/StringTypeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@ export interface StringTypeNode<
readonly encoding: TEncoding;
}

export function stringTypeNode<TEncoding extends StringEncoding = 'utf8'>(
encoding?: TEncoding
export function stringTypeNode<TEncoding extends StringEncoding>(
encoding: TEncoding
): StringTypeNode<TEncoding> {
return {
kind: 'stringTypeNode',
encoding: (encoding ?? 'utf8') as TEncoding,
};
return { kind: 'stringTypeNode', encoding };
}
2 changes: 1 addition & 1 deletion src/nodes/typeNodes/TypeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export const createTypeNodeFromIdl = (idlType: IdlType): TypeNode => {
if (idlType === 'bool') return booleanTypeNode();
if (idlType === 'publicKey') return publicKeyTypeNode();
if (idlType === 'string')
return sizePrefixTypeNode(stringTypeNode(), numberTypeNode('u32'));
return sizePrefixTypeNode(stringTypeNode('utf8'), numberTypeNode('u32'));
if (idlType === 'bytes')
return sizePrefixTypeNode(bytesTypeNode(), numberTypeNode('u32'));
return numberTypeNode(idlType);
Expand Down
8 changes: 4 additions & 4 deletions src/nodes/valueNodes/ConstantValueNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ export function constantValueNode<
return { kind: 'constantValueNode', type, value };
}

export function constantValueNodeFromString(
encoding: StringEncoding,
export function constantValueNodeFromString<TEncoding extends StringEncoding>(
encoding: TEncoding,
string: string
) {
return constantValueNode(stringTypeNode(encoding), stringValueNode(string));
}

export function constantValueNodeFromBytes(
encoding: BytesEncoding,
export function constantValueNodeFromBytes<TEncoding extends BytesEncoding>(
encoding: TEncoding,
data: string
) {
return constantValueNode(bytesTypeNode(), bytesValueNode(encoding, data));
Expand Down
6 changes: 3 additions & 3 deletions test/nodes/typeNodes/NestedTypeNode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import {

test('it resolved nested type nodes', (t) => {
const node = sizePrefixTypeNode(
fixedSizeTypeNode(stringTypeNode(), 32),
fixedSizeTypeNode(stringTypeNode('utf8'), 32),
numberTypeNode('u8')
);
t.deepEqual(resolveNestedTypeNode(node), stringTypeNode());
t.deepEqual(resolveNestedTypeNode(node), stringTypeNode('utf8'));
});

test('it returns the same instance when resolving nested types nodes', (t) => {
Expand All @@ -26,7 +26,7 @@ test('it returns the same instance when resolving nested types nodes', (t) => {

test('it transforms nested type nodes', (t) => {
const node = sizePrefixTypeNode(
fixedSizeTypeNode(stringTypeNode(), 32),
fixedSizeTypeNode(stringTypeNode('utf8'), 32),
numberTypeNode('u8')
);
const transformedNode = transformNestedTypeNode(node, () =>
Expand Down
6 changes: 3 additions & 3 deletions test/nodes/typeNodes/NestedTypeNode.typetest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {

{
// [NestedTypeNode]: it constraints the nested type of a node.
const stringNestedNode = fixedSizeTypeNode(stringTypeNode(), 32);
const stringNestedNode = fixedSizeTypeNode(stringTypeNode('utf8'), 32);
const numberNestedNode = fixedSizeTypeNode(numberTypeNode('u32'), 32);
stringNestedNode satisfies NestedTypeNode<StringTypeNode>;
numberNestedNode satisfies NestedTypeNode<NumberTypeNode>;
Expand All @@ -26,7 +26,7 @@ import {

{
// [resolveNestedTypeNode]: it unwraps the nested type of a node.
const stringNestedNode = fixedSizeTypeNode(stringTypeNode(), 32);
const stringNestedNode = fixedSizeTypeNode(stringTypeNode('utf8'), 32);
const numberNestedNode = fixedSizeTypeNode(numberTypeNode('u32'), 32);
resolveNestedTypeNode(stringNestedNode) satisfies StringTypeNode;
resolveNestedTypeNode(numberNestedNode) satisfies NumberTypeNode;
Expand All @@ -39,7 +39,7 @@ import {
{
// [transformNestedTypeNode]: it transforms the nested type of a nested node.
const transformedNode = transformNestedTypeNode(
fixedSizeTypeNode(stringTypeNode(), 32),
fixedSizeTypeNode(stringTypeNode('utf8'), 32),
() => numberTypeNode('u32')
);
transformedNode satisfies NestedTypeNode<NumberTypeNode>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const eventTypeNode = definedTypeNode({
name: 'event',
type: enumTypeNode([
enumEmptyVariantTypeNode('quit'),
enumTupleVariantTypeNode('write', tupleTypeNode([stringTypeNode()])),
enumTupleVariantTypeNode('write', tupleTypeNode([stringTypeNode('utf8')])),
enumStructVariantTypeNode(
'move',
structTypeNode([
Expand Down
5 changes: 4 additions & 1 deletion test/renderers/rust/definedTypesPage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ test('it renders a prefix string on a defined type', (t) => {
type: structTypeNode([
structFieldTypeNode({
name: 'contentType',
type: sizePrefixTypeNode(stringTypeNode(), numberTypeNode('u8')),
type: sizePrefixTypeNode(
stringTypeNode('utf8'),
numberTypeNode('u8')
),
}),
]),
}),
Expand Down
16 changes: 8 additions & 8 deletions test/visitors/bottomUpTransformerVisitor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ test('it can transform nodes into other nodes', (t) => {

// And a transformer visitor that transforms all number nodes into string nodes.
const visitor = bottomUpTransformerVisitor([
(node) => (isNode(node, 'numberTypeNode') ? stringTypeNode() : node),
(node) => (isNode(node, 'numberTypeNode') ? stringTypeNode('utf8') : node),
]);

// When we visit the tree using that visitor.
Expand All @@ -29,8 +29,8 @@ test('it can transform nodes into other nodes', (t) => {
t.deepEqual(
result,
tupleTypeNode([
stringTypeNode(),
tupleTypeNode([stringTypeNode(), publicKeyTypeNode()]),
stringTypeNode('utf8'),
tupleTypeNode([stringTypeNode('utf8'), publicKeyTypeNode()]),
])
);
});
Expand All @@ -46,7 +46,7 @@ test('it can transform nodes using node selectors', (t) => {
const visitor = bottomUpTransformerVisitor([
{
select: '[numberTypeNode]',
transform: () => stringTypeNode(),
transform: () => stringTypeNode('utf8'),
},
]);

Expand All @@ -57,8 +57,8 @@ test('it can transform nodes using node selectors', (t) => {
t.deepEqual(
result,
tupleTypeNode([
stringTypeNode(),
tupleTypeNode([stringTypeNode(), publicKeyTypeNode()]),
stringTypeNode('utf8'),
tupleTypeNode([stringTypeNode('utf8'), publicKeyTypeNode()]),
])
);
});
Expand Down Expand Up @@ -136,7 +136,7 @@ test('it can transform nodes using multiple node selectors', (t) => {
'[numberTypeNode]',
(_, nodeStack) => nodeStack.all().length > 1,
],
transform: () => stringTypeNode(),
transform: () => stringTypeNode('utf8'),
},
]);

Expand All @@ -148,7 +148,7 @@ test('it can transform nodes using multiple node selectors', (t) => {
result,
tupleTypeNode([
numberTypeNode('u32'),
tupleTypeNode([stringTypeNode(), publicKeyTypeNode()]),
tupleTypeNode([stringTypeNode('utf8'), publicKeyTypeNode()]),
])
);
});
4 changes: 2 additions & 2 deletions test/visitors/getByteSizeVisitor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ test(
structFieldTypeNode({ name: 'age', type: numberTypeNode('u32') }),
structFieldTypeNode({
name: 'firstname',
type: fixedSizeTypeNode(stringTypeNode(), 42),
type: fixedSizeTypeNode(stringTypeNode('utf8'), 42),
}),
]),
4 + 42
Expand All @@ -59,7 +59,7 @@ test(
macro,
structTypeNode([
structFieldTypeNode({ name: 'age', type: numberTypeNode('u32') }),
structFieldTypeNode({ name: 'firstname', type: stringTypeNode() }),
structFieldTypeNode({ name: 'firstname', type: stringTypeNode('utf8') }),
]),
null
);
Expand Down
4 changes: 2 additions & 2 deletions test/visitors/getDebugStringVisitor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ test('it returns a string representing the main information of a node for debugg
structTypeNode([
structFieldTypeNode({
name: 'firstname',
type: sizePrefixTypeNode(stringTypeNode(), numberTypeNode('u64')),
type: sizePrefixTypeNode(stringTypeNode('utf8'), numberTypeNode('u64')),
}),
structFieldTypeNode({ name: 'age', type: numberTypeNode('u32') }),
structFieldTypeNode({
Expand Down Expand Up @@ -58,7 +58,7 @@ test('it can create indented strings', (t) => {
structTypeNode([
structFieldTypeNode({
name: 'firstname',
type: sizePrefixTypeNode(stringTypeNode(), numberTypeNode('u64')),
type: sizePrefixTypeNode(stringTypeNode('utf8'), numberTypeNode('u64')),
}),
structFieldTypeNode({ name: 'age', type: numberTypeNode('u32') }),
structFieldTypeNode({
Expand Down
2 changes: 1 addition & 1 deletion test/visitors/nodes/DefinedTypeNode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const node = definedTypeNode({
type: structTypeNode([
structFieldTypeNode({
name: 'name',
type: fixedSizeTypeNode(stringTypeNode(), 42),
type: fixedSizeTypeNode(stringTypeNode('utf8'), 42),
}),
structFieldTypeNode({ name: 'age', type: numberTypeNode('u64') }),
]),
Expand Down
2 changes: 1 addition & 1 deletion test/visitors/nodes/typeNodes/EnumTypeNode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const node = enumTypeNode(
enumEmptyVariantTypeNode('quit'),
enumTupleVariantTypeNode(
'write',
tupleTypeNode([fixedSizeTypeNode(stringTypeNode(), 32)])
tupleTypeNode([fixedSizeTypeNode(stringTypeNode('utf8'), 32)])
),
enumStructVariantTypeNode(
'move',
Expand Down
2 changes: 1 addition & 1 deletion test/visitors/nodes/typeNodes/FixedSizeTypeNode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
mergeVisitorMacro,
} from '../_setup';

const node = fixedSizeTypeNode(stringTypeNode(), 42);
const node = fixedSizeTypeNode(stringTypeNode('utf8'), 42);

test(mergeVisitorMacro, node, 2);
test(identityVisitorMacro, node);
Expand Down
2 changes: 1 addition & 1 deletion test/visitors/nodes/typeNodes/MapTypeNode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
} from '../_setup';

const node = mapTypeNode(
fixedSizeTypeNode(stringTypeNode(), 32),
fixedSizeTypeNode(stringTypeNode('utf8'), 32),
publicKeyTypeNode(),
prefixedCountNode(numberTypeNode('u8'))
);
Expand Down
4 changes: 2 additions & 2 deletions test/visitors/nodes/typeNodes/PostOffsetTypeNode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
mergeVisitorMacro,
} from '../_setup';

const node = postOffsetTypeNode(stringTypeNode(), 42);
const node = postOffsetTypeNode(stringTypeNode('utf8'), 42);

test(mergeVisitorMacro, node, 2);
test(identityVisitorMacro, node);
Expand All @@ -25,7 +25,7 @@ postOffsetTypeNode [42.relative]
test(
'getDebugStringVisitor: different strategy',
getDebugStringVisitorMacro,
postOffsetTypeNode(stringTypeNode(), 42, 'absolute'),
postOffsetTypeNode(stringTypeNode('utf8'), 42, 'absolute'),
`
postOffsetTypeNode [42.absolute]
| stringTypeNode [utf8]`
Expand Down
4 changes: 2 additions & 2 deletions test/visitors/nodes/typeNodes/PreOffsetTypeNode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
mergeVisitorMacro,
} from '../_setup';

const node = preOffsetTypeNode(stringTypeNode(), 42);
const node = preOffsetTypeNode(stringTypeNode('utf8'), 42);

test(mergeVisitorMacro, node, 2);
test(identityVisitorMacro, node);
Expand All @@ -25,7 +25,7 @@ preOffsetTypeNode [42.relative]
test(
'getDebugStringVisitor: different strategy',
getDebugStringVisitorMacro,
preOffsetTypeNode(stringTypeNode(), 42, 'absolute'),
preOffsetTypeNode(stringTypeNode('utf8'), 42, 'absolute'),
`
preOffsetTypeNode [42.absolute]
| stringTypeNode [utf8]`
Expand Down
2 changes: 1 addition & 1 deletion test/visitors/nodes/typeNodes/SizePrefixTypeNode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
mergeVisitorMacro,
} from '../_setup';

const node = sizePrefixTypeNode(stringTypeNode(), numberTypeNode('u32'));
const node = sizePrefixTypeNode(stringTypeNode('utf8'), numberTypeNode('u32'));

test(mergeVisitorMacro, node, 3);
test(identityVisitorMacro, node);
Expand Down
2 changes: 1 addition & 1 deletion test/visitors/nodes/typeNodes/StringTypeNode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
mergeVisitorMacro,
} from '../_setup';

const node = stringTypeNode();
const node = stringTypeNode('utf8');

test(mergeVisitorMacro, node, 1);
test(identityVisitorMacro, node);
Expand Down

0 comments on commit 3726e1f

Please sign in to comment.