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
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
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
This commit is contained in:
@@ -46,6 +46,49 @@ target_link_libraries(schemagen_big PRIVATE ${PROJECT_NAME})
|
||||
target_compile_options(schemagen_big PRIVATE -Wno-switch-enum)
|
||||
add_dependencies(schemagen_big schemagen_big_h)
|
||||
|
||||
set(NULLABLE_OBJECT_SCHEMA
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/nullable_object.schema.json)
|
||||
set(NULLABLE_STRING_SCHEMA
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/nullable_string.schema.json)
|
||||
set(NULLABLE_ARRAY_SCHEMA
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/nullable_array.schema.json)
|
||||
set(NULLABLE_OBJECT_H ${CMAKE_CURRENT_BINARY_DIR}/nullable_object.h)
|
||||
set(NULLABLE_STRING_H ${CMAKE_CURRENT_BINARY_DIR}/nullable_string.h)
|
||||
set(NULLABLE_ARRAY_H ${CMAKE_CURRENT_BINARY_DIR}/nullable_array.h)
|
||||
|
||||
add_custom_command(
|
||||
OUTPUT ${NULLABLE_OBJECT_H}
|
||||
COMMAND ${Python3_EXECUTABLE} ${SCHEMAGEN_SCRIPT} ${NULLABLE_OBJECT_SCHEMA} -o
|
||||
${NULLABLE_OBJECT_H} --namespace nullable_object
|
||||
DEPENDS ${SCHEMAGEN_SCRIPT} ${NULLABLE_OBJECT_SCHEMA}
|
||||
COMMENT "Generating nullable_object.h")
|
||||
|
||||
add_custom_command(
|
||||
OUTPUT ${NULLABLE_STRING_H}
|
||||
COMMAND ${Python3_EXECUTABLE} ${SCHEMAGEN_SCRIPT} ${NULLABLE_STRING_SCHEMA} -o
|
||||
${NULLABLE_STRING_H} --namespace nullable_string
|
||||
DEPENDS ${SCHEMAGEN_SCRIPT} ${NULLABLE_STRING_SCHEMA}
|
||||
COMMENT "Generating nullable_string.h")
|
||||
|
||||
add_custom_command(
|
||||
OUTPUT ${NULLABLE_ARRAY_H}
|
||||
COMMAND ${Python3_EXECUTABLE} ${SCHEMAGEN_SCRIPT} ${NULLABLE_ARRAY_SCHEMA} -o
|
||||
${NULLABLE_ARRAY_H} --namespace nullable_array
|
||||
DEPENDS ${SCHEMAGEN_SCRIPT} ${NULLABLE_ARRAY_SCHEMA}
|
||||
COMMENT "Generating nullable_array.h")
|
||||
|
||||
add_custom_target(
|
||||
schemagen_nullable_h DEPENDS ${NULLABLE_OBJECT_H} ${NULLABLE_STRING_H}
|
||||
${NULLABLE_ARRAY_H})
|
||||
|
||||
add_executable(schemagen_nullable_root
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/test_nullable_root.cpp)
|
||||
target_include_directories(schemagen_nullable_root
|
||||
PRIVATE include ${CMAKE_CURRENT_BINARY_DIR})
|
||||
target_link_libraries(schemagen_nullable_root PRIVATE ${PROJECT_NAME})
|
||||
target_compile_options(schemagen_nullable_root PRIVATE -Wno-switch-enum)
|
||||
add_dependencies(schemagen_nullable_root schemagen_nullable_h)
|
||||
|
||||
add_test(
|
||||
NAME schemagen_example
|
||||
COMMAND schemagen_example
|
||||
@@ -55,3 +98,8 @@ add_test(
|
||||
NAME schemagen_big
|
||||
COMMAND schemagen_big
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
|
||||
|
||||
add_test(
|
||||
NAME schemagen_nullable_root
|
||||
COMMAND schemagen_nullable_root
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
|
||||
|
||||
Reference in New Issue
Block a user