diff options
| author | Bobby <[email protected]> | 2026-07-14 19:39:01 +0530 |
|---|---|---|
| committer | GitHub <[email protected]> | 2026-07-14 19:39:01 +0530 |
| commit | 64655e3d779a9a02ce730cad14763e613a2623e2 (patch) | |
| tree | b4ce2ac620c297057aa74d59a27110359c59b19c /tests/builder/reverse.test.py | |
| parent | 082325df2374340b4ccf6bdfd1b4842d2b9aa763 (diff) | |
| parent | 8eda172b4d7ad3388bafa912358f6a2c71e82c1c (diff) | |
| download | edify-64655e3d779a9a02ce730cad14763e613a2623e2.tar.xz edify-64655e3d779a9a02ce730cad14763e613a2623e2.zip | |
feat(pattern): canonical dict/JSON serialization for Pattern (#277)
Adds canonical serialization to `Pattern` — round-trip-safe dict and
JSON, with a schema-version header and a public `kind`-based AST
vocabulary that shields the wire format from internal class-name churn.
## Usage
```python
from edify import Pattern
p = Pattern().start_of_input().named_capture("year").exactly(4).digit().end().ignore_case()
blob = p.to_json()
# {"edify":0,"flags":{"ignore_case":true},"pattern":{"children":[...],"kind":"root"}}
restored = Pattern.from_json(blob)
assert restored == p
```
## Schema shape
```json
{
"edify": 0,
"pattern": {
"kind": "root",
"children": [
{ "kind": "start" },
{
"kind": "named-capture",
"name": "year",
"children": [
{ "kind": "exactly", "times": 4, "child": { "kind": "digit" } }
]
}
]
},
"flags": { "ignore_case": true }
}
```
Every element carries a public `kind` string (registered in
`edify.serialize.kinds`). Internal class names (`ExactlyElement`, etc.)
never appear in the payload, so later privatisation and refactors don't
leak into serialized output. The `flags` key is omitted when no flags
are set.
## Schema version
`SCHEMA_VERSION = 0` — **experimental**. The concrete AST shape may
change without a deprecation cycle until it is promoted to `1` in a
later release. Consumers should not commit to the on-wire shape yet.
`from_dict` / `from_json` reject any payload whose `edify` key does not
match this build's `SCHEMA_VERSION`, and reject unknown `kind` values
with `UnknownElementKindError` so payloads from a newer build fail
loudly on an older loader.
## Forward-compatibility
Unknown fields on individual element dicts are silently ignored, so a
newer edify that adds `{"kind": "digit", "future_marker": true}` will
still load on this build — the extra field just drops.
Closes #193
Closes #194
Closes #195
Closes #196
Closes #197
Diffstat (limited to 'tests/builder/reverse.test.py')
0 files changed, 0 insertions, 0 deletions
