-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from antonmashko/feat/custom-external-injection
Feat/custom external injection
- Loading branch information
Showing
8 changed files
with
133 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# External | ||
This package provides implementations of External option for EnvConf parsing process. | ||
|
||
Implement your own external or use any of already implemented | ||
|
||
## Usage | ||
```golang | ||
json := `{"foo":"bar"}` | ||
tc := struct { | ||
Foo string `env:"ENV_FOO"` | ||
}{} | ||
err := envconf.Parse(&tc, | ||
option.WithExternal(jsonconf.Json([]byte(json)))) | ||
``` | ||
If env variable is it not specified for Foo variable, EnvConf will define it from json input. | ||
|
||
## Wrapped external | ||
- json (encoding/json) | ||
- yaml (gopkg.in/yaml.v3) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package option | ||
|
||
import "strings" | ||
|
||
type withExternalEnvInjection func(string) (string, ConfigSource) | ||
|
||
func (f withExternalEnvInjection) Apply(opts *Options) { | ||
opts.externalInjection = f | ||
} | ||
|
||
// WithExternalInjection allows to inject variables from an external source | ||
// use format ${ .env.<ENV_VAR_NAME> } to make an env variable lookup while getting a field from the external source | ||
func WithExternalInjection() ClientOption { | ||
return WithCustomExternalInjection(nil) | ||
} | ||
|
||
func WithCustomExternalInjection(f func(string) (string, ConfigSource)) ClientOption { | ||
if f == nil { | ||
f = func(value string) (string, ConfigSource) { | ||
var ok bool | ||
const prefix = "${" | ||
value, ok = strings.CutPrefix(value, prefix) | ||
if !ok { | ||
return "", NoConfigValue | ||
} | ||
|
||
const suffix = "}" | ||
value, ok = strings.CutSuffix(value, suffix) | ||
if !ok { | ||
return "", NoConfigValue | ||
} | ||
|
||
value = strings.TrimSpace(value) | ||
const envInjectionPrefix = ".env." | ||
value, ok = strings.CutPrefix(value, envInjectionPrefix) | ||
if !ok { | ||
return "", NoConfigValue | ||
} | ||
return value, EnvVariable | ||
} | ||
} | ||
return withExternalEnvInjection(f) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package option | ||
|
||
import "testing" | ||
|
||
func TestWithCustomExternalInjection(t *testing.T) { | ||
tcs := []struct { | ||
name string | ||
value string | ||
resultVal string | ||
resultCS ConfigSource | ||
}{ | ||
{name: "ValidEnvVar_Ok", value: "${ .env.ENV_VAR }", | ||
resultVal: "ENV_VAR", resultCS: EnvVariable}, | ||
{name: "InvalidEnvVar_WithoutPrefix_Err", value: ".env.ENV_VAR }", | ||
resultVal: "", resultCS: NoConfigValue}, | ||
{name: "InvalidEnvVar_WithoutBracketSuffix_Err", value: "${ .env.ENV_VAR", | ||
resultVal: "", resultCS: NoConfigValue}, | ||
{name: "InvalidEnvVar_WithoutInjectionName_Err", value: "${ .ENV_VAR }", | ||
resultVal: "", resultCS: NoConfigValue}, | ||
} | ||
for _, tc := range tcs { | ||
t.Run(tc.name, func(t *testing.T) { | ||
f := WithExternalInjection() | ||
opts := &Options{} | ||
f.Apply(opts) | ||
res, cs := opts.ExternalInjection()(tc.value) | ||
if res != tc.resultVal || cs != tc.resultCS { | ||
t.Fatalf("unexpected result: expected={'%s','%s'} actual={'%s','%s'}", | ||
tc.resultVal, tc.resultCS, res, cs) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestWithCustomExternalInjection_CustomFunc_Ok(t *testing.T) { | ||
const suffix = "_test" | ||
const expectedCS = ExternalSource | ||
f := WithCustomExternalInjection(func(s string) (string, ConfigSource) { | ||
return s + suffix, expectedCS | ||
}) | ||
opts := &Options{} | ||
f.Apply(opts) | ||
const value = "abc" | ||
res, cs := opts.ExternalInjection()(value) | ||
if res != value+suffix || cs != expectedCS { | ||
t.Fatalf("unexpected result: expected={'%s','%s'} actual={'%s','%s'}", | ||
value+suffix, expectedCS, res, cs) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters