Skip to content
New issue

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

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

Already on GitHub? # to your account

Rule: use-object-keys #1044

Closed
anderseknert opened this issue Sep 4, 2024 · 0 comments · Fixed by #1399
Closed

Rule: use-object-keys #1044

anderseknert opened this issue Sep 4, 2024 · 0 comments · Fixed by #1399

Comments

@anderseknert
Copy link
Member

anderseknert commented Sep 4, 2024

Another rule in the "let's use a built-in for that" series. Whenever a set comprehension is used to collect the keys from an object, we should recommend using object.keys instead, as it's easier to read, and less "imperative".

Avoid

package policy

keys := {k | some k, _ in input.object}

# or

keys := {k | some k; input.object[k]}

Prefer

package policy

keys := object.keys(input.object)

Most often, we can't really tell the type of the input used in a comprehension like this statically. Meaning that something like:

{user | some user, _ in users}

...could be collecting indices of an array, or "key" values from a set. However, collecting array indices into a set seems unlikely / pointless, and collecting values from a set into another set with no further processing is equally pointless. Obviously, we'll need to ensure that only the simples form matches, not when more things happen in the comprehension.

anderseknert added a commit that referenced this issue Feb 6, 2025
Fixes #1044

Signed-off-by: Anders Eknert <anders@styra.com>
anderseknert added a commit that referenced this issue Feb 6, 2025
Fixes #1044

Signed-off-by: Anders Eknert <anders@styra.com>
anderseknert added a commit that referenced this issue Feb 6, 2025
Fixes #1044

Signed-off-by: Anders Eknert <anders@styra.com>
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant