Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/xdrgen/generators/elixir.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1153,7 +1153,9 @@ def build_typedef(typedef, is_struct)

case type.sub_type
when :optional
build_optional_typedef(typedef, base_type, name)
render_other_type(typedef)
optional_name = type_reference(typedef, typedef.name.camelize)
build_optional_typedef(typedef, optional_name, name)
when :array
build_list_typedef(name, base_type, "FixedArray", type)
when :var_array
Expand Down
26 changes: 13 additions & 13 deletions spec/output/generator_spec_elixir/test.x/opt_hash1.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,29 @@ defmodule MyXDR.OptHash1 do

@behaviour XDR.Declaration

alias MyXDR.Hash
alias MyXDR.OptionalHash

@optional_spec XDR.Optional.new(Hash)
@optional_spec XDR.Optional.new(OptionalHash)

@type hash :: Hash.t() | nil
@type optional_hash :: OptionalHash.t() | nil

@type t :: %__MODULE__{hash: hash()}
@type t :: %__MODULE__{optional_hash: optional_hash()}

defstruct [:hash]
defstruct [:optional_hash]

@spec new(hash :: hash()) :: t()
def new(hash \\ nil), do: %__MODULE__{hash: hash}
@spec new(optional_hash :: optional_hash()) :: t()
def new(optional_hash \\ nil), do: %__MODULE__{optional_hash: optional_hash}

@impl true
def encode_xdr(%__MODULE__{hash: hash}) do
hash
def encode_xdr(%__MODULE__{optional_hash: optional_hash}) do
optional_hash
|> XDR.Optional.new()
|> XDR.Optional.encode_xdr()
end

@impl true
def encode_xdr!(%__MODULE__{hash: hash}) do
hash
def encode_xdr!(%__MODULE__{optional_hash: optional_hash}) do
optional_hash
|> XDR.Optional.new()
|> XDR.Optional.encode_xdr!()
end
Expand All @@ -42,7 +42,7 @@ defmodule MyXDR.OptHash1 do

def decode_xdr(bytes, optional_spec) do
case XDR.Optional.decode_xdr(bytes, optional_spec) do
{:ok, {%XDR.Optional{type: hash}, rest}} -> {:ok, {new(hash), rest}}
{:ok, {%XDR.Optional{type: optional_hash}, rest}} -> {:ok, {new(optional_hash), rest}}
{:ok, {nil, rest}} -> {:ok, {new(), rest}}
error -> error
end
Expand All @@ -53,7 +53,7 @@ defmodule MyXDR.OptHash1 do

def decode_xdr!(bytes, optional_spec) do
case XDR.Optional.decode_xdr!(bytes, optional_spec) do
{%XDR.Optional{type: hash}, rest} -> {new(hash), rest}
{%XDR.Optional{type: optional_hash}, rest} -> {new(optional_hash), rest}
{nil, rest} -> {new(), rest}
end
end
Expand Down
26 changes: 13 additions & 13 deletions spec/output/generator_spec_elixir/test.x/opt_hash2.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,29 @@ defmodule MyXDR.OptHash2 do

@behaviour XDR.Declaration

alias MyXDR.Hash
alias MyXDR.OptionalHash

@optional_spec XDR.Optional.new(Hash)
@optional_spec XDR.Optional.new(OptionalHash)

@type hash :: Hash.t() | nil
@type optional_hash :: OptionalHash.t() | nil

@type t :: %__MODULE__{hash: hash()}
@type t :: %__MODULE__{optional_hash: optional_hash()}

defstruct [:hash]
defstruct [:optional_hash]

@spec new(hash :: hash()) :: t()
def new(hash \\ nil), do: %__MODULE__{hash: hash}
@spec new(optional_hash :: optional_hash()) :: t()
def new(optional_hash \\ nil), do: %__MODULE__{optional_hash: optional_hash}

@impl true
def encode_xdr(%__MODULE__{hash: hash}) do
hash
def encode_xdr(%__MODULE__{optional_hash: optional_hash}) do
optional_hash
|> XDR.Optional.new()
|> XDR.Optional.encode_xdr()
end

@impl true
def encode_xdr!(%__MODULE__{hash: hash}) do
hash
def encode_xdr!(%__MODULE__{optional_hash: optional_hash}) do
optional_hash
|> XDR.Optional.new()
|> XDR.Optional.encode_xdr!()
end
Expand All @@ -42,7 +42,7 @@ defmodule MyXDR.OptHash2 do

def decode_xdr(bytes, optional_spec) do
case XDR.Optional.decode_xdr(bytes, optional_spec) do
{:ok, {%XDR.Optional{type: hash}, rest}} -> {:ok, {new(hash), rest}}
{:ok, {%XDR.Optional{type: optional_hash}, rest}} -> {:ok, {new(optional_hash), rest}}
{:ok, {nil, rest}} -> {:ok, {new(), rest}}
error -> error
end
Expand All @@ -53,7 +53,7 @@ defmodule MyXDR.OptHash2 do

def decode_xdr!(bytes, optional_spec) do
case XDR.Optional.decode_xdr!(bytes, optional_spec) do
{%XDR.Optional{type: hash}, rest} -> {new(hash), rest}
{%XDR.Optional{type: optional_hash}, rest} -> {new(optional_hash), rest}
{nil, rest} -> {new(), rest}
end
end
Expand Down
61 changes: 61 additions & 0 deletions spec/output/generator_spec_elixir/test.x/optional_hash.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
defmodule MyXDR.OptionalHash do
@moduledoc """
Automatically generated by xdrgen
DO NOT EDIT or your changes may be overwritten

Target implementation: elixir_xdr at https://hex.pm/packages/elixir_xdr

Representation of Stellar `OptionalHash` type.
"""

@behaviour XDR.Declaration

alias MyXDR.Hash

@optional_spec XDR.Optional.new(Hash)

@type hash :: Hash.t() | nil

@type t :: %__MODULE__{hash: hash()}

defstruct [:hash]

@spec new(hash :: hash()) :: t()
def new(hash \\ nil), do: %__MODULE__{hash: hash}

@impl true
def encode_xdr(%__MODULE__{hash: hash}) do
hash
|> XDR.Optional.new()
|> XDR.Optional.encode_xdr()
end

@impl true
def encode_xdr!(%__MODULE__{hash: hash}) do
hash
|> XDR.Optional.new()
|> XDR.Optional.encode_xdr!()
end

@impl true
def decode_xdr(bytes, optional_spec \\ @optional_spec)

def decode_xdr(bytes, optional_spec) do
case XDR.Optional.decode_xdr(bytes, optional_spec) do
{:ok, {%XDR.Optional{type: hash}, rest}} -> {:ok, {new(hash), rest}}
{:ok, {nil, rest}} -> {:ok, {new(), rest}}
error -> error
end
end

@impl true
def decode_xdr!(bytes, optional_spec \\ @optional_spec)

def decode_xdr!(bytes, optional_spec) do
case XDR.Optional.decode_xdr!(bytes, optional_spec) do
{%XDR.Optional{type: hash}, rest} -> {new(hash), rest}
{nil, rest} -> {new(), rest}
end
end

end