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

Question: How can I parse multi yaml file? #664

Open
wrbbz opened this issue Feb 17, 2025 · 2 comments
Open

Question: How can I parse multi yaml file? #664

wrbbz opened this issue Feb 17, 2025 · 2 comments
Labels
question Further information is requested

Comments

@wrbbz
Copy link

wrbbz commented Feb 17, 2025

Hello!
I've got a file with multiple yaml objects with the same structure. It looks like this:

---
apiVersion: ...
kind: ...
metadata:
  description: ...
spec:
  type: ...
---
apiVersion: ...
kind: ...
metadata:
  description: ...
spec:
  type: ...
---
. . .

I would like to parse each object separately and make some processing (i.e. validation)

I've tried the following approach ():

file, err := os.Open(yamlFile)
if err != nil {
  log.Fatal(err)
}

for {
  var yamlObject any
  dec := yaml.NewDecoder(file)
  if err := dec.Decode(&yamlObject); err != nil {
    log.Fatalf("Decoding error: %s", err)
  }

// some processing goes here

}

However, this parses only the first yaml block inside the file and the second iteration of for loop failes with EOF.

I've noticed this solution (somewhat like this) here. I understand, that this solution for another go-yaml, however, I would like to use this implementation

Also, I've noticed this issue which is also about multi-yaml file, so I've tried to initialize File type:

f, err := os.ReadFile(instanceFile)
if err != nil {
  log.Fatal(err)
}

var file ast.File
if _, err := file.Read(f); err != nil {
  log.Fatalln(err)
}

log.Println(len(file.Docs))

And all I've got is EOF

So, how can I properly parse multi-yaml file?

@shuheiktgw shuheiktgw added the question Further information is requested label Feb 17, 2025
@goccy
Copy link
Owner

goccy commented Feb 17, 2025

@wrbbz Your usage is incorrect. Similar to Go's standard library, yaml.NewDecoder must be initialized outside of the loop.

@wrbbz
Copy link
Author

wrbbz commented Feb 17, 2025

@goccy, yeeaaah. That makes a lot of sense. My bad. Thank you for pointing that out

Maybe, you can also point out what is wrong with the initialisation of ast.File in the code block above?

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

No branches or pull requests

3 participants