Introduce protocol to make entities suitable for sending to JS server
This commit is contained in:
parent
83e597edff
commit
8434bf8180
|
@ -0,0 +1,16 @@
|
|||
defmodule POAAgent.Entity do
|
||||
@moduledoc false
|
||||
|
||||
defmodule Name do
|
||||
@moduledoc false
|
||||
|
||||
def change({old, new}, data) do
|
||||
{value, data} = Map.pop(data, old)
|
||||
Map.put(data, new, value)
|
||||
end
|
||||
end
|
||||
|
||||
defprotocol NameConvention do
|
||||
def from_elixir_to_node(x)
|
||||
end
|
||||
end
|
|
@ -49,4 +49,22 @@ defmodule POAAgent.Entity.Ethereum.Block do
|
|||
:transactions_root,
|
||||
:uncles
|
||||
]
|
||||
|
||||
defimpl POAAgent.Entity.NameConvention do
|
||||
def from_elixir_to_node(x) do
|
||||
mapping = [
|
||||
extra_data: :extraData,
|
||||
gas_limit: :gasLimit,
|
||||
gas_used: :gasUsed,
|
||||
parent_hash: :parentHash,
|
||||
receipts_root: :receiptsRoot,
|
||||
seal_fields: :sealFields,
|
||||
sha3_uncles: :sha3Uncles,
|
||||
state_root: :stateRoot,
|
||||
total_difficulty: :totalDifficulty,
|
||||
transactions_root: :transactionsRoot
|
||||
]
|
||||
Enum.reduce(mapping, x, &POAAgent.Entity.Name.change/2)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -15,14 +15,27 @@ defmodule POAAgent.Entity.Ethereum.Statistics do
|
|||
}
|
||||
|
||||
defstruct [
|
||||
:active?,
|
||||
:mining?,
|
||||
:hashrate,
|
||||
:peers,
|
||||
:pending,
|
||||
:gas_price,
|
||||
:block,
|
||||
:syncing?,
|
||||
:uptime
|
||||
active?: nil,
|
||||
mining?: nil,
|
||||
hashrate: nil,
|
||||
peers: nil,
|
||||
pending: nil,
|
||||
gas_price: nil,
|
||||
block: %POAAgent.Entity.Ethereum.Block{},
|
||||
syncing?: nil,
|
||||
uptime: nil
|
||||
]
|
||||
|
||||
defimpl POAAgent.Entity.NameConvention do
|
||||
def from_elixir_to_node(x) do
|
||||
mapping = [
|
||||
active?: :active,
|
||||
mining?: :mining,
|
||||
gas_price: :gasPrice,
|
||||
syncing?: :syncing
|
||||
]
|
||||
x = Enum.reduce(mapping, x, &POAAgent.Entity.Name.change/2)
|
||||
Map.update!(x, :block, &POAAgent.Entity.NameConvention.from_elixir_to_node/1)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -31,4 +31,13 @@ defmodule POAAgent.Entity.Host.Information do
|
|||
:client,
|
||||
:can_update_history?
|
||||
]
|
||||
|
||||
defimpl POAAgent.Entity.NameConvention do
|
||||
def from_elixir_to_node(x) do
|
||||
mapping = [
|
||||
can_update_history?: :canUpdateHistory
|
||||
]
|
||||
Enum.reduce(mapping, x, &POAAgent.Entity.Name.change/2)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
8
mix.exs
8
mix.exs
|
@ -10,6 +10,7 @@ defmodule POAAgent.MixProject do
|
|||
elixir: "~> 1.6",
|
||||
start_permanent: Mix.env() == :prod,
|
||||
deps: deps(),
|
||||
dialyzer: dialyzer(),
|
||||
docs: docs()
|
||||
]
|
||||
end
|
||||
|
@ -45,4 +46,11 @@ defmodule POAAgent.MixProject do
|
|||
]
|
||||
end
|
||||
|
||||
defp dialyzer do
|
||||
[
|
||||
paths: [
|
||||
"_build/#{Mix.env()}/lib/poa_agent/consolidated"
|
||||
]
|
||||
]
|
||||
end
|
||||
end
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
defmodule POAAgent.EntityTest do
|
||||
use ExUnit.Case
|
||||
|
||||
test "key transformation is Node/JS friendly" do
|
||||
alias POAAgent.Entity.Host
|
||||
alias POAAgent.Entity.Ethereum
|
||||
|
||||
good? = fn x ->
|
||||
not String.contains?(x, ["_", "?"])
|
||||
end
|
||||
exception = "os_v"
|
||||
|
||||
x = [Host.Information, Ethereum.Block, Ethereum.Statistics]
|
||||
|> Enum.map(&Kernel.struct/1)
|
||||
|> Enum.map(&POAAgent.Entity.NameConvention.from_elixir_to_node/1)
|
||||
|> Enum.map(&Map.from_struct/1)
|
||||
|> Enum.map(&Map.keys/1)
|
||||
|> List.flatten()
|
||||
|> Enum.map(&Atom.to_string/1)
|
||||
|> List.delete(exception)
|
||||
|
||||
assert Enum.all?(x, good?)
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue