This project adds support for YamlPath queries on YamlDotNet's YamlNode
objects as a Query
extension method.
The API for Query is specified like so:
/// <summary>
/// Get all the <see cref="YamlNode" /> that match the provided yamlPath query.
/// For YamlPath documentation see
/// https://github.com/wwkimball/yamlpath/wiki/Segments-of-a-YAML-Path
/// Does not support Collectors.
/// </summary>
/// <param name="yamlNode">The YamlMappingNode to operate on</param>
/// <param name="yamlPath">The YamlPath query to use</param>
/// <returns>An <see cref="IEnumerable{YamlNode}" /> of the matching nodes</returns>
public static IEnumerable<YamlNode> Query(this YamlNode yamlNode, string yamlPath)
Basic Usage to search a whole document:
// The extension methods are in this namespace
using gfs.YamlDotNet.YamlPath;
// Your query goes here
string yourYamlPathQuery = "...";
// load yml
YamlStream yaml = new YamlStream();
yaml.Load(new StringReader(targetYamlString));
// get the node you want to query, for example the root node of the first document
YamlMappingNode mapping = (YamlMappingNode)yaml.Documents[0].RootNode;
// Receive an enumeration of nodes which match the YamlPath query
IEnumerable<YamlNode> matching = mapping.Query(yourYamlPathQuery);
Check your query for errors/unsupported behavior, receiving a human readable list of errors found, or an empty list for no errors:
using gfs.YamlDotNet.YamlPath;
string yourYamlPathQuery = "...";
List<string> queryProblems = YamlPathExtensions.GetQueryProblems(yourYamlPathQuery);
Implemented based on the YamlPath Spec located here: https://github.com/wwkimball/yamlpath/wiki/Segments-of-a-YAML-Path
Supported:
- Anchors
- Array Elements
- Array Element Searches
- Array Slices
- Hash Attribute Searches
- Hash Keys
- Hash Slices
- Pass-Through Selections for Arrays of Hashes
- Search Keywords
- Set Values
- Wildcard Segments
Not supported: Collectors
Originally a 2022 Microsoft Hackathon Project to add YamlPath support to Application Inspector queries. PR with Original Implementation.
This repository extends and improves that implementation and publishes it as a consumable NuGet package. This repository and its NuGet packages are not supported or maintained by Microsoft Corporation.
MIT