ng-form-foundry-transformers 0.2.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/LICENSE +201 -0
- package/README.md +186 -0
- package/dist/core/index.d.ts +14 -0
- package/dist/core/index.js +19 -0
- package/dist/core/infer.d.ts +15 -0
- package/dist/core/infer.js +67 -0
- package/dist/core/json-schema.d.ts +26 -0
- package/dist/core/json-schema.js +87 -0
- package/dist/core/registry.d.ts +18 -0
- package/dist/core/registry.js +40 -0
- package/dist/core/schema.d.ts +69 -0
- package/dist/core/schema.js +15 -0
- package/dist/core/transformer.d.ts +48 -0
- package/dist/core/transformer.js +2 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.js +40 -0
- package/dist/transformers/json/index.d.ts +8 -0
- package/dist/transformers/json/index.js +11 -0
- package/dist/transformers/json/json-transformer.d.ts +38 -0
- package/dist/transformers/json/json-transformer.js +41 -0
- package/dist/transformers/yaml/index.d.ts +11 -0
- package/dist/transformers/yaml/index.js +15 -0
- package/dist/transformers/yaml/revert.d.ts +13 -0
- package/dist/transformers/yaml/revert.js +74 -0
- package/dist/transformers/yaml/yaml-transformer.d.ts +32 -0
- package/dist/transformers/yaml/yaml-transformer.js +37 -0
- package/dist/transformers/yang/adapter.d.ts +40 -0
- package/dist/transformers/yang/adapter.js +68 -0
- package/dist/transformers/yang/cache.d.ts +18 -0
- package/dist/transformers/yang/cache.js +19 -0
- package/dist/transformers/yang/engine.d.ts +41 -0
- package/dist/transformers/yang/engine.js +2 -0
- package/dist/transformers/yang/engines/fake-engine.d.ts +15 -0
- package/dist/transformers/yang/engines/fake-engine.js +26 -0
- package/dist/transformers/yang/engines/subprocess-engine.d.ts +30 -0
- package/dist/transformers/yang/engines/subprocess-engine.js +64 -0
- package/dist/transformers/yang/index.d.ts +20 -0
- package/dist/transformers/yang/index.js +27 -0
- package/dist/transformers/yang/mapper.d.ts +14 -0
- package/dist/transformers/yang/mapper.js +96 -0
- package/dist/transformers/yang/model.d.ts +111 -0
- package/dist/transformers/yang/model.js +13 -0
- package/dist/transformers/yang/revert.d.ts +6 -0
- package/dist/transformers/yang/revert.js +173 -0
- package/dist/transformers/yang/rfc7951.d.ts +38 -0
- package/dist/transformers/yang/rfc7951.js +80 -0
- package/dist/transformers/yang/yang-transformer.d.ts +16 -0
- package/dist/transformers/yang/yang-transformer.js +29 -0
- package/package.json +55 -0
- package/python/emit_effective_tree.py +227 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or Derivative
|
|
95
|
+
Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Mathias Santos de Brito
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
# ng-form-foundry-transformers
|
|
2
|
+
|
|
3
|
+
A catalog of **source-format transformers** for
|
|
4
|
+
[ng-form-foundry](https://ng-form-foundry.readthedocs.io): turn a model or config
|
|
5
|
+
into a schema your Angular app can render, then revert the edited form value back
|
|
6
|
+
to the source format.
|
|
7
|
+
|
|
8
|
+
Framework-agnostic **Node + TypeScript** — no framework imports. NestJS is the
|
|
9
|
+
likely host, but the library assumes only Node; wire it into Express, a worker,
|
|
10
|
+
or a CLI just as easily.
|
|
11
|
+
|
|
12
|
+
## Transformers
|
|
13
|
+
|
|
14
|
+
| id | Source | Reverts to | Status |
|
|
15
|
+
| --- | --- | --- | --- |
|
|
16
|
+
| `yang` | YANG model (via an engine) | RFC 7951 instance data | available |
|
|
17
|
+
| `yaml` | YAML config (optionally a JSON Schema) | YAML (comments preserved) | available |
|
|
18
|
+
| `json` | JSON config (optionally a JSON Schema) | JSON (indent preserved) | available |
|
|
19
|
+
|
|
20
|
+
The YAML and JSON transformers share the same format-agnostic form builders in
|
|
21
|
+
`core` (`inferNodeGroup`, `jsonSchemaToNodeGroup`) — a JSON Schema is an *option*
|
|
22
|
+
to either, not a transformer of its own.
|
|
23
|
+
|
|
24
|
+
## The contract
|
|
25
|
+
|
|
26
|
+
Every format implements one `Transformer`, so a single catalog can turn any of
|
|
27
|
+
them into a form and write the edited value back. Look one up by `id` through a
|
|
28
|
+
`TransformerRegistry`, or import the one you need directly (tree-shakeable).
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
interface Transformer<TSource, TData = TSource, TBinding = BindingMap, TOptions = void> {
|
|
32
|
+
readonly id: string;
|
|
33
|
+
// source → form schema (+ revert context, and any initial value)
|
|
34
|
+
toSchema(source: TSource, options?: TOptions): MaybePromise<{
|
|
35
|
+
schema: NodeGroup; binding: TBinding; initialValue?: FormValue;
|
|
36
|
+
}>;
|
|
37
|
+
// edited form value → source data, using the binding from toSchema
|
|
38
|
+
toSource(value: FormValue, binding: TBinding): MaybePromise<TData>;
|
|
39
|
+
}
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
For config formats (YAML, JSON) the thing you load and save is the same document
|
|
43
|
+
type, so `TData` defaults to `TSource`. Schema formats differ: YANG's `toSchema`
|
|
44
|
+
consumes a model source but its `toSource` reverts to RFC 7951 *data*.
|
|
45
|
+
|
|
46
|
+
## YAML transformer
|
|
47
|
+
|
|
48
|
+
Edit a **YAML config file**: turn it into a form, then write the edited value
|
|
49
|
+
back to YAML with comments, key order, and formatting preserved. Pass a JSON
|
|
50
|
+
Schema to drive types/required/enums, or omit it to infer the form from the data.
|
|
51
|
+
|
|
52
|
+
```ts
|
|
53
|
+
import { yamlTransformer } from 'ng-form-foundry-transformers';
|
|
54
|
+
|
|
55
|
+
const yaml = `# upstream API
|
|
56
|
+
host: localhost # dev only
|
|
57
|
+
port: 8080
|
|
58
|
+
`;
|
|
59
|
+
|
|
60
|
+
// forward: infer a form schema (+ initial value) from the config
|
|
61
|
+
const { schema, binding, initialValue } = yamlTransformer.toSchema(yaml);
|
|
62
|
+
// schema → NodeGroup the Angular app renders with buildFormFromSchema
|
|
63
|
+
// initialValue → { host: 'localhost', port: 8080 }
|
|
64
|
+
|
|
65
|
+
// reverse: write the edited value back — comments survive
|
|
66
|
+
const out = yamlTransformer.toSource({ ...initialValue, port: 9090 }, binding);
|
|
67
|
+
// # upstream API
|
|
68
|
+
// host: localhost # dev only
|
|
69
|
+
// port: 9090
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
With a JSON Schema: `yamlTransformer.toSchema(yaml, { schema })` — `object` →
|
|
73
|
+
nodeGroup, `array` of objects → nodeGroupList, scalar `enum` → enum leaf,
|
|
74
|
+
`required`/`title`/`default` carried onto the leaves.
|
|
75
|
+
|
|
76
|
+
## JSON transformer
|
|
77
|
+
|
|
78
|
+
Same as YAML, for **JSON config files** — `jsonTransformer.toSchema(json, { schema? })`
|
|
79
|
+
and `toSource(value, binding)`. Revert re-serializes with the source's indent width
|
|
80
|
+
and trailing newline preserved. Standard JSON has no comments; for JSON **with**
|
|
81
|
+
comments (JSONC), use the YAML transformer instead (`JSON.parse` rejects comments).
|
|
82
|
+
|
|
83
|
+
```ts
|
|
84
|
+
import { jsonTransformer } from 'ng-form-foundry-transformers';
|
|
85
|
+
|
|
86
|
+
const { schema, binding, initialValue } = jsonTransformer.toSchema(configText);
|
|
87
|
+
const out = jsonTransformer.toSource({ ...initialValue, replicas: 5 }, binding);
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## YANG transformer
|
|
91
|
+
|
|
92
|
+
Turn a **YANG** model into an ng-form-foundry schema, then revert the edited form
|
|
93
|
+
value back to **RFC 7951** instance data for write-back to a NETCONF/RESTCONF
|
|
94
|
+
datastore. Use the fuller `YangFormAdapter` when you want caching by `modelId`,
|
|
95
|
+
engine-side validation, and RFC 7951 → form-value conversion:
|
|
96
|
+
|
|
97
|
+
```ts
|
|
98
|
+
import { YangFormAdapter, SubprocessEngine } from 'ng-form-foundry-transformers';
|
|
99
|
+
|
|
100
|
+
const adapter = new YangFormAdapter(new SubprocessEngine());
|
|
101
|
+
|
|
102
|
+
// resolve a device model once (cached by modelId)
|
|
103
|
+
await adapter.compile({
|
|
104
|
+
modelId: 'acme-router@2026-01-01',
|
|
105
|
+
entryModule: 'ietf-interfaces',
|
|
106
|
+
source: { kind: 'dir', path: './yang' },
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
// forward: hand the Angular app a NodeGroup it renders with buildFormFromSchema
|
|
110
|
+
const schema = await adapter.getFormSchema('acme-router@2026-01-01');
|
|
111
|
+
|
|
112
|
+
// load the device's current config into a plain form value
|
|
113
|
+
const value = await adapter.toFormValue(deviceGetResponse, 'acme-router@2026-01-01');
|
|
114
|
+
|
|
115
|
+
// reverse: the edited form value → RFC 7951 JSON, ready for a RESTCONF PUT
|
|
116
|
+
const rfc7951 = await adapter.toYangData(editedValue, 'acme-router@2026-01-01');
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
Or use the catalog-conformant `createYangTransformer(engine)` for the plain
|
|
120
|
+
`Transformer` view (no caching/validation).
|
|
121
|
+
|
|
122
|
+
### What it does
|
|
123
|
+
|
|
124
|
+
- **Forward** — maps a resolved YANG tree to an ng-form-foundry `NodeGroup`
|
|
125
|
+
(`container` → nodeGroup, `list` → nodeGroupList, `leaf`/`leaf-list` → leaf/leafList).
|
|
126
|
+
- **Reverse** — reconstructs RFC 7951 JSON from a plain form value, restoring
|
|
127
|
+
module-namespaced member names, list keys, and — critically —
|
|
128
|
+
`int64`/`uint64`/`decimal64` as **strings** so precision survives. `config false`
|
|
129
|
+
state is shown for context but dropped from write-back.
|
|
130
|
+
- **Binding stays server-side** — the frontend only ever sees the `NodeGroup`
|
|
131
|
+
schema and returns a plain value; the resolved YANG model (the "binding") never
|
|
132
|
+
leaves the adapter.
|
|
133
|
+
|
|
134
|
+
### Engines
|
|
135
|
+
|
|
136
|
+
The only part that touches YANG semantics or the environment is the injected
|
|
137
|
+
`YangEngine`:
|
|
138
|
+
|
|
139
|
+
- **`SubprocessEngine`** (recommended) shells out to the bundled Python helper,
|
|
140
|
+
which wraps **pyang** to resolve `uses`/`augment`/`typedef` and emit a
|
|
141
|
+
normalized effective tree. Requires Python + `pip install pyang` on the host.
|
|
142
|
+
- **`FakeEngine`** serves pre-resolved models — used in tests and local dev, no
|
|
143
|
+
Python required.
|
|
144
|
+
|
|
145
|
+
Caching is pluggable (`ArtifactCache`; an `InMemoryCache` ships by default).
|
|
146
|
+
|
|
147
|
+
### Leaf and structural type coverage
|
|
148
|
+
|
|
149
|
+
| YANG type | Form control | Notes |
|
|
150
|
+
| --- | --- | --- |
|
|
151
|
+
| `string`, `boolean`, int/uint 8–32 | text / checkbox / number | direct |
|
|
152
|
+
| `int64`, `uint64`, `decimal64` | text | kept as strings (precision-safe) |
|
|
153
|
+
| `enumeration`, `identityref` | enum (dropdown) | identityref re-qualified across modules |
|
|
154
|
+
| `empty` | checkbox | `[null]` when set, omitted when not |
|
|
155
|
+
| `bits` | group of checkboxes | reverts to the space-separated set |
|
|
156
|
+
| `binary`, `instance-identifier`, `leafref`, `union` | text | member types / leafref path kept in the binding |
|
|
157
|
+
|
|
158
|
+
Plain `container` → nodeGroup; **presence** `container` → nodeGroup flagged
|
|
159
|
+
`presence: true` (on/off toggle; present-but-empty round-trips as `{}`, absent is
|
|
160
|
+
omitted). **`choice`/`case`** → a `Choice` node: the form value carries
|
|
161
|
+
`{ __case, ...fields }` and the adapter flattens it to the inline YANG encoding on
|
|
162
|
+
write-back. `must`/`when` cross-field validity is left to server-side validation.
|
|
163
|
+
|
|
164
|
+
### Using it from NestJS
|
|
165
|
+
|
|
166
|
+
The core is a plain class, so a NestJS provider is a thin wrapper — see
|
|
167
|
+
[`examples/nestjs-provider.ts`](examples/nestjs-provider.ts).
|
|
168
|
+
|
|
169
|
+
## Develop
|
|
170
|
+
|
|
171
|
+
```bash
|
|
172
|
+
npm install
|
|
173
|
+
npm run build # tsc -> dist/
|
|
174
|
+
npm test # node:test on the compiled output (no Python needed)
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
## Status
|
|
178
|
+
|
|
179
|
+
`0.2.1` — first release under this name (formerly `yang-form-foundry`),
|
|
180
|
+
restructured into a transformer catalog. YANG, YAML, and JSON transformers are
|
|
181
|
+
complete; YAML and JSON share the format-agnostic form builders in `core` and are
|
|
182
|
+
JSON-Schema-driven or inferred.
|
|
183
|
+
|
|
184
|
+
## License
|
|
185
|
+
|
|
186
|
+
Apache-2.0 © Mathias Santos de Brito
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Format-agnostic core of the transformer catalog: the {@link Transformer}
|
|
3
|
+
* contract every format implements, the {@link TransformerRegistry} for runtime
|
|
4
|
+
* lookup, the ng-form-foundry schema types that are the shared target of all
|
|
5
|
+
* transformers, and the shared schema builders (infer a form from plain data, or
|
|
6
|
+
* map a JSON Schema) that any data-oriented transformer reuses.
|
|
7
|
+
*/
|
|
8
|
+
export type { Transformer, TransformResult, BindingMap } from './transformer';
|
|
9
|
+
export { TransformerRegistry } from './registry';
|
|
10
|
+
export { inferNodeGroup } from './infer';
|
|
11
|
+
export { jsonSchemaToNodeGroup } from './json-schema';
|
|
12
|
+
export type { JsonSchema } from './json-schema';
|
|
13
|
+
export type { NodeGroup, NodeType, Leaf, LeafList, NodeGroupList, Choice, LeafType, FormValue, } from './schema';
|
|
14
|
+
export { CASE_KEY } from './schema';
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Format-agnostic core of the transformer catalog: the {@link Transformer}
|
|
4
|
+
* contract every format implements, the {@link TransformerRegistry} for runtime
|
|
5
|
+
* lookup, the ng-form-foundry schema types that are the shared target of all
|
|
6
|
+
* transformers, and the shared schema builders (infer a form from plain data, or
|
|
7
|
+
* map a JSON Schema) that any data-oriented transformer reuses.
|
|
8
|
+
*/
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.CASE_KEY = exports.jsonSchemaToNodeGroup = exports.inferNodeGroup = exports.TransformerRegistry = void 0;
|
|
11
|
+
var registry_1 = require("./registry");
|
|
12
|
+
Object.defineProperty(exports, "TransformerRegistry", { enumerable: true, get: function () { return registry_1.TransformerRegistry; } });
|
|
13
|
+
// Shared schema builders — used by the YAML and JSON transformers alike.
|
|
14
|
+
var infer_1 = require("./infer");
|
|
15
|
+
Object.defineProperty(exports, "inferNodeGroup", { enumerable: true, get: function () { return infer_1.inferNodeGroup; } });
|
|
16
|
+
var json_schema_1 = require("./json-schema");
|
|
17
|
+
Object.defineProperty(exports, "jsonSchemaToNodeGroup", { enumerable: true, get: function () { return json_schema_1.jsonSchemaToNodeGroup; } });
|
|
18
|
+
var schema_1 = require("./schema");
|
|
19
|
+
Object.defineProperty(exports, "CASE_KEY", { enumerable: true, get: function () { return schema_1.CASE_KEY; } });
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { NodeGroup } from './schema';
|
|
2
|
+
/**
|
|
3
|
+
* Infer a root {@link NodeGroup} from a parsed data object when no schema is
|
|
4
|
+
* given. Structure and types come from the values themselves:
|
|
5
|
+
*
|
|
6
|
+
* - object → nodeGroup (recursed)
|
|
7
|
+
* - array of objects → nodeGroupList (item schema = union of the items' keys)
|
|
8
|
+
* - array of scalars → leafList (element type from the first element)
|
|
9
|
+
* - scalar → typed leaf (string / number / boolean)
|
|
10
|
+
*
|
|
11
|
+
* `null` and empty/mixed arrays fall back to `string`, since a value alone can't
|
|
12
|
+
* declare an enum, a constraint, or an empty array's element type. Pass a JSON
|
|
13
|
+
* Schema to {@link import('./json-schema').jsonSchemaToNodeGroup} for richer output.
|
|
14
|
+
*/
|
|
15
|
+
export declare function inferNodeGroup(data: Record<string, unknown>, name?: string): NodeGroup;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.inferNodeGroup = inferNodeGroup;
|
|
4
|
+
const ROOT = '__root__';
|
|
5
|
+
/**
|
|
6
|
+
* Infer a root {@link NodeGroup} from a parsed data object when no schema is
|
|
7
|
+
* given. Structure and types come from the values themselves:
|
|
8
|
+
*
|
|
9
|
+
* - object → nodeGroup (recursed)
|
|
10
|
+
* - array of objects → nodeGroupList (item schema = union of the items' keys)
|
|
11
|
+
* - array of scalars → leafList (element type from the first element)
|
|
12
|
+
* - scalar → typed leaf (string / number / boolean)
|
|
13
|
+
*
|
|
14
|
+
* `null` and empty/mixed arrays fall back to `string`, since a value alone can't
|
|
15
|
+
* declare an enum, a constraint, or an empty array's element type. Pass a JSON
|
|
16
|
+
* Schema to {@link import('./json-schema').jsonSchemaToNodeGroup} for richer output.
|
|
17
|
+
*/
|
|
18
|
+
function inferNodeGroup(data, name = ROOT) {
|
|
19
|
+
const group = objectToNodeGroup(data, name);
|
|
20
|
+
group.root = true;
|
|
21
|
+
return group;
|
|
22
|
+
}
|
|
23
|
+
function objectToNodeGroup(data, name) {
|
|
24
|
+
const children = {};
|
|
25
|
+
for (const [key, value] of Object.entries(data)) {
|
|
26
|
+
children[key] = inferNode(key, value);
|
|
27
|
+
}
|
|
28
|
+
return { kind: 'nodeGroup', name, children };
|
|
29
|
+
}
|
|
30
|
+
function inferNode(name, value) {
|
|
31
|
+
if (Array.isArray(value))
|
|
32
|
+
return inferArray(name, value);
|
|
33
|
+
if (isPlainObject(value))
|
|
34
|
+
return objectToNodeGroup(value, name);
|
|
35
|
+
return { kind: 'leaf', name, type: scalarType(value) };
|
|
36
|
+
}
|
|
37
|
+
function inferArray(name, items) {
|
|
38
|
+
const objects = items.filter(isPlainObject);
|
|
39
|
+
if (objects.length && objects.length === items.length) {
|
|
40
|
+
return { kind: 'nodeGroupList', name, type: objectToNodeGroup(unionKeys(objects), name) };
|
|
41
|
+
}
|
|
42
|
+
// array of scalars (or empty / mixed → string fallback)
|
|
43
|
+
const first = items.find((v) => !isPlainObject(v) && !Array.isArray(v));
|
|
44
|
+
return { kind: 'leafList', name, type: scalarType(first) };
|
|
45
|
+
}
|
|
46
|
+
/** A representative object with every key seen across `items`, each mapped to the first non-null sample. */
|
|
47
|
+
function unionKeys(items) {
|
|
48
|
+
const merged = {};
|
|
49
|
+
for (const item of items) {
|
|
50
|
+
for (const [key, value] of Object.entries(item)) {
|
|
51
|
+
if (!(key in merged) || (merged[key] == null && value != null))
|
|
52
|
+
merged[key] = value;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return merged;
|
|
56
|
+
}
|
|
57
|
+
/** Infer a form {@link LeafType} from a scalar value (default `string`). */
|
|
58
|
+
function scalarType(value) {
|
|
59
|
+
if (typeof value === 'boolean')
|
|
60
|
+
return 'boolean';
|
|
61
|
+
if (typeof value === 'number')
|
|
62
|
+
return 'number';
|
|
63
|
+
return 'string';
|
|
64
|
+
}
|
|
65
|
+
function isPlainObject(v) {
|
|
66
|
+
return typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
67
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { NodeGroup } from './schema';
|
|
2
|
+
/**
|
|
3
|
+
* The subset of JSON Schema (draft-07 style) this transformer maps to a form.
|
|
4
|
+
* Only the keywords that shape a form are read; anything else is ignored. No
|
|
5
|
+
* validation is performed — the schema drives *structure and types*, not
|
|
6
|
+
* constraint checking (that stays server-side).
|
|
7
|
+
*/
|
|
8
|
+
export interface JsonSchema {
|
|
9
|
+
type?: JsonSchemaType | JsonSchemaType[];
|
|
10
|
+
properties?: Record<string, JsonSchema>;
|
|
11
|
+
required?: string[];
|
|
12
|
+
items?: JsonSchema;
|
|
13
|
+
enum?: unknown[];
|
|
14
|
+
title?: string;
|
|
15
|
+
default?: unknown;
|
|
16
|
+
}
|
|
17
|
+
type JsonSchemaType = 'object' | 'array' | 'string' | 'number' | 'integer' | 'boolean' | 'null';
|
|
18
|
+
/**
|
|
19
|
+
* Map a JSON Schema whose top level is an object into a root {@link NodeGroup}.
|
|
20
|
+
* `object` → nodeGroup, `array` of objects → nodeGroupList, `array` of scalars →
|
|
21
|
+
* leafList, a scalar with `enum` → enum leaf, other scalars → typed leaf.
|
|
22
|
+
* `required` marks child leaves, `title` becomes the label, `default` carries a
|
|
23
|
+
* scalar default.
|
|
24
|
+
*/
|
|
25
|
+
export declare function jsonSchemaToNodeGroup(schema: JsonSchema, name?: string): NodeGroup;
|
|
26
|
+
export {};
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.jsonSchemaToNodeGroup = jsonSchemaToNodeGroup;
|
|
4
|
+
const ROOT = '__root__';
|
|
5
|
+
/**
|
|
6
|
+
* Map a JSON Schema whose top level is an object into a root {@link NodeGroup}.
|
|
7
|
+
* `object` → nodeGroup, `array` of objects → nodeGroupList, `array` of scalars →
|
|
8
|
+
* leafList, a scalar with `enum` → enum leaf, other scalars → typed leaf.
|
|
9
|
+
* `required` marks child leaves, `title` becomes the label, `default` carries a
|
|
10
|
+
* scalar default.
|
|
11
|
+
*/
|
|
12
|
+
function jsonSchemaToNodeGroup(schema, name = ROOT) {
|
|
13
|
+
const group = objectToNodeGroup(schema, name, schema.title);
|
|
14
|
+
group.root = true;
|
|
15
|
+
return group;
|
|
16
|
+
}
|
|
17
|
+
function objectToNodeGroup(schema, name, label) {
|
|
18
|
+
const required = new Set(schema.required ?? []);
|
|
19
|
+
const children = {};
|
|
20
|
+
for (const [key, propSchema] of Object.entries(schema.properties ?? {})) {
|
|
21
|
+
children[key] = schemaToNode(key, propSchema, required.has(key));
|
|
22
|
+
}
|
|
23
|
+
const group = { kind: 'nodeGroup', name, children };
|
|
24
|
+
if (label)
|
|
25
|
+
group.label = label;
|
|
26
|
+
return group;
|
|
27
|
+
}
|
|
28
|
+
function schemaToNode(name, schema, required) {
|
|
29
|
+
const type = primaryType(schema);
|
|
30
|
+
if (type === 'object') {
|
|
31
|
+
return objectToNodeGroup(schema, name, schema.title);
|
|
32
|
+
}
|
|
33
|
+
if (type === 'array') {
|
|
34
|
+
const items = schema.items ?? {};
|
|
35
|
+
if (primaryType(items) === 'object') {
|
|
36
|
+
const node = {
|
|
37
|
+
kind: 'nodeGroupList',
|
|
38
|
+
name,
|
|
39
|
+
type: objectToNodeGroup(items, name, items.title),
|
|
40
|
+
};
|
|
41
|
+
if (schema.title)
|
|
42
|
+
node.label = schema.title;
|
|
43
|
+
return node;
|
|
44
|
+
}
|
|
45
|
+
const list = { kind: 'leafList', name, type: scalarType(items) };
|
|
46
|
+
if (schema.title)
|
|
47
|
+
list.label = schema.title;
|
|
48
|
+
return list;
|
|
49
|
+
}
|
|
50
|
+
// scalar leaf (possibly an enum)
|
|
51
|
+
const leaf = { kind: 'leaf', name, type: scalarType(schema) };
|
|
52
|
+
if (schema.enum && schema.enum.length) {
|
|
53
|
+
leaf.type = 'enum';
|
|
54
|
+
leaf.enum = schema.enum.filter((v) => typeof v === 'string' || typeof v === 'number');
|
|
55
|
+
}
|
|
56
|
+
if (required)
|
|
57
|
+
leaf.required = true;
|
|
58
|
+
if (isScalar(schema.default))
|
|
59
|
+
leaf.default = schema.default;
|
|
60
|
+
if (schema.title)
|
|
61
|
+
leaf.label = schema.title;
|
|
62
|
+
return leaf;
|
|
63
|
+
}
|
|
64
|
+
/** The first declared type, ignoring a `null` companion in a `[T, 'null']` union. */
|
|
65
|
+
function primaryType(schema) {
|
|
66
|
+
const t = schema.type;
|
|
67
|
+
if (Array.isArray(t))
|
|
68
|
+
return t.find((x) => x !== 'null');
|
|
69
|
+
return t;
|
|
70
|
+
}
|
|
71
|
+
/** Map a JSON Schema scalar type to a form {@link LeafType} (default `string`). */
|
|
72
|
+
function scalarType(schema) {
|
|
73
|
+
if (schema.enum && schema.enum.length)
|
|
74
|
+
return 'enum';
|
|
75
|
+
switch (primaryType(schema)) {
|
|
76
|
+
case 'boolean':
|
|
77
|
+
return 'boolean';
|
|
78
|
+
case 'number':
|
|
79
|
+
case 'integer':
|
|
80
|
+
return 'number';
|
|
81
|
+
default:
|
|
82
|
+
return 'string';
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
function isScalar(v) {
|
|
86
|
+
return typeof v === 'string' || typeof v === 'number' || typeof v === 'boolean';
|
|
87
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Transformer } from './transformer';
|
|
2
|
+
/**
|
|
3
|
+
* A lookup of {@link Transformer}s by their `id`. Lets a consumer pick a format
|
|
4
|
+
* at runtime (`registry.get('yaml')`) instead of importing each transformer
|
|
5
|
+
* directly. Direct imports remain available and tree-shakeable for consumers
|
|
6
|
+
* that only need one format.
|
|
7
|
+
*/
|
|
8
|
+
export declare class TransformerRegistry {
|
|
9
|
+
private readonly byId;
|
|
10
|
+
/** Register a transformer under its `id`. Throws if the `id` is already taken. */
|
|
11
|
+
register(transformer: Transformer<any, any, any, any>): this;
|
|
12
|
+
/** Get a transformer by `id`, or `undefined` if none is registered. */
|
|
13
|
+
get(id: string): Transformer<any, any, any, any> | undefined;
|
|
14
|
+
/** Get a transformer by `id`, throwing a listing of known ids if absent. */
|
|
15
|
+
require(id: string): Transformer<any, any, any, any>;
|
|
16
|
+
/** The ids of all registered transformers. */
|
|
17
|
+
ids(): string[];
|
|
18
|
+
}
|