You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
XDR.QuadFloat # Section 4.8, not supported for 128-byte size.
50
50
XDR.Const # Section 4.17, can be replaced with elixir constants.
51
-
XDR.Typedef # Section 4.18, may be implemented with elixir modules. More info bellow in this guude.
51
+
XDR.Typedef # Section 4.18, may be implemented with elixir modules. More info bellow in this guide.
52
52
```
53
53
54
-
## How to implement a XDR type?
54
+
## Better without macros
55
+
56
+
`It is an Open Source project, not a code that only I understand`
57
+
58
+
Macros are harder to write than ordinary Elixir functions, implementing them increases the code complexity which is not good especially if you are planning to build an Open Source code easy to understand to everyone. We decided to go without macros, we want to let everyone to expand or implement their own XDR types with a clear model based on Elixir functions.
59
+
60
+
## How to implement an XDR type?
55
61
**Behavior is the key**. When implementing a new XDR type follow this [Behavior's Declaration](https://github.com/kommitters/elixir_xdr/blob/develop/lib/xdr/declaration.ex).
As mentioned before all the XDR types follow the same [Behavior's Declaration](https://github.com/kommitters/elixir_xdr/blob/develop/lib/xdr/declaration.ex)
72
+
73
+
### Integer
66
74
For encoding integers use `encode_xdr/2` or use the raising version of the function `encode_xdr!/2`.
As mentioned before all the XDR types follow the same [Behavior's Declaration](https://github.com/kommitters/elixir_xdr/blob/develop/lib/xdr/declaration.ex)
91
+
### Unsigned Integer
92
+
93
+
Represents integer values in a range of `[0, 4294967295]`.
94
+
95
+
For encoding
96
+
```elixir
97
+
iex>XDR.UInt.new(564) |>XDR.UInt.encode_xdr()
98
+
{:ok, <<0, 0, 2, 52>>}
99
+
100
+
iex>XDR.UInt.new(564) |>XDR.UInt.encode_xdr!()
101
+
<<0, 0, 2, 52>>
102
+
103
+
```
104
+
105
+
For decoding
106
+
```elixir
107
+
iex>XDR.UInt.decode_xdr(<<0, 0, 2, 52>>)
108
+
{:ok, {%XDR.UInt{datum:564}, <<>>}}
109
+
110
+
iex>XDR.UInt.decode_xdr!(<<0, 0, 2, 52>>)
111
+
{%XDR.UInt{datum:564}, <<>>}
112
+
```
113
+
114
+
### Enumeration
115
+
116
+
Represents subsets of integers.
117
+
118
+
**Implementation**
119
+
120
+
Enums are keywords lists containing a set of **declarations (statically defined)** and a **identifier** with the key of the selected declaration. The [XDR.Bool](https://github.com/kommitters/elixir_xdr/blob/develop/lib/xdr/bool.ex) is a clear example of an Enum implementation.
FixedOpaque is used for fixed-length uninterpreted data that needs to be passed among machines, in other words, let's think on a string that must match a fixed length.
An example is available here: [Struct Type](https://github.com/kommitters/elixir_xdr/wiki/Struct-example).
156
372
157
-
158
-
### XDR.Union
373
+
### Union
159
374
A union is a type composed of a discriminant **(Statement)** followed by a type selected from a set of prearranged types **(UnionStatement)**. The type of discriminant is either "Int", "Unsigned Int", or an Enumerated type, such as "Bool". The **(UnionStatement)** types are called "arms" of the union and are preceded by the value of the discriminant that implies their encoding.
An example is available here: [Union Example](https://github.com/kommitters/elixir_xdr/wiki/Union-example)
170
385
171
-
### XDR.Optional
386
+
### Void
387
+
388
+
Represents the void types or nil in elixir case
389
+
390
+
For encoding
391
+
```elixir
392
+
iex>XDR.Void.new(nil) |>XDR.Void.encode_xdr()
393
+
{:ok, <<>>}
394
+
395
+
iex>XDR.Void.new(nil) |>XDR.Void.encode_xdr!()
396
+
<<>>
397
+
```
398
+
For decoding
399
+
```elixir
400
+
iex>XDR.Void.decode_xdr(<<>>)
401
+
{:ok, {nil, <<>>}}
402
+
403
+
iex>XDR.Void.decode_xdr!(<<>>)
404
+
{nil, <<>>}
405
+
```
406
+
407
+
### Optional
408
+
Think that you are filling out a form and it has optional fields such as the phone number if you do not want to fill this field you can leave it empty and the field will have a nil value, on the contrary, if you want to fill it out you can do it and it will take the indicated value
0 commit comments