Summary
The GetConfigString function in internal/config/config.go currently accepts the format argument as a string type. This should instead use the DataFormat type from the github.com/OpenCHAMI/ochami/pkg/format package to ensure type safety and consistency across the codebase.
Current Implementation
File: internal/config/config.go:897-926
func GetConfigString(cfg Config, key, format string) (string, error) {
val, err := GetConfig(cfg, key)
if err != nil {
return "", err
}
if val == nil {
return "", nil
}
switch val.(type) {
case map[string]interface{}, []interface{}:
var err error
var valBytes []byte
switch format {
case "yaml":
valBytes, err = yaml.Marshal(val)
case "json":
valBytes, err = json.Marshal(val)
case "json-pretty":
valBytes, err = json.MarshalIndent(val, "", "\t")
default:
return "", fmt.Errorf("unknown format: %s", format)
}
if err != nil {
return "", fmt.Errorf("failed to marshal value for key %q: %w", key, err)
}
return string(valBytes), nil
default:
return fmt.Sprintf("%v", val), nil
}
}
Problem
Using a string type for the format argument allows for invalid format values and lacks compile-time type checking. The DataFormat type in pkg/format provides proper type safety and validation.
Solution
Update the function signature to use the DataFormat type:
func GetConfigString(cfg Config, key string, format pkg/format.DataFormat) (string, error)
Additionally, review all other functions in the codebase that accept format arguments and ensure they also use the DataFormat type for consistency.
Related
pkg/format package: github.com/OpenCHAMI/ochami/pkg/format
- All functions taking format arguments should follow this pattern
Summary
The
GetConfigStringfunction ininternal/config/config.gocurrently accepts theformatargument as astringtype. This should instead use theDataFormattype from thegithub.com/OpenCHAMI/ochami/pkg/formatpackage to ensure type safety and consistency across the codebase.Current Implementation
File:
internal/config/config.go:897-926Problem
Using a
stringtype for theformatargument allows for invalid format values and lacks compile-time type checking. TheDataFormattype inpkg/formatprovides proper type safety and validation.Solution
Update the function signature to use the
DataFormattype:Additionally, review all other functions in the codebase that accept format arguments and ensure they also use the
DataFormattype for consistency.Related
pkg/formatpackage:github.com/OpenCHAMI/ochami/pkg/format