You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by dr...@apache.org on 2009/03/30 22:46:48 UTC

svn commit: r760161 - in /incubator/thrift/trunk: lib/erl/src/thrift_protocol.erl test/ThriftTest.thrift test/erl/src/test_membuffer.erl

Author: dreiss
Date: Mon Mar 30 20:46:47 2009
New Revision: 760161

URL: http://svn.apache.org/viewvc?rev=760161&view=rev
Log:
THRIFT-127. erlang: Skip fields that have the wrong type

In the other languages (AFAIK), if a field has the wrong type,
it is silently skipped.  Erlang currently assumes that the type
is correct and de-synchronizes.  This change makes it behave like
the other languages (except that it is not silent).

Modified:
    incubator/thrift/trunk/lib/erl/src/thrift_protocol.erl
    incubator/thrift/trunk/test/ThriftTest.thrift
    incubator/thrift/trunk/test/erl/src/test_membuffer.erl

Modified: incubator/thrift/trunk/lib/erl/src/thrift_protocol.erl
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/erl/src/thrift_protocol.erl?rev=760161&r1=760160&r2=760161&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/erl/src/thrift_protocol.erl (original)
+++ incubator/thrift/trunk/lib/erl/src/thrift_protocol.erl Mon Mar 30 20:46:47 2009
@@ -140,19 +140,30 @@
         _Else ->
             case dict:find(Fid, SDict) of
                 {ok, {Type, Index}} ->
-                    {ok, Val} = read(IProto, Type),
-                    thrift_protocol:read(IProto, field_end),
-                    NewRTuple = setelement(Index, RTuple, Val),
-                    read_struct_loop(IProto, SDict, NewRTuple);
+                    case term_to_typeid(Type) of
+                        FType ->
+                            {ok, Val} = read(IProto, Type),
+                            thrift_protocol:read(IProto, field_end),
+                            NewRTuple = setelement(Index, RTuple, Val),
+                            read_struct_loop(IProto, SDict, NewRTuple);
+                        Expected ->
+                            error_logger:info_msg(
+                              "Skipping field ~p with wrong type (~p != ~p)~n",
+                              [Fid, FType, Expected]),
+                            skip_field(FType, IProto, SDict, RTuple)
+                    end;
                 _Else2 ->
-                    error_logger:info_msg("Skipping fid ~p~n", [Fid]),
-                    FTypeAtom = thrift_protocol:typeid_to_atom(FType),
-                    thrift_protocol:skip(IProto, FTypeAtom),
-                    read(IProto, field_end),
-                    read_struct_loop(IProto, SDict, RTuple)
+                    error_logger:info_msg("Skipping field ~p with unknown fid~n", [Fid]),
+                    skip_field(FType, IProto, SDict, RTuple)
             end
     end.
 
+skip_field(FType, IProto, SDict, RTuple) ->
+    FTypeAtom = thrift_protocol:typeid_to_atom(FType),
+    thrift_protocol:skip(IProto, FTypeAtom),
+    read(IProto, field_end),
+    read_struct_loop(IProto, SDict, RTuple).
+
 
 skip(Proto, struct) ->
     ok = read(Proto, struct_begin),

Modified: incubator/thrift/trunk/test/ThriftTest.thrift
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/test/ThriftTest.thrift?rev=760161&r1=760160&r2=760161&view=diff
==============================================================================
--- incubator/thrift/trunk/test/ThriftTest.thrift (original)
+++ incubator/thrift/trunk/test/ThriftTest.thrift Mon Mar 30 20:46:47 2009
@@ -42,6 +42,15 @@
   3: i32    i32_thing
 }
 
+struct Xtruct3
+{
+  1:  string string_thing,
+  4:  i32    changed,
+  9:  i32    i32_thing,
+  11: i64    i64_thing
+}
+
+
 struct Insanity
 {
   1: map<Numberz, UserId> userMap,

Modified: incubator/thrift/trunk/test/erl/src/test_membuffer.erl
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/test/erl/src/test_membuffer.erl?rev=760161&r1=760160&r2=760161&view=diff
==============================================================================
--- incubator/thrift/trunk/test/erl/src/test_membuffer.erl (original)
+++ incubator/thrift/trunk/test/erl/src/test_membuffer.erl Mon Mar 30 20:46:47 2009
@@ -23,6 +23,24 @@
     Result = TestData.
 
 
+t2() ->
+    {ok, Transport} = thrift_memory_buffer:new(),
+    {ok, Protocol} = thrift_binary_protocol:new(Transport),
+    TestData = test_data(),
+    ok = thrift_protocol:write(Protocol,
+			       {{struct, element(2, thriftTest_types:struct_info('xtruct'))},
+				TestData}),
+    {ok, Result} = thrift_protocol:read(Protocol,
+					{struct, element(2, thriftTest_types:struct_info('xtruct3'))},
+					'xtruct3'),
+
+    Result = #xtruct3{string_thing = TestData#xtruct.string_thing,
+                      changed = undefined,
+                      i32_thing = TestData#xtruct.i32_thing,
+                      i64_thing = TestData#xtruct.i64_thing}.
+
+
 t() ->
-    t1().
+    t1(),
+    t2().