-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.html
298 lines (286 loc) · 9.69 KB
/
index.html
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
286
287
288
289
290
291
292
293
294
295
296
297
298
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="" />
<meta name="author" content="Maxim Sokhatsky" />
<title>RPC</title>
<link rel="stylesheet" href="https://n2o.dev/blank.css" />
<link rel="stylesheet" href="https://n2o.dev/zima.css" />
</head>
<body>
<nav>
<a href="https://n2o.dev/">DEV</a>
<a href="https://rpc.n2o.dev/" style="background:#ededed;">RPC</a>
<div class="dropdown">
<a onclick="drop()" class="dropbtn">EN</a>
<div id="dropdown" class="dropdown-content">
<a href="https://n2o.dev/deps/rpc/man/ua/index.html">UA</a>
<a href="https://n2o.dev/deps/rpc/man/de/index.html">DE</a>
<a href="https://rpc.n2o.dev/">EN</a>
</div>
</div>
</nav>
<header>
<a href="https://github.com/synrc/bert"><img src="https://openmoji.org/data/color/svg/260E.svg"/></a>
<h1>RPC</h1>
</header>
<aside>
<article>
<section>
<h3>SYNOPSIS</h3>
<div>RPC is a set of parse_transform modules that produce various encode/decode generators
from Erlang HRL file type specifications. This is the most Erlang way possible.
Supported Generators:<br/><br/><div>
<div>
<ul>
<li>Erlang validation according to Type Spec (BERT)</li>
<li>JavaScript (BERT)</li>
<li>Swift (BERT)</li>
<li>Google Protobuf Specification (PROTOv3)</li>
</ul>
</div>
</section>
</article>
</aside>
<main>
<section>
<a name="intro"></a><h3>INTRO</h3>
<p>
As you may know, Erlang has its own binary encoding BERT inside its virtual machine, called BEAM.
For enterprise RPC usually, you use protobuf or MessagePack or Thrift or ASN.1 binary parser generators.
However, as you may know Erlang is not so fast in any tasks except moving binaries between sockets.
So we at Synrc usually use native Erlang BERT encoding on all clients with zero encoding/decoding on server side.
</p>
<p>
The encoders/decoders could be of two types:
strict (with checking the model for particular type signature with Sums and Products)
and general which encode/decode anything that can be translated into correct encoding.
For example, JavaScript encode/decode generator presented in this repo is just like that
(it doesn’t check types and constants, given in Erlang HRL files).
However, Swift version has the ability to check encoded/decoded term to comply the Erlang Type Specification.
</p>
</section>
<section>
<a name="samples"></a><h3>Samples</h3>
<p>
First, you can choose the language which is not presented in this repo
and try to implement your own BERT enc/dec generator for this language
using Swift (Type Spec precise) and JavaScript (open relay) generator as examples.
</p>
<p>
The aim of this contest is to create encoders/decoders for each language
and make bridges to other protocol descriptive formats like Can’n’Proto or protobuf!
</p>
</section>
<section>
<a name="ehrl"></a><h3>Erlang HRL</h3>
<figure>
<code>
-record(error, { code=[] :: [] | binary() }).
</code>
</figure>
<figure>
<code>
-record(ok, { code=[] :: [] | binary() }).
</code>
</figure>
<figure>
<code>
-record(io, { code=[] :: [] | #ok{} | #error{},
data=[] :: [] | <<>>
| { atom(), binary() | integer() } }).
</code>
</figure>
</section>
<section>
<a name="smodel"></a><h3>Swift Model</h3>
<figure>
<code>
class Err {
var code: AnyObject?
}
</code>
</figure>
<figure>
<code>
class Ok {
var code: AnyObject?
}
</code>
</figure>
<figure>
<code>
class Io {
var code: AnyObject?
var data: AnyObject?
}
</code>
</figure>
</section>
<section>
<a name="sspec"></a><h3>Swift Spec</h3>
<figure>
<code>
Chain(types: [
Model(value:Tuple(name: "io", body: [
Model(value:Chain(types: [
Model(value: Tuple(name:"ok", body: [
Model(value:Atom())])),
Model(value: Tuple(name:"error", body: [
Model(value:Atom())]))])),
Model(value:Tuple(name:"", body:[
Model(value:Atom()),
Model(value:Chain(types: [
Model(value:Binary()),
Model(value:Number())]))]))])) ])
</code>
</figure>
</section>
<section>
<a name="js"></a><h3>JavaScript</h3>
<figure>
<code>
function check() {
var res = true;
//@TODO: MORE TEST DATA
testData = [
1,
[1, 2, 3],
"string",
{tup: 'io', code: 'login', data: {tup: '$', 0: 'Auth', 1: 12}},
{tup: 'io', code: 'login', data: {tup: 'Auth'}},
{tup: 'io', code: 'login', data: {tup: '$', 0: 'doe', 1: 12}},
{tup: 'Roster', userlist: [{tup: 'Contact'}], status: 'get'},
{tup: 'p2p', from: 'john', to: 'doe'},
{tup: 'Profile', accounts: [1], status: 'maxim'}
];
testData.forEach(function (o) {
var o = JSON.stringify(o);
var d = JSON.stringify(
decode(dec(enc(encode(o)).buffer)))
.replace(/\\/g, '');
if (JSON.stringify(o) != JSON.stringify(
decode(dec(enc(encode(o)).buffer)))) {
console.log("Original: " + o + " #&60;=&&62; Decode: "
+ d + " %c [Error]", "color: red");
res = false;
} else {
console.log("Data: " + o + " %c [OK]", "color: green");
}
});
return res;
}
</code>
</figure>
</section>
<section>
<a name="samplep"></a><h3>Protobuf Sample</h3>
<p>Erlang BERT/HRL (source):</p>
<figure>
<code>
-type authType() :: google_api | facebook_api | mobile |
email | voice | resend | verify |
push | logout | get | delete | clear.
-type authStatus() :: invalid_version | mismatch_user_data |
number_not_allowed | session_not_found |
attempts_expired | invalid_sms_code |
invalid_jwt_code | permission_denied |
invalid_data.
-record('Feature', { id = [] :: [] | binary(),
key = [] :: [] | binary(),
value = [] :: [] | binary(),
group = [] :: [] | binary()}).
-record('Auth', { client_id = [] :: [] | binary(),
dev_key = [] :: [] | binary(),
user_id = [] :: [] | binary(),
phone = [] :: [] | binary(),
token = [] :: [] | binary(),
type = email :: authStatus(),
sms_code = [] :: [] | binary(),
attempts = [] :: [] | integer(),
services = [] :: list(atom()),
settings = [] :: list(#'Feature'{}),
push = [] :: [] | binary(),
os = [] :: [] | ios | android | web,
created = [] :: [] | integer(),
last_online = [] :: [] | integer() }).
</code>
</figure>
<p>Proto V3 (target):</p>
<figure>
<code>
enum osEnum {
ios = 0;
android = 1;
web = 2;
}
enum authStatus {
invalid_version = 0;
mismatch_user_data = 1;
number_not_allowed = 2;
session_not_found = 3;
attempts_expired = 4;
invalid_sms_code = 5;
invalid_jwt_code = 6;
permission_denied = 7;
invalid_data = 8;
}
message Feature {
string id = 1;
string key = 2;
string value = 3;
string group = 4;
}
message Auth {
string client_id = 1;
string dev_key = 2;
string user_id = 3;
string phone = 4;
string token = 5;
authStatus type = 6;
string sms_code = 7;
int64 attempts = 8;
repeated google.protobuf.Any services = 9;
repeated Feature settings = 10;
string push = 11;
osEnum os = 12;
int64 created = 13;
int64 last_online = 14;
}
</code>
</figure>
</section>
<section>
<a name="run"></a><h3>Run</h3>
<figure>
<code>
$ mad com
==> "/Users/maxim/depot/synrc/bert"
Generated Protobuf Model: "priv/protobuf/authType.proto"
Generated Protobuf Model: "priv/protobuf/authStatus.proto"
Generated Protobuf Model: "priv/protobuf/Feature.proto"
Generated Protobuf Model: "priv/protobuf/Auth.proto"
Generated Protobuf Model: "priv/protobuf/AuthError.proto"
Compiling /src/bert_sample.erl
OK
</code>
</figure>
</section>
<section>
<a name="credits"></a><h3>Credits</h3>
<ul><li>Yuri Maslovsky — ERLANG</li>
<li>Maxim Sokhatsky — GOOGLE, JAVASCRIPT</li>
<li>Dmytro Boiko — ERLANG, JAVASCRIPT</li>
<li>Anton Makarov — SWIFT</li>
<li>Viacheslav Katsuba — JAVASCRIPT</li></ul>
</section>
</main>
<footer>
Made with <span class="heart">❤</span> to Erlang
</footer>
</body>
<script>function drop(){document.getElementById("dropdown").classList.toggle("show");}</script>
</html>