metano-runtime 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/system/collections/hash-set.d.ts +29 -0
- package/dist/system/collections/hash-set.js +138 -0
- package/dist/system/collections/immutable-collection.d.ts +31 -0
- package/dist/system/collections/immutable-collection.js +58 -0
- package/dist/system/collections/index.d.ts +3 -0
- package/dist/system/collections/index.js +3 -0
- package/dist/system/collections/list-helpers.d.ts +43 -0
- package/dist/system/collections/list-helpers.js +58 -0
- package/dist/system/equality.d.ts +38 -0
- package/dist/system/equality.js +178 -0
- package/dist/system/hash-code.d.ts +35 -0
- package/dist/system/hash-code.js +177 -0
- package/dist/system/index.d.ts +6 -0
- package/dist/system/index.js +6 -0
- package/dist/system/json/converters.d.ts +22 -0
- package/dist/system/json/converters.js +261 -0
- package/dist/system/json/index.d.ts +5 -0
- package/dist/system/json/index.js +4 -0
- package/dist/system/json/json-serializer.d.ts +39 -0
- package/dist/system/json/json-serializer.js +57 -0
- package/dist/system/json/property-naming-policy.d.ts +14 -0
- package/dist/system/json/property-naming-policy.js +116 -0
- package/dist/system/json/serializer-context.d.ts +30 -0
- package/dist/system/json/serializer-context.js +38 -0
- package/dist/system/json/types.d.ts +85 -0
- package/dist/system/json/types.js +1 -0
- package/dist/system/linq/append-enumerable.d.ts +8 -0
- package/dist/system/linq/append-enumerable.js +15 -0
- package/dist/system/linq/compare-keys.d.ts +7 -0
- package/dist/system/linq/compare-keys.js +31 -0
- package/dist/system/linq/concat-enumerable.d.ts +8 -0
- package/dist/system/linq/concat-enumerable.js +15 -0
- package/dist/system/linq/distinct-by-enumerable.d.ts +8 -0
- package/dist/system/linq/distinct-by-enumerable.js +21 -0
- package/dist/system/linq/distinct-enumerable.d.ts +7 -0
- package/dist/system/linq/distinct-enumerable.js +19 -0
- package/dist/system/linq/enumerable-base.d.ts +98 -0
- package/dist/system/linq/enumerable-base.js +306 -0
- package/dist/system/linq/enumerable.d.ts +16 -0
- package/dist/system/linq/enumerable.js +33 -0
- package/dist/system/linq/except-enumerable.d.ts +8 -0
- package/dist/system/linq/except-enumerable.js +22 -0
- package/dist/system/linq/group-by-enumerable.d.ts +8 -0
- package/dist/system/linq/group-by-enumerable.js +31 -0
- package/dist/system/linq/index.d.ts +23 -0
- package/dist/system/linq/index.js +70 -0
- package/dist/system/linq/intersect-enumerable.d.ts +8 -0
- package/dist/system/linq/intersect-enumerable.js +22 -0
- package/dist/system/linq/order-by-enumerable.d.ts +9 -0
- package/dist/system/linq/order-by-enumerable.js +22 -0
- package/dist/system/linq/prepend-enumerable.d.ts +8 -0
- package/dist/system/linq/prepend-enumerable.js +15 -0
- package/dist/system/linq/reverse-enumerable.d.ts +7 -0
- package/dist/system/linq/reverse-enumerable.js +15 -0
- package/dist/system/linq/select-enumerable.d.ts +8 -0
- package/dist/system/linq/select-enumerable.js +17 -0
- package/dist/system/linq/select-many-enumerable.d.ts +8 -0
- package/dist/system/linq/select-many-enumerable.js +17 -0
- package/dist/system/linq/skip-enumerable.d.ts +8 -0
- package/dist/system/linq/skip-enumerable.js +21 -0
- package/dist/system/linq/skip-while-enumerable.d.ts +8 -0
- package/dist/system/linq/skip-while-enumerable.js +21 -0
- package/dist/system/linq/source-enumerable.d.ts +7 -0
- package/dist/system/linq/source-enumerable.js +12 -0
- package/dist/system/linq/take-enumerable.d.ts +8 -0
- package/dist/system/linq/take-enumerable.js +20 -0
- package/dist/system/linq/take-while-enumerable.d.ts +8 -0
- package/dist/system/linq/take-while-enumerable.js +19 -0
- package/dist/system/linq/then-by-enumerable.d.ts +9 -0
- package/dist/system/linq/then-by-enumerable.js +61 -0
- package/dist/system/linq/union-enumerable.d.ts +8 -0
- package/dist/system/linq/union-enumerable.js +27 -0
- package/dist/system/linq/where-enumerable.d.ts +8 -0
- package/dist/system/linq/where-enumerable.js +18 -0
- package/dist/system/linq/zip-enumerable.d.ts +9 -0
- package/dist/system/linq/zip-enumerable.js +24 -0
- package/dist/system/temporal-helpers.d.ts +10 -0
- package/dist/system/temporal-helpers.js +12 -0
- package/dist/type-checking/index.d.ts +1 -0
- package/dist/type-checking/index.js +1 -0
- package/dist/type-checking/primitive-type-checks.d.ts +33 -0
- package/dist/type-checking/primitive-type-checks.js +74 -0
- package/package.json +44 -0
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Port of System.HashCode from .NET (xxHash32-based).
|
|
3
|
+
* Used by Metano-generated record classes for value-based equality.
|
|
4
|
+
*
|
|
5
|
+
* Reference: https://github.com/dotnet/corert/blob/master/src/System.Private.CoreLib/shared/System/HashCode.cs
|
|
6
|
+
* Algorithm: xxHash32 by Yann Collet (BSD 2-Clause)
|
|
7
|
+
*/
|
|
8
|
+
const Prime1 = 0x9e3779b1;
|
|
9
|
+
const Prime2 = 0x85ebca77;
|
|
10
|
+
const Prime3 = 0xc2b2ae3d;
|
|
11
|
+
const Prime4 = 0x27d4eb2f;
|
|
12
|
+
const Prime5 = 0x165667b1;
|
|
13
|
+
// Fixed seed (deterministic — no random seed like .NET since we need consistent hashing)
|
|
14
|
+
const Seed = 0;
|
|
15
|
+
function rotateLeft(value, count) {
|
|
16
|
+
return (value << count) | (value >>> (32 - count)) | 0;
|
|
17
|
+
}
|
|
18
|
+
function round(hash, input) {
|
|
19
|
+
hash = (hash + Math.imul(input, Prime2)) | 0;
|
|
20
|
+
hash = rotateLeft(hash, 13);
|
|
21
|
+
hash = Math.imul(hash, Prime1);
|
|
22
|
+
return hash;
|
|
23
|
+
}
|
|
24
|
+
function queueRound(hash, queuedValue) {
|
|
25
|
+
hash = (hash + Math.imul(queuedValue, Prime3)) | 0;
|
|
26
|
+
hash = rotateLeft(hash, 17);
|
|
27
|
+
hash = Math.imul(hash, Prime4);
|
|
28
|
+
return hash;
|
|
29
|
+
}
|
|
30
|
+
function mixState(v1, v2, v3, v4) {
|
|
31
|
+
const hash = rotateLeft(v1, 1) + rotateLeft(v2, 7) + rotateLeft(v3, 12) + rotateLeft(v4, 18);
|
|
32
|
+
return hash | 0;
|
|
33
|
+
}
|
|
34
|
+
function mixFinal(hash) {
|
|
35
|
+
hash = (hash ^ (hash >>> 15)) | 0;
|
|
36
|
+
hash = Math.imul(hash, Prime2);
|
|
37
|
+
hash = (hash ^ (hash >>> 13)) | 0;
|
|
38
|
+
hash = Math.imul(hash, Prime3);
|
|
39
|
+
hash = (hash ^ (hash >>> 16)) | 0;
|
|
40
|
+
return hash;
|
|
41
|
+
}
|
|
42
|
+
function mixEmptyState() {
|
|
43
|
+
return (Seed + Prime5) | 0;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Computes a hash code for a single value.
|
|
47
|
+
*/
|
|
48
|
+
function hashValue(value) {
|
|
49
|
+
if (value === null || value === undefined)
|
|
50
|
+
return 0;
|
|
51
|
+
if (typeof value === "number") {
|
|
52
|
+
// For integers, use the value directly. For floats, use bit representation.
|
|
53
|
+
if (Number.isInteger(value))
|
|
54
|
+
return value | 0;
|
|
55
|
+
// Float: use DataView to get bit pattern
|
|
56
|
+
const buf = new ArrayBuffer(8);
|
|
57
|
+
new DataView(buf).setFloat64(0, value);
|
|
58
|
+
const dv = new DataView(buf);
|
|
59
|
+
return dv.getInt32(0) ^ dv.getInt32(4);
|
|
60
|
+
}
|
|
61
|
+
if (typeof value === "string") {
|
|
62
|
+
let hash = 0;
|
|
63
|
+
for (let i = 0; i < value.length; i++) {
|
|
64
|
+
hash = Math.imul(hash, 31) + value.charCodeAt(i);
|
|
65
|
+
hash = hash | 0;
|
|
66
|
+
}
|
|
67
|
+
return hash;
|
|
68
|
+
}
|
|
69
|
+
if (typeof value === "boolean")
|
|
70
|
+
return value ? 1 : 0;
|
|
71
|
+
if (typeof value === "bigint")
|
|
72
|
+
return Number(value & 0xffffffffn) | 0;
|
|
73
|
+
// Objects with hashCode method (other Metano records)
|
|
74
|
+
if (typeof value === "object" &&
|
|
75
|
+
value !== null &&
|
|
76
|
+
"hashCode" in value &&
|
|
77
|
+
typeof value.hashCode === "function") {
|
|
78
|
+
return value.hashCode();
|
|
79
|
+
}
|
|
80
|
+
return 0;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Accumulator for computing hash codes, modeled after System.HashCode.
|
|
84
|
+
*
|
|
85
|
+
* Usage:
|
|
86
|
+
* ```ts
|
|
87
|
+
* const hc = new HashCode();
|
|
88
|
+
* hc.add(this.x);
|
|
89
|
+
* hc.add(this.y);
|
|
90
|
+
* return hc.toHashCode();
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
93
|
+
export class HashCode {
|
|
94
|
+
_v1;
|
|
95
|
+
_v2;
|
|
96
|
+
_v3;
|
|
97
|
+
_v4;
|
|
98
|
+
_queue1 = 0;
|
|
99
|
+
_queue2 = 0;
|
|
100
|
+
_queue3 = 0;
|
|
101
|
+
_length = 0;
|
|
102
|
+
constructor() {
|
|
103
|
+
this._v1 = (Seed + Prime1 + Prime2) | 0;
|
|
104
|
+
this._v2 = (Seed + Prime2) | 0;
|
|
105
|
+
this._v3 = Seed;
|
|
106
|
+
this._v4 = (Seed - Prime1) | 0;
|
|
107
|
+
}
|
|
108
|
+
add(value) {
|
|
109
|
+
const hc = hashValue(value);
|
|
110
|
+
const pos = this._length % 4;
|
|
111
|
+
this._length++;
|
|
112
|
+
switch (pos) {
|
|
113
|
+
case 0:
|
|
114
|
+
this._queue1 = hc;
|
|
115
|
+
break;
|
|
116
|
+
case 1:
|
|
117
|
+
this._queue2 = hc;
|
|
118
|
+
break;
|
|
119
|
+
case 2:
|
|
120
|
+
this._queue3 = hc;
|
|
121
|
+
break;
|
|
122
|
+
case 3:
|
|
123
|
+
this._v1 = round(this._v1, this._queue1);
|
|
124
|
+
this._v2 = round(this._v2, this._queue2);
|
|
125
|
+
this._v3 = round(this._v3, this._queue3);
|
|
126
|
+
this._v4 = round(this._v4, hc);
|
|
127
|
+
break;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
toHashCode() {
|
|
131
|
+
const length = this._length;
|
|
132
|
+
let hash;
|
|
133
|
+
if (length >= 4) {
|
|
134
|
+
hash = mixState(this._v1, this._v2, this._v3, this._v4);
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
hash = mixEmptyState();
|
|
138
|
+
}
|
|
139
|
+
hash = (hash + length * 4) | 0;
|
|
140
|
+
const remaining = length % 4;
|
|
141
|
+
if (remaining >= 1)
|
|
142
|
+
hash = queueRound(hash, this._queue1);
|
|
143
|
+
if (remaining >= 2)
|
|
144
|
+
hash = queueRound(hash, this._queue2);
|
|
145
|
+
if (remaining >= 3)
|
|
146
|
+
hash = queueRound(hash, this._queue3);
|
|
147
|
+
hash = mixFinal(hash);
|
|
148
|
+
return hash;
|
|
149
|
+
}
|
|
150
|
+
// ─── Static combine helpers ───────────────────────────────
|
|
151
|
+
static combine(v1) {
|
|
152
|
+
const hc = new HashCode();
|
|
153
|
+
hc.add(v1);
|
|
154
|
+
return hc.toHashCode();
|
|
155
|
+
}
|
|
156
|
+
static combine2(v1, v2) {
|
|
157
|
+
const hc = new HashCode();
|
|
158
|
+
hc.add(v1);
|
|
159
|
+
hc.add(v2);
|
|
160
|
+
return hc.toHashCode();
|
|
161
|
+
}
|
|
162
|
+
static combine3(v1, v2, v3) {
|
|
163
|
+
const hc = new HashCode();
|
|
164
|
+
hc.add(v1);
|
|
165
|
+
hc.add(v2);
|
|
166
|
+
hc.add(v3);
|
|
167
|
+
return hc.toHashCode();
|
|
168
|
+
}
|
|
169
|
+
static combine4(v1, v2, v3, v4) {
|
|
170
|
+
const hc = new HashCode();
|
|
171
|
+
hc.add(v1);
|
|
172
|
+
hc.add(v2);
|
|
173
|
+
hc.add(v3);
|
|
174
|
+
hc.add(v4);
|
|
175
|
+
return hc.toHashCode();
|
|
176
|
+
}
|
|
177
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { TypeDescriptor, TypeSpec, PropertySpec, JsonConverter } from "./types.ts";
|
|
2
|
+
/**
|
|
3
|
+
* Resolves the serialize/deserialize pair for a given type descriptor, checking
|
|
4
|
+
* custom converters first (user overrides), then falling back to built-in ones.
|
|
5
|
+
*/
|
|
6
|
+
export declare function resolveConverter(descriptor: TypeDescriptor, customConverters?: readonly JsonConverter[]): {
|
|
7
|
+
serialize: (value: unknown) => unknown;
|
|
8
|
+
deserialize: (value: unknown) => unknown;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Collects all properties from a spec chain, walking the base hierarchy.
|
|
12
|
+
* Base properties come first, own properties last.
|
|
13
|
+
*/
|
|
14
|
+
export declare function collectProperties(spec: TypeSpec): readonly PropertySpec[];
|
|
15
|
+
/**
|
|
16
|
+
* Serialize a TS object to a JSON-safe plain object using the given spec.
|
|
17
|
+
*/
|
|
18
|
+
export declare function serializeWithSpec(value: unknown, spec: TypeSpec, customConverters?: readonly JsonConverter[]): Record<string, unknown>;
|
|
19
|
+
/**
|
|
20
|
+
* Deserialize unknown data (from JSON.parse) into a typed instance using the spec.
|
|
21
|
+
*/
|
|
22
|
+
export declare function deserializeWithSpec<T>(data: unknown, spec: TypeSpec<T>, customConverters?: readonly JsonConverter[]): T;
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
import { HashSet } from "../collections/hash-set.js";
|
|
2
|
+
// ─── Converter Resolution ────────────────────────────────────────────────────
|
|
3
|
+
/**
|
|
4
|
+
* Resolves the serialize/deserialize pair for a given type descriptor, checking
|
|
5
|
+
* custom converters first (user overrides), then falling back to built-in ones.
|
|
6
|
+
*/
|
|
7
|
+
export function resolveConverter(descriptor, customConverters) {
|
|
8
|
+
if (customConverters) {
|
|
9
|
+
const custom = customConverters.find((c) => c.kind === descriptor.kind);
|
|
10
|
+
if (custom)
|
|
11
|
+
return custom;
|
|
12
|
+
}
|
|
13
|
+
return getBuiltinConverter(descriptor, customConverters);
|
|
14
|
+
}
|
|
15
|
+
// ─── Built-in Converters ─────────────────────────────────────────────────────
|
|
16
|
+
function getBuiltinConverter(descriptor, customConverters) {
|
|
17
|
+
switch (descriptor.kind) {
|
|
18
|
+
case "primitive":
|
|
19
|
+
return primitiveConverter;
|
|
20
|
+
case "temporal":
|
|
21
|
+
return temporalConverter(descriptor);
|
|
22
|
+
case "decimal":
|
|
23
|
+
return decimalConverter;
|
|
24
|
+
case "map":
|
|
25
|
+
return mapConverter(descriptor, customConverters);
|
|
26
|
+
case "array":
|
|
27
|
+
return arrayConverter(descriptor, customConverters);
|
|
28
|
+
case "hashSet":
|
|
29
|
+
return hashSetConverter(descriptor, customConverters);
|
|
30
|
+
case "branded":
|
|
31
|
+
return brandedConverter(descriptor);
|
|
32
|
+
case "enum":
|
|
33
|
+
return enumConverter(descriptor);
|
|
34
|
+
case "numericEnum":
|
|
35
|
+
return numericEnumConverter(descriptor);
|
|
36
|
+
case "nullable":
|
|
37
|
+
return nullableConverter(descriptor, customConverters);
|
|
38
|
+
case "ref":
|
|
39
|
+
return refConverter(descriptor, customConverters);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
// ─── Primitive ───────────────────────────────────────────────────────────────
|
|
43
|
+
const primitiveConverter = {
|
|
44
|
+
serialize: (value) => value,
|
|
45
|
+
deserialize: (value) => value,
|
|
46
|
+
};
|
|
47
|
+
// ─── Temporal ────────────────────────────────────────────────────────────────
|
|
48
|
+
function temporalConverter(descriptor) {
|
|
49
|
+
return {
|
|
50
|
+
serialize: (value) => {
|
|
51
|
+
if (value == null)
|
|
52
|
+
return null;
|
|
53
|
+
return String(value);
|
|
54
|
+
},
|
|
55
|
+
deserialize: (value) => {
|
|
56
|
+
if (value == null)
|
|
57
|
+
return null;
|
|
58
|
+
return descriptor.parse(value);
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
// ─── Decimal ─────────────────────────────────────────────────────────────────
|
|
63
|
+
/**
|
|
64
|
+
* Default decimal converter: serializes via toNumber() (matching C# default),
|
|
65
|
+
* deserializes by constructing a new Decimal. Users who need string precision
|
|
66
|
+
* can override with a custom converter.
|
|
67
|
+
*/
|
|
68
|
+
const decimalConverter = {
|
|
69
|
+
serialize: (value) => {
|
|
70
|
+
if (value == null)
|
|
71
|
+
return null;
|
|
72
|
+
// Decimal from decimal.js has toNumber()
|
|
73
|
+
return value.toNumber();
|
|
74
|
+
},
|
|
75
|
+
deserialize: (value) => {
|
|
76
|
+
if (value == null)
|
|
77
|
+
return null;
|
|
78
|
+
// We don't import Decimal directly to avoid a hard dependency.
|
|
79
|
+
// The TypeSpec's factory will handle construction. For standalone
|
|
80
|
+
// deserialize, the user should provide a custom converter or the
|
|
81
|
+
// generated spec handles it via the factory.
|
|
82
|
+
return value;
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
// ─── Map ─────────────────────────────────────────────────────────────────────
|
|
86
|
+
function mapConverter(descriptor, customConverters) {
|
|
87
|
+
const keyConv = resolveConverter(descriptor.key, customConverters);
|
|
88
|
+
const valueConv = resolveConverter(descriptor.value, customConverters);
|
|
89
|
+
return {
|
|
90
|
+
serialize: (value) => {
|
|
91
|
+
if (value == null)
|
|
92
|
+
return null;
|
|
93
|
+
const map = value;
|
|
94
|
+
const result = {};
|
|
95
|
+
for (const [k, v] of map) {
|
|
96
|
+
const serializedKey = String(keyConv.serialize(k));
|
|
97
|
+
result[serializedKey] = valueConv.serialize(v);
|
|
98
|
+
}
|
|
99
|
+
return result;
|
|
100
|
+
},
|
|
101
|
+
deserialize: (value) => {
|
|
102
|
+
if (value == null)
|
|
103
|
+
return null;
|
|
104
|
+
const obj = value;
|
|
105
|
+
const map = new Map();
|
|
106
|
+
for (const [k, v] of Object.entries(obj)) {
|
|
107
|
+
map.set(keyConv.deserialize(k), valueConv.deserialize(v));
|
|
108
|
+
}
|
|
109
|
+
return map;
|
|
110
|
+
},
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
// ─── Array ───────────────────────────────────────────────────────────────────
|
|
114
|
+
function arrayConverter(descriptor, customConverters) {
|
|
115
|
+
const elemConv = resolveConverter(descriptor.element, customConverters);
|
|
116
|
+
return {
|
|
117
|
+
serialize: (value) => {
|
|
118
|
+
if (value == null)
|
|
119
|
+
return null;
|
|
120
|
+
return value.map((item) => elemConv.serialize(item));
|
|
121
|
+
},
|
|
122
|
+
deserialize: (value) => {
|
|
123
|
+
if (value == null)
|
|
124
|
+
return null;
|
|
125
|
+
return value.map((item) => elemConv.deserialize(item));
|
|
126
|
+
},
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
// ─── HashSet ─────────────────────────────────────────────────────────────────
|
|
130
|
+
function hashSetConverter(descriptor, customConverters) {
|
|
131
|
+
const elemConv = resolveConverter(descriptor.element, customConverters);
|
|
132
|
+
return {
|
|
133
|
+
serialize: (value) => {
|
|
134
|
+
if (value == null)
|
|
135
|
+
return null;
|
|
136
|
+
const set = value;
|
|
137
|
+
const result = [];
|
|
138
|
+
for (const item of set) {
|
|
139
|
+
result.push(elemConv.serialize(item));
|
|
140
|
+
}
|
|
141
|
+
return result;
|
|
142
|
+
},
|
|
143
|
+
deserialize: (value) => {
|
|
144
|
+
if (value == null)
|
|
145
|
+
return null;
|
|
146
|
+
const arr = value.map((item) => elemConv.deserialize(item));
|
|
147
|
+
return new HashSet(arr);
|
|
148
|
+
},
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
// ─── Branded ─────────────────────────────────────────────────────────────────
|
|
152
|
+
function brandedConverter(descriptor) {
|
|
153
|
+
return {
|
|
154
|
+
serialize: (value) => value, // brand erases at runtime
|
|
155
|
+
deserialize: (value) => descriptor.create(value),
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
// ─── Enum (String) ───────────────────────────────────────────────────────────
|
|
159
|
+
function enumConverter(descriptor) {
|
|
160
|
+
const validValues = new Set(Object.values(descriptor.values));
|
|
161
|
+
return {
|
|
162
|
+
serialize: (value) => value, // already a string
|
|
163
|
+
deserialize: (value) => {
|
|
164
|
+
if (typeof value !== "string" || !validValues.has(value)) {
|
|
165
|
+
throw new Error(`Invalid enum value: ${JSON.stringify(value)}. ` +
|
|
166
|
+
`Expected one of: ${[...validValues].join(", ")}`);
|
|
167
|
+
}
|
|
168
|
+
return value;
|
|
169
|
+
},
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
// ─── Enum (Numeric) ──────────────────────────────────────────────────────────
|
|
173
|
+
function numericEnumConverter(descriptor) {
|
|
174
|
+
const validValues = new Set(Object.values(descriptor.values));
|
|
175
|
+
return {
|
|
176
|
+
serialize: (value) => value, // already a number
|
|
177
|
+
deserialize: (value) => {
|
|
178
|
+
if (typeof value !== "number" || !validValues.has(value)) {
|
|
179
|
+
throw new Error(`Invalid numeric enum value: ${JSON.stringify(value)}. ` +
|
|
180
|
+
`Expected one of: ${[...validValues].join(", ")}`);
|
|
181
|
+
}
|
|
182
|
+
return value;
|
|
183
|
+
},
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
// ─── Nullable ────────────────────────────────────────────────────────────────
|
|
187
|
+
function nullableConverter(descriptor, customConverters) {
|
|
188
|
+
const innerConv = resolveConverter(descriptor.inner, customConverters);
|
|
189
|
+
return {
|
|
190
|
+
serialize: (value) => (value == null ? null : innerConv.serialize(value)),
|
|
191
|
+
deserialize: (value) => (value == null ? null : innerConv.deserialize(value)),
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
// ─── Ref ─────────────────────────────────────────────────────────────────────
|
|
195
|
+
function refConverter(descriptor, customConverters) {
|
|
196
|
+
return {
|
|
197
|
+
serialize: (value) => {
|
|
198
|
+
if (value == null)
|
|
199
|
+
return null;
|
|
200
|
+
return serializeWithSpec(value, descriptor.spec(), customConverters);
|
|
201
|
+
},
|
|
202
|
+
deserialize: (value) => {
|
|
203
|
+
if (value == null)
|
|
204
|
+
return null;
|
|
205
|
+
return deserializeWithSpec(value, descriptor.spec(), customConverters);
|
|
206
|
+
},
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
// ─── Core serialize/deserialize by spec ──────────────────────────────────────
|
|
210
|
+
/**
|
|
211
|
+
* Collects all properties from a spec chain, walking the base hierarchy.
|
|
212
|
+
* Base properties come first, own properties last.
|
|
213
|
+
*/
|
|
214
|
+
export function collectProperties(spec) {
|
|
215
|
+
const base = spec.base ? collectProperties(spec.base) : [];
|
|
216
|
+
return [...base, ...spec.properties];
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Serialize a TS object to a JSON-safe plain object using the given spec.
|
|
220
|
+
*/
|
|
221
|
+
export function serializeWithSpec(value, spec, customConverters) {
|
|
222
|
+
const obj = value;
|
|
223
|
+
const props = collectProperties(spec);
|
|
224
|
+
const result = {};
|
|
225
|
+
for (const prop of props) {
|
|
226
|
+
const tsValue = obj[prop.ts];
|
|
227
|
+
if (tsValue == null && prop.optional) {
|
|
228
|
+
result[prop.json] = null;
|
|
229
|
+
continue;
|
|
230
|
+
}
|
|
231
|
+
const conv = resolveConverter(prop.type, customConverters);
|
|
232
|
+
result[prop.json] = conv.serialize(tsValue);
|
|
233
|
+
}
|
|
234
|
+
return result;
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Deserialize unknown data (from JSON.parse) into a typed instance using the spec.
|
|
238
|
+
*/
|
|
239
|
+
export function deserializeWithSpec(data, spec, customConverters) {
|
|
240
|
+
if (data == null || typeof data !== "object") {
|
|
241
|
+
throw new Error(`Expected object for deserialization, got ${data === null ? "null" : typeof data}`);
|
|
242
|
+
}
|
|
243
|
+
const raw = data;
|
|
244
|
+
const props = collectProperties(spec);
|
|
245
|
+
const deserialized = {};
|
|
246
|
+
for (const prop of props) {
|
|
247
|
+
const jsonValue = raw[prop.json];
|
|
248
|
+
if (jsonValue === undefined || jsonValue === null) {
|
|
249
|
+
if (prop.optional) {
|
|
250
|
+
deserialized[prop.ts] = null;
|
|
251
|
+
continue;
|
|
252
|
+
}
|
|
253
|
+
if (jsonValue === undefined) {
|
|
254
|
+
throw new Error(`Missing required property: "${prop.json}"`);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
const conv = resolveConverter(prop.type, customConverters);
|
|
258
|
+
deserialized[prop.ts] = conv.deserialize(jsonValue);
|
|
259
|
+
}
|
|
260
|
+
return spec.factory(deserialized);
|
|
261
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export type { TypeDescriptor, PrimitiveDescriptor, RefDescriptor, TemporalDescriptor, DecimalDescriptor, MapDescriptor, ArrayDescriptor, HashSetDescriptor, BrandedDescriptor, EnumDescriptor, NumericEnumDescriptor, NullableDescriptor, PropertySpec, TypeSpec, JsonConverter, SerializerContextOptions, } from "./types.ts";
|
|
2
|
+
export { SerializerContext } from "./serializer-context.ts";
|
|
3
|
+
export { JsonSerializer, BoundSerializer } from "./json-serializer.ts";
|
|
4
|
+
export { PropertyNamingPolicy } from "./property-naming-policy.ts";
|
|
5
|
+
export { collectProperties } from "./converters.ts";
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { TypeSpec, JsonConverter } from "./types.ts";
|
|
2
|
+
import type { SerializerContext } from "./serializer-context.ts";
|
|
3
|
+
/**
|
|
4
|
+
* A bound serializer that uses a SerializerContext to resolve custom converters
|
|
5
|
+
* and look up specs by constructor.
|
|
6
|
+
*/
|
|
7
|
+
export declare class BoundSerializer {
|
|
8
|
+
private readonly context;
|
|
9
|
+
constructor(context: SerializerContext);
|
|
10
|
+
/** Serialize a TS object to a JSON-safe plain object. */
|
|
11
|
+
serialize<T>(value: T, spec: TypeSpec<T>): Record<string, unknown>;
|
|
12
|
+
/** Deserialize unknown data into a typed instance. */
|
|
13
|
+
deserialize<T>(data: unknown, spec: TypeSpec<T>): T;
|
|
14
|
+
/** Resolve a spec by constructor — throws if not registered. */
|
|
15
|
+
specFor<T>(type: abstract new (...args: any[]) => T): TypeSpec<T>;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Static serializer — mirrors the C# `JsonSerializer` API.
|
|
19
|
+
*
|
|
20
|
+
* Usage:
|
|
21
|
+
* ```ts
|
|
22
|
+
* // Direct with spec
|
|
23
|
+
* const json = JsonSerializer.serialize(order, JsonContext.default.order);
|
|
24
|
+
* const order = JsonSerializer.deserialize(data, JsonContext.default.order);
|
|
25
|
+
*
|
|
26
|
+
* // With context (for custom converters + spec lookup)
|
|
27
|
+
* const s = JsonSerializer.withContext(JsonContext.default);
|
|
28
|
+
* const json = s.serialize(order, s.specFor(Order));
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export declare class JsonSerializer {
|
|
32
|
+
private constructor();
|
|
33
|
+
/** Serialize a TS object to a JSON-safe plain object using the given spec. */
|
|
34
|
+
static serialize<T>(value: T, spec: TypeSpec<T>, converters?: readonly JsonConverter[]): Record<string, unknown>;
|
|
35
|
+
/** Deserialize unknown data into a typed instance using the given spec. */
|
|
36
|
+
static deserialize<T>(data: unknown, spec: TypeSpec<T>, converters?: readonly JsonConverter[]): T;
|
|
37
|
+
/** Create a bound serializer that uses the context's converters and registry. */
|
|
38
|
+
static withContext(context: SerializerContext): BoundSerializer;
|
|
39
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { serializeWithSpec, deserializeWithSpec } from "./converters.js";
|
|
2
|
+
/**
|
|
3
|
+
* A bound serializer that uses a SerializerContext to resolve custom converters
|
|
4
|
+
* and look up specs by constructor.
|
|
5
|
+
*/
|
|
6
|
+
export class BoundSerializer {
|
|
7
|
+
context;
|
|
8
|
+
constructor(context) {
|
|
9
|
+
this.context = context;
|
|
10
|
+
}
|
|
11
|
+
/** Serialize a TS object to a JSON-safe plain object. */
|
|
12
|
+
serialize(value, spec) {
|
|
13
|
+
return serializeWithSpec(value, spec, this.context.converters);
|
|
14
|
+
}
|
|
15
|
+
/** Deserialize unknown data into a typed instance. */
|
|
16
|
+
deserialize(data, spec) {
|
|
17
|
+
return deserializeWithSpec(data, spec, this.context.converters);
|
|
18
|
+
}
|
|
19
|
+
/** Resolve a spec by constructor — throws if not registered. */
|
|
20
|
+
specFor(type) {
|
|
21
|
+
const spec = this.context.resolve(type);
|
|
22
|
+
if (!spec) {
|
|
23
|
+
throw new Error(`No TypeSpec registered for ${type.name}. ` +
|
|
24
|
+
`Make sure the type is listed in [JsonSerializable] on the context.`);
|
|
25
|
+
}
|
|
26
|
+
return spec;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Static serializer — mirrors the C# `JsonSerializer` API.
|
|
31
|
+
*
|
|
32
|
+
* Usage:
|
|
33
|
+
* ```ts
|
|
34
|
+
* // Direct with spec
|
|
35
|
+
* const json = JsonSerializer.serialize(order, JsonContext.default.order);
|
|
36
|
+
* const order = JsonSerializer.deserialize(data, JsonContext.default.order);
|
|
37
|
+
*
|
|
38
|
+
* // With context (for custom converters + spec lookup)
|
|
39
|
+
* const s = JsonSerializer.withContext(JsonContext.default);
|
|
40
|
+
* const json = s.serialize(order, s.specFor(Order));
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
export class JsonSerializer {
|
|
44
|
+
constructor() { }
|
|
45
|
+
/** Serialize a TS object to a JSON-safe plain object using the given spec. */
|
|
46
|
+
static serialize(value, spec, converters) {
|
|
47
|
+
return serializeWithSpec(value, spec, converters);
|
|
48
|
+
}
|
|
49
|
+
/** Deserialize unknown data into a typed instance using the given spec. */
|
|
50
|
+
static deserialize(data, spec, converters) {
|
|
51
|
+
return deserializeWithSpec(data, spec, converters);
|
|
52
|
+
}
|
|
53
|
+
/** Create a bound serializer that uses the context's converters and registry. */
|
|
54
|
+
static withContext(context) {
|
|
55
|
+
return new BoundSerializer(context);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Naming policy for converting C# PascalCase property names to JSON wire names.
|
|
3
|
+
*
|
|
4
|
+
* The transpiler pre-computes all JSON names at compile time, so this class is
|
|
5
|
+
* only needed when constructing a SerializerContext manually at runtime.
|
|
6
|
+
*/
|
|
7
|
+
export declare abstract class PropertyNamingPolicy {
|
|
8
|
+
abstract convert(name: string): string;
|
|
9
|
+
static readonly camelCase: PropertyNamingPolicy;
|
|
10
|
+
static readonly snakeCaseLower: PropertyNamingPolicy;
|
|
11
|
+
static readonly snakeCaseUpper: PropertyNamingPolicy;
|
|
12
|
+
static readonly kebabCaseLower: PropertyNamingPolicy;
|
|
13
|
+
static readonly kebabCaseUpper: PropertyNamingPolicy;
|
|
14
|
+
}
|