Skip to content

Commit a46db9e

Browse files
NSHkrNSHkr
authored andcommitted
Use Access Behavior in Foundation.Config
1 parent 301530e commit a46db9e

File tree

1 file changed

+71
-36
lines changed

1 file changed

+71
-36
lines changed

lib/elixir_scope/foundation/config.ex

Lines changed: 71 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ defmodule ElixirScope.Foundation.Config do
88

99
alias ElixirScope.Foundation.{Error, ErrorContext}
1010

11+
@behaviour Access
12+
1113
@type config_path :: [atom()]
1214
@type config_value :: term()
1315

@@ -111,6 +113,39 @@ defmodule ElixirScope.Foundation.Config do
111113
dev: map()
112114
}
113115

116+
## Access Behavior Implementation
117+
118+
@impl Access
119+
def fetch(%__MODULE__{} = config, key) do
120+
config
121+
|> Map.from_struct()
122+
|> Map.fetch(key)
123+
end
124+
125+
@impl Access
126+
def get_and_update(%__MODULE__{} = config, key, function) do
127+
map_config = Map.from_struct(config)
128+
129+
case Map.get_and_update(map_config, key, function) do
130+
{current_value, updated_map} ->
131+
case struct(__MODULE__, updated_map) do
132+
updated_config when is_struct(updated_config, __MODULE__) ->
133+
{current_value, updated_config}
134+
_ ->
135+
# Fallback if struct creation fails
136+
{current_value, config}
137+
end
138+
end
139+
end
140+
141+
@impl Access
142+
def pop(%__MODULE__{} = config, key) do
143+
map_config = Map.from_struct(config)
144+
{value, updated_map} = Map.pop(map_config, key)
145+
updated_config = struct(__MODULE__, updated_map)
146+
{value, updated_config}
147+
end
148+
114149
# Paths that can be updated at runtime
115150
@updatable_paths [
116151
[:ai, :planning, :sampling_rate],
@@ -248,7 +283,7 @@ defmodule ElixirScope.Foundation.Config do
248283

249284
@impl GenServer
250285
def handle_call({:get_config_path, path}, _from, config) do
251-
value = get_config_value(config, path)
286+
value = get_in(config, path)
252287
{:reply, value, config}
253288
end
254289

@@ -269,7 +304,7 @@ defmodule ElixirScope.Foundation.Config do
269304
# new_config = put_in(config, path, value)
270305

271306
# With this:
272-
new_config = put_config_value(config, path, value)
307+
new_config = put_in(config, path, value)
273308

274309
case validate(new_config) do
275310
:ok ->
@@ -287,49 +322,49 @@ defmodule ElixirScope.Foundation.Config do
287322
end
288323
end
289324

290-
# Add this private function:
291-
defp put_config_value(_config, [], value) do
292-
value
293-
end
325+
# # Add this private function:
326+
# defp put_config_value(_config, [], value) do
327+
# value
328+
# end
294329

295-
defp put_config_value(config, [key], value) when is_struct(config) do
296-
Map.put(config, key, value)
297-
end
330+
# defp put_config_value(config, [key], value) when is_struct(config) do
331+
# Map.put(config, key, value)
332+
# end
298333

299-
defp put_config_value(config, [key], value) when is_map(config) do
300-
Map.put(config, key, value)
301-
end
334+
# defp put_config_value(config, [key], value) when is_map(config) do
335+
# Map.put(config, key, value)
336+
# end
302337

303-
defp put_config_value(config, [key | rest], value) when is_struct(config) do
304-
current_value = Map.get(config, key)
305-
new_value = put_config_value(current_value, rest, value)
306-
Map.put(config, key, new_value)
307-
end
338+
# defp put_config_value(config, [key | rest], value) when is_struct(config) do
339+
# current_value = Map.get(config, key)
340+
# new_value = put_config_value(current_value, rest, value)
341+
# Map.put(config, key, new_value)
342+
# end
308343

309-
defp put_config_value(config, [key | rest], value) when is_map(config) do
310-
current_value = Map.get(config, key)
311-
new_value = put_config_value(current_value, rest, value)
312-
Map.put(config, key, new_value)
313-
end
344+
# defp put_config_value(config, [key | rest], value) when is_map(config) do
345+
# current_value = Map.get(config, key)
346+
# new_value = put_config_value(current_value, rest, value)
347+
# Map.put(config, key, new_value)
348+
# end
314349

315350

316-
# Add this private function:
317-
defp get_config_value(config, []) do
318-
config
319-
end
351+
# # # Add this private function:
352+
# # defp get_config_value(config, []) do
353+
# # config
354+
# # end
320355

321-
defp get_config_value(config, [key | rest]) when is_struct(config) do
322-
# Convert struct to map for the first access
323-
map_config = Map.from_struct(config)
324-
get_config_value(Map.get(map_config, key), rest)
325-
end
356+
# defp get_config_value(config, [key | rest]) when is_struct(config) do
357+
# # Convert struct to map for the first access
358+
# map_config = Map.from_struct(config)
359+
# get_config_value(Map.get(map_config, key), rest)
360+
# end
326361

327-
defp get_config_value(config, [key | rest]) when is_map(config) do
328-
get_config_value(Map.get(config, key), rest)
329-
end
362+
# defp get_config_value(config, [key | rest]) when is_map(config) do
363+
# get_config_value(Map.get(config, key), rest)
364+
# end
330365

331-
defp get_config_value(nil, _path), do: nil
332-
defp get_config_value(value, []), do: value
366+
# defp get_config_value(nil, _path), do: nil
367+
# defp get_config_value(value, []), do: value
333368

334369
## Validation Functions
335370

0 commit comments

Comments
 (0)