Skip to content

Commit c63a73b

Browse files
committed
ContextBuilder: sort resolved_property_defs entries by path
_get_descendants uses a stack (LIFO), so siblings were returned in reverse insertion order (h, g, f, e...). Sort each def's entry list by path — with numeric segments compared as integers — so branches always appear in ascending index order (a, b, c...).
1 parent 025b8eb commit c63a73b

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

ogc/na/annotate_schema.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1439,6 +1439,9 @@ def _remove_descendants(path: list[str]) -> None:
14391439
entry = copy.deepcopy(v)
14401440
entry.path = v.path[src_len:]
14411441
def_entries.append(entry)
1442+
def_entries.sort(key=lambda e: [
1443+
int(seg) if seg.isdigit() else seg for seg in e.path
1444+
])
14421445
self._resolved_property_defs[cache_key] = def_entries
14431446
_remove_descendants(src_path)
14441447
src_key = sep.join(src_path)
@@ -1572,6 +1575,11 @@ def _main():
15721575
help='Dump visited properties and their ids to a file',
15731576
)
15741577

1578+
parser.add_argument(
1579+
'--dump-resolved',
1580+
help='Dump resolved properties and defs as JSON to a file (- for stdout)',
1581+
)
1582+
15751583
parser.add_argument(
15761584
'--ignore-existing',
15771585
help="Ignore existing x-jsonld- properties when annotating",
@@ -1604,6 +1612,34 @@ def write_visited(stream):
16041612
else:
16051613
with open(args.dump_visited, 'w', newline='') as f:
16061614
write_visited(f)
1615+
1616+
if args.dump_resolved:
1617+
def _serialize_rp(rp: ResolvedProperty) -> dict:
1618+
d = dataclasses.asdict(rp)
1619+
d['sources'] = [str(s) for s in rp.sources]
1620+
d['effectiveId'] = rp.effective_id
1621+
return {k: v for k, v in d.items()
1622+
if v is not None and (k == 'const' or (v != [] and v is not False))}
1623+
1624+
key_map = {k: str(i) for i, k in enumerate(ctx_builder.resolved_property_defs)}
1625+
1626+
def _with_short_ref(d: dict) -> dict:
1627+
if 'ref' in d and d['ref'] in key_map:
1628+
d['ref'] = key_map[d['ref']]
1629+
return d
1630+
1631+
properties_list = [_with_short_ref(_serialize_rp(rp))
1632+
for rp in ctx_builder.resolved_properties.values()]
1633+
defs = {
1634+
key_map[k]: [_with_short_ref(_serialize_rp(e)) for e in entries]
1635+
for k, entries in ctx_builder.resolved_property_defs.items()
1636+
}
1637+
dump = {'defs': defs, 'properties': properties_list}
1638+
if args.dump_resolved == '-':
1639+
json.dump(dump, sys.stdout, indent=2)
1640+
else:
1641+
with open(args.dump_resolved, 'w') as f:
1642+
json.dump(dump, f, indent=2)
16071643
else:
16081644
annotator = SchemaAnnotator(ignore_existing=args.ignore_existing)
16091645
annotated = annotator.process_schema(args.schema, args.context)

0 commit comments

Comments
 (0)