]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Meson: Generate swagger api and apidocfiles.h
authorFred Morcos <fred.morcos@open-xchange.com>
Thu, 17 Aug 2023 14:59:58 +0000 (16:59 +0200)
committerFred Morcos <fred.morcos@open-xchange.com>
Wed, 20 Mar 2024 12:28:43 +0000 (13:28 +0100)
pdns/.gitignore
pdns/generate-api-swagger.py [new file with mode: 0644]
pdns/meson.build

index b7e90bd0cc1d11663686a59e17a4bebdac56dc5f..e4d16ba7dd2eb330793c42a8eafabd7d38761271 100644 (file)
@@ -57,6 +57,7 @@ version_generated.h
 /api-swagger.yaml
 /api-swagger.json
 /.venv
+/.swagger-api-venv
 effective_tld_names.dat
 /dnsmessage.pb.cc
 /dnsmessage.pb.h
diff --git a/pdns/generate-api-swagger.py b/pdns/generate-api-swagger.py
new file mode 100644 (file)
index 0000000..5371976
--- /dev/null
@@ -0,0 +1,42 @@
+"""Produce API swagger YAML and JSON representations usable from C."""
+
+import json
+import sys
+
+import yaml
+
+
+def dump_hex(contents, array_name, string_name):
+    """Dump the hex contents to stdout."""
+    array = "static const unsigned char {0}[] = {{"
+    string = "static const std::string {0}{{(const char*){1}, sizeof({1})}};"
+    close_array = "};"
+
+    print(array.format(array_name))
+    for index, byte in enumerate(contents.encode()):
+        if (index + 1) % 15 == 0:
+            print()
+        print(f"{byte:#x}", end=", ")
+    print()
+    print(close_array)
+
+    print()
+    print(string.format(string_name, array_name))
+
+
+yaml_filename = sys.argv[1]
+
+with open(yaml_filename, mode="r", encoding="utf-8") as f_in:
+    yaml_contents = f_in.read()
+    contents = yaml.safe_load(yaml_contents)
+    json_contents = json.dumps(contents, indent=2, separators=(",", ": "))
+
+    header = "#pragma once\n#include <string>"
+    print(header)
+    print()
+
+    dump_hex(yaml_contents, "api_swagger_yamlData", "g_api_swagger_yaml")
+    print()
+    print("// -----------------------------------------------------------")
+    print()
+    dump_hex(json_contents, "api_swagger_jsonData", "g_api_swagger_json")
index 4b161e11f256cd210dcb227c21f45aae3db55ab8..78f695eaa8bcfec30f0e307a69aeb47a9edbba50 100644 (file)
@@ -256,6 +256,24 @@ libpdns = declare_dependency(
   )
 )
 
+libpdns_auth_api_swagger = custom_target(
+  'libpdns-auth-api-swagger',
+  command: [
+    python_prog,
+    '@INPUT0@',
+    '@INPUT1@',
+  ],
+  input: [
+    'generate-api-swagger.py',
+    product_source_dir / 'docs' / 'http-api' / 'swagger' / 'authoritative-api-swagger.yaml',
+  ],
+  output: 'apidocfiles.h',
+  capture: true,
+)
+libpdns_auth_api_swagger = declare_dependency(
+  sources: [libpdns_auth_api_swagger],
+)
+
 libpdns_auth_main = declare_dependency(
   link_with: static_library(
     'pdns-auth-main',
@@ -315,6 +333,7 @@ libpdns_auth = declare_dependency(
     ],
     dependencies: [
       deps,
+      libpdns_auth_api_swagger,
       libpdns_auth_lua,
       libpdns_auth_main,
       libpdns_auth_ws,