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

Remove public methods from IdNumber that are not intended to be directly called #1214

Closed
asolntsev opened this issue May 25, 2024 · 8 comments · Fixed by #1229
Closed

Remove public methods from IdNumber that are not intended to be directly called #1214

asolntsev opened this issue May 25, 2024 · 8 comments · Fixed by #1229
Assignees
Labels
enhancement New feature or request

Comments

@asolntsev
Copy link
Collaborator

asolntsev commented May 25, 2024

Describe the bug
It's rather a design issue, not a bug.
I don't like the fact that class net.datafaker.providers.base.IdNumber has all these public methods:

  • validSvSeSsn()
  • validEnZaSsn()
  • inValidEnZaSsn()
  • invalidSvSeSsn()
  • validEsMXSsn()
  • invalidEsMXSsn()
  • validEstonianPersonalCode()
  • invalidEstonianPersonalCode()
  • validAlbanianPersonalCode()
  • invalidAlbanianPersonalCode()
  • validMoldovaPersonalCode()
  • invalidMoldovaPersonalCode()
  • validBulgarianPersonalCode()
  • invalidBulgarianPersonalCode()

Problem 1:

I assume they are NOT intended to be explicitly called from client's code.
Instead, clients should generate corresponding ID numbers using locale/country:

  Faker faker = new Faker(new Locale("et", "EE"));
  String code = faker.idNumbers().valid();

Problem 2:

Having these methods as public makes it impossible to extend functionality of DataFaker. Like, when user adds a custom JAR to classpath that implements yet another language/country.

Expected behavior
It should be possible to add "yet another country" without adding any new public methods.
It should be enough just to add file "LANG.yml" and probably some (package-private) classes like BulgarianIdNumber.

Versions:

  • Faker Version: 2.2.2

@bodiam @snuyanzin thoughts?

@asolntsev asolntsev added the enhancement New feature or request label May 25, 2024
@kingthorin
Copy link
Collaborator

If that's the intent then I'm in agreement. I haven't been working on the project long enough to know.

@bodiam
Copy link
Contributor

bodiam commented May 25, 2024

Makes sense. I don't think we have a super clear agreement about what is our public api vs our more internal. What would you propose to limit the visibility of these methods? Make them package protected? Or something else?

@asolntsev
Copy link
Collaborator Author

I think we don't need these methods at all.
What I suggest:

  1. Remove public methods from IdNumber class:
    • public String validMacedonianPersonalCode()
    • public String invalidMacedonianPersonalCode()
    • etc.
  2. Load all classes like MacedonianIdNumber as service providers (a built-in mechanism in Java). Every provider knows what country it's responsible for.

I can submit a minimal pull request to demonstrate this approach.

@asolntsev asolntsev self-assigned this May 26, 2024
@snuyanzin
Copy link
Collaborator

snuyanzin commented May 26, 2024

yes that probably makes sense

why it was done in current way - probably nobody knows since it was done in javafaker first and those contributors are not active in both communities...

I can submit a minimal pull request to demonstrate this approach.

yes,go ahead

The only thing to note since they are public for a long time then first need to mark as deprecated and then remove later

asolntsev added a commit that referenced this issue May 28, 2024
…try code

Thus, we can add ID numbers for new countries just by implementing "IdNumbers" without need to add public methods like "validZhCNSsn()", "validEstonianPersonalCode()", "validBulgarianPersonalCode()" to class "IdNumber".

Now all country-specific ID number generators are loaded from file "src/main/resources/META-INF/services/net.datafaker.idnumbers.IdNumbers" using a standard Java mechanism "service loader".

+ rename existing "*IdNumber" classes to human-readable, e.g. "ZhCnIdNumber" to "ChineseIdNumber".
asolntsev added a commit that referenced this issue May 29, 2024
there is no point in having method `option(String... options)` when we already have generic method `option(E... options)`.
asolntsev added a commit that referenced this issue May 29, 2024
... instead of introducing a new method.
asolntsev added a commit that referenced this issue May 29, 2024
…try code

Thus, we can add ID numbers for new countries just by implementing "IdNumbers" without need to add public methods like "validZhCNSsn()", "validEstonianPersonalCode()", "validBulgarianPersonalCode()" to class "IdNumber".

Now all country-specific ID number generators are loaded from file "src/main/resources/META-INF/services/net.datafaker.idnumbers.IdNumbers" using a standard Java mechanism "service loader".

+ rename existing "*IdNumber" classes to human-readable, e.g. "ZhCnIdNumber" to "ChineseIdNumber".
asolntsev added a commit that referenced this issue May 29, 2024
... instead of introducing a new method.
@snuyanzin
Copy link
Collaborator

@asolntsev may be for future: I would suggest squashing commits while merging since it will simplify navigation through commits history
All the highlights could be done in commit message

@asolntsev
Copy link
Collaborator Author

@snuyanzin Thanks. Yes, in this case I should squash all commits.
But generally it's not always a good idea. Independent changes should stay in separate commits.

@snuyanzin
Copy link
Collaborator

yep, in generic case it depends on the context

@kingthorin
Copy link
Collaborator

For my two cents it's the overall contribution that matters to the project not the individual bits of work/correction. So I'm in favour of squash and merge or using amending commits.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
4 participants