mirror of
https://github.com/optim-enterprises-bv/kubernetes.git
synced 2025-11-25 02:45:12 +00:00
Tests pass now.
This commit is contained in:
@@ -24,18 +24,32 @@ func ToWireFormat(data []byte, storage string) ([]byte, error) {
|
||||
return nil, fmt.Errorf("unknown storage type: %v", storage)
|
||||
}
|
||||
|
||||
// Try parsing as json and yaml
|
||||
parsed_json := reflect.New(prototypeType).Interface()
|
||||
json_err := json.Unmarshal(data, parsed_json)
|
||||
parsed_yaml := reflect.New(prototypeType).Interface()
|
||||
yaml_err := yaml.Unmarshal(data, parsed_yaml)
|
||||
// Try parsing json
|
||||
json_out, json_err := tryJSON(data, reflect.New(prototypeType).Interface())
|
||||
if json_err == nil {
|
||||
return json_out, json_err
|
||||
}
|
||||
|
||||
if json_err != nil && yaml_err != nil {
|
||||
// Try parsing yaml
|
||||
yaml_out, yaml_err := tryYAML(data, reflect.New(prototypeType).Interface())
|
||||
if yaml_err != nil {
|
||||
return nil, fmt.Errorf("Could not parse input as json (error: %v) or yaml (error: %v", json_err, yaml_err)
|
||||
}
|
||||
|
||||
if json_err != nil {
|
||||
return json.Marshal(parsed_json)
|
||||
}
|
||||
return json.Marshal(parsed_yaml)
|
||||
return yaml_out, yaml_err
|
||||
}
|
||||
|
||||
func tryJSON(data []byte, obj interface{}) ([]byte, error) {
|
||||
err := json.Unmarshal(data, obj)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return json.Marshal(obj)
|
||||
}
|
||||
|
||||
func tryYAML(data []byte, obj interface{}) ([]byte, error) {
|
||||
err := yaml.Unmarshal(data, obj)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return json.Marshal(obj)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user