@ -9,6 +9,8 @@ defmodule Explorer.Helper do
import Ecto.Query , only : [ where : 3 ]
@max_safe_integer round ( :math . pow ( 2 , 63 ) ) - 1
@spec decode_data ( binary ( ) | map ( ) , list ( ) ) :: list ( ) | nil
def decode_data ( " 0x " , types ) do
for _ <- types , do : nil
@ -67,6 +69,35 @@ defmodule Explorer.Helper do
number
end
@doc """
Converts a string to an integer , ensuring it ' s non-negative and within the
acceptable range for database insertion .
## Examples
iex > safe_parse_non_negative_integer ( " 0 " )
{ :ok , 0 }
iex > safe_parse_non_negative_integer ( " -1 " )
{ :error , :negative_integer }
iex > safe_parse_non_negative_integer ( " 27606393966689717254124294199939478533331961967491413693980084341759630764504 " )
{ :error , :too_big_integer }
"""
def safe_parse_non_negative_integer ( string ) do
case Integer . parse ( string ) do
{ num , " " } ->
case num do
_ when num > @max_safe_integer -> { :error , :too_big_integer }
_ when num < 0 -> { :error , :negative_integer }
_ -> { :ok , num }
end
_ ->
{ :error , :invalid_integer }
end
end
@doc """
Function to preload a ` struct ` for each element of the ` list ` .
You should specify a primary key for a ` struct ` in ` references_field ` ,