Skip to content

Commit

Permalink
Fix Dialyzer (#265)
Browse files Browse the repository at this point in the history
* Fix post-Elixir 1.16 divergence in File.stream!

* Add exclusion for PhilomenaWeb.Config compile-time variance

* Add missing Autocomplete.t
  • Loading branch information
liamwhite authored Jun 2, 2024
1 parent 9d20b9c commit 80f9fa9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
2 changes: 2 additions & 0 deletions lib/philomena/autocomplete/autocomplete.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ defmodule Philomena.Autocomplete.Autocomplete do
use Ecto.Schema
import Ecto.Changeset

@type t :: %__MODULE__{}

@primary_key false
schema "autocomplete" do
field :content, :binary
Expand Down
22 changes: 19 additions & 3 deletions lib/philomena/sha512.ex
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
defmodule Philomena.Sha512 do
@spec file(String.t()) :: String.t()
def file(file) do
@chunk_size 10_485_760

@spec file(Path.t()) :: String.t()
def file(path) do
hash_ref = :crypto.hash_init(:sha512)

File.stream!(file, [], 10_485_760)
path
|> stream_file()
|> Enum.reduce(hash_ref, &:crypto.hash_update(&2, &1))
|> :crypto.hash_final()
|> Base.encode16(case: :lower)
end

if Version.match?(System.version(), ">= 1.16.0") do
# `stream!/2` was added in Elixir 1.16 to accept a shortened form,
# where we only need to specify the size of each stream chunk
defp stream_file(file) do
File.stream!(file, @chunk_size)
end
else
# Use legacy stream/3 for older Elixir versions
defp stream_file(file) do
File.stream!(file, [], @chunk_size)
end
end
end
4 changes: 4 additions & 0 deletions lib/philomena_web/config.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
defmodule PhilomenaWeb.Config do
# Dialyzer only analyzes beam files directly and cannot see the compile-time variance in
# the associated values, so it flags a false positive here.
@dialyzer [:no_match]

@reload_enabled Application.compile_env(:philomena, :vite_reload, false)
@csp_relaxed Application.compile_env(:philomena, :csp_relaxed, false)

Expand Down

0 comments on commit 80f9fa9

Please sign in to comment.