Skip to content

Commit

Permalink
fix(config): add unknown test config format
Browse files Browse the repository at this point in the history
Signed-off-by: Fabian Wienand <[email protected]>
  • Loading branch information
Fabian Wienand committed Mar 13, 2023
1 parent 5f9e349 commit efd6bb0
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions pkg/config/jobdescriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package config
import (
"encoding/json"
"fmt"
"strings"

"gopkg.in/yaml.v3"
)
Expand All @@ -17,17 +18,16 @@ type JobDescFormat int

// List of supported job descriptor formats
const (
JobDescFormatJSON JobDescFormat = iota
JobDescFormatUnknown JobDescFormat = iota
JobDescFormatJSON
JobDescFormatYAML
)

// ParseJobDescriptor validates a job descriptor's well-formedness, and returns a
// JSON-formatted descriptor if it was provided in a different format.
// The currently supported format are JSON and YAML.
func ParseJobDescriptor(data []byte, jobDescFormat JobDescFormat) ([]byte, error) {
var (
jobDesc = make(map[string]interface{})
)
jobDesc := make(map[string]interface{})
switch jobDescFormat {
case JobDescFormatJSON:
if err := json.Unmarshal(data, &jobDesc); err != nil {
Expand All @@ -37,6 +37,8 @@ func ParseJobDescriptor(data []byte, jobDescFormat JobDescFormat) ([]byte, error
if err := yaml.Unmarshal(data, &jobDesc); err != nil {
return nil, fmt.Errorf("failed to parse YAML job descriptor: %w", err)
}
default:
return nil, fmt.Errorf("unknown job descriptor format")
}

// then marshal the structure back to JSON
Expand All @@ -46,3 +48,25 @@ func ParseJobDescriptor(data []byte, jobDescFormat JobDescFormat) ([]byte, error
}
return jobDescJSON, nil
}

func JobDescFormatFromString(jobDescFormat string) JobDescFormat {
switch {
case strings.ToLower(jobDescFormat) == "json":
return JobDescFormatJSON
case strings.ToLower(jobDescFormat) == "yaml":
return JobDescFormatYAML
default:
return JobDescFormatUnknown
}
}

func (j JobDescFormat) String() string {
switch j {
case JobDescFormatJSON:
return "json"
case JobDescFormatYAML:
return "yaml"
default:
return "unknown"
}
}

0 comments on commit efd6bb0

Please sign in to comment.