Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions .dialyzer_ignore.exs
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
[
{"lib/deep_merge/resolver.ex", :pattern_match}
]
[]
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ You can check out [a benchmark and its results](https://github.com/PragTob/deep_

The TLDR; is this: In the sample it is about 30 times slower than `Map.merge/2` - however, less than twice as slow as calling `Map.merge/3` with simple overriding behaviour (same behaviour as `Map.merge/2`). This is because `Map.merge/2` is highly optimized, but we need to do much more than the `Map.merge/3` sample in the benchmark so I think it's a very passable result. We're still talking about a couple of μs.

## Development

The tests emit type warnings right now on elixir 1.20+.
These tests are actively exercising behavior with unmatching types, so... kinda expected.

## Considered feature-complete

Unless you come with great feature ideas of course ;) So if you come here and there are no recent commits, don't worry - there are no known bugs or whatever. It's a small little library that does its job.
Expand Down
25 changes: 10 additions & 15 deletions lib/deep_merge/resolver.ex
Original file line number Diff line number Diff line change
Expand Up @@ -94,29 +94,24 @@ defimpl DeepMerge.Resolver, for: Any do
@doc """
Fall back to always taking the override.

Also the implementation for `@derive [DeppMerge.Resolver]` where structs of the same type that
Also the implementation for `@derive [DeepMerge.Resolver]` where structs of the same type that
implement the protocol are deeply merged.
"""
def resolve(original = %{__struct__: struct}, override = %{__struct__: struct}, resolver) do
implementors = get_implementors(DeepMerge.Resolver.__protocol__(:impls))

if Enum.member?(implementors, struct) do
impl_module = Module.concat(DeepMerge.Resolver, struct)

# We check for the existence of the generated implementation module rather than using
# `impl_for/1`, because on Elixir < 1.15 `impl_for` returns `DeepMerge.Resolver.Any`
# for derived structs (consolidation inlines the Any delegation), making it impossible
# to distinguish "opted in via @derive" from "no implementation, falling back to Any".
# The generated module (e.g. `DeepMerge.Resolver.Derived`) always exists when @derive
# is used and always defines `__impl__/1`, regardless of version or consolidation state.
if function_exported?(impl_module, :__impl__, 1) do
Map.merge(original, override, resolver)
else
override
end
end

def resolve(_original, override, _fun), do: override

defp get_implementors({:consolidated, implementors}), do: implementors

defp get_implementors(:not_consolidated) do
IO.warn(
"Protocols not consolidated and trying to merge two structs of the same type. Not supported!"
)

# let the code work with override semantics without being intrusive
[]
end
end
9 changes: 7 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,13 @@ defmodule DeepMerge.Mixfile do
flags: [:error_handling, :underspecs],
ignore_warnings: ".dialyzer_ignore.exs",
list_unused_filters: true
],
preferred_cli_env: [
]
]
end

def cli do
[
preferred_envs: [
coveralls: :test,
"coveralls.detail": :test,
"coveralls.post": :test,
Expand Down
Loading