Skip to content

Select SDL nodes with a path ​

Nested byChild loops get long when a tool wants "every dependency inside a configuration that pins a version". A selector says it in one line: a DQL path picks the nodes, and an optional DQL filter keeps the ones that match. The editor takes the result directly, so a lossless edit of every match is one call.

Add dependency "sparkles:wired-sdl-select" and import sparkles.wired.sdl.select; plain sparkles:wired does not build DQL.

Select by path ​

SdlPath.parse checks a path and borrows it; nodes(start) walks the selection lazily, in document order, without allocating:

d
auto path = SdlPath.parse("configuration.dependency");
foreach (dependency; path.value.nodes(document.root))
    writeln(dependency.byValue.front.stringValue);

Each segment is a child step that keeps the children of that name; ns:name names a namespaced tag. An index keeps only the k-th match, counted from zero under each parent: configuration[1].dependency reads the second configuration's dependencies.

SDL names may hold ., which a plain segment cannot spell. Quote such a name as a JSON string in brackets: build["my.tool"].dependency, or ["ns:my.tool"] for a namespaced one. writeDqlSegment from sparkles.dql.path prints a name that way when it needs to, so a path you build from names always parses back.

A leading .. lets the first segment match at any depth below the start: ..dependency reads every dependency, at the top level, in a configuration or in a sub-package, in document order. .. begins a path and nowhere else, and DQL filters do not accept it.

attributes(start) reads the last segment as an attribute name instead: dependency.path yields the path attribute of every root dependency.

A malformed path is an error with its byte offset, never a partial selection.

Filter with DQL ​

SdlSelector.parse(path, filter) adds a filter, evaluated with each selected node as its subject. Inside the filter:

Path in the filterMeans
versionthe node's first version attribute
values[0]its first value
attributes[1]its second attribute's value, whatever its name
targetType.values[0]the first value of its first targetType child
dependency[1].versionthe version of its second dependency child
dflags (alone, as a category)whether it has a dflags child
x == nullx is absent or SDL null
attributes[1] == nullit has at most one attribute
["dflags.ldc"] == nullit has no dflags.ldc attribute

Strings, booleans, integers and floats compare as themselves; &&, ||, !, parentheses, globMatch, regexMatch and fuzzyMatch work as in any DQL query. A filter on an attribute selection tests the node that holds the attributes.

A selector is a value: SdlSelector.parse(…).value binds to a variable, and copies share the parsed filter. Its ranges share it too, so a range may outlive its selector: foreach (n; SdlSelector.parse(…).value.nodes(root)) is fine.

Edit what you selected ​

remove, removeAttribute, replaceAttribute and setAttribute accept a selection and apply the operation to each target, exactly as a loop of single calls would.

A selection is an ordinary range, so a predicate the filter cannot state, such as a byte range, narrows it with filter. In the repository, apps/ci/tools/standardize-build-settings.d removes stale build types that way:

d
auto stale = SdlSelector.parse("buildType", `values[1] == null && values[0] == "checked"`).value;
editor.remove(stale.nodes(root).filter!(node => !inRegion(node)));

Example ​

d
#!/usr/bin/env dub
/+ dub.sdl:
    name "sdl_select"
    dependency "sparkles:wired" version="*"
    dependency "sparkles:wired-sdl-select" version="*"
    buildType "checked" {
        buildOptions "optimize" "inline" "debugInfo"
    }
+/
import std.experimental.allocator.mallocator : Mallocator;
import std.stdio : write, writeln;

import sparkles.base.buffer : UniqueBuffer;
import sparkles.wired.sdl : parseSdlDocument, SdlQualifiedName, SdlScalar,
    sdlEditor;
import sparkles.wired.sdl.select : SdlPath, SdlSelector;

enum text = `name "app"
dependency "sparkles:base" version="*"
dependency "silly" version="~>1.1"
configuration "unittest" {
    dependency "sparkles:test-utils" version="*" // in-tree
}
`;

void main()
{
    auto parsed = parseSdlDocument(text, "dub.sdl");
    assert(parsed.hasValue, parsed.error.toString);
    auto root = parsed.document.root;

    foreach (node; SdlPath.parse("configuration.dependency").value.nodes(root))
        writeln("configuration dependency: ", node.byValue.front.stringValue);

    // Every in-tree dependency, at any depth, points at the checkout
    // instead of the registry.
    auto editor = sdlEditor(parsed.document, text);
    auto inTree = SdlSelector.parse("..dependency.version",
        "globMatch(values[0], `sparkles:*`) && version == `*`").value;
    editor.replaceAttribute(inTree.attributes(root),
        SdlQualifiedName(null, "path"), SdlScalar("../.."));
    UniqueBuffer!char output;
    Mallocator allocator;
    const written = editor.write(output, allocator);
    assert(!written.hasError, written.hasError ? written.error.toString : "");
    write(output[]);
}
configuration dependency: sparkles:test-utils
name "app"
dependency "sparkles:base" path="../.."
dependency "silly" version="~>1.1"
configuration "unittest" {
    dependency "sparkles:test-utils" path="../.." // in-tree
}

The comment, the layout and the pinned silly survive; only the two attributes the selector named changed. See editing §3.2 for the full contract.