-
Notifications
You must be signed in to change notification settings - Fork 0
Add warn for helper func #38
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "log/slog" | ||
| "os" | ||
| "strconv" | ||
| "strings" | ||
|
|
@@ -14,22 +15,32 @@ import ( | |
| func setStringIfPresent(key string, dst *string) { | ||
| if v, ok := os.LookupEnv(key); ok { | ||
| *dst = v | ||
| } else { | ||
| slog.Warn("missing_env", "env", key) | ||
| } | ||
| } | ||
|
|
||
| func setBoolIfPresent(key string, dst *bool) { | ||
| if v, ok := os.LookupEnv(key); ok { | ||
| if b, err := strconv.ParseBool(v); err == nil { | ||
| *dst = b | ||
| } else { | ||
| slog.Warn("invalid_bool", "env", key, "value", v) | ||
| } | ||
| } else { | ||
| slog.Warn("missing_env", "env", key) | ||
| } | ||
|
Comment on lines
+28
to
32
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change introduces two issues:
Consider removing the missing environment warning and logging the error instead of the raw value. slog.Warn("invalid_bool", "env", key, "err", err)
}
} |
||
| } | ||
|
|
||
| func setIntIfPresent(key string, dst *int) { | ||
| if v, ok := os.LookupEnv(key); ok { | ||
| if i, err := strconv.Atoi(v); err == nil { | ||
| *dst = i | ||
| } else { | ||
| slog.Warn("invalid_int", "env", key, "value", v) | ||
| } | ||
| } else { | ||
| slog.Warn("missing_env", "env", key) | ||
| } | ||
|
Comment on lines
+40
to
44
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to slog.Warn("invalid_int", "env", key, "err", err)
}
} |
||
| } | ||
|
|
||
|
|
||
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.
The
setStringIfPresentfunction is designed for optional environment variables. Logging a warning when the variable is missing contradicts the "IfPresent" semantics and will result in excessive log noise for optional configuration. If a variable is mandatory, it should be handled by a separate validation step or a different helper function.