Function Tag.expectTagValue
Lookup a child tag by name, and retrieve a value of type T from it. Throws if not found,
Useful if you only expect one value of type T from a given tag. Only
looks for immediate child tags of this
, doesn't search recursively.
This is a shortcut for expectTag()
.
Prototype
T expectTagValue(T)(
string fullTagName
)
if (isValueType!T);
Example
import std .exception;
import sdlang .parser;
auto root = parseSource(`
foo 1 "a" 2 "b"
foo 3 "c" 4 "d" // expectTagValue considers this to override the first foo
bar "hi"
bar 379 // expectTagValue considers this to override the first bar
`);
assert( root .expectTagValue!int("foo") == 3 );
assert( root .expectTagValue!string("foo") == "c" );
// The last "bar" tag doesn't have a string (only the first "bar" tag does)
// If you'd rather receive a default value than an exception, use `getTagValue` instead.
assertThrown!ValueNotFoundException( root .expectTagValue!string("bar") );
// Tag not found
assertThrown!TagNotFoundException( root .expectTagValue!int("doesnt-exist") );
// Using namespaces:
root = parseSource(`
ns1:foo 1 "a" 2 "b"
ns1:foo 3 "c" 4 "d"
ns2:foo 11 "aa" 22 "bb"
ns2:foo 33 "cc" 44 "dd"
ns1:bar "hi"
ns1:bar 379 // expectTagValue considers this to override the first bar
`);
assert( root .expectTagValue!int("ns1:foo") == 3 );
assert( root .expectTagValue!int("*:foo" ) == 33 ); // Search all namespaces
assert( root .expectTagValue!string("ns1:foo") == "c" );
assert( root .expectTagValue!string("*:foo" ) == "cc" ); // Search all namespaces
// The last "bar" tag doesn't have a string (only the first "bar" tag does)
assertThrown!ValueNotFoundException( root .expectTagValue!string("*:bar") );
// Namespace not found
assertThrown!TagNotFoundException( root .expectTagValue!int("doesnt-exist:bar") );