-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 4a9eaa2
Showing
16 changed files
with
553 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Used by "mix format" | ||
[ | ||
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# The directory Mix will write compiled artifacts to. | ||
/_build/ | ||
|
||
.elixir_ls/ | ||
|
||
# If you run "mix test --cover", coverage assets end up here. | ||
/cover/ | ||
|
||
# The directory Mix downloads your dependencies sources to. | ||
/deps/ | ||
|
||
# Where third-party dependencies like ExDoc output generated docs. | ||
/doc/ | ||
|
||
# Ignore .fetch files in case you like to edit your project deps locally. | ||
/.fetch | ||
|
||
# If the VM crashes, it generates a dump, let's ignore it too. | ||
erl_crash.dump | ||
|
||
# Also ignore archive artifacts (built via "mix archive.build"). | ||
*.ez | ||
|
||
# Ignore package tarball (built via "mix hex.build"). | ||
c3p0-*.tar | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
Copyright (c) 2019 Daniel Neighman | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
"Software"), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# C3P0 | ||
|
||
C3P0 contains protocol definitions. | ||
|
||
Centralizing these definitions, it is hoped that libraries and applications will make use of them to make data more portable between components of a system. | ||
|
||
# Protocols | ||
|
||
## ID | ||
|
||
This protocol standardizes finding an ID for a peice of data. | ||
|
||
There are two functions in this protocol: | ||
|
||
1. `C3P0.ID.id(data)` | ||
2. `C3P0.ID.guid(data)` | ||
|
||
By default the guid will fall back to the id. | ||
|
||
This will work on all structs and maps which have fields `id` (and optionally `guid`) | ||
|
||
To customize for your struct if there are different fields, derive the `C3P0.ID` protocol. | ||
|
||
```elixir | ||
defmodule MyStruct do | ||
@derive {C3P0.ID, id_field: :local_id, guid_field: :global_id} | ||
defstruct [:local_id, :global_id] | ||
end | ||
``` | ||
|
||
## Humanize | ||
|
||
The `C3P0.Humanize` protocol displays as a string the data provided. | ||
|
||
Falls back to a simple `to_string` | ||
|
||
## Blank | ||
|
||
The `C3P0.Blank` protocol deals with blankness. | ||
|
||
Empty strings, bbinaries, lists, maps and tuples are considered `blank?` along with `nil`. | ||
|
||
The protocol defines 3 functions: | ||
|
||
* `blank?(data)` - true/false | ||
* `present?(data)` - true/false | ||
* `presence(data)` - returns the value if it is not blank. nil if it is blank | ||
|
||
|
||
|
||
## Installation | ||
|
||
If [available in Hex](https://hex.pm/docs/publish), the package can be installed | ||
by adding `c3p0` to your list of dependencies in `mix.exs`: | ||
|
||
```elixir | ||
def deps do | ||
[ | ||
{:c3p0, "~> 0.1.0"} | ||
] | ||
end | ||
``` | ||
|
||
Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc) | ||
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can | ||
be found at [https://hexdocs.pm/c3p0](https://hexdocs.pm/c3p0). | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
defmodule C3P0 do | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
defprotocol C3P0.Blank do | ||
@moduledoc """ | ||
Provides a blankness check to see if something is blank/present | ||
Empty lists, maps, strings, tuples and binaries are considered blank, as is nil. | ||
Default structs are also considered blank | ||
""" | ||
|
||
@fallback_to_any true | ||
|
||
@doc "true if the data is blank" | ||
@spec blank?(term) :: boolean | ||
def blank?(data) | ||
|
||
@doc "true if the data is not blank" | ||
@spec present?(term) :: boolean | ||
def present?(data) | ||
|
||
@doc "Returns the value if present or nil" | ||
@spec presence(term) :: term | nil | ||
def presence(data) | ||
end | ||
|
||
defimpl C3P0.Blank, for: Any do | ||
@blank ["", nil, [], %{}, <<>>, {}] | ||
|
||
def blank?(%mod{} = data), do: mod.__struct__() == data | ||
def blank?(data), do: data in @blank | ||
|
||
def present?(data), do: not C3P0.Blank.blank?(data) | ||
|
||
def presence(data) do | ||
if C3P0.Blank.blank?(data) do | ||
nil | ||
else | ||
data | ||
end | ||
end | ||
end | ||
|
||
defimpl C3P0.Blank, for: MapSet do | ||
def blank?(ms), do: MapSet.size(ms) == 0 | ||
def present?(ms), do: not C3P0.Blank.blank?(ms) | ||
def presence(ms) do | ||
if MapSet.size(ms) == 0 do | ||
nil | ||
else | ||
ms | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
defprotocol C3P0.Humanize do | ||
@moduledoc """ | ||
Converts a data object into a human readable form. | ||
By default, this will fallback to `Kernel.to_string/1` | ||
""" | ||
|
||
@fallback_to_any true | ||
|
||
@type options :: [] | ||
|
||
@spec display(term) :: String.t() | nil | ||
@spec display(term, options) :: String.t() | nil | ||
def display(data, options \\ []) | ||
end | ||
|
||
defimpl C3P0.Humanize, for: Any do | ||
def display(data, _ \\ []), do: to_string(data) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
defprotocol C3P0.ID do | ||
@moduledoc """ | ||
Formalizes fetching the ID from data. | ||
For maps the keys `id`, `"id"` are considered id fields and `guid`, `"guid"` are considered guid fields. | ||
When requesting a guid, if one cannot be found by default it will fall back to the id. | ||
## Using with your own structs | ||
By default your own structs will behave the same way as a map. | ||
However if you need to redefine which field should be considered the id/guid fields you'll need to derive the protocol. | ||
```elixir | ||
defmodule MyStruct do | ||
@derive {C3P0.ID, id_field: :token, guid_field: :arn} | ||
defstruct [:token, :arn, :name] | ||
end | ||
``` | ||
""" | ||
|
||
@fallback_to_any true | ||
|
||
@doc "Find the id of a piece of data" | ||
@spec id(term) :: binary | nil | ||
def id(data) | ||
|
||
@doc "Find a global id for a piece of data" | ||
@spec guid(term) :: binary | nil | ||
def guid(data) | ||
end | ||
|
||
defimpl C3P0.ID, for: [PID, Reference] do | ||
def id(pid), do: pid | ||
def guid(pid), do: pid | ||
end | ||
|
||
defimpl C3P0.ID, for: Any do | ||
defmacro __deriving__(module, _struct, options) do | ||
quote do | ||
defimpl C3P0.ID, for: unquote(module) do | ||
opts = unquote(options) | ||
id_field = Keyword.get(opts, :id_field) | ||
guid_field = Keyword.get(opts, :guid_field) || id_field | ||
|
||
unless id_field, do: raise "id field not provided to derive #{unquote(module)}" | ||
|
||
@id_field id_field | ||
@guid_field guid_field | ||
|
||
def id(item), do: Map.get(item, @id_field) | ||
def guid(item), do: Map.get(item, @guid_field) | ||
end | ||
end | ||
end | ||
|
||
def id(id) when is_binary(id), do: id | ||
def id(id) when is_atom(id), do: id | ||
def id(id) when is_number(id), do: id | ||
def id(%{id: id}), do: id | ||
def id(%{"id" => id}), do: id | ||
|
||
def id(%{uuid: id}), do: id | ||
def id(%{"uuid" => id}), do: id | ||
|
||
def id(%{guid: id}), do: id | ||
def id(%{"guid" => id}), do: id | ||
def id(%{}), do: nil | ||
def id(v) do | ||
raise Protocol.UndefinedError, protocol: C3P0.ID, value: v, description: "unknown value for id" | ||
end | ||
|
||
def guid(%{guid: id}), do: id | ||
def guid(%{"guid" => id}), do: id | ||
def guid(%{uuid: id}), do: id | ||
def guid(%{"uuid" => id}), do: id | ||
def guid(id), do: C3P0.ID.id(id) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
defmodule C3P0.MixProject do | ||
use Mix.Project | ||
|
||
@version "0.1.0" | ||
|
||
def project do | ||
[ | ||
app: :c3p0, | ||
version: "0.1.0", | ||
elixir: "~> 1.9", | ||
description: description(), | ||
consolidate_protocols: Mix.env() != :test, | ||
start_permanent: Mix.env() == :prod, | ||
elixirc_paths: elixirc_paths(Mix.env()), | ||
deps: deps(), | ||
docs: [ | ||
canonical: "https://hexdocs.pm/c3p0", | ||
extras: ["README.md"], | ||
source_ref: "v#{@version}", | ||
source_url: "https://github.com/hassox/c3p0", | ||
], | ||
package: [ | ||
files: [ | ||
".formatter.exs", | ||
"mix.exs", | ||
"README.md", | ||
"lib", | ||
], | ||
licenses: ["MIT"], | ||
links: %{"Github" => "https://github.com/hassox/c3p0"}, | ||
maintainers: ["Daniel Neighman"] | ||
], | ||
source_url: "https://github.com/hassox/c3p0", | ||
version: @version, | ||
|
||
] | ||
end | ||
|
||
# Run "mix help compile.app" to learn about applications. | ||
def application do | ||
[ | ||
extra_applications: [:logger] | ||
] | ||
end | ||
|
||
def elixirc_paths(:test), do: ["lib", "test/support"] | ||
def elixirc_paths(_), do: ["lib"] | ||
|
||
# Run "mix help deps" to learn about dependencies. | ||
defp deps do | ||
[ | ||
{:ex_doc, "~> 0.21", only: :dev, runtime: false}, | ||
] | ||
end | ||
|
||
defp description do | ||
""" | ||
C3P0 is a collection of protocols designed to facilitate easy data manipulation. | ||
""" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
%{ | ||
"dialyxir": {:hex, :dialyxir, "0.5.1", "b331b091720fd93e878137add264bac4f644e1ddae07a70bf7062c7862c4b952", [:mix], [], "hexpm"}, | ||
"earmark": {:hex, :earmark, "1.4.3", "364ca2e9710f6bff494117dbbd53880d84bebb692dafc3a78eb50aa3183f2bfd", [:mix], [], "hexpm"}, | ||
"ex_doc": {:hex, :ex_doc, "0.21.2", "caca5bc28ed7b3bdc0b662f8afe2bee1eedb5c3cf7b322feeeb7c6ebbde089d6", [:mix], [{:earmark, "~> 1.3.3 or ~> 1.4", [hex: :earmark, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm"}, | ||
"makeup": {:hex, :makeup, "1.0.0", "671df94cf5a594b739ce03b0d0316aa64312cee2574b6a44becb83cd90fb05dc", [:mix], [{:nimble_parsec, "~> 0.5.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm"}, | ||
"makeup_elixir": {:hex, :makeup_elixir, "0.14.0", "cf8b7c66ad1cff4c14679698d532f0b5d45a3968ffbcbfd590339cb57742f1ae", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm"}, | ||
"nimble_parsec": {:hex, :nimble_parsec, "0.5.2", "1d71150d5293d703a9c38d4329da57d3935faed2031d64bc19e77b654ef2d177", [:mix], [], "hexpm"}, | ||
} |
Oops, something went wrong.