Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for namespaced keywords #83

Merged
merged 3 commits into from
Jun 17, 2022
Merged

Conversation

vmfhrmfoaj
Copy link
Contributor

@vmfhrmfoaj vmfhrmfoaj commented Mar 26, 2022

First of all, thank you for developing and maintaining 'compliment'.
I found problems related to namespaced keywords:

  • compliment.context/try-read-replacing-maps return nil if aliased namespace keywords(e.g. ::my-ns/keyword) are contained in context string. (read-str try to resolve the alias)
    I changed try-read-replacing-maps to replace aliased namespace keyword to namespaced keyword(e.g. :my-ns/keyword) to avoid the problem.
  • compliment.sources.local-bindings/parse-binding ignore variables that destructuring the map with namespaced keywords:
    (require '[instaparse.gll :as gll])
    (let [#_..., {::gll/keys [start-index end-index]} (meta x)])
    I changed parse-binding to handle namespaced keywords.

Copy link
Contributor

@vemv vemv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some suggestions

@@ -18,6 +18,7 @@
(-> s
(str/replace "{" "(compliment-hashmap ")
(str/replace "}" ")")
(str/replace #"::([-\w]+/)" (fn [[_ kw-ns]] (str ":" kw-ns)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems more accurate to query (ns-aliases *ns*) to obtain a namespace name to be prepended to kw-ns?

{:keys [key1 key2] :strs [key3]} m2
{:keys [key1 key2] :strs [key3] :syms [key4]} m2
{:keys [:a/key5 :b/key6]} m3
{:c/keys [key7 key8]} m4
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't there also be test coverage for...?

  • :c/syms
  • ::c/keys
  • ::c/syms

@@ -82,10 +82,13 @@
'(let [foo 42,
[bar baz] lst
{a :a, {b :b :as c} :b, [d] :d} m
{:keys [key1 key2] :strs [key3]} m2
{:keys [key1 key2] :strs [key3] :syms [key4]} m2
{:keys [:a/key5 :b/key6]} m3
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there should also be coverage for {:keys [::a/keyN ::b/keyM]} (i.e. ns-aliased)

@@ -12,6 +12,10 @@

(def letfn-like-forms '#{letfn})

(def destructuring-keys #{:keys :strs :syms})

(def destructuring-key-names #{"keys" "strs" "syms"})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(def destructuring-key-names #{"keys" "strs" "syms"})
(def destructuring-key-names (into #{} (map name) destructuring-keys))

@vmfhrmfoaj
Copy link
Contributor Author

@vemv Fixed, thank you for review.

@bbatsov
Copy link
Collaborator

bbatsov commented Jun 17, 2022

@vmfhrmfoaj Your changes look to good to me. Can you also mention them in the changelog, please?

@vmfhrmfoaj
Copy link
Contributor Author

@bbatsov I updated.

@vemv
Copy link
Contributor

vemv commented Jun 17, 2022

Thanks for accurately satisfying all suggestions!

@bbatsov
Copy link
Collaborator

bbatsov commented Jun 17, 2022

@vmfhrmfoaj Can you also rebase on top master to resolve the conflict in the changelog?

CHANGELOG.md Outdated
@@ -2,6 +2,9 @@

### master (unreleased)

- Complete local bindings declared in map destructuring with namespaced keywords.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, prefix those entries with the link to the PR.

… generating the context

`read-string` try to resolve an alias of namespaced keyword and throw 'invalid token' exception if there is no the alias.
so, `try-read-replacing-maps` return `nil` if the context is contained the aliased namespace keyword.
(except if alias is already added to *ns*)

Benchmark before:
  Evaluation count : 126 in 6 samples of 21 calls.
               Execution time mean : 5.085000 ms
      Execution time std-deviation : 141.272913 µs
     Execution time lower quantile : 4.982329 ms ( 2.5%)
     Execution time upper quantile : 5.293605 ms (97.5%)
                     Overhead used : 5.443103 ns
--------------------------------------------------------------------------------
Benchmark after, string ver `(str/replace "::" ":")`:
  Evaluation count : 126 in 6 samples of 21 calls.
               Execution time mean : 5.219457 ms
      Execution time std-deviation : 213.306958 µs
     Execution time lower quantile : 5.046182 ms ( 2.5%)
     Execution time upper quantile : 5.456526 ms (97.5%)
                     Overhead used : 6.702279 ns
--------------------------------------------------------------------------------
Benchmark after, regular expression ver:
  Evaluation count : 120 in 6 samples of 20 calls.
               Execution time mean : 5.265777 ms
      Execution time std-deviation : 208.308093 µs
     Execution time lower quantile : 5.122747 ms ( 2.5%)
     Execution time upper quantile : 5.522446 ms (97.5%)
                     Overhead used : 5.454686 ns
if use namespaced keywords, can't complete destructured variables

Before benchmark:
  Evaluation count : 126 in 6 samples of 21 calls.
               Execution time mean : 5.070952 ms
      Execution time std-deviation : 86.793704 µs
     Execution time lower quantile : 5.011867 ms ( 2.5%)
     Execution time upper quantile : 5.208428 ms (97.5%)
                     Overhead used : 5.254489 ns
--------------------------------------------------------------------------------
After benchmark:
  Evaluation count : 120 in 6 samples of 20 calls.
               Execution time mean : 5.110484 ms
      Execution time std-deviation : 139.512617 µs
     Execution time lower quantile : 4.972072 ms ( 2.5%)
     Execution time upper quantile : 5.263425 ms (97.5%)
                     Overhead used : 6.469988 ns
@bbatsov bbatsov merged commit 684a296 into alexander-yakushev:master Jun 17, 2022
@bbatsov
Copy link
Collaborator

bbatsov commented Jun 17, 2022

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants