-
Notifications
You must be signed in to change notification settings - Fork 87
[2.x] Adapt nodes to Codama v2 #1171
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
Draft
lorisleiva
wants to merge
1
commit into
09-21-adapt_errors_to_codama_v2
Choose a base branch
from
09-22-adapt_nodes_to_codama_v2
base: 09-21-adapt_errors_to_codama_v2
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 was deleted.
Oops, something went wrong.
This file contains hidden or 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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,34 @@ | ||
| import type { TransformNode, TypeNode } from '@codama/node-types'; | ||
|
|
||
| /** | ||
| * Return a copy of `typeNode` with the given `transforms` appended after | ||
| * any it already carries. | ||
| * | ||
| * Transforms apply in array order, the first being the innermost, so | ||
| * appending keeps the node's existing transforms innermost and layers the | ||
| * new ones on the outside. This is the v2 counterpart of re-wrapping a | ||
| * type in its original wrapper nodes: a consumer that replaces a type node | ||
| * (e.g. rebuilds a `structTypeNode`) can carry the original's transforms | ||
| * across with `addTypeNodeTransforms(newNode, oldNode.transforms ?? [])` | ||
| * instead of a double spread. | ||
| * | ||
| * Only pass transforms the target node does not already carry. A node | ||
| * derived by spreading the original (`{ ...oldNode, fields }`) already has | ||
| * `oldNode.transforms`, so calling `addTypeNodeTransforms(rebuilt, | ||
| * oldNode.transforms ?? [])` on it would duplicate them. | ||
| * | ||
| * When `transforms` is empty the node is returned unchanged. The result | ||
| * omits the `transforms` attribute entirely when there is nothing to | ||
| * carry, matching the generated constructors. | ||
| * | ||
| * The return type keeps `T` for ergonomics; the node's `TTransforms` type | ||
| * parameter is not re-derived, as with the generated constructors' own | ||
| * `as TTransforms` casts. | ||
| */ | ||
| export function addTypeNodeTransforms<T extends TypeNode>(typeNode: T, transforms: readonly TransformNode[]): T { | ||
| if (transforms.length === 0) return typeNode; | ||
| const merged = [...(typeNode.transforms ?? []), ...transforms]; | ||
| // Spreading a generic `T` widens to an index-signature object, so the | ||
| // narrowed node type has to be reasserted through `unknown`. | ||
| return Object.freeze({ ...typeNode, transforms: merged }) as unknown as T; | ||
| } | ||
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,13 +1,29 @@ | ||
| import { expect, test } from 'vitest'; | ||
|
|
||
| import { accountNode } from '../src'; | ||
| import { accountNode, integerTypeNode, structFieldTypeNode, structTypeNode } from '../src'; | ||
|
|
||
| test('it returns the right node kind', () => { | ||
| const node = accountNode({ name: 'foo' }); | ||
| const node = accountNode({ identifier: 'foo' }); | ||
| expect(node.kind).toBe('accountNode'); | ||
| }); | ||
|
|
||
| test('it returns a frozen object', () => { | ||
| const node = accountNode({ name: 'foo' }); | ||
| const node = accountNode({ identifier: 'foo' }); | ||
| expect(Object.isFrozen(node)).toBe(true); | ||
| }); | ||
|
|
||
| test('it defaults the data to an empty struct', () => { | ||
| const node = accountNode({ identifier: 'foo' }); | ||
| expect(node.data).toEqual(structTypeNode([])); | ||
| }); | ||
|
|
||
| test('it keeps the provided data', () => { | ||
| const data = structTypeNode([structFieldTypeNode({ identifier: 'amount', type: integerTypeNode('u64') })]); | ||
| const node = accountNode({ data, identifier: 'foo' }); | ||
| expect(node.data).toBe(data); | ||
| }); | ||
|
|
||
| test('it preserves the identifier casing', () => { | ||
| const node = accountNode({ identifier: 'MyAccount' }); | ||
| expect(node.identifier).toBe('MyAccount'); | ||
| }); |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,37 +1,52 @@ | ||
| import { CODAMA_ERROR__INVALID_BRANDED_STRING, CodamaError } from '@codama/errors'; | ||
| import { expect, test } from 'vitest'; | ||
|
|
||
| import { constantNode, numberTypeNode, numberValueNode, stringTypeNode, stringValueNode } from '../src'; | ||
| import { constantNode, integerTypeNode, integerValueNode, stringTypeNode, stringValueNode } from '../src'; | ||
|
|
||
| test('it returns the right node kind', () => { | ||
| const node = constantNode('myConstant', numberTypeNode('u32'), numberValueNode(42)); | ||
| const node = constantNode('myConstant', integerTypeNode('u32'), integerValueNode('42')); | ||
| expect(node.kind).toBe('constantNode'); | ||
| }); | ||
|
|
||
| test('it returns a frozen object', () => { | ||
| const node = constantNode('myConstant', numberTypeNode('u32'), numberValueNode(42)); | ||
| const node = constantNode('myConstant', integerTypeNode('u32'), integerValueNode('42')); | ||
| expect(Object.isFrozen(node)).toBe(true); | ||
| }); | ||
|
|
||
| test('it creates a constant with a number type and value', () => { | ||
| const node = constantNode('maxItems', numberTypeNode('u64'), numberValueNode(100)); | ||
| expect(node.name).toBe('maxItems'); | ||
| expect(node.type.kind).toBe('numberTypeNode'); | ||
| expect(node.value.kind).toBe('numberValueNode'); | ||
| test('it creates a constant with an integer type and value', () => { | ||
| const node = constantNode('maxItems', integerTypeNode('u64'), integerValueNode('100')); | ||
| expect(node.identifier).toBe('maxItems'); | ||
| expect(node.type.kind).toBe('integerTypeNode'); | ||
| expect(node.value.kind).toBe('integerValueNode'); | ||
| }); | ||
|
|
||
| test('it creates a constant with a string type and value', () => { | ||
| const node = constantNode('appName', stringTypeNode('utf8'), stringValueNode('MyApp')); | ||
| expect(node.name).toBe('appName'); | ||
| expect(node.identifier).toBe('appName'); | ||
| expect(node.type.kind).toBe('stringTypeNode'); | ||
| expect(node.value.kind).toBe('stringValueNode'); | ||
| }); | ||
|
|
||
| test('it converts name to camelCase', () => { | ||
| const node = constantNode('my_constant', numberTypeNode('u8'), numberValueNode(1)); | ||
| expect(node.name).toBe('myConstant'); | ||
| test('it preserves the identifier casing', () => { | ||
| const node = constantNode('my_constant', integerTypeNode('u8'), integerValueNode('1')); | ||
| expect(node.identifier).toBe('my_constant'); | ||
| }); | ||
|
|
||
| test('it rejects invalid identifiers', () => { | ||
| expect(() => constantNode('my-constant', integerTypeNode('u8'), integerValueNode('1'))).toThrow( | ||
| new CodamaError(CODAMA_ERROR__INVALID_BRANDED_STRING, { | ||
| actual: 'my-constant', | ||
| expected: 'identifier (letters, digits and underscores; no leading digit)', | ||
| }), | ||
| ); | ||
| }); | ||
|
|
||
| test('it can have documentation', () => { | ||
| const node = constantNode('myConstant', numberTypeNode('u32'), numberValueNode(42), ['My docs']); | ||
| expect(node.docs).toEqual(['My docs']); | ||
| const node = constantNode('myConstant', integerTypeNode('u32'), integerValueNode('42'), { docs: 'My docs' }); | ||
| expect(node.docs).toBe('My docs'); | ||
| }); | ||
|
|
||
| test('it omits documentation when not provided', () => { | ||
| const node = constantNode('myConstant', integerTypeNode('u32'), integerValueNode('42')); | ||
| expect('docs' in node).toBe(false); | ||
| }); |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,13 +1,25 @@ | ||
| import { expect, test } from 'vitest'; | ||
|
|
||
| import { errorNode } from '../src'; | ||
| import { errorNode, textNode } from '../src'; | ||
|
|
||
| test('it returns the right node kind', () => { | ||
| const node = errorNode({ name: 'foo', code: 42, message: 'error message' }); | ||
| const node = errorNode({ code: 42, identifier: 'foo', message: 'error message' }); | ||
| expect(node.kind).toBe('errorNode'); | ||
| }); | ||
|
|
||
| test('it returns a frozen object', () => { | ||
| const node = errorNode({ name: 'foo', code: 42, message: 'error message' }); | ||
| const node = errorNode({ code: 42, identifier: 'foo', message: 'error message' }); | ||
| expect(Object.isFrozen(node)).toBe(true); | ||
| }); | ||
|
|
||
| test('it keeps the code and message', () => { | ||
| const node = errorNode({ code: 42, identifier: 'foo', message: 'error message' }); | ||
| expect(node.code).toBe(42); | ||
| expect(node.message).toBe('error message'); | ||
| }); | ||
|
|
||
| test('it accepts a text node as message', () => { | ||
| const message = textNode({ content: 'error message' }); | ||
| const node = errorNode({ code: 42, identifier: 'foo', message }); | ||
| expect(node.message).toBe(message); | ||
| }); |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,14 +1,18 @@ | ||
| import { expect, test } from 'vitest'; | ||
|
|
||
| import { structTypeNode } from '../src'; | ||
| import { eventNode } from '../src'; | ||
| import { eventNode, structTypeNode } from '../src'; | ||
|
|
||
| test('it returns the right node kind', () => { | ||
| const node = eventNode({ data: structTypeNode([]), name: 'foo' }); | ||
| const node = eventNode({ data: structTypeNode([]), identifier: 'foo' }); | ||
| expect(node.kind).toBe('eventNode'); | ||
| }); | ||
|
|
||
| test('it returns a frozen object', () => { | ||
| const node = eventNode({ data: structTypeNode([]), name: 'foo' }); | ||
| const node = eventNode({ data: structTypeNode([]), identifier: 'foo' }); | ||
| expect(Object.isFrozen(node)).toBe(true); | ||
| }); | ||
|
|
||
| test('it omits discriminators when the array is empty', () => { | ||
| const node = eventNode({ data: structTypeNode([]), discriminators: [], identifier: 'foo' }); | ||
| expect('discriminators' in node).toBe(false); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.