@@ -2,28 +2,31 @@ package env
22
33import (
44 "fmt"
5+ "log/slog"
56 "os"
67 "strconv"
78 "time"
89)
910
1011type (
11- // Loader interface for loading environment variables
12- Loader interface {
13- LoadVariable (key string , dest * string ) error
14- LoadDurationVariable (key string , dest * time.Duration ) error
15- LoadSecondsVariable (key string , dest * float64 ) error
16- LoadIntVariable (key string , dest * int ) error
17- }
18-
1912 // DefaultLoader struct
2013 DefaultLoader struct {
21- logger * Logger
14+ logger * slog. Logger
2215 }
2316)
2417
2518// NewDefaultLoader creates a new default environment variable loader
26- func NewDefaultLoader (loadFn func () error , logger * Logger ) (
19+ //
20+ // Parameters:
21+ //
22+ // - loadFn: function to load the environment variables (e.g., from a file)
23+ // - logger: logger instance for logging
24+ //
25+ // Returns:
26+ //
27+ // - *DefaultLoader: instance of DefaultLoader
28+ // - error: error if any occurred during loading
29+ func NewDefaultLoader (loadFn func () error , logger * slog.Logger ) (
2730 * DefaultLoader ,
2831 error ,
2932) {
@@ -33,11 +36,25 @@ func NewDefaultLoader(loadFn func() error, logger *Logger) (
3336 return nil , ErrFailedToLoadEnvironmentVariables
3437 }
3538 }
39+
40+ // Prepare the logger
41+ if logger != nil {
42+ logger = logger .With ("component" , "env_loader" )
43+ }
3644 return & DefaultLoader {logger }, nil
3745}
3846
3947// LoadVariable load variable from environment variables
40- func (d * DefaultLoader ) LoadVariable (key string , dest * string ) error {
48+ //
49+ // Parameters:
50+ //
51+ // - key: environment variable key
52+ // - dest: pointer to the destination string where the value will be stored
53+ //
54+ // Returns:
55+ //
56+ // - error: error if any occurred during loading or parsing
57+ func (d DefaultLoader ) LoadVariable (key string , dest * string ) error {
4158 // Check if the destination is nil
4259 if dest == nil {
4360 return fmt .Errorf (ErrNilDestination , key )
@@ -51,15 +68,22 @@ func (d *DefaultLoader) LoadVariable(key string, dest *string) error {
5168 * dest = variable
5269
5370 // Log the environment variable
54- if d .logger != nil {
55- d .logger .EnvironmentVariableLoaded (key )
56- }
71+ EnvironmentVariableLoaded (d .logger , key )
5772
5873 return nil
5974}
6075
6176// LoadDurationVariable load duration variable from environment variables
62- func (d * DefaultLoader ) LoadDurationVariable (
77+ //
78+ // Parameters:
79+ //
80+ // - key: environment variable key
81+ // - dest: pointer to the destination time.Duration where the value will be stored
82+ //
83+ // Returns:
84+ //
85+ // - error: error if any occurred during loading or parsing
86+ func (d DefaultLoader ) LoadDurationVariable (
6387 key string ,
6488 dest * time.Duration ,
6589) error {
@@ -84,7 +108,16 @@ func (d *DefaultLoader) LoadDurationVariable(
84108}
85109
86110// LoadSecondsVariable load duration variable in seconds from environment variables
87- func (d * DefaultLoader ) LoadSecondsVariable (key string , dest * float64 ) error {
111+ //
112+ // Parameters:
113+ //
114+ // - key: environment variable key
115+ // - dest: pointer to the destination float64 where the value in seconds will be stored
116+ //
117+ // Returns:
118+ //
119+ // - error: error if any occurred during loading or parsing
120+ func (d DefaultLoader ) LoadSecondsVariable (key string , dest * float64 ) error {
88121 // Check if the destination is nil
89122 if dest == nil {
90123 return fmt .Errorf (ErrNilDestination , key )
@@ -100,7 +133,16 @@ func (d *DefaultLoader) LoadSecondsVariable(key string, dest *float64) error {
100133}
101134
102135// LoadIntVariable load integer variable from environment variables
103- func (d * DefaultLoader ) LoadIntVariable (key string , dest * int ) error {
136+ //
137+ // Parameters:
138+ //
139+ // - key: environment variable key
140+ // - dest: pointer to the destination int where the value will be stored
141+ //
142+ // Returns:
143+ //
144+ // - error: error if any occurred during loading or parsing
145+ func (d DefaultLoader ) LoadIntVariable (key string , dest * int ) error {
104146 // Check if the destination is nil
105147 if dest == nil {
106148 return fmt .Errorf (ErrNilDestination , key )
0 commit comments