forked from stellar/js-xdr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunion.js
More file actions
39 lines (32 loc) · 740 Bytes
/
union.js
File metadata and controls
39 lines (32 loc) · 740 Bytes
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
import * as XDR from '../src';
let xdr = XDR.config((xdr) => {
xdr.union('Result', {
switchOn: xdr.lookup('ResultType'),
switches: [
['ok', xdr.void()],
['error', 'message']
],
// defaultArm: xdr.void(),
arms: {
message: xdr.string(100)
}
});
xdr.enum('ResultType', {
ok: 0,
error: 1,
nonsense: 2
});
});
let r = xdr.Result.ok();
r.set('error', 'this is an error');
r.message(); // => "this is an error"
r.get('message'); // => "this is an error"
r.set(xdr.ResultType.ok());
r.get(); // => undefined
// r.set("nonsense");
r.get(); // => undefined
let output = r.toXDR();
let parsed = xdr.Result.fromXDR(output);
console.log(r);
console.log(r.arm());
console.log(parsed);