mirror of
https://github.com/ceph/ceph-csi.git
synced 2025-06-13 10:33:35 +00:00
20
vendor/sigs.k8s.io/yaml/.gitignore
generated
vendored
20
vendor/sigs.k8s.io/yaml/.gitignore
generated
vendored
@ -1,20 +0,0 @@
|
||||
# OSX leaves these everywhere on SMB shares
|
||||
._*
|
||||
|
||||
# Eclipse files
|
||||
.classpath
|
||||
.project
|
||||
.settings/**
|
||||
|
||||
# Emacs save files
|
||||
*~
|
||||
|
||||
# Vim-related files
|
||||
[._]*.s[a-w][a-z]
|
||||
[._]s[a-w][a-z]
|
||||
*.un~
|
||||
Session.vim
|
||||
.netrwhist
|
||||
|
||||
# Go test binaries
|
||||
*.test
|
14
vendor/sigs.k8s.io/yaml/.travis.yml
generated
vendored
14
vendor/sigs.k8s.io/yaml/.travis.yml
generated
vendored
@ -1,14 +0,0 @@
|
||||
language: go
|
||||
dist: xenial
|
||||
go:
|
||||
- 1.9.x
|
||||
- 1.10.x
|
||||
- 1.11.x
|
||||
script:
|
||||
- go get -t -v ./...
|
||||
- diff -u <(echo -n) <(gofmt -d .)
|
||||
- diff -u <(echo -n) <(golint $(go list -e ./...) | grep -v YAMLToJSON)
|
||||
- go tool vet .
|
||||
- go test -v -race ./...
|
||||
install:
|
||||
- go get golang.org/x/lint/golint
|
31
vendor/sigs.k8s.io/yaml/CONTRIBUTING.md
generated
vendored
31
vendor/sigs.k8s.io/yaml/CONTRIBUTING.md
generated
vendored
@ -1,31 +0,0 @@
|
||||
# Contributing Guidelines
|
||||
|
||||
Welcome to Kubernetes. We are excited about the prospect of you joining our [community](https://github.com/kubernetes/community)! The Kubernetes community abides by the CNCF [code of conduct](code-of-conduct.md). Here is an excerpt:
|
||||
|
||||
_As contributors and maintainers of this project, and in the interest of fostering an open and welcoming community, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities._
|
||||
|
||||
## Getting Started
|
||||
|
||||
We have full documentation on how to get started contributing here:
|
||||
|
||||
<!---
|
||||
If your repo has certain guidelines for contribution, put them here ahead of the general k8s resources
|
||||
-->
|
||||
|
||||
- [Contributor License Agreement](https://git.k8s.io/community/CLA.md) Kubernetes projects require that you sign a Contributor License Agreement (CLA) before we can accept your pull requests
|
||||
- [Kubernetes Contributor Guide](http://git.k8s.io/community/contributors/guide) - Main contributor documentation, or you can just jump directly to the [contributing section](http://git.k8s.io/community/contributors/guide#contributing)
|
||||
- [Contributor Cheat Sheet](https://git.k8s.io/community/contributors/guide/contributor-cheatsheet.md) - Common resources for existing developers
|
||||
|
||||
## Mentorship
|
||||
|
||||
- [Mentoring Initiatives](https://git.k8s.io/community/mentoring) - We have a diverse set of mentorship programs available that are always looking for volunteers!
|
||||
|
||||
<!---
|
||||
Custom Information - if you're copying this template for the first time you can add custom content here, for example:
|
||||
|
||||
## Contact Information
|
||||
|
||||
- [Slack channel](https://kubernetes.slack.com/messages/kubernetes-users) - Replace `kubernetes-users` with your slack channel string, this will send users directly to your channel.
|
||||
- [Mailing list](URL)
|
||||
|
||||
-->
|
25
vendor/sigs.k8s.io/yaml/OWNERS
generated
vendored
25
vendor/sigs.k8s.io/yaml/OWNERS
generated
vendored
@ -1,25 +0,0 @@
|
||||
approvers:
|
||||
- dims
|
||||
- lavalamp
|
||||
- smarterclayton
|
||||
- deads2k
|
||||
- sttts
|
||||
- liggitt
|
||||
- caesarxuchao
|
||||
reviewers:
|
||||
- dims
|
||||
- thockin
|
||||
- lavalamp
|
||||
- smarterclayton
|
||||
- wojtek-t
|
||||
- deads2k
|
||||
- derekwaynecarr
|
||||
- caesarxuchao
|
||||
- mikedanese
|
||||
- liggitt
|
||||
- gmarek
|
||||
- sttts
|
||||
- ncdc
|
||||
- tallclair
|
||||
labels:
|
||||
- sig/api-machinery
|
121
vendor/sigs.k8s.io/yaml/README.md
generated
vendored
121
vendor/sigs.k8s.io/yaml/README.md
generated
vendored
@ -1,121 +0,0 @@
|
||||
# YAML marshaling and unmarshaling support for Go
|
||||
|
||||
[](https://travis-ci.org/ghodss/yaml)
|
||||
|
||||
## Introduction
|
||||
|
||||
A wrapper around [go-yaml](https://github.com/go-yaml/yaml) designed to enable a better way of handling YAML when marshaling to and from structs.
|
||||
|
||||
In short, this library first converts YAML to JSON using go-yaml and then uses `json.Marshal` and `json.Unmarshal` to convert to or from the struct. This means that it effectively reuses the JSON struct tags as well as the custom JSON methods `MarshalJSON` and `UnmarshalJSON` unlike go-yaml. For a detailed overview of the rationale behind this method, [see this blog post](http://ghodss.com/2014/the-right-way-to-handle-yaml-in-golang/).
|
||||
|
||||
## Compatibility
|
||||
|
||||
This package uses [go-yaml](https://github.com/go-yaml/yaml) and therefore supports [everything go-yaml supports](https://github.com/go-yaml/yaml#compatibility).
|
||||
|
||||
## Caveats
|
||||
|
||||
**Caveat #1:** When using `yaml.Marshal` and `yaml.Unmarshal`, binary data should NOT be preceded with the `!!binary` YAML tag. If you do, go-yaml will convert the binary data from base64 to native binary data, which is not compatible with JSON. You can still use binary in your YAML files though - just store them without the `!!binary` tag and decode the base64 in your code (e.g. in the custom JSON methods `MarshalJSON` and `UnmarshalJSON`). This also has the benefit that your YAML and your JSON binary data will be decoded exactly the same way. As an example:
|
||||
|
||||
```
|
||||
BAD:
|
||||
exampleKey: !!binary gIGC
|
||||
|
||||
GOOD:
|
||||
exampleKey: gIGC
|
||||
... and decode the base64 data in your code.
|
||||
```
|
||||
|
||||
**Caveat #2:** When using `YAMLToJSON` directly, maps with keys that are maps will result in an error since this is not supported by JSON. This error will occur in `Unmarshal` as well since you can't unmarshal map keys anyways since struct fields can't be keys.
|
||||
|
||||
## Installation and usage
|
||||
|
||||
To install, run:
|
||||
|
||||
```
|
||||
$ go get github.com/ghodss/yaml
|
||||
```
|
||||
|
||||
And import using:
|
||||
|
||||
```
|
||||
import "github.com/ghodss/yaml"
|
||||
```
|
||||
|
||||
Usage is very similar to the JSON library:
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/ghodss/yaml"
|
||||
)
|
||||
|
||||
type Person struct {
|
||||
Name string `json:"name"` // Affects YAML field names too.
|
||||
Age int `json:"age"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
// Marshal a Person struct to YAML.
|
||||
p := Person{"John", 30}
|
||||
y, err := yaml.Marshal(p)
|
||||
if err != nil {
|
||||
fmt.Printf("err: %v\n", err)
|
||||
return
|
||||
}
|
||||
fmt.Println(string(y))
|
||||
/* Output:
|
||||
age: 30
|
||||
name: John
|
||||
*/
|
||||
|
||||
// Unmarshal the YAML back into a Person struct.
|
||||
var p2 Person
|
||||
err = yaml.Unmarshal(y, &p2)
|
||||
if err != nil {
|
||||
fmt.Printf("err: %v\n", err)
|
||||
return
|
||||
}
|
||||
fmt.Println(p2)
|
||||
/* Output:
|
||||
{John 30}
|
||||
*/
|
||||
}
|
||||
```
|
||||
|
||||
`yaml.YAMLToJSON` and `yaml.JSONToYAML` methods are also available:
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/ghodss/yaml"
|
||||
)
|
||||
|
||||
func main() {
|
||||
j := []byte(`{"name": "John", "age": 30}`)
|
||||
y, err := yaml.JSONToYAML(j)
|
||||
if err != nil {
|
||||
fmt.Printf("err: %v\n", err)
|
||||
return
|
||||
}
|
||||
fmt.Println(string(y))
|
||||
/* Output:
|
||||
name: John
|
||||
age: 30
|
||||
*/
|
||||
j2, err := yaml.YAMLToJSON(y)
|
||||
if err != nil {
|
||||
fmt.Printf("err: %v\n", err)
|
||||
return
|
||||
}
|
||||
fmt.Println(string(j2))
|
||||
/* Output:
|
||||
{"age":30,"name":"John"}
|
||||
*/
|
||||
}
|
||||
```
|
9
vendor/sigs.k8s.io/yaml/RELEASE.md
generated
vendored
9
vendor/sigs.k8s.io/yaml/RELEASE.md
generated
vendored
@ -1,9 +0,0 @@
|
||||
# Release Process
|
||||
|
||||
The `yaml` Project is released on an as-needed basis. The process is as follows:
|
||||
|
||||
1. An issue is proposing a new release with a changelog since the last release
|
||||
1. All [OWNERS](OWNERS) must LGTM this release
|
||||
1. An OWNER runs `git tag -s $VERSION` and inserts the changelog and pushes the tag with `git push $VERSION`
|
||||
1. The release issue is closed
|
||||
1. An announcement email is sent to `kubernetes-dev@googlegroups.com` with the subject `[ANNOUNCE] kubernetes-template-project $VERSION is released`
|
17
vendor/sigs.k8s.io/yaml/SECURITY_CONTACTS
generated
vendored
17
vendor/sigs.k8s.io/yaml/SECURITY_CONTACTS
generated
vendored
@ -1,17 +0,0 @@
|
||||
# Defined below are the security contacts for this repo.
|
||||
#
|
||||
# They are the contact point for the Product Security Team to reach out
|
||||
# to for triaging and handling of incoming issues.
|
||||
#
|
||||
# The below names agree to abide by the
|
||||
# [Embargo Policy](https://github.com/kubernetes/sig-release/blob/master/security-release-process-documentation/security-release-process.md#embargo-policy)
|
||||
# and will be removed and replaced if they violate that agreement.
|
||||
#
|
||||
# DO NOT REPORT SECURITY VULNERABILITIES DIRECTLY TO THESE NAMES, FOLLOW THE
|
||||
# INSTRUCTIONS AT https://kubernetes.io/security/
|
||||
|
||||
cjcullen
|
||||
jessfraz
|
||||
liggitt
|
||||
philips
|
||||
tallclair
|
3
vendor/sigs.k8s.io/yaml/code-of-conduct.md
generated
vendored
3
vendor/sigs.k8s.io/yaml/code-of-conduct.md
generated
vendored
@ -1,3 +0,0 @@
|
||||
# Kubernetes Community Code of Conduct
|
||||
|
||||
Please refer to our [Kubernetes Community Code of Conduct](https://git.k8s.io/community/code-of-conduct.md)
|
46
vendor/sigs.k8s.io/yaml/yaml_go110_test.go
generated
vendored
46
vendor/sigs.k8s.io/yaml/yaml_go110_test.go
generated
vendored
@ -1,46 +0,0 @@
|
||||
// +build go1.10
|
||||
|
||||
package yaml
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestUnmarshalWithTags(t *testing.T) {
|
||||
type WithTaggedField struct {
|
||||
Field string `json:"field"`
|
||||
}
|
||||
|
||||
t.Run("Known tagged field", func(t *testing.T) {
|
||||
y := []byte(`field: "hello"`)
|
||||
v := WithTaggedField{}
|
||||
if err := Unmarshal(y, &v, DisallowUnknownFields); err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if v.Field != "hello" {
|
||||
t.Errorf("v.Field=%v, want 'hello'", v.Field)
|
||||
}
|
||||
|
||||
})
|
||||
t.Run("With unknown tagged field", func(t *testing.T) {
|
||||
y := []byte(`unknown: "hello"`)
|
||||
v := WithTaggedField{}
|
||||
err := Unmarshal(y, &v, DisallowUnknownFields)
|
||||
if err == nil {
|
||||
t.Errorf("want error because of unknown field, got <nil>: v=%#v", v)
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func exampleUnknown() {
|
||||
type WithTaggedField struct {
|
||||
Field string `json:"field"`
|
||||
}
|
||||
y := []byte(`unknown: "hello"`)
|
||||
v := WithTaggedField{}
|
||||
fmt.Printf("%v\n", Unmarshal(y, &v, DisallowUnknownFields))
|
||||
// Ouptut:
|
||||
// unmarshaling JSON: while decoding JSON: json: unknown field "unknown"
|
||||
}
|
423
vendor/sigs.k8s.io/yaml/yaml_test.go
generated
vendored
423
vendor/sigs.k8s.io/yaml/yaml_test.go
generated
vendored
@ -1,423 +0,0 @@
|
||||
package yaml
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type MarshalTest struct {
|
||||
A string
|
||||
B int64
|
||||
// Would like to test float64, but it's not supported in go-yaml.
|
||||
// (See https://github.com/go-yaml/yaml/issues/83.)
|
||||
C float32
|
||||
}
|
||||
|
||||
func TestMarshal(t *testing.T) {
|
||||
f32String := strconv.FormatFloat(math.MaxFloat32, 'g', -1, 32)
|
||||
s := MarshalTest{"a", math.MaxInt64, math.MaxFloat32}
|
||||
e := []byte(fmt.Sprintf("A: a\nB: %d\nC: %s\n", math.MaxInt64, f32String))
|
||||
|
||||
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))
|
||||
}
|
||||
}
|
||||
|
||||
type UnmarshalString struct {
|
||||
A string
|
||||
True string
|
||||
}
|
||||
|
||||
type UnmarshalStringMap struct {
|
||||
A map[string]string
|
||||
}
|
||||
|
||||
type UnmarshalNestedString struct {
|
||||
A NestedString
|
||||
}
|
||||
|
||||
type NestedString struct {
|
||||
A string
|
||||
}
|
||||
|
||||
type UnmarshalSlice struct {
|
||||
A []NestedSlice
|
||||
}
|
||||
|
||||
type NestedSlice struct {
|
||||
B string
|
||||
C *string
|
||||
}
|
||||
|
||||
func TestUnmarshal(t *testing.T) {
|
||||
y := []byte("a: 1")
|
||||
s1 := UnmarshalString{}
|
||||
e1 := UnmarshalString{A: "1"}
|
||||
unmarshal(t, y, &s1, &e1)
|
||||
|
||||
y = []byte("a: true")
|
||||
s1 = UnmarshalString{}
|
||||
e1 = UnmarshalString{A: "true"}
|
||||
unmarshal(t, y, &s1, &e1)
|
||||
|
||||
y = []byte("true: 1")
|
||||
s1 = UnmarshalString{}
|
||||
e1 = UnmarshalString{True: "1"}
|
||||
unmarshal(t, y, &s1, &e1)
|
||||
|
||||
y = []byte("a:\n a: 1")
|
||||
s2 := UnmarshalNestedString{}
|
||||
e2 := UnmarshalNestedString{NestedString{"1"}}
|
||||
unmarshal(t, y, &s2, &e2)
|
||||
|
||||
y = []byte("a:\n - b: abc\n c: def\n - b: 123\n c: 456\n")
|
||||
s3 := UnmarshalSlice{}
|
||||
e3 := UnmarshalSlice{[]NestedSlice{NestedSlice{"abc", strPtr("def")}, NestedSlice{"123", strPtr("456")}}}
|
||||
unmarshal(t, y, &s3, &e3)
|
||||
|
||||
y = []byte("a:\n b: 1")
|
||||
s4 := UnmarshalStringMap{}
|
||||
e4 := UnmarshalStringMap{map[string]string{"b": "1"}}
|
||||
unmarshal(t, y, &s4, &e4)
|
||||
|
||||
y = []byte(`
|
||||
a:
|
||||
name: TestA
|
||||
b:
|
||||
name: TestB
|
||||
`)
|
||||
type NamedThing struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
s5 := map[string]*NamedThing{}
|
||||
e5 := map[string]*NamedThing{
|
||||
"a": &NamedThing{Name: "TestA"},
|
||||
"b": &NamedThing{Name: "TestB"},
|
||||
}
|
||||
unmarshal(t, y, &s5, &e5)
|
||||
}
|
||||
|
||||
func unmarshal(t *testing.T, y []byte, s, e interface{}, opts ...JSONOpt) {
|
||||
err := Unmarshal(y, s, opts...)
|
||||
if err != nil {
|
||||
t.Errorf("error unmarshaling YAML: %v", err)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(s, e) {
|
||||
t.Errorf("unmarshal YAML was unsuccessful, expected: %+#v, got: %+#v",
|
||||
e, s)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnmarshalStrict(t *testing.T) {
|
||||
y := []byte("a: 1")
|
||||
s1 := UnmarshalString{}
|
||||
e1 := UnmarshalString{A: "1"}
|
||||
unmarshalStrict(t, y, &s1, &e1)
|
||||
|
||||
y = []byte("a: true")
|
||||
s1 = UnmarshalString{}
|
||||
e1 = UnmarshalString{A: "true"}
|
||||
unmarshalStrict(t, y, &s1, &e1)
|
||||
|
||||
y = []byte("true: 1")
|
||||
s1 = UnmarshalString{}
|
||||
e1 = UnmarshalString{True: "1"}
|
||||
unmarshalStrict(t, y, &s1, &e1)
|
||||
|
||||
y = []byte("a:\n a: 1")
|
||||
s2 := UnmarshalNestedString{}
|
||||
e2 := UnmarshalNestedString{NestedString{"1"}}
|
||||
unmarshalStrict(t, y, &s2, &e2)
|
||||
|
||||
y = []byte("a:\n - b: abc\n c: def\n - b: 123\n c: 456\n")
|
||||
s3 := UnmarshalSlice{}
|
||||
e3 := UnmarshalSlice{[]NestedSlice{NestedSlice{"abc", strPtr("def")}, NestedSlice{"123", strPtr("456")}}}
|
||||
unmarshalStrict(t, y, &s3, &e3)
|
||||
|
||||
y = []byte("a:\n b: 1")
|
||||
s4 := UnmarshalStringMap{}
|
||||
e4 := UnmarshalStringMap{map[string]string{"b": "1"}}
|
||||
unmarshalStrict(t, y, &s4, &e4)
|
||||
|
||||
y = []byte(`
|
||||
a:
|
||||
name: TestA
|
||||
b:
|
||||
name: TestB
|
||||
`)
|
||||
type NamedThing struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
s5 := map[string]*NamedThing{}
|
||||
e5 := map[string]*NamedThing{
|
||||
"a": &NamedThing{Name: "TestA"},
|
||||
"b": &NamedThing{Name: "TestB"},
|
||||
}
|
||||
unmarshal(t, y, &s5, &e5)
|
||||
|
||||
// When using not-so-strict unmarshal, we should
|
||||
// be picking up the ID-1 as the value in the "id" field
|
||||
y = []byte(`
|
||||
a:
|
||||
name: TestA
|
||||
id: ID-A
|
||||
id: ID-1
|
||||
`)
|
||||
type NamedThing2 struct {
|
||||
Name string `json:"name"`
|
||||
ID string `json:"id"`
|
||||
}
|
||||
s6 := map[string]*NamedThing2{}
|
||||
e6 := map[string]*NamedThing2{
|
||||
"a": {Name: "TestA", ID: "ID-1"},
|
||||
}
|
||||
unmarshal(t, y, &s6, &e6)
|
||||
}
|
||||
|
||||
func TestUnmarshalStrictFails(t *testing.T) {
|
||||
y := []byte("a: true\na: false")
|
||||
s1 := UnmarshalString{}
|
||||
unmarshalStrictFail(t, y, &s1)
|
||||
|
||||
y = []byte("a:\n - b: abc\n c: 32\n b: 123")
|
||||
s2 := UnmarshalSlice{}
|
||||
unmarshalStrictFail(t, y, &s2)
|
||||
|
||||
y = []byte("a:\n b: 1\n c: 3")
|
||||
s3 := UnmarshalStringMap{}
|
||||
unmarshalStrictFail(t, y, &s3)
|
||||
|
||||
type NamedThing struct {
|
||||
Name string `json:"name"`
|
||||
ID string `json:"id"`
|
||||
}
|
||||
// When using strict unmarshal, we should see
|
||||
// the unmarshal fail if there are multiple keys
|
||||
y = []byte(`
|
||||
a:
|
||||
name: TestA
|
||||
id: ID-A
|
||||
id: ID-1
|
||||
`)
|
||||
s4 := NamedThing{}
|
||||
unmarshalStrictFail(t, y, &s4)
|
||||
|
||||
// Strict unmarshal should fail for unknown fields
|
||||
y = []byte(`
|
||||
name: TestB
|
||||
id: ID-B
|
||||
unknown: Some-Value
|
||||
`)
|
||||
s5 := NamedThing{}
|
||||
unmarshalStrictFail(t, y, &s5)
|
||||
}
|
||||
|
||||
func unmarshalStrict(t *testing.T, y []byte, s, e interface{}, opts ...JSONOpt) {
|
||||
err := UnmarshalStrict(y, s, opts...)
|
||||
if err != nil {
|
||||
t.Errorf("error unmarshaling YAML: %v", err)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(s, e) {
|
||||
t.Errorf("unmarshal YAML was unsuccessful, expected: %+#v, got: %+#v",
|
||||
e, s)
|
||||
}
|
||||
}
|
||||
|
||||
func unmarshalStrictFail(t *testing.T, y []byte, s interface{}, opts ...JSONOpt) {
|
||||
err := UnmarshalStrict(y, s, opts...)
|
||||
if err == nil {
|
||||
t.Errorf("error unmarshaling YAML: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
type Case struct {
|
||||
input string
|
||||
output string
|
||||
// By default we test that reversing the output == input. But if there is a
|
||||
// difference in the reversed output, you can optionally specify it here.
|
||||
reverse *string
|
||||
}
|
||||
|
||||
type RunType int
|
||||
|
||||
const (
|
||||
RunTypeJSONToYAML RunType = iota
|
||||
RunTypeYAMLToJSON
|
||||
)
|
||||
|
||||
func TestJSONToYAML(t *testing.T) {
|
||||
cases := []Case{
|
||||
{
|
||||
`{"t":"a"}`,
|
||||
"t: a\n",
|
||||
nil,
|
||||
}, {
|
||||
`{"t":null}`,
|
||||
"t: null\n",
|
||||
nil,
|
||||
},
|
||||
}
|
||||
|
||||
runCases(t, RunTypeJSONToYAML, cases)
|
||||
}
|
||||
|
||||
func TestYAMLToJSON(t *testing.T) {
|
||||
cases := []Case{
|
||||
{
|
||||
"t: a\n",
|
||||
`{"t":"a"}`,
|
||||
nil,
|
||||
}, {
|
||||
"t: \n",
|
||||
`{"t":null}`,
|
||||
strPtr("t: null\n"),
|
||||
}, {
|
||||
"t: null\n",
|
||||
`{"t":null}`,
|
||||
nil,
|
||||
}, {
|
||||
"1: a\n",
|
||||
`{"1":"a"}`,
|
||||
strPtr("\"1\": a\n"),
|
||||
}, {
|
||||
"1000000000000000000000000000000000000: a\n",
|
||||
`{"1e+36":"a"}`,
|
||||
strPtr("\"1e+36\": a\n"),
|
||||
}, {
|
||||
"1e+36: a\n",
|
||||
`{"1e+36":"a"}`,
|
||||
strPtr("\"1e+36\": a\n"),
|
||||
}, {
|
||||
"\"1e+36\": a\n",
|
||||
`{"1e+36":"a"}`,
|
||||
nil,
|
||||
}, {
|
||||
"\"1.2\": a\n",
|
||||
`{"1.2":"a"}`,
|
||||
nil,
|
||||
}, {
|
||||
"- t: a\n",
|
||||
`[{"t":"a"}]`,
|
||||
nil,
|
||||
}, {
|
||||
"- t: a\n" +
|
||||
"- t:\n" +
|
||||
" b: 1\n" +
|
||||
" c: 2\n",
|
||||
`[{"t":"a"},{"t":{"b":1,"c":2}}]`,
|
||||
nil,
|
||||
}, {
|
||||
`[{t: a}, {t: {b: 1, c: 2}}]`,
|
||||
`[{"t":"a"},{"t":{"b":1,"c":2}}]`,
|
||||
strPtr("- t: a\n" +
|
||||
"- t:\n" +
|
||||
" b: 1\n" +
|
||||
" c: 2\n"),
|
||||
}, {
|
||||
"- t: \n",
|
||||
`[{"t":null}]`,
|
||||
strPtr("- t: null\n"),
|
||||
}, {
|
||||
"- t: null\n",
|
||||
`[{"t":null}]`,
|
||||
nil,
|
||||
},
|
||||
}
|
||||
|
||||
// Cases that should produce errors.
|
||||
_ = []Case{
|
||||
{
|
||||
"~: a",
|
||||
`{"null":"a"}`,
|
||||
nil,
|
||||
}, {
|
||||
"a: !!binary gIGC\n",
|
||||
"{\"a\":\"\x80\x81\x82\"}",
|
||||
nil,
|
||||
},
|
||||
}
|
||||
|
||||
runCases(t, RunTypeYAMLToJSON, cases)
|
||||
}
|
||||
|
||||
func runCases(t *testing.T, runType RunType, cases []Case) {
|
||||
var f func([]byte) ([]byte, error)
|
||||
var invF func([]byte) ([]byte, error)
|
||||
var msg string
|
||||
var invMsg string
|
||||
if runType == RunTypeJSONToYAML {
|
||||
f = JSONToYAML
|
||||
invF = YAMLToJSON
|
||||
msg = "JSON to YAML"
|
||||
invMsg = "YAML back to JSON"
|
||||
} else {
|
||||
f = YAMLToJSON
|
||||
invF = JSONToYAML
|
||||
msg = "YAML to JSON"
|
||||
invMsg = "JSON back to YAML"
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
// Convert the string.
|
||||
t.Logf("converting %s\n", c.input)
|
||||
output, err := f([]byte(c.input))
|
||||
if err != nil {
|
||||
t.Errorf("Failed to convert %s, input: `%s`, err: %v", msg, c.input, err)
|
||||
}
|
||||
|
||||
// Check it against the expected output.
|
||||
if string(output) != c.output {
|
||||
t.Errorf("Failed to convert %s, input: `%s`, expected `%s`, got `%s`",
|
||||
msg, c.input, c.output, string(output))
|
||||
}
|
||||
|
||||
// Set the string that we will compare the reversed output to.
|
||||
reverse := c.input
|
||||
// If a special reverse string was specified, use that instead.
|
||||
if c.reverse != nil {
|
||||
reverse = *c.reverse
|
||||
}
|
||||
|
||||
// Reverse the output.
|
||||
input, err := invF(output)
|
||||
if err != nil {
|
||||
t.Errorf("Failed to convert %s, input: `%s`, err: %v", invMsg, string(output), err)
|
||||
}
|
||||
|
||||
// Check the reverse is equal to the input (or to *c.reverse).
|
||||
if string(input) != reverse {
|
||||
t.Errorf("Failed to convert %s, input: `%s`, expected `%s`, got `%s`",
|
||||
invMsg, string(output), reverse, string(input))
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// To be able to easily fill in the *Case.reverse string above.
|
||||
func strPtr(s string) *string {
|
||||
return &s
|
||||
}
|
||||
|
||||
func TestYAMLToJSONStrict(t *testing.T) {
|
||||
const data = `
|
||||
foo: bar
|
||||
foo: baz
|
||||
`
|
||||
if _, err := YAMLToJSON([]byte(data)); err != nil {
|
||||
t.Error("expected YAMLtoJSON to pass on duplicate field names")
|
||||
}
|
||||
if _, err := YAMLToJSONStrict([]byte(data)); err == nil {
|
||||
t.Error("expected YAMLtoJSONStrict to fail on duplicate field names")
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user