Merge pull request 'schemagen: keep null elements in arrays with nullable item types' (#8) from weaselbot/weaseljson:weaselbot/issue-5 into main
CI / build (-DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++, clang-arm64, ubuntu-latest-arm64, true) (push) Successful in 53s
CI / pre-commit (push) Successful in 52s
CI / build (-DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++, gcc-arm64, ubuntu-latest-arm64, false) (push) Successful in 50s
CI / build (-DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++, clang-amd64, ubuntu-latest-amd64, true) (push) Successful in 1m31s
CI / build (-DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++, gcc-amd64, ubuntu-latest-amd64, false) (push) Successful in 1m25s
CI / build (-DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++, clang-arm64, ubuntu-latest-arm64, true) (push) Successful in 53s
CI / pre-commit (push) Successful in 52s
CI / build (-DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++, gcc-arm64, ubuntu-latest-arm64, false) (push) Successful in 50s
CI / build (-DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++, clang-amd64, ubuntu-latest-amd64, true) (push) Successful in 1m31s
CI / build (-DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++, gcc-amd64, ubuntu-latest-amd64, false) (push) Successful in 1m25s
Reviewed-on: #8
This commit was merged in pull request #8.
This commit is contained in:
@@ -718,6 +718,34 @@ namespace {ns} {{"""
|
||||
" }"
|
||||
)
|
||||
|
||||
def _is_array_kind(self):
|
||||
arrs = [n for n, _ in self.arr_types]
|
||||
if not arrs:
|
||||
return " bool isArrayKind(Kind) const { return false; }"
|
||||
cases = " ".join(f"case Kind::{n}:" for n in arrs)
|
||||
return (
|
||||
" bool isArrayKind(Kind k) const {\n"
|
||||
f" switch (k) {{ {cases} return true; default: return false; }}\n"
|
||||
" }"
|
||||
)
|
||||
|
||||
def _append_null(self):
|
||||
lines = [
|
||||
" void appendNull(Frame &f) {",
|
||||
" switch (f.kind) {",
|
||||
]
|
||||
for name, tarr in self.arr_types:
|
||||
vectype = self.base_cpp(tarr)
|
||||
lines.append(f" case Kind::{name}: {{")
|
||||
lines.append(f" auto *v = ({vectype} *)f.dest;")
|
||||
lines.append(" v->emplace_back();")
|
||||
lines.append(" return;")
|
||||
lines.append(" }")
|
||||
lines.append(" default: return;")
|
||||
lines.append(" }")
|
||||
lines.append(" }")
|
||||
return "\n".join(lines)
|
||||
|
||||
def _is_strict(self):
|
||||
strict = [n for n, o in self.b.objects.items() if o.strict]
|
||||
if not strict:
|
||||
@@ -992,6 +1020,10 @@ private:
|
||||
valueComplete();
|
||||
}}
|
||||
|
||||
{self._is_array_kind()}
|
||||
|
||||
{self._append_null()}
|
||||
|
||||
void cbNull() {{
|
||||
if (error_) return;
|
||||
Frame &f = stack_.back();
|
||||
@@ -999,6 +1031,7 @@ private:
|
||||
SlotInfo si = slotInfoG(f);
|
||||
if (si.cat == Cat::Skip) {{ valueComplete(); return; }}
|
||||
if (!si.nullable) {{ reject(); return; }}
|
||||
if (isArrayKind(f.kind)) appendNull(f); // keep null array elements
|
||||
valueComplete(); // leave optional empty / pointer null
|
||||
}}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user