Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
Metabase
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Engineering Digital Service
Metabase
Commits
7331061b
Unverified
Commit
7331061b
authored
1 year ago
by
metamben
Committed by
GitHub
1 year ago
Browse files
Options
Downloads
Patches
Plain Diff
Allow multiple key-value pairs for assoc-default (#31130)
parent
0ed94c99
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/metabase/util.cljc
+14
-5
14 additions, 5 deletions
src/metabase/util.cljc
test/metabase/util_test.cljc
+20
-0
20 additions, 0 deletions
test/metabase/util_test.cljc
with
34 additions
and
5 deletions
src/metabase/util.cljc
+
14
−
5
View file @
7331061b
...
...
@@ -787,8 +787,17 @@
(
defn
assoc-default
"Called like `(assoc m k v)`, this does [[assoc]] iff `m` does not contain `k`
and `v` is not nil."
[
m
k
v
]
(
if
(
or
(
nil?
v
)
(
contains?
m
k
))
m
(
assoc
m
k
v
)))
and `v` is not nil. Can be called with multiple key value pairs. If a key occurs
more than once, only the first occurrence with a non-nil value is used."
([
m
k
v
]
(
if
(
or
(
nil?
v
)
(
contains?
m
k
))
m
(
assoc
m
k
v
)))
([
m
k
v
&
kvs
]
(
let
[
ret
(
assoc-default
m
k
v
)]
(
if
kvs
(
if
(
next
kvs
)
(
recur
ret
(
first
kvs
)
(
second
kvs
)
(
nnext
kvs
))
(
throw
(
ex-info
"assoc-default expects an even number of key-values"
{
:kvs
kvs
})))
ret
))))
This diff is collapsed.
Click to expand it.
test/metabase/util_test.cljc
+
20
−
0
View file @
7331061b
...
...
@@ -391,3 +391,23 @@
(
is
(
=
{
:foo
false
}
(
u/assoc-dissoc
{
:foo
"bar"
}
:foo
false
))
"false should be assoc'd"
)))
(
deftest
^
:parallel
assoc-default-test
(
testing
"nil map"
(
is
(
=
{
:x
0
}
(
u/assoc-default
nil
:x
0
))))
(
testing
"empty map"
(
is
(
=
{
0
:x
}
(
u/assoc-default
{}
0
:x
))))
(
testing
"existing key"
(
is
(
=
{
:x
0
}
(
u/assoc-default
{
:x
0
}
:x
1
))))
(
testing
"nil value"
(
is
(
=
{
:x
0
}
(
u/assoc-default
{
:x
0
}
:y
nil
))))
(
testing
"multiple defaults"
(
is
(
=
{
:x
nil,
:z
1
}
(
u/assoc-default
{
:x
nil
}
:x
0
:y
nil
:z
1
))))
(
testing
"multiple defaults for the same key"
(
is
(
=
{
:x
nil,
:y
1
,
:z
2
}
(
u/assoc-default
{
:x
nil
}
:x
0
,
:y
nil,
:y
1
,
:z
2
,
:x
3
,
:z
4
)))))
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment