blob: 1cbacc02ed545813be3dbcea6d3b9e854e532eff [file] [log] [blame]
Christopher Piro5b3a8f72007-08-01 22:27:37 +00001%%% Copyright (c) 2007- Facebook
2%%% Distributed under the Thrift Software License
Christopher Pirode11d852007-11-18 02:10:20 +00003%%%
Christopher Piro5b3a8f72007-08-01 22:27:37 +00004%%% See accompanying file LICENSE or visit the Thrift site at:
5%%% http://developers.facebook.com/thrift/
6
Christopher Piro5f5fdf32007-07-25 22:41:00 +00007-module(thrift_utils).
8
Christopher Piro5b3a8f72007-08-01 22:27:37 +00009-include("transport/tTransportException.hrl").
Christopher Piro5f5fdf32007-07-25 22:41:00 +000010
Christopher Piro5b3a8f72007-08-01 22:27:37 +000011-export([tabulate/2, dict_size/1, sformat/2, unbrack/1, first_item/1, unnest_record/2]).
Christopher Piro5f5fdf32007-07-25 22:41:00 +000012
Christopher Piro5b3a8f72007-08-01 22:27:37 +000013%% tabulate
14tabulate(N,F) ->
15 tabulate(0, N, F).
16
17tabulate(N,M,_) when N==M ->
18 [];
19tabulate(N,M,F) ->
20 [F(N) | tabulate(N+1, M, F)].
21
22%% makin me sad
Christopher Piro5f5fdf32007-07-25 22:41:00 +000023dict_size(Dict) ->
24 dict:fold(fun (_,_,I) -> I+1 end,0,Dict).
Christopher Piro5b3a8f72007-08-01 22:27:37 +000025
26%% I CAN HAS EAZIER KTHX
27sformat(Format, Data) when is_list(Data) ->
28 lists:flatten(io_lib:format(Format, Data));
29sformat(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
34unbrack(List) ->
35 List1 = sformat("~w", [List]),
36 string:substr(List1, 2, length(List1)-2).
37
Christopher Piro3b63fe42007-10-19 21:34:31 +000038first_item(DeepTuple) when is_tuple(DeepTuple) ->
39 first_item(element(1, DeepTuple));
40first_item(NotTuple) ->
41 NotTuple.
Christopher Piro5b3a8f72007-08-01 22:27:37 +000042
43unnest_record(Term, RecordTag) ->
44 case is_record(Term, RecordTag) of
Christopher Piro3b63fe42007-10-19 21:34:31 +000045 true ->
46 {ok, Term};
47 false when is_tuple(Term) ->
48 unnest_record(element(1, Term), RecordTag);
49 _ ->
50 error
Christopher Piro5b3a8f72007-08-01 22:27:37 +000051 end.