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
d86597b1
Unverified
Commit
d86597b1
authored
1 year ago
by
Nemanja Glumac
Committed by
GitHub
1 year ago
Browse files
Options
Downloads
Patches
Plain Diff
Revert "shared.ns/import-fn(s) emit optimized defn forms (#29700)" (#29715)
This reverts commit
a817ed8a
.
parent
25a1b259
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
shared/src/metabase/shared/util/namespaces.clj
+7
-54
7 additions, 54 deletions
shared/src/metabase/shared/util/namespaces.clj
shared/test/metabase/shared/util/namespaces_test.cljc
+0
-39
0 additions, 39 deletions
shared/test/metabase/shared/util/namespaces_test.cljc
with
7 additions
and
93 deletions
shared/src/metabase/shared/util/namespaces.clj
+
7
−
54
View file @
d86597b1
(
ns
metabase.shared.util.namespaces
"Potemkin is Java-only, so here's a basic function-importing macro that works for both CLJS and CLJ."
(
:require
[
net.cgrand.macrovich
:as
macros
]
[
potemkin
:as
p
]))
[
net.cgrand.macrovich
:as
macros
]
[
potemkin
:as
p
]))
(
defn-
arity-form
"Generate the form for a given arity with `arglist` for a `defn` form created by [[-redef]]."
[
imported-symbol
arglist
]
(
let
[
variadic?
(
some
(
partial
=
'&
)
arglist
)
simplified-arglist
(
mapv
(
fn
[
arg
]
(
if
(
symbol?
arg
)
arg
(
gensym
"arg-"
)))
arglist
)]
(
list
simplified-arglist
(
cond->>
(
list*
imported-symbol
(
remove
(
partial
=
'&
)
simplified-arglist
))
variadic?
(
cons
'apply
)))))
(
defn-
defn-form
"Generate the `defn` form for [[-redef]]."
[
imported-symbol
defn-name
docstring
arglists
]
`
(
defn
~
defn-name
~
docstring
;; ClojureScript seems to ignore this metadata anyway, oh well. Make sure we re-quote arglists so the reader
;; doesn't try to evaluate them.
{
:arglists
'~
arglists
}
~@
(
for
[
arglist
arglists
]
(
arity-form
imported-symbol
arglist
))))
(
defmacro
-redef
"Impl for [[import-fn]] and [[import-fns]]."
[
imported-symbol
created-symbol
]
{
:pre
[(
qualified-symbol?
imported-symbol
)
((
some-fn
nil?
simple-symbol?
)
created-symbol
)]}
;; In ClojureScript compilation, `imported-symbol` will come in like `u/format-seconds` instead of
;; `metabase.util/format-seconds`, thus we'll need to use [[cljs.analyzer/resolve-symbol]] to resolve it to the real
;; unaliased version.
(
let
[
resolve-symbol
(
macros/case
:cljs
(
requiring-resolve
'cljs.analyzer/resolve-symbol
)
:clj
identity
)
imported-var
(
requiring-resolve
(
resolve-symbol
imported-symbol
))
imported-metadata
(
meta
imported-var
)
metadata
(
merge
{
:doc
"docstring"
:arglists
'
([
&
args
])}
;; preserve important metadata from the imported var.
imported-metadata
;; preserve metadata attached to the symbol(s) passed in to import-fn(s).
(
meta
created-symbol
)
(
meta
imported-symbol
))
defn-name
(
->
(
or
created-symbol
(
symbol
(
name
imported-symbol
)))
;; attach everything except for `:doc` and `:arglists` to the symbol we're deffing; we'll
;; deal with `:doc` and `:arglists` separately.
(
with-meta
(
dissoc
metadata
:doc
:arglists
)))]
(
defn-form
imported-symbol
defn-name
(
:doc
metadata
)
(
:arglists
metadata
))))
(
defn-
redef
[
target
sym
]
(
let
[
defn-name
(
or
sym
(
symbol
(
name
target
)))]
`
(
def
~
defn-name
"docstring"
(
fn
[
&
args
#
]
(
apply
~
target
args
#
)))))
(
defmacro
import-fn
"Imports a single defn from another namespace.
...
...
@@ -64,7 +17,7 @@
([
target
]
`
(
import-fn
~
target
nil
))
([
target
sym
]
`
(
-
redef
~
target
~
sym
)))
(
redef
target
sym
)))
(
defmacro
import-fns
"Imports defns from other namespaces.
...
...
@@ -79,5 +32,5 @@
:let
[
target-sym
(
if
(
vector?
f
)
(
first
f
)
f
)
new-sym
(
if
(
vector?
f
)
(
second
f
)
f
)
target
(
symbol
(
name
target-ns
)
(
name
target-sym
))]]
`
(
-
redef
~
target
~
new-sym
)))
(
redef
target
new-sym
)))
:clj
`
(
p/import-vars
~@
spaces
)))
This diff is collapsed.
Click to expand it.
shared/test/metabase/shared/util/namespaces_test.cljc
deleted
100644 → 0
+
0
−
39
View file @
25a1b259
(
ns
metabase.shared.util.namespaces-test
(
:require
[
clojure.test
:refer
[
deftest
is
testing
are
]]
[
metabase.shared.formatting.date
:as
shared.formatting.date
]
[
metabase.shared.util.namespaces
:as
shared.ns
]))
#
?
(
:clj
(
deftest
^
:synchronized
arity-form-test
(
with-redefs
[
gensym
(
fn
[
prefix
]
(
symbol
(
str
prefix
"1234"
)))]
(
are
[
arglist
expected-form
]
(
=
expected-form
(
#
'shared.ns/arity-form
'imported/fn
arglist
))
'
[
x
]
'
([
x
]
(
imported/fn
x
))
'
[
x
y
]
'
([
x
y
]
(
imported/fn
x
y
))
'
[{
:destructured
:map
}
x
]
'
([
arg-1234
x
]
(
imported/fn
arg-1234
x
))
'
[
x
&
more
]
'
([
x
&
more
]
(
apply
imported/fn
x
more
))))))
;;; this is just an arbitrarily selected function that has `:export` metadata; we can change it to something else if
;;; it changes upstream.
(
shared.ns/import-fn
^
::extra-key
shared.formatting.date/format-for-parameter
)
(
deftest
^
:parallel
import-fn-test
(
testing
"Should copy important metadata from the source var"
(
is
(
:export
(
meta
#
'format-for-parameter
)))
(
is
(
=
(
select-keys
(
meta
#
'shared.formatting.date/format-for-parameter
)
[
:export
:arglists
:doc
])
(
select-keys
(
meta
#
'format-for-parameter
)
[
:export
:arglists
:doc
]))))
(
testing
"Should preserve metadata on the symbol(s) passed to import-fn(s)"
(
is
(
::extra-key
(
meta
#
'format-for-parameter
)))))
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