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

Any suggested way to forbid calls to toString on EnumEntryies? #344

Open
PawelLipski opened this issue Apr 22, 2022 · 2 comments
Open

Any suggested way to forbid calls to toString on EnumEntryies? #344

PawelLipski opened this issue Apr 22, 2022 · 2 comments

Comments

@PawelLipski
Copy link

PawelLipski commented Apr 22, 2022

.toString on case objects is generated by Scala compiler, not overridden by Enumeratum... so when a entryName-mangling mixin like UpperSnakecase is involved, .toString and .entryName return different things. To avoid bugs (esp. in the data returned via API), I've added a piece of ArchUnit like that:

"never call toString() on EnumEntries but use entryName() instead" in {
      noClasses.should
        .callMethodWhere {
          target {
            owner {
              assignableTo(classOf[enumeratum.EnumEntry])
            }
          } and target {
            nameMatching("toString")
          }
        }
        .because("entryName should be used instead since toString does not take UPPER_SNAKE_CASE into account")
        .checkAll(importedProductionClasses, importedTestClasses)
    }
  }

But I've just realized this doesn't work, as toString is always recognized to be owned by java.lang.Object.

Is there suggested way to deal with this problem? I can't see anything below a scalac plugin could handle that properly TBH 🤔

@lloydmeta
Copy link
Owner

Yea this is a tough one. Maybe https://github.com/mrdziuban/disable-to-string?

@PawelLipski
Copy link
Author

See https://github.com/mblink/disable-toString

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

No branches or pull requests

2 participants