Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/freke/erlang_protobuffs i…
Browse files Browse the repository at this point in the history
…nto az2-merge-protobuffs-with-upstream

Conflicts:
	Makefile
	rebar
	src/protobuffs_parser.erl
	support/include.mk
	t/Makefile
	t/hasdefault.proto
	t/protobuffs_t_001.t
	t/protobuffs_t_002.t
	t/protobuffs_t_003.t
	t/protobuffs_t_005.t
	t/protobuffs_t_006.t
	t/protobuffs_t_007.t
	t/protobuffs_t_008.t
  • Loading branch information
Vagabond committed Apr 4, 2011
2 parents 84bd7e2 + 10f46ae commit 47476c1
Show file tree
Hide file tree
Showing 57 changed files with 2,224 additions and 1,053 deletions.
9 changes: 8 additions & 1 deletion .gitignore
@@ -1,3 +1,10 @@
ebin
*.beam
*~
*.gz
*~
doc
logs
.eunit
.directory
src/protobuffs_parser.erl
src/protobuffs_scanner.erl
3 changes: 3 additions & 0 deletions AUTHORS
@@ -0,0 +1,3 @@
Nick Gerakines
Jacob Vorreuter
erlrc integration by Cliff Moon
13 changes: 13 additions & 0 deletions ChangeLog
@@ -0,0 +1,13 @@
* Thu Dec 03 2009 Anthony Molinaro <anthony.molinaro@openx.org> 0.0.1
- Fixed path for unit tests
- Have .gitignore ignore all generated files
- Fixed bool type, they were being improperly left as 0 and 1 instead of using
the true and false atoms
- Nested types should now work according to spec (as much as there is one)
- Fixed bool defaults, they were being improperly scanned, so falling back to
keywords
* freke <davabe@hotmail.com>
- Fixed enum support
- Changed build system to Rake
- Added QuickCheck tests
- Changed so parsing of proto files is done by leex and yecc
14 changes: 11 additions & 3 deletions README.markdown
Expand Up @@ -55,9 +55,7 @@ decode functions for the messages defined.
## CAVEATS

Support for parsing proto files and creating code from it is volatile and
should be considered alpha software at best. It currently only supports flat
messages, simple types (ints, strings, etc) and will break on ENUM types and
any sort of nesting. Please do not use this in production.
should be considered alpha software at best. Please do not use this in production.

## no_debug_info

Expand All @@ -67,6 +65,16 @@ _pb modules. Running protobuffs_compile:scan_file/1 reads the erlang forms from
the pokemon_pb.beam file and expands and alters those forms to create the generated
module.

## Building with rebar
To compile
%>./rebar compile

To run all tests
%>./rebar eunit
%>./rebar ct

Se rebar doc for more information.

## CREDITS

Some of the protobuffs.erl module came from code written by Brian Buchanan.
Expand Down
10 changes: 10 additions & 0 deletions bin/protoc-erl
@@ -0,0 +1,10 @@
#!/usr/bin/env escript
%% -*- erlang -*-
%%! -sasl errlog_type error -boot start_sasl -noshell

main ([File]) ->
protobuffs_compile:generate_source (File);
main (_) ->
io:format ("usage: ~s <protofile>~n",
[filename:basename (escript:script_name())]),
halt (1).
18 changes: 0 additions & 18 deletions ebin/protobuffs.app

This file was deleted.

Binary file modified rebar
Binary file not shown.
4 changes: 3 additions & 1 deletion rebar.config
@@ -1 +1,3 @@
{erl_opts, [debug_info]}.
{erl_opts, [debug_info]}.

{clean_files, ["*~","**/*~"]}.
9 changes: 0 additions & 9 deletions src/Makefile

This file was deleted.

49 changes: 39 additions & 10 deletions src/pokemon_pb.erl
Expand Up @@ -24,10 +24,13 @@
%% OTHER DEALINGS IN THE SOFTWARE.
-module(pokemon_pb).
-export([encode_pikachu/1, decode_pikachu/1]).
-compile([export_all]).
-export([encode/1]).
-record(pikachu, {abc, def}).

%% ENCODE
encode(Record) ->
encode(element(1, Record), Record).

encode_pikachu(Record) when is_record(Record, pikachu) ->
encode(pikachu, Record).

Expand All @@ -44,6 +47,9 @@ with_default(Val, _) -> Val.
pack(_, optional, undefined, _, _) -> [];

pack(_, repeated, undefined, _, _) -> [];

pack(_, repeated_packed, undefined, _, _) -> [];
pack(_, repeated_packed, [], _, _) -> [];

pack(FNum, required, undefined, Type, _) ->
exit({error, {required_field_is_undefined, FNum, Type}});
Expand All @@ -54,12 +60,28 @@ pack(_, repeated, [], _, Acc) ->
pack(FNum, repeated, [Head|Tail], Type, Acc) ->
pack(FNum, repeated, Tail, Type, [pack(FNum, optional, Head, Type, [])|Acc]);

pack(FNum, repeated_packed, Data, Type, _) ->
protobuffs:encode_packed(FNum, Data, Type);

pack(FNum, _, Data, _, _) when is_tuple(Data) ->
[RecName|_] = tuple_to_list(Data),
protobuffs:encode(FNum, encode(RecName, Data), bytes);

pack(FNum, _, Data, Type, _) ->
protobuffs:encode(FNum, Data, Type).
pack(FNum, _, Data, Type, _) when Type=:=bool;Type=:=int32;Type=:=uint32;
Type=:=int64;Type=:=uint64;Type=:=sint32;
Type=:=sint64;Type=:=fixed32;Type=:=sfixed32;
Type=:=fixed64;Type=:=sfixed64;Type=:=string;
Type=:=bytes;Type=:=float;Type=:=double ->
protobuffs:encode(FNum, Data, Type);

pack(FNum, _, Data, Type, _) when is_atom(Data) ->
protobuffs:encode(FNum, enum_to_int(Type,Data), enum).

enum_to_int(pikachu,value) ->
1.

int_to_enum(_,Val) ->
Val.

%% DECODE
decode_pikachu(Bytes) when is_binary(Bytes) ->
Expand All @@ -72,29 +94,36 @@ decode(pikachu, Bytes) when is_binary(Bytes) ->

decode(<<>>, _, Acc) -> Acc;
decode(Bytes, Types, Acc) ->
{{FNum, WireType}, Rest} = protobuffs:read_field_num_and_wire_type(Bytes),
{ok, FNum} = protobuffs:next_field_num(Bytes),
case lists:keysearch(FNum, 1, Types) of
{value, {FNum, Name, Type, Opts}} ->
{Value1, Rest1} =
case lists:member(is_record, Opts) of
true ->
{V, R} = protobuffs:decode_value(Rest, WireType, bytes),
{{FNum, V}, R} = protobuffs:decode(Bytes, bytes),
RecVal = decode(list_to_atom(string:to_lower(atom_to_list(Type))), V),
{RecVal, R};
false ->
{V, R} = protobuffs:decode_value(Rest, WireType, Type),
{unpack_value(V, Type), R}
case lists:member(repeated_packed, Opts) of
true ->
{{FNum, V}, R} = protobuffs:decode_packed(Bytes, Type),
{V, R};
false ->
{{FNum, V}, R} = protobuffs:decode(Bytes, Type),
{unpack_value(V, Type), R}
end

end,
case lists:member(repeated, Opts) of
true ->
case lists:keytake(FNum, 1, Acc) of
{value, {FNum, Name, List}, Acc1} ->
decode(Rest1, Types, [{FNum, Name, lists:reverse([Value1|lists:reverse(List)])}|Acc1]);
decode(Rest1, Types, [{FNum, Name, lists:reverse([int_to_enum(Type,Value1)|lists:reverse(List)])}|Acc1]);
false ->
decode(Rest1, Types, [{FNum, Name, [Value1]}|Acc])
decode(Rest1, Types, [{FNum, Name, [int_to_enum(Type,Value1)]}|Acc])
end;
false ->
decode(Rest1, Types, [{FNum, Name, Value1}|Acc])
decode(Rest1, Types, [{FNum, Name, int_to_enum(Type,Value1)}|Acc])
end;
false ->
exit({error, {unexpected_field_index, FNum}})
Expand Down
8 changes: 8 additions & 0 deletions src/protobuffs.app.src
@@ -0,0 +1,8 @@
{ application, protobuffs,
[ { description, "Google protobuffs implementation for Erlang." },
{ vsn, "0.6.0" },
{ registered, [] },
{ applications, [kernel,stdlib] },
{ env, [] }
]
}.

0 comments on commit 47476c1

Please sign in to comment.