diff --git a/lib/xdrgen/generators/elixir.rb b/lib/xdrgen/generators/elixir.rb index 959acb22d..f337acf3c 100644 --- a/lib/xdrgen/generators/elixir.rb +++ b/lib/xdrgen/generators/elixir.rb @@ -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 diff --git a/spec/output/generator_spec_elixir/test.x/opt_hash1.ex b/spec/output/generator_spec_elixir/test.x/opt_hash1.ex index b588e1006..9328524b2 100644 --- a/spec/output/generator_spec_elixir/test.x/opt_hash1.ex +++ b/spec/output/generator_spec_elixir/test.x/opt_hash1.ex @@ -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 @@ -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 @@ -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 diff --git a/spec/output/generator_spec_elixir/test.x/opt_hash2.ex b/spec/output/generator_spec_elixir/test.x/opt_hash2.ex index 1fe8f376a..5700781b0 100644 --- a/spec/output/generator_spec_elixir/test.x/opt_hash2.ex +++ b/spec/output/generator_spec_elixir/test.x/opt_hash2.ex @@ -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 @@ -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 @@ -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 diff --git a/spec/output/generator_spec_elixir/test.x/optional_hash.ex b/spec/output/generator_spec_elixir/test.x/optional_hash.ex new file mode 100644 index 000000000..44414348f --- /dev/null +++ b/spec/output/generator_spec_elixir/test.x/optional_hash.ex @@ -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