DiUS / java-faker

Brings the popular ruby faker gem to Java
http://dius.github.io/java-faker
Other
4.77k stars 850 forks source link

Idea: faker.randomlyNull(x) #664

Open wcarmon opened 3 years ago

wcarmon commented 3 years ago

Is your feature request related to a problem? Please describe. for nullable fields, it's inconvenient to get a randomly null value.

Describe the solution you'd like

// in faker.options() or similar
public <T> T randomlyNull(T foo) {
  return faker.options().option(null, foo);
}

Describe alternatives you've considered faker.options().option(null, foo);

Additional context Add any other context or screenshots about the feature request here.

akashrpatel621 commented 3 years ago

Hello, I just wanted to ask for a bit more clarification about the feature to be added. I have looked at the options file and see the different overridden functions. Do you want it so that the solution you described is added in as an override function? Any general information would also be helpful. Thank you.

LC24 commented 2 years ago

Hello, if akashrpatel621 has decided to not work on the feature, then I volunteer to work on it in November/December. For the project maintainer, please let me know if this is okay and if you want it added.

akashrpatel621 commented 2 years ago

Hello, I am still making progress trying to complete the issue but just wanted some clarification.

wcarmon commented 2 years ago

Just a regular function.

People can combine it with other functions like

var randomlyNullCity = faker.randomlyNull( faker.cities.name() );
akashrpatel621 commented 2 years ago

I have made a pr but just wanted more clarity so by combining functions are we trying to prevent null from being returned. Right now in my implementation that I derived from the initial issue. I return randomly null with the randomlyNull function.

snuyanzin commented 2 years ago

@wcarmon

may be a bit too late but anyway there is a port of java-faker to jdk8 with lots of improvements including generation of collections with null rate https://github.com/datafaker-net/datafaker

for your case it could be done like that

new FakeCollection.Builder<Boolean>()
            .suppliers(() -> faker.bool().bool())
            .nullRate(0.5)
            .build()
            .singleton();

it will generate Boolean and with probability 0.5 it will return null

As you can see it is possible to use any provider here, so it could be applied not only for boolean