Skip to content

Commit

Permalink
Merge pull request #35 from ocsf/add-deprecated-annotation
Browse files Browse the repository at this point in the history
Fix #33 Add deprecated annotation
  • Loading branch information
rroupski authored Aug 24, 2023
2 parents 905e8d0 + 6b1f97b commit 2d5ab60
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 12 deletions.
35 changes: 33 additions & 2 deletions lib/schema/cache.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ defmodule Schema.Cache do
@type category_t() :: map()
@type dictionary_t() :: map()

@ocsf_deprecated :"@deprecated"

@doc """
Load the schema files and initialize the cache.
"""
Expand All @@ -64,6 +66,7 @@ defmodule Schema.Cache do

# Apply profiles to objects and classes
{objects, profiles} = Profiles.sanity_check(:object, objects, profiles)

objects =
objects
|> Utils.update_objects(attributes)
Expand Down Expand Up @@ -290,6 +293,7 @@ defmodule Schema.Cache do
[ext, _] ->
ext_name = String.to_atom("#{ext}/#{name}")
dictionary[ext_name] || dictionary[name]

_ ->
Logger.warning("#{name} has an invalid source: #{source}")
dictionary[name]
Expand Down Expand Up @@ -662,17 +666,44 @@ defmodule Schema.Cache do
end

defp final_check(maps, dictionary) do
Enum.into(maps, %{}, fn {name, value} ->
{name, final_check(name, value, dictionary)}
Enum.into(maps, %{}, fn {name, map} ->
deprecated_type(name, map, Map.get(map, @ocsf_deprecated))

{name, final_check(name, map, dictionary)}
end)
end

defp deprecated_type(_name, _map, nil) do
end

defp deprecated_type(name, map, deprecated) do
type =
if Map.has_key?(map, :category) do
"class"
else
"object"
end

message = Map.get(deprecated, :message)
Logger.warning("The #{name} #{type} has been deprecated. #{message}")
end

defp final_check(name, map, dictionary) do
profiles = map[:profiles]
attributes = map[:attributes]

list =
Enum.reduce(attributes, [], fn {key, attribute}, acc ->
case Map.get(attribute, @ocsf_deprecated) do
nil ->
:ok

deprecated ->
Logger.warning(
"The #{key} attribute in #{name} has been deprecated. #{Map.get(deprecated, :message)}"
)
end

if is_nil(attribute[:description]) do
desc = get_in(dictionary, [key, :description]) || ""

Expand Down
4 changes: 2 additions & 2 deletions lib/schema_web/templates/page/category.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ limitations under the License.
Category
</span>
</h3>
<div class="text-dark"><%= raw @data[:description] %></div>
<div class="text-dark"><%= raw description(@data) %></div>
</div>
<div class="col-md-auto fixed-right mt-2">
<div class="form-inline">
Expand Down Expand Up @@ -54,7 +54,7 @@ limitations under the License.
<% else %>
<td></td>
<% end %>
<td><%= raw class[:description] %></td>
<td><%= raw description(class) %></td>
</tr>
<% end %>
</tbody>
Expand Down
2 changes: 1 addition & 1 deletion lib/schema_web/templates/page/class.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ limitations under the License.
</h4>

<div class="text-secondary">
<%= raw @data[:description] %>
<%= raw description(@data) %>
</div>
<div class="mt-1">
<%= raw class_examples(@data) %>
Expand Down
2 changes: 1 addition & 1 deletion lib/schema_web/templates/page/classes.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ limitations under the License.
<% else %>
<td></td>
<% end %>
<td><%= raw map[:description] %></td>
<td><%= raw description(map) %></td>
</tr>
<% end %>
</tbody>
Expand Down
2 changes: 1 addition & 1 deletion lib/schema_web/templates/page/dictionary.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ limitations under the License.
<td><%= name %></td>
<td><%= raw format_type(@conn, field) %></td>
<td><%= raw links(@conn, key, field[:_links]) %></td>
<td><%= raw field[:description] %></td>
<td><%= raw description(field) %></td>
</tr>
<% end %>
</tbody>
Expand Down
2 changes: 1 addition & 1 deletion lib/schema_web/templates/page/object.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
</h3>

<div class="text-secondary">
<%= raw @data[:description] %>
<%= raw description(@data) %>
</div>
</div>
<div class="col-md-auto fixed-right mt-2">
Expand Down
2 changes: 1 addition & 1 deletion lib/schema_web/templates/page/objects.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ limitations under the License.
<td class="name"><a href="<%= path %>"><%= raw format_attribute_name(name, map) %></a></td>
<td><a href="<%= path %>"><%= name %></a></td>
<td><%= raw links(@conn, name, map[:_links]) %></td>
<td><%= raw map[:description] %></td>
<td><%= raw description(map) %></td>
</tr>
<% end %>
</tbody>
Expand Down
22 changes: 20 additions & 2 deletions lib/schema_web/views/page_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -313,9 +313,9 @@ defmodule SchemaWeb.PageView do
end
end

@spec format_desc(nil | map) :: any
@spec format_desc(map) :: any
def format_desc(obj) do
description = Map.get(obj, :description)
description = description(obj)

case Map.get(obj, :enum) do
nil ->
Expand Down Expand Up @@ -488,4 +488,22 @@ defmodule SchemaWeb.PageView do
defp format_number(n) do
Number.Delimit.number_to_delimited(n, precision: 0)
end

def description(map) do
deprecated(map, Map.get(map, :"@deprecated"))
end

defp deprecated(map, nil) do
Map.get(map, :description)
end

defp deprecated(map, deprecated) do
[
Map.get(map, :description),
"<div class='text-dark mt-2'><span class='bg-warning'>DEPRECATED</span> ",
Map.get(deprecated, :message),
"</div>"
]
end

end
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.53.0"
@version "2.54.0"

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

0 comments on commit 2d5ab60

Please sign in to comment.