Skip to content

Conversation

@asolntsev
Copy link
Collaborator

@asolntsev asolntsev commented May 28, 2024

Sorry for too big PR.
I just wanted to remove unneeded public method before we release a new version and people start using them.

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

…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".
/**
* @return ISO-2 code of the country ("US" for America, "EE" for Estonia etc.)
*/
String country();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would countryCode be perhaps a better named method? A method like country() might return "America" or so.

Copy link
Contributor

@bodiam bodiam May 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, shouldn't this class perhaps be called "IdNumber", instead of "IdNumbers" (so, without the s?)

update: ah, I see that we already have an IdNumber class. Okay, maybe ignore this comment then, or maybe IdNumberGenerator ?

Copy link
Collaborator Author

@asolntsev asolntsev May 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bodiam I like both variants: countryCode and IdNmberGenerator. Renamed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, thanks!

@bodiam
Copy link
Contributor

bodiam commented May 28, 2024

I left a tiny comment, but really nice PR, thanks for that!

Copy link
Collaborator

@kingthorin kingthorin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't get through the whole PR

}

public String getValid(BaseProviders faker) {
public String generateValid(BaseProviders faker) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will break for anyone using it directly. We should deprecate for removal and it can call the new method.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also shouldn't generate valid have an override annotation?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess my original comment here goes for any of these country classes

Copy link
Collaborator Author

@asolntsev asolntsev May 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will break for anyone using it directly

Generally it's a valid point, but not in this case.
These 5 countries were added recently by me (Estonia, Albania, Bulgaria, Macedonia, Moldova), and not yet released. So they are not yet used by anybody.

Also shouldn't generate valid have an override annotation?

Yes, good point. I've added missing @Override annotations and enabled corresponding inspection in IDEA.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great!

}

/**
* Returns a random String element from an varargs.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it could be not only String

Copy link
Collaborator Author

@asolntsev asolntsev May 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it could be not only String

@snuyanzin Good point, fixed javadoc.
UPD I removed this method at all, used nextElement instead.

Comment on lines 113 to 115
public <T> T option(List<T> options) {
return options.get(faker.random().nextInt(options.size()));
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the very end of this class there is already existing method

    /**
     * Returns a random element from a list.
     *
     * @param list The list to take a random element from.
     * @param <E>  The type of the elements in the list.
     * @return A randomly selected element from the list.
     */
    public <E> E nextElement(List<E> list) {
        return list.get(faker.random().nextInt(list.size()));
    }

will not it help?

We need a separate for String because it could be used in expressions, however it's impossible to use generics in expressions... at least right now

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@snuyanzin Yes, indeed, I had to use this method. Thanks, fixed.

... instead of introducing a new method.
@asolntsev asolntsev force-pushed the refactoring/1214-load-country-specific-providers-by-country-code branch from bf3717f to c712b4f Compare May 29, 2024 06:15
Copy link
Collaborator

@kingthorin kingthorin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@bodiam
Copy link
Contributor

bodiam commented May 29, 2024

LGTM2!

@asolntsev asolntsev merged commit 3a57f20 into main May 29, 2024
@asolntsev asolntsev deleted the refactoring/1214-load-country-specific-providers-by-country-code branch May 29, 2024 14:16
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

Successfully merging this pull request may close these issues.

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

4 participants