Current section
Files
Jump to
Current section
Files
lib/boundary/data_source/data_source_yaml.ex
defmodule SplitClient.DataSourceYAML do
@moduledoc """
A DataSource intended for local development. Will
consume a YAML file that developers can use to test
feature flags without having to modify global data
on the Split.IO web portal
See also: [Local Feature Development](README.md#local-feature-development)
"""
alias SplitClient.Boundary.DataSource.SplitStub
@behaviour SplitClient.Boundary.DataSourceBehaviour
@impl true
def get_treatment(key, split_name, _opts \\ []) do
treatment =
read_split_file()
|> SplitStub.find_treatment(key, split_name)
{:ok, treatment}
end
@impl true
def get_treatments(key, split_names, _opts \\ []) when is_list(split_names) do
treatments =
read_split_file()
|> SplitStub.find_treatments(key, split_names)
{:ok, treatments}
end
@impl true
def get_all_treatments(keys, _opts \\ []) when is_list(keys) do
treatments =
read_split_file()
|> SplitStub.find_all_treatments(keys)
{:ok, treatments}
end
defp read_split_file do
yaml().read_from_file!(split_file_path()) |> process_yaml()
end
# an empty file will return an empty map. we're expecting a list.
defp process_yaml(%{} = _yaml) do
process_yaml([])
end
defp process_yaml(yaml) when is_list(yaml) do
SplitStub.from_list(yaml)
end
defp split_file_path do
Application.get_env(:split_client, :split_file, Path.join(File.cwd!(), "/split.yml"))
end
defp yaml do
Application.get_env(:split_client, :yaml, SplitClient.Boundary.YAML)
end
end