#!/usr/bin/env python3 """Emit tests/conformance/cpp/fixture_dispatcher.hpp. velox::proto::Dispatcher has one pure virtual per method, on purpose: a daemon that forgets to implement a method does not compile. That is exactly what a conformance runner needs, and also what makes one tedious to hand-write — so it is generated. The dispatcher answers each method from that method's golden fixture, which lets the C++ side exercise the real dispatch path: envelope handling, the transport check, parameter parsing, and result serialisation. Run: python3 contracts/codegen/gen_cpp_conformance.py """ from __future__ import annotations import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent)) from gen_cpp import BANNER, cpp_type, handler_name # noqa: E402 from schema_ir import load # noqa: E402 OUT = Path(__file__).resolve().parent.parent.parent / "tests" / "conformance" / "cpp" / "fixture_dispatcher.hpp" def main() -> int: c = load() o = [BANNER.format(version=c.version), "#pragma once", "", '#include "velox_proto.hpp"', "", "#include ", "#include ", "", "namespace velox::conformance {", "", "/// Answers every method from its golden fixture, so the generated dispatch path", "/// itself is under test: envelope, transport check, param parse, result serialise.", "class FixtureDispatcher final : public proto::Dispatcher {", "public:", " /// `results` maps a method name to that method's golden result JSON.", " explicit FixtureDispatcher(std::function results)", " : results_(std::move(results)) {}", ""] for m in c.methods: # Every method's params and result is a named struct, and this class lives in # velox::conformance, so the names need qualifying. pt, rt = "proto::" + cpp_type(m.params), "proto::" + cpp_type(m.result) o += [ f" proto::Result<{rt}> {handler_name(m.name)}(const {pt}& params) override {{", " (void)params;", f' return golden<{rt}>("{m.name}");', " }", "", ] o += [ "private:", " template ", " proto::Result golden(const std::string& method) {", " const nlohmann::json* value = results_(method);", " if (value == nullptr)", ' return std::unexpected(proto::ParseError{method, "no fixture for this method"});', " return proto::parse(*value, method);", " }", "", " std::function results_;", "};", "", "} // namespace velox::conformance", "", ] OUT.parent.mkdir(parents=True, exist_ok=True) OUT.write_text("\n".join(o)) print(f"gen_cpp_conformance: {len(c.methods)} handlers -> {OUT}") return 0 if __name__ == "__main__": raise SystemExit(main())