Permalink
Browse files

passed test case unregistered typed object for amf0

  • Loading branch information...
1 parent 6297974 commit 37ec5e43568ba46e634c80ba5be67d16cea47449 @trung committed Oct 7, 2009
Showing with 62 additions and 26 deletions.
  1. +1 −1 include/types.hrl
  2. +4 −1 src/Makefile
  3. +32 −15 src/amf0.erl
  4. +7 −2 src/record_helper.erl
  5. +10 −4 src/record_utils.erl
  6. +2 −2 support/include.mk
  7. +6 −1 test/amf0_test.erl
View
@@ -6,7 +6,7 @@
-record(ecma_array, {data}).
--record(asobject, {array}).
+-record(asobject, {array = []}).
-record(xml, {data}).
View
@@ -13,4 +13,7 @@ clean:
rm -rf $(EBIN_FILES_NO_DOCS)
record_utils.erl: $(EBIN_DIR)/record_helper.$(EMULATOR) $(ERL_GENERATED_SOURCES)
- $(ERL) -noshell -pa $(EBIN_DIR) -s record_helper make $(ERL_GENERATED_SOURCES) -s init stop
+ $(ERL) -noshell -pa $(EBIN_DIR) -s record_helper make $(ERL_GENERATED_SOURCES) -s init stop
+
+$(ERL_GENERATED_SOURCES):
+ @echo "Nothing"
View
@@ -111,14 +111,19 @@ read_xml(Bin) ->
{ok, #xml{data = LongString#long_string.data}, NextBin}.
read_typed_object_property(Bin, Obj) ->
- {ok, {string, PropertyName}, BinAfterName} = read_string(Bin),
- io:fwrite("~n~p~n", [PropertyName]),
+ {ok, PropertyName, BinAfterName} = read_string(Bin),
case read_object(BinAfterName) of
{object_end_marker, NextBin, _} ->
{ok, Obj, NextBin};
{ok, ObjValue, NextBin} ->
- {ok, NewObj, _} = record_utils:set(Obj, list_to_atom(PropertyName), ObjValue),
- read_typed_object_property(NextBin, NewObj)
+ case record_utils:type(Obj) of
+ asobject ->
+ {ok, NewObj, _} = record_utils:set(Obj, PropertyName, ObjValue),
+ read_typed_object_property(NextBin, NewObj);
+ _ ->
+ {ok, NewObj, _} = record_utils:set(Obj, list_to_atom(PropertyName#string.data), ObjValue),
+ read_typed_object_property(NextBin, NewObj)
+ end
end.
read_typed_object(Bin) ->
@@ -167,7 +172,7 @@ read_reference(Bin) ->
read_object(<<?number_marker:8, Rest/binary>>) -> read_number(Rest);
read_object(<<?boolean_marker:8, Rest/binary>>) -> read_boolean(Rest);
read_object(<<?string_marker:8, Rest/binary>>) -> read_string(Rest);
-read_object(<<?object_marker:8, Rest/binary>>) -> {bad, {"Not yet implemented", ?MODULE,?LINE, Rest}};
+read_object(<<?object_marker:8, Rest/binary>>) -> read_typed_object_property(Rest, #asobject{});
read_object(<<?movieclip_marker:8, Rest/binary>>) -> {bad, {"Reserved, not supported", Rest}};
read_object(<<?null_marker:8, Rest/binary>>) -> read_null(Rest);
read_object(<<?undefined_marker:8, Rest/binary>>) -> {bad, {"Undefined marker", ?MODULE, ?LINE, Rest}};
@@ -200,18 +205,26 @@ write_object({{Ye, Mo, Da}, {Ho, Mi, Se}}) -> write_date({{Ye, Mo, Da}, {Ho, Mi,
write_object(Xml) when is_record(Xml, xml) -> write_xml(Xml);
write_object(Array) when is_list(Array) -> write_strict_array(Array);
write_object(Obj) ->
- ObjType = record_utils:type(Obj),
+ ObjType = record_utils:type(Obj),
case registry:record_to_fc(ObjType) of
{ok, undefined} ->
- {bad, {"Unknown object", ObjType, ?MODULE, ?LINE}};
+ % write asobject
+ case ObjType of
+ asobject ->
+ PropertyArray = Obj#asobject.array,
+ {ok, ObjEnd} = write_object_end(),
+ write_object_now(?object_marker, [write_object_info(Field, Obj) || {Field, Obj} <- PropertyArray] ++ ObjEnd);
+ _ ->
+ {bad, {"Unknown object type", ObjType, ?MODULE, ?LINE}}
+ end;
{ok, ClassName} ->
{ok, ClassNameBin} = string_to_binary(#string{data = ClassName}),
case ObjType of
undefined ->
{bad, {"Object type not found even it was registered", ClassName, Obj}};
Type ->
Fields = record_utils:fields_atom(Type),
- ObjBin = list_to_binary([write_object_info(Obj, X) || X <- Fields ]),
+ ObjBin = list_to_binary([write_object_info(X, Obj) || X <- Fields ]),
{ok, ObjEnd} = write_object_end(),
write_object_now(?typed_object_marker, [ClassNameBin, ObjBin, ObjEnd])
end
@@ -220,15 +233,19 @@ write_object(Obj) ->
write_object(ref, Ref) -> write_reference(Ref);
write_object(amf3, Obj) -> amf3:write_object(Obj).
-write_object_info(Obj, Field) ->
+write_object_info(Field, Obj) when is_record(Field, string) ->
+ {ok, FieldBin} = string_to_binary(Field),
+ {ok, ValueBin} = write_object(Obj),
+ [FieldBin, ValueBin];
+write_object_info(Field, Obj) ->
{ok, FieldBin} = string_to_binary(#string{data = atom_to_list(Field)}),
case record_utils:get(Obj, Field) of
- {ok, undefined} ->
- [];
- {ok, Value} ->
- {ok, ValueBin} = write_object(Value),
- [FieldBin, ValueBin]
- end.
+ {ok, undefined} ->
+ [];
+ {ok, Value} ->
+ {ok, ValueBin} = write_object(Value),
+ [FieldBin, ValueBin]
+ end.
%% return {ok, ReturnBin} or {bad, Reason}
write_u8(Value) -> {ok, <<Value:8>>}.
View
@@ -1,6 +1,7 @@
%% For each header file, it scans thru all records and create helper functions
%% Helper functions are:
%% setters, getters, fields, fields_atom, type
+
-module(record_helper).
-author("trung@mdkt.org").
@@ -24,10 +25,12 @@ make(HeaderFiles, OutDir) ->
read(HeaderFile) ->
try epp:parse_file(HeaderFile,[],[]) of
{ok, Tree} ->
- parse(Tree)
+ parse(Tree);
+ {error, Error} ->
+ {error, {"Error parsing header file", HeaderFile, Error}}
catch
_:Error ->
- {error, Error}
+ {catched_error, {"Error parsing header file", HeaderFile, Error}}
end.
format_src([{_, _, _, Src}|T]) when length(T) == 0 ->
@@ -107,6 +110,8 @@ generate_fields_atom_function(RecordName, RecordFields) ->
generate_setter_getter_function(RecordName, {record_field, _, {atom, _, FieldName}, {record, _, ParentRecordName, _}}) ->
to_setter_getter_function(atom_to_list(RecordName), atom_to_list(FieldName), atom_to_list(ParentRecordName));
+generate_setter_getter_function(RecordName, {record_field, _, {atom, _, array}}) ->
+ to_setter_getter_function(atom_to_list(RecordName));
generate_setter_getter_function(RecordName, {record_field, _, {atom, _, array}, _}) ->
to_setter_getter_function(atom_to_list(RecordName));
generate_setter_getter_function(RecordName, {record_field, _, {atom, _, FieldName}, _}) ->
View
@@ -57,8 +57,14 @@ get(Obj, timeToLive) when is_record(Obj, abstract_message) ->
get(Obj, timestamp) when is_record(Obj, abstract_message) ->
{ok, Obj#abstract_message.timestamp};
-get(Obj, array) when is_record(Obj, asobject) ->
- {ok, Obj#asobject.array};
+get(Obj, PropertyName) when is_record(Obj, asobject) ->
+ Ret = [X || {P, X} <- Obj#asobject.array, P == PropertyName],
+ if
+ length(Ret) == 0 -> {bad, {"PropertyName not found in the object", Obj, PropertyName}};
+ true ->
+ [Value|_] = Ret,
+ {ok, Value}
+ end;
get(Obj, correlationId) when is_record(Obj, async_message) ->
{ok, Obj#async_message.correlationId};
@@ -151,8 +157,8 @@ set(Obj, timestamp, Value) when is_record(Obj, abstract_message) ->
NewObj = Obj#abstract_message{timestamp = Value},
{ok, NewObj, {timestamp, Value}};
-set(Obj, array, Value) when is_record(Obj, asobject) ->
- NewObj = Obj#asobject{array = Value},
+set(Obj, PropertyName, Value) when is_record(Obj, asobject) ->
+ NewObj = #asobject{array = Obj#asobject.array ++ [{PropertyName, Value}]},
{ok, NewObj, {array, Value}};
set(Obj, correlationId, Value) when is_record(Obj, async_message) ->
View
@@ -28,8 +28,8 @@ ERL_OBJECTS := $(ERL_SOURCES:%.erl=$(EBIN_DIR)/%.$(EMULATOR))
ERL_DOCUMENTS := $(ERL_SOURCES:%.erl=$(DOC_DIR)/%.html)
ERL_OBJECTS_LOCAL := $(ERL_SOURCES:%.erl=./%.$(EMULATOR))
APP_FILES := $(wildcard *.app)
-EBIN_FILES = $(ERL_OBJECTS) $(ERL_DOCUMENTS) $(APP_FILES:%.app=../ebin/%.app) $(ERL_HEADERS)
-EBIN_FILES_NO_DOCS = $(ERL_OBJECTS) $(APP_FILES:%.app=../ebin/%.app) $(ERL_HEADERS)
+EBIN_FILES = $(ERL_OBJECTS) $(ERL_DOCUMENTS) $(APP_FILES:%.app=../ebin/%.app)
+EBIN_FILES_NO_DOCS = $(ERL_OBJECTS) $(APP_FILES:%.app=../ebin/%.app)
MODULES = $(ERL_SOURCES:%.erl=%)
../ebin/%.app: %.app
View
@@ -13,8 +13,9 @@
verify(ExpectedValue) ->
{ok, Bin} = amf0:write_object(ExpectedValue),
?assert(Bin /= <<>>),
- ?debugFmt("~p~n", [Bin]),
+% ?debugFmt("~p=~p~n", [Bin, size(Bin)]),
{ok, ActualValue, _Rest} = amf0:read_object(Bin),
+% ?debugFmt("~p~n", [ActualValue]),
?assertEqual(ExpectedValue, ActualValue).
%%
@@ -96,3 +97,7 @@ strict_array_test() ->
registered_typed_object_test() ->
ExpectedValue = #remoting_message{source = #string{data = "somesource"}},
verify(ExpectedValue).
+
+unregistered_typed_object_test() ->
+ ExpectedValue = #asobject{array = [{#string{data = "DSId"}, #string{data = "someId"}}, {#string{data = "DSTemp"}, 12.0}]},
+ verify(ExpectedValue).

0 comments on commit 37ec5e4

Please sign in to comment.