Skip to content

Commit

Permalink
wip: Play with :xmerl_xpath
Browse files Browse the repository at this point in the history
  • Loading branch information
erszcz committed Oct 24, 2024
1 parent 02c700c commit d1ba939
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
16 changes: 12 additions & 4 deletions lib/uof_feed/model/outcome.ex
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
defmodule UofFeed.Model.Outcome do
require DataSchema

#{:outcome, [ id: ~c"12s", odds: ~c"4.25", probabilities: ~c"0.185077449", active: ~c"1" ], []}

@fields [
field: {:id, {:attr, :id}, {__MODULE__, :charlist_to_integer, []}}
field: {:id, ~c"string(@id)", {__MODULE__, :cast_id, []}}
]

@data_accessor UofFeed.XmerlSimpleAccessor
DataSchema.data_schema @fields

## If we wanted to use `DataSchema.to_struct/5`, for example to integrate with Ecto,
## it'd be handy to have the fields accessible.
def fields, do: @fields

def charlist_to_integer(clist) do
def cast_id({:xmlObj, :string, clist}), do: cast_id(clist)
def cast_id(clist) when is_list(clist) do
case :string.to_integer(clist) do
{int, []} -> {:ok, int}
{:error, :badarg} -> {:error, {:badarg, clist}}
{_, leftover} -> {:error, {:leftover, leftover}}
end
end

def test do
o_simple = {:outcome, [ id: ~c"12", odds: ~c"4.25", probabilities: ~c"0.185077449", active: ~c"1" ], []}
o = o_simple |> :xmerl_lib.normalize_element()
DataSchema.to_struct(o, UofFeed.Model.Outcome)
end
end
4 changes: 2 additions & 2 deletions lib/uof_feed/xmerl_simple_accessor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ defmodule UofFeed.XmerlSimpleAccessor do
@behaviour DataSchema.DataAccessBehaviour

@impl true
def field({_, attrs, _} = _data, {:attr, attr}) do
attrs[attr]
def field(element = _data, xpath) do
:xmerl_xpath.string(xpath, element)
end

@impl true
Expand Down

0 comments on commit d1ba939

Please sign in to comment.