Skip to content

Commit

Permalink
Merge branch 'release/0.1.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
ejpcmac committed Nov 13, 2018
2 parents 7674d96 + e6a8572 commit b972eb8
Show file tree
Hide file tree
Showing 10 changed files with 214 additions and 60 deletions.
2 changes: 1 addition & 1 deletion .envrc
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ use_nix() {
if [[ ! -f "${dump}" ]] || [[ "${XDG_CONFIG_DIR}/direnv/direnvrc" -nt "${dump}" ]]; then
log_status "use nix: updating cache"

old=`find ${dir} -name 'dump-*'`
old=`find "${dir}" -name 'dump-*'`
nix-shell "${drv}" --show-trace "$@" --run 'direnv dump' > "${dump}"
rm -f ${old}
fi
Expand Down
25 changes: 25 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
sudo: false
language: elixir
elixir:
- 1.6.6
- 1.7.4
otp_release:
- 19.3
- 20.3
- 21.1
env:
- PLT_DIR=$HOME/.plt
before_script:
- mkdir -p $PLT_DIR
- mix deps.compile
- MIX_ENV=test mix deps.compile
- travis_wait mix dialyzer --plt
script:
- mix compile --force --verbose --warnings-as-errors
- mix test --trace
- mix dialyzer --no-compile --no-check --halt-exit-status
- mix credo
- if [[ "$TRAVIS_ELIXIR_VERSION" == "1.7.4" ]]; then mix format --check-formatted; fi
cache:
directories:
- $PLT_DIR
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## v0.1.4

* Add the ability to generate an opaque type (#10)

## v0.1.3

* Fix a bug where fields with `default: false` where still enforced when setting
Expand Down
32 changes: 31 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# TypedStruct

[![Build Status](https://travis-ci.com/ejpcmac/typed_struct.svg?branch=develop)](https://travis-ci.com/ejpcmac/typed_struct)
[![hex.pm version](http://img.shields.io/hexpm/v/typed_struct.svg?style=flat)](https://hex.pm/packages/typed_struct)

TypedStruct is a library for defining structs with a type without writing
Expand Down Expand Up @@ -81,7 +82,7 @@ Thanks to TypedStruct, this is now possible :)
To use TypedStruct in your project, add this to your Mix dependencies:

```elixir
{:typed_struct, "~> 0.1.3"}
{:typed_struct, "~> 0.1.4"}
```

If you do not plan to compile modules using TypedStruct at runtime, you can add
Expand Down Expand Up @@ -144,6 +145,19 @@ defmodule MyStruct do
end
```

You can also generate an opaque type for the struct:

```elixir
defmodule MyOpaqueStruct do
use TypedStruct

# Generate an opaque type for the struct.
typedstruct opaque: true do
field :name, String.t()
end
end
```

### Documentation

To add a `@typedoc` to the struct type, just add the attribute above the
Expand Down Expand Up @@ -281,6 +295,22 @@ case it is enforced. Both options would generate the following type:
}
```

Passing `opaque: true` replaces `@type` with `@opaque` in the struct type
specification:

```elixir
typedstruct opaque: true do
field :name, String.t()
end

# Becomes

@opaque t() :: %__MODULE__{
name: String.t()
}
```


## [Contributing](CONTRIBUTING.md)

Before contributing to this project, please read the
Expand Down
2 changes: 1 addition & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use Mix.Config
# values for your application for 3rd-party users, it should be done in your
# "mix.exs" file.

if Mix.env() == :dev do
if Mix.env() == :test do
# Clear the console before each test run
config :mix_test_watch, clear: true
end
Expand Down
39 changes: 35 additions & 4 deletions lib/typed_struct.ex
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,17 @@ defmodule TypedStruct do
end
end
You can also generate an opaque type for the struct:
defmodule MyOpaqueStruct do
use TypedStruct
# Generate an opaque type for the struct.
typedstruct opaque: true do
field :name, String.t()
end
end
### Documentation
To add a `@typedoc` to the struct type, just add the attribute above the
Expand Down Expand Up @@ -249,6 +260,19 @@ defmodule TypedStruct do
@type t() :: %__MODULE__{
name: String.t() # Not nullable
}
Passing `opaque: true` replaces `@type` with `@opaque` in the struct type
specification:
typedstruct opaque: true do
field :name, String.t()
end
# Becomes
@opaque t() :: %__MODULE__{
name: String.t()
}
"""

@doc false
Expand All @@ -269,6 +293,7 @@ defmodule TypedStruct do
* `enforce` - if set to true, sets `enforce: true` to all fields by default.
This can be overridden by setting `enforce: false` or a default value on
individual fields.
* `opaque` - if set to true, creates an opaque type for the struct
## Examples
Expand Down Expand Up @@ -309,7 +334,7 @@ defmodule TypedStruct do
@enforce_keys @keys_to_enforce
defstruct @fields

TypedStruct.__type__(@types)
TypedStruct.__type__(@types, unquote(opts))

def __keys__, do: @fields |> Keyword.keys() |> Enum.reverse()
def __defaults__, do: Enum.reverse(@fields)
Expand Down Expand Up @@ -371,9 +396,15 @@ defmodule TypedStruct do
end

@doc false
defmacro __type__(types) do
quote bind_quoted: [types: types] do
@type t() :: %__MODULE__{unquote_splicing(types)}
defmacro __type__(types, opts) do
if Keyword.get(opts, :opaque, false) do
quote bind_quoted: [types: types] do
@opaque t() :: %__MODULE__{unquote_splicing(types)}
end
else
quote bind_quoted: [types: types] do
@type t() :: %__MODULE__{unquote_splicing(types)}
end
end
end

Expand Down
50 changes: 35 additions & 15 deletions mix.exs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
defmodule TypedStruct.MixProject do
use Mix.Project

@version "0.1.3"
@version "0.1.4"
@repo_url "https://github.com/ejpcmac/typed_struct"

def project do
[
app: :typed_struct,
version: @version,
elixir: "~> 1.6",
elixirc_paths: elixirc_paths(Mix.env()),
start_permanent: Mix.env() == :prod,
deps: deps(),

Expand All @@ -34,33 +33,28 @@ defmodule TypedStruct.MixProject do
]
end

def application do
[extra_applications: []]
end

# Specifies which paths to compile per environment.
defp elixirc_paths(:test), do: ["lib", "test/support"]
defp elixirc_paths(_), do: ["lib"]

defp deps do
[
# Development and test dependencies
{:credo, "~> 0.10.0", only: :dev, runtime: false},
{:dialyxir, ">= 0.0.0", only: :dev, runtime: false},
{:dialyxir, "~> 1.0-rc", only: :dev, runtime: false},
{:excoveralls, ">= 0.0.0", only: :test, runtime: false},
{:mix_test_watch, ">= 0.0.0", only: :dev, runtime: false},
{:mix_test_watch, ">= 0.0.0", only: :test, runtime: false},
{:ex_unit_notifier, ">= 0.0.0", only: :test, runtime: false},

# Project dependencies

# Documentation dependencies
{:ex_doc, "~> 0.19", only: :dev, runtime: false}
{:ex_doc, "~> 0.19", only: :docs, runtime: false}
]
end

# Dialyzer configuration
defp dialyzer do
[
# Use a custom PLT directory for continuous integration caching.
plt_core_path: System.get_env("PLT_DIR"),
plt_file: plt_file(),
plt_add_deps: :transitive,
flags: [
:unmatched_returns,
Expand All @@ -71,12 +65,25 @@ defmodule TypedStruct.MixProject do
]
end

defp plt_file do
case System.get_env("PLT_DIR") do
nil -> nil
plt_dir -> {:no_warn, Path.join(plt_dir, "typed_struct.plt")}
end
end

defp cli_env do
[
# Always run coveralls mix tasks in `:test` env.
# Run mix test.watch in `:test` env.
"test.watch": :test,

# Always run Coveralls Mix tasks in `:test` env.
coveralls: :test,
"coveralls.detail": :test,
"coveralls.html": :test
"coveralls.html": :test,

# Use a custom env for docs.
docs: :docs
]
end

Expand All @@ -86,4 +93,17 @@ defmodule TypedStruct.MixProject do
links: %{"GitHub" => @repo_url}
]
end

# Helper to add a development revision to the version. Do NOT make a call to
# Git this way in a production release!!
def dev do
with {rev, 0} <-
System.cmd("git", ["rev-parse", "--short", "HEAD"],
stderr_to_stdout: true
) do
"-dev+" <> String.trim(rev)
else
_ -> "-dev"
end
end
end
29 changes: 15 additions & 14 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
%{
"bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm"},
"certifi": {:hex, :certifi, "2.3.1", "d0f424232390bf47d82da8478022301c561cf6445b5b5fb6a84d49a9e76d2639", [:rebar3], [{:parse_trans, "3.2.0", [hex: :parse_trans, repo: "hexpm", optional: false]}], "hexpm"},
"credo": {:hex, :credo, "0.10.0", "66234a95effaf9067edb19fc5d0cd5c6b461ad841baac42467afed96c78e5e9e", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm"},
"dialyxir": {:hex, :dialyxir, "0.5.1", "b331b091720fd93e878137add264bac4f644e1ddae07a70bf7062c7862c4b952", [:mix], [], "hexpm"},
"certifi": {:hex, :certifi, "2.4.2", "75424ff0f3baaccfd34b1214184b6ef616d89e420b258bb0a5ea7d7bc628f7f0", [:rebar3], [{:parse_trans, "~>3.3", [hex: :parse_trans, repo: "hexpm", optional: false]}], "hexpm"},
"credo": {:hex, :credo, "0.10.2", "03ad3a1eff79a16664ed42fc2975b5e5d0ce243d69318060c626c34720a49512", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm"},
"dialyxir": {:hex, :dialyxir, "1.0.0-rc.4", "71b42f5ee1b7628f3e3a6565f4617dfb02d127a0499ab3e72750455e986df001", [:mix], [{:erlex, "~> 0.1", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm"},
"earmark": {:hex, :earmark, "1.2.6", "b6da42b3831458d3ecc57314dff3051b080b9b2be88c2e5aa41cd642a5b044ed", [:mix], [], "hexpm"},
"erlex": {:hex, :erlex, "0.1.6", "c01c889363168d3fdd23f4211647d8a34c0f9a21ec726762312e08e083f3d47e", [:mix], [], "hexpm"},
"ex_doc": {:hex, :ex_doc, "0.19.1", "519bb9c19526ca51d326c060cb1778d4a9056b190086a8c6c115828eaccea6cf", [:mix], [{:earmark, "~> 1.1", [hex: :earmark, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.7", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm"},
"ex_unit_notifier": {:hex, :ex_unit_notifier, "0.1.4", "36a2dcab829f506e01bf17816590680dd1474407926d43e64c1263e627c364b8", [:mix], [], "hexpm"},
"excoveralls": {:hex, :excoveralls, "0.10.0", "a4508bdd408829f38e7b2519f234b7fd5c83846099cda348efcb5291b081200c", [:mix], [{:hackney, "~> 1.13", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm"},
"excoveralls": {:hex, :excoveralls, "0.10.2", "fb4abd5b8a1b9d52d35e1162e7e2ea8bfb84b47ae07c38d39aa8ce64be0b0794", [:mix], [{:hackney, "~> 1.13", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm"},
"file_system": {:hex, :file_system, "0.2.6", "fd4dc3af89b9ab1dc8ccbcc214a0e60c41f34be251d9307920748a14bf41f1d3", [:mix], [], "hexpm"},
"hackney": {:hex, :hackney, "1.13.0", "24edc8cd2b28e1c652593833862435c80661834f6c9344e84b6a2255e7aeef03", [:rebar3], [{:certifi, "2.3.1", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "5.1.2", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.1", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm"},
"idna": {:hex, :idna, "5.1.2", "e21cb58a09f0228a9e0b95eaa1217f1bcfc31a1aaa6e1fdf2f53a33f7dbd9494", [:rebar3], [{:unicode_util_compat, "0.3.1", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm"},
"jason": {:hex, :jason, "1.1.1", "d3ccb840dfb06f2f90a6d335b536dd074db748b3e7f5b11ab61d239506585eb2", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm"},
"makeup": {:hex, :makeup, "0.5.1", "966c5c2296da272d42f1de178c1d135e432662eca795d6dc12e5e8787514edf7", [:mix], [{:nimble_parsec, "~> 0.2.2", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm"},
"makeup_elixir": {:hex, :makeup_elixir, "0.8.0", "1204a2f5b4f181775a0e456154830524cf2207cf4f9112215c05e0b76e4eca8b", [:mix], [{:makeup, "~> 0.5.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 0.2.2", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm"},
"hackney": {:hex, :hackney, "1.14.3", "b5f6f5dcc4f1fba340762738759209e21914516df6be440d85772542d4a5e412", [:rebar3], [{:certifi, "2.4.2", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "6.0.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.4", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm"},
"idna": {:hex, :idna, "6.0.0", "689c46cbcdf3524c44d5f3dde8001f364cd7608a99556d8fbd8239a5798d4c10", [:rebar3], [{:unicode_util_compat, "0.4.1", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm"},
"jason": {:hex, :jason, "1.1.2", "b03dedea67a99223a2eaf9f1264ce37154564de899fd3d8b9a21b1a6fd64afe7", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm"},
"makeup": {:hex, :makeup, "0.5.5", "9e08dfc45280c5684d771ad58159f718a7b5788596099bdfb0284597d368a882", [:mix], [{:nimble_parsec, "~> 0.4", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm"},
"makeup_elixir": {:hex, :makeup_elixir, "0.10.0", "0f09c2ddf352887a956d84f8f7e702111122ca32fbbc84c2f0569b8b65cbf7fa", [:mix], [{:makeup, "~> 0.5.5", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm"},
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm"},
"mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [:rebar3], [], "hexpm"},
"mix_test_watch": {:hex, :mix_test_watch, "0.8.0", "acf97da2abc66532e7dc1aa66a5d6c9fc4442d7992d5d7eb4faeaeb964c2580e", [:mix], [{:file_system, "~> 0.2.1 or ~> 0.3", [hex: :file_system, repo: "hexpm", optional: false]}], "hexpm"},
"nimble_parsec": {:hex, :nimble_parsec, "0.2.2", "d526b23bdceb04c7ad15b33c57c4526bf5f50aaa70c7c141b4b4624555c68259", [:mix], [], "hexpm"},
"parse_trans": {:hex, :parse_trans, "3.2.0", "2adfa4daf80c14dc36f522cf190eb5c4ee3e28008fc6394397c16f62a26258c2", [:rebar3], [], "hexpm"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.1", "28a4d65b7f59893bc2c7de786dec1e1555bd742d336043fe644ae956c3497fbe", [:make, :rebar], [], "hexpm"},
"unicode_util_compat": {:hex, :unicode_util_compat, "0.3.1", "a1f612a7b512638634a603c8f401892afbf99b8ce93a45041f8aaca99cadb85e", [:rebar3], [], "hexpm"},
"mix_test_watch": {:hex, :mix_test_watch, "0.9.0", "c72132a6071261893518fa08e121e911c9358713f62794a90c95db59042af375", [:mix], [{:file_system, "~> 0.2.1 or ~> 0.3", [hex: :file_system, repo: "hexpm", optional: false]}], "hexpm"},
"nimble_parsec": {:hex, :nimble_parsec, "0.4.0", "ee261bb53214943679422be70f1658fff573c5d0b0a1ecd0f18738944f818efe", [:mix], [], "hexpm"},
"parse_trans": {:hex, :parse_trans, "3.3.0", "09765507a3c7590a784615cfd421d101aec25098d50b89d7aa1d66646bc571c1", [:rebar3], [], "hexpm"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.4", "f0eafff810d2041e93f915ef59899c923f4568f4585904d010387ed74988e77b", [:make, :mix, :rebar3], [], "hexpm"},
"unicode_util_compat": {:hex, :unicode_util_compat, "0.4.1", "d869e4c68901dd9531385bb0c8c40444ebf624e60b6962d95952775cac5e90cd", [:rebar3], [], "hexpm"},
}
11 changes: 4 additions & 7 deletions shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,14 @@ with pkgs;
let
inherit (lib) optional optionals;

erlang = beam.interpreters.erlangR21.override {
# Temporary fix to enable use on OS X El Capitan.
enableKernelPoll = if stdenv.isDarwin then false else true;
};

elixir = (beam.packages.erlangR21.override { inherit erlang; }).elixir_1_7;
elixir = beam.packages.erlangR21.elixir_1_7;
gitflow = gitAndTools.gitflow;
in

mkShell {
buildInputs = [ elixir git ]
buildInputs = [ elixir git gitflow ]
++ optional stdenv.isLinux libnotify # For ExUnit Notifier on Linux.
++ optional stdenv.isLinux inotify-tools # For file_system on Linux.
++ optional stdenv.isDarwin terminal-notifier # For ExUnit Notifier on macOS.
++ optionals stdenv.isDarwin (with darwin.apple_sdk.frameworks; [
# For file_system on macOS.
Expand Down
Loading

0 comments on commit b972eb8

Please sign in to comment.