enumeratum icon indicating copy to clipboard operation
enumeratum copied to clipboard

Any suggested way to forbid calls to `toString` on `EnumEntry`ies?

Open PawelLipski opened this issue 2 years ago • 2 comments

.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 🤔

PawelLipski avatar Apr 22 '22 10:04 PawelLipski

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

lloydmeta avatar Apr 22 '22 11:04 lloydmeta

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

PawelLipski avatar Feb 16 '23 18:02 PawelLipski