From 3f05231c75c4a6a03ec9acfb908a03222005f2e8 Mon Sep 17 00:00:00 2001 From: aguirrem Date: Thu, 4 Jun 2020 11:05:31 -0500 Subject: [PATCH] Add Metadata request (#6) * Provide api to request tag metadata Why: In order to make this information visible to the applications using scrub How: add new public api and corresponding queries in the session.ex application * Fix return tuple from from read_metadata to includ :ok and data * Add template data filter Why: Scrub.session appends template data to the metadata map. This data contains information about templates for structured types not needed currently How: Filtering items whose structure map key is not set to :atomic * Remove incorrectly uncommented code * Increase Version Number * Including mix format changes --- lib/scrub.ex | 25 +++++++++++++++++++++++++ lib/scrub/session.ex | 31 ++++++++++++++++++++++++++++++- mix.exs | 2 +- 3 files changed, 56 insertions(+), 2 deletions(-) diff --git a/lib/scrub.ex b/lib/scrub.ex index 76b0bc0..25ef4f5 100644 --- a/lib/scrub.ex +++ b/lib/scrub.ex @@ -38,6 +38,16 @@ defmodule Scrub do Scrub.Session.close(session) end + def read_metadata(session) do + case Session.get_tags_metadata(session) do + {:ok, metadata} -> + {:ok, filter_template_data(metadata)} + + error -> + error + end + end + def read_tag(session, tag) when is_binary(tag) do case Session.get_tag_metadata(session, tag) do {:ok, tag} -> @@ -81,6 +91,21 @@ defmodule Scrub do end end + def filter_template_data(tags) do + tags + |> Enum.reject(fn item -> + is_structure_type(item) + end) + end + + def is_structure_type(%{structure: :atomic}) do + false + end + + def is_structure_type(_x) do + true + end + # def read_tag(host, tag) when is_binary(host) do # open_conn(host) # |> read_tag(tag) diff --git a/lib/scrub/session.ex b/lib/scrub/session.ex index 4676c76..9568a1e 100644 --- a/lib/scrub/session.ex +++ b/lib/scrub/session.ex @@ -49,6 +49,19 @@ defmodule Scrub.Session do DBConnection.start_link(__MODULE__, opts) end + def get_tags_metadata(session) do + case DBConnection.execute(session, %Query{query: :get_all_tags_metadata}, <<>>) do + {:ok, _query, {:error, err}} -> + {:error, err} + + {:ok, _query, result} -> + {:ok, result} + + {:error, _} = err -> + err + end + end + def get_tag_metadata(session, tag) do case DBConnection.execute(session, %Query{query: :get_tag_metadata}, tag) do {:ok, _query, {:error, err}} -> @@ -229,6 +242,16 @@ defmodule Scrub.Session do end end + @impl true + def handle_execute( + %Query{query: :get_all_tags_metadata} = query, + _data, + _opts, + %{tag_metadata: reply} = state + ) do + {:ok, query, reply, state} + end + @impl true def handle_execute( %Query{query: :get_tag_metadata} = query, @@ -477,7 +500,13 @@ defmodule Scrub.Session do end def encode(%Query{query: tag}, data, _s) - when tag in [:send_rr_data, :close, :get_tag_metadata, :send_unit_data] do + when tag in [ + :send_rr_data, + :close, + :get_tag_metadata, + :get_all_tags_metadata, + :send_unit_data + ] do data end diff --git a/mix.exs b/mix.exs index 93279ce..a78d9e8 100644 --- a/mix.exs +++ b/mix.exs @@ -4,7 +4,7 @@ defmodule Scrub.MixProject do def project do [ app: :scrub, - version: "0.1.2", + version: "0.1.3", elixir: "~> 1.8", start_permanent: Mix.env() == :prod, deps: deps()