-
Notifications
You must be signed in to change notification settings - Fork 62
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
Example YAML appears to be invalid #94
Comments
Hi @kmcquade. The YAML spec refers to this as a "complex map key" which we leverage here to denote an Endpoint which is a combination of a Path and a Method. The YAML spec explains it but not very well unfortunately: You can also see this related thread. I hope this clarifies it. |
Oh wow I didn't know that. I guess you do learn something new every day. I'm trying to load that YAML into Python but I don't think the Python YAML module supports this. Any suggestions? Is there a way for oasdiff to toggle that feature by chance? |
This seems to work: from pathlib import Path
import ruamel.yaml
file_name = Path('data/test.yaml')
yaml = ruamel.yaml.YAML()
data = yaml.load(file_name)
yaml.dump(data['endpoints']['modified'] , sys.stdout) Installation: |
Sorry, I should have been more specific. I'm trying to load that YAML into a python dictionary. That code snippet above loads Any ideas on reading it as a dictionary? |
How about this? from yaml import load
import io
import json
try:
from yaml import CLoader as Loader
except ImportError:
from yaml import Loader
stream=io.open('data/test.yaml', mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True)
data = load(stream, Loader=Loader)
print(json.dumps(data, indent=4, sort_keys=True)) Edit: |
I didn't want to leave this mystery unsolved so I did some more digging. from yaml import load
import io
import json
try:
from yaml import CLoader as Loader
except ImportError:
from yaml import Loader
class Pairs(list):
def __repr__(self):
return f"{type(self).__name__}({super().__repr__()})"
def construct_mapping(self, node):
value = self.construct_pairs(node)
try:
return dict(value)
except TypeError:
return Pairs(value)
Loader.construct_mapping = construct_mapping
Loader.add_constructor('tag:yaml.org,2002:map', Loader.construct_mapping)
stream = io.open('data/test.yaml', mode='r', buffering=-1,
encoding=None, errors=None, newline=None, closefd=True)
data = load(stream, Loader=Loader)
print(json.dumps(data, indent=4, sort_keys=True)) |
Yes!!! Thank you so much. I never would have figured all that out on my own. You're awesome. I really appreciate it. |
First of all, thanks for such a cool tool! This is awesome.
Description
I was following the tutorial and tinkering a bit and realized that the example YAML output is actually invalid YAML. See the screenshot from the README of the repo below.
To Reproduce
It's the main README - but for the sake of conforming to the issue template 🙂 here's the command from the root of the repo:
Expected behavior
I would expect the YAML to be valid. I wasn't sure if this was just a diff format thing - but I would assume that we'd want a way to generate valid YAML.
I'm excited to get this working! 😄 Cool stuff!
The text was updated successfully, but these errors were encountered: