Skip to content

Commit

Permalink
Merge pull request #108 from noaccOS/refactor/standard-ecto-functions…
Browse files Browse the repository at this point in the history
…-bangs

feat: bangs of standard ecto.type functions
  • Loading branch information
Annopaolo authored Sep 23, 2024
2 parents 6d2f92a + bab5ccd commit 48eba84
Show file tree
Hide file tree
Showing 10 changed files with 198 additions and 111 deletions.
32 changes: 22 additions & 10 deletions lib/astarte_core/interface/aggregation.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# This file is part of Astarte.
#
# Copyright 2017-2018 Ispirata Srl
# Copyright 2017-2024 SECO Mind Srl
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -48,8 +48,23 @@ defmodule Astarte.Core.Interface.Aggregation do
end
end

def cast(int) when is_integer(int) do
load(int)
end

def cast(_), do: :error

def cast!(value) do
case cast(value) do
{:ok, aggregation} ->
aggregation

:error ->
raise ArgumentError,
message: "#{inspect(value)} is not a valid aggregation representation"
end
end

@impl true
def dump(aggregation) when is_atom(aggregation) do
case aggregation do
Expand All @@ -59,7 +74,7 @@ defmodule Astarte.Core.Interface.Aggregation do
end
end

def to_int(aggregation) when is_atom(aggregation) do
def dump!(aggregation) when is_atom(aggregation) do
case dump(aggregation) do
{:ok, aggregation_int} -> aggregation_int
:error -> raise ArgumentError, message: "#{inspect(aggregation)} is not a valid aggregation"
Expand All @@ -75,14 +90,11 @@ defmodule Astarte.Core.Interface.Aggregation do
end
end

def from_int(aggregation_int) when is_integer(aggregation_int) do
case load(aggregation_int) do
{:ok, aggregation} ->
aggregation
def to_int(aggregation) when is_atom(aggregation) do
dump!(aggregation)
end

:error ->
raise ArgumentError,
message: "#{aggregation_int} is not a valid aggregation int representation"
end
def from_int(int) when is_integer(int) do
cast!(int)
end
end
31 changes: 21 additions & 10 deletions lib/astarte_core/interface/ownership.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# This file is part of Astarte.
#
# Copyright 2017-2018 Ispirata Srl
# Copyright 2017-2024 SECO Mind Srl
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -60,8 +60,22 @@ defmodule Astarte.Core.Interface.Ownership do
end
end

def cast(int) when is_integer(int) do
load(int)
end

def cast(_), do: :error

def cast!(value) do
case cast(value) do
{:ok, ownership} ->
ownership

:error ->
raise ArgumentError, message: "#{inspect(value)} is not a valid ownership representation"
end
end

@impl true
def dump(ownership) when is_atom(ownership) do
case ownership do
Expand All @@ -71,7 +85,7 @@ defmodule Astarte.Core.Interface.Ownership do
end
end

def to_int(ownership) when is_atom(ownership) do
def dump!(ownership) when is_atom(ownership) do
case dump(ownership) do
{:ok, ownership_int} -> ownership_int
:error -> raise ArgumentError, message: "#{inspect(ownership)} is not a valid ownership"
Expand All @@ -87,14 +101,11 @@ defmodule Astarte.Core.Interface.Ownership do
end
end

def from_int(ownership_int) when is_integer(ownership_int) do
case load(ownership_int) do
{:ok, ownership} ->
ownership
def to_int(ownership) when is_atom(ownership) do
dump!(ownership)
end

:error ->
raise ArgumentError,
message: "#{ownership_int} is not a valid ownership int representation"
end
def from_int(int) when is_integer(int) do
cast!(int)
end
end
32 changes: 22 additions & 10 deletions lib/astarte_core/interface/type.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# This file is part of Astarte.
#
# Copyright 2017-2018 Ispirata Srl
# Copyright 2017-2024 SECO Mind Srl
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -48,8 +48,23 @@ defmodule Astarte.Core.Interface.Type do
end
end

def cast(int) when is_integer(int) do
load(int)
end

def cast(_), do: :error

def cast!(value) do
case cast(value) do
{:ok, type} ->
type

:error ->
raise ArgumentError,
message: "#{inspect(value)} is not a valid interface type representation"
end
end

@impl true
def dump(type) when is_atom(type) do
case type do
Expand All @@ -59,7 +74,7 @@ defmodule Astarte.Core.Interface.Type do
end
end

def to_int(type) when is_atom(type) do
def dump!(type) when is_atom(type) do
case dump(type) do
{:ok, type_int} -> type_int
:error -> raise ArgumentError, message: "#{inspect(type)} is not a valid interface type"
Expand All @@ -75,14 +90,11 @@ defmodule Astarte.Core.Interface.Type do
end
end

def from_int(type_int) when is_integer(type_int) do
case load(type_int) do
{:ok, type} ->
type
def to_int(interface) when is_atom(interface) do
dump!(interface)
end

:error ->
raise ArgumentError,
message: "#{type_int} is not a valid interface type int representation"
end
def from_int(int) when is_integer(int) do
cast!(int)
end
end
10 changes: 5 additions & 5 deletions lib/astarte_core/interface_descriptor.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# This file is part of Astarte.
#
# Copyright 2017 Ispirata Srl
# Copyright 2017-2024 SECO Mind Srl
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -66,11 +66,11 @@ defmodule Astarte.Core.InterfaceDescriptor do
name: name,
major_version: major_version,
minor_version: minor_version,
type: Type.from_int(type),
ownership: Ownership.from_int(ownership),
aggregation: Aggregation.from_int(aggregation),
type: Type.cast!(type),
ownership: Ownership.cast!(ownership),
aggregation: Aggregation.cast!(aggregation),
storage: storage,
storage_type: StorageType.from_int(storage_type),
storage_type: StorageType.cast!(storage_type),
automaton:
{:erlang.binary_to_term(automaton_transitions),
:erlang.binary_to_term(automaton_accepting_states)},
Expand Down
30 changes: 15 additions & 15 deletions lib/astarte_core/mapping.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# This file is part of Astarte.
#
# Copyright 2017-2018 Ispirata Srl
# Copyright 2017-2024 SECO Mind Srl
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -185,31 +185,31 @@ defmodule Astarte.Core.Mapping do
interface_id: interface_id
} = db_result

database_retention_policy =
database_retention_policy
|> Kernel.||(:no_ttl)
|> DatabaseRetentionPolicy.cast!()

doc = Map.get(db_result, :doc)
description = Map.get(db_result, :description)

%Mapping{
endpoint: endpoint,
value_type: ValueType.from_int(value_type),
reliability: Reliability.from_int(reliability),
retention: Retention.from_int(retention),
value_type: ValueType.cast!(value_type),
reliability: Reliability.cast!(reliability),
retention: Retention.cast!(retention),
expiry: expiry,
database_retention_policy:
database_retention_policy_from_maybe_int(database_retention_policy),
database_retention_policy: database_retention_policy,
database_retention_ttl: database_retention_ttl,
allow_unset: allow_unset,
explicit_timestamp: explicit_timestamp,
endpoint_id: endpoint_id,
interface_id: interface_id,
doc: Map.get(db_result, :doc),
description: Map.get(db_result, :description)
doc: doc,
description: description
}
end

defp database_retention_policy_from_maybe_int(database_retention_policy) do
case database_retention_policy do
nil -> :no_ttl
_any_int -> DatabaseRetentionPolicy.from_int(database_retention_policy)
end
end

defp validate_not_set_unless(changeset, field, param, values) do
unless Enum.member?(values, param) do
validate_change(changeset, field, fn field, value ->
Expand Down
36 changes: 24 additions & 12 deletions lib/astarte_core/mapping/database_retention_policy.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# This file is part of Astarte.
#
# Copyright 2017-2018 Ispirata Srl
# Copyright 2017-2024 SECO Mind Srl
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -48,8 +48,23 @@ defmodule Astarte.Core.Mapping.DatabaseRetentionPolicy do
end
end

def cast(int) when is_integer(int) do
load(int)
end

def cast(_), do: :error

def cast!(value) do
case cast(value) do
{:ok, policy} ->
policy

:error ->
raise ArgumentError,
message: "#{inspect(value)} is not a valid database retention policy representation"
end
end

@impl true
def dump(policy) when is_atom(policy) do
case policy do
Expand All @@ -59,10 +74,10 @@ defmodule Astarte.Core.Mapping.DatabaseRetentionPolicy do
end
end

def to_int(policy) when is_atom(policy) do
def dump!(policy) when is_atom(policy) do
case dump(policy) do
{:ok, int} ->
int
{:ok, policy_int} ->
policy_int

:error ->
raise ArgumentError,
Expand All @@ -79,14 +94,11 @@ defmodule Astarte.Core.Mapping.DatabaseRetentionPolicy do
end
end

def from_int(policy_int) when is_integer(policy_int) do
case load(policy_int) do
{:ok, policy} ->
policy
def to_int(database_retention_policy) when is_atom(database_retention_policy) do
dump!(database_retention_policy)
end

:error ->
raise ArgumentError,
message: "#{policy_int} is not a valid database retention policy int representation"
end
def from_int(int) when is_integer(int) do
cast!(int)
end
end
34 changes: 23 additions & 11 deletions lib/astarte_core/mapping/reliability.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# This file is part of Astarte.
#
# Copyright 2017-2018 Ispirata Srl
# Copyright 2017-2024 SECO Mind Srl
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -51,8 +51,23 @@ defmodule Astarte.Core.Mapping.Reliability do
end
end

def cast(int) when is_integer(int) do
load(int)
end

def cast(_), do: :error

def cast!(reliability) do
case cast(reliability) do
{:ok, reliability} ->
reliability

:error ->
raise ArgumentError,
message: "#{inspect(reliability)} is not a valid reliability representation"
end
end

@impl true
def dump(reliability) when is_atom(reliability) do
case reliability do
Expand All @@ -63,9 +78,9 @@ defmodule Astarte.Core.Mapping.Reliability do
end
end

def to_int(reliability) when is_atom(reliability) do
def dump!(reliability) when is_atom(reliability) do
case dump(reliability) do
{:ok, int} -> int
{:ok, reliability_int} -> reliability_int
:error -> raise ArgumentError, message: "#{inspect(reliability)} is not a valid reliability"
end
end
Expand All @@ -80,14 +95,11 @@ defmodule Astarte.Core.Mapping.Reliability do
end
end

def from_int(reliability_int) when is_integer(reliability_int) do
case load(reliability_int) do
{:ok, reliability} ->
reliability
def to_int(reliability) when is_atom(reliability) do
dump!(reliability)
end

:error ->
raise ArgumentError,
message: "#{reliability_int} is not a valid reliability int representation"
end
def from_int(int) when is_integer(int) do
cast!(int)
end
end
Loading

0 comments on commit 48eba84

Please sign in to comment.