Skip to content

Commit

Permalink
feat: update is_on check to string and readme
Browse files Browse the repository at this point in the history
  • Loading branch information
D-nDev committed Nov 6, 2024
1 parent 9e8ac34 commit 7f78611
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 3 deletions.
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

0 comments on commit 7f78611

Please sign in to comment.