Skip to content

Commit c11d6bb

Browse files
committed
Merge branch 'release/0.1.2'
2 parents 6113896 + 08679cb commit c11d6bb

File tree

10 files changed

+310
-51
lines changed

10 files changed

+310
-51
lines changed

.credo.exs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
# You can give explicit globs or simply directories.
2222
# In the latter case `**/*.{ex,exs}` will be used.
2323
#
24-
included: ["lib/", "test/"],
25-
excluded: [~r"/_build/", ~r"/deps/"]
24+
included: ["lib/", "src/", "test/", "web/", "apps/"],
25+
excluded: [~r"/_build/", ~r"/deps/", ~r"/node_modules/"]
2626
},
2727
#
2828
# If you create your own checks, you must specify the source files for
@@ -72,7 +72,6 @@
7272
# set the `excluded_macros` parameter to `[:schema, :setup, :test]`.
7373
#
7474
{Credo.Check.Design.DuplicatedCode, excluded_macros: []},
75-
7675
# You can also customize the exit_status of each check.
7776
# If you don't want TODO comments to cause `mix credo` to fail, just
7877
# set this value to 0 (zero).
@@ -83,6 +82,7 @@
8382
#
8483
## Readability Checks
8584
#
85+
{Credo.Check.Readability.AliasOrder},
8686
{Credo.Check.Readability.FunctionNames},
8787
{Credo.Check.Readability.LargeNumbers},
8888
{Credo.Check.Readability.MaxLineLength, priority: :low, max_length: 80},
@@ -109,6 +109,7 @@
109109
{Credo.Check.Refactor.CyclomaticComplexity},
110110
{Credo.Check.Refactor.FunctionArity},
111111
{Credo.Check.Refactor.LongQuoteBlocks},
112+
{Credo.Check.Refactor.MapInto},
112113
{Credo.Check.Refactor.MatchInCondition},
113114
{Credo.Check.Refactor.NegatedConditionsInUnless},
114115
{Credo.Check.Refactor.NegatedConditionsWithElse},

.envrc

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
####################################
2+
# Environment setup for Nix shells #
3+
####################################
4+
5+
# From https://github.com/direnv/direnv/wiki/Nix#persistent-cached-shell
6+
#
7+
# Usage: use_nix [...]
8+
#
9+
# Load environment variables from `nix-shell`.
10+
# If you have a `default.nix` or `shell.nix` one of these will be used and
11+
# the derived environment will be stored at ./.direnv/env-<hash>
12+
# and symlink to it will be created at ./.direnv/default.
13+
# Dependencies are added to the GC roots, such that the environment remains persistent.
14+
#
15+
# Packages can also be specified directly via e.g `use nix -p ocaml`,
16+
# however those will not be added to the GC roots.
17+
#
18+
# The resulting environment is cached for better performance.
19+
#
20+
# To trigger switch to a different environment:
21+
# `rm -f .direnv/default`
22+
#
23+
# To derive a new environment:
24+
# `rm -rf .direnv/env-$(md5sum {shell,default}.nix 2> /dev/null | cut -c -32)`
25+
#
26+
# To remove cache:
27+
# `rm -f .direnv/dump-*`
28+
#
29+
# To remove all environments:
30+
# `rm -rf .direnv/env-*`
31+
#
32+
# To remove only old environments:
33+
# `find .direnv -name 'env-*' -and -not -name `readlink .direnv/default` -exec rm -rf {} +`
34+
#
35+
use_nix() {
36+
set -e
37+
38+
local shell="shell.nix"
39+
if [[ ! -f "${shell}" ]]; then
40+
shell="default.nix"
41+
fi
42+
43+
if [[ ! -f "${shell}" ]]; then
44+
fail "use nix: shell.nix or default.nix not found in the folder"
45+
fi
46+
47+
local dir="${PWD}"/.direnv
48+
local default="${dir}/default"
49+
if [[ ! -L "${default}" ]] || [[ ! -d `readlink "${default}"` ]]; then
50+
local wd="${dir}/env-`md5sum "${shell}" | cut -c -32`" # TODO: Hash also the nixpkgs version?
51+
mkdir -p "${wd}"
52+
53+
local drv="${wd}/env.drv"
54+
if [[ ! -f "${drv}" ]]; then
55+
log_status "use nix: deriving new environment"
56+
IN_NIX_SHELL=1 nix-instantiate --add-root "${drv}" --indirect "${shell}" > /dev/null
57+
nix-store -r `nix-store --query --references "${drv}"` --add-root "${wd}/dep" --indirect > /dev/null
58+
fi
59+
60+
rm -f "${default}"
61+
ln -s `basename "${wd}"` "${default}"
62+
fi
63+
64+
local drv=`readlink -f "${default}/env.drv"`
65+
local dump="${dir}/dump-`md5sum ".envrc" | cut -c -32`-`md5sum ${drv} | cut -c -32`"
66+
67+
if [[ ! -f "${dump}" ]] || [[ "${XDG_CONFIG_DIR}/direnv/direnvrc" -nt "${dump}" ]]; then
68+
log_status "use nix: updating cache"
69+
70+
old=`find ${dir} -name 'dump-*'`
71+
nix-shell "${drv}" --show-trace "$@" --run 'direnv dump' > "${dump}"
72+
rm -f ${old}
73+
fi
74+
75+
direnv_load cat "${dump}"
76+
77+
watch_file "${default}"
78+
watch_file shell.nix
79+
if [[ ${shell} == "default.nix" ]]; then
80+
watch_file default.nix
81+
fi
82+
}
83+
84+
use nix

.gitignore

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,37 @@
1-
# Application artifacts (Elixir)
1+
##
2+
## Application artifacts
3+
##
4+
5+
# direnv cache for Nix shells
6+
/.direnv/
7+
8+
# Elixir build directory
29
/_build/
10+
11+
# Elixir dependencies
312
/deps/
413
/.fetch
14+
15+
# Elixir binary files
516
*.beam
617
*.ez
7-
typed_struct-*.tar
18+
/typed_struct-*.tar
19+
/typed_struct
820

921
# Test coverage and documentation
1022
/cover/
1123
/doc/
1224

13-
# Editor artifacts
25+
##
26+
## Editor artifacts
27+
##
28+
1429
/.elixir_ls/
1530
/.history/
1631

17-
# Crash dumps
32+
##
33+
## Crash dumps
34+
##
35+
36+
# Erang VM
1837
erl_crash.dump

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## v0.1.2
4+
5+
* Add the ability to enforce keys by default (#6)
6+
* Clarify the documentation about `runtime: false`
7+
38
## v0.1.1
49

510
* Do not make the type nullable when there is a default value

README.md

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,22 @@ Thanks to TypedStruct, this is now possible :)
7878

7979
### Setup
8080

81-
To use TypedStruct in your project, add this to you Mix dependencies:
81+
To use TypedStruct in your project, add this to your Mix dependencies:
8282

8383
```elixir
84-
{:typed_struct, "~> 0.1.1", runtime: false}
84+
{:typed_struct, "~> 0.1.2"}
8585
```
8686

87+
If you do not plan to compile modules using TypedStruct at runtime, you can add
88+
`runtime: false` to the dependency tuple as TypedStruct is only used during
89+
compilation.
90+
8791
If you want to avoid `mix format` putting parentheses on field definitions,
88-
you can write in your `.formatter.exs`:
92+
you can add to your `.formatter.exs`:
8993

9094
```elixir
9195
[
96+
...,
9297
import_deps: [:typed_struct]
9398
]
9499
```
@@ -100,25 +105,45 @@ To define a typed struct, use `TypedStruct`, then define your struct within a
100105

101106
```elixir
102107
defmodule MyStruct do
103-
# Use TypedStruct to import the typedstruct macro
108+
# Use TypedStruct to import the typedstruct macro.
104109
use TypedStruct
105110

106-
# Define your struct
111+
# Define your struct.
107112
typedstruct do
108-
# Define each field with the field macro
113+
# Define each field with the field macro.
109114
field :a_string, String.t()
110115

111-
# You can set a default value
116+
# You can set a default value.
112117
field :string_with_default, String.t(), default: "default"
113118

114-
# You can enforce a field
119+
# You can enforce a field.
115120
field :enforced_field, integer(), enforce: true
116121
end
117122
end
118123
```
119124

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

127+
If you want to enforce all the keys by default, you can do:
128+
129+
```elixir
130+
defmodule MyStruct do
131+
use TypedStruct
132+
133+
# Enforce keys by default.
134+
typedstruct enforce: true do
135+
# This key is enforced.
136+
field :enforced_by_default, term()
137+
138+
# You can override the default behaviour.
139+
field :not_enforced, term(), enforce: false
140+
141+
# A key with a default value is not enforced.
142+
field :not_enforced_either, integer(), default: 1
143+
end
144+
end
145+
```
146+
122147
### Documentation
123148

124149
To add a `@typedoc` to the struct type, just add the attribute above the

0 commit comments

Comments
 (0)