adrienverge / yamllint

A linter for YAML files.
GNU General Public License v3.0
2.85k stars 273 forks source link

Optional extended detection in key-duplicates #649

Closed fenuks closed 8 months ago

fenuks commented 8 months ago

Let's say I have this yaml document.

logging.level.root: DEBUG
logging.level.org.springframework: INFO

logging:
  level:
    root: DEBUG
    org.springframework: INFO

In generated JSON

{
  "logging.level.root": "DEBUG",
  "logging.level.org.springframework": "INFO",
  "logging": {
    "level": {
      "root": "DEBUG",
      "org.springframework": "INFO"
    }
  }
}

no key is duplicated.

However, some programs, e.g. spring-based allow user to use second form so common prefix is written only once. Logically these are duplicates, so I wonder if key-duplicates rule could have option to enable detection of this, or this is too specific?

adrienverge commented 8 months ago

Hello,

Thank you, this is very clear.

But just as you suspected, this is too specific.

Yamllint handles pure YAML, and logging.level.org.springframework: INFO is different from logging: {level: {org.springframework: INFO}} (by the way, it would be hard to know if we should read org.springframework: … or org: {springframework: …})

fenuks commented 8 months ago

I understand, thank you. I guess I will try to detect this with yq and some shell scripting.

by the way, it would be hard to know if we should read org.springframework: … or org: {springframework: …})

To my understanding, the latter. Keys are always split on dots. Alternatively, dictionary keys could be joined to get flattened dot-delimited key, and then it's easy to check if there are duplicates.

fenuks commented 8 months ago

For anybody wanting perform such check, this is how it can be achieved with a shell one-liner:

yq -r 'tostream | select(length==2) | (.[0] | join(".")) as $k | .[1] as $v | "\($k)"' file.yml | sort | uniq --repeated --count