From 03b0023e4a0c9cb3f31eac0d6e84abccf987cf3a Mon Sep 17 00:00:00 2001 From: Titus Date: Mon, 23 Nov 2026 22:02:26 +0100 Subject: [PATCH] 0.146.0: read_folder takes a declared type only when it is one of the 22 slugs as written The folder format says a declared type is a string exactly equal to one of the 22 type slugs, and that anything else in `type` declares nothing, so the directory fills it. The reader took any non-empty string, in lower case: "Location" in elements/character/ was served as a location, and an unknown word became the element's type. Each is now read as its directory's type and flagged type_inferred, its body left as written. A slug as written still beats the directory. Held by 21 cases in one JSON file, the same bytes the npm SDK's folder source runs, its sha256 pinned in both suites. Co-Authored-By: Claude Fable 5.2 --- CHANGELOG.md | 4 ++++ src/onlyworlds/_version.py | 2 +- src/onlyworlds/folder.py | 18 +++++++++----- tests/fixtures/declared-type.cases.json | 26 ++++++++++++++++++++ tests/test_reader.py | 32 +++++++++++++++++++++++++ 5 files changed, 75 insertions(+), 7 deletions(-) create mode 100644 tests/fixtures/declared-type.cases.json diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e2515a..0d4b6a9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ The package version runs on its own semver; the schema it is generated from is ` ## Unreleased +## 0.146.0 (2026-11-23) + +- **`read_folder` takes a declared type only when it is one of the 22 slugs as written.** The folder format says a declared type is a string exactly equal to one of the 22 type slugs, and that anything else in `type` declares nothing, so the directory fills it. The reader took any non-empty string, in lower case: `"type": "Location"` in `elements/character/` was served as a location, and `"type": "widget"` as an element of type `widget`. Each is now a character, flagged `type_inferred`, its body left as the file wrote it. A slug as written still beats the directory: `"type": "location"` in `elements/character/` is a location. What follows for callers: `push` sends such an element to its directory's route, and a file whose `type` was an unknown word, which `pack` left out and the OKF export skipped, is now read as an element of its directory's type. The npm SDK's folder source already read it this way; both packages hold the same 21 cases (`tests/fixtures/declared-type.cases.json`). + ## 0.145.0 (2026-11-23) - **A share carries under `elements/` and `spatial/` only what the folder reader serves.** `write_world_file(include_hidden=False)` and `world_file_bytes` now ask `read_folder` which files are elements, and carry those that hold no hidden mark. Every other file there is left out and listed in `unread_left_out=`: a file with another extension (`notes.txt`, a browser's swap file), one deeper than a type folder (a hidden element's file moved into `old/` by hand), one directly under `elements/`, one in a folder that is no type and an object with no non-empty string `id`. Until now a share carried a file with another extension, or one not directly in a type folder, whatever it held, a hidden element's text included, and judged the other kinds by their own mark alone. A file that holds a mark and is not served as an element is listed with the unread, not the hidden. diff --git a/src/onlyworlds/_version.py b/src/onlyworlds/_version.py index 37d52ba..31a9856 100644 --- a/src/onlyworlds/_version.py +++ b/src/onlyworlds/_version.py @@ -1 +1 @@ -__version__ = "0.145.0" +__version__ = "0.146.0" diff --git a/src/onlyworlds/folder.py b/src/onlyworlds/folder.py index 63e56d3..df78e83 100644 --- a/src/onlyworlds/folder.py +++ b/src/onlyworlds/folder.py @@ -356,9 +356,10 @@ def write_folder( @dataclass(frozen=True) class FolderElement: id: str - #: the declared ``type`` from the body when it has one (lowercased), else the directory's + #: the declared ``type`` from the body when it is one of the 22 slugs as written, else the directory's type: str - #: True when ``type`` came from the directory (fixture README: infer it, but FLAG the inference) + #: True when ``type`` came from the directory: the body declared none, or held something that + #: declares nothing (fixture README: infer it, but FLAG the inference) type_inferred: bool #: the parsed body exactly as on disk; the reader adds nothing to it (no ``type`` is injected) body: dict[str, Any] @@ -461,8 +462,11 @@ def read_folder(root: str | os.PathLike[str], *, legacy: bool = True) -> Folder: 0.145.0 read an extension and a directory name in any case. - A file that is not JSON, not an object, or has no non-empty string ``id`` is skipped, recorded in ``skipped``, and left where it is (§4.1). - - A declared ``type`` beats the directory the file sits in; an absent one is - inferred from the directory and flagged (``type_inferred``). + - A declared ``type`` beats the directory the file sits in. A declared type is a string exactly + equal to one of the 22 type slugs (§4.1): anything else in ``type`` (an unknown word, another + case such as ``"Location"``, a number, null) declares nothing, so the type is inferred from + the directory and flagged (``type_inferred``), as for an absent one. Versions before 0.146.0 + took any non-empty string, in lower case. - ``spatial/{map,pin,zone,marker}/`` (Atlas through v1.0) is read into ``legacy`` unless ``legacy=False``. A missing type directory is "none of that type". - A repeated id keeps the first file in path order (each name by code point, on every platform); @@ -524,11 +528,13 @@ def read_folder(root: str | os.PathLike[str], *, legacy: bool = True) -> Folder: if not isinstance(eid, str) or not eid: folder.skipped.append(SkippedFile(path, "no id: not an element")) continue + # A declared type is a string exactly equal to one of the 22 slugs (§4.1): anything + # else in `type` declares nothing, and the directory fills it. declared = body.get("type") - has_declared = isinstance(declared, str) and bool(declared) + has_declared = isinstance(declared, str) and declared in ELEMENT_TYPES element = FolderElement( id=eid, - type=str(declared).lower() if has_declared else dir_type, + type=str(declared) if has_declared else dir_type, type_inferred=not has_declared, body=body, path=path, diff --git a/tests/fixtures/declared-type.cases.json b/tests/fixtures/declared-type.cases.json new file mode 100644 index 0000000..3fc95fc --- /dev/null +++ b/tests/fixtures/declared-type.cases.json @@ -0,0 +1,26 @@ +{ + "about": "What a folder reader serves as an element's type when the file declares one. The folder spec's 4.1 (since 2026-11-22): a declared type is a string exactly equal to one of the 22 type slugs as the standard spells them; anything else in `type` declares nothing, and the directory fills it. Each case is one file; `text` is the file's JSON as written, `want` the type a conforming reader serves.", + "cases": [ + {"name": "no type key", "path": "elements/character/x.json", "text": "{\"id\":\"x1\",\"name\":\"X\"}", "want": "character"}, + {"name": "the directory's own slug", "path": "elements/character/x.json", "text": "{\"id\":\"x1\",\"name\":\"X\",\"type\":\"character\"}", "want": "character"}, + {"name": "another type's slug", "path": "elements/character/x.json", "text": "{\"id\":\"x1\",\"name\":\"X\",\"type\":\"location\"}", "want": "location"}, + {"name": "another type, capital first", "path": "elements/character/x.json", "text": "{\"id\":\"x1\",\"name\":\"X\",\"type\":\"Location\"}", "want": "character"}, + {"name": "another type, all capitals", "path": "elements/character/x.json", "text": "{\"id\":\"x1\",\"name\":\"X\",\"type\":\"LOCATION\"}", "want": "character"}, + {"name": "another type, a space before", "path": "elements/character/x.json", "text": "{\"id\":\"x1\",\"name\":\"X\",\"type\":\" location\"}", "want": "character"}, + {"name": "another type, a space after", "path": "elements/character/x.json", "text": "{\"id\":\"x1\",\"name\":\"X\",\"type\":\"location \"}", "want": "character"}, + {"name": "a slug with a long s", "path": "elements/character/x.json", "text": "{\"id\":\"x1\",\"name\":\"X\",\"type\":\"\\u017fpecies\"}", "want": "character"}, + {"name": "an unknown word", "path": "elements/character/x.json", "text": "{\"id\":\"x1\",\"name\":\"X\",\"type\":\"widget\"}", "want": "character"}, + {"name": "a plural", "path": "elements/character/x.json", "text": "{\"id\":\"x1\",\"name\":\"X\",\"type\":\"locations\"}", "want": "character"}, + {"name": "an empty string", "path": "elements/character/x.json", "text": "{\"id\":\"x1\",\"name\":\"X\",\"type\":\"\"}", "want": "character"}, + {"name": "null", "path": "elements/character/x.json", "text": "{\"id\":\"x1\",\"name\":\"X\",\"type\":null}", "want": "character"}, + {"name": "a number", "path": "elements/character/x.json", "text": "{\"id\":\"x1\",\"name\":\"X\",\"type\":7}", "want": "character"}, + {"name": "true", "path": "elements/character/x.json", "text": "{\"id\":\"x1\",\"name\":\"X\",\"type\":true}", "want": "character"}, + {"name": "a list holding a slug", "path": "elements/character/x.json", "text": "{\"id\":\"x1\",\"name\":\"X\",\"type\":[\"location\"]}", "want": "character"}, + {"name": "an object", "path": "elements/character/x.json", "text": "{\"id\":\"x1\",\"name\":\"X\",\"type\":{\"slug\":\"location\"}}", "want": "character"}, + {"name": "the key twice, the last one no slug", "path": "elements/character/x.json", "text": "{\"id\":\"x1\",\"name\":\"X\",\"type\":\"location\",\"type\":\"Location\"}", "want": "character"}, + {"name": "the key twice, the last one a slug", "path": "elements/character/x.json", "text": "{\"id\":\"x1\",\"name\":\"X\",\"type\":\"Location\",\"type\":\"location\"}", "want": "location"}, + {"name": "the old layout, no type key", "path": "spatial/pin/x.json", "text": "{\"id\":\"x1\",\"name\":\"X\"}", "want": "pin"}, + {"name": "the old layout, another slug", "path": "spatial/pin/x.json", "text": "{\"id\":\"x1\",\"name\":\"X\",\"type\":\"map\"}", "want": "map"}, + {"name": "the old layout, a capital", "path": "spatial/pin/x.json", "text": "{\"id\":\"x1\",\"name\":\"X\",\"type\":\"Map\"}", "want": "pin"} + ] +} diff --git a/tests/test_reader.py b/tests/test_reader.py index 62a99ef..0a77e6d 100644 --- a/tests/test_reader.py +++ b/tests/test_reader.py @@ -180,3 +180,35 @@ def test_a_repeated_id_keeps_the_first_file_by_code_point_on_every_platform(tmp_ f = read_folder(tmp_path) assert f.elements["1"].body["name"] == "upper" # "B" (U+0042) before "a" (U+0061) assert [d.ignored.name for d in f.duplicates] == ["a.json"] + + +DECLARED_TYPE = Path(__file__).parent / "fixtures" / "declared-type.cases.json" +DECLARED_TYPE_SHA256 = "89968d85deaa4d5974a20a4d73aab80c7e8ec374ee4236b0809e00c405831b96" + + +def test_a_declared_type_is_one_of_the_22_slugs_as_written_or_the_directory_fills_it(tmp_path: Path) -> None: + """Folder spec 4.1: a declared type is a string exactly equal to one of the 22 type slugs; anything + else in ``type`` declares nothing, and the directory fills it. The cases are the same bytes as the + npm SDK's ``test/fixtures/declared-type.cases.json``; both suites hold the file's sha256.""" + assert hashlib.sha256(DECLARED_TYPE.read_bytes()).hexdigest() == DECLARED_TYPE_SHA256 + cases = json.loads(DECLARED_TYPE.read_text(encoding="utf-8"))["cases"] + assert len(cases) == 21 + served: dict[str, Any] = {} + off = [] + for number, case in enumerate(cases): + root = tmp_path / f"case-{number:02d}" + _w(root / "world.json", '{"id": "w", "name": "W"}') + _w(root / case["path"], case["text"]) + element = read_folder(root).all_elements().get("x1") + served[case["name"]] = element + got = None if element is None else element.type + if got != case["want"]: + off.append(f"{case['name']}: served {got!r}, want {case['want']!r}") + assert off == [] + # The flag says where the type came from, and the body stays as the file wrote it. + assert served["another type's slug"].type_inferred is False + assert served["the directory's own slug"].type_inferred is False + assert served["no type key"].type_inferred is True + assert served["another type, capital first"].type_inferred is True + assert served["another type, capital first"].body["type"] == "Location" + assert served["the old layout, another slug"].layout == "spatial"