Skip to content

feat(confik): add js_option crate support #221

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

Merged
merged 1 commit into from
Apr 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions confik/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Implement `Configuration` for [`js_option::JsOption`](https://docs.rs/js_option/0.1.1/js_option/enum.JsOption.html)

## 0.13.0

- Update `bytesize` dependency to `2`.
Expand Down
2 changes: 2 additions & 0 deletions confik/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ camino = ["dep:camino"]
chrono = ["dep:chrono"]
common = []
ipnetwork = ["dep:ipnetwork"]
js_option = ["dep:js_option"]
rust_decimal = ["dep:rust_decimal"]
secrecy = ["dep:secrecy"]
url = ["dep:url"]
Expand All @@ -56,6 +57,7 @@ bytesize = { version = "2", optional = true, features = ["serde"] }
camino = { version = "1", optional = true, features = ["serde1"] }
chrono = { version = "0.4.40", optional = true, default-features = false, features = ["serde"] }
ipnetwork = { version = "0.21", optional = true, features = ["serde"] }
js_option = { version = "0.1", optional = true, features = ["serde"] }
rust_decimal = { version = "1", optional = true, features = ["serde"] }
secrecy = { version = "0.10", optional = true, features = ["serde"] }
url = { version = "2", optional = true, features = ["serde"] }
Expand Down
56 changes: 56 additions & 0 deletions confik/src/third_party.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,62 @@ mod ipnetwork {
}
}

#[cfg(feature = "js_option")]
mod js_option {
use js_option::JsOption;
use serde::de::DeserializeOwned;

use crate::{Configuration, ConfigurationBuilder};

impl<T> Configuration for JsOption<T>
where
T: DeserializeOwned + Configuration,
{
type Builder = JsOption<<T as Configuration>::Builder>;
}

impl<T> ConfigurationBuilder for JsOption<T>
where
T: DeserializeOwned + ConfigurationBuilder,
{
type Target = JsOption<<T as ConfigurationBuilder>::Target>;

fn merge(self, other: Self) -> Self {
match (self, other) {
// If both `Some` then merge the contained builders
(Self::Some(us), Self::Some(other)) => Self::Some(us.merge(other)),
// If we don't have a value then always take the other
(Self::Undefined, other) => other,
// Either:
// - We're explicitly `Null`
// - We're explicitly `Some` and the other is `Undefined` or `Null`
//
// In either case, just take our value, which should be preferred to other.
(us, _) => us,
}
}

fn try_build(self) -> Result<Self::Target, crate::Error> {
match self {
Self::Undefined => Ok(Self::Target::Undefined),
Self::Null => Ok(Self::Target::Null),
Self::Some(val) => Ok(Self::Target::Some(val.try_build()?)),
}
}

fn contains_non_secret_data(&self) -> Result<bool, crate::UnexpectedSecret> {
match self {
Self::Some(data) => data.contains_non_secret_data(),

// An explicit `Null` is counted as data, overriding any default.
Self::Null => Ok(true),

Self::Undefined => Ok(false),
}
}
}
}

#[cfg(feature = "secrecy")]
mod secrecy {
use secrecy::SecretString;
Expand Down
73 changes: 73 additions & 0 deletions confik/tests/third_party.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,76 @@ mod bigdecimal {
}
}
}

#[cfg(feature = "js_option")]
mod js_option {
use confik::{Configuration, TomlSource};
use js_option::JsOption;

#[derive(Configuration, Debug)]
struct Config {
opt: JsOption<usize>,
}

#[test]
fn undefined() {
let config = Config::builder()
.try_build()
.expect("Should be valid without config");
assert_eq!(config.opt, JsOption::Undefined);
}

#[cfg(feature = "json")]
#[test]
fn null() {
let json = r#"{ "opt": null }"#;

let config = Config::builder()
.override_with(confik::JsonSource::new(json))
.try_build()
.expect("Failed to parse config");
assert_eq!(config.opt, JsOption::Null);
}

#[test]
fn present() {
let toml = "opt = 5";

let config = Config::builder()
.override_with(TomlSource::new(toml))
.try_build()
.expect("Should be valid without config");
assert_eq!(config.opt, JsOption::Some(5));
}

#[cfg(feature = "json")]
#[test]
fn merge() {
#[derive(Debug, Configuration, PartialEq, Eq)]
struct Config {
one: JsOption<usize>,
two: JsOption<usize>,
three: JsOption<usize>,
four: JsOption<usize>,
}

let base = r#"{ "two": null, "three": 5 }"#;
let merge = r#"{ "one": 1, "two": 2, "three": 3}"#;

let config = Config::builder()
.override_with(confik::JsonSource::new(merge))
.override_with(confik::JsonSource::new(base))
.try_build()
.expect("Failed to parse config");

assert_eq!(
config,
Config {
one: JsOption::Some(1),
two: JsOption::Null,
three: JsOption::Some(5),
four: JsOption::Undefined,
}
);
}
}
Loading