Current section
Files
Jump to
Current section
Files
lib/structs/unity_asset_bundle.ex
defmodule Manganese.SerializationKit.Structs.UnityAssetBundle do
@moduledoc """
"""
alias Manganese.SerializationKit.Structs
alias Manganese.SerializationKit.Enumerations
@typedoc """
"""
@type t :: %Structs.UnityAssetBundle{
assets: [ Structs.UnityAsset.t ],
metadata: map | nil
}
defstruct [
:assets,
:metadata
]
# Deserialization
@doc """
"""
@spec from_map(t_external) :: t
def from_map(%{ "assets" => assets } = map) do
%Structs.UnityAssetBundle{
assets: assets |> Enum.map(&Structs.UnityAsset.from_map/1),
metadata: Map.get(map, "metadata", nil)
}
end
# Serialization
@type t_external :: map
@doc """
"""
@spec to_map(t) :: t_external
def to_map(%Structs.UnityAssetBundle{
assets: assets,
metadata: metadata
}) do
%{
"assets" => assets |> Enum.map(&Structs.UnityAsset.to_map/1),
"metadata" => metadata
}
end
# Utilities
@doc """
"""
@spec has_asset(t, String.t, Enumerations.UnityAssetType.t | nil) :: boolean
def has_asset(%Structs.UnityAssetBundle{ assets: assets }, asset_path, asset_type \\ nil) do
case Enum.find(assets, fn asset -> asset.path == asset_path end) do
%_{ type: type } ->
cond do
not is_nil(asset_type) and type != asset_type -> false
true -> true
end
_ ->
false
end
end
# Ecto type
@behaviour Ecto.Type
@doc """
The PostgreSQL type used to represent a unity asset bundle.
"""
@spec type :: :map
def type, do: :map
@doc false
def cast(%Structs.UnityAssetBundle{} = unity_asset_bundle) do
{ :ok, unity_asset_bundle }
end
@doc false
def cast(%{} = map) do
{ :ok, from_map map }
end
@doc false
def cast(nil) do
{ :ok, nil }
end
@doc false
def cast(_), do: :error
@doc false
def load(%{} = map) do
{ :ok, from_map map }
end
@doc false
def dump(%Structs.UnityAssetBundle{} = unity_asset_bundle) do
{ :ok, to_map unity_asset_bundle }
end
end