Skip to content

Commit c6a8d53

Browse files
committed
Added readme and license (apache v2).
1 parent c092e04 commit c6a8d53

File tree

5 files changed

+268
-0
lines changed

5 files changed

+268
-0
lines changed

LICENSE

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
Apache License, Version 2.0
2+
Apache License
3+
Version 2.0, January 2004
4+
http://www.apache.org/licenses/
5+
6+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7+
8+
1. Definitions.
9+
10+
"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
11+
12+
"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
13+
14+
"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
15+
16+
"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
17+
18+
"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
19+
20+
"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
21+
22+
"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
23+
24+
"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
25+
26+
"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
27+
28+
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
29+
30+
2. Grant of Copyright License.
31+
32+
Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
33+
34+
3. Grant of Patent License.
35+
36+
Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
37+
38+
4. Redistribution.
39+
40+
You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
41+
42+
You must give any other recipients of the Work or Derivative Works a copy of this License; and
43+
You must cause any modified files to carry prominent notices stating that You changed the files; and
44+
You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
45+
If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.
46+
You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
47+
48+
5. Submission of Contributions.
49+
50+
Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
51+
52+
6. Trademarks.
53+
54+
This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
55+
56+
7. Disclaimer of Warranty.
57+
58+
Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
59+
60+
8. Limitation of Liability.
61+
62+
In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
63+
64+
9. Accepting Warranty or Additional Liability.
65+
66+
While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
67+
68+
END OF TERMS AND CONDITIONS

README.md

+158
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
# rec2json
2+
3+
## Overview
4+
5+
Rec2json takes standard erlang source or include files, scans them for
6+
record definitions, and generates a module for each record to convert that
7+
record to and from the
8+
[proposed erlang json standard](http://www.erlang.org/eeps/eep-0018.html)
9+
10+
**This should be considered alpha software. Many of the features listed
11+
are not done**
12+
13+
## Features
14+
15+
Features that are not full implemented are marked with (WIP).
16+
17+
* Resulting modules can be used as parameterized modules or pure erlang.
18+
* (WIP) Type checking on json -> record conversion.
19+
* Atom 'undefined' fields in records optionally skipped or set to null.
20+
* Atom 'null' in json optionally converted to 'undefined'.
21+
* Post processing options on record -> json convertion.
22+
* (WIP) Seed json -> record conversion with a record.
23+
* (WIP) Nested json -> record and record -> json conversions for other
24+
records that have been compiled using rec2json.
25+
* Resulting modules have no dependency on rec2json.
26+
27+
## Compiling
28+
29+
./rebar get-deps compile
30+
31+
To run tests:
32+
33+
./rebar eunit ct skip_deps=true
34+
35+
## Compiling Records
36+
37+
Full implemented is the scan_file and scan_string functions. The scan file
38+
processes the given file, while scan_string can only process a single record
39+
definition given as a string.
40+
41+
rec2json_compile:scan_file("src_file.hrl", []).
42+
rec2json_compile:scan_string("-record(a, {b = 1 :: integer()}).", []).
43+
44+
The Options (empty list at the end) are:
45+
46+
* {imports_dir, "path/to/imports"}
47+
* {output_dir, "path/to/ebin"}
48+
49+
Multple files can be scanned by either using wildcards or listing them:
50+
51+
rec2json_compile:scan_files("include/*.hrl", Options).
52+
rec2json_compile:scan_files(["rec.hrl", "other_rec.hrl"], Options).
53+
54+
To generate a stand-alone script for compiling:
55+
56+
./rebar escriptize skip_deps=true
57+
58+
This will create a "rec2json" script that can be run:
59+
60+
./rec2json -src=include/*.hrl -dest=ebin -include=include
61+
62+
## Use
63+
64+
The records are unchanged and can be used normally. The module to convert
65+
the record to and from json has the same name as the record. A record can
66+
also be used as a paramterized module, making is simple to use with the
67+
[erlydtl Django templates](https://github.com/evanmiller/erlydtl) project.
68+
69+
The given examples use the following record and record defintion:
70+
71+
-record(persion {
72+
name :: binary(),
73+
age = 0 :: pos_integer(),
74+
spouse :: #person{}
75+
}).
76+
Record = #person{ name = <<"John">>, age = 32, spouse = undefined }.
77+
78+
### to_json
79+
80+
To convert a record to a json structure:
81+
82+
Json = Record:to_json().
83+
Json = person:to_json(Record).
84+
[{name, <<"John">>}, {age, 32}].
85+
86+
The to_json function can take a list of mutators. Mutators are applied in
87+
the order listed except {null_is_undefined}. Supported mutators are:
88+
89+
* Turn undefined into null instead of skipping the field
90+
Record:to_json([{null_is_undefined}]).
91+
person:to_json(Record, [{null_is_undefined}]).
92+
93+
* Add a property
94+
Record:to_json([{single, true}]).
95+
person:to_json(Record, [{single, true}]).
96+
97+
* Remove a property
98+
Record:to_json([age]).
99+
person:to_json(Record, [age]).
100+
101+
* Modify based only on the json
102+
ModFunc = fun(Json) ->
103+
case proplists:get_value(spouse, Json) of
104+
undefined ->
105+
[{single, true} | Json];
106+
_ ->
107+
[{single, false} | Json]
108+
end
109+
end.
110+
Record:to_json([ModFunc]).
111+
person:to_json(Record, [ModFunc]).
112+
113+
* Modify based on both json and record
114+
ModFunc = fun(Json, Record) ->
115+
case Record#person.spouse of
116+
undefined ->
117+
[{single, true} | Json];
118+
_ ->
119+
[{single, false} | Json]
120+
end
121+
end.
122+
Record:to_json([ModFunc]).
123+
person:to_json(Record, [ModFunc]).
124+
125+
### from_json
126+
127+
Converting from a json structure to a record is just as simple:
128+
129+
{ok, Record} = person:from_json([
130+
{<<"name">>, <<"John">>},
131+
{<<"age">>, 32},
132+
{<<"spouse">>, null}
133+
]).
134+
135+
It may be desireable to change 'null' into 'undefined' in the record:
136+
137+
{ok, Record} = person:from_json(Json, [null_is_undefined]).
138+
139+
It may be desireable to start with an existing record instead of creating
140+
a new one:
141+
142+
{ok, Record2} = Record:from_json(Json).
143+
{ok, Record2} = person:from_json(Json, Record).
144+
{ok, Record2} = Record:from_json(Json, [null_is_undefined]).
145+
{ok, Record2} = person:from_json(Json, Record, [null_is_undefined]).
146+
147+
If the json struction has a type that connot be reconciled with a type
148+
specified by the record definition, a list of fields with possible errors
149+
is returned. The record will have the data that was in the json structure.
150+
An untyped record field is the same as having the type 'any()'. There are
151+
no warings about missing properties in the json, they simply retain the
152+
default value of the record.
153+
154+
{ok, Record, [age]} = person:from_json([{<<"age">>, <<"32">>}]).
155+
156+
## Contributing
157+
158+
Fork and submit a pull request with relevent tests.

src/rec2json.app.src

+14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
%% Copyright 2012 Micah Warren
2+
%%
3+
%% Licensed under the Apache License, Version 2.0 (the "License");
4+
%% you may not use this file except in compliance with the License.
5+
%% You may obtain a copy of the License at
6+
%%
7+
%% http://www.apache.org/licenses/LICENSE-2.0
8+
%%
9+
%% Unless required by applicable law or agreed to in writing, software
10+
%% distributed under the License is distributed on an "AS IS" BASIS,
11+
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
%% See the License for the specific language governing permissions and
13+
%% limitations under the License.
14+
115
{application, rec2json,
216
[
317
{description, "Library for parsing records and generating utility modules."},

src/rec2json.erl

+14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
%% Copyright 2012 Micah Warren
2+
%%
3+
%% Licensed under the Apache License, Version 2.0 (the "License");
4+
%% you may not use this file except in compliance with the License.
5+
%% You may obtain a copy of the License at
6+
%%
7+
%% http://www.apache.org/licenses/LICENSE-2.0
8+
%%
9+
%% Unless required by applicable law or agreed to in writing, software
10+
%% distributed under the License is distributed on an "AS IS" BASIS,
11+
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
%% See the License for the specific language governing permissions and
13+
%% limitations under the License.
14+
115
-module(rec2json).
216

317
-export([parse_file/1,parse_file/2]).

src/rec2json_compile.erl

+14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
%% Copyright 2012 Micah Warren
2+
%%
3+
%% Licensed under the Apache License, Version 2.0 (the "License");
4+
%% you may not use this file except in compliance with the License.
5+
%% You may obtain a copy of the License at
6+
%%
7+
%% http://www.apache.org/licenses/LICENSE-2.0
8+
%%
9+
%% Unless required by applicable law or agreed to in writing, software
10+
%% distributed under the License is distributed on an "AS IS" BASIS,
11+
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
%% See the License for the specific language governing permissions and
13+
%% limitations under the License.
14+
115
-module(rec2json_compile).
216

317
-export([scan_file/2, scan_string/2]).

0 commit comments

Comments
 (0)