Skip to content
This repository
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 143 lines (130 sloc) 4.558 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
%% -----------------------------------------------------------------------------
%%
%% Erlang System Monitoring Commons: Library API
%%
%% Copyright (c) 2010 Tim Watson (watson.timothy@gmail.com)
%%
%% Permission is hereby granted, free of charge, to any person obtaining a copy
%% of this software and associated documentation files (the "Software"), to deal
%% in the Software without restriction, including without limitation the rights
%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
%% copies of the Software, and to permit persons to whom the Software is
%% furnished to do so, subject to the following conditions:
%%
%% The above copyright notice and this permission notice shall be included in
%% all copies or substantial portions of the Software.
%%
%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
%% THE SOFTWARE.
%% -----------------------------------------------------------------------------
%% @author Tim Watson [http://hyperthunk.wordpress.com]
%% @copyright (c) Tim Watson, 2010
%% @since: March 2010
%%
%% @doc
%%
%% -----------------------------------------------------------------------------
-module(dxcommon).
-author('Tim Watson <watson.timothy@gmail.com>').
-include("dxcommon.hrl").

-compile(export_all).

record_to_proplist(R) ->
    case type(R) of
        {error, _Reason}=Err ->
            Err;
        {Type, Mod} ->
            record_to_proplist(Type, Mod, R)
    end.

record_to_proplist(Type, Mod, R) ->
    Members = Mod:'#info-'(Type, fields),
    PList = lists:zip(Members, [ Mod:'#get-'(M, R) || M <- Members ]),
    {Type, lists:map(fun enforce_fields/1, PList)}.

enforce_fields({Name, Value}=Pair) when is_tuple(Value) ->
    MaybeRecName = element(1, Value),
    case type(MaybeRecName) of
        {error, _} ->
            %% even if this isn't a record, we want a nested proplist
            case Value of
                {_, _}=KVP ->
                    {Name, [dx_json:jsonify(KVP)]};
                _ ->
                    dx_json:jsonify(Pair)
            end;
        {Type, Mod} ->
            case record_to_proplist(Type, Mod, Value) of
                {_, PList} ->
                    {Name, PList};
                Other ->
                    %% ????
                    {Name, Other}
            end
    end;
enforce_fields({_Name, _Value}=Pair) ->
    dx_json:jsonify(Pair).

%% TODO: deprecate this....
new_r(Type, Values) ->
    case type(Type) of
        {error, Reason} ->
            throw(Reason);
        {Type, Mod} ->
            Properties = lists:zip(type_members(Type, Mod), Values),
            apply(Mod, list_to_atom("#new-" ++ atom_to_list(Type)), [Properties])
    end.

new(Type, Properties) ->
    case type(Type) of
        {error, Reason} ->
            throw(Reason);
        {_, Mod} ->
            apply(Mod, list_to_atom("#new-" ++ atom_to_list(Type)), [Properties])
    end.

new(Type) ->
    case type(Type) of
        {error, Reason} ->
            throw(Reason);
        {_, Mod} ->
            case erlang:function_exported(Mod, 'new', 0) of
                true ->
                    Mod:new();
                false ->
                    apply(Mod, list_to_atom("#new-" ++ atom_to_list(Type)), [])
            end
    end.

members(Type) ->
    case type(Type) of
        {error, Reason} ->
            throw(Reason);
        {Type, Mod} ->
            type_members(Type, Mod)
    end.

type_members(Type, Mod) ->
    Mod:'#info-'(Type, fields).

%% TODO: generate this boilerplate away....

type(R) when is_tuple(R) ->
    case type(element(1, R)) of
        {error, _} ->
            {error, {unknown_type, R}};
        {T, Mod} ->
            get_type(Mod, T, R)
    end;
type(T) when is_atom(T) ->
    Mod = list_to_atom("dx_" ++ atom_to_list(T)),
    case code:ensure_loaded(Mod) of
        {error, embedded} ->
            {T, Mod};
        {error, _} ->
            {error, {unknown_type, T}};
        {module, _} ->
            {T, Mod}
    end.

get_type(Mod, T, R) ->
    case Mod:'#is_record-'(R) of
        true ->
            {T, Mod};
        false ->
            {error, {unknown_type, T}}
    end.
Something went wrong with that request. Please try again.