diff --git a/CHANGES.txt b/CHANGES.txt index eaff78b..fb909ee 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,5 +1,6 @@ 0.2.0 (February 14, 2025): - Added new variations of the get treatment functions to support evaluating flags in given flag set/s: `Split.get_treatments_by_flag_set/3`, `Split.get_treatments_by_flag_sets/3`, `Split.get_treatments_with_config_by_flag_set/3`, and `Split.get_treatments_with_config_by_flag_sets/3`. + - Updated the `:socket_path` option for `Split.Supervisor.start_link/1` to be optional, defaulting to `"/var/run/splitd.sock"`. - BREAKING CHANGES: - Removed the `fallback_enabled` option from `Split.Supervisor.start_link/1`. Fallback behavior is now always enabled, so `Split` functions no longer return `{:error, _}` tuples but instead use the fallback value when an error occurs. - Renamed the `Split.Treatment` struct to `Split.TreatmentWithConfig` and removed the `label`, `change_number`, and `timestamp` fields. diff --git a/README.md b/README.md index 9d2d47a..d0a9e81 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ by adding `split_thin_elixir` to your list of dependencies in `mix.exs`: ```elixir def deps do [ - {:split, "~> 0.1.0", hex: :split_thin_sdk} + {:split, "~> 0.2.0", hex: :split_thin_sdk} ] end ``` diff --git a/lib/split.ex b/lib/split.ex index 6380993..94c2569 100644 --- a/lib/split.ex +++ b/lib/split.ex @@ -32,7 +32,7 @@ defmodule Split do `Split` takes a number of keyword arguments as options when starting. The following options are available: - - `:socket_path`: **REQUIRED** The path to the splitd socket file. For example `/var/run/splitd.sock`. + - `:socket_path`: **OPTIONAL** The path to the splitd socket file. Default is `"/var/run/splitd.sock"`. - `:pool_size`: **OPTIONAL** The size of the pool of connections to the splitd daemon. Default is the number of online schedulers in the Erlang VM (See: https://www.erlang.org/doc/apps/erts/erl_cmd.html). - `:connect_timeout`: **OPTIONAL** The timeout in milliseconds to connect to the splitd daemon. Default is `1000`. @@ -98,7 +98,8 @@ defmodule Split do iex> Split.get_treatment("user_id", "located_in_usa", %{country: "USA"}) "on" """ - @spec get_treatment(split_key(), String.t(), attributes() | nil) :: String.t() + @spec get_treatment(split_key(), String.t()) :: String.t() + @spec get_treatment(split_key(), String.t(), attributes()) :: String.t() def get_treatment(key, feature_name, attributes \\ %{}) do request = Message.get_treatment( @@ -120,7 +121,8 @@ defmodule Split do iex> Split.get_treatment("user_id", "located_in_usa", %{country: "USA"}) %Split.TreatmentWithConfig{treatment: "on", config: nil} """ - @spec get_treatment_with_config(split_key(), String.t(), attributes() | nil) :: + @spec get_treatment_with_config(split_key(), String.t()) :: TreatmentWithConfig.t() + @spec get_treatment_with_config(split_key(), String.t(), attributes()) :: TreatmentWithConfig.t() def get_treatment_with_config(key, feature_name, attributes \\ %{}) do request = @@ -143,7 +145,10 @@ defmodule Split do iex> Split.get_treatments("user_id", ["located_in_usa"], %{country: "USA"}) %{"located_in_usa" => "on"} """ - @spec get_treatments(split_key(), [String.t()], attributes() | nil) :: %{ + @spec get_treatments(split_key(), [String.t()]) :: %{ + String.t() => String.t() + } + @spec get_treatments(split_key(), [String.t()], attributes()) :: %{ String.t() => String.t() } def get_treatments(key, feature_names, attributes \\ %{}) do @@ -167,7 +172,10 @@ defmodule Split do iex> Split.get_treatments_with_config("user_id", ["located_in_usa"], %{country: "USA"}) %{"located_in_usa" => %Split.TreatmentWithConfig{treatment: "on", config: nil}} """ - @spec get_treatments_with_config(split_key(), [String.t()], attributes() | nil) :: %{ + @spec get_treatments_with_config(split_key(), [String.t()]) :: %{ + String.t() => TreatmentWithConfig.t() + } + @spec get_treatments_with_config(split_key(), [String.t()], attributes()) :: %{ String.t() => TreatmentWithConfig.t() } def get_treatments_with_config(key, feature_names, attributes \\ %{}) do @@ -191,7 +199,10 @@ defmodule Split do iex> Split.get_treatments_by_flag_set("user_id", "frontend_flags", %{country: "USA"}) %{"located_in_usa" => "on"} """ - @spec get_treatments_by_flag_set(split_key(), String.t(), attributes() | nil) :: %{ + @spec get_treatments_by_flag_set(split_key(), String.t()) :: %{ + String.t() => String.t() + } + @spec get_treatments_by_flag_set(split_key(), String.t(), attributes()) :: %{ String.t() => String.t() } def get_treatments_by_flag_set(key, flag_set_name, attributes \\ %{}) do @@ -215,10 +226,15 @@ defmodule Split do iex> Split.get_treatments_with_config_by_flag_set("user_id", "frontend_flags", %{country: "USA"}) %{"located_in_usa" => %Split.TreatmentWithConfig{treatment: "on", config: nil}} """ + @spec get_treatments_with_config_by_flag_set( + split_key(), + String.t() + ) :: + %{String.t() => TreatmentWithConfig.t()} @spec get_treatments_with_config_by_flag_set( split_key(), String.t(), - attributes() | nil + attributes() ) :: %{String.t() => TreatmentWithConfig.t()} def get_treatments_with_config_by_flag_set( @@ -246,7 +262,9 @@ defmodule Split do iex> Split.get_treatments_by_flag_sets("user_id", ["frontend_flags", "backend_flags"], %{country: "USA"}) %{"located_in_usa" => "on"} """ - @spec get_treatments_by_flag_sets(split_key(), [String.t()], attributes() | nil) :: + @spec get_treatments_by_flag_sets(split_key(), [String.t()]) :: + %{String.t() => String.t()} + @spec get_treatments_by_flag_sets(split_key(), [String.t()], attributes()) :: %{String.t() => String.t()} def get_treatments_by_flag_sets( key, @@ -273,10 +291,15 @@ defmodule Split do iex> Split.get_treatments_with_config_by_flag_sets("user_id", ["frontend_flags", "backend_flags"], %{country: "USA"}) %{"located_in_usa" => %Split.TreatmentWithConfig{treatment: "on", config: nil}} """ + @spec get_treatments_with_config_by_flag_sets( + split_key(), + [String.t()] + ) :: + %{String.t() => TreatmentWithConfig.t()} @spec get_treatments_with_config_by_flag_sets( split_key(), [String.t()], - attributes() | nil + attributes() ) :: %{String.t() => TreatmentWithConfig.t()} def get_treatments_with_config_by_flag_sets( @@ -306,11 +329,14 @@ defmodule Split do true iex> Split.track("user_id", "user", "my-event", 42) true + iex> Split.track("user_id", "user", "my-event", nil, %{property1: "value1"}) + true iex> Split.track("user_id", "user", "my-event", 42, %{property1: "value1"}) true """ - @spec track(split_key(), String.t(), String.t(), number() | nil, properties() | nil) :: - boolean() + @spec track(split_key(), String.t(), String.t()) :: boolean() + @spec track(split_key(), String.t(), String.t(), number() | nil) :: boolean() + @spec track(split_key(), String.t(), String.t(), number() | nil, properties()) :: boolean() def track(key, traffic_type, event_type, value \\ nil, properties \\ %{}) do request = Message.track(key, traffic_type, event_type, value, properties) execute_rpc(request) diff --git a/lib/split/rpc/message.ex b/lib/split/rpc/message.ex index c0584d7..3a74147 100644 --- a/lib/split/rpc/message.ex +++ b/lib/split/rpc/message.ex @@ -24,12 +24,12 @@ defmodule Split.RPC.Message do @type get_treatment_args :: {:key, Split.split_key()} | {:feature_name, String.t()} - | {:attributes, Split.attributes() | nil} + | {:attributes, Split.attributes()} @type get_treatments_args :: {:key, Split.split_key()} | {:feature_names, list(String.t())} - | {:attributes, Split.attributes() | nil} + | {:attributes, Split.attributes()} @doc """ Builds a message to register a client in splitd. @@ -301,6 +301,8 @@ defmodule Split.RPC.Message do iex> Message.track("user_key", "traffic_type", "my_event") %Message{v: 1, o: 128, a: ["user_key", "traffic_type", "my_event", nil, %{}]} """ + @spec track(Split.split_key(), String.t(), String.t()) :: t() + @spec track(Split.split_key(), String.t(), String.t(), number() | nil) :: t() @spec track(Split.split_key(), String.t(), String.t(), number() | nil, Split.properties()) :: t() def track(key, traffic_type, event_type, value \\ nil, properties \\ %{}) do diff --git a/lib/split/sockets/pool.ex b/lib/split/sockets/pool.ex index 778d1c3..328b812 100644 --- a/lib/split/sockets/pool.ex +++ b/lib/split/sockets/pool.ex @@ -17,11 +17,13 @@ defmodule Split.Sockets.Pool do end def start_link(opts) do + socket_path = Keyword.get(opts, :socket_path, "/var/run/splitd.sock") pool_name = Keyword.get(opts, :pool_name, __MODULE__) pool_size = Keyword.get(opts, :pool_size, System.schedulers_online()) opts = opts + |> Keyword.put_new(:socket_path, socket_path) |> Keyword.put_new(:pool_size, pool_size) |> Keyword.put_new(:pool_name, pool_name) diff --git a/lib/split/supervisor.ex b/lib/split/supervisor.ex index 999e182..41ff8cf 100644 --- a/lib/split/supervisor.ex +++ b/lib/split/supervisor.ex @@ -11,8 +11,9 @@ defmodule Split.Supervisor do {:ok, init_arg} end + @spec start_link() :: Supervisor.on_start() @spec start_link(Split.options()) :: Supervisor.on_start() - def start_link(opts) do + def start_link(opts \\ []) do child = {Pool, opts} Supervisor.start_link([child], strategy: :one_for_one) end