Skip to content

[Bug]: Use DataFormat type instead of string for format arguments #125

Description

@synackd

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions