-
Notifications
You must be signed in to change notification settings - Fork 1
/
untagged-union.test.ts
217 lines (195 loc) · 5.37 KB
/
untagged-union.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import { expectType, TypeEqual } from "ts-expect";
import { expect, test } from "vitest";
import {
array,
Codec,
field,
fields,
Infer,
InferEncoded,
number,
string,
tag,
taggedUnion,
unknown,
} from "../index.js";
import { run } from "../tests/helpers.js";
test("untagged union", () => {
type User = {
name: string;
followers: number;
};
type Failure = {
error: string;
errorCode: number;
};
type UserResult = Failure | User;
const userCodec = fields({
name: string,
followers: number,
});
const failureCodec = fields({
error: string,
errorCode: number,
});
const userResultCodec: Codec<UserResult> = {
decoder: (value) =>
// This is a bit annoying to do. Prefer a tagged union and use `fields`.
// But when that’s not possible, this is a simple way of “committing” to one
// of the union variants and choosing a decoder based on that.
// This approach results in much easier to understand error messages at
// runtime than an approach of first trying the first decoder, and then
// the second (because if both fail, you need to display both error messages).
typeof value === "object" && value !== null && "error" in value
? failureCodec.decoder(value)
: userCodec.decoder(value),
encoder: (value) =>
"error" in value ? failureCodec.encoder(value) : userCodec.encoder(value),
};
expect(userResultCodec.decoder({ name: "John", followers: 42 }))
.toMatchInlineSnapshot(`
{
"tag": "Valid",
"value": {
"followers": 42,
"name": "John",
},
}
`);
expect(userResultCodec.encoder({ name: "John", followers: 42 }))
.toMatchInlineSnapshot(`
{
"followers": 42,
"name": "John",
}
`);
expect(userResultCodec.decoder({ error: "Not found", errorCode: 404 }))
.toMatchInlineSnapshot(`
{
"tag": "Valid",
"value": {
"error": "Not found",
"errorCode": 404,
},
}
`);
expect(userResultCodec.encoder({ error: "Not found", errorCode: 404 }))
.toMatchInlineSnapshot(`
{
"error": "Not found",
"errorCode": 404,
}
`);
});
test("tagged tuples", () => {
// A codec that turns `[a, b, c]` into `{ 0: a, 1: b, 2: c }` and back.
const arrayToObject: Codec<Record<string, unknown>, Array<unknown>> = {
decoder: (value) => {
const arrResult = array(unknown).decoder(value);
if (arrResult.tag === "DecoderError") {
return arrResult;
}
const result: Record<string, unknown> = {};
for (const [index, item] of arrResult.value.entries()) {
result[index] = item;
}
return { tag: "Valid", value: result };
},
encoder: (value) => {
const result: Array<unknown> = [];
for (const key in value) {
const num = Number(key);
if (Number.isFinite(num)) {
result[num] = value[key];
}
}
return result;
},
};
// A function that takes a regular `taggedUnion` codec, but makes it work on
// tagged tuples instead.
function toArrayUnion<
Decoded extends Record<string, unknown>,
Encoded extends Record<string, unknown>,
>(codec: Codec<Decoded, Encoded>): Codec<Decoded, Array<unknown>> {
return {
decoder: (value) => {
const decoderResult = arrayToObject.decoder(value);
switch (decoderResult.tag) {
case "DecoderError":
return decoderResult;
case "Valid":
return codec.decoder(decoderResult.value);
}
},
encoder: (value) => arrayToObject.encoder(codec.encoder(value)),
};
}
type Shape = Infer<typeof Shape>;
const Shape = toArrayUnion(
taggedUnion("tag", [
{
tag: tag("Circle", { renameFieldFrom: "0" }),
radius: field(number, { renameFrom: "1" }),
},
{
tag: tag("Rectangle", { renameFieldFrom: "0" }),
width: field(number, { renameFrom: "1" }),
height: field(number, { renameFrom: "2" }),
},
]),
);
expectType<
TypeEqual<
Shape,
| {
tag: "Circle";
radius: number;
}
| {
tag: "Rectangle";
width: number;
height: number;
}
>
>(true);
expectType<TypeEqual<InferEncoded<typeof Shape>, Array<unknown>>>(true);
expect(run(Shape, ["Circle", 5])).toStrictEqual({ tag: "Circle", radius: 5 });
expect(run(Shape, ["Rectangle", 5, 6])).toStrictEqual({
tag: "Rectangle",
width: 5,
height: 6,
});
expect(Shape.encoder({ tag: "Circle", radius: 5 })).toStrictEqual([
"Circle",
5,
]);
expect(
Shape.encoder({ tag: "Rectangle", width: 5, height: 6 }),
).toStrictEqual(["Rectangle", 5, 6]);
// The error messages aren’t perfect, but still quite understandable.
expect(run(Shape, ["Square", 5])).toMatchInlineSnapshot(`
At root["0"]:
Expected one of these tags:
"Circle",
"Rectangle"
Got: "Square"
`);
expect(run(Shape, ["Circle", "5"])).toMatchInlineSnapshot(`
At root["1"]:
Expected a number
Got: "5"
`);
expect(run(Shape, ["Circle"])).toMatchInlineSnapshot(`
At root:
Expected an object with a field called: "1"
Got: {
"0": "Circle"
}
`);
expect(run(Shape, [])).toMatchInlineSnapshot(`
At root:
Expected an object with a field called: "0"
Got: {}
`);
});