Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 1 | %%% Copyright (c) 2007- Facebook |
| 2 | %%% Distributed under the Thrift Software License |
Christopher Piro | de11d85 | 2007-11-18 02:10:20 +0000 | [diff] [blame] | 3 | %%% |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 4 | %%% See accompanying file LICENSE or visit the Thrift site at: |
| 5 | %%% http://developers.facebook.com/thrift/ |
| 6 | |
Christopher Piro | 5f5fdf3 | 2007-07-25 22:41:00 +0000 | [diff] [blame] | 7 | -module(thrift_utils). |
| 8 | |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 9 | -include("transport/tTransportException.hrl"). |
Christopher Piro | 5f5fdf3 | 2007-07-25 22:41:00 +0000 | [diff] [blame] | 10 | |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 11 | -export([tabulate/2, dict_size/1, sformat/2, unbrack/1, first_item/1, unnest_record/2]). |
Christopher Piro | 5f5fdf3 | 2007-07-25 22:41:00 +0000 | [diff] [blame] | 12 | |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 13 | %% tabulate |
| 14 | tabulate(N,F) -> |
| 15 | tabulate(0, N, F). |
| 16 | |
| 17 | tabulate(N,M,_) when N==M -> |
| 18 | []; |
| 19 | tabulate(N,M,F) -> |
| 20 | [F(N) | tabulate(N+1, M, F)]. |
| 21 | |
| 22 | %% makin me sad |
Christopher Piro | 5f5fdf3 | 2007-07-25 22:41:00 +0000 | [diff] [blame] | 23 | dict_size(Dict) -> |
| 24 | dict:fold(fun (_,_,I) -> I+1 end,0,Dict). |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 25 | |
| 26 | %% I CAN HAS EAZIER KTHX |
| 27 | sformat(Format, Data) when is_list(Data) -> |
| 28 | lists:flatten(io_lib:format(Format, Data)); |
| 29 | sformat(Format, Item) -> |
| 30 | error_logger:warning_msg("sformat called with non-list Data: (~p, ~p)", [Format, Item]), |
| 31 | sformat(Format, [Item]). |
| 32 | |
| 33 | %% render a list and pick off the square brackets |
| 34 | unbrack(List) -> |
| 35 | List1 = sformat("~w", [List]), |
| 36 | string:substr(List1, 2, length(List1)-2). |
| 37 | |
Christopher Piro | 3b63fe4 | 2007-10-19 21:34:31 +0000 | [diff] [blame] | 38 | first_item(DeepTuple) when is_tuple(DeepTuple) -> |
| 39 | first_item(element(1, DeepTuple)); |
| 40 | first_item(NotTuple) -> |
| 41 | NotTuple. |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 42 | |
| 43 | unnest_record(Term, RecordTag) -> |
| 44 | case is_record(Term, RecordTag) of |
Christopher Piro | 3b63fe4 | 2007-10-19 21:34:31 +0000 | [diff] [blame] | 45 | true -> |
| 46 | {ok, Term}; |
| 47 | false when is_tuple(Term) -> |
| 48 | unnest_record(element(1, Term), RecordTag); |
| 49 | _ -> |
| 50 | error |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 51 | end. |