0%

erlang kvlist,record互转

目前项目中将record结构转换为KVList, 然后保存到mongodb中
由于record_info(fields,Record)不支持传入参数,处理从record转换为kvlist比较麻烦

目前做法

目前的做法是罗列所有的record,如下:

1
2
3
4
5
6
7
8
9
save(Rec) when is_record(Rec, aaa) ->
KvList = lists:zip(erlang:tl(tuple_to_list(Rec), record_info(fields, aaa)),
%% 保存到mongodb
save_db(KvList);
save(Rec) when is_record(Rec, bbb) ->
KvList = lists:zip(erlang:tl(tuple_to_list(Rec), record_info(fields, bbb)),
%% 保存到mongodb
save_db(KvList);
...

如果存在#aaa{id = #bbb{}}这种结构,需要一层一层的手动解析

1
2
3
4
5
6
7
8
9
-define(REC_TO_KVLIST(Rec, RecName),
lists:zip(erlang:tl(tuple_to_list(Rec)), record_info(fields, RecName))
).

A = #aaa{id = #bbb{}},
A1 = A#aaa{id = ?REC_TO_KVLIST(A#aaa.id, bbb)},
A2 = ?REC_TO_KVLIST(A1, aaa),
保存到mongodb中
save_db(A2)

相应的,从mongodb中读取数据,也要进行多次转换
稍微不注意就会出现db升级失败,数据丢失

可能的解决方法(未在项目中使用)

归根到底还是record_info,不支持传入参数
搜索github, 发现record_info_runtime

erl文件
record_info_runtime.erl Parse transform for using record_info(size, _) and record_info(fields, _) during runtime
增加了record_init/1
l2r.erl record,kvlist互转
rec_a.hrl 测试头文件

运行结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Eshell V6.3  (abort with ^G)
1> rr("rec_a.hrl").
[name,person]
2> KV = l2r:rec_to_kvlist(#person{}).
[{'__record_name__',person},
{id,0},
{name,[{'__record_name__',name},{first,"aaa"},{last,"bbb"}]},
{names,[[{'__record_name__',name},{first,"abc"},{last,[]}],
[{'__record_name__',name},{first,[]},{last,"def"}]]},
{sex,0}]
3> l2r:kvlist_to_rec(KV).
#person{id = 0,
name = #name{first = "aaa",last = "bbb"},
names = [#name{first = "abc",last = []},
#name{first = [],last = "def"}],
sex = 0}
4> #person{}.
#person{id = 0,
name = #name{first = "aaa",last = "bbb"},
names = [#name{first = "abc",last = []},
#name{first = [],last = "def"}],
sex = 0}
5>

最后贴代码凑字数

record_info_runtime.erlview raw
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
%-*-Mode:erlang;coding:utf-8;tab-width:4;c-basic-offset:4;indent-tabs-mode:()-*-
% ex: set ft=erlang fenc=utf-8 sts=4 ts=4 sw=4 et:
%%%
%%%------------------------------------------------------------------------
%%% @doc
%%% ==Record Info Runtime Parse Transform==
%%% Insert private functions into a module if the module contains any
%%% record definitions (including those from header files). The private
%%% functions inserted are record_info_size/1 and record_info_fields/1, so
%%% the record_info(size, _) and record_info(fields, _) can be called at
%%% runtime, but without exported functions. The unused function warning
%%% is automatically turned off for the inserted functions, so they
%%% don't necessarily need to be used.
%%% @end
%%%
%%% BSD LICENSE
%%%
%%% Copyright (c) 2013, Michael Truog <mjtruog at gmail dot com>
%%% All rights reserved.
%%%
%%% Redistribution and use in source and binary forms, with or without
%%% modification, are permitted provided that the following conditions are met:
%%%
%%% * Redistributions of source code must retain the above copyright
%%% notice, this list of conditions and the following disclaimer.
%%% * Redistributions in binary form must reproduce the above copyright
%%% notice, this list of conditions and the following disclaimer in
%%% the documentation and/or other materials provided with the
%%% distribution.
%%% * All advertising materials mentioning features or use of this
%%% software must display the following acknowledgment:
%%% This product includes software developed by Michael Truog
%%% * The name of the author may not be used to endorse or promote
%%% products derived from this software without specific prior
%%% written permission
%%%
%%% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
%%% CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
%%% INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
%%% OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
%%% DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
%%% CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
%%% SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
%%% BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
%%% SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
%%% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
%%% WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
%%% NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
%%% OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
%%% DAMAGE.
%%%
%%% @author Michael Truog <mjtruog [at] gmail (dot) com>
%%% @copyright 2013 Michael Truog
%%% @version 1.0.0 {@date} {@time}
%%%------------------------------------------------------------------------

-module(record_info_runtime).
-author('mjtruog [at] gmail (dot) com').

-export([parse_transform/2]).

-record(state,
{
records = [], % stored in reverse order
types = dict:new()
}).

parse_transform(Forms, _CompileOptions) ->
%% {ok, IO} = file:open("a.txt", [write]),
%% io:format(IO, "~p", [Forms]),
%% io:close(IO),
forms_process(Forms, [], #state{}).

forms_process([{eof, _} = EOF], L,
#state{records = []}) ->
% nothing to do (no records found)
lists:reverse([EOF | L]);
forms_process([{eof, _} = EOF], L,
#state{records = Records,
types = Types}) ->
[{attribute, _, file, _} = FILE |
NewForms] = lists:reverse([EOF,
record_init(Records),
record_info_size(Records),
record_info_fields(Records),
record_info_fieldtypes(Records, Types),
record_new(Records),
records(Records) | L]),
[FILE, record_info_f_nowarn() | NewForms];
forms_process([{attribute, _Line, record,
{Name, _Fields}} = H | Forms], L,
#state{records = Records} = State) ->
forms_process(Forms, [H | L],
State#state{records = [Name | Records]});
forms_process([{attribute, _Line, type,
{{record, Name}, Fields, _}} = H | Forms], L,
State) ->
forms_process(Forms, [H | L],
type_record_fields(Fields, Name, State));
forms_process([H | Forms], L, State) ->
forms_process(Forms, [H | L], State).

record_info_f_nowarn() ->
{attribute, 1, compile,
{nowarn_unused_function,
[{records, 0},
{record_new, 1},
{record_info_fieldtypes, 1},
{record_info_fields, 1},
{record_init, 1},
{record_info_size, 1}]}}.

records(Records) ->
{function, 1, records, 0,
[{clause, 1, [], [],
[records_f(lists:reverse(Records))]}]}.

records_f([Name]) ->
{cons, 1, {atom, 1, Name}, {nil, 1}};
records_f([Name | Records]) ->
{cons, 1, {atom, 1, Name}, records_f(Records)}.

record_new(Records) ->
{function, 1, record_new, 1,
record_new_f([], Records)}.

record_new_f(L, []) ->
L;
record_new_f(L, [Name | Records]) ->
record_new_f([{clause, 1,
[{atom, 1, Name}], [],
[{record, 1, Name, []}]} | L], Records).

record_info_fieldtypes(Records, Types) ->
{function, 1, record_info_fieldtypes, 1,
record_info_fieldtypes_f([], Records, Types)}.

record_info_fieldtypes_f_entry_types([]) ->
{nil, 1};
record_info_fieldtypes_f_entry_types([TypeInfo | TypeList]) ->
{cons, 1,
record_info_fieldtypes_f_entry_typeinfo(TypeInfo, false),
record_info_fieldtypes_f_entry_types(TypeList)}.

record_info_fieldtypes_f_entry_typeinfo_type({M, T})
when is_atom(M), is_atom(T) ->
{tuple, 1, [{atom, 1, M}, {atom, 1, T}]};
record_info_fieldtypes_f_entry_typeinfo_type(T)
when is_atom(T) ->
{atom, 1, T}.

record_info_fieldtypes_f_entry_typeinfo({Type, []}, false) ->
record_info_fieldtypes_f_entry_typeinfo_type(Type);
record_info_fieldtypes_f_entry_typeinfo({Type, TypeList}, _) ->
{tuple, 1,
[record_info_fieldtypes_f_entry_typeinfo_type(Type),
record_info_fieldtypes_f_entry_types(TypeList)]};
record_info_fieldtypes_f_entry_typeinfo(Type, _)
when is_atom(Type) ->
% e.g.: any
{atom, 1, Type};
record_info_fieldtypes_f_entry_typeinfo({Type, _Line, Constant}, _)
when is_atom(Type) ->
% e.g.: {integer, 1, 128}
{Type, 1, Constant};
record_info_fieldtypes_f_entry_typeinfo(Literal, _) ->
exit({badarg, Literal}).
%{string, 1, lists:flatten(io_lib:format("~p",[Literal]))}.

record_info_fieldtypes_f_entry(FieldName, undefined) ->
{tuple, 1,
[{atom, 1, FieldName},
{atom, 1, undefined}]};
record_info_fieldtypes_f_entry(FieldName, TypeInfo) ->
{tuple, 1,
[{atom, 1, FieldName},
record_info_fieldtypes_f_entry_typeinfo(TypeInfo, true)]}.

record_info_fieldtypes_f_entries([{FieldName, TypeInfo}]) ->
{cons, 1, record_info_fieldtypes_f_entry(FieldName, TypeInfo),
{nil, 1}};
record_info_fieldtypes_f_entries([{FieldName, TypeInfo} | L]) ->
{cons, 1, record_info_fieldtypes_f_entry(FieldName, TypeInfo),
record_info_fieldtypes_f_entries(L)}.

record_info_fieldtypes_f(L, [], _Types) ->
L;
record_info_fieldtypes_f(L, [Name | Records], Types) ->
ClauseBody = case dict:find(Name, Types) of
{ok, FieldTypes} ->
[record_info_fieldtypes_f_entries(lists:reverse(FieldTypes))];
error ->
[{lc, 1,
{tuple, 1,
[{var, 1, 'FieldName'},
{atom, 1, undefined}]},
[{generate, 1,
{var, 1, 'FieldName'},
{call, 1,
{atom, 1, record_info},
[{atom, 1, fields},
{atom, 1, Name}]}}]}]
end,
record_info_fieldtypes_f([{clause, 1,
[{atom, 1, Name}], [],
ClauseBody} | L], Records, Types).

record_init(Records) ->
{function, 1, record_init, 1,
record_init_f([], Records)}.

record_init_f(L, []) ->
L;
record_init_f(L, [Name|Records]) ->
record_init_f([{clause, 1,
[{atom, 1, Name}], [],
[{record,1,Name, []}]}|L], Records).

record_info_fields(Records) ->
{function, 1, record_info_fields, 1,
record_info_fields_f([], Records)}.

record_info_fields_f(L, []) ->
L;
record_info_fields_f(L, [Name | Records]) ->
record_info_fields_f([{clause, 1,
[{atom, 1, Name}], [],
[{call, 1,
{atom, 1, record_info},
[{atom, 1, fields},
{atom, 1, Name}]}]} | L], Records).

record_info_size(Records) ->
{function, 1, record_info_size, 1,
record_info_size_f([], Records)}.

record_info_size_f(L, []) ->
L;
record_info_size_f(L, [Name | Records]) ->
record_info_size_f([{clause, 1,
[{atom, 1, Name}], [],
[{call, 1,
{atom, 1, record_info},
[{atom, 1, size},
{atom, 1, Name}]}]} | L], Records).

type_record_field_info({type, _Line1, union,
[{atom, _Line2, undefined}, TypeInfo]}) ->
type_record_field_info(TypeInfo);
type_record_field_info({type, _Line, Type, any}) ->
{Type, [any]};
type_record_field_info({type, _Line, Type, TypeList}) ->
{Type, [type_record_field_info(T) || T <- TypeList]};
type_record_field_info({remote_type, _Line1,
[{atom, _Line2, Module},
{atom, _Line3, Type}, Arguments]}) ->
{{Module, Type}, Arguments};
type_record_field_info(Literal) ->
Literal.

type_record_fields([], _RecordName, State) ->
State;
type_record_fields([{record_field, _Line1,
{atom, _Line2, FieldName}, _Value} | Fields],
RecordName, #state{types = Types} = State) ->
Entry = {FieldName, undefined},
NewTypes = dict:update(RecordName, fun(L) ->
[Entry | L]
end, [Entry], Types),
type_record_fields(Fields, RecordName, State#state{types = NewTypes});
type_record_fields([{typed_record_field, RecordField, TypeInfo} | Fields],
RecordName, #state{types = Types} = State) ->
FieldName = case RecordField of
{record_field, _Line1,
{atom, _Line2, Name}, _Value} ->
Name;
{record_field, _Line1,
{atom, _Line2, Name}} ->
Name
end,
Entry = {FieldName, type_record_field_info(TypeInfo)},
NewTypes = dict:update(RecordName, fun(L) ->
[Entry | L]
end, [Entry], Types),
type_record_fields(Fields, RecordName, State#state{types = NewTypes}).
rec_a.hrlview raw
1
2
3
4
5
6
7
8
9
10
11
-record(name, {
first = "",
last = ""
}).

-record(person, {
id = 0
,name = #name{first="aaa", last="bbb"}
,names = [#name{first="abc"}, #name{last="def"}]
,sex = 0
}).
l2r.erlview raw
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
%% coding: latin-1
%%%-----------------------------------------------
%%% @doc
%%% record,kvlist互转
%%% 包含头文件即可
%%% @end
%%%-----------------------------------------------
-module(l2r).

-include("rec_a.hrl").

-define(RECORD_NAME, '__record_name__').

-define(ERROR_MSG(Format, Args), io:format("[ERROR]" ++ Format ++ "\n", Args)).
-define(INFO_MSG(Format, Args), io:format("[INFO]" ++ Format ++ "\n", Args)).

-compile([{parse_transform, record_info_runtime}]).

-export([
rec_to_kvlist/1,
kvlist_to_rec/1
]).

-export([
records/0,
record_info_fields/1,
record_info_size/1
]).

rec_to_kvlist(Rec) ->
rec_to_kvlist_f1(Rec, 1).

rec_to_kvlist_f1(Rec, Depth) when Depth > 10 ->
throw({error, Depth, Rec});
rec_to_kvlist_f1(Rec, Depth) when is_tuple(Rec) ->
case is_record(Rec) of
true ->
RecName = erlang:element(1, Rec),
Fields = [?RECORD_NAME|record_info_fields(RecName)],
Values = tuple_to_list(Rec),
Fun = fun({Field, Value}) when is_tuple(Value) ->
{Field, rec_to_kvlist_f1(Value, Depth + 1)};
({Field, Value}) when is_list(Value) ->
{Field, [rec_to_kvlist_f1(V, Depth + 1) || V <- Value]};
({Field, Value}) ->
{Field, Value}
end,
lists:map(Fun, lists:zip(Fields, Values));
false ->
Rec
end;
rec_to_kvlist_f1(Val, _) -> Val.

kvlist_to_rec(KvList) when is_list(KvList) ->
case proplists:get_value(?RECORD_NAME, KvList) of
undefined ->
Fun = fun(Val) -> kvlist_to_rec(Val) end,
lists:map(Fun, KvList);
RecName ->
InitList = lists:zip(
[?RECORD_NAME|record_info_fields(RecName)],
tuple_to_list(record_init(RecName))),
Fun = fun(Field) ->
case proplists:get_value(Field, KvList) of
undefined ->
proplists:get_value(Field, InitList);
Val when is_list(Val) ->
kvlist_to_rec(Val);
Val ->
Val
end
end,
list_to_tuple(lists:map(Fun, [?RECORD_NAME|record_info_fields(RecName)]))
end;
kvlist_to_rec(KvList) -> KvList.

is_record(Rec) when is_tuple(Rec), tuple_size(Rec) >= 1 ->
RecName = erlang:element(1, Rec),

is_list(record_info_fields(RecName))
andalso length(tuple_to_list(Rec)) =:= record_info_size(RecName);
is_record(_Other) -> false.