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

unmarshal with comments provide wrong structure #459

Closed
RaynDol opened this issue May 7, 2019 · 6 comments
Closed

unmarshal with comments provide wrong structure #459

RaynDol opened this issue May 7, 2019 · 6 comments

Comments

@RaynDol
Copy link

RaynDol commented May 7, 2019

This is the version

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

please see that the comment is starting the array and not the name property which should start the array .

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, I think the comment and the state of the yaml is one of the killer feature in V3

@niemeyer
Copy link
Contributor

niemeyer commented May 7, 2019

Per note in the other issue, that yaml seems valid.

Which parser is failing to handle it?

@RaynDol
Copy link
Author

RaynDol commented May 7, 2019

@niemeyer no parser, just when you look on it seems to be wrong , it should be the same as before the unmarseling process, what do you think ?

@niemeyer
Copy link
Contributor

niemeyer commented May 7, 2019

The description above states it's invalid yaml. That looks and parses as valid yaml representing the same data that was unmarshalled.

We also currently do not attempt to preserve 100% of the formatting of the yaml parsed. The intent is to produce a format that most people would find pleasant by default. For example, in your input the comment is indented at a different column than the content it's documenting. At least to my taste that feels awkward.

@superbogy
Copy link

I had same issue. Read from file then Unmarshal it and the structure changed. It seems that the pkg sort it when Unmarshal.

yaml file:

openapi: 3.0.0
info:
  version: 1.0.0
  title: Swagger Petstore
  license:
    name: MIT
servers:
  - url: http://petstore.swagger.io/v1
paths:
  bar: foo
  a: 1
  z: 2
  b: 3
components:
  baz: true


after Unmarshal:

components:
  baz: true
info:
  license:
    name: MIT
  title: Swagger Petstore
  version: 1.0.0
openapi: 3.0.0
paths:
  a: 1
  b: 3
  bar: foo
  z: 2
servers:
- url: http://petstore.swagger.io/v1

@niemeyer

@mikefarah
Copy link

One of my users has reported that this is breaking his cassandra's docker entry point

mikefarah/yq#364

@niemeyer
Copy link
Contributor

niemeyer commented May 4, 2020

Sorry, I still don't understand what the issue is here. The two maps reported above are exactly the same yaml. That's also completely unrelated to comments since neither of them have any comments whatsoever.

If someone wants to report an issue, please do so in a slightly more readable form.

@niemeyer niemeyer closed this as completed May 4, 2020
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