Skip to content

Commit

Permalink
fix: compilation under elixir 1.6 by hiding since docs behind macro
Browse files Browse the repository at this point in the history
  • Loading branch information
SpaceEEC committed Nov 3, 2018
1 parent f02424f commit b8aa6c4
Show file tree
Hide file tree
Showing 21 changed files with 226 additions and 85 deletions.
8 changes: 5 additions & 3 deletions lib/structs.ex
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
defmodule Crux.Structs do
@moduledoc since: "0.1.0"
@moduledoc """
Provides a unified function to create one or a list of structs, invoking their `create/1` function if available.
"""

alias Crux.Structs.Util
require Util

Util.modulesince("0.1.0")

@doc """
Can be implemented by structs to transform the inital data.
Expand Down Expand Up @@ -69,7 +71,7 @@ defmodule Crux.Structs do
"""
@spec create(data :: map(), target :: module()) :: struct()
@spec create(data :: list(), target :: module()) :: list(struct())
@doc since: "0.1.0"
Util.since("0.1.0")
def create(data, target)
def create(nil, _target), do: nil

Expand All @@ -80,7 +82,7 @@ defmodule Crux.Structs do
def create(%{__struct__: target} = data, target), do: data

def create(data, target) do
if Keyword.has_key?(target.__info__(:functions), :create) do
if :erlang.function_exported(target, :create, 1) do
target.create(data)
else
data = Util.atomify(data)
Expand Down
9 changes: 7 additions & 2 deletions lib/structs/attachment.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
defmodule Crux.Structs.Attachment do
@moduledoc since: "0.1.0"
@moduledoc """
Represents a Discord [Attachment Object](https://discordapp.com/developers/docs/resources/channel#attachment-object-attachment-structure)
Expand All @@ -9,6 +8,9 @@ defmodule Crux.Structs.Attachment do
@behaviour Crux.Structs

alias Crux.Structs.Util
require Util

Util.modulesince("0.1.0")

defstruct(
id: nil,
Expand All @@ -20,7 +22,8 @@ defmodule Crux.Structs.Attachment do
width: nil
)

@typedoc since: "0.1.0"
Util.typesince("0.1.0")

@type t :: %__MODULE__{
id: Crux.Rest.snowflake(),
filename: String.t(),
Expand All @@ -36,6 +39,8 @@ defmodule Crux.Structs.Attachment do
> Automatically invoked by `Crux.Structs.create/2`.
"""
Util.since("0.1.0")

def create(data) do
data =
data
Expand Down
10 changes: 7 additions & 3 deletions lib/structs/audit_log.ex
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
defmodule Crux.Structs.AuditLog do
@moduledoc since: "0.1.6"
@moduledoc """
Represents a Discord [Audit Log Object](https://discordapp.com/developers/docs/resources/audit-log#audit-log-object)
"""

@behaviour Crux.Structs

alias Crux.Structs.{AuditLogEntry, User, Util, Webhook}
require Util

Util.modulesince("0.1.6")

defstruct(
webhooks: %{},
users: %{},
audit_log_entries: %{}
)

@typedoc since: "0.1.6"
Util.typesince("0.1.6")

@type t :: %__MODULE__{
webhooks: %{Crux.Rest.snowflake() => Webhook.t()},
users: %{Crux.Rest.snowflake() => User.t()},
Expand All @@ -26,7 +29,8 @@ defmodule Crux.Structs.AuditLog do
> Automatically invoked by `Crux.Structs.create/2`.
"""
@doc since: "0.1.6"
Util.since("0.1.6")

def create(data) do
data =
data
Expand Down
13 changes: 9 additions & 4 deletions lib/structs/audit_log_change.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
defmodule Crux.Structs.AuditLogChange do
@moduledoc since: "0.1.6"
@moduledoc """
Represents a Discord [Audit Log Change Object](https://discordapp.com/developers/docs/resources/audit-log#audit-log-change)
"""
Expand All @@ -8,6 +7,9 @@ defmodule Crux.Structs.AuditLogChange do

alias Crux.Structs
alias Crux.Structs.{Overwrite, Role, Util}
require Util

Util.modulesince("0.1.6")

defstruct(
new_value: nil,
Expand All @@ -20,7 +22,8 @@ defmodule Crux.Structs.AuditLogChange do
> Note that the Role object returned by Discord in audit logs is a partial role that only contains id and name.
"""
@typedoc since: "0.1.6"
Util.typesince("0.1.6")

@type audit_log_change_value ::
String.t()
| Crux.Rest.snowflake()
Expand All @@ -29,7 +32,8 @@ defmodule Crux.Structs.AuditLogChange do
| [Role.t()]
| [Overwrite.t()]

@typedoc since: "0.1.6"
Util.typesince("0.1.6")

@type t :: %__MODULE__{
new_value: audit_log_change_value() | nil,
old_value: audit_log_change_value() | nil,
Expand All @@ -41,7 +45,8 @@ defmodule Crux.Structs.AuditLogChange do
> Automatically invoked by `Crux.Structs.create/2`.
"""
@doc since: "0.1.6"
Util.since("0.1.6")

def create(data) do
data =
data
Expand Down
17 changes: 11 additions & 6 deletions lib/structs/audit_log_entry.ex
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
defmodule Crux.Structs.AuditLogEntry do
@moduledoc since: "0.1.6"
@moduledoc """
Represents a Discord [Audit Log Object](https://discordapp.com/developers/docs/resources/audit-log)
"""

@behaviour Crux.Structs

alias Crux.Structs.{AuditLogChange, Util}
require Util

Util.modulesince("0.1.6")

@audit_log_events %{
guild_update: 1,
Expand Down Expand Up @@ -40,7 +42,8 @@ defmodule Crux.Structs.AuditLogEntry do
@typedoc """
Union type of audit log event name atoms.
"""
@typedoc since: "0.1.6"
Util.typesince("0.1.6")

@type event_name ::
:guild_update
| :channel_create
Expand Down Expand Up @@ -75,14 +78,14 @@ defmodule Crux.Structs.AuditLogEntry do
Returns a map of all audit log event names with their id
"""
@spec events() :: %{event_name => non_neg_integer()}
@doc since: "0.1.6"
Util.since("0.1.6")
def events, do: @audit_log_events

@doc """
Gets the event name from the action type id
"""
@spec event_name(action_type :: non_neg_integer()) :: atom()
@doc since: "0.1.6"
Util.since("0.1.6")
def event_name(action_type), do: Map.get(@audit_log_events_key, action_type)

defstruct(
Expand All @@ -95,7 +98,8 @@ defmodule Crux.Structs.AuditLogEntry do
reason: nil
)

@typedoc since: "0.1.6"
Util.typesince("0.1.6")

@type t :: %__MODULE__{
id: Crux.Rest.snowflake(),
target_id: Crux.Rest.snowflake(),
Expand All @@ -111,7 +115,8 @@ defmodule Crux.Structs.AuditLogEntry do
> Automatically invoked by `Crux.Structs.create/2`.
"""
@doc since: "0.1.6"
Util.since("0.1.6")

def create(data) do
data =
data
Expand Down
12 changes: 8 additions & 4 deletions lib/structs/channel.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
defmodule Crux.Structs.Channel do
@moduledoc since: "0.1.0"
@moduledoc """
Represents a Discord [Channel Object](https://discordapp.com/developers/docs/resources/channel#channel-object-channel-structure).
Expand Down Expand Up @@ -33,6 +32,9 @@ defmodule Crux.Structs.Channel do
@behaviour Crux.Structs

alias Crux.Structs.{Overwrite, Util}
require Util

Util.modulesince("0.1.0")

defstruct(
application_id: nil,
Expand All @@ -55,7 +57,8 @@ defmodule Crux.Structs.Channel do
user_limit: nil
)

@typedoc since: "0.1.0"
Util.typesince("0.1.0")

@type t :: %__MODULE__{
application_id: Crux.Rest.snowflake(),
bitrate: integer(),
Expand All @@ -82,7 +85,8 @@ defmodule Crux.Structs.Channel do
> Automatically invoked by `Crux.Structs.create/2`
"""
@doc since: "0.1.0"
Util.since("0.1.0")

def create(data) do
data =
data
Expand Down Expand Up @@ -116,7 +120,7 @@ defmodule Crux.Structs.Channel do
```
"""
@spec to_mention(user :: Crux.Structs.Channel.t()) :: String.t()
@doc since: "0.1.1"
Util.since("0.1.1")
def to_mention(%__MODULE__{id: id}), do: "<##{id}>"

defimpl String.Chars, for: Crux.Structs.Channel do
Expand Down
9 changes: 7 additions & 2 deletions lib/structs/embed.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
defmodule Crux.Structs.Embed do
@moduledoc since: "0.1.0"
@moduledoc """
Represents a Discord [Embed Object](https://discordapp.com/developers/docs/resources/channel#embed-object-embed-structure).
Expand All @@ -8,6 +7,11 @@ defmodule Crux.Structs.Embed do

@behaviour Crux.Structs

alias Crux.Structs.Util
require Util

Util.modulesince("0.1.0")

defstruct(
title: nil,
type: "rich",
Expand All @@ -24,7 +28,8 @@ defmodule Crux.Structs.Embed do
fields: []
)

@typedoc since: "0.1.0"
Util.typesince("0.1.0")

@type t :: %__MODULE__{
title: String.t() | nil,
type: String.t() | nil,
Expand Down
12 changes: 8 additions & 4 deletions lib/structs/emoji.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
defmodule Crux.Structs.Emoji do
@moduledoc since: "0.1.0"
@moduledoc """
Represents a Discord [Emoji Object](https://discordapp.com/developers/docs/resources/emoji#emoji-object-emoji-structure).
Expand All @@ -10,6 +9,9 @@ defmodule Crux.Structs.Emoji do
@behaviour Crux.Structs

alias Crux.Structs.Util
require Util

Util.modulesince("0.1.0")

defstruct(
animated: nil,
Expand All @@ -21,7 +23,8 @@ defmodule Crux.Structs.Emoji do
managed: nil
)

@typedoc since: "0.1.0"
Util.typesince("0.1.0")

@type t :: %__MODULE__{
animated: boolean() | nil,
id: Crux.Rest.snowflake() | nil,
Expand All @@ -37,7 +40,8 @@ defmodule Crux.Structs.Emoji do
> Automatically invoked by `Crux.Structs.create/2`.
"""
@doc since: "0.1.0"
Util.since("0.1.0")

def create(data) do
data =
data
Expand Down Expand Up @@ -93,7 +97,7 @@ defmodule Crux.Structs.Emoji do
"""
@spec to_identifier(emoji :: Crux.Structs.Emoji.t() | Crux.Structs.Reaction.t() | String.t()) ::
String.t()
@doc since: "0.1.1"
Util.since("0.1.1")
def to_identifier(%Crux.Structs.Reaction{emoji: emoji}), do: to_identifier(emoji)
def to_identifier(%__MODULE__{id: nil, name: name}), do: name |> URI.encode_www_form()
def to_identifier(%__MODULE__{id: id, name: name, animated: true}), do: "a:#{name}:#{id}"
Expand Down
10 changes: 7 additions & 3 deletions lib/structs/guild.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
defmodule Crux.Structs.Guild do
@moduledoc since: "0.1.0"
@moduledoc """
Represents a Discord [Guild Object](https://discordapp.com/developers/docs/resources/guild#guild-object-guild-structure).
Expand All @@ -12,6 +11,9 @@ defmodule Crux.Structs.Guild do
@behaviour Crux.Structs

alias Crux.Structs.{Member, Role, Util, VoiceState}
require Util

Util.modulesince("0.1.0")

defstruct(
id: nil,
Expand Down Expand Up @@ -42,7 +44,8 @@ defmodule Crux.Structs.Guild do
# presences: %{}
)

@typedoc since: "0.1.0"
Util.typesince("0.1.0")

@type t :: %__MODULE__{
id: Crux.Rest.snowflake(),
name: String.t(),
Expand Down Expand Up @@ -77,7 +80,8 @@ defmodule Crux.Structs.Guild do
> Automatically invoked by `Crux.Structs.create/2`.
"""
# TODO: Write a test
@doc since: "0.1.0"
Util.since("0.1.0")

def create(data) do
data =
data
Expand Down
Loading

0 comments on commit b8aa6c4

Please sign in to comment.