You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 4043eca
Browse filesBrowse the repository at this point in the historyBrowse files
Values are raw JavaScript values that stay close to the bytes. For instance, a fixed point decodes to its raw integer, not to a decimal number. Types such as `Uint8Array`, `Set` or `Map` are avoided to keep values JSON compatible, with the exception of `bigint`.
70
+
Values are raw JavaScript values that stay close to the bytes. For instance, a fixed point decodes to its raw integer, not to a decimal number. Types such as `Uint8Array`, `Set` or `Map` are avoided in decoded values to keep them JSON compatible, with the exception of `bigint`. When encoding, a few more inputs are accepted for convenience, as described in the notes below.
|[`StringTypeNode`](https://github.com/codama-idl/spec/blob/main/docs/typeNodes/StringTypeNode.md)|`"Hello World"`| Uses the encoding of the node. |
81
-
|[`BytesTypeNode`](https://github.com/codama-idl/spec/blob/main/docs/typeNodes/BytesTypeNode.md)|`["base64", "SGVsbG8="]`|Encodes using the given encoding and decodes using the `bytesEncoding` option. |
81
+
|[`BytesTypeNode`](https://github.com/codama-idl/spec/blob/main/docs/typeNodes/BytesTypeNode.md)|`["base64", "SGVsbG8="]`|Also encodes from a `Uint8Array`. Decodes using the `bytesEncoding` option.|
82
82
|[`PublicKeyTypeNode`](https://github.com/codama-idl/spec/blob/main/docs/typeNodes/PublicKeyTypeNode.md)|`"9BbWp6tcX9MEGSUEpNXfspYxYsWCxE9FgRkAc3RpftkT"`| A base58 string. |
83
-
|[`StructTypeNode`](https://github.com/codama-idl/spec/blob/main/docs/typeNodes/StructTypeNode.md)|`{ first_name: "John", age: 42n }`| Keys are the raw field identifiers. |
83
+
|[`StructTypeNode`](https://github.com/codama-idl/spec/blob/main/docs/typeNodes/StructTypeNode.md)|`{ first_name: "John", age: 42n }`| Keys are the raw field identifiers. See [Default values](#default-values).|
|[`EnumTypeNode`](https://github.com/codama-idl/spec/blob/main/docs/typeNodes/EnumTypeNode.md)|`{ __kind: "move", __discriminator: 2, data: { x: 1n } }`| See [Enums](#enums). Variants without data also encode from their identifier.|
|[`SetTypeNode`](https://github.com/codama-idl/spec/blob/main/docs/typeNodes/SetTypeNode.md)|`[1n, 2n, 3n]`| Same as arrays. |
88
88
|[`MapTypeNode`](https://github.com/codama-idl/spec/blob/main/docs/typeNodes/MapTypeNode.md)|`{ key1: "value1", key2: "value2" }`| An object. |
89
89
|[`OptionTypeNode`](https://github.com/codama-idl/spec/blob/main/docs/typeNodes/OptionTypeNode.md)|`{ __option: "Some", value: 42n }` or `{ __option: "None" }`| Option objects, rather than `T \| null`, keep nested options distinct. |
90
90
|[`RemainderOptionTypeNode`](https://github.com/codama-idl/spec/blob/main/docs/typeNodes/RemainderOptionTypeNode.md)|`{ __option: "Some", value: 42n }` or `{ __option: "None" }`| Same as options. |
91
91
|[`ZeroableOptionTypeNode`](https://github.com/codama-idl/spec/blob/main/docs/typeNodes/ZeroableOptionTypeNode.md)|`{ __option: "Some", value: 42n }` or `{ __option: "None" }`| Same as options. |
92
92
|[`StructFieldTypeNode`](https://github.com/codama-idl/spec/blob/main/docs/typeNodes/StructFieldTypeNode.md)| - | Same as `node.type`. |
93
-
|[`EnumVariantTypeNode`](https://github.com/codama-idl/spec/blob/main/docs/typeNodes/EnumVariantTypeNode.md)| - |Same as `node.data`, without the enum discriminator. |
93
+
|[`EnumVariantTypeNode`](https://github.com/codama-idl/spec/blob/main/docs/typeNodes/EnumVariantTypeNode.md)| - |`{ __kind, data }`, prefixed by the enum discriminator when the path includes the enum.|
94
94
|[`DefinedTypeNode`](https://github.com/codama-idl/spec/blob/main/docs/DefinedTypeNode.md)| - | Same as `node.type`. |
95
95
|[`AccountNode`](https://github.com/codama-idl/spec/blob/main/docs/AccountNode.md)| - | Same as `node.data`. |
96
96
|[`EventNode`](https://github.com/codama-idl/spec/blob/main/docs/EventNode.md)| - | Same as `node.data`. |
@@ -101,7 +101,7 @@ Values are raw JavaScript values that stay close to the bytes. For instance, a f
101
101
102
102
### Enums
103
103
104
-
Every enum decodes to an object with the raw variant identifier as `__kind` and the variant discriminator as `__discriminator`. Variants with data carry it under `data`, whatever its type. When encoding, only `__kind` and `data` are read.
104
+
Every enum decodes to an object with the raw variant identifier as `__kind` and the variant discriminator as `__discriminator`. Variants with data carry it under `data`, whatever its type. When encoding, only `__kind` and `data` are read, and variants without data may also be encoded from their identifier alone. Identifiers must match exactly, and variants with data must provide it.
codec.encode('quit'); // 0x00, same as { __kind: 'quit' }
122
+
```
123
+
124
+
### Default values
125
+
126
+
Struct fields with a `defaultValue` do not need to be provided when encoding. Fields whose `defaultValueStrategy` is `omitted`, such as discriminators, always encode their default value, even if a value is provided. Other fields encode their default value when missing from the input.
Default values are only evaluated when needed. For instance, an `injectedValueNode` default only throws when it is not provided and the field is missing from the input.
147
+
122
148
### Transforms
123
149
124
150
The `transforms` of a type node are applied in order, from the innermost to the outermost. Here, the size prefix wraps the fixed-size string.
0 commit comments