|
1 | 1 | # @evolution-sdk/evolution |
2 | 2 |
|
| 3 | +## 0.2.4 |
| 4 | + |
| 5 | +### Patch Changes |
| 6 | + |
| 7 | +- [#68](https://github.com/IntersectMBO/evolution-sdk/pull/68) [`5b735c8`](https://github.com/IntersectMBO/evolution-sdk/commit/5b735c856fac3562f0e5892bf84c841b1dc85281) Thanks [@solidsnakedev](https://github.com/solidsnakedev)! - ## TSchema Code Simplifications and Test Coverage |
| 8 | + |
| 9 | + ### Summary |
| 10 | + |
| 11 | + Added Literal options (index, flatInUnion) for better Union control. Simplified TSchema implementation by removing redundant code, extracting helpers, and optimizing algorithms. Added 7 missing round-trip tests for comprehensive coverage. |
| 12 | + |
| 13 | + ### New Features |
| 14 | + |
| 15 | + **Literal options for custom indices and flat unions:** |
| 16 | + |
| 17 | + ```typescript |
| 18 | + // Custom index for positioning in unions |
| 19 | + const Action = TSchema.Literal("withdraw", { index: 100 }) |
| 20 | + |
| 21 | + // Flat in union - unwraps the Literal at the Union level |
| 22 | + const FlatUnion = TSchema.Union( |
| 23 | + TSchema.Literal("OptionA", { flatInUnion: true }), |
| 24 | + TSchema.Literal("OptionB", { flatInUnion: true }) |
| 25 | + ) |
| 26 | + |
| 27 | + // Before: Union wraps each literal |
| 28 | + // Constr(0, [Constr(0, [])]) for OptionA |
| 29 | + // Constr(1, [Constr(1, [])]) for OptionB |
| 30 | + |
| 31 | + // After: Literals are unwrapped at Union level |
| 32 | + // Constr(0, []) for OptionA |
| 33 | + // Constr(1, []) for OptionB |
| 34 | + |
| 35 | + // Note: TSchema.Literal("OptionA", "OptionB") creates a single schema |
| 36 | + // with multiple literal values, which is different from a Union of |
| 37 | + // separate Literal schemas. Use Union + flatInUnion for explicit control. |
| 38 | + ``` |
| 39 | + |
| 40 | + **LiteralOptions interface:** |
| 41 | + |
| 42 | + ```typescript |
| 43 | + interface LiteralOptions { |
| 44 | + index?: number // Custom Constr index (default: auto-increment) |
| 45 | + flatInUnion?: boolean // Unwrap when used in Union (default: false) |
| 46 | + } |
| 47 | + |
| 48 | + // Overloaded signatures |
| 49 | + function Literal(...values: Literals): Literal<Literals> |
| 50 | + function Literal(...args: [...Literals, LiteralOptions]): Literal<Literals> |
| 51 | + ``` |
| 52 | + |
| 53 | + ### Code Simplifications |
| 54 | + |
| 55 | + **Removed redundant OneLiteral function:** |
| 56 | + |
| 57 | + ```typescript |
| 58 | + // Before: Separate function for single literals |
| 59 | + const Action = TSchema.OneLiteral("withdraw") |
| 60 | +
|
| 61 | + // After: Use Literal directly |
| 62 | + const Action = TSchema.Literal("withdraw") |
| 63 | + ``` |
| 64 | + |
| 65 | + **Simplified Boolean validation:** |
| 66 | + |
| 67 | + ```typescript |
| 68 | + // Before: Two separate checks |
| 69 | + decode: ({ fields, index }) => { |
| 70 | + if (index !== 0n && index !== 1n) { |
| 71 | + throw new Error(`Expected constructor index to be 0 or 1, got ${index}`) |
| 72 | + } |
| 73 | + if (fields.length !== 0) { |
| 74 | + throw new Error("Expected a constructor with no fields") |
| 75 | + } |
| 76 | + return index === 1n |
| 77 | + } |
| 78 | +
|
| 79 | + // After: Combined check with better error message |
| 80 | + decode: ({ fields, index }) => { |
| 81 | + if ((index !== 0n && index !== 1n) || fields.length !== 0) { |
| 82 | + throw new Error( |
| 83 | + `Expected constructor with index 0 or 1 and no fields, got index ${index} with ${fields.length} fields` |
| 84 | + ) |
| 85 | + } |
| 86 | + return index === 1n |
| 87 | + } |
| 88 | + ``` |
| 89 | + |
| 90 | + **Optimized collision detection (O(n²) → O(n)):** |
| 91 | + |
| 92 | + ```typescript |
| 93 | + // Before: Nested loops |
| 94 | + for (let i = 0; i < flatMembers.length; i++) { |
| 95 | + for (let j = i + 1; j < flatMembers.length; j++) { |
| 96 | + if (flatMembers[i].index === flatMembers[j].index) { |
| 97 | + // collision detected |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | +
|
| 102 | + // After: Map-based tracking |
| 103 | + const indexMap = new globalThis.Map<number, number>() |
| 104 | + for (const member of flatMembers) { |
| 105 | + if (indexMap.has(member.index)) { |
| 106 | + // collision detected |
| 107 | + } |
| 108 | + indexMap.set(member.index, member.position) |
| 109 | + } |
| 110 | + ``` |
| 111 | + |
| 112 | + **Extracted helper functions:** |
| 113 | + - `getTypeName(value)` - Centralized type name logic for error messages |
| 114 | + - Simplified `getLiteralFieldValue` with ternary operators |
| 115 | + - Simplified tag field detection logic |
| 116 | + |
| 117 | + ### New Round-Trip Tests |
| 118 | + |
| 119 | + Added comprehensive test coverage for previously untested features: |
| 120 | + 1. **UndefinedOr** - Both defined and undefined value encoding/decoding |
| 121 | + 2. **Struct with custom index** - Validates custom Constr index is preserved |
| 122 | + 3. **Struct with flatFields** - Verifies field merging into parent struct |
| 123 | + 4. **Variant** - Multi-option tagged unions (Mint, Burn, Transfer) |
| 124 | + 5. **TaggedStruct** - Default "\_tag" field and custom tagField names |
| 125 | + 6. **flatInUnion Literals in Union** - Validates flat Literals with Structs |
| 126 | + 7. **flatInUnion mixed types** - Literals and Structs with flatFields |
| 127 | + |
3 | 128 | ## 0.2.3 |
4 | 129 |
|
5 | 130 | ### Patch Changes |
|
0 commit comments