Skip to content

Commit

Permalink
Add FixedSizeTypeNode and SizePrefixTypeNode (#191)
Browse files Browse the repository at this point in the history
* Add FixedSizeTypeNode and SizePrefixTypeNode

* Add changeset
  • Loading branch information
lorisleiva authored Apr 6, 2024
1 parent 937742d commit 6021aa9
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/nasty-olives-scream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@metaplex-foundation/kinobi": minor
---

Add FixedSizeTypeNode and SizePrefixTypeNode
18 changes: 18 additions & 0 deletions src/nodes/typeNodes/FixedSizeTypeNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { TypeNode } from './TypeNode';

export type FixedSizeTypeNode = {
readonly kind: 'fixedSizeTypeNode';

// Children.
readonly type: TypeNode;

// Data.
readonly size: number;
};

export function fixedSizeTypeNode(
type: TypeNode,
size: number
): FixedSizeTypeNode {
return { kind: 'fixedSizeTypeNode', type, size };
}
17 changes: 17 additions & 0 deletions src/nodes/typeNodes/SizePrefixTypeNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { NumberTypeNode } from './NumberTypeNode';
import { TypeNode } from './TypeNode';

export type SizePrefixTypeNode = {
readonly kind: 'sizePrefixTypeNode';

// Children.
readonly type: TypeNode;
readonly prefix: NumberTypeNode;
};

export function sizePrefixTypeNode(
type: TypeNode,
prefix: NumberTypeNode
): SizePrefixTypeNode {
return { kind: 'sizePrefixTypeNode', type, prefix };
}
4 changes: 4 additions & 0 deletions src/nodes/typeNodes/TypeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ import { EnumEmptyVariantTypeNode } from './EnumEmptyVariantTypeNode';
import { EnumStructVariantTypeNode } from './EnumStructVariantTypeNode';
import { EnumTupleVariantTypeNode } from './EnumTupleVariantTypeNode';
import { EnumTypeNode, enumTypeNodeFromIdl } from './EnumTypeNode';
import { FixedSizeTypeNode } from './FixedSizeTypeNode';
import { MapTypeNode, mapTypeNodeFromIdl } from './MapTypeNode';
import { NumberTypeNode, numberTypeNode } from './NumberTypeNode';
import { OptionTypeNode, optionTypeNodeFromIdl } from './OptionTypeNode';
import { PublicKeyTypeNode, publicKeyTypeNode } from './PublicKeyTypeNode';
import { SetTypeNode, setTypeNodeFromIdl } from './SetTypeNode';
import { SizePrefixTypeNode } from './SizePrefixTypeNode';
import { SolAmountTypeNode } from './SolAmountTypeNode';
import { StringTypeNode, stringTypeNode } from './StringTypeNode';
import { StructFieldTypeNode } from './StructFieldTypeNode';
Expand All @@ -34,11 +36,13 @@ export const STANDALONE_TYPE_NODES = {
bytesTypeNode: {} as BytesTypeNode,
dateTimeTypeNode: {} as DateTimeTypeNode,
enumTypeNode: {} as EnumTypeNode,
fixedSizeTypeNode: {} as FixedSizeTypeNode,
mapTypeNode: {} as MapTypeNode,
numberTypeNode: {} as NumberTypeNode,
optionTypeNode: {} as OptionTypeNode,
publicKeyTypeNode: {} as PublicKeyTypeNode,
setTypeNode: {} as SetTypeNode,
sizePrefixTypeNode: {} as SizePrefixTypeNode,
solAmountTypeNode: {} as SolAmountTypeNode,
stringTypeNode: {} as StringTypeNode,
structTypeNode: {} as StructTypeNode,
Expand Down
2 changes: 2 additions & 0 deletions src/nodes/typeNodes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ export * from './EnumStructVariantTypeNode';
export * from './EnumTupleVariantTypeNode';
export * from './EnumTypeNode';
export * from './EnumVariantTypeNode';
export * from './FixedSizeTypeNode';
export * from './MapTypeNode';
export * from './NumberTypeNode';
export * from './OptionTypeNode';
export * from './PublicKeyTypeNode';
export * from './SetTypeNode';
export * from './SizePrefixTypeNode';
export * from './SolAmountTypeNode';
export * from './StringTypeNode';
export * from './StructFieldTypeNode';
Expand Down
2 changes: 2 additions & 0 deletions src/visitors/getDebugStringVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ function getNodeDetails(node: Node): string[] {
return [node.name, ...(node.offset > 0 ? [`offset:${node.offset}`] : [])];
case 'sizeDiscriminatorNode':
return [node.size.toString()];
case 'fixedSizeTypeNode':
return [node.size.toString()];
default:
return 'name' in node ? [node.name] : [];
}
Expand Down
23 changes: 23 additions & 0 deletions src/visitors/identityVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
enumTupleVariantTypeNode,
enumTypeNode,
enumValueNode,
fixedSizeTypeNode,
instructionAccountNode,
instructionArgumentNode,
instructionByteDeltaNode,
Expand All @@ -45,6 +46,7 @@ import {
rootNode,
setTypeNode,
setValueNode,
sizePrefixTypeNode,
solAmountTypeNode,
someValueNode,
stringTypeNode,
Expand Down Expand Up @@ -605,5 +607,26 @@ export function identityVisitor<TNodeKind extends NodeKind = NodeKind>(
};
}

if (castedNodeKeys.includes('fixedSizeTypeNode')) {
visitor.visitFixedSizeType = function visitFixedSizeType(node) {
const type = visit(this)(node.type);
if (type === null) return null;
assertIsNode(type, TYPE_NODES);
return fixedSizeTypeNode(type, node.size);
};
}

if (castedNodeKeys.includes('sizePrefixTypeNode')) {
visitor.visitSizePrefixType = function visitSizePrefixType(node) {
const prefix = visit(this)(node.prefix);
if (prefix === null) return null;
assertIsNode(prefix, 'numberTypeNode');
const type = visit(this)(node.type);
if (type === null) return null;
assertIsNode(type, TYPE_NODES);
return sizePrefixTypeNode(type, prefix);
};
}

return visitor as Visitor<Node, TNodeKind>;
}
15 changes: 15 additions & 0 deletions src/visitors/mergeVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,5 +332,20 @@ export function mergeVisitor<TReturn, TNodeKind extends NodeKind = NodeKind>(
};
}

if (castedNodeKeys.includes('fixedSizeTypeNode')) {
visitor.visitFixedSizeType = function visitFixedSizeType(node) {
return merge(node, visit(this)(node.type));
};
}

if (castedNodeKeys.includes('sizePrefixTypeNode')) {
visitor.visitSizePrefixType = function visitSizePrefixType(node) {
return merge(node, [
...visit(this)(node.prefix),
...visit(this)(node.type),
]);
};
}

return visitor as Visitor<TReturn, TNodeKind>;
}
31 changes: 31 additions & 0 deletions test/visitors/nodes/typeNodes/FixedSizeTypeNode.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import test from 'ava';
import {
fixedSizeTypeNode,
remainderSizeNode,
stringTypeNode,
} from '../../../../src';
import {
deleteNodesVisitorMacro,
getDebugStringVisitorMacro,
identityVisitorMacro,
mergeVisitorMacro,
} from '../_setup';

const node = fixedSizeTypeNode(
stringTypeNode({ size: remainderSizeNode() }),
42
);

test(mergeVisitorMacro, node, 3);
test(identityVisitorMacro, node);
test(deleteNodesVisitorMacro, node, '[stringTypeNode]', null);
test(deleteNodesVisitorMacro, node, '[fixedSizeTypeNode]', null);
test(
getDebugStringVisitorMacro,
node,
`
fixedSizeTypeNode [42]
| stringTypeNode [utf8]
| | remainderSizeNode
`
);
34 changes: 34 additions & 0 deletions test/visitors/nodes/typeNodes/SizePrefixTypeNode.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import test from 'ava';
import {
numberTypeNode,
remainderSizeNode,
sizePrefixTypeNode,
stringTypeNode,
} from '../../../../src';
import {
deleteNodesVisitorMacro,
getDebugStringVisitorMacro,
identityVisitorMacro,
mergeVisitorMacro,
} from '../_setup';

const node = sizePrefixTypeNode(
stringTypeNode({ size: remainderSizeNode() }),
numberTypeNode('u32')
);

test(mergeVisitorMacro, node, 4);
test(identityVisitorMacro, node);
test(deleteNodesVisitorMacro, node, '[sizePrefixTypeNode]', null);
test(deleteNodesVisitorMacro, node, '[stringTypeNode]', null);
test(deleteNodesVisitorMacro, node, '[numberTypeNode]', null);
test(
getDebugStringVisitorMacro,
node,
`
sizePrefixTypeNode
| numberTypeNode [u32]
| stringTypeNode [utf8]
| | remainderSizeNode
`
);

0 comments on commit 6021aa9

Please sign in to comment.