Skip to content

Commit 958549f

Browse files
authored
Merge pull request #10 from corka149/8-allow-atoms-as-key-via-option
8 allow atoms as key via option
2 parents 8fecc92 + e79c44d commit 958549f

18 files changed

Lines changed: 611 additions & 247 deletions

.credo.exs

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
# This file contains the configuration for Credo and you are probably reading
2+
# this after creating it with `mix credo.gen.config`.
3+
#
4+
# If you find anything wrong or unclear in this file, please report an
5+
# issue on GitHub: https://github.com/rrrene/credo/issues
6+
#
7+
%{
8+
#
9+
# You can have as many configs as you like in the `configs:` field.
10+
configs: [
11+
%{
12+
#
13+
# Run any config using `mix credo -C <name>`. If no config name is given
14+
# "default" is used.
15+
#
16+
name: "default",
17+
#
18+
# These are the files included in the analysis:
19+
files: %{
20+
#
21+
# You can give explicit globs or simply directories.
22+
# In the latter case `**/*.{ex,exs}` will be used.
23+
#
24+
included: [
25+
"lib/",
26+
"src/",
27+
"test/",
28+
"web/",
29+
"apps/*/lib/",
30+
"apps/*/src/",
31+
"apps/*/test/",
32+
"apps/*/web/"
33+
],
34+
excluded: [~r"/_build/", ~r"/deps/", ~r"/node_modules/"]
35+
},
36+
#
37+
# Load and configure plugins here:
38+
#
39+
plugins: [],
40+
#
41+
# If you create your own checks, you must specify the source files for
42+
# them here, so they can be loaded by Credo before running the analysis.
43+
#
44+
requires: [],
45+
#
46+
# If you want to enforce a style guide and need a more traditional linting
47+
# experience, you can change `strict` to `true` below:
48+
#
49+
strict: false,
50+
#
51+
# To modify the timeout for parsing files, change this value:
52+
#
53+
parse_timeout: 5000,
54+
#
55+
# If you want to use uncolored output by default, you can change `color`
56+
# to `false` below:
57+
#
58+
color: true,
59+
#
60+
# You can customize the parameters of any check by adding a second element
61+
# to the tuple.
62+
#
63+
# To disable a check put `false` as second element:
64+
#
65+
# {Credo.Check.Design.DuplicatedCode, false}
66+
#
67+
checks: [
68+
#
69+
## Consistency Checks
70+
#
71+
{Credo.Check.Consistency.ExceptionNames, []},
72+
{Credo.Check.Consistency.LineEndings, []},
73+
{Credo.Check.Consistency.ParameterPatternMatching, []},
74+
{Credo.Check.Consistency.SpaceAroundOperators, []},
75+
{Credo.Check.Consistency.SpaceInParentheses, []},
76+
{Credo.Check.Consistency.TabsOrSpaces, []},
77+
78+
#
79+
## Design Checks
80+
#
81+
# You can customize the priority of any check
82+
# Priority values are: `low, normal, high, higher`
83+
#
84+
{Credo.Check.Design.AliasUsage,
85+
[priority: :low, if_nested_deeper_than: 2, if_called_more_often_than: 0]},
86+
# You can also customize the exit_status of each check.
87+
# If you don't want TODO comments to cause `mix credo` to fail, just
88+
# set this value to 0 (zero).
89+
#
90+
{Credo.Check.Design.TagTODO, [exit_status: 2]},
91+
{Credo.Check.Design.TagFIXME, []},
92+
93+
#
94+
## Readability Checks
95+
#
96+
{Credo.Check.Readability.AliasOrder, []},
97+
{Credo.Check.Readability.FunctionNames, []},
98+
{Credo.Check.Readability.LargeNumbers, []},
99+
{Credo.Check.Readability.MaxLineLength, [priority: :low, max_length: 120]},
100+
{Credo.Check.Readability.ModuleAttributeNames, []},
101+
{Credo.Check.Readability.ModuleDoc, []},
102+
{Credo.Check.Readability.ModuleNames, []},
103+
{Credo.Check.Readability.ParenthesesInCondition, []},
104+
{Credo.Check.Readability.ParenthesesOnZeroArityDefs, []},
105+
{Credo.Check.Readability.PredicateFunctionNames, []},
106+
{Credo.Check.Readability.PreferImplicitTry, []},
107+
{Credo.Check.Readability.RedundantBlankLines, []},
108+
{Credo.Check.Readability.Semicolons, []},
109+
{Credo.Check.Readability.SpaceAfterCommas, []},
110+
{Credo.Check.Readability.StringSigils, []},
111+
{Credo.Check.Readability.TrailingBlankLine, []},
112+
{Credo.Check.Readability.TrailingWhiteSpace, []},
113+
{Credo.Check.Readability.UnnecessaryAliasExpansion, []},
114+
{Credo.Check.Readability.VariableNames, []},
115+
116+
#
117+
## Refactoring Opportunities
118+
#
119+
{Credo.Check.Refactor.CondStatements, []},
120+
{Credo.Check.Refactor.CyclomaticComplexity, []},
121+
{Credo.Check.Refactor.FunctionArity, []},
122+
{Credo.Check.Refactor.LongQuoteBlocks, []},
123+
# {Credo.Check.Refactor.MapInto, []},
124+
{Credo.Check.Refactor.MatchInCondition, []},
125+
{Credo.Check.Refactor.NegatedConditionsInUnless, []},
126+
{Credo.Check.Refactor.NegatedConditionsWithElse, []},
127+
{Credo.Check.Refactor.Nesting, []},
128+
{Credo.Check.Refactor.UnlessWithElse, []},
129+
{Credo.Check.Refactor.WithClauses, []},
130+
131+
#
132+
## Warnings
133+
#
134+
{Credo.Check.Warning.ApplicationConfigInModuleAttribute, []},
135+
{Credo.Check.Warning.BoolOperationOnSameValues, []},
136+
{Credo.Check.Warning.ExpensiveEmptyEnumCheck, []},
137+
{Credo.Check.Warning.IExPry, []},
138+
{Credo.Check.Warning.IoInspect, []},
139+
# {Credo.Check.Warning.LazyLogging, []},
140+
{Credo.Check.Warning.MixEnv, false},
141+
{Credo.Check.Warning.OperationOnSameValues, []},
142+
{Credo.Check.Warning.OperationWithConstantResult, []},
143+
{Credo.Check.Warning.RaiseInsideRescue, []},
144+
{Credo.Check.Warning.UnusedEnumOperation, []},
145+
{Credo.Check.Warning.UnusedFileOperation, []},
146+
{Credo.Check.Warning.UnusedKeywordOperation, []},
147+
{Credo.Check.Warning.UnusedListOperation, []},
148+
{Credo.Check.Warning.UnusedPathOperation, []},
149+
{Credo.Check.Warning.UnusedRegexOperation, []},
150+
{Credo.Check.Warning.UnusedStringOperation, []},
151+
{Credo.Check.Warning.UnusedTupleOperation, []},
152+
{Credo.Check.Warning.UnsafeExec, []},
153+
154+
#
155+
# Checks scheduled for next check update (opt-in for now, just replace `false` with `[]`)
156+
157+
#
158+
# Controversial and experimental checks (opt-in, just replace `false` with `[]`)
159+
#
160+
{Credo.Check.Consistency.MultiAliasImportRequireUse, false},
161+
{Credo.Check.Consistency.UnusedVariableNames, false},
162+
{Credo.Check.Design.DuplicatedCode, false},
163+
{Credo.Check.Readability.AliasAs, false},
164+
{Credo.Check.Readability.BlockPipe, false},
165+
{Credo.Check.Readability.ImplTrue, false},
166+
{Credo.Check.Readability.MultiAlias, false},
167+
{Credo.Check.Readability.SeparateAliasRequire, false},
168+
{Credo.Check.Readability.SinglePipe, false},
169+
{Credo.Check.Readability.Specs, false},
170+
{Credo.Check.Readability.StrictModuleLayout, false},
171+
{Credo.Check.Readability.WithCustomTaggedTuple, false},
172+
{Credo.Check.Refactor.ABCSize, false},
173+
{Credo.Check.Refactor.AppendSingleItem, false},
174+
{Credo.Check.Refactor.DoubleBooleanNegation, false},
175+
{Credo.Check.Refactor.ModuleDependencies, false},
176+
{Credo.Check.Refactor.NegatedIsNil, false},
177+
{Credo.Check.Refactor.PipeChainStart, false},
178+
{Credo.Check.Refactor.VariableRebinding, false},
179+
{Credo.Check.Warning.LeakyEnvironment, false},
180+
{Credo.Check.Warning.MapGetUnsafePass, false},
181+
{Credo.Check.Warning.UnsafeToAtom, false}
182+
183+
#
184+
# Custom checks can be created using `mix credo.gen.check`.
185+
#
186+
]
187+
}
188+
]
189+
}

CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# 0.13.0
2+
- Allow usage of atoms for keys via `keys` option
3+
14
# 0.12.1
25
- Generate diffs with correct order (thanks https://github.com/smartepsh)
36

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44
check:
5-
mix format
5+
mix format --check-formatted
66
mix test
77
mix dialyzer
88
mix credo --strict

lib/jsonpatch.ex

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ defmodule Jsonpatch do
3232
when any path is invalid or any other error occured. When a list is provided, the operations are
3333
applied in the order as they appear in the list.
3434
35+
Atoms are never garbage collected. Therefore, `Jsonpatch` works by default only with maps
36+
which used binary strings as key. This behaviour can be controlled via the `:keys` option.
37+
38+
## Options
39+
* `:keys` - controls how parts of paths are decoded. Possible values:
40+
* `:strings` (default) - decodes parts of paths as binary strings,
41+
* `:atoms` - parts of paths are converted to atoms using `String.to_atom/1`,
42+
* `:atoms!` - parts of paths are converted to atoms using `String.to_existing_atom/1`
43+
3544
## Examples
3645
iex> patch = [
3746
...> %Jsonpatch.Operation.Add{path: "/age", value: 33},
@@ -56,16 +65,16 @@ defmodule Jsonpatch do
5665
iex> Jsonpatch.apply_patch(patch, target)
5766
{:error, :test_failed, "Expected value 'Alice' at '/name'"}
5867
"""
59-
@spec apply_patch(Jsonpatch.t() | list(Jsonpatch.t()), map()) ::
68+
@spec apply_patch(Jsonpatch.t() | list(Jsonpatch.t()), map(), keyword()) ::
6069
{:ok, map()} | Jsonpatch.error()
61-
def apply_patch(json_patch, target)
70+
def apply_patch(json_patch, target, opts \\ [])
6271

63-
def apply_patch(json_patch, %{} = target) when is_list(json_patch) do
72+
def apply_patch(json_patch, %{} = target, opts) when is_list(json_patch) do
6473
# https://datatracker.ietf.org/doc/html/rfc6902#section-3
6574
# > Operations are applied sequentially in the order they appear in the array.
6675
result =
6776
Enum.reduce_while(json_patch, target, fn patch, acc ->
68-
case Jsonpatch.Operation.apply_op(patch, acc) do
77+
case Jsonpatch.Operation.apply_op(patch, acc, opts) do
6978
{:error, _, _} = error -> {:halt, error}
7079
result -> {:cont, result}
7180
end
@@ -77,8 +86,8 @@ defmodule Jsonpatch do
7786
end
7887
end
7988

80-
def apply_patch(json_patch, %{} = target) do
81-
apply_patch([json_patch], target)
89+
def apply_patch(json_patch, %{} = target, opts) do
90+
apply_patch([json_patch], target, opts)
8291
end
8392

8493
@doc """
@@ -88,11 +97,11 @@ defmodule Jsonpatch do
8897
8998
(See Jsonpatch.apply_patch/2 for more details)
9099
"""
91-
@spec apply_patch!(Jsonpatch.t() | list(Jsonpatch.t()), map()) :: map()
92-
def apply_patch!(json_patch, target)
100+
@spec apply_patch!(Jsonpatch.t() | list(Jsonpatch.t()), map(), keyword()) :: map()
101+
def apply_patch!(json_patch, target, opts \\ [])
93102

94-
def apply_patch!(json_patch, target) do
95-
case apply_patch(json_patch, target) do
103+
def apply_patch!(json_patch, target, opts) do
104+
case apply_patch(json_patch, target, opts) do
96105
{:ok, patched} -> patched
97106
{:error, _, _} = error -> raise JsonpatchException, error
98107
end

lib/jsonpatch/operation.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ defprotocol Jsonpatch.Operation do
1313
"""
1414

1515
@doc """
16-
Executes the given patch to map/struct.
16+
Executes the given patch to map/struct. Possible options are defined in `Jsonpatch`.
1717
"""
18-
@spec apply_op(Jsonpatch.t(), map() | Jsonpatch.error()) :: map() | Jsonpatch.error()
19-
def apply_op(patch, target)
18+
@spec apply_op(Jsonpatch.t(), map() | Jsonpatch.error(), keyword()) :: map() | Jsonpatch.error()
19+
def apply_op(patch, target, opts \\ [])
2020
end

0 commit comments

Comments
 (0)