Function Tag.getTag

Lookup a child tag by name. Returns null if not found.

Useful if you only expect one, and only one, child tag of a given name. Only looks for immediate child tags of this, doesn't search recursively.

If you expect multiple tags by the same name and want to get them all, use maybe.tags[string] instead.

The name can optionally include a namespace, as in "namespace:name". Or, you can search all namespaces using "*:name". Use an empty string to search for anonymous tags, or "namespace:" for anonymous tags inside a namespace. Wildcard searching is only supported for namespaces, not names. Use maybe.tags[0] if you don't care about the name.

If there are multiple tags by the chosen name, the last tag will always be chosen. That is, this function considers later tags with the same name to override previous ones.

If the tag cannot be found, and you provides a default value, the default value is returned. Otherwise null is returned. If you'd prefer an exception thrown, use expectTag instead.

Prototype

Tag getTag(
  string fullTagName,
  Tag defaultValue = null
);

Example

import std.exception;
import sdlang.parser;

auto root = parseSource(`
	foo 1
	foo 2  // getTag considers this to override the first foo

	ns1:foo 3
	ns1:foo 4   // getTag considers this to override the first ns1:foo
	ns2:foo 33
	ns2:foo 44  // getTag considers this to override the first ns2:foo
`);
assert( root.getTag("foo"    ).values[0].get!int() == 2  );
assert( root.getTag("ns1:foo").values[0].get!int() == 4  );
assert( root.getTag("*:foo"  ).values[0].get!int() == 44 ); // Search all namespaces

// Not found
// If you'd prefer an exception, use `expectTag` instead.
assert( root.getTag("doesnt-exist") is null );

// Default value
auto foo = root.getTag("foo");
assert( root.getTag("doesnt-exist", foo) is foo );

Authors

Copyright

License