THRIFT-2513 clean up enum value assignment
Patch: Dave Watson
This closes #88
Summary:
Clean up how enum values are handled if an integer value is not
explicitly specified in the thrift file.
For example, the following used to be a compile error, but
works now:
enum MyEnum {
SOMEVALUE
}
struct MyStruct {
1: MyEnum e = SOMEVALUE
}
This change also cleans up some of the error handling with out-of-range
values. Previously thrift simply issued a warning for enum values that
didn't fit in an i32, but serialized them as i32 anyway. Now
out-of-range enum values result in a compile failure.
Test Plan:
Included a new unit test to verify the assignment of enum values. I
also verified that g++ makes the same enum value assignments when
compiling these enums as C++ code.
diff --git a/lib/cpp/test/Makefile.am b/lib/cpp/test/Makefile.am
index c1fad3e..e68e66b 100755
--- a/lib/cpp/test/Makefile.am
+++ b/lib/cpp/test/Makefile.am
@@ -20,6 +20,8 @@
noinst_LTLIBRARIES = libtestgencpp.la libprocessortest.la
nodist_libtestgencpp_la_SOURCES = \
gen-cpp/DebugProtoTest_types.cpp \
+ gen-cpp/EnumTest_types.cpp \
+ gen-cpp/EnumTest_types.h \
gen-cpp/OptionalRequiredTest_types.cpp \
gen-cpp/DebugProtoTest_types.cpp \
gen-cpp/ThriftTest_types.cpp \
@@ -65,7 +67,8 @@
TFileTransportTest \
UnitTests \
link_test \
- OpenSSLManualInitTest
+ OpenSSLManualInitTest \
+ EnumTest
# disable these test ... too strong
# processor_test
# concurrency_test
@@ -112,6 +115,13 @@
-l:libboost_unit_test_framework.a \
-lz
+EnumTest_SOURCES = \
+ EnumTest.cpp
+
+EnumTest_LDADD = \
+ libtestgencpp.la \
+ $(BOOST_ROOT_PATH)/lib/libboost_unit_test_framework.a
+
TFileTransportTest_SOURCES = \
TFileTransportTest.cpp
@@ -233,6 +243,9 @@
gen-cpp/DebugProtoTest_types.cpp gen-cpp/DebugProtoTest_types.h: $(top_srcdir)/test/DebugProtoTest.thrift
$(THRIFT) --gen cpp:dense $<
+gen-cpp/EnumTest_types.cpp gen-cpp/EnumTest_types.h: $(top_srcdir)/test/EnumTest.thrift
+ $(THRIFT) --gen cpp $<
+
gen-cpp/OptionalRequiredTest_types.cpp gen-cpp/OptionalRequiredTest_types.h: $(top_srcdir)/test/OptionalRequiredTest.thrift
$(THRIFT) --gen cpp:dense $<