Skip to content

Commit 0a31baa

Browse files
committed
refactor: added dest parameter to load environment variables-related functions
* Added dest parameter to load environment variables-related functions * Added HTTP listener logger and related errors and functions
1 parent 0910c05 commit 0a31baa

File tree

5 files changed

+54
-110
lines changed

5 files changed

+54
-110
lines changed

env/errors.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import "errors"
44

55
var (
66
ErrNilLoader = errors.New("loader cannot be nil")
7+
ErrNilDestination = "destination cannot be nil for the given key: %v"
78
ErrEnvironmentVariableNotFound = "environment variable not found: %v"
89
ErrFailedToLoadEnvironmentVariables = errors.New("failed to load environment variables")
910
ErrInvalidDuration = "invalid key '%v' duration value: %v"

env/loader.go

Lines changed: 53 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ import (
1010
type (
1111
// Loader interface for loading environment variables
1212
Loader interface {
13-
LoadVariable(key string) (uri string, err error)
14-
LoadDurationVariable(key string) (duration time.Duration, err error)
15-
LoadSecondsVariable(key string) (seconds float64, err error)
16-
LoadIntVariable(key string) (value int, err error)
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
1717
}
1818

1919
// DefaultLoader struct
@@ -32,59 +32,80 @@ func NewDefaultLoader(loadFn func() error) (*DefaultLoader, error) {
3232
}
3333

3434
// LoadVariable load variable from environment variables
35-
func (d *DefaultLoader) LoadVariable(key string) (uri string, err error) {
35+
func (d *DefaultLoader) LoadVariable(key string, dest *string) error {
36+
// Check if the destination is nil
37+
if dest == nil {
38+
return fmt.Errorf(ErrNilDestination, key)
39+
}
40+
3641
// Get environment variable
3742
variable, exists := os.LookupEnv(key)
3843
if !exists {
39-
return "", fmt.Errorf(ErrEnvironmentVariableNotFound, key)
44+
return fmt.Errorf(ErrEnvironmentVariableNotFound, key)
4045
}
41-
return variable, nil
46+
*dest = variable
47+
return nil
4248
}
4349

4450
// LoadDurationVariable load duration variable from environment variables
45-
func (d *DefaultLoader) LoadDurationVariable(key string) (
46-
duration time.Duration,
47-
err error,
48-
) {
51+
func (d *DefaultLoader) LoadDurationVariable(
52+
key string,
53+
dest *time.Duration,
54+
) error {
55+
// Check if the destination is nil
56+
if dest == nil {
57+
return fmt.Errorf(ErrNilDestination, key)
58+
}
59+
4960
// Get environment variable
50-
variable, err := d.LoadVariable(key)
51-
if err != nil {
52-
return 0, err
61+
var durationStr string
62+
if err := d.LoadVariable(key, &durationStr); err != nil {
63+
return err
5364
}
5465

5566
// Parse the duration
56-
duration, err = time.ParseDuration(variable)
67+
duration, err := time.ParseDuration(durationStr)
5768
if err != nil {
58-
return 0, fmt.Errorf(ErrInvalidDuration, key, variable)
69+
return fmt.Errorf(ErrInvalidDuration, key, durationStr)
5970
}
60-
return duration, nil
71+
*dest = duration
72+
return nil
6173
}
6274

6375
// LoadSecondsVariable load duration variable in seconds from environment variables
64-
func (d *DefaultLoader) LoadSecondsVariable(key string) (
65-
seconds float64,
66-
err error,
67-
) {
76+
func (d *DefaultLoader) LoadSecondsVariable(key string, dest *float64) error {
77+
// Check if the destination is nil
78+
if dest == nil {
79+
return fmt.Errorf(ErrNilDestination, key)
80+
}
81+
6882
// Get the duration
69-
duration, err := d.LoadDurationVariable(key)
70-
if err != nil {
71-
return 0, err
83+
var duration time.Duration
84+
if err := d.LoadDurationVariable(key, &duration); err != nil {
85+
return err
7286
}
73-
return duration.Seconds(), nil
87+
*dest = duration.Seconds()
88+
return nil
7489
}
7590

7691
// LoadIntVariable load integer variable from environment variables
77-
func (d *DefaultLoader) LoadIntVariable(key string) (value int, err error) {
92+
func (d *DefaultLoader) LoadIntVariable(key string, dest *int) error {
93+
// Check if the destination is nil
94+
if dest == nil {
95+
return fmt.Errorf(ErrNilDestination, key)
96+
}
97+
7898
// Get environment variable
79-
variable, err := d.LoadVariable(key)
80-
if err != nil {
81-
return 0, err
99+
var valueStr string
100+
if err := d.LoadVariable(key, &valueStr); err != nil {
101+
return err
82102
}
83103

84104
// Parse the integer
85-
value, err = strconv.Atoi(variable)
105+
value, err := strconv.Atoi(valueStr)
86106
if err != nil {
87-
return 0, fmt.Errorf(ErrInvalidInteger, key, variable)
107+
return fmt.Errorf(ErrInvalidInteger, key, valueStr)
88108
}
89-
return value, nil
109+
*dest = value
110+
return nil
90111
}

http/listener/errors.go

Lines changed: 0 additions & 9 deletions
This file was deleted.

http/listener/loader.go

Lines changed: 0 additions & 39 deletions
This file was deleted.

http/listener/logger.go

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)