Skip to content

Commit 234e4d9

Browse files
authored
Merge pull request #89 from deploymenttheory/dev
Refactor LoadConfigFromFile function to improve readability and error…
2 parents 13af56e + 0cb9b58 commit 234e4d9

File tree

1 file changed

+21
-41
lines changed

1 file changed

+21
-41
lines changed

httpclient/httpclient_client_configuration.go

+21-41
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package httpclient
22

33
import (
4-
"bufio"
54
"encoding/json"
65
"fmt"
7-
"io"
86
"log"
97
"os"
108
"strconv"
@@ -24,56 +22,38 @@ const (
2422
DefaultTimeout = 10 * time.Second
2523
)
2624

27-
// loadConfigFromFile loads configuration values from a JSON file into the ClientConfig struct.
28-
// It opens the specified configuration file, reads its content, and unmarshals the JSON data
29-
// into the ClientConfig struct. This function is crucial for initializing the client configuration
30-
// with values that may not be provided through environment variables or default values.
31-
// It uses Go's standard log package for logging, as the zap logger is not yet initialized when
32-
// this function is called.
33-
func (config *ClientConfig) LoadConfigFromFile(filePath string) error {
34-
// Open the configuration file
35-
file, err := os.Open(filePath)
25+
// LoadConfigFromFile loads configuration values from a JSON file into the ClientConfig struct.
26+
// This function opens the specified configuration file, reads its content, and unmarshals the JSON data
27+
// into the ClientConfig struct. It's designed to initialize the client configuration with values
28+
// from a file, complementing or overriding defaults and environment variable settings.
29+
// LoadConfigFromFile loads configuration values from a JSON file into the ClientConfig struct.
30+
func LoadConfigFromFile(filePath string) (*ClientConfig, error) {
31+
// Read the entire file
32+
fileBytes, err := os.ReadFile(filePath)
3633
if err != nil {
37-
log.Printf("Failed to open the configuration file: %s, error: %v", filePath, err)
38-
return err
39-
}
40-
defer file.Close()
41-
42-
reader := bufio.NewReader(file)
43-
var builder strings.Builder
44-
45-
// Read the file content
46-
for {
47-
part, _, err := reader.ReadLine()
48-
if err == io.EOF {
49-
break
50-
}
51-
if err != nil {
52-
log.Printf("Failed to read the configuration file: %s, error: %v", filePath, err)
53-
return err
54-
}
55-
builder.Write(part)
34+
return nil, fmt.Errorf("failed to read the configuration file: %s, error: %w", filePath, err)
5635
}
5736

58-
// Unmarshal JSON content into the ClientConfig struct
59-
err = json.Unmarshal([]byte(builder.String()), config)
60-
if err != nil {
61-
log.Printf("Failed to unmarshal the configuration file: %s, error: %v", filePath, err)
62-
return err
37+
// Initialize an instance of ClientConfig
38+
var config ClientConfig
39+
40+
// Unmarshal the file content into the ClientConfig struct
41+
if err := json.Unmarshal(fileBytes, &config); err != nil {
42+
return nil, fmt.Errorf("failed to unmarshal the configuration file: %s, error: %w", filePath, err)
6343
}
6444

6545
log.Printf("Configuration successfully loaded from file: %s", filePath)
6646

6747
// Set default values if necessary
68-
setLoggerDefaultValues(config)
69-
setClientDefaultValues(config)
48+
setLoggerDefaultValues(&config)
49+
setClientDefaultValues(&config)
7050

71-
// validate configuration
72-
if err := validateMandatoryConfiguration(config); err != nil {
73-
return fmt.Errorf("configuration validation failed: %w", err)
51+
// Validate mandatory configuration fields
52+
if err := validateMandatoryConfiguration(&config); err != nil {
53+
return nil, fmt.Errorf("configuration validation failed: %w", err)
7454
}
7555

76-
return nil
56+
return &config, nil
7757
}
7858

7959
// LoadConfigFromEnv populates the ClientConfig structure with values from environment variables.

0 commit comments

Comments
 (0)