Skip to content

Commit 57d04d6

Browse files
authored
Merge pull request #2 from kianmeng/refactor-new-connection
Refactor OpenApiTypesense.Connection.new/1
2 parents 26ceb5f + a1b10d5 commit 57d04d6

2 files changed

Lines changed: 71 additions & 10 deletions

File tree

lib/open_api_typesense/connection.ex

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,52 @@ defmodule OpenApiTypesense.Connection do
2020
Setting new connection or using the default config.
2121
2222
> #### On using this function {: .info}
23-
> Functions e.g. `OpenApiTypesense.Health.health` don't need to explicitly pass this
23+
> Functions e.g. `OpenApiTypesense.Health.health/0` don't need to explicitly pass this
2424
> unless you want to use another connection. Also, `api_key` is hidden when invoking
2525
> this function.
2626
2727
## Examples
28+
2829
iex> alias OpenApiTypesense.Connection
29-
30+
3031
iex> conn = Connection.new()
3132
%OpenApiTypesense.Connection{
3233
host: "localhost",
3334
port: 8108,
3435
scheme: "http",
3536
...
3637
}
38+
39+
iex> Connection.new(%{})
40+
** (ArgumentError) Missing required fields: [:port, :scheme, :host, :api_key]
41+
(open_api_typesense 0.2.0) lib/open_api_typesense/connection.ex:56: OpenApiTypesense.Connection.new/1
42+
iex:2: (file)
43+
3744
"""
3845
@doc since: "0.2.0"
3946
@spec new(connection :: t() | map()) :: %__MODULE__{}
40-
def new(connection \\ defaults()) when is_map(connection) do
41-
%__MODULE__{
42-
host: Map.get(connection, :host),
43-
api_key: Map.get(connection, :api_key),
44-
port: Map.get(connection, :port),
45-
scheme: Map.get(connection, :scheme)
46-
}
47+
48+
def new(connection \\ defaults())
49+
50+
def new(connection) when is_map(connection) do
51+
missing_fields = required_fields() -- Map.keys(connection)
52+
53+
if missing_fields == [] do
54+
struct(__MODULE__, connection)
55+
else
56+
raise ArgumentError, "Missing required fields: #{inspect(missing_fields)}"
57+
end
58+
end
59+
60+
def new(_) do
61+
raise ArgumentError, "Expected a map for connection options"
62+
end
63+
64+
@spec required_fields :: map()
65+
defp required_fields do
66+
struct(__MODULE__, %{}) |> Map.drop([:__struct__]) |> Map.keys()
4767
end
4868

49-
@doc since: "0.2.0"
5069
@spec defaults :: map()
5170
defp defaults do
5271
%{
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
defmodule OpenApiTypesense.ConnectionTest do
2+
use ExUnit.Case, async: true
3+
4+
alias OpenApiTypesense.Connection
5+
6+
test "new/0 using the default config to creates a connection struct" do
7+
conn = Connection.new()
8+
9+
assert conn == %Connection{
10+
api_key: "xyz",
11+
host: "localhost",
12+
port: 8108,
13+
scheme: "http"
14+
}
15+
end
16+
17+
test "new/1 with custom fields creates a connection struct" do
18+
conn =
19+
Connection.new(%{
20+
host: "otherhost",
21+
port: 9200,
22+
scheme: "https",
23+
api_key: "myapikey"
24+
})
25+
26+
assert conn == %Connection{
27+
api_key: "myapikey",
28+
host: "otherhost",
29+
port: 9200,
30+
scheme: "https"
31+
}
32+
end
33+
34+
test "new/1 with empty map raises ArgumentError" do
35+
msg = "Missing required fields: [:port, :scheme, :host, :api_key]"
36+
assert_raise ArgumentError, msg, fn -> Connection.new(%{}) end
37+
end
38+
39+
test "new/1 with invalid data type raises ArgumentError" do
40+
assert_raise ArgumentError, fn -> Connection.new("invalid") end
41+
end
42+
end

0 commit comments

Comments
 (0)