@atolis-hq/corum 0.1.22 → 0.1.23

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.
@@ -6,18 +6,35 @@ export function mapDocument(document, specPath) {
6
6
  const nodes = [];
7
7
  const edges = [];
8
8
  const diagnostics = [];
9
+ const allSchemas = (document.components?.schemas ?? {});
9
10
  for (const gap of document.gaps ?? []) {
10
11
  const msg = gap.nodeId
11
12
  ? `[${gap.kind}] ${gap.nodeId}: ${gap.reason ?? ''}`
12
13
  : `[${gap.kind}] ${gap.reason ?? ''}`;
13
14
  diagnostics.push({ severity: 'warning', file: specPath, message: msg });
14
15
  }
15
- for (const raw of document.nodes) {
16
- if (!raw.id || !raw.template) {
17
- diagnostics.push({ severity: 'warning', file: specPath, message: `Node missing id or template — skipping` });
16
+ for (const [nodeId, entry] of Object.entries(document.nodes)) {
17
+ if (!nodeId || !entry.type) {
18
+ diagnostics.push({ severity: 'warning', file: specPath, message: 'Node missing id or type — skipping' });
18
19
  continue;
19
20
  }
20
- nodes.push(mapNode(raw, specPath));
21
+ const component = nodeId.split('.')[0];
22
+ const properties = {};
23
+ let schemaName;
24
+ if (entry.schema?.$ref) {
25
+ schemaName = schemaRefName(entry.schema.$ref);
26
+ if (schemaName)
27
+ properties.schema = schemaName;
28
+ }
29
+ if (entry.title && !schemaName) {
30
+ properties.description = entry.title;
31
+ }
32
+ const node = makeNode(entry.type, component, specPath, nodeId, entry.provenance, properties);
33
+ nodes.push(node);
34
+ if (schemaName) {
35
+ const expanded = new Map();
36
+ expandSchema(schemaName, nodeId, allSchemas, nodes, edges, specPath, component, expanded);
37
+ }
21
38
  }
22
39
  for (const raw of document.edges ?? []) {
23
40
  if (!VALID_EDGE_TYPES.has(raw.type)) {
@@ -32,19 +49,156 @@ export function mapDocument(document, specPath) {
32
49
  }
33
50
  return { nodes, edges, diagnostics };
34
51
  }
35
- function mapNode(raw, specPath) {
52
+ function schemaRefName(ref) {
53
+ const prefix = '#/components/schemas/';
54
+ if (!ref.startsWith(prefix))
55
+ return undefined;
56
+ return ref.slice(prefix.length);
57
+ }
58
+ function expandSchema(schemaName, rootNodeId, allSchemas, nodes, edges, specPath, component, expanded) {
59
+ const existing = expanded.get(schemaName);
60
+ if (existing)
61
+ return existing;
62
+ const schemaId = `${rootNodeId}.schemas.${schemaName}`;
63
+ expanded.set(schemaName, schemaId);
64
+ const schemaDef = allSchemas[schemaName];
65
+ const schemaNode = makeNode('Schema', component, specPath, schemaId, undefined, {});
66
+ nodes.push(schemaNode);
67
+ edges.push(makeHasFieldEdge(rootNodeId, schemaId));
68
+ const localMappings = new Map();
69
+ for (const [fieldName, rawProp] of Object.entries(schemaDef?.properties ?? {})) {
70
+ const fieldId = `${schemaId}.fields.${fieldName}`;
71
+ const fieldNode = makeNode('Field', component, specPath, fieldId, undefined, {});
72
+ const required = Array.isArray(schemaDef?.required) && schemaDef.required.includes(fieldName);
73
+ fieldNode.properties = resolveFieldProperties(fieldName, rawProp, required, schemaId, rootNodeId, allSchemas, nodes, edges, specPath, component, expanded, localMappings);
74
+ nodes.push(fieldNode);
75
+ edges.push(makeHasFieldEdge(schemaId, fieldId));
76
+ }
77
+ return schemaId;
78
+ }
79
+ function resolveFieldProperties(fieldName, prop, required, schemaId, rootNodeId, allSchemas, nodes, edges, specPath, component, expanded, localMappings) {
80
+ const nullable = !required;
81
+ if (typeof prop.$ref === 'string') {
82
+ const refName = schemaRefName(prop.$ref);
83
+ if (refName) {
84
+ const refId = expandSchema(refName, rootNodeId, allSchemas, nodes, edges, specPath, component, expanded);
85
+ return { $ref: refId, nullable, collection: 'one' };
86
+ }
87
+ return { type: 'string', nullable, collection: 'one' };
88
+ }
89
+ const type = prop.type;
90
+ if (type === 'array') {
91
+ const items = prop.items;
92
+ if (items && typeof items.$ref === 'string') {
93
+ const refName = schemaRefName(items.$ref);
94
+ if (refName) {
95
+ const refId = expandSchema(refName, rootNodeId, allSchemas, nodes, edges, specPath, component, expanded);
96
+ return { $ref: refId, nullable, collection: 'array' };
97
+ }
98
+ }
99
+ const itemType = mapScalar(items?.type ?? 'string', items?.format);
100
+ return { type: itemType, nullable, collection: 'array' };
101
+ }
102
+ if (type === 'object' && prop.additionalProperties !== undefined) {
103
+ createMapping(fieldName, prop.additionalProperties, schemaId, rootNodeId, allSchemas, nodes, edges, specPath, component, expanded, localMappings);
104
+ return { $ref: `#/mappings/${fieldName}`, nullable, collection: 'one' };
105
+ }
106
+ const scalarType = mapScalar(type ?? 'string', prop.format);
107
+ return { type: scalarType, nullable, collection: 'one' };
108
+ }
109
+ function createMapping(mappingName, addlDef, schemaId, rootNodeId, allSchemas, nodes, edges, specPath, component, expanded, localMappings) {
110
+ const existing = localMappings.get(mappingName);
111
+ if (existing)
112
+ return existing;
113
+ const mappingId = `${schemaId}.mappings.${mappingName}`;
114
+ localMappings.set(mappingName, mappingId);
115
+ const mappingNode = makeNode('Mapping', component, specPath, mappingId, undefined, {});
116
+ const props = {};
117
+ if (!addlDef || typeof addlDef === 'boolean') {
118
+ props.type = 'string';
119
+ }
120
+ else {
121
+ const addl = addlDef;
122
+ if (typeof addl.$ref === 'string') {
123
+ const refName = schemaRefName(addl.$ref);
124
+ if (refName) {
125
+ props.$ref = expandSchema(refName, rootNodeId, allSchemas, nodes, edges, specPath, component, expanded);
126
+ }
127
+ else {
128
+ props.type = 'string';
129
+ }
130
+ }
131
+ else if (addl.type === 'array') {
132
+ props['value-collection'] = 'array';
133
+ const items = addl.items;
134
+ if (items && typeof items.$ref === 'string') {
135
+ const refName = schemaRefName(items.$ref);
136
+ if (refName) {
137
+ props.$ref = expandSchema(refName, rootNodeId, allSchemas, nodes, edges, specPath, component, expanded);
138
+ }
139
+ else {
140
+ props.type = 'string';
141
+ }
142
+ }
143
+ else {
144
+ props.type = mapScalar(items?.type ?? 'string', items?.format);
145
+ }
146
+ }
147
+ else if (addl.type === 'object' && addl.additionalProperties !== undefined) {
148
+ const innerName = `${mappingName}-values`;
149
+ props.$ref = createMapping(innerName, addl.additionalProperties, schemaId, rootNodeId, allSchemas, nodes, edges, specPath, component, expanded, localMappings);
150
+ }
151
+ else {
152
+ props.type = mapScalar(addl.type ?? 'string', addl.format);
153
+ }
154
+ }
155
+ mappingNode.properties = props;
156
+ nodes.push(mappingNode);
157
+ return mappingId;
158
+ }
159
+ function mapScalar(type, format) {
160
+ if (type === 'string') {
161
+ if (format === 'uuid')
162
+ return 'uuid';
163
+ if (format === 'date-time')
164
+ return 'datetime';
165
+ if (format === 'date')
166
+ return 'date';
167
+ return 'string';
168
+ }
169
+ if (type === 'integer')
170
+ return 'integer';
171
+ if (type === 'number')
172
+ return 'decimal';
173
+ if (type === 'boolean')
174
+ return 'boolean';
175
+ return 'string';
176
+ }
177
+ function makeNode(template, component, specPath, id, provenance, properties) {
36
178
  return {
37
- id: raw.id,
38
- template: raw.template,
39
- component: raw.id.split('.')[0],
179
+ id,
180
+ template,
181
+ component,
40
182
  state: 'implemented',
41
183
  stability: 'unstable',
42
184
  schemaVersion: '1',
43
185
  lastModifiedAt: new Date().toISOString().split('T')[0],
44
186
  extractedFrom: specPath,
45
- derivation: derivationOf(raw.provenance),
46
- derivedBy: derivedByOf(raw.provenance),
47
- properties: raw.properties ?? {},
187
+ derivation: provenance?.derivation === 'inferred' ? 'inferred' : 'determined',
188
+ derivedBy: provenance?.derivedBy ?? 'adapter:corum',
189
+ properties,
190
+ };
191
+ }
192
+ function makeHasFieldEdge(from, to) {
193
+ return {
194
+ id: `${from}__has-field__${to}`,
195
+ from,
196
+ to,
197
+ type: 'has-field',
198
+ state: 'implemented',
199
+ stability: 'unstable',
200
+ derivation: 'determined',
201
+ derivedBy: 'adapter:corum',
48
202
  };
49
203
  }
50
204
  function mapEdge(raw) {
@@ -55,13 +209,7 @@ function mapEdge(raw) {
55
209
  type: raw.type,
56
210
  state: 'implemented',
57
211
  stability: 'unstable',
58
- derivation: derivationOf(raw.provenance),
59
- derivedBy: derivedByOf(raw.provenance),
212
+ derivation: raw.provenance?.derivation === 'inferred' ? 'inferred' : 'determined',
213
+ derivedBy: raw.provenance?.derivedBy ?? 'adapter:corum',
60
214
  };
61
215
  }
62
- function derivationOf(p) {
63
- return p?.derivation === 'inferred' ? 'inferred' : 'determined';
64
- }
65
- function derivedByOf(p) {
66
- return p?.by ? `adapter:corum/${p.by}` : 'adapter:corum';
67
- }
@@ -19,16 +19,16 @@ export function parseSpec(specPath) {
19
19
  diagnostics.push({
20
20
  severity: 'error',
21
21
  file: specPath,
22
- message: 'Invalid corum interchange file: missing required "corumInterchange" key or "nodes" array',
22
+ message: 'Invalid corum interchange file: missing required "corum" key or "nodes" object',
23
23
  });
24
24
  return { document: null, diagnostics };
25
25
  }
26
26
  const doc = raw;
27
- if (doc.corumInterchange !== SUPPORTED_VERSION) {
27
+ if (doc.corum !== SUPPORTED_VERSION) {
28
28
  diagnostics.push({
29
29
  severity: 'warning',
30
30
  file: specPath,
31
- message: `Unknown corumInterchange version "${doc.corumInterchange}" — expected "${SUPPORTED_VERSION}", continuing`,
31
+ message: `Unknown corum version "${doc.corum}" — expected "${SUPPORTED_VERSION}", continuing`,
32
32
  });
33
33
  }
34
34
  return { document: doc, diagnostics };
@@ -37,5 +37,8 @@ function isCorumInterchangeDocument(value) {
37
37
  if (typeof value !== 'object' || value === null)
38
38
  return false;
39
39
  const v = value;
40
- return typeof v.corumInterchange === 'string' && Array.isArray(v.nodes);
40
+ return (typeof v.corum === 'string' &&
41
+ typeof v.nodes === 'object' &&
42
+ v.nodes !== null &&
43
+ !Array.isArray(v.nodes));
41
44
  }
@@ -47,6 +47,7 @@ program
47
47
  let graph;
48
48
  try {
49
49
  graph = await loadGraph({ source: config.source, strict: true });
50
+ reportGraphDiagnostics(graph.diagnostics);
50
51
  }
51
52
  catch (err) {
52
53
  process.stderr.write(`[ERROR] ${err instanceof Error ? err.message : String(err)}\n`);
@@ -287,6 +288,15 @@ function reportDiagnostics(diagnostics) {
287
288
  const warnings = diagnostics.filter(d => d.severity === 'warning').length;
288
289
  process.stdout.write(`Import complete. ${errors} error(s), ${warnings} warning(s).\n`);
289
290
  }
291
+ function reportGraphDiagnostics(diagnostics) {
292
+ const warnings = diagnostics.filter(d => d.severity === 'warning');
293
+ for (const d of warnings) {
294
+ process.stderr.write(`[WARN] ${d.file ?? 'graph'}: ${d.message}\n`);
295
+ }
296
+ if (warnings.length > 0) {
297
+ process.stderr.write(`Graph loaded with ${warnings.length} warning(s).\n`);
298
+ }
299
+ }
290
300
  async function readPackRegistryUrl(cwd) {
291
301
  const configPath = path.join(cwd, '.corum', 'config.yaml');
292
302
  const text = await fsReadFile(configPath, 'utf8');
@@ -27,11 +27,11 @@ export function loadEdges(content, nodes, diagnostics) {
27
27
  }
28
28
  let unresolvedEndpoint = false;
29
29
  if (!nodes.has(edgeRecord.from)) {
30
- diagnostics.push({ severity: 'error', file: key, message: `edge from unresolved node: ${edgeRecord.from}` });
30
+ diagnostics.push({ severity: 'warning', file: key, message: `edge from unresolved node: ${edgeRecord.from}` });
31
31
  unresolvedEndpoint = true;
32
32
  }
33
33
  if (!nodes.has(edgeRecord.to)) {
34
- diagnostics.push({ severity: 'error', file: key, message: `edge to unresolved node: ${edgeRecord.to}` });
34
+ diagnostics.push({ severity: 'warning', file: key, message: `edge to unresolved node: ${edgeRecord.to}` });
35
35
  unresolvedEndpoint = true;
36
36
  }
37
37
  if (unresolvedEndpoint)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atolis-hq/corum",
3
- "version": "0.1.22",
3
+ "version": "0.1.23",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",