import std.exception; import std.math; import gfx.decl.sdlang.parser; auto root = parseSource(` foo z=0 X=1 X=true X=2 X=false `); auto foo = root.getTag("foo"); assert( foo.expectAttribute!int("X") == 1 ); assert( foo.expectAttribute!bool("X") == true ); // Attribute name not found // If you'd rather receive a default value than an exception, use `getAttribute` instead. assertThrown!AttributeNotFoundException( foo.expectAttribute!int("doesnt-exist") ); // No strings found assertThrown!AttributeNotFoundException( foo.expectAttribute!string("X") ); // No floats found assertThrown!AttributeNotFoundException( foo.expectAttribute!float("X") ); // Using namespaces: root = parseSource(` foo ns1:z=0 ns1:X=1 ns1:X=2 ns2:X=3 ns2:X=4 `); foo = root.getTag("foo"); assert( foo.expectAttribute!int("ns2:X") == 3 ); assert( foo.expectAttribute!int("*:X") == 1 ); // Search all namespaces // Namespace not found assertThrown!AttributeNotFoundException( foo.expectAttribute!int("doesnt-exist:X") ); // No attribute X is in the default namespace assertThrown!AttributeNotFoundException( foo.expectAttribute!int("X") ); // Attribute name not found assertThrown!AttributeNotFoundException( foo.expectAttribute!int("ns1:doesnt-exist") );
Lookup an attribute of this tag by name, and retrieve a value of type T from it. Throws if not found.
Useful if you only expect one attribute of the given name and type.
Only looks for attributes of this tag, it does not search child tags. If you wish to search for a value in a child tag (for example, if this current tag is a root tag), try expectTagAttribute.
If you expect multiple attributes by the same name and want to get them all, use attributes[string] instead.
The attribute name can optionally include a namespace, as in "namespace:name". Or, you can search all namespaces using "*:name". (Note that unlike tags. attributes can't be anonymous - that's what values are.) Wildcard searching is only supported for namespaces, not names. Use attributes[0] if you don't care about the name.
If this tag has multiple attributes, the first attribute matching the requested name and type will be returned. Ie, Extra attributes in the tag are ignored.
An sdlang.exception.AttributeNotFoundException will be thrown if no value of the requested type can be found. If you'd rather receive a default value, use getAttribute instead.