Skip to content

feat(third_party): add support for serde_json::Value#374

Merged
robjtede merged 1 commit intomainfrom
easy-foreign-types
Mar 12, 2026
Merged

feat(third_party): add support for serde_json::Value#374
robjtede merged 1 commit intomainfrom
easy-foreign-types

Conversation

@tenuous-guidance
Copy link
Contributor

@tenuous-guidance tenuous-guidance commented Mar 11, 2026

This gives a very simple way of adding support for foreign types, as per the provided example.

Summary by CodeRabbit

  • New Features

    • Optional JSON value support so serde_json::Value can be used as a configuration destination with merge semantics for primitives, arrays and objects.
  • Documentation

    • Added an example demonstrating mapping external types via JSON values.
    • Updated changelog with guidance for the JSON-backed configuration flow and example usage.
  • Tests

    • Added tests covering JSON-value merging, construction, and multi-source merging scenarios.

@coderabbitai
Copy link

coderabbitai bot commented Mar 11, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: c198f62e-8da8-4427-84af-a389a229dfee

📥 Commits

Reviewing files that changed from the base of the PR and between e19e54b and f50081d.

📒 Files selected for processing (5)
  • confik/CHANGELOG.md
  • confik/Cargo.toml
  • confik/examples/foreign_type_via_value.rs
  • confik/src/third_party.rs
  • confik/tests/third_party.rs

📝 Walkthrough

Walkthrough

Adds optional serde_json support: feature flag, example, Configuration/ConfigurationBuilder impls for serde_json::Value with merge semantics, tests, and changelog. Some implementation and test blocks are duplicated under the serde_json feature.

Changes

Cohort / File(s) Summary
Manifest & Changelog
confik/Cargo.toml, confik/CHANGELOG.md
Adds serde_json feature mapping and a new example foreign_type_via_value; documents an Unreleased entry for serde_json::Value support.
Configuration implementations
confik/src/third_party.rs
Adds impl Configuration and impl ConfigurationBuilder for serde_json::Value behind #[cfg(feature = "serde_json")], defining merge rules (primitives unchanged, arrays concatenated, objects merged/extended). Note: two identical implementation blocks are present (duplicate).
Example
confik/examples/foreign_type_via_value.rs
New example demonstrating wrapping serde_json::Value (Wrapper), TryFrom<Wrapper>ForeignType, and using #[confik(try_from = Wrapper)] to build a config from multiple TOML sources.
Tests
confik/tests/third_party.rs
Adds #[cfg(feature = "serde_json")] tests for JSON merge semantics, try_build, contains_non_secret_data, and TOML→Value loading/merging. Note: test block appears duplicated under the same feature flag.

Sequence Diagram

sequenceDiagram
    participant Builder as Config Builder
    participant TomlSource as TOML Source
    participant JsonValue as serde_json::Value
    participant Wrapper as Wrapper
    participant ForeignType as ForeignType

    Builder->>TomlSource: override_with(TOML 1)
    TomlSource-->>JsonValue: produce Value
    Builder->>TomlSource: override_with(TOML 2)
    TomlSource-->>JsonValue: produce/merge Value
    JsonValue->>JsonValue: merge (primitives/arrays/objects)
    JsonValue->>Wrapper: wrap for field
    Wrapper->>ForeignType: TryFrom conversion (serde_json::from_value)
    ForeignType-->>Builder: constructed field
    Builder->>Builder: try_build() -> Config
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

🐰 I cuddled Values, stitched TOML into a seam,
I wrapped and tried from JSON—what a dream!
Fields hopped into place with a tiny cheer,
Configs now munch data both far and near.

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 61.54% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The pull request title accurately describes the main change: adding Configuration support for serde_json::Value as a destination type, demonstrated through example and test implementations.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch easy-foreign-types

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@confik/CHANGELOG.md`:
- Around line 16-25: Fix the example in the TryFrom implementation and the
Config struct: change the call inside fn try_from(wrapper: Wrapper) from
Self::from_value(wrapper.0) to serde_json::from_value(wrapper.0) so it calls the
correct serde_json function, and correct the field name in the
#[derive(Configuration)] struct Config from forgein_type to foreign_type (the
attribute #[confik(try_from = Wrapper)] and the struct name Config remain the
same).

In `@confik/examples/foreign_type_via_value.rs`:
- Around line 1-43: Add a [[example]] entry to confik/Cargo.toml specifying
required-features so the example foreign_type_via_value.rs can compile with toml
and serde_json; specifically, add an example block with name
"foreign_type_via_value" and required-features ["toml","serde_json"] so that the
example that uses TomlSource and serde_json (see types Wrapper, ForeignType,
Config and their use in main) is built with the needed features.

In `@confik/src/third_party.rs`:
- Around line 285-301: The merge implementation currently keeps earlier
primitive values by returning the left-hand operand; update the primitive match
arm in the merge method so that when merging primitives (patterns Self::Null,
Self::Bool(_), Self::Number(_), Self::String(_)) it returns the right-hand
operand (`other`) instead of `self`, i.e. flip the pattern to match the
primitive on the right side so later sources (the `other` passed from
reduce/override_with) take precedence; leave the Array and Object arms (extend
semantics) unchanged.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: e1393b2e-e169-45bc-bacc-492e93014b92

📥 Commits

Reviewing files that changed from the base of the PR and between 2e8cc5d and a9419ac.

📒 Files selected for processing (4)
  • confik/CHANGELOG.md
  • confik/Cargo.toml
  • confik/examples/foreign_type_via_value.rs
  • confik/src/third_party.rs

This gives a very simple way of adding support for foreign types, as per the provided example.
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
confik/tests/third_party.rs (1)

154-159: Add one overlapping-key merge assertion.

This only locks in the disjoint-key case. confik/src/third_party.rs:284-301 also defines what happens when both objects contain the same key, and that precedence rule is the behavior most likely to regress.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@confik/tests/third_party.rs` around lines 154 - 159, The test
merge_objects_combine_disjoint_keys only covers disjoint keys; add an
overlapping-key assertion inside the same test
(merge_objects_combine_disjoint_keys) that calls the same merge method
(JSON::merge) with two objects sharing a key and assert the expected precedence
(the right-hand object's value should override the left-hand), e.g. assert that
merging json!({"a": 1}) with json!({"a": 2}) yields json!({"a": 2}).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@confik/tests/third_party.rs`:
- Around line 130-133: The test module named `mod serde_json { ... }` shadows
the external crate, so change the module name (e.g., `mod serde_json_tests`) or
import the external crate with an absolute path (replace `use serde_json::{json,
Value};` with `use ::serde_json::{json, Value};`) so `json` and `Value` resolve
to the external crate; update any other `serde_json` references inside the
module (including the `mod` declaration and imports) to match the rename or
absolute import.

---

Nitpick comments:
In `@confik/tests/third_party.rs`:
- Around line 154-159: The test merge_objects_combine_disjoint_keys only covers
disjoint keys; add an overlapping-key assertion inside the same test
(merge_objects_combine_disjoint_keys) that calls the same merge method
(JSON::merge) with two objects sharing a key and assert the expected precedence
(the right-hand object's value should override the left-hand), e.g. assert that
merging json!({"a": 1}) with json!({"a": 2}) yields json!({"a": 2}).

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: e026e508-8fb8-42f5-875c-b82cfbcd33e8

📥 Commits

Reviewing files that changed from the base of the PR and between a9419ac and e19e54b.

📒 Files selected for processing (5)
  • confik/CHANGELOG.md
  • confik/Cargo.toml
  • confik/examples/foreign_type_via_value.rs
  • confik/src/third_party.rs
  • confik/tests/third_party.rs
🚧 Files skipped from review as they are similar to previous changes (2)
  • confik/src/third_party.rs
  • confik/examples/foreign_type_via_value.rs

@robjtede robjtede merged commit c669682 into main Mar 12, 2026
9 checks passed
@robjtede robjtede deleted the easy-foreign-types branch March 12, 2026 11:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants