[erlang-questions] Code critique please

Jarrod Roberson <>
Wed Dec 2 06:45:38 CET 2009


On Tue, Dec 1, 2009 at 9:36 AM, Hynek Vychodil <> wrote:
>
> I would prefer something like this:
>
> process_dnsrec(Sub,{ok,#dns_rec{anlist=Responses}}) ->
>    dict:map(fun(S, V) -> process_responses(S, V, Responses) end, Sub).
>
> % process the list of resource records one at a time
> process_responses(S, Value, Responses) ->
>    lists:foldl(fun(#dns_rr{domain = Domain} = Response, Val) ->
>        process_response(lists:suffix(S, Domain), Response, Val)
>    end, Value, Responses).
>
> process_response(false, _Response, Val) -> Val;
> process_response(true, #dns_rr{ttl = TTL} = Response, Val) when TTL == 0 ->
>    dict:new();
> process_response(true, #dns_rr{domain = Domain, type = Type, class =
> Class} = Response) ->
>    NewRR = Response#dns_rr{tm=get_timestamp()},
>    dict:store({Domain, Type, Class}, NewRR, Val).
>
> You have got pull request on hithub ;-)
>

thanks for the help, this really set off a lightbulb for me!

I was able to add TXT record data parsing with your example and the
other foldl example from Zoltan Lajos Kis
I came up with this on my own.

process_response(true, #dns_rr{domain = Domain, type = Type, class =
Class} = Response, Val) when Type == txt ->
    DTXT = lists:foldl(fun(T,D) -> [K,V] =
re:split(T,"=",[{return,list}]),dict:store(K,V,D)
end,dict:new(),Response#dns_rr.data),
    NewRR = Response#dns_rr{tm=get_timestamp(),data=DTXT},
    dict:store({Domain,Type,Class},NewRR,Val);

note: I had to get rid of the string:tokens since I ended up getting
things like "url=" and I was getting "bad_match" errors. So I had to
learn
someelse new, the re module! Next up is actually sending the Queries
to generate the responses without having to get iChat to broadcast
manually.

-- Jarrod Roberson
www.vertigrated.com/blog/


More information about the erlang-questions mailing list