forked from ghodss/yaml
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use changes in sigs.k8s.io/json & match API-style
Signed-off-by: Inteon <[email protected]>
- Loading branch information
Showing
8 changed files
with
238 additions
and
193 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 |
---|---|---|
@@ -1,5 +1,10 @@ | ||
module github.com/amurant/yaml | ||
module sigs.k8s.io/yaml | ||
|
||
go 1.16 | ||
go 1.17 | ||
|
||
require gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b | ||
require ( | ||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b | ||
sigs.k8s.io/json v0.0.0-20211020170558-c049b76a60c6 | ||
) | ||
|
||
replace gopkg.in/yaml.v3 => github.com/amurant/go-yaml v0.0.0-20211021125301-a69ce44590ee |
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,4 +1,6 @@ | ||
github.com/amurant/go-yaml v0.0.0-20211021125301-a69ce44590ee h1:hh4GlzhK0sFfqNroFo5dgDa4NylwldjzlweQzm85sdI= | ||
github.com/amurant/go-yaml v0.0.0-20211021125301-a69ce44590ee/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= | ||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= | ||
sigs.k8s.io/json v0.0.0-20211020170558-c049b76a60c6 h1:fD1pz4yfdADVNfFmcP2aBEtudwUQ1AlLnRBALr33v3s= | ||
sigs.k8s.io/json v0.0.0-20211020170558-c049b76a60c6/go.mod h1:p4QtZmO4uMYipTQNzagwnNoseA6OxSUutVw05NhYDRs= |
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package yaml | ||
|
||
import ( | ||
"fmt" | ||
|
||
"encoding/json" | ||
|
||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
// JSONToYAML Converts JSON to YAML. | ||
func JSONToYAML(jsonBytes []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. | ||
if err := yaml.Unmarshal(jsonBytes, &jsonObj); err != nil { | ||
return nil, fmt.Errorf("error converting JSON to YAML: %v", err) | ||
} | ||
|
||
// Marshal this object into YAML. | ||
yamlBytes, err := yaml.Marshal(jsonObj) | ||
if err != nil { | ||
return nil, fmt.Errorf("error converting JSON to YAML: %v", err) | ||
} | ||
|
||
return yamlBytes, nil | ||
} | ||
|
||
// Marshal marshals the object into JSON then converts JSON to YAML and returns the | ||
// YAML. | ||
func Marshal(obj interface{}) ([]byte, error) { | ||
jsonBytes, err := json.Marshal(obj) | ||
if err != nil { | ||
return nil, fmt.Errorf("error marshaling into JSON: %v", err) | ||
} | ||
|
||
return JSONToYAML(jsonBytes) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package yaml | ||
|
||
import ( | ||
"fmt" | ||
"math" | ||
"reflect" | ||
"strconv" | ||
"testing" | ||
) | ||
|
||
func strPtr(str string) *string { | ||
return &str | ||
} | ||
|
||
type MarshalTest struct { | ||
A string | ||
B int64 | ||
C float64 | ||
} | ||
|
||
func TestMarshal(t *testing.T) { | ||
f64String := strconv.FormatFloat(math.MaxFloat64, 'g', -1, 64) | ||
s := MarshalTest{"a", math.MaxInt64, math.MaxFloat64} | ||
e := []byte(fmt.Sprintf("A: a\nB: %d\nC: %s\n", math.MaxInt64, f64String)) | ||
|
||
y, err := Marshal(s) | ||
if err != nil { | ||
t.Errorf("error marshaling YAML: %v", err) | ||
} | ||
|
||
if !reflect.DeepEqual(y, e) { | ||
t.Errorf("marshal YAML was unsuccessful, expected: %#v, got: %#v", | ||
string(e), string(y)) | ||
} | ||
} |
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
Oops, something went wrong.