Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
oliveromahony committed Dec 12, 2024
1 parent 5c0ff91 commit eada69c
Show file tree
Hide file tree
Showing 4 changed files with 365 additions and 20 deletions.
70 changes: 53 additions & 17 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package config

import (
"context"
"encoding/json"
"errors"
"fmt"
"log/slog"
Expand Down Expand Up @@ -422,31 +423,66 @@ func resolveLabels() map[string]interface{} {
result := make(map[string]interface{})

for key, value := range input {
// Try to parse as an integer
if intValue, err := strconv.Atoi(value); err == nil {
result[key] = intValue
continue
}
trimmedValue := strings.TrimSpace(value)

// Try to parse as a float
if floatValue, err := strconv.ParseFloat(value, 64); err == nil {
result[key] = floatValue
continue
}
switch {
case trimmedValue == "" || trimmedValue == "nil": // Handle empty values as nil
result[key] = nil

// Try to parse as a boolean
if boolValue, err := strconv.ParseBool(value); err == nil {
result[key] = boolValue
continue
}
case parseInt(trimmedValue) != nil: // Integer
result[key] = parseInt(trimmedValue)

case parseFloat(trimmedValue) != nil: // Float
result[key] = parseFloat(trimmedValue)

// If no conversion was possible, keep it as a string
result[key] = value
case parseBool(trimmedValue) != nil: // Boolean
result[key] = parseBool(trimmedValue)

case parseJSON(trimmedValue) != nil: // JSON object/array
result[key] = parseJSON(trimmedValue)

default: // String
result[key] = trimmedValue
}
}

return result
}

// Parsing helper functions return the parsed value or nil if parsing fails
func parseInt(value string) interface{} {
if intValue, err := strconv.Atoi(value); err == nil {
return intValue
}

return nil
}

func parseFloat(value string) interface{} {
if floatValue, err := strconv.ParseFloat(value, 64); err == nil {
return floatValue
}

return nil
}

func parseBool(value string) interface{} {
if boolValue, err := strconv.ParseBool(value); err == nil {
return boolValue
}

return nil
}

func parseJSON(value string) interface{} {
var jsonValue interface{}
if err := json.Unmarshal([]byte(value), &jsonValue); err == nil {
return jsonValue
}

return nil
}

func resolveDataPlaneConfig() *DataPlaneConfig {
return &DataPlaneConfig{
Nginx: &NginxDataPlaneConfig{
Expand Down
Loading

0 comments on commit eada69c

Please sign in to comment.