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

Parse error #347

Closed
1 of 2 tasks
adjbcbc opened this issue May 22, 2024 · 6 comments · Fixed by #348 or #351
Closed
1 of 2 tasks

Parse error #347

adjbcbc opened this issue May 22, 2024 · 6 comments · Fixed by #348 or #351
Labels
bug Something isn't working

Comments

@adjbcbc
Copy link

adjbcbc commented May 22, 2024

Description

it seems that an issue occurs when parsing yaml in the following file.

contexts:                                                                    
- context:                                                                   
    cluster: abcdef                                                          
    extension:                                                               
    - extension:                                                             
        last-update: blah                                                    
        version: 0.1.0                                                       
      name: blah                                                             
    bug: default                                                             
  ctx: ctx    

Reproduction steps

 #include <iostream>                                                     
 #include "fkYAML/node.hpp"                                              
 int main() {                                                            
     fkyaml::node root = fkyaml::node::deserialize("./yamltest");        
                                                                         
     for (const auto &ctx : root["contexts"]) {
         std::cout << ctx["context"]["cluster"].get_value<std::string>(); // error                  
         std::cout << ctx["context"]["bug"].get_value<std::string>();      // error
     }                                                                   
 }                

Expected vs. actual results

terminate called after throwing an instance of 'fkyaml::v0_3_7::type_error'
what(): type_error: The target node is neither of sequence nor mapping types. type=null
Aborted (core dumped)

Minimal code example

No response

Error messages

No response

Compiler and operating system

g++ 14.1.1 & linux 6.9.1-arch1-1

Library version

v0.3.7

Validation

@fktn-k
Copy link
Owner

fktn-k commented May 22, 2024

@adjbcbc
Thanks for sharing the issue and sorry for your inconvenience.

First of all, the fkyaml::node::deserialize() function doesn't expect a file path as an input.
If you want to deserialize file contents, you must first open a file and pass the file handle, i.e., FILE* or any object which can be interpreted as std::istream, like the following:

#include <iostream>
#include <fstream> // ADDED
#include "fkYAML/node.hpp"
int main() {                                                       
    std::ifstream ifs("./yamltest");                    // ADDED
    fkyaml::node root = fkyaml::node::deserialize(ifs); // CHANGED

    for (const auto &ctx : root["contexts"]) {
        std::cout << ctx["context"]["cluster"].get_value<std::string>(); // error
        std::cout << ctx["context"]["bug"].get_value<std::string>();      // error
    }
}

If a file path is given, fkYAML tries to parse the string ./yamltest itself, which is why you encountered the reported error.

So, I tried the input with the above code and found a bug thanks to your report.
There seems to be a bug in handling indentation after a block sequence with the same indentation as its key node (line 9 in the YAML file).
Due to the bug, the node associated with the bug key was mistakenly parsed as a child of the sequence node with extension key.

Although I'm going to fix the bug hopefully this weekend, a quick workaround is to use the slightly modified version which works fine in my environment:

contexts:                                                                    
- context:                                                                   
    cluster: abcdef                                                          
    extension:                                                               
      - extension:                                                             
          last-update: blah                                                    
          version: 0.1.0                                                       
        name: blah                                                             
    bug: default                                                             
  ctx: ctx    

@fktn-k
Copy link
Owner

fktn-k commented May 25, 2024

@adjbcbc
The PR #348 has fixed the bug in parsing the YAML snippet you shared above.
Could you confirm if you can now get the correct parse result on the latest develop branch?

@adjbcbc
Copy link
Author

adjbcbc commented May 27, 2024

thinks for quick fix
i have confirmed that above example has been corrected properly.
but, i originally intended to try following file, but now encounted the following error.

terminate called after throwing an instance of 'fkyaml::v0_3_7::parse_error'
  what():  parse_error: invalid flow mapping ending is found. (at line 26, column 14)
Aborted (core dumped)
apiVersion: v1
clusters:
- cluster:
    certificate-authority: /home/x/.minikube/ca.crt
    extensions:
    - extension:
        last-update: Mon, 27 May 2024 06:05:41 UTC
        provider: minikube.sigs.k8s.io
        version: v1.32.0
      name: cluster_info
    server: https://192.168.30.12:8441
  name: minikube
contexts:
- context:
    cluster: minikube
    extensions:
    - extension:
        last-update: Mon, 27 May 2024 06:05:41 UTC
        provider: minikube.sigs.k8s.io
        version: v1.32.0
      name: context_info
    namespace: default
    user: minikube
  name: minikube
current-context: minikube
kind: Config
preferences: {}
users:
- name: minikube
  user:
    client-certificate: /home/x/.minikube/profiles/minikube/client.crt
    client-key: /home/x/.minikube/profiles/minikube/client.key

how can i solve the above issue?
tools like kubectl had no problems reading that file

@fktn-k
Copy link
Owner

fktn-k commented May 27, 2024

@adjbcbc
Thanks for confirming the fix and reporting the new issue.

The above error was caused because I forgot to implement for empty flow containers in the PR #350.
I can't think of a workaround for this issue, but it shouldn't require any major changes and will be fixed soon.

@fktn-k
Copy link
Owner

fktn-k commented May 27, 2024

The error on empty flow containers has been fixed in the PR #351.
And I've checked that the parsing of the YAML file shared here succeeds with the modified parser, just locally though.
Can you please check if it can now be parsed with no issue in your environment too?

@fktn-k fktn-k linked a pull request May 27, 2024 that will close this issue
4 tasks
@adjbcbc
Copy link
Author

adjbcbc commented May 28, 2024

i have confirmed that it works in my environment as well. thank you for the quick response. this is a good project!

@adjbcbc adjbcbc closed this as completed May 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
2 participants