107 lines
2.9 KiB
Elixir
107 lines
2.9 KiB
Elixir
defmodule Dexcord.Test.Color do
|
|
@moduledoc false
|
|
use Dexcord.Enum, red: 0, green: 1, blue: 4
|
|
end
|
|
|
|
defmodule Dexcord.Test.TestFlags do
|
|
@moduledoc false
|
|
use Dexcord.Flags, alpha: 0, beta: 1, gamma: 4
|
|
end
|
|
|
|
defmodule Dexcord.Test.Gadget do
|
|
@moduledoc false
|
|
use Dexcord.Struct
|
|
|
|
discord_struct do
|
|
field :id, :snowflake
|
|
field :name, :string
|
|
end
|
|
end
|
|
|
|
defmodule Dexcord.Test.Widget do
|
|
@moduledoc false
|
|
use Dexcord.Struct
|
|
|
|
discord_struct do
|
|
field :id, :snowflake
|
|
field :name, :string
|
|
field :count, :integer
|
|
field :enabled, :boolean
|
|
field :created_at, :datetime
|
|
field :gadget, {:struct, Dexcord.Test.Gadget}
|
|
field :gadgets, {:list, {:struct, Dexcord.Test.Gadget}}, default: []
|
|
field :tag_ids, {:list, :snowflake}, default: []
|
|
field :color, {:enum, Dexcord.Test.Color}
|
|
field :flags, {:flags, Dexcord.Test.TestFlags}
|
|
field :parent, {:struct, __MODULE__}, tristate: true
|
|
field :premium, :presence
|
|
field :note, :string, default: "n/a"
|
|
field :extra, :raw
|
|
hydrate :gadget_full, from: :id, type: Dexcord.Test.Gadget
|
|
end
|
|
end
|
|
|
|
# A plain-data field-group provider for exercising `include_fields/1`.
|
|
defmodule Dexcord.Test.SharedGroup do
|
|
@moduledoc false
|
|
|
|
def __included_fields__ do
|
|
[
|
|
{:shared_a, :string, []},
|
|
{:shared_b, {:list, :snowflake}, [default: []]}
|
|
]
|
|
end
|
|
end
|
|
|
|
# Exercises Task 1's DSL extensions: `:number`, string-wire flags, the
|
|
# `wire_string:` opt, and `include_fields/1` interleaved with own fields.
|
|
defmodule Dexcord.Test.Doohickey do
|
|
@moduledoc false
|
|
use Dexcord.Struct
|
|
|
|
discord_struct do
|
|
field :id, :snowflake
|
|
field :ratio, :number
|
|
field :perms, {:flags, Dexcord.Test.TestFlags}, wire_string: true
|
|
field :bits, {:flags, Dexcord.Test.TestFlags}
|
|
include_fields Dexcord.Test.SharedGroup
|
|
field :tail, :string
|
|
end
|
|
end
|
|
|
|
# Exercises Phase 3 Task 1: the `{:id_map, type}` snowflake-keyed dictionary
|
|
# field type (Discord's resolved-data shape).
|
|
defmodule Dexcord.Test.Contraption do
|
|
@moduledoc false
|
|
use Dexcord.Struct
|
|
|
|
discord_struct do
|
|
field :id, :snowflake
|
|
field :things, {:id_map, {:struct, Dexcord.Test.Gadget}}, default: %{}
|
|
end
|
|
end
|
|
|
|
# Exercises Phase 3 Task 1: overriding a generated function and calling
|
|
# `super/1` to post-process the DSL-generated decode.
|
|
defmodule Dexcord.Test.Overridden do
|
|
@moduledoc false
|
|
use Dexcord.Struct
|
|
|
|
discord_struct do
|
|
field :id, :snowflake
|
|
field :name, :string
|
|
end
|
|
|
|
# NOTE: the override must NOT pattern-match `%__MODULE__{}` — `defstruct` is
|
|
# injected by `@before_compile` and is not yet defined where the body compiles.
|
|
# Match the struct as a plain map (a struct IS a map) guarded by `is_struct/2`.
|
|
def from_map(map) do
|
|
case super(map) do
|
|
%{name: name} = struct when is_struct(struct, __MODULE__) and is_binary(name) ->
|
|
%{struct | name: String.upcase(name)}
|
|
|
|
other ->
|
|
other
|
|
end
|
|
end
|
|
end
|