Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add ignore file and code optimization #82

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ Session.vim

# Go test binaries
*.test

.idea/
12 changes: 5 additions & 7 deletions fields.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2013 The Go Authors. All rights reserved.
// Package yaml Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package yaml
Expand Down Expand Up @@ -347,8 +347,9 @@ const (
// 4) simpleLetterEqualFold, no specials, no non-letters.
//
// The letters S and K are special because they map to 3 runes, not just 2:
// * S maps to s and to U+017F 'ſ' Latin small letter long s
// * k maps to K and to U+212A 'K' Kelvin sign
// - S maps to s and to U+017F 'ſ' Latin small letter long s
// - k maps to K and to U+212A 'K' Kelvin sign
//
// See http://play.golang.org/p/tTxjOc0OGo
//
// The returned function is specialized for matching against s and
Expand Down Expand Up @@ -419,10 +420,7 @@ func equalFoldRight(s, t []byte) bool {
t = t[size:]

}
if len(t) > 0 {
return false
}
return true
return len(t) <= 0
}

// asciiEqualFold is a specialization of bytes.EqualFold for use when
Expand Down
23 changes: 11 additions & 12 deletions yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
// JSON methods MarshalJSON and UnmarshalJSON unlike go-yaml.
//
// See also http://ghodss.com/2014/the-right-way-to-handle-yaml-in-golang
//
package yaml // import "github.com/ghodss/yaml"
package yaml // import "github.com/ghodss/yaml"

import (
"bytes"
Expand All @@ -21,7 +20,7 @@ import (
"gopkg.in/yaml.v2"
)

// Marshals the object into JSON then converts JSON to YAML and returns the
// 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)
Expand Down Expand Up @@ -83,7 +82,7 @@ func jsonUnmarshal(r io.Reader, o interface{}, opts ...JSONOpt) error {
return nil
}

// Convert JSON to YAML.
// JSONToYAML Convert JSON to YAML.
func JSONToYAML(j []byte) ([]byte, error) {
// Convert the JSON to an object.
var jsonObj interface{}
Expand All @@ -105,12 +104,12 @@ func JSONToYAML(j []byte) ([]byte, error) {
// passing JSON through this method should be a no-op.
//
// Things YAML can do that are not supported by JSON:
// * In YAML you can have binary and null keys in your maps. These are invalid
// in JSON. (int and float keys are converted to strings.)
// * Binary data in YAML with the !!binary tag is not supported. If you want to
// use binary data with this library, encode the data as base64 as usual but do
// not use the !!binary tag in your YAML. This will ensure the original base64
// encoded data makes it all the way through to the JSON.
// - In YAML you can have binary and null keys in your maps. These are invalid
// in JSON. (int and float keys are converted to strings.)
// - Binary data in YAML with the !!binary tag is not supported. If you want to
// use binary data with this library, encode the data as base64 as usual but do
// not use the !!binary tag in your YAML. This will ensure the original base64
// encoded data makes it all the way through to the JSON.
//
// For strict decoding of YAML, use YAMLToJSONStrict.
func YAMLToJSON(y []byte) ([]byte, error) {
Expand Down Expand Up @@ -153,7 +152,7 @@ func convertToJSONableObject(yamlObj interface{}, jsonTarget *reflect.Value) (in
// string.
if jsonTarget != nil {
ju, tu, pv := indirect(*jsonTarget, false)
// We have a JSON or Text Umarshaler at this level, so we can't be trying
// We have a JSON or Text Unmarshaler at this level, so we can't be trying
// to decode into a string.
if ju != nil || tu != nil {
jsonTarget = nil
Expand All @@ -165,7 +164,7 @@ func convertToJSONableObject(yamlObj interface{}, jsonTarget *reflect.Value) (in
// If yamlObj is a number or a boolean, check if jsonTarget is a string -
// if so, coerce. Else return normal.
// If yamlObj is a map or array, find the field that each key is
// unmarshaling to, and when you recurse pass the reflect.Value for that
// unmarshalling to, and when you recurse pass the reflect.Value for that
// field back into this function.
switch typedYAMLObj := yamlObj.(type) {
case map[interface{}]interface{}:
Expand Down
21 changes: 11 additions & 10 deletions yaml_go110_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build go1.10
// +build go1.10

package yaml
Expand Down Expand Up @@ -40,10 +41,10 @@ func TestUnmarshalWithTags(t *testing.T) {
// duplicate fields in the YAML input.
func TestUnmarshalStrictWithJSONOpts(t *testing.T) {
for _, tc := range []struct {
yaml []byte
opts []JSONOpt
want UnmarshalString
wantErr string
yaml []byte
opts []JSONOpt
want UnmarshalString
wantErr string
}{
{
// By default, unknown field is ignored.
Expand All @@ -52,9 +53,9 @@ func TestUnmarshalStrictWithJSONOpts(t *testing.T) {
},
{
// Unknown field produces an error with `DisallowUnknownFields` option.
yaml: []byte("a: 1\nunknownField: 2"),
opts: []JSONOpt{DisallowUnknownFields},
wantErr: `unknown field "unknownField"`,
yaml: []byte("a: 1\nunknownField: 2"),
opts: []JSONOpt{DisallowUnknownFields},
wantErr: `unknown field "unknownField"`,
},
} {
po := prettyFunctionName(tc.opts)
Expand All @@ -69,7 +70,7 @@ func TestUnmarshalStrictWithJSONOpts(t *testing.T) {
continue
}
// We expect that duplicate fields are discovered during JSON unmarshalling.
if want := "error unmarshaling JSON"; tc.wantErr != "" && !strings.Contains(err.Error(), want) {
if want := "error unmarshalling JSON"; tc.wantErr != "" && !strings.Contains(err.Error(), want) {
t.Errorf("UnmarshalStrict(%#q, &s, %#v) = %v; want err contains %#q", string(tc.yaml), po, err, want)
}
if tc.wantErr != "" && !strings.Contains(err.Error(), tc.wantErr) {
Expand All @@ -95,6 +96,6 @@ func ExampleUnknown() {
y := []byte(`unknown: "hello"`)
v := WithTaggedField{}
fmt.Printf("%v\n", Unmarshal(y, &v, DisallowUnknownFields))
// Ouptut:
// unmarshaling JSON: while decoding JSON: json: unknown field "unknown"
// Output:
// unmarshalling JSON: while decoding JSON: json: unknown field "unknown"
}