Created
August 14, 2013 18:43
-
-
Save ferd/6234149 to your computer and use it in GitHub Desktop.
This file contains hidden or 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(test). | |
| -compile(export_all). | |
| -record(request_status, {code, id, properties, message}). | |
| main() -> | |
| print(#request_status{code=232}), | |
| print(#request_status{code=232, id= <<"ok">>, properties=[{a,b}], message= <<"hi">>}), | |
| print(#request_status{code=232, id= <<"ok">>, properties=[{a,b}], message= "hi"}), | |
| print(#request_status{code=232, id= <<"ok">>, properties="still a list, you fail", message= <<"hi">>}), | |
| print(#request_status{code=232, id= {ok}, properties=[{a,b}], message= <<"hi">>}), | |
| print(#request_status{code=232, id= {ok}, properties=[{a,b}], message= "hi"}). | |
| print(Req) -> | |
| try | |
| io:format("~s~n",[[validate(X) || X <- convert(Req)]]) | |
| catch | |
| error:{bad_type, {Prop, Term}} -> | |
| io:format("bad value for ~p: ~p~n", [Prop,Term]) | |
| end. | |
| convert(#request_status{code=C, id=I, properties=P, message=M}) -> | |
| [{code,C}, {id,I}, {properties,P}, {message,M}]. | |
| validate({code, Code}) -> [" code=", Code]; | |
| validate({id, undefined}) -> ""; | |
| validate({message,undefined}) -> ""; | |
| validate({properties,undefined}) -> ""; | |
| validate({id, Id}) when is_binary(Id) -> [" id=", Id]; | |
| validate({message,M}) when is_binary(M) -> [" message=", M]; | |
| validate({properties,L}) when is_list(L) -> [" props=", io_lib:format("~p",[L])]; | |
| validate({Prop, Term}) -> error({bad_type, {Prop, Term}}). |
ferd
commented
Aug 14, 2013
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment