Skip to content

Commit

Permalink
read/write typed object for amf0
Browse files Browse the repository at this point in the history
  • Loading branch information
trung committed Oct 7, 2009
1 parent 0c6e6cd commit 9ad6cd8
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 15 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ docs:
-run edoc_run application "'BeepBeep'" '"."' '[no_packages]'

clean:
(cd src;$(MAKE) clean)
(cd src;$(MAKE) clean)
(cd test;$(MAKE) clean)
9 changes: 5 additions & 4 deletions src/amf0.erl
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,13 @@ read_xml(Bin) ->
{ok, #xml{data = LongString#long_string.data}, NextBin}.

read_typed_object_property(Bin, Obj) ->
{ok, PropertyName, BinAfterName} = read_string(Bin),
{ok, {string, PropertyName}, BinAfterName} = read_string(Bin),
io:fwrite("~n~p~n", [PropertyName]),
case read_object(BinAfterName) of
{object_end_marker, NextBin, _} ->
{ok, Obj, NextBin};
{ok, ObjValue, NextBin} ->
{ok, NewObj, _} = record_utils:set(Obj, PropertyName, ObjValue),
{ok, NewObj, _} = record_utils:set(Obj, list_to_atom(PropertyName), ObjValue),
read_typed_object_property(NextBin, NewObj)
end.

Expand Down Expand Up @@ -204,7 +205,7 @@ write_object(Obj) ->
{ok, undefined} ->
{bad, {"Unknown object", ObjType, ?MODULE, ?LINE}};
{ok, ClassName} ->
{ok, ClassNameBin} = write_string(#string{data = ClassName}),
{ok, ClassNameBin} = string_to_binary(#string{data = ClassName}),
case ObjType of
undefined ->
{bad, {"Object type not found even it was registered", ClassName, Obj}};
Expand All @@ -220,7 +221,7 @@ write_object(ref, Ref) -> write_reference(Ref);
write_object(amf3, Obj) -> amf3:write_object(Obj).

write_object_info(Obj, Field) ->
{ok, FieldBin} = write_string(#string{data = atom_to_list(Field)}),
{ok, FieldBin} = string_to_binary(#string{data = atom_to_list(Field)}),
case record_utils:get(Obj, Field) of
{ok, undefined} ->
[];
Expand Down
14 changes: 4 additions & 10 deletions src/record_utils.erl
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,8 @@ 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, 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, array) when is_record(Obj, asobject) ->
{ok, Obj#asobject.array};

get(Obj, correlationId) when is_record(Obj, async_message) ->
{ok, Obj#async_message.correlationId};
Expand Down Expand Up @@ -157,8 +151,8 @@ set(Obj, timestamp, Value) when is_record(Obj, abstract_message) ->
NewObj = Obj#abstract_message{timestamp = Value},
{ok, NewObj, {timestamp, Value}};

set(Obj, PropertyName, Value) when is_record(Obj, asobject) ->
NewObj = #asobject{array = Obj#asobject.array ++ [{PropertyName, Value}]},
set(Obj, array, Value) when is_record(Obj, asobject) ->
NewObj = Obj#asobject{array = Value},
{ok, NewObj, {array, Value}};

set(Obj, correlationId, Value) when is_record(Obj, async_message) ->
Expand Down
1 change: 1 addition & 0 deletions test/amf0_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
verify(ExpectedValue) ->
{ok, Bin} = amf0:write_object(ExpectedValue),
?assert(Bin /= <<>>),
?debugFmt("~p~n", [Bin]),
{ok, ActualValue, _Rest} = amf0:read_object(Bin),
?assertEqual(ExpectedValue, ActualValue).

Expand Down

0 comments on commit 9ad6cd8

Please sign in to comment.