Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function to generate an enum type #1

Merged
merged 5 commits into from
Apr 27, 2023
Merged
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
108 changes: 94 additions & 14 deletions lib/xdrgen/generators/elixir.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Xdrgen
module Generators
class Elixir < Xdrgen::Generators::Base
MAX_INT = (2**31) - 1

def generate
path = "#{@namespace}_generated.ex"
out = @output.open(path)
Expand Down Expand Up @@ -32,7 +33,7 @@ def render_definition(out, defn)
when AST::Definitions::Struct ;
render_struct out, defn
when AST::Definitions::Enum ;
render_enum out, defn
render_enum defn
when AST::Definitions::Union ;
render_union out, defn
when AST::Definitions::Typedef ;
Expand All @@ -49,43 +50,64 @@ def render_source_comment(out, defn)

out.puts <<-EOS.strip_heredoc
comment ~S"""
=== xdr source ============================================================

XDR Source Code::\n
EOS

out.puts " " + defn.text_value.split("\n").join("\n ")

out.puts <<-EOS.strip_heredoc
"""\n
EOS
end

def render_moduledoc(out)
out.puts <<-EOS.strip_heredoc
@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
"""
EOS
out.break
end

def render_moduledoc(out)
def render_moduledoc_enum_type(out, type)
out.puts <<-EOS.strip_heredoc
@moduledoc """
Automatically generated by xdrgen
DO NOT EDIT or your changes may be overwritten

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

Representation of Stellar `#{type.upcase_first}` type.
"""
EOS
out.break
out.puts "@behaviour XDR.Declaration\n\n"
end

def render_define_block(out)
out.puts "defmodule #{@namespace} do"
out.indent do
render_moduledoc(out)
out.puts "use XDR.Base\n\n"
end
yield
ensure
out.puts "end"
out.break
end

def render_define_block_enum_type(out, module_name)
out.puts "defmodule #{@namespace}.#{module_name.upcase_first} do"
out.indent do
render_moduledoc_enum_type(out, module_name)
end
yield
ensure
out.puts "end"
out.break
end

def render_typedef(out, typedef)
out.puts "define_type(\"#{name typedef}\", #{build_type_args typedef.declaration.type})"
Expand All @@ -105,14 +127,72 @@ def render_struct(out, struct)
out.puts ")"
end

def render_enum(out, enum)
out.puts "define_type(\"#{name enum}\", Enum,"
out.indent do
enum.members.each_with_index do |m, i|
out.puts "#{member_name m}: #{m.value}#{comma_unless_last(i, enum.members)}"
def render_enum(enum)
file_name = "#{enum.name.underscore.downcase}.ex"
out = @output.open(file_name)

render_define_block_enum_type(out, enum.name) do
out.indent do
out.puts "@declarations [\n"
out.indent do
enum.members.each_with_index do |m, i|
out.puts "#{m.name}: #{m.value}#{comma_unless_last(i, enum.members)}"
end
end
out.puts "]\n\n"

out.puts "@enum_spec %XDR.Enum{declarations: @declarations, identifier: nil}\n\n"

out.puts "@type t :: %__MODULE__{identifier: atom()}\n\n"

out.puts "defstruct [:identifier]\n\n"

out.puts "@spec new(type :: atom()) :: t()\n"
out.puts "def new(type \\\\ :#{enum.members.first.name}), do: %__MODULE__{identifier: type}\n\n"

out.puts "@impl true"
out.puts "def encode_xdr(%__MODULE__{identifier: type}) do\n"
out.indent do
out.puts "@declarations\n"
out.puts "|> XDR.Enum.new(type)\n"
out.puts "|> XDR.Enum.encode_xdr()\n"
end
out.puts "end\n\n"

out.puts "@impl true"
out.puts "def encode_xdr!(%__MODULE__{identifier: type}) do\n"
out.indent do
out.puts "@declarations\n"
out.puts "|> XDR.Enum.new(type)\n"
out.puts "|> XDR.Enum.encode_xdr!()\n"
end
out.puts "end\n\n"

out.puts "@impl true"
out.puts "def decode_xdr(bytes, spec \\\\ @enum_spec)\n\n"

out.puts "def decode_xdr(bytes, spec) do\n"
out.indent do
out.puts "case XDR.Enum.decode_xdr(bytes, spec) do\n"
out.indent do
out.puts "{:ok, {%XDR.Enum{identifier: type}, rest}} -> {:ok, {new(type), rest}}\n"
out.puts "error -> error\n"
end
out.puts "end\n"
end
out.puts "end\n\n"

out.puts "@impl true"
out.puts "def decode_xdr!(bytes, spec \\\\ @enum_spec)\n\n"

out.puts "def decode_xdr!(bytes, spec) do\n"
out.indent do
out.puts "{%XDR.Enum{identifier: type}, rest} = XDR.Enum.decode_xdr!(bytes, spec)\n"
out.puts "{new(type), rest}\n"
end
out.puts "end\n"
end
end
out.puts ")"
end

def render_union(out, union)
Expand Down Expand Up @@ -176,7 +256,7 @@ def const_name(named)
end

def member_name(member)
name(member).underscore
name(member).underscore.upcase
end

# this can be a string to reference a custom type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,17 @@ defmodule MyXDR do
Automatically generated by xdrgen
DO NOT EDIT or your changes may be overwritten

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

use XDR.Base

comment ~S"""
=== xdr source ============================================================
XDR Source Code::

enum AccountFlags
{ // masks for each flag
AUTH_REQUIRED_FLAG = 0x1
};

===========================================================================
"""
define_type("AccountFlags", Enum,
auth_required_flag: 1
)


end
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
defmodule MyXDR.AccountFlags 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 `AccountFlags` type.
"""

@behaviour XDR.Declaration

@declarations [
AUTH_REQUIRED_FLAG: 1
]

@enum_spec %XDR.Enum{declarations: @declarations, identifier: nil}

@type t :: %__MODULE__{identifier: atom()}

defstruct [:identifier]

@spec new(type :: atom()) :: t()
def new(type \\ :AUTH_REQUIRED_FLAG), do: %__MODULE__{identifier: type}

@impl true
def encode_xdr(%__MODULE__{identifier: type}) do
@declarations
|> XDR.Enum.new(type)
|> XDR.Enum.encode_xdr()
end

@impl true
def encode_xdr!(%__MODULE__{identifier: type}) do
@declarations
|> XDR.Enum.new(type)
|> XDR.Enum.encode_xdr!()
end

@impl true
def decode_xdr(bytes, spec \\ @enum_spec)

def decode_xdr(bytes, spec) do
case XDR.Enum.decode_xdr(bytes, spec) do
{:ok, {%XDR.Enum{identifier: type}, rest}} -> {:ok, {new(type), rest}}
error -> error
end
end

@impl true
def decode_xdr!(bytes, spec \\ @enum_spec)

def decode_xdr!(bytes, spec) do
{%XDR.Enum{identifier: type}, rest} = XDR.Enum.decode_xdr!(bytes, spec)
{new(type), rest}
end
end
19 changes: 7 additions & 12 deletions spec/output/generator_spec_elixir/const.x/MyXDR_generated.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,31 @@ defmodule MyXDR do
Automatically generated by xdrgen
DO NOT EDIT or your changes may be overwritten

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

use XDR.Base

comment ~S"""
=== xdr source ============================================================
XDR Source Code::

const FOO = 1;

===========================================================================
"""

define_type("FOO", Const, 1);

comment ~S"""
=== xdr source ============================================================
XDR Source Code::

typedef int TestArray[FOO];

===========================================================================
"""

define_type("TestArray", Array, length: "FOO", type: buid_type(base_ref))

comment ~S"""
=== xdr source ============================================================
XDR Source Code::

typedef int TestArray2<FOO>;

===========================================================================
"""

define_type("TestArray2", VariableArray, max_length: "FOO", type: buid_type(base_ref))

end
45 changes: 7 additions & 38 deletions spec/output/generator_spec_elixir/enum.x/MyXDR_generated.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ defmodule MyXDR do
Automatically generated by xdrgen
DO NOT EDIT or your changes may be overwritten

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

use XDR.Base

comment ~S"""
=== xdr source ============================================================
XDR Source Code::

enum MessageType
{
Expand All @@ -34,58 +32,29 @@ defmodule MyXDR do
FBA_QUORUMSET,
FBA_MESSAGE
};

===========================================================================
"""
define_type("MessageType", Enum,
error_msg: 0,
hello: 1,
dont_have: 2,
get_peers: 3,
peers: 4,
get_tx_set: 5,
tx_set: 6,
get_validations: 7,
validations: 8,
transaction: 9,
json_transaction: 10,
get_fba_quorumset: 11,
fba_quorumset: 12,
fba_message: 13
)


comment ~S"""
=== xdr source ============================================================
XDR Source Code::

enum Color {
RED=0,
GREEN=1,
BLUE=2
};

===========================================================================
"""
define_type("Color", Enum,
red: 0,
green: 1,
blue: 2
)


comment ~S"""
=== xdr source ============================================================
XDR Source Code::

enum Color2 {
RED2=RED,
GREEN2=1,
BLUE2=2
};

===========================================================================
"""
define_type("Color2", Enum,
red2: 0,
green2: 1,
blue2: 2
)


end
Loading