-
Notifications
You must be signed in to change notification settings - Fork 2
refactor(flagd): migrate JSONLogic evaluator to datalogic-rs v5 via C FFI #105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
shankar-gpio
wants to merge
1
commit into
open-feature:main
Choose a base branch
from
shankar-gpio:feat/migrate-flagd-to-datalogic-rs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,8 @@ | ||
| # TODO(#57): Workaround for errors related to linker script when compiling abseil: | ||
| # error: relocation refers to local symbol "...", which is defined in a discarded section | ||
| build --copt=-fno-asynchronous-unwind-tables | ||
| build --linkopt=-Wl,--gc-sections | ||
| # Auto-applies build:linux / build:macos / build:windows based on host OS. | ||
| build --enable_platform_specific_config | ||
|
|
||
| # TODO(#57): Workaround for errors related to linker script when compiling abseil | ||
| # on Linux: "error: relocation refers to local symbol '...', which is defined in | ||
| # a discarded section". GNU ld-only — macOS ld64 rejects --gc-sections. | ||
| build:linux --copt=-fno-asynchronous-unwind-tables | ||
| build:linux --linkopt=-Wl,--gc-sections |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| #include "datalogic_engine.h" | ||
|
|
||
| #include <memory> | ||
| #include <string> | ||
|
|
||
| #include "absl/status/status.h" | ||
| #include "absl/strings/str_cat.h" | ||
|
|
||
| namespace flagd { | ||
|
|
||
| namespace { | ||
|
|
||
| std::string LastError() { | ||
| const char* type = datalogic_last_error_type(); | ||
| const char* msg = datalogic_last_error_message(); | ||
| return absl::StrCat(type ? type : "Unknown", ": ", msg ? msg : "(no detail)"); | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| DatalogicEngine::DatalogicEngine() | ||
| : engine_(datalogic_engine_new(/*templating=*/0)) {} | ||
|
|
||
| DatalogicEngine::~DatalogicEngine() { datalogic_engine_free(engine_); } | ||
|
|
||
| absl::StatusOr<nlohmann::json> DatalogicEngine::Apply( | ||
| const nlohmann::json& rule, const nlohmann::json& data) const { | ||
| if (engine_ == nullptr) { | ||
| return absl::InternalError("datalogic engine not initialized"); | ||
| } | ||
| const std::string rule_str = rule.dump(); | ||
| const std::string data_str = data.dump(); | ||
|
|
||
| std::unique_ptr<char, decltype(&datalogic_string_free)> result( | ||
|
shankar-gpio marked this conversation as resolved.
|
||
| datalogic_engine_apply(engine_, rule_str.c_str(), data_str.c_str()), | ||
| datalogic_string_free); | ||
| if (result == nullptr) { | ||
| return absl::InternalError(LastError()); | ||
| } | ||
| try { | ||
| return nlohmann::json::parse(result.get()); | ||
| } catch (const nlohmann::json::exception& err) { | ||
| return absl::InternalError( | ||
| absl::StrCat("failed to parse datalogic result: ", err.what())); | ||
| } | ||
| } | ||
|
|
||
| } // namespace flagd | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| #ifndef PROVIDERS_FLAGD_SRC_EVALUATOR_DATALOGIC_ENGINE_H_ | ||
| #define PROVIDERS_FLAGD_SRC_EVALUATOR_DATALOGIC_ENGINE_H_ | ||
|
|
||
| #include <datalogic.h> | ||
|
|
||
| #include <nlohmann/json.hpp> | ||
|
|
||
| #include "absl/status/statusor.h" | ||
|
|
||
| namespace flagd { | ||
|
|
||
| // RAII wrapper around the datalogic-rs C engine handle. The underlying | ||
| // engine is `Send + Sync` (Arc-wrapped Rust handle); one instance can be | ||
| // shared across threads. | ||
| class DatalogicEngine { | ||
| public: | ||
| DatalogicEngine(); | ||
| ~DatalogicEngine(); | ||
|
|
||
| DatalogicEngine(const DatalogicEngine&) = delete; | ||
| DatalogicEngine& operator=(const DatalogicEngine&) = delete; | ||
| DatalogicEngine(DatalogicEngine&&) = delete; | ||
| DatalogicEngine& operator=(DatalogicEngine&&) = delete; | ||
|
|
||
| // One-shot evaluate: serializes `rule` and `data` to JSON strings, | ||
| // calls `datalogic_engine_apply`, parses and returns the result. | ||
| // Returns absl::InternalError carrying the thread-local | ||
| // `datalogic_last_error_*` context on parse or evaluation failure. | ||
| // | ||
| // Performance: every call re-serializes the rule and re-parses it | ||
| // inside datalogic. For hot paths, the C ABI also exposes | ||
| // `datalogic_engine_compile` + `datalogic_rule_evaluate` | ||
| // (compile-once, evaluate-many) which this wrapper could cache | ||
| // behind. Deferred until profiling motivates it. | ||
| absl::StatusOr<nlohmann::json> Apply(const nlohmann::json& rule, | ||
| const nlohmann::json& data) const; | ||
|
|
||
| private: | ||
| datalogic_engine* engine_; | ||
| }; | ||
|
|
||
| } // namespace flagd | ||
|
|
||
| #endif // PROVIDERS_FLAGD_SRC_EVALUATOR_DATALOGIC_ENGINE_H_ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.