-
Notifications
You must be signed in to change notification settings - Fork 8
Env or default #56
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
Open
deankarn
wants to merge
10
commits into
master
Choose a base branch
from
env-or-default
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Env or default #56
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
285610b
Add osext.EnvOrDefault
deankarn d9e0d67
update actions
deankarn 349ad7f
update actions
deankarn 744e3f4
tag test package too
deankarn 45b5e2a
expand go version tests
deankarn 978827a
enhance
deankarn d690d0c
old linter
deankarn 6c69cac
simplify
deankarn 8b602a0
extend, simplify and reuse
deankarn 97b758e
increase version
deankarn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,79 @@ | ||
| //go:build go1.22 | ||
| // +build go1.22 | ||
|
|
||
| package osext | ||
|
|
||
| import ( | ||
| "os" | ||
| "reflect" | ||
| "strconv" | ||
|
|
||
| constraintsext "github.com/go-playground/pkg/v5/constraints" | ||
| . "github.com/go-playground/pkg/v5/values/option" | ||
| . "github.com/go-playground/pkg/v5/values/result" | ||
| ) | ||
|
|
||
| // EnvDefaults interface defines the supported types that can be used for environment variables conversions. | ||
| type EnvDefaults interface { | ||
| constraintsext.Number | ~string | ||
| } | ||
|
|
||
| // Env retrieves the value of the environment variable named by the key. | ||
| // | ||
| // If the variable is not set, or if the conversion fails due to incorrect value, | ||
| // a default value is returned. | ||
| func Env[T EnvDefaults](key string, defaultValue T) T { | ||
| return GetEnv[T](key).UnwrapOr(defaultValue) | ||
| } | ||
|
|
||
| // GetEnv retrieves the value of the environment variable named by the key. | ||
| // | ||
| // If the variable is not set, or if the conversion fails it returns `None`, otherwise `Some`. | ||
| func GetEnv[T EnvDefaults](key string) Option[T] { | ||
| r := LookupEnv[T](key) | ||
| if r.IsErr() { | ||
| return None[T]() | ||
| } | ||
| return r.Unwrap() | ||
| } | ||
|
|
||
| // LookupEnv retrieves the value of the environment variable named by the key. | ||
| // | ||
| // If the variable is not present it returns Ok(None) | ||
| // If the variable is present and conversion is successful, the value Ok(Some) is returned | ||
| // If the variable is present and conversion fails it returns Err(error) | ||
| func LookupEnv[T EnvDefaults](key string) Result[Option[T], error] { | ||
| if v, ok := os.LookupEnv(key); ok { | ||
| ty := reflect.TypeFor[T]() | ||
| elem := reflect.New(ty).Elem() | ||
|
|
||
| switch ty.Kind() { | ||
| case reflect.String: | ||
| elem.SetString(v) | ||
| return Ok[Option[T], error](Some(elem.Interface().(T))) | ||
| case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: | ||
| i, err := strconv.ParseInt(v, 10, ty.Bits()) | ||
| if err != nil { | ||
| return Err[Option[T], error](err) | ||
| } | ||
| elem.SetInt(i) | ||
| return Ok[Option[T], error](Some(elem.Interface().(T))) | ||
|
|
||
| case reflect.Uintptr, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: | ||
| i, err := strconv.ParseUint(v, 10, ty.Bits()) | ||
| if err != nil { | ||
| return Err[Option[T], error](err) | ||
| } | ||
| elem.SetUint(i) | ||
| return Ok[Option[T], error](Some(elem.Interface().(T))) | ||
| case reflect.Float32, reflect.Float64: | ||
| f, err := strconv.ParseFloat(v, ty.Bits()) | ||
| if err != nil { | ||
| return Err[Option[T], error](err) | ||
| } | ||
| elem.SetFloat(f) | ||
| return Ok[Option[T], error](Some(elem.Interface().(T))) | ||
| } | ||
| } | ||
| return Ok[Option[T], error](None[T]()) | ||
| } |
This file contains hidden or 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,98 @@ | ||
| //go:build go1.22 | ||
| // +build go1.22 | ||
|
|
||
| package osext | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "math/rand" | ||
| "testing" | ||
| ) | ||
|
|
||
| type ( | ||
| CustomInt int | ||
| CustomInt8 int8 | ||
| CustomInt16 int16 | ||
| CustomInt32 int32 | ||
| CustomInt64 int64 | ||
| CustomUint uint | ||
| CustomUint8 uint8 | ||
| CustomUint16 uint16 | ||
| CustomUint32 uint32 | ||
| CustomUint64 uint64 | ||
| CustomUintptr uintptr | ||
| CustomFloat32 float32 | ||
| CustomFloat64 float64 | ||
| CustomString string | ||
| ) | ||
|
|
||
| func TestEnv(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| testFunc func() error | ||
| }{ | ||
| // Integer types | ||
| {"int", func() error { return SetAndUnsetTest(t, "24", 24, 42) }}, | ||
| {"CustomInt", func() error { return SetAndUnsetTest(t, "24", CustomInt(24), CustomInt(42)) }}, | ||
| {"int8", func() error { return SetAndUnsetTest(t, "24", int8(24), int8(42)) }}, | ||
| {"CustomInt8", func() error { return SetAndUnsetTest(t, "24", CustomInt8(24), CustomInt8(42)) }}, | ||
| {"int16", func() error { return SetAndUnsetTest(t, "24", int16(24), int16(42)) }}, | ||
| {"CustomInt16", func() error { return SetAndUnsetTest(t, "24", CustomInt16(24), CustomInt16(42)) }}, | ||
| {"int32", func() error { return SetAndUnsetTest(t, "24", int32(24), int32(42)) }}, | ||
| {"CustomInt32", func() error { return SetAndUnsetTest(t, "24", CustomInt32(24), CustomInt32(42)) }}, | ||
| {"int64", func() error { return SetAndUnsetTest(t, "24", int64(24), int64(42)) }}, | ||
| {"CustomInt64", func() error { return SetAndUnsetTest(t, "24", CustomInt64(24), CustomInt64(42)) }}, | ||
| {"uint", func() error { return SetAndUnsetTest(t, "24", uint(24), uint(42)) }}, | ||
| {"CustomUint", func() error { return SetAndUnsetTest(t, "24", CustomUint(24), CustomUint(42)) }}, | ||
| {"uint8", func() error { return SetAndUnsetTest(t, "24", uint8(24), uint8(42)) }}, | ||
| {"CustomUint8", func() error { return SetAndUnsetTest(t, "24", CustomUint8(24), CustomUint8(42)) }}, | ||
| {"uint16", func() error { return SetAndUnsetTest(t, "24", uint16(24), uint16(42)) }}, | ||
| {"CustomUint16", func() error { return SetAndUnsetTest(t, "24", CustomUint16(24), CustomUint16(42)) }}, | ||
| {"uint32", func() error { return SetAndUnsetTest(t, "24", uint32(24), uint32(42)) }}, | ||
| {"CustomUint32", func() error { return SetAndUnsetTest(t, "24", CustomUint32(24), CustomUint32(42)) }}, | ||
| {"uint64", func() error { return SetAndUnsetTest(t, "24", uint64(24), uint64(42)) }}, | ||
| {"CustomUint64", func() error { return SetAndUnsetTest(t, "24", CustomUint64(24), CustomUint64(42)) }}, | ||
| {"uintptr", func() error { return SetAndUnsetTest(t, "24", uintptr(24), uintptr(42)) }}, | ||
| {"CustomUintptr", func() error { return SetAndUnsetTest(t, "24", CustomUintptr(24), CustomUintptr(42)) }}, | ||
|
|
||
| // Float types | ||
| {"float32", func() error { return SetAndUnsetTest(t, "24.5", float32(24.5), float32(42.5)) }}, | ||
| {"CustomFloat32", func() error { return SetAndUnsetTest(t, "24.5", CustomFloat32(24.5), CustomFloat32(42.5)) }}, | ||
| {"float64", func() error { return SetAndUnsetTest(t, "24.5", float64(24.5), float64(42.5)) }}, | ||
| {"CustomFloat64", func() error { return SetAndUnsetTest(t, "24.5", CustomFloat64(24.5), CustomFloat64(42.5)) }}, | ||
|
|
||
| // String types | ||
| {"string", func() error { return SetAndUnsetTest(t, "hello", "hello", "world") }}, | ||
| {"CustomString", func() error { return SetAndUnsetTest(t, "hello", CustomString("hello"), CustomString("world")) }}, | ||
|
|
||
| // Conversion failure test cases - these should return default values when conversion fails | ||
| {"int_conversion_failure", func() error { return SetAndUnsetTest(t, "not_a_number", 42, 42) }}, | ||
| {"float32_conversion_failure", func() error { return SetAndUnsetTest(t, "not_a_float", float32(42.5), float32(42.5)) }}, | ||
| {"float64_conversion_failure", func() error { return SetAndUnsetTest(t, "not_a_float", float64(42.5), float64(42.5)) }}, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| if err := tt.testFunc(); err != nil { | ||
| t.Errorf("Test %s failed: %v", tt.name, err) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func SetAndUnsetTest[T EnvDefaults](t *testing.T, envValueSet string, expectedSet T, expectedDefault T) error { | ||
| constTestEnvKey := fmt.Sprintf("TEST_ENV_%d", rand.Intn(1000000)) | ||
|
|
||
| v := Env(constTestEnvKey, expectedDefault) | ||
| if v != expectedDefault { | ||
| return fmt.Errorf("default value mismatch: got %v, want %v", v, expectedDefault) | ||
| } | ||
|
|
||
| t.Setenv(constTestEnvKey, envValueSet) | ||
|
|
||
| v = Env(constTestEnvKey, expectedDefault) | ||
| if v != expectedSet { | ||
| return fmt.Errorf("set value mismatch: got %v, want %v", v, expectedSet) | ||
| } | ||
| return nil | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use
assert.Equallike other test files?