Skip to content

Commit 23f0d0f

Browse files
committed
feat(TSchema): add explicit flat option for Union members
1 parent 7d42e0d commit 23f0d0f

31 files changed

+1523
-1391
lines changed

docs/content/docs/modules/core/Data.mdx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,8 @@ export type Data =
330330
| ReadonlyArray<Data>
331331
// Int (runtime as bigint)
332332
| bigint
333-
// ByteArray (runtime as Uint8Array)
334-
| Uint8Array
333+
// ByteArray (runtime as hex string)
334+
| string
335335
```
336336
337337
Added in v2.0.0
@@ -410,7 +410,7 @@ Type guard to check if a value is a PlutusBytes
410410
**Signature**
411411
412412
```ts
413-
export declare const isBytes: (u: unknown, overrideOptions?: ParseOptions | number) => u is Uint8Array
413+
export declare const isBytes: (u: unknown, overrideOptions?: ParseOptions | number) => u is string
414414
```
415415
416416
Added in v2.0.0
@@ -472,7 +472,7 @@ Schema for PlutusBytes data type
472472
**Signature**
473473
474474
```ts
475-
export declare const ByteArray: Schema.Schema<Uint8Array, string, never>
475+
export declare const ByteArray: Schema.refine<string, typeof Schema.String>
476476
```
477477
478478
Added in v2.0.0
@@ -741,7 +741,7 @@ export declare const matchData: <T>(
741741
Map: (entries: ReadonlyArray<[Data, Data]>) => T
742742
List: (items: ReadonlyArray<Data>) => T
743743
Int: (value: bigint) => T
744-
Bytes: (bytes: Uint8Array) => T
744+
Bytes: (bytes: string) => T
745745
Constr: (constr: Constr) => T
746746
}
747747
) => T

docs/content/docs/modules/core/TSchema.mdx

Lines changed: 101 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ parent: Modules
2424
- [NullOr](#nullor)
2525
- [OneLiteral](#oneliteral)
2626
- [Struct](#struct)
27+
- [StructOptions (interface)](#structoptions-interface)
2728
- [Tuple](#tuple)
2829
- [UndefinedOr](#undefinedor)
2930
- [Union](#union)
@@ -37,8 +38,8 @@ parent: Modules
3738

3839
## ByteArray
3940

40-
ByteArray schema that transforms hex string to Data.ByteArray for PlutusData.
41-
This enables withSchema compatibility by transforming from hex string to Uint8Array.
41+
ByteArray schema for PlutusData hex strings.
42+
Since Data.ByteArray is now hex string based, this is just an alias to it.
4243

4344
**Signature**
4445

@@ -108,11 +109,7 @@ Added in v2.0.0
108109
**Signature**
109110
110111
```ts
111-
export interface ByteArray
112-
extends Schema.transform<
113-
Schema.SchemaClass<Uint8Array<ArrayBufferLike>, Uint8Array<ArrayBufferLike>, never>,
114-
typeof Schema.String
115-
> {}
112+
export interface ByteArray extends Schema.Schema<string, string, never> {}
116113
```
117114

118115
## Integer (interface)
@@ -180,16 +177,71 @@ export declare const OneLiteral: <Single extends Exclude<SchemaAST.LiteralValue,
180177
## Struct
181178
182179
Creates a schema for struct types using Plutus Data Constructor
183-
Objects are represented as a constructor with index 0 and fields as an array
180+
Objects are represented as a constructor with index (default 0) and fields as an array
184181
185182
**Signature**
186183
187184
```ts
188-
export declare const Struct: <Fields extends Schema.Struct.Fields>(fields: Fields) => Struct<Fields>
185+
export declare const Struct: <Fields extends Schema.Struct.Fields>(
186+
fields: Fields,
187+
options?: StructOptions
188+
) => Struct<Fields>
189+
```
190+
191+
**Example**
192+
193+
```typescript
194+
import { TSchema } from "@evolution-sdk/evolution"
195+
196+
// Default: nested in Union, index 0
197+
TSchema.Struct({ name: TSchema.ByteArray, age: TSchema.Integer })
198+
```
199+
200+
**Example**
201+
202+
```typescript
203+
import { TSchema } from "@evolution-sdk/evolution"
204+
205+
// Flat union variants with custom indices
206+
TSchema.Struct({ amount: TSchema.Integer }, { index: 121, flat: true })
207+
TSchema.Struct({ amount: TSchema.Integer }, { index: 122, flat: true })
208+
```
209+
210+
**Example**
211+
212+
```typescript
213+
import { TSchema } from "@evolution-sdk/evolution"
214+
215+
// Custom index but stay nested (advanced use case)
216+
TSchema.Struct({ data: TSchema.Integer }, { index: 10, flat: false })
189217
```
190218

191219
Added in v2.0.0
192220

221+
## StructOptions (interface)
222+
223+
Options for Struct schema
224+
225+
**Signature**
226+
227+
```ts
228+
export interface StructOptions {
229+
/**
230+
* Custom Constr index for this struct (default: 0)
231+
* Useful when creating union variants with specific indices
232+
*/
233+
index?: number
234+
/**
235+
* When used in a Union, controls whether this Struct should be "flattened" (unwrapped).
236+
* - true: Encodes as Constr(index, [fields]) directly
237+
* - false: Encodes as Constr(unionPos, [Constr(index, [fields])]) (nested)
238+
*
239+
* Default: true when index is specified, false otherwise
240+
*/
241+
flat?: boolean
242+
}
243+
```
244+
193245
## Tuple
194246

195247
Creates a schema for tuple types - just passes through to Schema.Tuple directly
@@ -221,14 +273,53 @@ Added in v2.0.0
221273
## Union
222274
223275
Creates a schema for union types using Plutus Data Constructor
224-
Unions are represented as a constructor with index 0 and fields as an array
276+
Unions are represented as a constructor with index 0, 1, 2... and fields as an array
277+
278+
Members marked with flat: true will be encoded directly using their index
279+
instead of being wrapped in an additional Constr layer.
225280
226281
**Signature**
227282
228283
```ts
229284
export declare const Union: <Members extends ReadonlyArray<Schema.Schema.Any>>(...members: Members) => Union<Members>
230285
```
231286
287+
**Example**
288+
289+
```typescript
290+
import { TSchema } from "@evolution-sdk/evolution"
291+
292+
// Standard union with auto indices (nested)
293+
TSchema.Union(TSchema.Struct({ a: TSchema.Integer }), TSchema.Struct({ b: TSchema.Integer }))
294+
// Encodes to: Constr(0, [Constr(0, [a])]) or Constr(1, [Constr(0, [b])])
295+
```
296+
297+
**Example**
298+
299+
```typescript
300+
import { TSchema } from "@evolution-sdk/evolution"
301+
302+
// Union with flat Structs (single-level encoding)
303+
TSchema.Union(
304+
TSchema.Struct({ amount: TSchema.Integer }, { index: 121, flat: true }),
305+
TSchema.Struct({ amount: TSchema.Integer }, { index: 122, flat: true })
306+
)
307+
// Encodes to: Constr(121, [amount]) or Constr(122, [amount]) - single level!
308+
```
309+
310+
**Example**
311+
312+
```typescript
313+
import { TSchema } from "@evolution-sdk/evolution"
314+
315+
// Mixed union: some nested, some flat
316+
TSchema.Union(
317+
TSchema.Struct({ a: TSchema.Integer }), // nested, auto index 0
318+
TSchema.Struct({ b: TSchema.Integer }, { flat: true }), // flat, auto index 1
319+
TSchema.Struct({ c: TSchema.Integer }, { index: 100, flat: true }) // flat, custom index 100
320+
)
321+
```
322+
232323
Added in v2.0.0
233324

234325
## compose

0 commit comments

Comments
 (0)