-
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.
Add BytesValueNode and ConstantValueNode (#198)
- Loading branch information
1 parent
8eaf605
commit f74707d
Showing
13 changed files
with
203 additions
and
3 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": minor | ||
--- | ||
|
||
Add BytesValueNode and ConstantValueNode |
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,37 @@ | ||
import { | ||
getBase16Encoder, | ||
getBase58Encoder, | ||
getBase64Encoder, | ||
getUtf8Encoder, | ||
} from '@solana/codecs-strings'; | ||
|
||
export type BytesEncoding = 'utf8' | 'base16' | 'base58' | 'base64'; | ||
|
||
export interface BytesValueNode { | ||
readonly kind: 'bytesValueNode'; | ||
|
||
// Data. | ||
readonly data: string; | ||
readonly encoding: BytesEncoding; | ||
} | ||
|
||
export function bytesValueNode( | ||
encoding: BytesEncoding, | ||
data: string | ||
): BytesValueNode { | ||
return { kind: 'bytesValueNode', data, encoding }; | ||
} | ||
|
||
export function getBytesFromBytesValueNode(node: BytesValueNode): Uint8Array { | ||
switch (node.encoding) { | ||
case 'utf8': | ||
return getUtf8Encoder().encode(node.data); | ||
case 'base16': | ||
return getBase16Encoder().encode(node.data); | ||
case 'base58': | ||
return getBase58Encoder().encode(node.data); | ||
case 'base64': | ||
default: | ||
return getBase64Encoder().encode(node.data); | ||
} | ||
} |
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,38 @@ | ||
import { bytesTypeNode } from '../typeNodes/BytesTypeNode'; | ||
import { StringEncoding, stringTypeNode } from '../typeNodes/StringTypeNode'; | ||
import { TypeNode } from '../typeNodes/TypeNode'; | ||
import { BytesEncoding, bytesValueNode } from './BytesValueNode'; | ||
import { stringValueNode } from './StringValueNode'; | ||
import { ValueNode } from './ValueNode'; | ||
|
||
export interface ConstantValueNode< | ||
TType extends TypeNode = TypeNode, | ||
TValue extends ValueNode = ValueNode, | ||
> { | ||
readonly kind: 'constantValueNode'; | ||
|
||
// Children. | ||
readonly type: TType; | ||
readonly value: TValue; | ||
} | ||
|
||
export function constantValueNode< | ||
TType extends TypeNode, | ||
TValue extends ValueNode, | ||
>(type: TType, value: TValue): ConstantValueNode<TType, TValue> { | ||
return { kind: 'constantValueNode', type, value }; | ||
} | ||
|
||
export function constantValueNodeFromString( | ||
encoding: StringEncoding, | ||
string: string | ||
) { | ||
return constantValueNode(stringTypeNode(encoding), stringValueNode(string)); | ||
} | ||
|
||
export function constantValueNodeFromBytes( | ||
encoding: BytesEncoding, | ||
data: string | ||
) { | ||
return constantValueNode(bytesTypeNode(), bytesValueNode(encoding, data)); | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import test from 'ava'; | ||
import { bytesValueNode } from '../../../../src'; | ||
import { | ||
deleteNodesVisitorMacro, | ||
getDebugStringVisitorMacro, | ||
identityVisitorMacro, | ||
mergeVisitorMacro, | ||
} from '../_setup'; | ||
|
||
const node = bytesValueNode('base64', 'SGVsbG8gV29ybGQ='); | ||
|
||
test(mergeVisitorMacro, node, 1); | ||
test(identityVisitorMacro, node); | ||
test(deleteNodesVisitorMacro, node, '[bytesValueNode]', null); | ||
test( | ||
getDebugStringVisitorMacro, | ||
node, | ||
`bytesValueNode [base64.SGVsbG8gV29ybGQ=]` | ||
); |
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,28 @@ | ||
import test from 'ava'; | ||
import { | ||
constantValueNode, | ||
numberTypeNode, | ||
numberValueNode, | ||
} from '../../../../src'; | ||
import { | ||
deleteNodesVisitorMacro, | ||
getDebugStringVisitorMacro, | ||
identityVisitorMacro, | ||
mergeVisitorMacro, | ||
} from '../_setup'; | ||
|
||
const node = constantValueNode(numberTypeNode('u8'), numberValueNode(42)); | ||
|
||
test(mergeVisitorMacro, node, 3); | ||
test(identityVisitorMacro, node); | ||
test(deleteNodesVisitorMacro, node, '[constantValueNode]', null); | ||
test(deleteNodesVisitorMacro, node, '[numberTypeNode]', null); | ||
test(deleteNodesVisitorMacro, node, '[numberValueNode]', null); | ||
test( | ||
getDebugStringVisitorMacro, | ||
node, | ||
` | ||
constantValueNode | ||
| numberTypeNode [u8] | ||
| numberValueNode [42]` | ||
); |