Various typespec and compilation improvements
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
@@ -19,9 +19,17 @@ defmodule Mobilizon.Service.Activity.Renderer do
|
||||
require Logger
|
||||
import Mobilizon.Web.Gettext, only: [dgettext: 3]
|
||||
|
||||
@type render :: %{body: String.t(), url: String.t()}
|
||||
@type render :: %{
|
||||
body: String.t(),
|
||||
url: String.t(),
|
||||
timestamp: String.t(),
|
||||
locale: String.t(),
|
||||
title: String.t()
|
||||
}
|
||||
|
||||
@callback render(entity :: Activity.t(), Keyword.t()) :: render()
|
||||
@type common_render :: %{body: String.t(), url: String.t()}
|
||||
|
||||
@callback render(entity :: Activity.t(), Keyword.t()) :: common_render()
|
||||
|
||||
@spec render(Activity.t()) :: render()
|
||||
def render(%Activity{} = activity, options \\ []) do
|
||||
@@ -43,6 +51,7 @@ defmodule Mobilizon.Service.Activity.Renderer do
|
||||
res
|
||||
end
|
||||
|
||||
@spec do_render(Activity.t(), Keyword.t()) :: common_render()
|
||||
defp do_render(%Activity{type: type} = activity, options) do
|
||||
case type do
|
||||
:discussion -> Discussion.render(activity, options)
|
||||
|
||||
@@ -12,7 +12,7 @@ defmodule Mobilizon.Service.Activity.Utils do
|
||||
|> add_activity_object()
|
||||
end
|
||||
|
||||
@spec add_activity_object(Activity.t()) :: Activity.t()
|
||||
@spec add_activity_object(Activity.t()) :: map()
|
||||
def add_activity_object(%Activity{} = activity) do
|
||||
Map.put(activity, :object, ActivityService.object(activity))
|
||||
end
|
||||
|
||||
@@ -143,15 +143,13 @@ defmodule Mobilizon.Service.Geospatial.Nominatim do
|
||||
if is_nil(value), do: url, else: do_add_parameter(url, key, value)
|
||||
end
|
||||
|
||||
@spec do_add_parameter(String.t(), atom(), any()) :: String.t()
|
||||
@spec do_add_parameter(String.t(), :zoom | :country_code | :api_key, any()) :: String.t()
|
||||
defp do_add_parameter(url, :zoom, zoom),
|
||||
do: "#{url}&zoom=#{zoom}"
|
||||
|
||||
@spec do_add_parameter(String.t(), atom(), any()) :: String.t()
|
||||
defp do_add_parameter(url, :country_code, country_code),
|
||||
do: "#{url}&countrycodes=#{country_code}"
|
||||
|
||||
@spec do_add_parameter(String.t(), atom(), any()) :: String.t()
|
||||
defp do_add_parameter(url, :api_key, api_key),
|
||||
do: "#{url}&key=#{api_key}"
|
||||
|
||||
|
||||
@@ -67,7 +67,7 @@ defmodule Mobilizon.Service.Geospatial.Provider do
|
||||
@doc """
|
||||
Returns a `Geo.Point` for given coordinates
|
||||
"""
|
||||
@spec coordinates([number], number) :: Geo.Point.t()
|
||||
@spec coordinates([number], number) :: Geo.Point.t() | nil
|
||||
def coordinates(coords, srid \\ 4326)
|
||||
|
||||
def coordinates([x, y], srid) when is_number(x) and is_number(y) do
|
||||
@@ -78,7 +78,6 @@ defmodule Mobilizon.Service.Geospatial.Provider do
|
||||
%Geo.Point{coordinates: {String.to_float(x), String.to_float(y)}, srid: srid}
|
||||
end
|
||||
|
||||
@spec coordinates(any) :: nil
|
||||
def coordinates(_, _), do: nil
|
||||
|
||||
@spec endpoint(atom()) :: String.t()
|
||||
|
||||
@@ -3,7 +3,31 @@ defmodule Mobilizon.Service.Guards do
|
||||
Various guards
|
||||
"""
|
||||
|
||||
defguard is_valid_string?(value) when is_binary(value) and value != ""
|
||||
@doc """
|
||||
Returns `true` if `term` is a valid string and not empty.
|
||||
|
||||
defguard is_valid_list?(value) when is_list(value) and length(value) > 0
|
||||
## Examples
|
||||
|
||||
iex> is_valid_string("one")
|
||||
true
|
||||
iex> is_valid_string("")
|
||||
false
|
||||
iex> is_valid_string(2)
|
||||
false
|
||||
"""
|
||||
defguard is_valid_string(term) when is_binary(term) and term != ""
|
||||
|
||||
@doc """
|
||||
Returns `true` if `term` is a valid list and not empty.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> is_valid_list(["one"])
|
||||
true
|
||||
iex> is_valid_list([])
|
||||
false
|
||||
iex> is_valid_list("foo")
|
||||
false
|
||||
"""
|
||||
defguard is_valid_list(term) when is_list(term) and length(term) > 0
|
||||
end
|
||||
|
||||
@@ -64,7 +64,7 @@ defmodule Mobilizon.Service.LanguageDetection do
|
||||
|
||||
def normalize(language) do
|
||||
case Cldr.AcceptLanguage.parse(language, Mobilizon.Cldr) do
|
||||
{:ok, [{_, tag}]} ->
|
||||
{:ok, [{_, %Cldr.LanguageTag{} = tag}]} ->
|
||||
tag.language
|
||||
|
||||
_ ->
|
||||
|
||||
Reference in New Issue
Block a user