From d3c21247e0484a3ebf26202e146e36e146776d43 Mon Sep 17 00:00:00 2001 From: Chili Palmer Date: Sun, 21 Jun 2026 14:30:40 +0200 Subject: [PATCH] Replace local maybe_put helpers --- AUDIT.md | 1 + lib/bds/ai/chat_tools.ex | 27 ++++++++++--------- lib/bds/desktop/shell_live/import_editor.ex | 5 +--- .../import_editor/analysis_state.ex | 6 +---- lib/bds/import_analysis.ex | 7 ++--- lib/bds/import_definitions.ex | 14 +++++----- lib/bds/tags.ex | 11 ++++---- 7 files changed, 32 insertions(+), 39 deletions(-) diff --git a/AUDIT.md b/AUDIT.md index a3f6786..dd18417 100644 --- a/AUDIT.md +++ b/AUDIT.md @@ -45,6 +45,7 @@ after each item. - [x] `desktop/shell_live/code_entity_editor.ex` - [x] `scripting/runtime.ex` - [x] `desktop/external_links.ex` +- [x] `MapUtils.maybe_put/3` replacements - [x] `desktop/error_html.ex` (verified keep: still required by Phoenix `render_errors` wiring) - [x] `desktop/health_controller.ex` (verified keep: current Phoenix route wiring still expects a controller module) - [x] `desktop/folder_picker.ex` (verified keep: no equivalent `Desktop.OS` picker primitive exists to inline onto) diff --git a/lib/bds/ai/chat_tools.ex b/lib/bds/ai/chat_tools.ex index c29054b..9d627b0 100644 --- a/lib/bds/ai/chat_tools.ex +++ b/lib/bds/ai/chat_tools.ex @@ -942,13 +942,16 @@ defmodule BDS.AI.ChatTools do defp search_filters(arguments) do %{} - |> maybe_put(:category, arguments["category"]) - |> maybe_put(:tags, arguments["tags"]) - |> maybe_put(:language, arguments["language"]) - |> maybe_put(:missing_translation_language, arguments["missingTranslationLanguage"]) - |> maybe_put(:year, arguments["year"]) - |> maybe_put(:month, arguments["month"]) - |> maybe_put(:status, BDS.BoundedAtoms.post_status(arguments["status"])) + |> BDS.MapUtils.maybe_put(:category, BDS.MapUtils.blank_to_nil(arguments["category"])) + |> BDS.MapUtils.maybe_put(:tags, BDS.MapUtils.blank_to_nil(arguments["tags"])) + |> BDS.MapUtils.maybe_put(:language, BDS.MapUtils.blank_to_nil(arguments["language"])) + |> BDS.MapUtils.maybe_put( + :missing_translation_language, + BDS.MapUtils.blank_to_nil(arguments["missingTranslationLanguage"]) + ) + |> BDS.MapUtils.maybe_put(:year, arguments["year"]) + |> BDS.MapUtils.maybe_put(:month, arguments["month"]) + |> BDS.MapUtils.maybe_put(:status, BDS.BoundedAtoms.post_status(arguments["status"])) |> Map.put(:offset, normalize_offset(arguments["offset"])) |> Map.put(:limit, normalize_limit(arguments["limit"])) end @@ -995,10 +998,6 @@ defmodule BDS.AI.ChatTools do end end - defp maybe_put(map, _key, nil), do: map - defp maybe_put(map, _key, ""), do: map - defp maybe_put(map, key, value), do: Map.put(map, key, value) - defp counted_terms(project_id, field) do Repo.all( from post in Post, where: post.project_id == ^project_id, select: field(post, ^field) @@ -1012,7 +1011,11 @@ defmodule BDS.AI.ChatTools do defp metadata_attrs(arguments, keys) do Enum.reduce(keys, %{}, fn key, acc -> - maybe_put(acc, BDS.MapUtils.safe_atomize_key(key), arguments[key]) + BDS.MapUtils.maybe_put( + acc, + BDS.MapUtils.safe_atomize_key(key), + BDS.MapUtils.blank_to_nil(arguments[key]) + ) end) end diff --git a/lib/bds/desktop/shell_live/import_editor.ex b/lib/bds/desktop/shell_live/import_editor.ex index 567f02d..b23574c 100644 --- a/lib/bds/desktop/shell_live/import_editor.ex +++ b/lib/bds/desktop/shell_live/import_editor.ex @@ -693,7 +693,7 @@ defmodule BDS.Desktop.ShellLive.ImportEditor do wxr_file_path: analysis_state.file_path, last_analysis_result: report } - |> maybe_put(:name, AnalysisState.suggested_definition_name(report)) + |> BDS.MapUtils.maybe_put(:name, AnalysisState.suggested_definition_name(report)) case ImportDefinitions.update_definition(definition_id, attrs) do {:ok, _definition} -> @@ -1453,7 +1453,4 @@ defmodule BDS.Desktop.ShellLive.ImportEditor do defp blank?(value), do: value in [nil, ""] defp blank_to_nil(""), do: nil defp blank_to_nil(value), do: value - - defp maybe_put(map, _key, nil), do: map - defp maybe_put(map, key, value), do: Map.put(map, key, value) end diff --git a/lib/bds/desktop/shell_live/import_editor/analysis_state.ex b/lib/bds/desktop/shell_live/import_editor/analysis_state.ex index 2ee43ab..657f9b6 100644 --- a/lib/bds/desktop/shell_live/import_editor/analysis_state.ex +++ b/lib/bds/desktop/shell_live/import_editor/analysis_state.ex @@ -160,7 +160,7 @@ defmodule BDS.Desktop.ShellLive.ImportEditor.AnalysisState do wxr_file_path: analysis_state.file_path, last_analysis_result: report } - |> maybe_put(:name, suggested_definition_name(report)) + |> BDS.MapUtils.maybe_put(:name, suggested_definition_name(report)) case ImportDefinitions.update_definition(definition_id, attrs) do {:ok, _definition} -> @@ -274,10 +274,6 @@ defmodule BDS.Desktop.ShellLive.ImportEditor.AnalysisState do get_in(report, [:site_info, :url]) || get_in(report, [:site_info, :title]) end - @spec maybe_put(term(), term(), term()) :: term() - def maybe_put(map, _key, nil), do: map - def maybe_put(map, key, value), do: Map.put(map, key, value) - @spec allow_repo_sandbox(term()) :: term() def allow_repo_sandbox(pid) when is_pid(pid) do if Code.ensure_loaded?(Ecto.Adapters.SQL.Sandbox) do diff --git a/lib/bds/import_analysis.ex b/lib/bds/import_analysis.ex index bad870e..9a04a59 100644 --- a/lib/bds/import_analysis.ex +++ b/lib/bds/import_analysis.ex @@ -264,7 +264,7 @@ defmodule BDS.ImportAnalysis do status: item.status } - maybe_put(base, :resolution, item.resolution) + BDS.MapUtils.maybe_put(base, :resolution, item.resolution) end defp summary_item(item) do @@ -276,7 +276,7 @@ defmodule BDS.ImportAnalysis do status: item.status } - maybe_put(base, :resolution, item.resolution) + BDS.MapUtils.maybe_put(base, :resolution, item.resolution) end defp summarize_post_items(items) do @@ -546,9 +546,6 @@ defmodule BDS.ImportAnalysis do end) end - defp maybe_put(map, _key, nil), do: map - defp maybe_put(map, key, value), do: Map.put(map, key, value) - defp blank_to_nil(nil), do: nil defp blank_to_nil(""), do: nil defp blank_to_nil(value), do: value diff --git a/lib/bds/import_definitions.ex b/lib/bds/import_definitions.ex index cbf88d7..405dd34 100644 --- a/lib/bds/import_definitions.ex +++ b/lib/bds/import_definitions.ex @@ -40,10 +40,13 @@ defmodule BDS.ImportDefinitions do %ImportDefinition{} = definition -> updates = %{} - |> maybe_put(:name, BDS.MapUtils.attr(attrs, :name)) - |> maybe_put(:wxr_file_path, BDS.MapUtils.attr(attrs, :wxr_file_path)) - |> maybe_put(:uploads_folder_path, BDS.MapUtils.attr(attrs, :uploads_folder_path)) - |> maybe_put( + |> BDS.MapUtils.maybe_put(:name, BDS.MapUtils.attr(attrs, :name)) + |> BDS.MapUtils.maybe_put(:wxr_file_path, BDS.MapUtils.attr(attrs, :wxr_file_path)) + |> BDS.MapUtils.maybe_put( + :uploads_folder_path, + BDS.MapUtils.attr(attrs, :uploads_folder_path) + ) + |> BDS.MapUtils.maybe_put( :last_analysis_result, normalize_analysis_result(BDS.MapUtils.attr(attrs, :last_analysis_result)) ) @@ -86,9 +89,6 @@ defmodule BDS.ImportDefinitions do ) end - defp maybe_put(map, _key, nil), do: map - defp maybe_put(map, key, value), do: Map.put(map, key, value) - alias BDS.MapUtils defp normalize_analysis_result(nil), do: nil diff --git a/lib/bds/tags.ex b/lib/bds/tags.ex index 017cfc5..4482089 100644 --- a/lib/bds/tags.ex +++ b/lib/bds/tags.ex @@ -266,8 +266,11 @@ defmodule BDS.Tags do |> Enum.sort_by(&String.downcase(&1.name)) |> Enum.map(fn tag -> %{"name" => tag.name} - |> maybe_put("color", tag.color) - |> maybe_put("postTemplateSlug", tag.post_template_slug) + |> BDS.MapUtils.maybe_put("color", BDS.MapUtils.blank_to_nil(tag.color)) + |> BDS.MapUtils.maybe_put( + "postTemplateSlug", + BDS.MapUtils.blank_to_nil(tag.post_template_slug) + ) end) Persistence.atomic_write(path, Jason.encode!(payload, pretty: true)) @@ -386,8 +389,4 @@ defmodule BDS.Tags do :ok end - defp maybe_put(map, _key, nil), do: map - defp maybe_put(map, _key, ""), do: map - defp maybe_put(map, key, value), do: Map.put(map, key, value) - end