Replace local attr helpers
This commit is contained in:
1
AUDIT.md
1
AUDIT.md
@@ -37,6 +37,7 @@ after each item.
|
|||||||
- [x] `scripting/capabilities/util.ex`
|
- [x] `scripting/capabilities/util.ex`
|
||||||
- [x] `desktop/automation.ex` (verified keep: current module is an external-process automation harness; replacing it with `Desktop.Window` primitives would not be a local simplification)
|
- [x] `desktop/automation.ex` (verified keep: current module is an external-process automation harness; replacing it with `Desktop.Window` primitives would not be a local simplification)
|
||||||
- [x] `desktop/shell_live/sidebar_events.ex`
|
- [x] `desktop/shell_live/sidebar_events.ex`
|
||||||
|
- [x] `MapUtils.attr/2` replacements
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|||||||
@@ -16,11 +16,12 @@ defmodule BDS.ImportDefinitions do
|
|||||||
%ImportDefinition{}
|
%ImportDefinition{}
|
||||||
|> ImportDefinition.changeset(%{
|
|> ImportDefinition.changeset(%{
|
||||||
id: Ecto.UUID.generate(),
|
id: Ecto.UUID.generate(),
|
||||||
project_id: attr(attrs, :project_id),
|
project_id: BDS.MapUtils.attr(attrs, :project_id),
|
||||||
name: attr(attrs, :name) || "",
|
name: BDS.MapUtils.attr(attrs, :name) || "",
|
||||||
wxr_file_path: attr(attrs, :wxr_file_path),
|
wxr_file_path: BDS.MapUtils.attr(attrs, :wxr_file_path),
|
||||||
uploads_folder_path: attr(attrs, :uploads_folder_path),
|
uploads_folder_path: BDS.MapUtils.attr(attrs, :uploads_folder_path),
|
||||||
last_analysis_result: normalize_analysis_result(attr(attrs, :last_analysis_result)),
|
last_analysis_result:
|
||||||
|
normalize_analysis_result(BDS.MapUtils.attr(attrs, :last_analysis_result)),
|
||||||
created_at: now,
|
created_at: now,
|
||||||
updated_at: now
|
updated_at: now
|
||||||
})
|
})
|
||||||
@@ -39,12 +40,12 @@ defmodule BDS.ImportDefinitions do
|
|||||||
%ImportDefinition{} = definition ->
|
%ImportDefinition{} = definition ->
|
||||||
updates =
|
updates =
|
||||||
%{}
|
%{}
|
||||||
|> maybe_put(:name, attr(attrs, :name))
|
|> maybe_put(:name, BDS.MapUtils.attr(attrs, :name))
|
||||||
|> maybe_put(:wxr_file_path, attr(attrs, :wxr_file_path))
|
|> maybe_put(:wxr_file_path, BDS.MapUtils.attr(attrs, :wxr_file_path))
|
||||||
|> maybe_put(:uploads_folder_path, attr(attrs, :uploads_folder_path))
|
|> maybe_put(:uploads_folder_path, BDS.MapUtils.attr(attrs, :uploads_folder_path))
|
||||||
|> maybe_put(
|
|> maybe_put(
|
||||||
:last_analysis_result,
|
:last_analysis_result,
|
||||||
normalize_analysis_result(attr(attrs, :last_analysis_result))
|
normalize_analysis_result(BDS.MapUtils.attr(attrs, :last_analysis_result))
|
||||||
)
|
)
|
||||||
|> Map.put(:updated_at, Persistence.now_ms())
|
|> Map.put(:updated_at, Persistence.now_ms())
|
||||||
|
|
||||||
@@ -85,8 +86,6 @@ defmodule BDS.ImportDefinitions do
|
|||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp attr(attrs, key), do: Map.get(attrs, key) || Map.get(attrs, Atom.to_string(key))
|
|
||||||
|
|
||||||
defp maybe_put(map, _key, nil), do: map
|
defp maybe_put(map, _key, nil), do: map
|
||||||
defp maybe_put(map, key, value), do: Map.put(map, key, value)
|
defp maybe_put(map, key, value), do: Map.put(map, key, value)
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
defmodule BDS.Menu do
|
defmodule BDS.Menu do
|
||||||
@moduledoc false
|
@moduledoc false
|
||||||
|
|
||||||
|
import BDS.MapUtils, only: [attr: 2]
|
||||||
|
|
||||||
alias BDS.Persistence
|
alias BDS.Persistence
|
||||||
alias BDS.Projects
|
alias BDS.Projects
|
||||||
|
|
||||||
@@ -263,11 +265,4 @@ defmodule BDS.Menu do
|
|||||||
|> String.replace(~s("), """)
|
|> String.replace(~s("), """)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp attr(attrs, key) do
|
|
||||||
cond do
|
|
||||||
Map.has_key?(attrs, key) -> Map.get(attrs, key)
|
|
||||||
Map.has_key?(attrs, Atom.to_string(key)) -> Map.get(attrs, Atom.to_string(key))
|
|
||||||
true -> nil
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
defmodule BDS.Metadata do
|
defmodule BDS.Metadata do
|
||||||
@moduledoc false
|
@moduledoc false
|
||||||
|
|
||||||
|
import BDS.MapUtils, only: [attr: 2, attr: 3]
|
||||||
|
|
||||||
require Logger
|
require Logger
|
||||||
|
|
||||||
alias BDS.Embeddings
|
alias BDS.Embeddings
|
||||||
@@ -674,19 +676,4 @@ defmodule BDS.Metadata do
|
|||||||
defp maybe_backfill_embeddings(result, _project_id, _previous_state, _project_metadata),
|
defp maybe_backfill_embeddings(result, _project_id, _previous_state, _project_metadata),
|
||||||
do: result
|
do: result
|
||||||
|
|
||||||
defp attr(attrs, key) do
|
|
||||||
cond do
|
|
||||||
Map.has_key?(attrs, key) -> Map.get(attrs, key)
|
|
||||||
Map.has_key?(attrs, Atom.to_string(key)) -> Map.get(attrs, Atom.to_string(key))
|
|
||||||
true -> nil
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
defp attr(attrs, key, default) do
|
|
||||||
cond do
|
|
||||||
Map.has_key?(attrs, key) -> Map.get(attrs, key)
|
|
||||||
Map.has_key?(attrs, Atom.to_string(key)) -> Map.get(attrs, Atom.to_string(key))
|
|
||||||
true -> default
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ defmodule BDS.Projects do
|
|||||||
|
|
||||||
import Ecto.Query
|
import Ecto.Query
|
||||||
|
|
||||||
|
alias BDS.MapUtils
|
||||||
alias BDS.Metadata
|
alias BDS.Metadata
|
||||||
alias BDS.Persistence
|
alias BDS.Persistence
|
||||||
alias BDS.Projects.Project
|
alias BDS.Projects.Project
|
||||||
@@ -151,8 +152,8 @@ defmodule BDS.Projects do
|
|||||||
@spec create_project(attrs()) :: {:ok, Project.t()} | {:error, term()}
|
@spec create_project(attrs()) :: {:ok, Project.t()} | {:error, term()}
|
||||||
def create_project(attrs) do
|
def create_project(attrs) do
|
||||||
now = Persistence.now_ms()
|
now = Persistence.now_ms()
|
||||||
name = attr(attrs, :name) || ""
|
name = MapUtils.attr(attrs, :name) || ""
|
||||||
slug = unique_slug(attr(attrs, :slug) || Slug.slugify(name))
|
slug = unique_slug(MapUtils.attr(attrs, :slug) || Slug.slugify(name))
|
||||||
|
|
||||||
retry_create_project(fn ->
|
retry_create_project(fn ->
|
||||||
Repo.transaction(fn ->
|
Repo.transaction(fn ->
|
||||||
@@ -162,8 +163,8 @@ defmodule BDS.Projects do
|
|||||||
id: Ecto.UUID.generate(),
|
id: Ecto.UUID.generate(),
|
||||||
name: name,
|
name: name,
|
||||||
slug: slug,
|
slug: slug,
|
||||||
description: attr(attrs, :description),
|
description: MapUtils.attr(attrs, :description),
|
||||||
data_path: attr(attrs, :data_path),
|
data_path: MapUtils.attr(attrs, :data_path),
|
||||||
created_at: now,
|
created_at: now,
|
||||||
updated_at: now,
|
updated_at: now,
|
||||||
is_active: false
|
is_active: false
|
||||||
@@ -391,11 +392,4 @@ defmodule BDS.Projects do
|
|||||||
File.write(path, Jason.encode!(registry))
|
File.write(path, Jason.encode!(registry))
|
||||||
end
|
end
|
||||||
|
|
||||||
defp attr(attrs, key) do
|
|
||||||
cond do
|
|
||||||
Map.has_key?(attrs, key) -> Map.get(attrs, key)
|
|
||||||
Map.has_key?(attrs, Atom.to_string(key)) -> Map.get(attrs, Atom.to_string(key))
|
|
||||||
true -> nil
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ defmodule BDS.Tags do
|
|||||||
|
|
||||||
import Ecto.Query
|
import Ecto.Query
|
||||||
|
|
||||||
|
alias BDS.MapUtils
|
||||||
alias BDS.Persistence
|
alias BDS.Persistence
|
||||||
alias BDS.Posts
|
alias BDS.Posts
|
||||||
alias BDS.Posts.Post
|
alias BDS.Posts.Post
|
||||||
@@ -16,8 +17,8 @@ defmodule BDS.Tags do
|
|||||||
|
|
||||||
@spec create_tag(attrs()) :: tag_result()
|
@spec create_tag(attrs()) :: tag_result()
|
||||||
def create_tag(attrs) do
|
def create_tag(attrs) do
|
||||||
project_id = attr(attrs, :project_id)
|
project_id = MapUtils.attr(attrs, :project_id)
|
||||||
name = attr(attrs, :name) |> to_string() |> String.trim()
|
name = MapUtils.attr(attrs, :name) |> to_string() |> String.trim()
|
||||||
|
|
||||||
with :ok <- validate_unique_name(project_id, name) do
|
with :ok <- validate_unique_name(project_id, name) do
|
||||||
now = Persistence.now_ms()
|
now = Persistence.now_ms()
|
||||||
@@ -27,8 +28,8 @@ defmodule BDS.Tags do
|
|||||||
id: Ecto.UUID.generate(),
|
id: Ecto.UUID.generate(),
|
||||||
project_id: project_id,
|
project_id: project_id,
|
||||||
name: name,
|
name: name,
|
||||||
color: attr(attrs, :color),
|
color: MapUtils.attr(attrs, :color),
|
||||||
post_template_slug: attr(attrs, :post_template_slug),
|
post_template_slug: MapUtils.attr(attrs, :post_template_slug),
|
||||||
created_at: now,
|
created_at: now,
|
||||||
updated_at: now
|
updated_at: now
|
||||||
})
|
})
|
||||||
@@ -115,8 +116,8 @@ defmodule BDS.Tags do
|
|||||||
|
|
||||||
tag ->
|
tag ->
|
||||||
updates = %{
|
updates = %{
|
||||||
color: attr(attrs, :color),
|
color: MapUtils.attr(attrs, :color),
|
||||||
post_template_slug: attr(attrs, :post_template_slug),
|
post_template_slug: MapUtils.attr(attrs, :post_template_slug),
|
||||||
updated_at: Persistence.now_ms()
|
updated_at: Persistence.now_ms()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -389,11 +390,4 @@ defmodule BDS.Tags do
|
|||||||
defp maybe_put(map, _key, ""), do: map
|
defp maybe_put(map, _key, ""), do: map
|
||||||
defp maybe_put(map, key, value), do: Map.put(map, key, value)
|
defp maybe_put(map, key, value), do: Map.put(map, key, value)
|
||||||
|
|
||||||
defp attr(attrs, key) do
|
|
||||||
cond do
|
|
||||||
Map.has_key?(attrs, key) -> Map.get(attrs, key)
|
|
||||||
Map.has_key?(attrs, Atom.to_string(key)) -> Map.get(attrs, Atom.to_string(key))
|
|
||||||
true -> nil
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -373,8 +373,8 @@ defmodule BDS.Tasks do
|
|||||||
status: status,
|
status: status,
|
||||||
progress: nil,
|
progress: nil,
|
||||||
message: nil,
|
message: nil,
|
||||||
group_id: attr(attrs, :group_id),
|
group_id: BDS.MapUtils.attr(attrs, :group_id),
|
||||||
group_name: attr(attrs, :group_name),
|
group_name: BDS.MapUtils.attr(attrs, :group_name),
|
||||||
created_at: DateTime.utc_now(),
|
created_at: DateTime.utc_now(),
|
||||||
started_at: nil,
|
started_at: nil,
|
||||||
finished_at: nil,
|
finished_at: nil,
|
||||||
@@ -570,11 +570,4 @@ defmodule BDS.Tasks do
|
|||||||
|> Keyword.get(:finished_task_ttl_ms, @default_finished_task_ttl_ms)
|
|> Keyword.get(:finished_task_ttl_ms, @default_finished_task_ttl_ms)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp attr(attrs, key) do
|
|
||||||
cond do
|
|
||||||
Map.has_key?(attrs, key) -> Map.get(attrs, key)
|
|
||||||
Map.has_key?(attrs, Atom.to_string(key)) -> Map.get(attrs, Atom.to_string(key))
|
|
||||||
true -> nil
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user