Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
47 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,7 +25,7 @@ linters: | |
- bodyclose | ||
- depguard | ||
- dogsled | ||
- dupl | ||
# - dupl | ||
- exportloopref | ||
- exhaustive | ||
- goconst | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,56 @@ | ||
package kstatus | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"testing" | ||
|
||
"gopkg.in/yaml.v2" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/apimachinery/pkg/util/yaml" | ||
k8syaml "k8s.io/apimachinery/pkg/util/yaml" | ||
) | ||
|
||
func YamlToUnstructured(t *testing.T, yml string) *unstructured.Unstructured { | ||
t.Helper() | ||
m := make(map[string]interface{}) | ||
err := yaml.Unmarshal([]byte(yml), &m) | ||
err := k8syaml.Unmarshal([]byte(yml), &m) | ||
if err != nil { | ||
t.Fatalf("error parsing yaml: %v", err) | ||
return nil | ||
} | ||
return &unstructured.Unstructured{Object: m} | ||
} | ||
|
||
// marshal marshals the object into JSON then converts JSON to YAML and returns the | ||
// YAML. | ||
func marshal(o interface{}) ([]byte, error) { | ||
j, err := json.Marshal(o) | ||
if err != nil { | ||
return nil, fmt.Errorf("error marshaling into JSON: %v", err) | ||
} | ||
|
||
y, err := json2yaml(j) | ||
if err != nil { | ||
return nil, fmt.Errorf("error converting JSON to YAML: %v", err) | ||
} | ||
|
||
return y, nil | ||
} | ||
|
||
// json2yaml Converts JSON to YAML. | ||
func json2yaml(j []byte) ([]byte, error) { | ||
// Convert the JSON to an object. | ||
var jsonObj interface{} | ||
// We are using yaml.Unmarshal here (instead of json.Unmarshal) because the | ||
// Go JSON library doesn't try to pick the right number type (int, float, | ||
// etc.) when unmarshalling to interface{}, it just picks float64 | ||
// universally. go-yaml does go through the effort of picking the right | ||
// number type, so we can preserve number type throughout this process. | ||
err := yaml.Unmarshal(j, &jsonObj) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Marshal this object into YAML. | ||
return yaml.Marshal(jsonObj) | ||
} |