Skip to content
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

feat: update is_on check to string and readme #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "growthbook-rust-sdk"
version = "0.1.0"
version = "0.1.1"
edition = "2021"
authors = [
"Deroldo <[email protected]",
Expand Down
67 changes: 67 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,73 @@ Initializing SDK
let gb_url = "HTTP_OR_HTTPS_URL";
let sdk_key = "SDK_KEY";
let gb = GrowthBookClient::new(gb_url, sdk_key, None, None)?;
```

## Checking if a Feature is Enabled or Disabled

With GrowthBook, you can determine if a feature is on or off by checking the feature's value using different data types. A feature is considered off (i.e., is_on is false) under the following conditions, based on the data type:

### String Feature

If the feature value (regardless if it is default_value, force, or derived from an experiment) is an **empty string** or explicitly **OFF**, then is_on will be false.

```rust
let gb_url = "HTTP_OR_HTTPS_URL";
let sdk_key = "SDK_KEY";
let gb = GrowthBookClient::new(gb_url, sdk_key, None, None)?;

let my_gb_feature = gb.feature_result(
"my_feature_string".to_string(),
Some(gb_attributes.clone()) // Optional: use attributes or set to None
);

```

### Numeric Feature

If the feature value is **0** (regardless if it is default_value, force, or derived from an experiment), then is_on will be false.

```rust
let gb_url = "HTTP_OR_HTTPS_URL";
let sdk_key = "SDK_KEY";
let gb = GrowthBookClient::new(gb_url, sdk_key, None, None)?;

let my_gb_feature = gb.feature_result(
"my_feature_numeric".to_string(),
Some(gb_attributes.clone()) // Optional: use attributes or set to None
);

```

### Object (HashMap) Feature

If the feature value is an **empty object** (regardless if it is default_value, force, or derived from an experiment), then is_on will be false.

```rust
let gb_url = "HTTP_OR_HTTPS_URL";
let sdk_key = "SDK_KEY";
let gb = GrowthBookClient::new(gb_url, sdk_key, None, None)?;

let my_gb_feature = gb.feature_result(
"my_feature_as_object".to_string(),
Some(gb_attributes.clone()) // Optional: use attributes or set to None
);

```

### Array Feature

If the feature value is an **empty array** (regardless if it is default_value, force, or derived from an experiment), then is_on will be false.

```rust
let gb_url = "HTTP_OR_HTTPS_URL";
let sdk_key = "SDK_KEY";
let gb = GrowthBookClient::new(gb_url, sdk_key, None, None)?;

let my_gb_feature = gb.feature_result(
"my_feature_as_array".to_string(),
Some(gb_attributes.clone()) // Optional: use attributes or set to None
);

```

Expand Down
2 changes: 1 addition & 1 deletion src/model_private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ impl FeatureResult {
fn is_on(value: &Value) -> bool {
let is_on = if value.is_null() {
false
} else if (value.is_number() && value.force_f64(-1.0) != 0.0) || (value.is_string() && value.force_string("any") != "") {
} else if (value.is_number() && value.force_f64(-1.0) != 0.0) || (value.is_string() && (value.force_string("any") != "" && value.force_string("any") != "OFF")) {
true
} else if value.is_boolean() {
value.as_bool().unwrap_or(false)
Expand Down
6 changes: 6 additions & 0 deletions tests/commons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,12 @@ pub async fn all_cases(
"simple-flag": {
"defaultValue": true
},
"is-off-string-flag": {
"defaultValue": "OFF"
},
"is-off-empty-string-flag": {
"defaultValue": ""
},
"simple-flag-disabled": {
"defaultValue": false
},
Expand Down
31 changes: 31 additions & 0 deletions tests/is_off_string_value.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
mod commons;

#[cfg(test)]
mod test {
use rstest::rstest;
use test_context::test_context;

use crate::commons::TestContext;

#[test_context(TestContext)]
#[rstest]
#[tokio::test]
async fn should_return_off_when_value_is_string_value_off(ctx: &mut TestContext) -> Result<(), Box<dyn std::error::Error>> {
let feature = ctx.growthbook.feature_result("is-off-string-flag", None);

assert!(!feature.on);

Ok(())
}

#[test_context(TestContext)]
#[rstest]
#[tokio::test]
async fn should_return_off_when_value_is_string_value_is_empty(ctx: &mut TestContext) -> Result<(), Box<dyn std::error::Error>> {
let feature = ctx.growthbook.feature_result("is-off-empty-string-flag", None);

assert!(!feature.on);

Ok(())
}
}