From e800f1110389d3c878bb8601d399c62a56c57ef1 Mon Sep 17 00:00:00 2001 From: yukkop Date: Wed, 23 Apr 2025 19:14:29 +0000 Subject: [PATCH] refactor: `hemar`: logs --- package/c/hectic/default.nix | 4 +- package/c/hemar/hemar--0.1.sql | 26 ++++++++-- package/c/hemar/hemar.c | 89 ++++++++++++++++++++++++++++++---- 3 files changed, 105 insertions(+), 14 deletions(-) diff --git a/package/c/hectic/default.nix b/package/c/hectic/default.nix index b11b4e5..16e4973 100644 --- a/package/c/hectic/default.nix +++ b/package/c/hectic/default.nix @@ -77,7 +77,7 @@ stdenv.mkDerivation { esac shift done - EOF +EOF chmod +x $out/bin/hectic-config ''; @@ -86,4 +86,4 @@ stdenv.mkDerivation { description = "hectic"; license = lib.licenses.mit; }; -} +} \ No newline at end of file diff --git a/package/c/hemar/hemar--0.1.sql b/package/c/hemar/hemar--0.1.sql index 5e49ee4..f49a958 100755 --- a/package/c/hemar/hemar--0.1.sql +++ b/package/c/hemar/hemar--0.1.sql @@ -20,7 +20,27 @@ CREATE SCHEMA hemar; -- $hemar$ -- ); -- ``` -CREATE FUNCTION "hemar"."render"("declare" json, "template" text) +CREATE FUNCTION "hemar"."render"("declare" jsonb, "template" text) RETURNS text -AS 'hemar', 'render' -LANGUAGE C STRICT; \ No newline at end of file +AS 'hemar', 'pg_render' +LANGUAGE C STRICT; + +CREATE FUNCTION "hemar"."test_log"() +RETURNS void +AS 'hemar', 'pg_test_log' +LANGUAGE C STRICT; + +CREATE FUNCTION "hemar"."test_log_2"(text, text) +RETURNS void +AS 'hemar', 'pg_test_log_2' +LANGUAGE C STRICT; + +CREATE FUNCTION "hemar"."test_log_3"(name1 text, name2 text) +RETURNS void +AS 'hemar', 'pg_test_log_2' +LANGUAGE C STRICT; + +CREATE FUNCTION "hemar"."template_parse"(text) +RETURNS void +AS 'hemar', 'pg_template_parse' +LANGUAGE C STRICT; diff --git a/package/c/hemar/hemar.c b/package/c/hemar/hemar.c index 16ab98b..1fb1c75 100755 --- a/package/c/hemar/hemar.c +++ b/package/c/hemar/hemar.c @@ -2,6 +2,7 @@ #include #include #include +#include #include "hectic.h" #include @@ -9,6 +10,19 @@ PG_MODULE_MAGIC; #endif +#define LOG_FILE "/tmp/hemar.log" + +#define INIT \ + logger_init(); \ + logger_level(LOG_LEVEL_TRACE); \ + logger_set_file(LOG_FILE); \ + logger_set_output_mode(LOG_OUTPUT_BOTH); \ + Arena arena = arena_init(MEM_MiB); + +#define FREE \ + arena_free(&arena); \ + logger_free(); + /* helper function to get a JSON value by key path */ static Json *json_get_by_path(Arena *arena, const Json *context, const char *key_path) { @@ -297,7 +311,7 @@ static char *render_template_node(Arena *arena, const TemplateNode *node, const } /* Define the function render */ -PG_FUNCTION_INFO_V1(render); +PG_FUNCTION_INFO_V1(pg_render); /* * Function to render templates using hectic library with JSON context @@ -305,17 +319,22 @@ PG_FUNCTION_INFO_V1(render); * 1. declare - JSON context for rendering * 2. template - The template text to render */ -Datum render(PG_FUNCTION_ARGS) +Datum pg_render(PG_FUNCTION_ARGS) { + INIT; + + printf("Rendering template\n"); + text *context_text = PG_GETARG_TEXT_PP(0); text *template_text = PG_GETARG_TEXT_PP(1); + + printf("Context: %s\n", text_to_cstring(context_text)); /* Convert input text to C string */ char *template_str = text_to_cstring(template_text); char *context_str = text_to_cstring(context_text); - - /* Initialize arena for memory management */ - Arena arena = arena_init(MEM_MiB); + + printf("Template: %s\n", template_str); TemplateNode root_node; TemplateResult template_result; @@ -333,7 +352,7 @@ Datum render(PG_FUNCTION_ARGS) context = json_parse(&arena, &json_ptr); if (!context) { - arena_free(&arena); + FREE; ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("Invalid JSON context"))); } @@ -344,7 +363,7 @@ Datum render(PG_FUNCTION_ARGS) template_result = template_parse(&arena, &template_ptr, &config); if (IS_RESULT_ERROR(template_result)) { - arena_free(&arena); + FREE; ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("Failed to parse template: %s", RESULT_ERROR_MESSAGE(template_result)))); @@ -357,7 +376,59 @@ Datum render(PG_FUNCTION_ARGS) /* Prepare return value */ result = cstring_to_text(result_str); - arena_free(&arena); - + FREE; PG_RETURN_TEXT_P(result); } + +PG_FUNCTION_INFO_V1(pg_test_log); + +Datum pg_test_log(PG_FUNCTION_ARGS) { + INIT; + raise_info("Testing log"); + + FREE; + PG_RETURN_VOID(); +} + +PG_FUNCTION_INFO_V1(pg_test_log_2); + +Datum pg_test_log_2(PG_FUNCTION_ARGS) { + INIT; + raise_info("Testing log"); + + text *context_text = PG_GETARG_TEXT_PP(0); + text *template_text = PG_GETARG_TEXT_PP(1); + + raise_info("Context: %s", text_to_cstring(context_text)); + raise_info("Template: %s", text_to_cstring(template_text)); + + FREE; + PG_RETURN_VOID(); +} + +PG_FUNCTION_INFO_V1(pg_template_parse); + +Datum pg_template_parse(PG_FUNCTION_ARGS) { + INIT; + + TemplateConfig config; + const char *template_ptr; + TemplateResult template_result; + + template_ptr = "{{ name }}"; + config = template_default_config(); + template_result = template_parse(&arena, &template_ptr, &config); + + if (IS_RESULT_ERROR(template_result)) { + FREE; + ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("Failed to parse template: %s", + RESULT_ERROR_MESSAGE(template_result)))); + } + + const char *json_str = "{\"a\": 1}"; + Jsonb *jb = DatumGetJsonbP(DirectFunctionCall1(jsonb_in, CStringGetDatum(json_str))); + + FREE; + PG_RETURN_VOID(); +} \ No newline at end of file