THRIFT-1586 Two small D issues
Patch: David Nadlinger
git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1331810 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/d/src/thrift/codegen/base.d b/lib/d/src/thrift/codegen/base.d
index e7e3ead..9624012 100644
--- a/lib/d/src/thrift/codegen/base.d
+++ b/lib/d/src/thrift/codegen/base.d
@@ -368,7 +368,7 @@
static assert(is(This == struct) || is(This : Exception),
"TStructHelpers can only be used inside a struct or an Exception class.");
- static if (is(TIsSetFlags!(This, fieldMetaData))) {
+ static if (TIsSetFlags!(This, fieldMetaData).tupleof.length > 0) {
// If we need to keep isSet flags around, create an instance of the
// container struct.
TIsSetFlags!(This, fieldMetaData) isSetFlags;
@@ -530,38 +530,35 @@
assert(f.toString() == `Foo(a: a string, b: 0 (unset), c: 4)`);
}
+
/**
* Generates an eponymous struct with boolean flags for the non-required
- * non-nullable fields of T, if any, or nothing otherwise (i.e. the template
- * body is empty).
+ * non-nullable fields of T.
*
- * Nullable fields are just set to null to signal »not set«.
+ * Nullable fields are just set to null to signal »not set«, so no flag is
+ * emitted for them, even if they are optional.
*
* In most cases, you do not want to use this directly, but via TStructHelpers
* instead.
*/
-// DMD @@BUG@@: Using getFieldMeta!T in here horribly breaks things to the point
-// where getFieldMeta is *instantiated twice*, with different bodies. This is
-// connected to the position of »enum fieldMeta« in TStructHelpers.
template TIsSetFlags(T, alias fieldMetaData) {
mixin({
- string boolDefinitions;
- foreach (name; __traits(derivedMembers, T)) {
- static if (!is(MemberType!(T, name)) || is(MemberType!(T, name) == void)) {
- // We hit something strange like the TStructHelpers template itself,
- // just ignore.
- } else static if (isNullable!(MemberType!(T, name))) {
- // If the field is nullable, we don't need an isSet flag as we can map
- // unset to null.
- } else static if (memberReq!(T, name, fieldMetaData) != TReq.REQUIRED) {
- boolDefinitions ~= "bool " ~ name ~ ";\n";
+ string code = "struct TIsSetFlags {\n";
+ foreach (meta; fieldMetaData) {
+ code ~= "static if (!is(MemberType!(T, `" ~ meta.name ~ "`))) {\n";
+ code ~= q{
+ static assert(false, "Field '" ~ meta.name ~
+ "' referenced in metadata not present in struct '" ~ T.stringof ~ "'.");
+ };
+ code ~= "}";
+ if (meta.req == TReq.OPTIONAL || meta.req == TReq.OPT_IN_REQ_OUT) {
+ code ~= "else static if (!isNullable!(MemberType!(T, `" ~ meta.name ~ "`))) {\n";
+ code ~= " bool " ~ meta.name ~ ";\n";
+ code ~= "}\n";
}
}
- if (!boolDefinitions.empty) {
- return "struct TIsSetFlags {\n" ~ boolDefinitions ~ "}";
- } else {
- return "";
- }
+ code ~= "}";
+ return code;
}());
}
diff --git a/tutorial/d/Makefile b/tutorial/d/Makefile
index efbb93e..fcee9af 100644
--- a/tutorial/d/Makefile
+++ b/tutorial/d/Makefile
@@ -31,7 +31,7 @@
dmd -I${LIB_D_DIR}/src -L-L${LIB_D_DIR} -L-lthriftd client.d ${GEN_SRC}
async_client: async_client.d
- dmd -I${LIB_D_DIR}/src -L-L${LIB_D_DIR} -L-lthriftd -L-lthriftd-event -L-levent async_client.d ${GEN_SRC}
+ dmd -I${LIB_D_DIR}/src -L-L${LIB_D_DIR} -L-lthriftd-event -L-lthriftd -L-levent async_client.d ${GEN_SRC}
clean:
$(RM) -f server client async_client