|
1 | 1 | # Environment Variable JSON Flag Provider
|
2 | 2 |
|
3 |
| -This repository contains a very simple environment variable based feature flag provider. |
4 |
| -This provider uses a JSON evaluation for matching a flag `Variant` to a provided `EvaluationContext`. Each flag `Variant` contains a slice of `Criteria`, if all `Criteria` match then the flags value is returned. Each `Variant` is evaluated starting at index 0, therefore the first matching `Variant` is returned. Each variant also has a `TargetingKey`, when set it must match the `TargetingKey` provided in the `EvaluationContext` for the `Variant` to be returned. |
| 3 | +This repository contains a very simple environment variable based feature flag provider. |
| 4 | +This provider uses a JSON evaluation for matching a flag `Variant` to a provided `EvaluationContext`. Each flag `Variant` contains a slice of `Criteria`, if all `Criteria` match then the flags value is returned. Each `Variant` is evaluated starting at index 0, therefore the first matching `Variant` is returned. Each variant also has a `TargetingKey`, when set it must match the `TargetingKey` provided in the `EvaluationContext` for the `Variant` to be returned. |
5 | 5 |
|
| 6 | +## Flag Configuration Structure |
6 | 7 |
|
7 |
| -## Flag Configuration Structure. |
| 8 | +Flag configurations are stored as JSON strings, with one configuration per flag key. An example configuration is described below. |
8 | 9 |
|
9 |
| -Flag configurations are stored as JSON strings, with one configuration per flag key. An example configuration is described below. |
10 | 10 | ```json
|
11 | 11 | {
|
12 | 12 | "defaultVariant": "not-yellow",
|
@@ -43,10 +43,11 @@ Flag configurations are stored as JSON strings, with one configuration per flag
|
43 | 43 | }
|
44 | 44 | ```
|
45 | 45 |
|
46 |
| -## Example Usage |
47 |
| -Below is a simple example of using this `Provider`, in this example the above flag configuration is saved with the key `AM_I_YELLOW.` |
| 46 | +## Example Usage |
48 | 47 |
|
49 |
| -``` |
| 48 | +Below is a simple example of using this `Provider`, in this example the above flag configuration is saved with the key `AM_I_YELLOW.` |
| 49 | + |
| 50 | +```sh |
50 | 51 | export AM_I_YELLOW='{"defaultVariant":"not-yellow","variants":[{"name":"yellow-with-key","targetingKey":"user","criteria":[{"key":"color","value":"yellow"}],"value":true},{"name":"yellow","targetingKey":"","criteria":[{"key":"color","value":"yellow"}],"value":true},{"name":"not-yellow","targetingKey":"","criteria": [],"value":false}]}'
|
51 | 52 | ```
|
52 | 53 |
|
@@ -107,20 +108,37 @@ func main() {
|
107 | 108 | )
|
108 | 109 | fmt.Println(resS, err)
|
109 | 110 | }
|
110 |
| - |
111 | 111 | ```
|
| 112 | + |
112 | 113 | Console output:
|
113 |
| -``` |
| 114 | + |
| 115 | +```console |
114 | 116 | {AM_I_YELLOW 0 {true TARGETING_MATCH yellow}} <nil>
|
115 | 117 | {AM_I_YELLOW 0 {true TARGETING_MATCH yellow-with-key}} <nil>
|
116 | 118 | {i am a default value {AM_I_YELLOW string { ERROR TYPE_MISMATCH }}} error code: TYPE_MISMATCH
|
117 | 119 | ```
|
118 | 120 |
|
119 |
| -## Common Error Response Types |
| 121 | +### Name Mapping |
| 122 | + |
| 123 | +To transform the flag name into an environment variable name at runtime, you can use the option `WithFlagToEnvMapper`. |
120 | 124 |
|
121 |
| -Error Value | Error Reason |
122 |
| -------------- | ------------- |
123 |
| -PARSE_ERROR | A required `DefaultVariant` does not exist, or, the stored flag configuration cannot be parsed into the `StoredFlag` struct |
124 |
| -TYPE_MISMATCH | The responses value type does not match that of the request. |
125 |
| -FLAG_NOT_FOUND | The requested flag key does not have an associated environment variable. |
| 125 | +For example: |
| 126 | + |
| 127 | +```go |
| 128 | +mapper := func(flagKey string) string { |
| 129 | + return fmt.Sprintf("MY_%s", strings.ToUpper(strings.ReplaceAll(flagKey, "-", "_"))) |
| 130 | +} |
| 131 | + |
| 132 | +p := fromEnv.NewProvider(fromEnv.WithFlagToEnvMapper(mapper)) |
| 133 | + |
| 134 | +// This will look up MY_SOME_FLAG env variable |
| 135 | +res := p.BooleanEvaluation(context.Background(), "some-flag", false, evalCtx) |
| 136 | +``` |
| 137 | + |
| 138 | +## Common Error Response Types |
126 | 139 |
|
| 140 | +| Error Value | Error Reason | |
| 141 | +| -------------- | --------------------------------------------------------------------------------------------------------------------------- | |
| 142 | +| PARSE_ERROR | A required `DefaultVariant` does not exist, or, the stored flag configuration cannot be parsed into the `StoredFlag` struct | |
| 143 | +| TYPE_MISMATCH | The responses value type does not match that of the request. | |
| 144 | +| FLAG_NOT_FOUND | The requested flag key does not have an associated environment variable. | |
0 commit comments