Skip to content
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

Merged
merged 10 commits into from
Jul 18, 2024
Merged

Error signature refactoring #6

merged 10 commits into from
Jul 18, 2024

Conversation

syntaqx
Copy link
Owner

@syntaqx syntaqx commented Jul 18, 2024

  • Refactors to a val, err := Get("ENV") based signature
  • Changes most functions to a val, err signature to match this
  • Breaks code into functional files for sanity
  • Renames GetSlice to GetStringSlice and supports more types
  • Adds support for multiple default slice values through default=[one,two,three]
    • Does not require the brackets if there is only 1 default item.

@syntaqx syntaqx linked an issue Jul 18, 2024 that may be closed by this pull request
Copy link

codecov bot commented Jul 18, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (577c2ff) to head (fd90697).

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.
📢 Have feedback on the report? Share it here.

@syntaqx syntaqx merged commit 021a772 into main Jul 18, 2024
5 checks passed
@syntaqx syntaqx deleted the error-signature-refactor branch July 18, 2024 04:29
Copy link

@ccoVeille ccoVeille left a 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]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Hosts []string `env:"HOSTS,default=[localhost,localhost2],required"
Hosts []string `env:"HOSTS,default=[localhost,localhost2],required"`

Comment on lines +267 to +268
err := Set("UNSUPPORTED", "invalid")
assertNoError(t, err, "Set UNSUPPORTED")

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

Suggested change
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 {

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")

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Are you open for code review?
2 participants