Skip to content

Commit

Permalink
Merge branch 'release/0.1.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
ejpcmac committed Sep 5, 2018
2 parents 6113896 + 08679cb commit c11d6bb
Show file tree
Hide file tree
Showing 10 changed files with 310 additions and 51 deletions.
7 changes: 4 additions & 3 deletions .credo.exs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
# You can give explicit globs or simply directories.
# In the latter case `**/*.{ex,exs}` will be used.
#
included: ["lib/", "test/"],
excluded: [~r"/_build/", ~r"/deps/"]
included: ["lib/", "src/", "test/", "web/", "apps/"],
excluded: [~r"/_build/", ~r"/deps/", ~r"/node_modules/"]
},
#
# If you create your own checks, you must specify the source files for
Expand Down Expand Up @@ -72,7 +72,6 @@
# set the `excluded_macros` parameter to `[:schema, :setup, :test]`.
#
{Credo.Check.Design.DuplicatedCode, excluded_macros: []},

# You can also customize the exit_status of each check.
# If you don't want TODO comments to cause `mix credo` to fail, just
# set this value to 0 (zero).
Expand All @@ -83,6 +82,7 @@
#
## Readability Checks
#
{Credo.Check.Readability.AliasOrder},
{Credo.Check.Readability.FunctionNames},
{Credo.Check.Readability.LargeNumbers},
{Credo.Check.Readability.MaxLineLength, priority: :low, max_length: 80},
Expand All @@ -109,6 +109,7 @@
{Credo.Check.Refactor.CyclomaticComplexity},
{Credo.Check.Refactor.FunctionArity},
{Credo.Check.Refactor.LongQuoteBlocks},
{Credo.Check.Refactor.MapInto},
{Credo.Check.Refactor.MatchInCondition},
{Credo.Check.Refactor.NegatedConditionsInUnless},
{Credo.Check.Refactor.NegatedConditionsWithElse},
Expand Down
84 changes: 84 additions & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
####################################
# Environment setup for Nix shells #
####################################

# From https://github.com/direnv/direnv/wiki/Nix#persistent-cached-shell
#
# Usage: use_nix [...]
#
# Load environment variables from `nix-shell`.
# If you have a `default.nix` or `shell.nix` one of these will be used and
# the derived environment will be stored at ./.direnv/env-<hash>
# and symlink to it will be created at ./.direnv/default.
# Dependencies are added to the GC roots, such that the environment remains persistent.
#
# Packages can also be specified directly via e.g `use nix -p ocaml`,
# however those will not be added to the GC roots.
#
# The resulting environment is cached for better performance.
#
# To trigger switch to a different environment:
# `rm -f .direnv/default`
#
# To derive a new environment:
# `rm -rf .direnv/env-$(md5sum {shell,default}.nix 2> /dev/null | cut -c -32)`
#
# To remove cache:
# `rm -f .direnv/dump-*`
#
# To remove all environments:
# `rm -rf .direnv/env-*`
#
# To remove only old environments:
# `find .direnv -name 'env-*' -and -not -name `readlink .direnv/default` -exec rm -rf {} +`
#
use_nix() {
set -e

local shell="shell.nix"
if [[ ! -f "${shell}" ]]; then
shell="default.nix"
fi

if [[ ! -f "${shell}" ]]; then
fail "use nix: shell.nix or default.nix not found in the folder"
fi

local dir="${PWD}"/.direnv
local default="${dir}/default"
if [[ ! -L "${default}" ]] || [[ ! -d `readlink "${default}"` ]]; then
local wd="${dir}/env-`md5sum "${shell}" | cut -c -32`" # TODO: Hash also the nixpkgs version?
mkdir -p "${wd}"

local drv="${wd}/env.drv"
if [[ ! -f "${drv}" ]]; then
log_status "use nix: deriving new environment"
IN_NIX_SHELL=1 nix-instantiate --add-root "${drv}" --indirect "${shell}" > /dev/null
nix-store -r `nix-store --query --references "${drv}"` --add-root "${wd}/dep" --indirect > /dev/null
fi

rm -f "${default}"
ln -s `basename "${wd}"` "${default}"
fi

local drv=`readlink -f "${default}/env.drv"`
local dump="${dir}/dump-`md5sum ".envrc" | cut -c -32`-`md5sum ${drv} | cut -c -32`"

if [[ ! -f "${dump}" ]] || [[ "${XDG_CONFIG_DIR}/direnv/direnvrc" -nt "${dump}" ]]; then
log_status "use nix: updating cache"

old=`find ${dir} -name 'dump-*'`
nix-shell "${drv}" --show-trace "$@" --run 'direnv dump' > "${dump}"
rm -f ${old}
fi

direnv_load cat "${dump}"

watch_file "${default}"
watch_file shell.nix
if [[ ${shell} == "default.nix" ]]; then
watch_file default.nix
fi
}

use nix
27 changes: 23 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,18 +1,37 @@
# Application artifacts (Elixir)
##
## Application artifacts
##

# direnv cache for Nix shells
/.direnv/

# Elixir build directory
/_build/

# Elixir dependencies
/deps/
/.fetch

# Elixir binary files
*.beam
*.ez
typed_struct-*.tar
/typed_struct-*.tar
/typed_struct

# Test coverage and documentation
/cover/
/doc/

# Editor artifacts
##
## Editor artifacts
##

/.elixir_ls/
/.history/

# Crash dumps
##
## Crash dumps
##

# Erang VM
erl_crash.dump
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## v0.1.2

* Add the ability to enforce keys by default (#6)
* Clarify the documentation about `runtime: false`

## v0.1.1

* Do not make the type nullable when there is a default value
Expand Down
41 changes: 33 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,22 @@ Thanks to TypedStruct, this is now possible :)

### Setup

To use TypedStruct in your project, add this to you Mix dependencies:
To use TypedStruct in your project, add this to your Mix dependencies:

```elixir
{:typed_struct, "~> 0.1.1", runtime: false}
{:typed_struct, "~> 0.1.2"}
```

If you do not plan to compile modules using TypedStruct at runtime, you can add
`runtime: false` to the dependency tuple as TypedStruct is only used during
compilation.

If you want to avoid `mix format` putting parentheses on field definitions,
you can write in your `.formatter.exs`:
you can add to your `.formatter.exs`:

```elixir
[
...,
import_deps: [:typed_struct]
]
```
Expand All @@ -100,25 +105,45 @@ To define a typed struct, use `TypedStruct`, then define your struct within a

```elixir
defmodule MyStruct do
# Use TypedStruct to import the typedstruct macro
# Use TypedStruct to import the typedstruct macro.
use TypedStruct

# Define your struct
# Define your struct.
typedstruct do
# Define each field with the field macro
# Define each field with the field macro.
field :a_string, String.t()

# You can set a default value
# You can set a default value.
field :string_with_default, String.t(), default: "default"

# You can enforce a field
# You can enforce a field.
field :enforced_field, integer(), enforce: true
end
end
```

Each field is defined through the `field/2` macro.

If you want to enforce all the keys by default, you can do:

```elixir
defmodule MyStruct do
use TypedStruct

# Enforce keys by default.
typedstruct enforce: true do
# This key is enforced.
field :enforced_by_default, term()

# You can override the default behaviour.
field :not_enforced, term(), enforce: false

# A key with a default value is not enforced.
field :not_enforced_either, integer(), default: 1
end
end
```

### Documentation

To add a `@typedoc` to the struct type, just add the attribute above the
Expand Down
Loading

0 comments on commit c11d6bb

Please sign in to comment.