Giter VIP home page Giter VIP logo

yaml's Introduction

YAML marshaling and unmarshaling support for Go

Build Status

Introduction

A wrapper around go-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.

Compatibility

This package uses go-yaml and therefore supports everything go-yaml supports.

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:

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:

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"}
	*/
}

yaml's People

Contributors

aggrolite avatar alexagriffith avatar bradfitz avatar creachadair avatar davidxia avatar diekmann avatar fabianofranz avatar filmil avatar greensnark avatar hw-qiaolei avatar joe2far avatar kishaningithub avatar philoserf avatar stapelberg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

yaml's Issues

How to decode yaml with ---

kind: Namespace
metadata:
   name: test
---
name: aaa
type Namespace struct {
    Kind     string `json:"kind"`
    Metadata struct {
        Name string `json:"name"`
    } `json:"metadata"`
}

type Name struct {
    Name string `json:"name"`
}

Can't decode the name struct, How?

bug

I found a bug, when I test jsonToyaml. when value is a number, yaml.JSONToYAML() function will return "".

func Test_JsonToYaml(t *testing.T) {
var data = { "deployment": { "kind": "Deployment", "apiVersion": "extensions/v1beta1", "metadata": { "name": "app-0807-002", "annotations": { "cpu": "0.5", "memory": "256" }, "namespace": "030bb124aef6409daec0d026f21e8dea", "labels": { "app": "app-0807-002", "ns": "030bb124aef6409daec0d026f21e8dea" } }, "spec": { "replicas": 1, "selector": { "matchLabels": { "app": "app-0807-002", "ns": "030bb124aef6409daec0d026f21e8dea" } }, "template": { "metadata": { "labels": { "app": "app-0807-002", "ns": "030bb124aef6409daec0d026f21e8dea" } }, "spec": { "containers": [ { "image": "registry.paas/library/nginx:latest", "imagePullPolicy": "IfNotPresent", "name": "nginx", "resources": { "limits": { "cpu": "500m", "memory": "256Mi" } } } ] } } } } }
yaml, err := yaml.JSONToYAML([]byte(data))
if err != nil {
fmt.Printf("err: %v\n", err)
return
}
fmt.Println(string(yaml))
}

response:
deployment:
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
annotations:
cpu: "0.5"
memory: "256"

labels:
app: app-0807-002
ns: 030bb124aef6409daec0d026f21e8dea
name: app-0807-002
namespace: 030bb124aef6409daec0d026f21e8dea
spec:
replicas: 1
selector:
matchLabels:
app: app-0807-002
ns: 030bb124aef6409daec0d026f21e8dea
template:
metadata:
labels:
app: app-0807-002
ns: 030bb124aef6409daec0d026f21e8dea
spec:
containers:
- image: registry.paas/library/nginx:latest
imagePullPolicy: IfNotPresent
name: nginx
resources:
limits:
cpu: 500m
memory: 256Mi

Marshalled data fails validation with several online validators

When I have a long string value that also contains a single quote, the marshalled yaml fails validation with a number of online yaml validators.

e.g. the third example generated by this code fails in :

https://codebeautify.org/yaml-validator
https://onlineyamltools.com/validate-yaml

package main

import (
	"fmt"
	"github.com/ghodss/yaml"
)

type X struct {
	Y string
}

func main() {

	// single-quotes are ok
	b, _ := yaml.Marshal(X{Y: `'`})
	fmt.Println(string(b))

	// new lines with single quotes are ok
	b, _ = yaml.Marshal(X{Y: `'\n`})
	fmt.Println(string(b))

	// single quotes with wrapped lines causes failed validation
	b, _ = yaml.Marshal(X{Y: `' 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789`})
	fmt.Println(string(b))

The above code outputs :

"Y": ''''

"Y": '''\n'

"Y": ''' 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789
  123456789'

I appreciate that the yaml marshaller outputs valid yaml, but the fact that it fails in some of the most popular online validators is a bit of a frustration.

Is there anything that could be done to tackle this issue?

Int overflow on 32 bits arches

Golang 1.12.6 on i686 and armv7:

Testing    in: /builddir/build/BUILD/yaml-1.0.0/_build/src
         PATH: /builddir/build/BUILD/yaml-1.0.0/_build/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/sbin
       GOPATH: /builddir/build/BUILD/yaml-1.0.0/_build:/usr/share/gocode
  GO111MODULE: off
      command: go test -buildmode pie -compiler gc -ldflags "-X github.com/ghodss/yaml/version=1.0.0 -extldflags '-Wl,-z,relro -Wl,--as-needed  -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld '"
      testing: github.com/ghodss/yaml
github.com/ghodss/yaml
FAIL	github.com/ghodss/yaml [build failed]
BUILDSTDERR: # github.com/ghodss/yaml [github.com/ghodss/yaml.test]
BUILDSTDERR: ./yaml_test.go:22:26: constant 9223372036854775807 overflows int

struct with field name starting with `N` fails to read data

Here is a code snippet that I am running:

$ cat non.go 
package main

import (
        "fmt"
        "log"

        "github.com/ghodss/yaml"
)

type C struct {
        M int `yaml:"M"`
        N int `yaml:"N"`
}

func main() {
        d := []byte(`
M: 1
N: 2
`)

        var c C
        err := yaml.Unmarshal(d, &c)
        if err != nil {
                log.Fatalln(err)
        }
        fmt.Printf("%#v\n", c)

}

Once I run it:

$ go run non.go 
main.C{M:1, N:0}

Travis doesn't allow testing with go 1.10

Time passes and go 1.10 is now the "official" current go version to use.

Sadly, .travis.yaml considers go 1.10 to be go 1.1, so doesn't build and test properly.

See here:
https://travis-ci.org/ghodss/yaml/builds/363368191?utm_source=github_status&utm_medium=notification

The .travis.yml file used is this one:
https://github.com/ghodss/yaml/pull/27/files#diff-354f30a63fb0907d4ad57269548329e3

note how tests are performed for go versions 1.3, 1.4 and 1.1.

I would expect 1.3, 1.4 and 1.10 instead.

⚡ Is anyone maintaining the project ?

My apologies but I'd like to know if someone takes care of this project. ghodss/yaml is apparently used in many projects and would deserve at least minimal maintenance (at least for future CVE dependabot reporting).

thx

cc @ghodss

Difference with go-yaml for numeric parsing

I'm not sure it's really an issue, but I noticed a difference between the ways numeric parsing is done in this library and the one it wraps:

package main

import (
	"fmt"
	"reflect"

	"github.com/ghodss/yaml"
	yamlv2 "gopkg.in/yaml.v2"
)

func print(m map[string]interface{}) {
	for k, v := range m {
		fmt.Printf("%s: %v (%v)\n", k, v, reflect.TypeOf(v))
	}
}

func main() {

	s := []byte(`
aaa: 123
bbb: 123.4
`)

	m := map[string]interface{}{}
	yamlv2.Unmarshal(s, &m)

	m2 := map[string]interface{}{}
	yaml.Unmarshal(s, &m2)

	fmt.Println("gopkg.in/yaml.v2.Unmarshal:")
	print(m)
	fmt.Println("-----------------------")
	fmt.Println("ghodss/yaml.Unmarshal:")
	print(m2)
}

which will print:

gopkg.in/yaml.v2.Unmarshal:
aaa: 123 (int)
bbb: 123.4 (float64)
-----------------------
ghodss/yaml.Unmarshal:
aaa: 123 (float64)
bbb: 123.4 (float64)

I understand this is probably due to the fact that there is no integer type in JavaScript, but in my particular case, this discrepancy is a problem I have to deal with.

add vendoring

The code uses a library:

- package: gopkg.in/yaml.v2
  repo: https://github.com/go-yaml/yaml.git
  version: v2.2.1

which is not vendored in. This means if you use this library and do full vendoring, this library is missing.

Unmarshaling is incorrect, particularly around numbers.

Minimal example

https://play.golang.org/p/jZK6kGxo_vB

for '--- 1' og vs output: (int)1!=(float64)1
for '--- 031' og vs output: (int)25!=(float64)25
for '--- 031' ogoutstring vs outstring: 031!=25
for '--- 031' ogoutnum vs outnum: 031!=25
for '--- {"hi":031}' og vs output: (map[interface {}]interface {})map[hi:25]!=(map[string]interface {})map[hi:25]
err: error unmarshaling JSON: json: cannot unmarshal object into Go value of type string
err: error unmarshaling JSON: json: cannot unmarshal object into Go value of type json.Number

I'm ok with many of these. What I really want though is for json.Number to be decoded without 0 truncation and octal conversion as the OG yaml library does.

Support YAML to pretty JSON

Thanks for this library!

Right now YAMLToJSON prints the following YAML

t: a

as {"t":"a"}.

Could you add support for printing the YAML as

{
  "t": "a"
}

Basically add a way to use json.MarshalIndent here.

build failed

yaml.v2 --> "gopkg.in/yaml.v2" need to remove,because not used in this file.

getting error with docker build

this is what I get, got it 2x, so not sure what it is about yet

1057.1  github.com/ghodss/[email protected]: reading github.com/ghodss/yaml/go.mod at revision v1.0.0: git ls-remote -q origin in /go/pkg/mod/cache/vcs/5c75ad62eb9c289b6ed86c76998b4ab8c8545a841036e879d703a2bbc5fcfcea: exit status 128:
1057.1  fatal: unable to access 'https://github.com/ghodss/yaml/': GnuTLS recv error (-110): The TLS connection was non-properly terminated.
------
Dockerfile.local.df:13
--------------------
  11 |     COPY go.mod go.in.mod
  12 |     RUN grep -v "^replace" "go.in.mod" > "go.mod"  # remove replace lines from go.mod
  13 | >>> RUN go mod download -x
  14 |     RUN go mod tidy
  15 |     
--------------------
ERROR: failed to solve: process "/bin/sh -c go mod download -x" did not complete successfully: exit code: 1

yamltojson got wrong output format

I have this yaml

- repo: mhinz/vim-startify
  hook_add: source  $VIM_PATH/modules/module-startify.vim
  hook_post_source: |
      function! StartifyEntryFormat()
        return 'WebDevIconsGetFileTypeSymbol(absolute_path) ." ". entry_path'
      endfunction
- repo: neoclide/coc.nvim
  merge: 0
  build: 'yarn install --frozen-lockfile'
  hook_add: source  $VIM_PATH/modules/module-coc.vim

use yamltojson function, i got this output format

[{"hook_add":"source  $VIM_PATH/modules/module-startify.vim","hook_post_source":"function! StartifyEntryFormat()\n  return 'WebDevIcons
GetFileTypeSymbol(absolute_path) .\" \". entry_path'\nendfunction\n","repo":"mhinz/vim-startify"},{"build":"yarn install --frozen-lockf
ile","hook_add":"source  $VIM_PATH/modules/module-coc.vim","merge":0,"repo":"neoclide/coc.nvim"}]

it should be

[{"repo":"mhinz/vim-startify","hook_add":"source  $VIM_PATH/modules/module-startify.vim",
"hook_post_source ":"  function! StartifyEntryFormat()\n return'WebDevIconsGetFileTypeSymbol(absolute_path) ." ". entry_path'\n endfunction\n"},
{"repo":"neoclide/coc.nvim","merge":0,"build":"yarn install --frozen-lockfile","hook_add":
"$VIM_PATH/modules/module-coc.vim"}]

Support NewDecoder?

The package claims to support "everything go-yaml supports" but it doesn't seem to provide a streaming interface. Is this planned? Is there a workaround (other than just reading stream to end) I'm not seeing?

Japanese caractere

Hi,

My Yaml is containing some japanese caractere.

When I try to convert to Json I got this error

err: error converting YAML to JSON: yaml: [while parsing a quoted scalar] did not find expected hexdecimal number at line 3, column 19
---
description: "テ Testing"

This failed, if you have any hints please let me know.

Thanks

export convertToJSONableObject

I have a map[interface{}]interface{} object (or an object the contains it. originated from a yaml parser), and I need to marshal this object into json. I don't think this is the purpose of this library, but it would be super helpful if you just exported the function convertToJSONableObject and allowed people to use it in cases like this.

Don't Unmarshal field with underline

package main

import (
	"fmt"
	"github.com/ghodss/yaml"
)

type MyStruct struct {
	Exchange   string `yaml:"exchange"`
	RoutingKey string `yaml:"routing_key"`
	Role       string `yaml:"role"`
}
func main() {
	var mystr MyStruct
	yamlstr := `
exchange: "data1"
routing_key: "data2"
role: data3
`
	yaml.Unmarshal([]byte(yamlstr), &mystr)
	fmt.Printf("%+v\n", mystr)
}

out:
{Exchange:data1 RoutingKey: Role:data3}

Add support for UnmarshalStrict

Hi,

I'm wondering if there is a plan to add support for the UnmarshalStrict function of go-yaml?
It's not in the current release, are there any plans on releasing soon?

Please tag a new release after #49

#49 adds basic support for Go modules, but the latest current release tag (v1.0.0) is dated March 2017. Could you please find a suitable point at or after #49 to add a newer tag? (I think v1.1.0 is a reasonable choice, since there don't seem to be any API changes, but I didn't check exhaustively).

Newline and carriage return control characters characters are not escaped when converting YAML to JSON

Currently, control characters such as newlines and carriage returns are not escaped and passed through.

e.g.
When one inputs the following byte data into YAMLToJSON()

{"name": "-----BEGIN CERTIFICATE-----
MIIC3aaaaaaaDAwMQswCQYDVQQGxxxxxxxsjlsdkfjsdlkfjsdlkjsdvlskdEwJB
VTENMAsGA1UECwwEZGF3dTESMBAGA1UEAwwJKi5kYXd1LmlvMB4XDTE5MDYwNjAz
MTEzNVoXDTIwMDYwNTAzMTEzNVowMDELMAkGA1UEBhMCQVUxDTALBgNVBAsMBGRh
d3UxEjAQBgNVBAMMCSouZGF3dS5pbzCCASIwDQYJKoZIhvcNAQEBBQADggEPADCC
AQoCggEBANBuhemwaaaaaaaaaaaaaaaaaaajCj+Yo20QhidabJ8SgrrSFJEz/btm
UQATAqLXOczhIqxJzIOIhW2klI4JQTkR96FZkqCOktKtQNjXrL+5L2Jb7ouHLak7
8j3HjhCmmfgh7MZ1wq7O+MUyDCEHI5ixd8eC8TEr1XgCJyhqlYestsWhvGUP8gth
DGgFHMBOx033RvVkTbJcagSspMsQZ/sCAwEAATANBgkqhkiG9w0BAQsFAAOCAQEA
bocw2bBJfb2uvK0Ej5PpTQPBnynVsJxBvCJlSAqA7Mk9NieRSbdj7Ul+nRViSwER
41bTogRS4mhl3qaLGThB4DlsS/wqRp5ocTqBMWkCssEdCWwkJVWZxPQ42Kh5uXYY
9JsEudPLv0hh+b5Oyoi7f2asljasflkasjflkGSqEJH2xauwfZnKTjeb/tmtqtoi
coL/1owTdHspg59SQYUBJUPlc0Ids6OolsmOnO9icwJ7c1AIkpKps4mL9XgxCtD7
BVRmQ5RwTOlBLAmpU7R1gg==
-----END CERTIFICATE-----", "age": 30}

the byte output is the following

{"age":30,"name":"-----BEGIN CERTIFICATE----- MIIC3aaaaaaaDAwMQswCQYDVQQGxxxxxxxsjlsdkfjsdlkfjsdlkjsdvlskdEwJB VTENMAsGA1UECwwEZGF3dTESMBAGA1UEAwwJKi5kYXd1LmlvMB4XDTE5MDYwNjAz MTEzNVoXDTIwMDYwNTAzMTEzNVowMDELMAkGA1UEBhMCQVUxDTALBgNVBAsMBGRh d3UxEjAQBgNVBAMMCSouZGF3dS5pbzCCASIwDQYJKoZIhvcNAQEBBQADggEPADCC AQoCggEBANBuhemwaaaaaaaaaaaaaaaaaaajCj+Yo20QhidabJ8SgrrSFJEz/btm UQATAqLXOczhIqxJzIOIhW2klI4JQTkR96FZkqCOktKtQNjXrL+5L2Jb7ouHLak7 8j3HjhCmmfgh7MZ1wq7O+MUyDCEHI5ixd8eC8TEr1XgCJyhqlYestsWhvGUP8gth DGgFHMBOx033RvVkTbJcagSspMsQZ/sCAwEAATANBgkqhkiG9w0BAQsFAAOCAQEA bocw2bBJfb2uvK0Ej5PpTQPBnynVsJxBvCJlSAqA7Mk9NieRSbdj7Ul+nRViSwER 41bTogRS4mhl3qaLGThB4DlsS/wqRp5ocTqBMWkCssEdCWwkJVWZxPQ42Kh5uXYY 9JsEudPLv0hh+b5Oyoi7f2asljasflkasjflkGSqEJH2xauwfZnKTjeb/tmtqtoi coL/1owTdHspg59SQYUBJUPlc0Ids6OolsmOnO9icwJ7c1AIkpKps4mL9XgxCtD7 BVRmQ5RwTOlBLAmpU7R1gg== -----END CERTIFICATE-----"}

An example of this can be found at this playground
https://play.golang.org/p/1SFyCUgmMBN

It would be nice if newline and carriage return characters can be escaped after running YAMLToJSON()
i.e.,

{"age": 30, "name": "-----BEGIN CERTIFICATE-----\nMIIC3aaaaaaaDAwMQswCQYDVQQGxxxxxxxsjlsdkfjsdlkfjsdlkjsdvlskdEwJB\nVTENMAsGA1UECwwEZGF3dTESMBAGA1UEAwwJKi5kYXd1LmlvMB4XDTE5MDYwNjAz\nMTEzNVoXDTIwMDYwNTAzMTEzNVowMDELMAkGA1UEBhMCQVUxDTALBgNVBAsMBGRh\nd3UxEjAQBgNVBAMMCSouZGF3dS5pbzCCASIwDQYJKoZIhvcNAQEBBQADggEPADCC\nAQoCggEBANBuhemwaaaaaaaaaaaaaaaaaaajCj+Yo20QhidabJ8SgrrSFJEz/btm\nUQATAqLXOczhIqxJzIOIhW2klI4JQTkR96FZkqCOktKtQNjXrL+5L2Jb7ouHLak7\n8j3HjhCmmfgh7MZ1wq7O+MUyDCEHI5ixd8eC8TEr1XgCJyhqlYestsWhvGUP8gth\nDGgFHMBOx033RvVkTbJcagSspMsQZ/sCAwEAATANBgkqhkiG9w0BAQsFAAOCAQEA\nbocw2bBJfb2uvK0Ej5PpTQPBnynVsJxBvCJlSAqA7Mk9NieRSbdj7Ul+nRViSwER\n41bTogRS4mhl3qaLGThB4DlsS/wqRp5ocTqBMWkCssEdCWwkJVWZxPQ42Kh5uXYY\n9JsEudPLv0hh+b5Oyoi7f2asljasflkasjflkGSqEJH2xauwfZnKTjeb/tmtqtoi\ncoL/1owTdHspg59SQYUBJUPlc0Ids6OolsmOnO9icwJ7c1AIkpKps4mL9XgxCtD7\nBVRmQ5RwTOlBLAmpU7R1gg==\n-----END CERTIFICATE-----"}

Panic when trying to unmarshal specific yaml

This is a pretty bizarre one. When trying to unmarshal this yaml:

Port: 7746
Processes:
    Something:
        Command: some command

I get this panic:

panic: reflect: reflect.Value.Set using unaddressable value

goroutine 1 [running]:
panic(0x3cdb80, 0xc820132680)
    /usr/local/Cellar/go/1.6.2/libexec/src/runtime/panic.go:481 +0x3e6
reflect.flag.mustBeAssignable(0x16)
    /usr/local/Cellar/go/1.6.2/libexec/src/reflect/value.go:229 +0x221
reflect.Value.Set(0x3a98e0, 0x0, 0x16, 0x3a98e0, 0xc82016e2c0, 0x16)
    /usr/local/Cellar/go/1.6.2/libexec/src/reflect/value.go:1328 +0x25
dj/vendor/github.com/ghodss/yaml.indirect(0x3a98e0, 0x0, 0x16, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, ...)
    /Users/craig/go-clean/src/dj/vendor/github.com/ghodss/yaml/fields.go:48 +0x528
dj/vendor/github.com/ghodss/yaml.convertToJSONableObject(0x3ca880, 0xc820136630, 0xc8201346a0, 0x0, 0x0, 0x0, 0x0)
    /Users/craig/go-clean/src/dj/vendor/github.com/ghodss/yaml/yaml.go:106 +0xc9
dj/vendor/github.com/ghodss/yaml.convertToJSONableObject(0x3ca880, 0xc8201365d0, 0xc8200d7170, 0x0, 0x0, 0x0, 0x0)
    /Users/craig/go-clean/src/dj/vendor/github.com/ghodss/yaml/yaml.go:205 +0xf3a
dj/vendor/github.com/ghodss/yaml.convertToJSONableObject(0x3ca880, 0xc8201365a0, 0xc8200d7628, 0x0, 0x0, 0x0, 0x0)
    /Users/craig/go-clean/src/dj/vendor/github.com/ghodss/yaml/yaml.go:195 +0xb15
dj/vendor/github.com/ghodss/yaml.yamlToJSON(0xc820162000, 0x43, 0x243, 0xc8200d78a0, 0x0, 0x0, 0x0, 0x0, 0x0)
    /Users/craig/go-clean/src/dj/vendor/github.com/ghodss/yaml/yaml.go:89 +0x110
dj/vendor/github.com/ghodss/yaml.Unmarshal(0xc820162000, 0x43, 0x243, 0x3a9880, 0x78c480, 0x0, 0x0)
    /Users/craig/go-clean/src/dj/vendor/github.com/ghodss/yaml/yaml.go:32 +0xa8

Interestingly it seems like it might be something specific to the word Processes cause if I change that to Process or really any other word it works fine. I was previously using an older version of your library c3eb24a and it works fine there.

【bug】Capital change to lowercase

originalData = `env:
  CODE_SCAN:
    type: string
    value: True
    readonly: true`
json, err := yaml.YAMLToJSON([]byte(originalData))
if err != nil {
	fmt.Printf("failed: %+v", err)
}
fmt.Println(string(json))

when I use yaml.YAMLToJSON([]byte(CiPipeLine)), it print:
{"env":{"CODEDOG_CONCURRENT_SCAN":{"readonly":true,"type":"string","value":true}}}

🚚 New fork available at invopop/yaml

I've been using this package extensively over the last few years as it makes working with YAML in Go much easier. YAML is great, but it's flexibility also makes it hard to work with sometimes in Go; converting first to JSON makes a ton of sense for most use-cases. I was quite sad to see the number of unresolved issues and felt that the latest security scare (CVE-2022-28948) was enough to warrant a fork, so I did:

https://github.com/invopop/yaml

Most of the issues I see are related to the fact that the library is still using yaml.v2, so our first step was to upgrade to yaml.v3: invopop/yaml#2. It was more complex than expected due to the change in handling of keys in maps, but it's working now.

We also maintain the jsonschema package so are quite used to the messy reflection stuff that needs to happen behind the scenes.

I do hope that the original maintainers don't mind, and I'd like to take this opportunity to thank them for all their work! I hope we can continue to maintain our fork effectively, and welcome any contributions.

return yaml as same as json

Right now converting a JSON to YAML returns in sorted order. Can we generate the YAML as same as the JSON structure ?.

Main license

Hello,

Could you please help me with information regarding the main license? In the LICENSE file there are enlisted both a BSD 3-Clause license and the MIT License. Is this a case of dual licensing (and then we can choose one of them) or a case of multiple licensing (when both of them apply as the main license for this component)?

Thank you very much in advance!
Best regards,

Julia

unable to convert zero prepended integer from YAML file to YAML object using “yaml.Unmarshal”

Hello experts,

Would like to know how to convert zero prepended integer( i.e octal integer) number from YAML file to YAML Object.
Tried with yaml.v3 also but could see same results.

Any help would be appreciates

zero prepended integer examples:

01
001
00002 and so

Sample program
==============

package main

import "fmt"
import "gopkg.in/yaml.v2"

var data = a: 01 b: 1

func main() {
var obj interface{}
_ = yaml.Unmarshal([]byte(data), &obj)
fmt.Println("YAML data:", data)
fmt.Println("OBJ:", obj)
}_

O/p of above program:

===================
YAML data:
a: 01
b: 1
OBJ: map[a:1 b:1]

Unmarshal() is case insensitive to object attribute names

Hey folks,

I happened to bump upon a case where an object source yaml payload contains 2 distinct keys which are being parsed as the same key. Effectively, the only difference in the key names is the case spelling: attrFoo Vs attrfoo (see example).

My struct definition:

type Foo struct {
        Attr int32 `json:"attrFoo"`
}

The yaml payload is defined as:

const data = `
---
bar:
  attrFoo: 42

baz:
  attrfoo: 84

barbaz:
  attrFoo: 122
  attrfoo: 456
`

And the test program:

func main() {
        var foomap map[string]Foo
        err := yaml.Unmarshal([]byte(jsonData), &foomap)
        if err != nil {
                panic(err.Error())
        }
        fmt.Printf("foo map: %+v\n", foomap)
}

The output seems a bit odd:

foo map: map[bar:{Attr:42} barbaz:{Attr:456} baz:{Attr:84}]

Here is a full gist.

On the flip side yaml.v2 behaviour is exactly what I expect: it strips unknown attrfoo attribute:

foo map: map[bar:{Attr:42} barbaz:{Attr:122} baz:{Attr:0}]

Here is a yaml.v2 gist.

As yaml specification implies on key case sensitivity, from the user perspective all 3 objects in my example document look valid, but the parsing result is a bit surprising. Could you please give me an idea if this behaviour is expected? Thanks!

yaml.v3 support

I see that #53 was closed without merging at some point last year. I'm wondering what the current status of this is, and whether you'd accept PRs for additional work if necessary.

Support for jsonpb

Hi there!

We have a use case where we have configuration backed by protocol buffer definitions. Today, we use jsonpb to marshal/unmarshal as the std library's json en/decoder doesn't play nice with protocol buffers.

Can your library add support for the jsonpb codec? We'd love to be able to support yaml files for our users

"y" argument not parsed

After parsing the following yaml :

x: 10
y: 20

x is being extracted, but y is not. Changing 'y' to anything else works.

Reproducible sample:

package main

import (
    "fmt"
    "github.com/ghodss/yaml"
)

type Position struct {
    X int `json:"x"`
    Y int `json:"y"`
}

func main() {
    s := []byte(`
x: 10
y: 20
`)

    var p Position
    err := yaml.Unmarshal(s, &p)
    if err != nil {
        fmt.Printf("err: %v\n", err)
        return
    }
    fmt.Println(p) // Should be {10 20}, but prints {10 0}
}

Unmarshal does not work if a field name differs from related annotation name

Yaml

apiVersion: v1
kind: Secret
metadata:
  name: registrysecret
  annotations:
    "helm.sh/hook": pre-install
type: kubernetes.io/dockerconfigjson

Struct

type Template struct {
  Version string `yaml:"apiVersion,omitempty"`
  Kind    string `yaml:"kind,omitempty"`
}

After unmarshaling (yaml.Unmarshal) the field Version is empty.
If the field is renamed to ApiVersion unmarshal works properly.

Cast Variable Values As String

Not sure if this is the right place to report this or not, but I'm running into an issue in a downstream project. The exact error is:

json: cannot unmarshal number into Go value of type string

kubernetes/kubernetes#12889

And the source code

If you believe this is not the right place and you have a moment, please point me in the right direction.

Thanks much!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.