Skip to content

Commit 5b735c8

Browse files
committed
docs: add changeset
1 parent 0355a0a commit 5b735c8

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed

.changeset/beige-donuts-wish.md

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

0 commit comments

Comments
 (0)