@palantir/pack.schema 0.0.1-beta.1
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/.turbo/turbo-lint.log +4 -0
- package/.turbo/turbo-transpileBrowser.log +5 -0
- package/.turbo/turbo-transpileCjs.log +5 -0
- package/.turbo/turbo-transpileEsm.log +5 -0
- package/.turbo/turbo-transpileTypes.log +5 -0
- package/.turbo/turbo-typecheck.log +4 -0
- package/LICENSE.txt +13 -0
- package/README.md +87 -0
- package/build/browser/index.js +176 -0
- package/build/browser/index.js.map +1 -0
- package/build/cjs/index.cjs +192 -0
- package/build/cjs/index.cjs.map +1 -0
- package/build/cjs/index.d.cts +131 -0
- package/build/esm/index.js +176 -0
- package/build/esm/index.js.map +1 -0
- package/build/types/__tests__/defineMigration.test.d.ts +1 -0
- package/build/types/__tests__/defineMigration.test.d.ts.map +1 -0
- package/build/types/__tests__/defineRecord.test.d.ts +1 -0
- package/build/types/__tests__/defineRecord.test.d.ts.map +1 -0
- package/build/types/__tests__/defineUnion.test.d.ts +1 -0
- package/build/types/__tests__/defineUnion.test.d.ts.map +1 -0
- package/build/types/__tests__/integration.test.d.ts +1 -0
- package/build/types/__tests__/integration.test.d.ts.map +1 -0
- package/build/types/__tests__/primitives.test.d.ts +1 -0
- package/build/types/__tests__/primitives.test.d.ts.map +1 -0
- package/build/types/__tests__/testTypeUtils.d.ts +74 -0
- package/build/types/__tests__/testTypeUtils.d.ts.map +1 -0
- package/build/types/defineMigration.d.ts +21 -0
- package/build/types/defineMigration.d.ts.map +1 -0
- package/build/types/defineRecord.d.ts +14 -0
- package/build/types/defineRecord.d.ts.map +1 -0
- package/build/types/defineUnion.d.ts +12 -0
- package/build/types/defineUnion.d.ts.map +1 -0
- package/build/types/defs.d.ts +24 -0
- package/build/types/defs.d.ts.map +1 -0
- package/build/types/index.d.ts +6 -0
- package/build/types/index.d.ts.map +1 -0
- package/build/types/primitives.d.ts +41 -0
- package/build/types/primitives.d.ts.map +1 -0
- package/build/types/utils.d.ts +20 -0
- package/build/types/utils.d.ts.map +1 -0
- package/package.json +55 -0
- package/src/__tests__/defineMigration.test.ts +265 -0
- package/src/__tests__/defineRecord.test.ts +167 -0
- package/src/__tests__/defineUnion.test.ts +231 -0
- package/src/__tests__/integration.test.ts +126 -0
- package/src/__tests__/primitives.test.ts +153 -0
- package/src/__tests__/testTypeUtils.ts +92 -0
- package/src/defineMigration.ts +159 -0
- package/src/defineRecord.ts +83 -0
- package/src/defineUnion.ts +58 -0
- package/src/defs.ts +46 -0
- package/src/index.ts +22 -0
- package/src/primitives.ts +80 -0
- package/src/utils.ts +62 -0
- package/tsconfig.json +21 -0
- package/vitest.config.mjs +26 -0
package/LICENSE.txt
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
Copyright 2025 Palantir Technologies, Inc.
|
|
2
|
+
|
|
3
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
you may not use this file except in compliance with the License.
|
|
5
|
+
You may obtain a copy of the License at
|
|
6
|
+
|
|
7
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
|
|
9
|
+
Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
See the License for the specific language governing permissions and
|
|
13
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
# @palantir/pack.schema
|
|
2
|
+
|
|
3
|
+
Programmatic API for defining schemas using TypeScript builders, providing type-safe schema definition with records, unions, and migrations.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
This package provides builder functions and types for defining document schemas programmatically in TypeScript. It offers an alternative to YAML-based schema definitions, allowing for type-safe schema construction with full IDE support.
|
|
8
|
+
|
|
9
|
+
## Key Exports
|
|
10
|
+
|
|
11
|
+
### Schema Definition Functions
|
|
12
|
+
|
|
13
|
+
- `defineRecord(name, config)` - Define record types with fields and documentation
|
|
14
|
+
- `defineUnion(name, variants)` - Define discriminated unions of record types
|
|
15
|
+
- `defineMigration(previousSchema, migrationFn)` - Define schema migrations
|
|
16
|
+
|
|
17
|
+
### Type System
|
|
18
|
+
|
|
19
|
+
- `Type` - Base type for all PACK field types
|
|
20
|
+
- `String`, `Double` - Primitive field types
|
|
21
|
+
- `Array<T>`, `Optional<T>` - Composite field types
|
|
22
|
+
- `Ref` - References to other defined records/unions
|
|
23
|
+
|
|
24
|
+
### Utilities
|
|
25
|
+
|
|
26
|
+
- Field validation and resolution utilities
|
|
27
|
+
- Schema conversion and processing functions
|
|
28
|
+
|
|
29
|
+
## Usage
|
|
30
|
+
|
|
31
|
+
```typescript
|
|
32
|
+
import * as P from "@pack/document-schema-api";
|
|
33
|
+
|
|
34
|
+
// Define a record type
|
|
35
|
+
const Position = P.defineRecord("Position", {
|
|
36
|
+
docs: "A position in 2D space",
|
|
37
|
+
fields: {
|
|
38
|
+
x: P.Double,
|
|
39
|
+
y: P.Double,
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
// Define a record with optional fields and references
|
|
44
|
+
const Node = P.defineRecord("Node", {
|
|
45
|
+
docs: "A node in the graph",
|
|
46
|
+
extends: [Position],
|
|
47
|
+
fields: {
|
|
48
|
+
label: P.Optional(P.String),
|
|
49
|
+
connections: P.Array(Ref(() => Node)), // Forward reference
|
|
50
|
+
},
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
// Define a union type
|
|
54
|
+
const Shape = P.defineUnion("Shape", {
|
|
55
|
+
circle: Circle,
|
|
56
|
+
rectangle: Rectangle,
|
|
57
|
+
node: Node,
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
// Define a migration
|
|
61
|
+
const v2Schema = P.defineMigration(v1Schema, schema => {
|
|
62
|
+
// Migration logic
|
|
63
|
+
return schema;
|
|
64
|
+
});
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Field Types
|
|
68
|
+
|
|
69
|
+
### Primitives
|
|
70
|
+
|
|
71
|
+
- `String` - String values
|
|
72
|
+
- `Double` - Numeric values
|
|
73
|
+
- `Unknown` - Any value type
|
|
74
|
+
|
|
75
|
+
### Composites
|
|
76
|
+
|
|
77
|
+
- `Array<T>` - Arrays of type T
|
|
78
|
+
- `Optional<T>` - Optional values of type T
|
|
79
|
+
- `Ref<T>` - References to other defined types
|
|
80
|
+
|
|
81
|
+
## Features
|
|
82
|
+
|
|
83
|
+
- **Type Safety** - Full TypeScript type checking during schema definition
|
|
84
|
+
- **Forward References** - Support for circular/forward references using functions
|
|
85
|
+
- **Schema Evolution** - Migration system for schema versioning
|
|
86
|
+
- **IDE Support** - IntelliSense and auto-completion for schema building
|
|
87
|
+
- **Validation** - Runtime validation of schema definitions
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
// src/defs.ts
|
|
2
|
+
var ModelDefType = {
|
|
3
|
+
RECORD: "record",
|
|
4
|
+
UNION: "union"
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
// src/primitives.ts
|
|
8
|
+
var TypeKind = {
|
|
9
|
+
ARRAY: "array",
|
|
10
|
+
DOUBLE: "double",
|
|
11
|
+
OPTIONAL: "optional",
|
|
12
|
+
REF: "ref",
|
|
13
|
+
STRING: "string",
|
|
14
|
+
UNKNOWN: "unknown"
|
|
15
|
+
};
|
|
16
|
+
var String = {
|
|
17
|
+
type: TypeKind.STRING
|
|
18
|
+
};
|
|
19
|
+
var Double = {
|
|
20
|
+
type: TypeKind.DOUBLE
|
|
21
|
+
};
|
|
22
|
+
var Unknown = {
|
|
23
|
+
type: TypeKind.UNKNOWN
|
|
24
|
+
};
|
|
25
|
+
var Array = (item) => ({
|
|
26
|
+
type: TypeKind.ARRAY,
|
|
27
|
+
items: item
|
|
28
|
+
});
|
|
29
|
+
var Optional = (item) => ({
|
|
30
|
+
type: TypeKind.OPTIONAL,
|
|
31
|
+
item
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
// src/utils.ts
|
|
35
|
+
function modelToRef(model) {
|
|
36
|
+
return createRef(model.name, model.type);
|
|
37
|
+
}
|
|
38
|
+
function createRef(name, refType) {
|
|
39
|
+
return {
|
|
40
|
+
type: TypeKind.REF,
|
|
41
|
+
name,
|
|
42
|
+
refType
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
function isRecordDef(value) {
|
|
46
|
+
return value != null && typeof value === "object" && "type" in value && value.type === ModelDefType.RECORD && "fields" in value;
|
|
47
|
+
}
|
|
48
|
+
function isUnionDef(value) {
|
|
49
|
+
return value != null && typeof value === "object" && "type" in value && value.type === ModelDefType.UNION && "variants" in value;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// src/defineUnion.ts
|
|
53
|
+
function defineUnion(name, union) {
|
|
54
|
+
const entries = Object.entries(union.variants).map(([key, value]) => [key, resolveUnionVariantArg(value)]);
|
|
55
|
+
const variants = Object.fromEntries(entries);
|
|
56
|
+
return {
|
|
57
|
+
type: ModelDefType.UNION,
|
|
58
|
+
discriminant: "type",
|
|
59
|
+
name,
|
|
60
|
+
docs: union.docs,
|
|
61
|
+
variants
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
function resolveUnionVariantArg(arg) {
|
|
65
|
+
const resolved = typeof arg === "function" ? arg() : arg;
|
|
66
|
+
return resolved.type === TypeKind.REF ? resolved : modelToRef(resolved);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// src/defineMigration.ts
|
|
70
|
+
var UnionBuilderImpl = class _UnionBuilderImpl {
|
|
71
|
+
name;
|
|
72
|
+
docs;
|
|
73
|
+
variants;
|
|
74
|
+
constructor({
|
|
75
|
+
name,
|
|
76
|
+
docs,
|
|
77
|
+
variants
|
|
78
|
+
}) {
|
|
79
|
+
this.name = name;
|
|
80
|
+
this.docs = docs;
|
|
81
|
+
this.variants = {
|
|
82
|
+
...variants
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
addVariant(variantName, modelDefOrRef) {
|
|
86
|
+
const ref = resolveUnionVariantArg(modelDefOrRef);
|
|
87
|
+
const newVariants = {
|
|
88
|
+
...this.variants,
|
|
89
|
+
[variantName]: ref
|
|
90
|
+
};
|
|
91
|
+
return new _UnionBuilderImpl({
|
|
92
|
+
type: ModelDefType.UNION,
|
|
93
|
+
name: this.name,
|
|
94
|
+
variants: newVariants,
|
|
95
|
+
discriminant: "type"
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
build() {
|
|
99
|
+
return {
|
|
100
|
+
type: ModelDefType.UNION,
|
|
101
|
+
variants: this.variants,
|
|
102
|
+
discriminant: "type",
|
|
103
|
+
name: this.name
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
var RecordBuilderImpl = class _RecordBuilderImpl {
|
|
108
|
+
name;
|
|
109
|
+
fields;
|
|
110
|
+
constructor(initialRecordDef) {
|
|
111
|
+
this.name = initialRecordDef.name;
|
|
112
|
+
this.fields = {
|
|
113
|
+
...initialRecordDef.fields
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
// TODO: resolve field value from arg instead
|
|
117
|
+
addField(name, value) {
|
|
118
|
+
return new _RecordBuilderImpl({
|
|
119
|
+
type: ModelDefType.RECORD,
|
|
120
|
+
name: this.name,
|
|
121
|
+
fields: {
|
|
122
|
+
...this.fields,
|
|
123
|
+
[name]: value
|
|
124
|
+
},
|
|
125
|
+
docs: ""
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
build() {
|
|
129
|
+
return {
|
|
130
|
+
type: ModelDefType.RECORD,
|
|
131
|
+
name: this.name,
|
|
132
|
+
fields: this.fields,
|
|
133
|
+
docs: ""
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
function defineMigration(previous, migration) {
|
|
138
|
+
const builders = {};
|
|
139
|
+
for (const key in previous) {
|
|
140
|
+
const v = previous[key];
|
|
141
|
+
if (isRecordDef(v)) {
|
|
142
|
+
const recordBuilder = new RecordBuilderImpl(v);
|
|
143
|
+
builders[key] = recordBuilder;
|
|
144
|
+
} else if (isUnionDef(v)) {
|
|
145
|
+
const unionBuilder = new UnionBuilderImpl(v);
|
|
146
|
+
builders[key] = unionBuilder;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
return {
|
|
150
|
+
...previous,
|
|
151
|
+
...migration(builders)
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// src/defineRecord.ts
|
|
156
|
+
function resolveFieldArg(value) {
|
|
157
|
+
const resolved = typeof value === "function" ? value() : value;
|
|
158
|
+
if (isRecordDef(resolved) || isUnionDef(resolved)) {
|
|
159
|
+
return modelToRef(resolved);
|
|
160
|
+
}
|
|
161
|
+
return resolved;
|
|
162
|
+
}
|
|
163
|
+
function defineRecord(name, record) {
|
|
164
|
+
const entries = Object.entries(record.fields).map(([key, value]) => [key, resolveFieldArg(value)]);
|
|
165
|
+
const fields = Object.fromEntries(entries);
|
|
166
|
+
return {
|
|
167
|
+
type: ModelDefType.RECORD,
|
|
168
|
+
name,
|
|
169
|
+
docs: record.docs,
|
|
170
|
+
fields
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export { Array, Double, ModelDefType, Optional, String, TypeKind, Unknown, createRef, defineMigration, defineRecord, defineUnion, isRecordDef, isUnionDef, modelToRef, resolveUnionVariantArg };
|
|
175
|
+
//# sourceMappingURL=index.js.map
|
|
176
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/defs.ts","../../src/primitives.ts","../../src/utils.ts","../../src/defineUnion.ts","../../src/defineMigration.ts","../../src/defineRecord.ts"],"names":[],"mappings":";AAgBO,IAAM,YAAA,GAAe;AAAA,EAC1B,MAAA,EAAQ,QAAA;AAAA,EACR,KAAA,EAAO;AACT;;;ACHO,IAAM,QAAA,GA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c. All rights reserved.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nexport const ModelDefType = {\n RECORD: \"record\",\n UNION: \"union\"\n};","/*\n * Copyright 2025 Palantir Technologies, Inc. All rights reserved.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nexport const TypeKind = {\n ARRAY: \"array\",\n DOUBLE: \"double\",\n OPTIONAL: \"optional\",\n REF: \"ref\",\n STRING: \"string\",\n UNKNOWN: \"unknown\"\n};\n\n// Forward declaration to avoid circular reference\n\n// A reference to a record or a union type\n\nexport const String = {\n type: TypeKind.STRING\n};\nexport const Double = {\n type: TypeKind.DOUBLE\n};\nexport const Unknown = {\n type: TypeKind.UNKNOWN\n};\nexport const Array = item => ({\n type: TypeKind.ARRAY,\n items: item\n});\nexport const Optional = item => ({\n type: TypeKind.OPTIONAL,\n item\n});","/*\n * Copyright 2025 Palantir Technologies, Inc. All rights reserved.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { ModelDefType } from \"./defs.js\";\nimport { TypeKind } from \"./primitives.js\";\n\n/**\n * Helper function to safely transform a record definition to a reference.\n * This function helps TypeScript track type transformations better.\n */\nexport function modelToRef(model) {\n return createRef(model.name, model.type);\n}\n\n/**\n * Helper function to create a reference with proper typing.\n * This avoids type assertions in multiple places.\n */\nexport function createRef(name, refType) {\n return {\n type: TypeKind.REF,\n name,\n refType\n };\n}\n\n/**\n * Type guard to check if a value is a RecordDef\n */\nexport function isRecordDef(value) {\n return value != null && typeof value === \"object\" && \"type\" in value && value.type === ModelDefType.RECORD && \"fields\" in value;\n}\n\n/**\n * Type guard to check if a value is a UnionDef\n */\nexport function isUnionDef(value) {\n return value != null && typeof value === \"object\" && \"type\" in value && value.type === ModelDefType.UNION && \"variants\" in value;\n}","/*\n * Copyright 2025 Palantir Technologies, Inc. All rights reserved.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { ModelDefType } from \"./defs.js\";\nimport { TypeKind } from \"./primitives.js\";\nimport { modelToRef } from \"./utils.js\";\nexport function defineUnion(name, union) {\n const entries = Object.entries(union.variants).map(([key, value]) => [key, resolveUnionVariantArg(value)]);\n const variants = Object.fromEntries(entries);\n return {\n type: ModelDefType.UNION,\n discriminant: \"type\",\n name: name,\n docs: union.docs,\n variants\n };\n}\nexport function resolveUnionVariantArg(arg) {\n const resolved = typeof arg === \"function\" ? arg() : arg;\n return resolved.type === TypeKind.REF ? resolved : modelToRef(resolved);\n}","/*\n * Copyright 2025 Palantir Technologies, Inc. All rights reserved.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { resolveUnionVariantArg } from \"./defineUnion.js\";\nimport { ModelDefType } from \"./defs.js\";\nimport { isRecordDef, isUnionDef } from \"./utils.js\";\nclass UnionBuilderImpl {\n name;\n docs;\n variants;\n constructor({\n name,\n docs,\n variants\n }) {\n this.name = name;\n this.docs = docs;\n this.variants = {\n ...variants\n };\n }\n addVariant(variantName, modelDefOrRef) {\n const ref = resolveUnionVariantArg(modelDefOrRef);\n const newVariants = {\n ...this.variants,\n [variantName]: ref\n };\n return new UnionBuilderImpl({\n type: ModelDefType.UNION,\n name: this.name,\n variants: newVariants,\n discriminant: \"type\"\n });\n }\n build() {\n return {\n type: ModelDefType.UNION,\n variants: this.variants,\n discriminant: \"type\",\n name: this.name\n };\n }\n}\nclass RecordBuilderImpl {\n name;\n fields;\n constructor(initialRecordDef) {\n this.name = initialRecordDef.name;\n this.fields = {\n ...initialRecordDef.fields\n };\n }\n\n // TODO: resolve field value from arg instead\n addField(name, value) {\n return new RecordBuilderImpl({\n type: ModelDefType.RECORD,\n name: this.name,\n fields: {\n ...this.fields,\n [name]: value\n },\n docs: \"\"\n });\n }\n build() {\n return {\n type: ModelDefType.RECORD,\n name: this.name,\n fields: this.fields,\n docs: \"\"\n };\n }\n}\nexport function defineMigration(previous, migration) {\n /**\n * Create a map of builders for the schema.\n *\n * Note about type assertions:\n * We use a type assertion here (SchemaBuilder<T>) because TypeScript cannot\n * track the relationship between the keys in the original schema and the\n * corresponding builder types. This is a fundamental limitation of TypeScript's\n * type system when dealing with dynamically constructed objects.\n *\n * The assertion is safe because:\n * 1. We're only adding keys that exist in the original schema\n * 2. For each key, we're creating a builder of the correct type\n * 3. The resulting object exactly matches the SchemaBuilder<T> type\n */\n const builders = {};\n for (const key in previous) {\n const v = previous[key];\n\n // Use our type guards for safer type narrowing\n if (isRecordDef(v)) {\n // This assertion is necessary because TypeScript can't track the field types\n // through the dynamic property access and object construction\n const recordBuilder = new RecordBuilderImpl(v);\n builders[key] = recordBuilder;\n } else if (isUnionDef(v)) {\n // Similar assertion needed for union builders\n const unionBuilder = new UnionBuilderImpl(v);\n builders[key] = unionBuilder;\n }\n }\n return {\n ...previous,\n ...migration(builders)\n };\n}","/*\n * Copyright 2025 Palantir Technologies, Inc. All rights reserved.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { ModelDefType } from \"./defs.js\";\nimport { isRecordDef, isUnionDef, modelToRef } from \"./utils.js\";\n/**\n * Helper function to resolve a field value, handling functions and transforming\n * types as needed.\n */\nfunction resolveFieldArg(value) {\n // Handle function values\n const resolved = typeof value === \"function\" ? value() : value;\n if (isRecordDef(resolved) || isUnionDef(resolved)) {\n return modelToRef(resolved);\n }\n\n // For primitive types and other Types\n return resolved;\n}\nexport function defineRecord(name, record) {\n const entries = Object.entries(record.fields).map(([key, value]) => [key, resolveFieldArg(value)]);\n const fields = Object.fromEntries(entries);\n return {\n type: ModelDefType.RECORD,\n name: name,\n docs: record.docs,\n fields\n };\n}"]}
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/defs.ts
|
|
4
|
+
var ModelDefType = {
|
|
5
|
+
RECORD: "record",
|
|
6
|
+
UNION: "union"
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
// src/primitives.ts
|
|
10
|
+
var TypeKind = {
|
|
11
|
+
ARRAY: "array",
|
|
12
|
+
DOUBLE: "double",
|
|
13
|
+
OPTIONAL: "optional",
|
|
14
|
+
REF: "ref",
|
|
15
|
+
STRING: "string",
|
|
16
|
+
UNKNOWN: "unknown"
|
|
17
|
+
};
|
|
18
|
+
var String = {
|
|
19
|
+
type: TypeKind.STRING
|
|
20
|
+
};
|
|
21
|
+
var Double = {
|
|
22
|
+
type: TypeKind.DOUBLE
|
|
23
|
+
};
|
|
24
|
+
var Unknown = {
|
|
25
|
+
type: TypeKind.UNKNOWN
|
|
26
|
+
};
|
|
27
|
+
var Array = (item) => ({
|
|
28
|
+
type: TypeKind.ARRAY,
|
|
29
|
+
items: item
|
|
30
|
+
});
|
|
31
|
+
var Optional = (item) => ({
|
|
32
|
+
type: TypeKind.OPTIONAL,
|
|
33
|
+
item
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
// src/utils.ts
|
|
37
|
+
function modelToRef(model) {
|
|
38
|
+
return createRef(model.name, model.type);
|
|
39
|
+
}
|
|
40
|
+
function createRef(name, refType) {
|
|
41
|
+
return {
|
|
42
|
+
type: TypeKind.REF,
|
|
43
|
+
name,
|
|
44
|
+
refType
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
function isRecordDef(value) {
|
|
48
|
+
return value != null && typeof value === "object" && "type" in value && value.type === ModelDefType.RECORD && "fields" in value;
|
|
49
|
+
}
|
|
50
|
+
function isUnionDef(value) {
|
|
51
|
+
return value != null && typeof value === "object" && "type" in value && value.type === ModelDefType.UNION && "variants" in value;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// src/defineUnion.ts
|
|
55
|
+
function defineUnion(name, union) {
|
|
56
|
+
const entries = Object.entries(union.variants).map(([key, value]) => [key, resolveUnionVariantArg(value)]);
|
|
57
|
+
const variants = Object.fromEntries(entries);
|
|
58
|
+
return {
|
|
59
|
+
type: ModelDefType.UNION,
|
|
60
|
+
discriminant: "type",
|
|
61
|
+
name,
|
|
62
|
+
docs: union.docs,
|
|
63
|
+
variants
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
function resolveUnionVariantArg(arg) {
|
|
67
|
+
const resolved = typeof arg === "function" ? arg() : arg;
|
|
68
|
+
return resolved.type === TypeKind.REF ? resolved : modelToRef(resolved);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// src/defineMigration.ts
|
|
72
|
+
var UnionBuilderImpl = class _UnionBuilderImpl {
|
|
73
|
+
name;
|
|
74
|
+
docs;
|
|
75
|
+
variants;
|
|
76
|
+
constructor({
|
|
77
|
+
name,
|
|
78
|
+
docs,
|
|
79
|
+
variants
|
|
80
|
+
}) {
|
|
81
|
+
this.name = name;
|
|
82
|
+
this.docs = docs;
|
|
83
|
+
this.variants = {
|
|
84
|
+
...variants
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
addVariant(variantName, modelDefOrRef) {
|
|
88
|
+
const ref = resolveUnionVariantArg(modelDefOrRef);
|
|
89
|
+
const newVariants = {
|
|
90
|
+
...this.variants,
|
|
91
|
+
[variantName]: ref
|
|
92
|
+
};
|
|
93
|
+
return new _UnionBuilderImpl({
|
|
94
|
+
type: ModelDefType.UNION,
|
|
95
|
+
name: this.name,
|
|
96
|
+
variants: newVariants,
|
|
97
|
+
discriminant: "type"
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
build() {
|
|
101
|
+
return {
|
|
102
|
+
type: ModelDefType.UNION,
|
|
103
|
+
variants: this.variants,
|
|
104
|
+
discriminant: "type",
|
|
105
|
+
name: this.name
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
var RecordBuilderImpl = class _RecordBuilderImpl {
|
|
110
|
+
name;
|
|
111
|
+
fields;
|
|
112
|
+
constructor(initialRecordDef) {
|
|
113
|
+
this.name = initialRecordDef.name;
|
|
114
|
+
this.fields = {
|
|
115
|
+
...initialRecordDef.fields
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
// TODO: resolve field value from arg instead
|
|
119
|
+
addField(name, value) {
|
|
120
|
+
return new _RecordBuilderImpl({
|
|
121
|
+
type: ModelDefType.RECORD,
|
|
122
|
+
name: this.name,
|
|
123
|
+
fields: {
|
|
124
|
+
...this.fields,
|
|
125
|
+
[name]: value
|
|
126
|
+
},
|
|
127
|
+
docs: ""
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
build() {
|
|
131
|
+
return {
|
|
132
|
+
type: ModelDefType.RECORD,
|
|
133
|
+
name: this.name,
|
|
134
|
+
fields: this.fields,
|
|
135
|
+
docs: ""
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
function defineMigration(previous, migration) {
|
|
140
|
+
const builders = {};
|
|
141
|
+
for (const key in previous) {
|
|
142
|
+
const v = previous[key];
|
|
143
|
+
if (isRecordDef(v)) {
|
|
144
|
+
const recordBuilder = new RecordBuilderImpl(v);
|
|
145
|
+
builders[key] = recordBuilder;
|
|
146
|
+
} else if (isUnionDef(v)) {
|
|
147
|
+
const unionBuilder = new UnionBuilderImpl(v);
|
|
148
|
+
builders[key] = unionBuilder;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
return {
|
|
152
|
+
...previous,
|
|
153
|
+
...migration(builders)
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// src/defineRecord.ts
|
|
158
|
+
function resolveFieldArg(value) {
|
|
159
|
+
const resolved = typeof value === "function" ? value() : value;
|
|
160
|
+
if (isRecordDef(resolved) || isUnionDef(resolved)) {
|
|
161
|
+
return modelToRef(resolved);
|
|
162
|
+
}
|
|
163
|
+
return resolved;
|
|
164
|
+
}
|
|
165
|
+
function defineRecord(name, record) {
|
|
166
|
+
const entries = Object.entries(record.fields).map(([key, value]) => [key, resolveFieldArg(value)]);
|
|
167
|
+
const fields = Object.fromEntries(entries);
|
|
168
|
+
return {
|
|
169
|
+
type: ModelDefType.RECORD,
|
|
170
|
+
name,
|
|
171
|
+
docs: record.docs,
|
|
172
|
+
fields
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
exports.Array = Array;
|
|
177
|
+
exports.Double = Double;
|
|
178
|
+
exports.ModelDefType = ModelDefType;
|
|
179
|
+
exports.Optional = Optional;
|
|
180
|
+
exports.String = String;
|
|
181
|
+
exports.TypeKind = TypeKind;
|
|
182
|
+
exports.Unknown = Unknown;
|
|
183
|
+
exports.createRef = createRef;
|
|
184
|
+
exports.defineMigration = defineMigration;
|
|
185
|
+
exports.defineRecord = defineRecord;
|
|
186
|
+
exports.defineUnion = defineUnion;
|
|
187
|
+
exports.isRecordDef = isRecordDef;
|
|
188
|
+
exports.isUnionDef = isUnionDef;
|
|
189
|
+
exports.modelToRef = modelToRef;
|
|
190
|
+
exports.resolveUnionVariantArg = resolveUnionVariantArg;
|
|
191
|
+
//# sourceMappingURL=index.cjs.map
|
|
192
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/defs.ts","../../src/primitives.ts","../../src/utils.ts","../../src/defineUnion.ts","../../src/defineMigration.ts","../../src/defineRecord.ts"],"names":[],"mappings":";;;AAgBO,IAAM,YAAA,GAAe;AAAA,EAC1B,MAAA,EAAQ,QAAA;AAAA,EACR,KAAA,EAAO;AACT;;;ACHO,IAAM,QAAA,GAAW;AAAA,EACtB,KAAA,EAAO,OAAA;AAAA,EACP,MAAA,EAAQ,QAAA;AAAA,EACR,QAAA,EAAU,UAAA;AAAA,EACV,GAAA,EAAK,KAAA;AAAA,EACL,MAAA,EAAQ,QAAA;AAAA,EACR,OAAA,EAAS;AACX;AAMO,IAAM,MAAA,GAAS;AAAA,EACpB,MAAM,QAAA,CAAS;AACjB;AACO,IAAM,MAAA,GAAS;AAAA,EACpB,MAAM,QAAA,CAAS;AACjB;AACO,IAAM,OAAA,GAAU;AAAA,EAC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AA;AAC3C,MAAA,QAAA,CAAS,GAAG,CAAA,GAAI,YAAA;AAAA,IAClB;AAAA,EACF;AACA,EAAA,OAAO;AAAA,IACL,GAAG,QAAA;AAAA,IACH,GAAG,UAAU,QAAQ;AAAA,GACvB;AACF;;;ACpGA,SAAS,gBAAgB,KAAA,EAAO;AAE9B,EAAA,MAAM,QAAA,GAAW,OAAO,KAAA,KAAU,UAAA,GAAa,OAAM,GAAI,KAAA;AACzD,EAAA,IAAI,WAAA,CAAY,QAAQ,CAAA,IAAK,UAAA,CAAW,QAAQ,CAAA,EAAG;AACjD,IAAA,OAAO,WAAW,QAAQ,CAAA;AAAA,EAC5B;AAGA,EAAA,OAAO,QAAA;AACT;AACO,SAAS,YAAA,CAAa,MAAM,MAAA,EAAQ;AACzC,EAAA,MAAM,UAAU,MAAA,CAAO,OAAA,CAAQ,MAAA,CAAO,MAAM,EAAE,GAAA,CAAI,CAAC,CAAC,GAAA,EAAK,KAAK,CAAA,KAAM,CAAC,KAAK,eAAA,CAAgB,KAAK,CAAC,CAAC,CAAA;AACjG,EAAA,MAAM,MAAA,GAAS,MAAA,CAAO,WAAA,CAAY,OAAO,CAAA;AACzC,EAAA,OAAO;AAAA,IACL,MAAM,YAAA,CAAa,MAAA;AAAA,IACnB,IAAA;AAAA,IACA,MAAM,MAAA,CAAO,IAAA;AAAA,IACb;AAAA,GACF;AACF","file":"index.cjs","sourcesContent":["/*\n * Copyright 2025 Palantir Technologies, Inc. All rights reserved.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nexport const ModelDefType = {\n RECORD: \"record\",\n UNION: \"union\"\n};","/*\n * Copyright 2025 Palantir Technologies, Inc. All rights reserved.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nexport const TypeKind = {\n ARRAY: \"array\",\n DOUBLE: \"double\",\n OPTIONAL: \"optional\",\n REF: \"ref\",\n STRING: \"string\",\n UNKNOWN: \"unknown\"\n};\n\n// Forward declaration to avoid circular reference\n\n// A reference to a record or a union type\n\nexport const String = {\n type: TypeKind.STRING\n};\nexport const Double = {\n type: TypeKind.DOUBLE\n};\nexport const Unknown = {\n type: TypeKind.UNKNOWN\n};\nexport const Array = item => ({\n type: TypeKind.ARRAY,\n items: item\n});\nexport const Optional = item => ({\n type: TypeKind.OPTIONAL,\n item\n});","/*\n * Copyright 2025 Palantir Technologies, Inc. All rights reserved.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { ModelDefType } from \"./defs.js\";\nimport { TypeKind } from \"./primitives.js\";\n\n/**\n * Helper function to safely transform a record definition to a reference.\n * This function helps TypeScript track type transformations better.\n */\nexport function modelToRef(model) {\n return createRef(model.name, model.type);\n}\n\n/**\n * Helper function to create a reference with proper typing.\n * This avoids type assertions in multiple places.\n */\nexport function createRef(name, refType) {\n return {\n type: TypeKind.REF,\n name,\n refType\n };\n}\n\n/**\n * Type guard to check if a value is a RecordDef\n */\nexport function isRecordDef(value) {\n return value != null && typeof value === \"object\" && \"type\" in value && value.type === ModelDefType.RECORD && \"fields\" in value;\n}\n\n/**\n * Type guard to check if a value is a UnionDef\n */\nexport function isUnionDef(value) {\n return value != null && typeof value === \"object\" && \"type\" in value && value.type === ModelDefType.UNION && \"variants\" in value;\n}","/*\n * Copyright 2025 Palantir Technologies, Inc. All rights reserved.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { ModelDefType } from \"./defs.js\";\nimport { TypeKind } from \"./primitives.js\";\nimport { modelToRef } from \"./utils.js\";\nexport function defineUnion(name, union) {\n const entries = Object.entries(union.variants).map(([key, value]) => [key, resolveUnionVariantArg(value)]);\n const variants = Object.fromEntries(entries);\n return {\n type: ModelDefType.UNION,\n discriminant: \"type\",\n name: name,\n docs: union.docs,\n variants\n };\n}\nexport function resolveUnionVariantArg(arg) {\n const resolved = typeof arg === \"function\" ? arg() : arg;\n return resolved.type === TypeKind.REF ? resolved : modelToRef(resolved);\n}","/*\n * Copyright 2025 Palantir Technologies, Inc. All rights reserved.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { resolveUnionVariantArg } from \"./defineUnion.js\";\nimport { ModelDefType } from \"./defs.js\";\nimport { isRecordDef, isUnionDef } from \"./utils.js\";\nclass UnionBuilderImpl {\n name;\n docs;\n variants;\n constructor({\n name,\n docs,\n variants\n }) {\n this.name = name;\n this.docs = docs;\n this.variants = {\n ...variants\n };\n }\n addVariant(variantName, modelDefOrRef) {\n const ref = resolveUnionVariantArg(modelDefOrRef);\n const newVariants = {\n ...this.variants,\n [variantName]: ref\n };\n return new UnionBuilderImpl({\n type: ModelDefType.UNION,\n name: this.name,\n variants: newVariants,\n discriminant: \"type\"\n });\n }\n build() {\n return {\n type: ModelDefType.UNION,\n variants: this.variants,\n discriminant: \"type\",\n name: this.name\n };\n }\n}\nclass RecordBuilderImpl {\n name;\n fields;\n constructor(initialRecordDef) {\n this.name = initialRecordDef.name;\n this.fields = {\n ...initialRecordDef.fields\n };\n }\n\n // TODO: resolve field value from arg instead\n addField(name, value) {\n return new RecordBuilderImpl({\n type: ModelDefType.RECORD,\n name: this.name,\n fields: {\n ...this.fields,\n [name]: value\n },\n docs: \"\"\n });\n }\n build() {\n return {\n type: ModelDefType.RECORD,\n name: this.name,\n fields: this.fields,\n docs: \"\"\n };\n }\n}\nexport function defineMigration(previous, migration) {\n /**\n * Create a map of builders for the schema.\n *\n * Note about type assertions:\n * We use a type assertion here (SchemaBuilder<T>) because TypeScript cannot\n * track the relationship between the keys in the original schema and the\n * corresponding builder types. This is a fundamental limitation of TypeScript's\n * type system when dealing with dynamically constructed objects.\n *\n * The assertion is safe because:\n * 1. We're only adding keys that exist in the original schema\n * 2. For each key, we're creating a builder of the correct type\n * 3. The resulting object exactly matches the SchemaBuilder<T> type\n */\n const builders = {};\n for (const key in previous) {\n const v = previous[key];\n\n // Use our type guards for safer type narrowing\n if (isRecordDef(v)) {\n // This assertion is necessary because TypeScript can't track the field types\n // through the dynamic property access and object construction\n const recordBuilder = new RecordBuilderImpl(v);\n builders[key] = recordBuilder;\n } else if (isUnionDef(v)) {\n // Similar assertion needed for union builders\n const unionBuilder = new UnionBuilderImpl(v);\n builders[key] = unionBuilder;\n }\n }\n return {\n ...previous,\n ...migration(builders)\n };\n}","/*\n * Copyright 2025 Palantir Technologies, Inc. All rights reserved.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { ModelDefType } from \"./defs.js\";\nimport { isRecordDef, isUnionDef, modelToRef } from \"./utils.js\";\n/**\n * Helper function to resolve a field value, handling functions and transforming\n * types as needed.\n */\nfunction resolveFieldArg(value) {\n // Handle function values\n const resolved = typeof value === \"function\" ? value() : value;\n if (isRecordDef(resolved) || isUnionDef(resolved)) {\n return modelToRef(resolved);\n }\n\n // For primitive types and other Types\n return resolved;\n}\nexport function defineRecord(name, record) {\n const entries = Object.entries(record.fields).map(([key, value]) => [key, resolveFieldArg(value)]);\n const fields = Object.fromEntries(entries);\n return {\n type: ModelDefType.RECORD,\n name: name,\n docs: record.docs,\n fields\n };\n}"]}
|