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

Feature request: access YAML fields by string rather than full deserialization #333

Open
fj opened this issue Jun 25, 2018 · 1 comment
Open

Comments

@fj
Copy link

fj commented Jun 25, 2018

Sometimes one just wants to ask questions about the YAML that's being processed rather than needing to deserialize it into an entire object graph. This is especially common when parts of the structure of the input are user-specified, as in the case of things like Kubernetes cluster definitions.

My ideal API would allow me to use jq-style lookups to enumerate over the YAML and retrieve the pieces of interest. For example, that might look like:

var reader = new FileReader("my-file.yaml");
var listOfAllPodNames = Yaml.Deserialize(reader)
                            .Query(".pods[].name")
                            .As(List<string>);

or

var mapOfPodAttributes = Yaml.Deserialize(reader)
                             .Query(".pods[1].metadata")
                             .As(Dictionary<string, string>);

or

var listOfPodMetadata = Yaml.Deserialize(reader)
                            .Query(".pods[].metadata")
                            .As(Dictionary<string, string>[]);

Given the following YAML:

pods:
-
  name: alpha
  size: 1234
  metadata:
    label-one: four-five-six
-
  name: beta
  size: 3456
  metadata:
    label-two: three-seven-eight
    label-three: four-six-two

then we would expect to see the following results:

// .pods[].name
string[]{"alpha", "beta"}

// .pods[1].metadata
Dictionary<string, string> { {"label-one", "four-five-six"} }

// .pods[].metadata
Dictionary<string, string>[] {
  Dictionary<string, string>{ {"label-one", "four-five-six"} },
  Dictionary<string, string>{
    {"label-two", "three-seven-eight"},
    {"label-three", "four-six-two"}
  }
}
@gfs
Copy link

gfs commented Aug 4, 2022

I would also find this feature useful - see referenced issue - it would be very helpful for my use case to also be able to get the character index in the original string of the start of the found element.

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

Successfully merging a pull request may close this issue.

3 participants