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

Marshal From yaml.Node gives invalid Yaml #454

Closed
will7200 opened this issue Apr 18, 2019 · 11 comments
Closed

Marshal From yaml.Node gives invalid Yaml #454

will7200 opened this issue Apr 18, 2019 · 11 comments

Comments

@will7200
Copy link

Summary

Reading a valid yaml spec into a yaml.Node and then Marshalling doesn't produce valid yaml.

Occurrence

Go version: go1.12.3 windows/amd64
Branch: v3
Commit: cdc409d

Example

package main

import (
    "fmt"
    "log"

    "gopkg.in/yaml.v3"
)

var (
    sourceYaml = `version: 1
type: verbose
kind : bfr

# my list of applications
applications:

#  First app
  - name: app1
    kind: nodejs
    path: app1
    exec:
      platforms: k8s
      builder: test
`
)

func main() {
    t := yaml.Node{}

    err := yaml.Unmarshal([]byte(sourceYaml), &t)
    if err != nil {
        log.Fatalf("error: %v", err)
    }

    b, err := yaml.Marshal(&t)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(string(b))
}

Produces the following yaml

version: 1
type: verbose
kind: bfr


# my list of applications
applications:
-   #  First app
name: app1
    kind: nodejs
    path: app1
    exec:
        platforms: k8s
        builder: test

Expected yaml

version: 1
type: verbose
kind: bfr


# my list of applications
applications:
-   #  First app
    name: app1
    kind: nodejs
    path: app1
    exec:
        platforms: k8s
        builder: test
@will7200 will7200 changed the title Remarshalling From yaml.Node gives invalid Yaml Marshal From yaml.Node gives invalid Yaml Apr 18, 2019
@niemeyer
Copy link
Contributor

I believe this has been fixed shortly after the release with fc85683.

Will close this, but please reopen if you can reproduce after ensuring your tree is up to date.

@will7200
Copy link
Author

Here is my go.mod

module retainyamlcommments

go 1.12

require (
	github.com/davecgh/go-spew v1.1.1 // indirect
	gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
	gopkg.in/yaml.v3 v3.0.0-20190409140830-cdc409dda467
)

Please correct me if I am wrong, but this is the latest commit.

@will7200
Copy link
Author

@niemeyer Cannot Reopen for whatever reason

@SofiaTa
Copy link

SofiaTa commented Apr 22, 2019

@niemeyer - we also took the latest cdc409dc commit which include fc85683 and the issue is persist, can you please re-open it ? since it doesn't work ( I verify that the new code if !first || emitter.states[len(emitter.states)-1] != yaml_EMIT_BLOCK_SEQUENCE_ITEM_STATE { is there but it doesnt works) is there some workaround ?

@niemeyer
Copy link
Contributor

Thanks, I'll look into it.

@niemeyer niemeyer reopened this Apr 22, 2019
@niemeyer
Copy link
Contributor

niemeyer commented May 2, 2019

This should have been fixed by 55513ca. Please let me know if you you find any other issues.

@niemeyer niemeyer closed this as completed May 2, 2019
@RaynDol
Copy link

RaynDol commented May 5, 2019

@niemeyer - im trying to run the program with the issue against commit 55513cacd4ae8b250e3a9084ab9d8c407b1ed618 and getting the exact same result , i.e. the yaml is still looks the same and invalid ...Do I miss something ? I just run the program in the post

@niemeyer
Copy link
Contributor

niemeyer commented May 5, 2019

@RaynDol If you're getting the exact same result you're just not running the right code. I've copy & pasted the logic and can go back and forth between the two revisions and get the error or not depending on which revision I'm running.

@RaynDol
Copy link

RaynDol commented May 6, 2019

@niemeyer - not sure that I got you, I use the latest version see go.mod

module mta_parse

go 1.12

require gopkg.in/yaml.v3 v3.0.0-20190502103701-55513cacd4ae

and run the following code which includes valid yaml

package main

import (
	"fmt"
	"log"

	"gopkg.in/yaml.v3"
)

var (
	sourceYaml = `version: 1
type: verbose
kind : bfr

# my list of applications
applications:

#  First app
  - name: app1
    kind: nodejs
    path: app1
    exec:
      platforms: k8s
      builder: test
`
)

func main() {
	t := yaml.Node{}

	err := yaml.Unmarshal([]byte(sourceYaml), &t)
	if err != nil {
		log.Fatalf("error: %v", err)
	}

	b, err := yaml.Marshal(&t)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(string(b))
}

The output is

version: 1
type: verbose
kind: bfr


# my list of applications
applications:
  - #  First app
    name: app1
    kind: nodejs
    path: app1
    exec:
        platforms: k8s
        builder: test


Process finished with exit code 0

what am I missing ? does it works for you ?

please see that the comment is starting the array as before ....

expected yaml

 #  First app
 -  name: app1
    kind: nodejs
    path: app1
    exec:
        platforms: k8s
        builder: test

if I remove the comment (first app) it looks ok, but with the comment change the yaml to invalid yaml

@niemeyer
Copy link
Contributor

niemeyer commented May 6, 2019

@RaynDol That's not the exact same output, and that's valid yaml that represents the same data that was marshalled.

I'm happy to evolve the formatting so comments are placed in the locations most people would appreciate, but that's neither the same output nor invalid, so separate issue.

@RaynDol
Copy link

RaynDol commented May 7, 2019

@niemeyer - Thanks, I've opend the following issue. it will be great if you can have a look

#459

Thanks a lot for your hard work! V3 features are superb!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants