From c74037bda7a15de00ef22852ef441701449b7bd8 Mon Sep 17 00:00:00 2001 From: Emiliano Sanchez Date: Wed, 5 Feb 2025 20:07:23 -0300 Subject: [PATCH 1/2] Moved the Split struct into the new Split.SplitView module. --- CHANGES.txt | 1 + lib/split.ex | 29 +++-------------------------- lib/split/rpc/response_parser.ex | 3 ++- lib/split/split_view.ex | 25 +++++++++++++++++++++++++ test/rpc/response_parser_test.exs | 7 ++++--- test/split_test.exs | 5 +++-- 6 files changed, 38 insertions(+), 32 deletions(-) create mode 100644 lib/split/split_view.ex diff --git a/CHANGES.txt b/CHANGES.txt index dd13eab..cef547c 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -4,6 +4,7 @@ - Updated the return type of `Split` functions to never return `{:error, _}` tuples. Instead, they will use the fallback value when an error occurs. - Updated the return types of `Split.get_treatment/3` and `Split.get_treatments/3` to return the treatment string value and a map of treatment string values, respectively. - Updated the `Split` struct: renamed the `configurations` field to `configs`, the `flag_sets` field to `sets`, and added the `impressions_disabled` field. + - Moved the `Split` struct into the new `Split.SplitView` module. 0.1.0 (January 27, 2025): - BREAKING CHANGES: diff --git a/lib/split.ex b/lib/split.ex index ab8838e..1447a4a 100644 --- a/lib/split.ex +++ b/lib/split.ex @@ -52,21 +52,10 @@ defmodule Split do alias Split.Telemetry alias Split.Sockets.Pool alias Split.Treatment + alias Split.SplitView alias Split.RPC.Message alias Split.RPC.ResponseParser - @type t :: %Split{ - name: String.t(), - traffic_type: String.t(), - killed: boolean(), - treatments: [String.t()], - change_number: integer(), - configs: map(), - default_treatment: String.t(), - sets: [String.t()], - impressions_disabled: boolean() - } - @typedoc "An option that can be provided when starting `Split`." @type option :: {:socket_path, String.t()} @@ -75,18 +64,6 @@ defmodule Split do @type options :: [option()] - defstruct [ - :name, - :traffic_type, - :killed, - :treatments, - :change_number, - :configs, - :default_treatment, - :sets, - :impressions_disabled - ] - @doc """ Builds a child specification to use in a Supervisor. @@ -166,14 +143,14 @@ defmodule Split do execute_rpc(request) end - @spec split(String.t()) :: Split.t() | nil + @spec split(String.t()) :: SplitView.t() | nil def split(name) do request = Message.split(name) execute_rpc(request) end - @spec splits() :: [Split.t()] + @spec splits() :: [SplitView.t()] def splits do request = Message.splits() execute_rpc(request) diff --git a/lib/split/rpc/response_parser.ex b/lib/split/rpc/response_parser.ex index 4a2ef6e..be2a708 100644 --- a/lib/split/rpc/response_parser.ex +++ b/lib/split/rpc/response_parser.ex @@ -9,6 +9,7 @@ defmodule Split.RPC.ResponseParser do alias Split.RPC.Message alias Split.Telemetry alias Split.Treatment + alias Split.SplitView @type splitd_response :: {:ok, map()} | {:error, term()} @@ -151,7 +152,7 @@ defmodule Split.RPC.ResponseParser do end defp parse_split(payload) do - %Split{ + %SplitView{ name: Map.get(payload, "n", nil), traffic_type: payload["t"], killed: payload["k"], diff --git a/lib/split/split_view.ex b/lib/split/split_view.ex new file mode 100644 index 0000000..312e7e7 --- /dev/null +++ b/lib/split/split_view.ex @@ -0,0 +1,25 @@ +defmodule Split.SplitView do + defstruct [ + :name, + :traffic_type, + :killed, + :treatments, + :change_number, + :configs, + :default_treatment, + :sets, + :impressions_disabled + ] + + @type t :: %__MODULE__{ + name: String.t(), + traffic_type: String.t(), + killed: boolean(), + treatments: [String.t()], + change_number: integer(), + configs: map(), + default_treatment: String.t(), + sets: [String.t()], + impressions_disabled: boolean() + } +end diff --git a/test/rpc/response_parser_test.exs b/test/rpc/response_parser_test.exs index 8a7c776..c185a9d 100644 --- a/test/rpc/response_parser_test.exs +++ b/test/rpc/response_parser_test.exs @@ -6,6 +6,7 @@ defmodule Split.RPC.ResponseParserTest do alias Split.RPC.ResponseParser alias Split.RPC.Message alias Split.Treatment + alias Split.SplitView import ExUnit.CaptureLog @@ -166,7 +167,7 @@ defmodule Split.RPC.ResponseParserTest do }} assert ResponseParser.parse_response(response, message) == - %Split{ + %SplitView{ name: "feature_name", traffic_type: "user", killed: false, @@ -222,7 +223,7 @@ defmodule Split.RPC.ResponseParserTest do # Order of splits is not guaranteed assert ResponseParser.parse_response(response, message) |> Enum.sort_by(& &1.name) == [ - %Split{ + %SplitView{ name: "feature_a", traffic_type: "user", killed: false, @@ -233,7 +234,7 @@ defmodule Split.RPC.ResponseParserTest do sets: [], impressions_disabled: false }, - %Split{ + %SplitView{ name: "feature_b", traffic_type: "user", killed: false, diff --git a/test/split_test.exs b/test/split_test.exs index 36f440d..f279948 100644 --- a/test/split_test.exs +++ b/test/split_test.exs @@ -3,6 +3,7 @@ defmodule SplitThinElixirTest do alias Split.Impression alias Split.Treatment + alias Split.SplitView setup_all context do test_id = :erlang.phash2(context.case) @@ -104,12 +105,12 @@ defmodule SplitThinElixirTest do end test "split/1" do - assert %Split{name: "test-split"} = + assert %SplitView{name: "test-split"} = Split.split("test-split") end test "splits/0" do - assert [%Split{name: "test-split"}] = Split.splits() + assert [%SplitView{name: "test-split"}] = Split.splits() end describe "telemetry" do From 66b85cb29e667e53a25c668504c82779ed073333 Mon Sep 17 00:00:00 2001 From: Emiliano Sanchez Date: Wed, 5 Feb 2025 21:08:32 -0300 Subject: [PATCH 2/2] Fix parse_response --- lib/split/rpc/response_parser.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/split/rpc/response_parser.ex b/lib/split/rpc/response_parser.ex index be2a708..2bae8bc 100644 --- a/lib/split/rpc/response_parser.ex +++ b/lib/split/rpc/response_parser.ex @@ -18,7 +18,7 @@ defmodule Split.RPC.ResponseParser do """ @spec parse_response(response :: splitd_response(), request :: Message.t(), [ {:span_context, reference()} | {:span_context, nil} - ]) :: map() | list() | Treatment.t() | Split.t() | boolean() | nil + ]) :: map() | list() | Treatment.t() | SplitView.t() | boolean() | nil def parse_response(response, original_request, opts \\ [])