schemagen: fix nullable root types (#13) #29

Merged
andrew merged 1 commits from weaselbot/weaseljson:weaselbot/issue-13 into main 2026-06-23 17:43:20 +00:00
Member

Closes #13

The C++ code generator now handles schemas where the top-level type is nullable (["object", "null"], ["string", "null"], ["array", "null"]).

  • Renames the inner object struct for nullable root objects so the std::optional alias no longer conflicts with struct Root.
  • Treats nullable root objects/arrays as container roots, pointing the root frame at the contained value.
  • Makes engage() for nullable root scalars/enums return a pointer to the value inside the optional.
  • Handles top-level null safely in cbNull().

Regression tests are added for nullable object, string, and array roots.

Closes #13 The C++ code generator now handles schemas where the top-level type is nullable (`["object", "null"]`, `["string", "null"]`, `["array", "null"]`). - Renames the inner object struct for nullable root objects so the `std::optional` alias no longer conflicts with `struct Root`. - Treats nullable root objects/arrays as container roots, pointing the root frame at the contained value. - Makes `engage()` for nullable root scalars/enums return a pointer to the value inside the optional. - Handles top-level `null` safely in `cbNull()`. Regression tests are added for nullable object, string, and array roots.
weaselbot added 1 commit 2026-06-23 16:58:41 +00:00
schemagen: fix nullable root types (#13)
CI / build (-DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++, clang-arm64, ubuntu-latest-arm64, true) (pull_request) Successful in 52s
CI / pre-commit (pull_request) Successful in 54s
CI / build (-DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++, gcc-arm64, ubuntu-latest-arm64, false) (pull_request) Successful in 47s
CI / build (-DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++, clang-amd64, ubuntu-latest-amd64, true) (pull_request) Successful in 1m35s
CI / build (-DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++, gcc-amd64, ubuntu-latest-amd64, false) (pull_request) Successful in 1m27s
b5491afb38
The C++ code generator now handles schemas where the top-level type is
nullable ("type": ["object", "null"], ["string", "null"], or
["array", "null"]).

Changes to weaseljson_schemagen.py:
- Rename the inner object struct when the root is a nullable object, so
  the `using Root = std::optional<...>` alias no longer conflicts with
  `struct Root`.
- Treat nullable root objects and arrays as container roots, emplacing
  the inner value before pushing the root frame and pointing the frame at
  the contained value.
- For nullable root scalars/enums, engage() now returns a pointer to the
  value inside the optional rather than to the optional wrapper itself.
- cbNull() now safely accepts a top-level null when the root is nullable
  and rejects it otherwise.

Regression tests added:
- nullable_object.schema.json + test_nullable_root.cpp
- nullable_string.schema.json
- nullable_array.schema.json

Closes #13
weaselbot marked the pull request as ready for review 2026-06-23 16:58:47 +00:00
andrew merged commit 5427db4b3d into main 2026-06-23 17:43:20 +00:00
andrew deleted branch weaselbot/issue-13 2026-06-23 17:43:21 +00:00
Sign in to join this conversation.
No Reviewers
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: weaselab/weaseljson#29