Skip to content

rainerscript: add append_json() function#6385

Merged
rgerhards merged 1 commit intorsyslog:mainfrom
20syldev:append_json
Jan 13, 2026
Merged

rainerscript: add append_json() function#6385
rgerhards merged 1 commit intorsyslog:mainfrom
20syldev:append_json

Conversation

@20syldev
Copy link
Copy Markdown
Contributor

Summary

Users need to dynamically build JSON arrays and objects by adding elements without external processing or complex workarounds.

Notes

  • Tests included: rscript_append_json.sh and rscript_append_json-vg.sh
  • Documentation added: doc/source/rainerscript/functions/rs-append_json.rst

Impact: New RainerScript function available to all users.

  • Before: No native way to append elements to JSON arrays/objects in RainerScript.
  • After: append_json(array, element) or append_json(object, key, value) returns a new JSON structure with the element/property added.

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @20syldev, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances RainerScript's JSON manipulation capabilities by introducing a new append_json() function. This function provides a native way for users to dynamically modify JSON arrays by appending elements or JSON objects by adding key-value pairs. It addresses the previous lack of such functionality, allowing for more flexible and efficient JSON processing within RainerScript configurations. The implementation ensures that the original JSON structure remains unchanged, as the function returns a newly constructed JSON object or array.

Highlights

  • New Function: append_json(): Introduces a new RainerScript function, append_json(), to dynamically build JSON arrays and objects.
  • JSON Array Manipulation: Allows appending an element to a JSON array, returning a new array with the added element.
  • JSON Object Manipulation: Enables adding a key-value pair to a JSON object, returning a new object with the new property.
  • Immutability: The append_json() function always returns a new JSON structure, ensuring the original input JSON is not modified.
  • Comprehensive Testing and Documentation: Includes new test scripts (rscript_append_json.sh, rscript_append_json-vg.sh) and dedicated documentation (rs-append_json.rst) for the new function.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new and useful append_json function to RainerScript. The implementation is mostly sound, but I've identified several critical and high-severity issues related to memory management, particularly in error-handling paths. There are multiple potential memory leaks if json-c library calls fail, and a potential crash due to an unhandled NULL return from var2CString. I've also included suggestions for a minor refactoring to reduce code duplication and for expanding the test suite to cover more use cases for this new function.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new append_json() function to RainerScript, which is a valuable addition for dynamic JSON manipulation. The implementation is mostly correct, but there are a few critical issues related to memory management and error handling when using the json-c library that could lead to memory leaks or incorrect behavior under error conditions (like out-of-memory). The provided comments offer suggestions to improve the robustness of the implementation by using safer json-c functions and adding proper error handling. Additionally, expanding the test suite to cover more use cases and edge cases for the new function is recommended.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a useful append_json function to RainerScript for manipulating JSON data. The implementation is mostly solid, with accompanying documentation and tests. However, I've identified a couple of critical memory leak issues in the object-handling logic due to missing error checks, which could lead to instability under certain conditions. I've also provided a suggestion to improve the array-handling code for better maintainability. Addressing these points will make the new function more robust.

@rgerhards

This comment was marked as outdated.

@rgerhards rgerhards self-assigned this Dec 24, 2025
@rgerhards rgerhards added this to the v8.2602 milestone Dec 24, 2025
@rgerhards

This comment was marked as outdated.

@rgerhards
Copy link
Copy Markdown
Member

@20syldev looks like this needs a rebase (not merge)

@20syldev
Copy link
Copy Markdown
Contributor Author

@20syldev looks like this needs a rebase (not merge)

Done!

@20syldev 20syldev marked this pull request as ready for review December 26, 2025 19:50
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new append_json() function to RainerScript, which is a great addition for dynamically manipulating JSON data. The implementation is mostly correct and includes documentation and tests.

My review focuses on improving the robustness and maintainability of the C implementation in grammar/rainerscript.c. I've identified a critical issue where silent failures during JSON object creation could lead to data loss. I've also suggested refactoring the error handling to align with the project's established conventions and improving variable scoping for better readability.

The documentation and test cases are well-written and cover the intended functionality. Addressing the feedback will make the new function more reliable.

@20syldev 20syldev changed the title wip: rainerscript: add append_json() function rainerscript: add append_json() function Dec 28, 2025
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new RainerScript function, append_json(), which allows users to dynamically add elements to JSON arrays and properties to JSON objects. The implementation is accompanied by documentation and tests. The overall approach is sound, creating a new JSON object to ensure immutability of the original. However, I've identified two potential memory leaks in the object-handling logic due to the use of a deprecated json-c function that doesn't report errors. My review includes suggestions to fix these issues by using the recommended _ex version of the function and adding proper error handling, consistently applying the finalize_it: label for error management as per rsyslog policies.

Copy link
Copy Markdown
Member

@rgerhards rgerhards left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can make the adjustments myself, but would appreciate if you could do them.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new append_json() function to RainerScript, allowing users to add elements to JSON arrays and properties to JSON objects. The implementation is well-structured and includes documentation and tests. My review focuses on improving the robustness and safety of the new C function. The main suggestions are to use json_object_deep_copy to prevent side-effects from shared mutable objects, to make type checking more explicit and robust, and to ensure error handling aligns with rsyslog policies.

@20syldev
Copy link
Copy Markdown
Contributor Author

20syldev commented Jan 2, 2026

Please let me know if any of the issues I marked as “Resolved” are possible, as I haven't figured out how to do it with libfastjson.

gemini-code-assist[bot]

This comment was marked as outdated.

Copy link
Copy Markdown
Member

@rgerhards rgerhards left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added comments to the AI review. Once this is through, I can do a manual round of review.

@rgerhards
Copy link
Copy Markdown
Member

/gemini review

@20syldev 20syldev requested a review from rgerhards January 5, 2026 11:13
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new and useful RainerScript function, append_json(), for dynamically building JSON arrays and objects. The implementation is well-structured, and the inclusion of documentation and tests is commendable. I've identified a critical compilation issue due to a likely function name typo, a high-severity issue related to a potential memory leak from an unchecked return value (with a reminder to align with rsyslog error handling policies), and a medium-severity suggestion to refactor some duplicated code for better maintainability. Addressing these points will make this a solid contribution.

@rgerhards rgerhards added enhancement do_first Things that should be done very soon,usually used for things that did not make it into prev release. labels Jan 12, 2026
@rgerhards
Copy link
Copy Markdown
Member

@20syldev my bad, I had a wrong merge order. I'll see that I can rebase it.

@20syldev
Copy link
Copy Markdown
Contributor Author

@20syldev my bad, I had a wrong merge order. I'll see that I can rebase it.

No problem

Users need to parse delimited strings (CSV, tags, paths) into arrays
for iteration or JSON output without external processing.

Impact: New RainerScript function available to all users.

Before: No native way to split strings into arrays in RainerScript.
After: split(string, separator) returns a JSON array of substrings.

Technical overview:
Implements doFunct_split() in grammar/rainerscript.c
Registers "split" in scriptFunct table with 2 required args
Adds CNFFUNC_SPLIT enum in rainerscript.h
Uses unified strstr-based iteration for all separator lengths
Handles edge cases: empty input, leading/trailing/consecutive delimiters
Includes error handling for json-c memory allocation failures
Returns empty JSON array on null/empty input or separator
Includes documentation (rs-split.rst) and test scripts
@rgerhards
Copy link
Copy Markdown
Member

CI fail is a know and unrelated flake

@rgerhards rgerhards merged commit 98c8394 into rsyslog:main Jan 13, 2026
46 of 47 checks passed
@20syldev
Copy link
Copy Markdown
Contributor Author

CI fail is a know and unrelated flake

Great, I was wondering about that. Thanks for the merge!

@20syldev 20syldev deleted the append_json branch January 13, 2026 23:38
@20syldev 20syldev restored the append_json branch January 17, 2026 00:26
@20syldev
Copy link
Copy Markdown
Contributor Author

@rgerhards Hi, I just noticed that the commit message was copied incorrectly for append_json. Here is the correct one:

Title:

rainerscript: add append_json() function

Description:

Users need to dynamically build JSON arrays and objects by adding
elements without external processing or complex workarounds.

Impact: New RainerScript function available to all users.

Before: No native way to append elements to JSON arrays/objects in RainerScript.
After: append_json(array, element) or append_json(object, key, value) returns
a new JSON structure with the element/property added.

Technical overview:
Implements doFunct_append() in grammar/rainerscript.c
Registers "append_json" in scriptFunct table with 2-3 args
Adds CNFFUNC_APPEND_JSON enum in rainerscript.h
Supports both JSON arrays (2 args) and objects (3 args)
Returns new object (functional style, original unchanged)
Uses json_object_iter API for object copying
Handles edge cases: null input, non-JSON input, memory allocation failures
Includes documentation (rs-append_json.rst) and test scripts

@rgerhards
Copy link
Copy Markdown
Member

rgerhards commented Jan 17, 2026

sry for that, but now it's too late. It is impossible to change commit afterwards.

@20syldev
Copy link
Copy Markdown
Contributor Author

sry for that, but now it's too late. It is impossible to change commit afterwards.

Okay, I understand!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do_first Things that should be done very soon,usually used for things that did not make it into prev release. enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants