Tags field contains tag unique ids. I would like to display the name
of tags on a form instead of unique ids.
How about this:
-module(a).
-compile(export_all).
-record(expense, {uuid, amount, tags}).
show_action(#expense{uuid=UUID, amount=Amount, tags={A, B, C} }) ->
TagConversions= #{1 => "Joe", 2 => "Tammy", 3 => "Bob"},
A_Conv = maps:get(A, TagConversions, "Nathan"),
B_Conv = maps:get(B, TagConversions, "Nathan"),
C_Conv = maps:get(C, TagConversions, "Nathan"),
io:format("~w, ~w, {~s,~s,~s}~n",
[UUID, Amount, A_Conv, B_Conv, C_Conv]).
go() ->
Expense1 = #expense{uuid=1, amount=10, tags={1,2,3} },
show_action(Expense1).
In the shell:
12> c(a).
a.erl:2: Warning: export_all flag enabled - all functions will be exported
{ok,a}
13> a:go().
1, 10, {Joe,Tammy,Bob}
ok
I would like to have a displayable version of the record.
But it would create a lot of duplicated records.
Yes, but in OOP don't you have a proliferation of View objects that contain all or a subset of the data in the Model objects?
Options 1 Use the same record with different data format but I think
it would break the interface contract ; one will not be able to know
which version of the record one have.
You could structure the record to leave a blank spot for the tag names corresponding to the tag ids, then fill in the tag names when you are able:
-module(a).
-compile(export_all).
-record(tag, {id, name=""}).
-record(expense, {uuid, amount, tags}).
show_action(Expense = #expense{uuid=UUID, amount=Amount, tags={A, B, C} }) ->
TagConversions= #{1 => "Joe", 2 => "Tammy", 3 => "Bob"},
A_Conv = maps:get(A#tag.id, TagConversions, "Nathan"),
B_Conv = maps:get(B#tag.id, TagConversions, "Nathan"),
C_Conv = maps:get(C#tag.id, TagConversions, "Nathan"),
io:format("~w, ~w, {~s,~s,~s}~n",
[UUID, Amount, A_Conv, B_Conv, C_Conv]),
Expense#expense{tags={
A#tag{name=A_Conv},
B#tag{name=B_Conv},
C#tag{name=C_Conv}
}}.
go() ->
Expense1 = #expense{uuid=1, amount=10,
tags={#tag{id=1},
#tag{id=2},
#tag{id=3} }
},
show_action(Expense1).
In the shell:
5> c(a).
a.erl:2: Warning: export_all flag enabled - all functions will be exported
{ok,a}
6> a:go().
1, 10, {Joe,Tammy,Bob}
{expense,1,10,{{tag,1,"Joe"},{tag,2,"Tammy"},{tag,3,"Bob"}}}