-
-
Notifications
You must be signed in to change notification settings - Fork 1
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
Error signature refactoring #6
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #6 +/- ##
==========================================
Coverage 100.00% 100.00%
==========================================
Files 1 4 +3
Lines 138 244 +106
==========================================
+ Hits 138 244 +106 ☔ View full report in Codecov by Sentry. |
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.
Good refactoring, a few remarks anyway
|
||
```go | ||
type Config struct { | ||
ReadDatabase DatabaseConfig `env:"WRITE_DATABASE"` | ||
WriteDatabase DatabaseConfig `env:"READ_DATABASE"` | ||
Hosts []string `env:"HOSTS,default=[localhost,localhost2] |
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.
Hosts []string `env:"HOSTS,default=[localhost,localhost2] | |
Hosts []string `env:"HOSTS,default=[localhost,localhost2]` |
with `DATABASE_` as they are looked for. This allows you to reuse the same | ||
struct in multiple places without having to worry about conflicting environment | ||
variables. | ||
However if you want to declare multiple values as the default, you must enclose |
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.
However if you want to declare multiple values as the default, you must enclose | |
However, if you want to declare multiple values as the default, you must enclose |
|
||
```go | ||
type Config struct { | ||
Hosts []string `env:"HOSTS,default=[localhost,localhost2],required" |
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.
Hosts []string `env:"HOSTS,default=[localhost,localhost2],required" | |
Hosts []string `env:"HOSTS,default=[localhost,localhost2],required"` |
err := Set("UNSUPPORTED", "invalid") | ||
assertNoError(t, err, "Set UNSUPPORTED") |
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.
Here, and in a few other places, you could/should use the test helper
err := Set("UNSUPPORTED", "invalid") | |
assertNoError(t, err, "Set UNSUPPORTED") | |
setEnvForTest(t, "UNSUPPORTED", "invalid") |
assertError(t, err, "Unmarshal Required") | ||
|
||
expectedErr := "required environment variable REQUIRED_VAR is not set" | ||
if err != nil && err.Error() != expectedErr { |
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.
You could add a test helper
func assertErrorContains(t *testing.T, err string, needle string) {
assertError(t, err)
if !strings.Contains(err.Error(), needle) {
t.Fatal("blah blah")
}
}
Float64Value float64 `env:"FLOAT64_VALUE"` | ||
} | ||
err := Unmarshal(&cfg) | ||
assertNoError(t, err, "Unmarshal FloatConfig") |
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.
What about moving your asserter to a package
This way, you will use assert.NoError
, assert.Equal
…
It could be a local package, or a go module in a dedicated repository you would reuse in all your dev
val, err := Get("ENV")
based signatureval, err
signature to match thisGetSlice
toGetStringSlice
and supports more typesdefault
slice values throughdefault=[one,two,three]