Created
August 3, 2011 12:29
-
-
Save hntrmrrs/1122520 to your computer and use it in GitHub Desktop.
piqi example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(example). | |
-include("piqiex_piqi.hrl"). | |
-export([main/0]). | |
main() -> | |
Records = | |
[#piqiex_foo{bar=true, baz=false, quux=false}, | |
#piqiex_foo{bar=true, baz=true, quux=false}, | |
#piqiex_foo{bar=true, baz=true, quux=true}, | |
#piqiex_foo{bar=false, baz=false, quux=false}, | |
#piqiex_foo{bar=false, baz=true, quux=false}, | |
#piqiex_foo{bar=false, baz=false, quux=true}, | |
#piqiex_foo{bar=false, baz=true, quux=true}, | |
#piqiex_foo{bar=true, baz=false, quux=true}], | |
Bins = [iolist_to_binary(piqiex_piqi:gen_foo(X)) || X <- Records], | |
V = lists:zip([piqiex_piqi:parse_foo(X) || X <- Bins], Records), | |
[A = B || {A, B} <- V]. | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.module piqiex | |
.record [ | |
.name foo | |
.field [ | |
.name bar | |
.type bool | |
] | |
.field [ | |
.name baz | |
.type bool | |
] | |
.field [ | |
.name quux | |
.type bool | |
] | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(piqiex_piqi). | |
-compile(export_all). | |
-include("piqirun.hrl"). | |
-include("piqiex_piqi.hrl"). | |
-spec gen_bool/2 :: (Code :: piqirun_code(), X :: boolean()) -> iolist(). | |
gen_bool(Code, X) -> | |
piqirun:boolean_to_varint(Code, X). | |
packed_gen_bool(X) -> | |
piqirun:boolean_to_packed_varint(X). | |
-spec gen_foo/2 :: (Code :: piqirun_code(), X :: piqiex_foo()) -> iolist(). | |
gen_foo(Code, X) -> | |
piqirun:gen_record(Code, [ | |
piqirun:gen_required_field(1, fun gen_bool/2, X#piqiex_foo.bar), | |
piqirun:gen_required_field(2, fun gen_bool/2, X#piqiex_foo.baz), | |
piqirun:gen_required_field(3, fun gen_bool/2, X#piqiex_foo.quux) | |
]). | |
-spec gen_bool/1 :: (X :: boolean()) -> iolist(). | |
gen_bool(X) -> | |
gen_bool('undefined', X). | |
-spec gen_foo/1 :: (X :: piqiex_foo()) -> iolist(). | |
gen_foo(X) -> | |
gen_foo('undefined', X). | |
-spec parse_bool/1 :: (X :: piqirun_buffer()) -> boolean(). | |
parse_bool(X) -> | |
piqirun:boolean_of_varint(X). | |
packed_parse_bool(X) -> | |
piqirun:boolean_of_packed_varint(X). | |
-spec parse_foo/1 :: (X :: piqirun_buffer()) -> piqiex_foo(). | |
parse_foo(X) -> | |
R0 = piqirun:parse_record(X), | |
{_Bar, R1} = piqirun:parse_required_field(1, fun parse_bool/1, R0), | |
{_Baz, R2} = piqirun:parse_required_field(2, fun parse_bool/1, R1), | |
{_Quux, R3} = piqirun:parse_required_field(3, fun parse_bool/1, R2), | |
piqirun:check_unparsed_fields(R3), | |
#piqiex_foo{ | |
bar = _Bar, | |
baz = _Baz, | |
quux = _Quux | |
}. | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-ifndef(__PIQIEX_PIQI_HRL__). | |
-define(__PIQIEX_PIQI_HRL__, 1). | |
-record(piqiex_foo, { | |
bar :: boolean(), | |
baz :: boolean(), | |
quux :: boolean() | |
}). | |
-type(piqiex_foo() :: #piqiex_foo{}). | |
-endif. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment