Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename sizeNodes to countNodes #193

Merged
merged 1 commit into from
Apr 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/eight-numbers-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@metaplex-foundation/kinobi": minor
---

Rename sizeNodes to countNodes
4 changes: 2 additions & 2 deletions src/nodes/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { REGISTERED_CONTEXTUAL_VALUE_NODES } from './contextualValueNodes/Contex
import { REGISTERED_DISCRIMINATOR_NODES } from './discriminatorNodes/DiscriminatorNode';
import { REGISTERED_LINK_NODES } from './linkNodes/LinkNode';
import { REGISTERED_PDA_SEED_NODES } from './pdaSeedNodes/PdaSeedNode';
import { REGISTERED_SIZE_NODES } from './sizeNodes/SizeNode';
import { REGISTERED_COUNT_NODES } from './countNodes/CountNode';
import { REGISTERED_TYPE_NODES } from './typeNodes/TypeNode';
import { REGISTERED_VALUE_NODES } from './valueNodes/ValueNode';

Expand All @@ -38,7 +38,7 @@ const REGISTERED_NODES = {
...REGISTERED_DISCRIMINATOR_NODES,
...REGISTERED_LINK_NODES,
...REGISTERED_PDA_SEED_NODES,
...REGISTERED_SIZE_NODES,
...REGISTERED_COUNT_NODES,
...REGISTERED_TYPE_NODES,
...REGISTERED_VALUE_NODES,
};
Expand Down
23 changes: 23 additions & 0 deletions src/nodes/countNodes/CountNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { getNodeKinds } from '../../shared/utils';
import type { FixedCountNode } from './FixedCountNode';
import type { PrefixedCountNode } from './PrefixedCountNode';
import type { RemainderCountNode } from './RemainderCountNode';

// Count Node Registration.

export const REGISTERED_COUNT_NODES = {
fixedCountNode: {} as FixedCountNode,
remainderCountNode: {} as RemainderCountNode,
prefixedCountNode: {} as PrefixedCountNode,
};

export const REGISTERED_COUNT_NODE_KINDS = getNodeKinds(REGISTERED_COUNT_NODES);
export type RegisteredCountNodeKind =
(typeof REGISTERED_COUNT_NODE_KINDS)[number];
export type RegisteredCountNode =
(typeof REGISTERED_COUNT_NODES)[RegisteredCountNodeKind];

// Count Node Helpers.

export type CountNode = RegisteredCountNode;
export const COUNT_NODES = REGISTERED_COUNT_NODE_KINDS;
10 changes: 10 additions & 0 deletions src/nodes/countNodes/FixedCountNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export type FixedCountNode = {
readonly kind: 'fixedCountNode';

// Data.
readonly value: number;
};

export function fixedCountNode(value: number): FixedCountNode {
return { kind: 'fixedCountNode', value };
}
12 changes: 12 additions & 0 deletions src/nodes/countNodes/PrefixedCountNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { NumberTypeNode } from '../typeNodes';

export type PrefixedCountNode = {
readonly kind: 'prefixedCountNode';

// Children.
readonly prefix: NumberTypeNode;
};

export function prefixedCountNode(prefix: NumberTypeNode): PrefixedCountNode {
return { kind: 'prefixedCountNode', prefix };
}
7 changes: 7 additions & 0 deletions src/nodes/countNodes/RemainderCountNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export type RemainderCountNode = {
readonly kind: 'remainderCountNode';
};

export function remainderCountNode(): RemainderCountNode {
return { kind: 'remainderCountNode' };
}
4 changes: 4 additions & 0 deletions src/nodes/countNodes/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './CountNode';
export * from './FixedCountNode';
export * from './PrefixedCountNode';
export * from './RemainderCountNode';
2 changes: 1 addition & 1 deletion src/nodes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ export * from './ProgramNode';
export * from './RootNode';

export * from './contextualValueNodes';
export * from './countNodes';
export * from './discriminatorNodes';
export * from './linkNodes';
export * from './pdaSeedNodes';
export * from './sizeNodes';
export * from './typeNodes';
export * from './valueNodes';
10 changes: 0 additions & 10 deletions src/nodes/sizeNodes/FixedSizeNode.ts

This file was deleted.

12 changes: 0 additions & 12 deletions src/nodes/sizeNodes/PrefixedSizeNode.ts

This file was deleted.

7 changes: 0 additions & 7 deletions src/nodes/sizeNodes/RemainderSizeNode.ts

This file was deleted.

23 changes: 0 additions & 23 deletions src/nodes/sizeNodes/SizeNode.ts

This file was deleted.

4 changes: 0 additions & 4 deletions src/nodes/sizeNodes/index.ts

This file was deleted.

26 changes: 15 additions & 11 deletions src/nodes/typeNodes/ArrayTypeNode.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { IdlTypeArray, IdlTypeVec } from '../../idl';
import {
SizeNode,
fixedSizeNode,
prefixedSizeNode,
remainderSizeNode,
} from '../sizeNodes';
CountNode,
fixedCountNode,
prefixedCountNode,
remainderCountNode,
} from '../countNodes';
import { numberTypeNode } from './NumberTypeNode';
import { TypeNode, createTypeNodeFromIdl } from './TypeNode';

Expand All @@ -13,14 +13,17 @@ export type ArrayTypeNode = {

// Children.
readonly item: TypeNode;
readonly size: SizeNode;
readonly count: CountNode;
};

export function arrayTypeNode(item: TypeNode, size?: SizeNode): ArrayTypeNode {
export function arrayTypeNode(
item: TypeNode,
count?: CountNode
): ArrayTypeNode {
return {
kind: 'arrayTypeNode',
item,
size: size ?? prefixedSizeNode(numberTypeNode('u32')),
count: count ?? prefixedCountNode(numberTypeNode('u32')),
};
}

Expand All @@ -29,10 +32,11 @@ export function arrayTypeNodeFromIdl(
): ArrayTypeNode {
if ('array' in idl) {
const item = createTypeNodeFromIdl(idl.array[0]);
return arrayTypeNode(item, fixedSizeNode(idl.array[1]));
return arrayTypeNode(item, fixedCountNode(idl.array[1]));
}
const item = createTypeNodeFromIdl(idl.vec);
if (!idl.size) return arrayTypeNode(item);
if (idl.size === 'remainder') return arrayTypeNode(item, remainderSizeNode());
return arrayTypeNode(item, prefixedSizeNode(numberTypeNode(idl.size)));
if (idl.size === 'remainder')
return arrayTypeNode(item, remainderCountNode());
return arrayTypeNode(item, prefixedCountNode(numberTypeNode(idl.size)));
}
24 changes: 12 additions & 12 deletions src/nodes/typeNodes/MapTypeNode.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { IdlTypeMap } from '../../idl';
import {
SizeNode,
fixedSizeNode,
prefixedSizeNode,
remainderSizeNode,
} from '../sizeNodes';
CountNode,
fixedCountNode,
prefixedCountNode,
remainderCountNode,
} from '../countNodes';
import { numberTypeNode } from './NumberTypeNode';
import { TypeNode, createTypeNodeFromIdl } from './TypeNode';

Expand All @@ -14,7 +14,7 @@ export type MapTypeNode = {
// Children.
readonly key: TypeNode;
readonly value: TypeNode;
readonly size: SizeNode;
readonly count: CountNode;

// Data.
readonly idlMap: 'hashMap' | 'bTreeMap';
Expand All @@ -23,27 +23,27 @@ export type MapTypeNode = {
export function mapTypeNode(
key: TypeNode,
value: TypeNode,
size?: SizeNode,
count?: CountNode,
idlMap?: MapTypeNode['idlMap']
): MapTypeNode {
return {
kind: 'mapTypeNode',
key,
value,
size: size ?? prefixedSizeNode(numberTypeNode('u32')),
count: count ?? prefixedCountNode(numberTypeNode('u32')),
idlMap: idlMap ?? 'hashMap',
};
}

export function mapTypeNodeFromIdl(idl: IdlTypeMap): MapTypeNode {
const [key, value] = 'hashMap' in idl ? idl.hashMap : idl.bTreeMap;
let size: SizeNode | undefined;
let size: CountNode | undefined;
if (idl.size === 'remainder') {
size = remainderSizeNode();
size = remainderCountNode();
} else if (typeof idl.size === 'number') {
size = fixedSizeNode(idl.size);
size = fixedCountNode(idl.size);
} else if (idl.size) {
size = prefixedSizeNode(numberTypeNode(idl.size));
size = prefixedCountNode(numberTypeNode(idl.size));
}
return mapTypeNode(
createTypeNodeFromIdl(key),
Expand Down
24 changes: 12 additions & 12 deletions src/nodes/typeNodes/SetTypeNode.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { IdlTypeSet } from '../../idl';
import {
SizeNode,
fixedSizeNode,
prefixedSizeNode,
remainderSizeNode,
} from '../sizeNodes';
CountNode,
fixedCountNode,
prefixedCountNode,
remainderCountNode,
} from '../countNodes';
import { numberTypeNode } from './NumberTypeNode';
import { TypeNode, createTypeNodeFromIdl } from './TypeNode';

Expand All @@ -13,34 +13,34 @@ export type SetTypeNode = {

// Children.
readonly item: TypeNode;
readonly size: SizeNode;
readonly count: CountNode;

// Data.
readonly idlSet: 'hashSet' | 'bTreeSet';
};

export function setTypeNode(
item: TypeNode,
size?: SizeNode,
count?: CountNode,
idlSet?: SetTypeNode['idlSet']
): SetTypeNode {
return {
kind: 'setTypeNode',
item,
size: size ?? prefixedSizeNode(numberTypeNode('u32')),
count: count ?? prefixedCountNode(numberTypeNode('u32')),
idlSet: idlSet ?? 'hashSet',
};
}

export function setTypeNodeFromIdl(idl: IdlTypeSet): SetTypeNode {
const child = 'hashSet' in idl ? idl.hashSet : idl.bTreeSet;
let size: SetTypeNode['size'] | undefined;
let size: SetTypeNode['count'] | undefined;
if (idl.size === 'remainder') {
size = remainderSizeNode();
size = remainderCountNode();
} else if (typeof idl.size === 'number') {
size = fixedSizeNode(idl.size);
size = fixedCountNode(idl.size);
} else if (idl.size) {
size = prefixedSizeNode(numberTypeNode(idl.size));
size = prefixedCountNode(numberTypeNode(idl.size));
}
return setTypeNode(
createTypeNodeFromIdl(child),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ function getFieldConditionFragment(
isNode(field.type, 'arrayTypeNode') &&
isNode(field.type.item, 'numberTypeNode') &&
field.type.item.format === 'u8' &&
isNode(field.type.size, 'fixedSizeNode') &&
isNode(field.type.count, 'fixedCountNode') &&
isNode(field.defaultValue, 'arrayValueNode') &&
field.defaultValue.items.every(isNodeFilter('numberValueNode'))
) {
Expand Down
Loading
Loading