hello, is it possible to do value manipulation with rewrite?
ex: (facts ?d . !body ...) (deftest ?d . !body ...)
?d is a string and I want it to be a symbol
(facts "foo bar" ...)
to (deftest foo-bar ...)
Should be able to do (m/app some-conversion-fn ?d)
On the output of rewrite.
@fe.belineb m/symbol
also works on the right side
Not with a string that has a space though right?
Yeah…
(name (symbol "foo bar"))
;; => "foo bar"
Good ’ol garbage in/garbage out.
@fe.belineb Here are a couple examples
(m/rewrite "foo"
(m/pred string? ?x)
(m/symbol ?x))
;; => foo
(m/rewrite "foo"
(m/pred string? ?x)
(m/symbol ?x "bar"))
;; => foo/bar
(m/rewrite "foo"
(m/pred string? ?x)
(m/symbol "bar" ?x))
;; => bar/foo
You can also do it all on the left side via
(m/rewrite "foo"
(m/pred string? (m/app symbol ?x))
?x)
;; => foo
Which may be safer.
The example above has a space and then is turned into snake case. That's why I said app instead of symbol.
Nice I will take a look into it. The thing is that before converting it to a symbol I need to do some string manipulations, ex:
(facts "Testing account creation")
to
(deftest testing-account-creation")
Ah, didn’t catch that!
is this case I will need to m/app
the output right?
Yah
Like @jimmy suggested (m/app kebab-symbol)
or whatever.
Are you rewriting a test suite? 🙂
that is the idea, taking a look into some options to do that. like rewrite-clj
and https://comby.dev/ too
do you believe that meader is a great fit for that?
(talking about several thousands of test files)
I think there are actually a few people in here doing similar things. Specifically with test suites. Haha. Seems like a popular thing to rewrite. Imo meander should be great for that.
LOL, yeah, should be good for that, though I haven’t tried using it in conjunction with rewrite-clj
. I suppose if I were doing it thought I’d probably just read forms out of files and apply rewrite rules to them, write the files, then apply formatting in post.
One thing I keep meaning to try is rewrite a from to form’ then rewrite form’ to fipp printing instructions.
yes, I am not 100% sure but formatting should not be "that hard", also some formatting issues is better than doing it manually 😂 .
rewrite-clj
preserves the indentation but it is much more difficult to work with. I remember that even for simple tasks as upgrading dependencies version at project.clj
the code was hard to read.
Definitely let use know if you encounter any problems. Seems like rewriting in meander and using something like cljfmt should get you most of the way there.
> much more difficult to work with
That was my memory of it (years ago). Not trying to speak ill of it; I was less experienced then. :man-shrugging:
Just about the question that I did previously, I solve it in that way:
(def facts->deftest
(m*/bottom-up
(m*/attempt
(m*/rewrite
(facts ?d . !body ...) (deftest (m/app kebab-case ?d) . !body ...)))))
You can avoid the cost of the attempt by adding the clause ?x ?x
at the bottom of that rewrite.