Skip to content

Commit

Permalink
Issue #54: Explicitly set missing requirement attribute field (#56)
Browse files Browse the repository at this point in the history
* Issue #54: Explicitly set missing requirement field in attributes to "optional" during schema load. Log attributes with missing requirement field on a single line. Bump version. 

---------

Signed-off-by: Rick Mouritzen <[email protected]>
  • Loading branch information
rmouritzen-splunk authored Nov 28, 2023
1 parent 594982d commit 7d424b5
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
73 changes: 73 additions & 0 deletions lib/schema/cache.ex
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,21 @@ defmodule Schema.Cache do

base_event = final_check(:base_event, base_event, attributes)

no_req_set = MapSet.new()
{profiles, no_req_set} = fix_entities(profiles, no_req_set)
{base_event, no_req_set} = fix_entity(base_event, no_req_set)
{classes, no_req_set} = fix_entities(classes, no_req_set)
{objects, no_req_set} = fix_entities(objects, no_req_set)

if MapSet.size(no_req_set) > 0 do
no_reqs = no_req_set |> Enum.sort() |> Enum.join(", ")

Logger.warning(
"The following attributes do not have a \"requirement\" field," <>
" a value of \"optional\" will be used: #{no_reqs}"
)
end

new(version)
|> set_profiles(profiles)
|> set_categories(categories)
Expand Down Expand Up @@ -593,6 +608,64 @@ defmodule Schema.Cache do
end
end

# Final fix up a map of many name -> entity key-value pairs.
# The term "entities" means to profiles, objects, or classes.
@spec fix_entities(map(), MapSet.t()) :: {map(), MapSet.t()}
defp fix_entities(entities, no_req_set) do
Enum.reduce(
entities,
{Map.new(), no_req_set},
fn {entity_name, entity}, {entities, no_req_set} ->
{entity, no_req_set} = fix_entity(entity, no_req_set)
{Map.put(entities, entity_name, entity), no_req_set}
end
)
end

# Final fix up of an entity definition map.
# The term "entity" mean a single profile, object, class, or base_event (a special class).
@spec fix_entity(map(), MapSet.t()) :: {map(), MapSet.t()}
defp fix_entity(entity, no_req_set) do
{attributes, no_req_set} = fix_attributes(entity[:attributes], no_req_set)
{Map.put(entity, :attributes, attributes), no_req_set}
end

# Final fix up an attributes map.
@spec fix_attributes(map(), MapSet.t()) :: {map(), MapSet.t()}
defp fix_attributes(attributes, no_req_set) do
Enum.reduce(
attributes,
{Map.new(), no_req_set},
fn {attribute_name, attribute_details}, {attributes, no_req_set} ->
{
# The Map.put_new fixes the actual missing requirement problem
Map.put(
attributes,
attribute_name,
Map.put_new(attribute_details, :requirement, "optional")
),
# This adds attributes with missing requirement to a set for later logging
track_missing_requirement(attribute_name, attribute_details, no_req_set)
}
end
)
end

@spec track_missing_requirement(String.t(), map(), MapSet.t()) :: MapSet.t()
defp track_missing_requirement(attribute_name, attribute_details, no_req_set) do
if Map.has_key?(attribute_details, :requirement) do
no_req_set
else
context = "#{attribute_details[:_source]}.#{attribute_name}"

if MapSet.member?(no_req_set, context) do
no_req_set
else
MapSet.put(no_req_set, context)
end
end
end

defp set_profiles(%__MODULE__{} = schema, profiles) do
struct(schema, profiles: profiles)
end
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
defmodule Schema.MixProject do
use Mix.Project

@version "2.62.0"
@version "2.63.0"

def project do
build = System.get_env("GITHUB_RUN_NUMBER") || "SNAPSHOT"
Expand Down

0 comments on commit 7d424b5

Please sign in to comment.