[erlang-questions] erlyweb accessing record fields

jm <>
Tue Jul 3 02:42:47 CEST 2007


Had another chance to look at this this morning. (It was about 22:30
last night when I got stuck and posted the last message). For anyone
else who comes across this in the future it was solve as follows.

Based on http://dcaoyuan.javaeye.com/blog/84022 I found out about
decompiling the beam files and did

{ok, {_, [{abstract_code, {_, AC}}]}} = beam_lib:chunks("ebin/mod.beam",
[abstract_code]).
{ok, S} = file:open("decompiled_mod.erl", write).
io:fwrite(S, "~s~n", [erl_prettypr:format(erl_syntax:form_list(AC))]).
file:close(S).

looking at the decompiled code,
....
name(Rec) -> erlydb_base:get(4, Rec).

name(Rec, Val) -> setelement(4, Rec, Val).
....
new(name, duration, logo_small, logo_flat,
    description) ->
    {mod, true, undefined, name, duration, logo_small,
     logo_flat, description}.
....

>From this it can be see I was over doing things, ie over processing the
results. From the original message the mod_controller.erl was


jm wrote:

> In mod_controller.erl
> -export([show/3]).
> 
> show(A, Model, Id) ->
>     Rec = Model:find_id(Id),
>     Fields = tl(Model:db_fields()),
>     Vals = tl(Model:to_iolist(Rec)),
>     FV = lists:zip(Fields, Vals),
>     %%    IdStr = case Model:id(Rec) of
>     %%         undefined -> [];
>     %%         Id -> integer_to_list(Id)
>     %%     end,
>     FieldData = [{erlydb_field:name_bin(Field),
>                   erlydb_field:html_input_type(Field),
>                   erlydb_field:modifier(Field),
>                   Val} || {Field, Val} <- FV],
>     {data, {erlyweb:get_app_root(A),
>             atom_to_list(Model),
>             Id,
>             yaws_arg:server_path(A),
>             FieldData}}.
> 

This can be reduced to

-export([show/3]).

show(A, Model, Id) ->
    Rec = Model:find_id(Id),
    %% this IdStr bit could also possible go
    IdStr = case Model:id(Rec) of
                undefined -> [];
                Id2 -> integer_to_list(Id2)
            end,
    {data, {erlyweb:get_app_root(A),
            atom_to_list(Model),
            IdStr,
            yaws_arg:server_path(A),
            Rec}}.

Jeff.



More information about the erlang-questions mailing list