Skip to content

Commit

Permalink
More refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
edgurgel committed Aug 15, 2024
1 parent 0b9b426 commit 7d20d56
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 35 deletions.
54 changes: 44 additions & 10 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,54 @@
on: push
name: CI

on: [push, pull_request]

jobs:
format:
name: Format & credo
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2.3.1

- name: Install OTP and Elixir
uses: erlef/setup-beam@v1
with:
otp-version: 26.x
elixir-version: 1.16.x

- name: Install dependencies
run: mix deps.get

- name: Compile with --warnings-as-errors
run: mix compile --warnings-as-errors

- name: Run "mix format"
run: mix format --check-formatted

test:
name: Test (Elixir ${{matrix.elixir}} | Erlang/OTP ${{matrix.otp}})
runs-on: ubuntu-latest
name: OTP ${{matrix.otp}} / Elixir ${{matrix.elixir}}
strategy:
fail-fast: false
matrix:
otp: ['25.0']
elixir: ['1.13.4']
include:
- otp: 26.x
elixir: 1.16.x
coverage: true
- otp: 27.x
elixir: 1.17.x
env:
MIX_ENV: test
steps:
- uses: actions/checkout@v2
- uses: erlef/setup-beam@v1
- uses: actions/checkout@v2.3.1

- name: Install OTP and Elixir
uses: erlef/setup-beam@v1
with:
otp-version: ${{matrix.otp}}
elixir-version: ${{matrix.elixir}}
- run: mix deps.get
- run: mix format --check-formatted
- run: mix credo --strict
- run: mix test

- name: Install dependencies
run: mix deps.get --only test

- name: Run tests
run: mix test --trace
12 changes: 12 additions & 0 deletions lib/ham/cache.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,16 @@ defmodule Ham.Cache do
def get(key) do
:persistent_term.get(key, nil)
end

def fetch(key, fetcher) do
case get(key) do
nil ->
value = fetcher.()
put(key, value)
value

value ->
value
end
end
end
46 changes: 21 additions & 25 deletions lib/ham/type_checker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -98,31 +98,27 @@ defmodule Ham.TypeChecker do
end

defp fetch_specs(module, key, fetcher) do
case Cache.get({key, module}) do
nil ->
case fetcher.(module) do
{:ok, specs} ->
specs =
specs
|> Map.new(fn {{function_name, arity}, typespecs} ->
typespecs =
typespecs
|> Enum.map(&guards_to_annotated_types(&1))
|> Enum.map(&Utils.replace_user_types(&1, module))

{{function_name, arity}, typespecs}
end)

Cache.put({key, module}, specs)
{:ok, specs}

:error ->
{:error, {:module_fetch_failure, module}}
end

specs ->
{:ok, specs}
end
Cache.fetch({key, module}, fn ->
case fetcher.(module) do
{:ok, specs} ->
{:ok, build_typespecs_map(specs, module)}

:error ->
{:error, {:module_fetch_failure, module}}
end
end)
end

defp build_typespecs_map(specs, module) do
specs
|> Map.new(fn {{function_name, arity}, typespecs} ->
typespecs =
typespecs
|> Enum.map(&guards_to_annotated_types(&1))
|> Enum.map(&Utils.replace_user_types(&1, module))

{{function_name, arity}, typespecs}
end)
end

defp guards_to_annotated_types({:type, _, :fun, _} = typespec), do: typespec
Expand Down

0 comments on commit 7d20d56

Please sign in to comment.