Function Tag.expectTag

Lookup a child tag by name. Throws 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 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 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 no such tag is found, an sdlang.exception.TagNotFoundException will be thrown. If you'd rather receive a default value, use getTag instead.

Prototype

Tag expectTag(
  string fullTagName
);

Example

import std.exception;
import sdlang.parser;

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

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

// Not found
// If you'd rather receive a default value than an exception, use `getTag` instead.
assertThrown!TagNotFoundException( root.expectTag("doesnt-exist") );

Authors

Copyright

License