Comments (15)
Hi @dnicholas2001 , would you be willing to maybe implement or prototype a feature like this?
I'm very open to the idea, and I thought that creating a provider and giving it a faker in the constructor would already bring you quite far, except for the loading of the yml files.
However, I think in general it wouldn't be too bad to replace that whole list of yml files to the individual providers themselves.
from datafaker.
Ideally, it would support the (future) idea of being able to split the faker into separate modules. For example, I can imagine an Address faker is more interesting to most than something like an IT crowd faker. But I don't want to overcomplicate things, but just an idea.
from datafaker.
You're right that the Faker api complicates things.
The api I had in mind, but I'm not 100% sure, was to do either:
PlantProvider(new Faker()).flower()
Or:
public <T> T provider(Class<T> provider)
So that you can do:
new Faker().provider(PlantProvider.class).flower()
I don't know how the interface approach could work, since like you said, there's not really an overlap between the different domains, so not using interfaces but using generics could maybe be a better approach. I haven't tested it yet though, so it's just ideas.
from datafaker.
public <T> T provider(Class<T> provider) {
return null;
}
void test() {
new Faker().provider(Avatar.class).image();
}
The above seems to work (or at least compile), so something like this might be an option. If you'd mix this with an SPI (if you need it really, maybe just passing the class is good enough), that could be a good starting point.
It doesn't have a to be perfect, but maybe just a starting point, and we'll work out something,
from datafaker.
It also could be done by extending Faker
e.g. there is a custom rocket name generator
public static class Rocket<T extends Faker> {
private final T faker;
public Rocket(T faker) {
this.faker = faker;
}
public String name() {
String[] names = new String[] {"Appollo", "Soyuz", "Vostok", "Voskhod", "Progress", "Falcon", "Gemini", "Mercury"};
return names[faker.random().nextInt(names.length)];
}
}
Now need to extend Faker
like
public class MyFaker extends Faker {
public Rocket rocket() {
return getProvider(Rocket.class, () -> new Rocket<>(this));
}
}
and finally it could be used with names from custom
@Test
public void myRocketTest() {
MyFaker myFaker = new MyFaker();
for (int i = 0; i < 10; i++) {
System.out.println(myFaker.rocket().name());
}
}
@Test
public void myRocketTest2() {
MyFaker myFaker = new MyFaker();
for (int i = 0; i < 10; i++) {
System.out.println(myFaker.expression("#{Rocket.name}"));
}
}
The full ready to play example could be found here b0cc7bc
P.S. currently this could be done with usage of master's version...
datafaker 1.1.0 does not support this
from datafaker.
Current version does not support usage of Faker
s functionality for marshalling data from custom yml
.
In theory it could be improved
from datafaker.
@dnicholas2001 I have a PR supporting adding files with custom data, you might want to have a look at it
#49
Also there is an example inside tests
from datafaker.
Would be happy to contribute! The existing design is using concrete classes, so it would be a challenge to get the Faker class to have knowledge of an external service. For example, how would it know to call faker.spaceForce().nextRocketName()
, for example.
Unless, maybe, there was an interface to club together common categories of data. Maybe IProvideSampleData
and each interface method could provide some level of data. But the data provided is specific to the industry, so nextRocketName()
applies to space, but nextVehicleName()
applies to automotive.
Maybe there's a pattern that is common to the existing domains represented by the yml
files? Will look into this
from datafaker.
I agree, I like the idea of using Generics!
from datafaker.
@snuyanzin Many thanks for sharing. In your example, would you recommend we maintain the sample data within the Java file, or is there a way you think we can reuse the functionality within Faker
to marshal the sample data from the yml
files?
from datafaker.
What do you think, can this issue be closed?
from datafaker.
The pr was merged, there are several examples within a test src/test/java/net/datafaker/CustomFakerTest.java showing how custom fakers could be used
@dnicholas2001 could you please tell if there is anything missing?
from datafaker.
Looks good to me, thanks very much!!
Do we know when this feature will be released?
from datafaker.
@dnicholas2001 it's already released as a snapshot, so if you want to try that one out, that would be great. The actual 1.2.0 release is planned for ~10 days, but would be great if you could verify it before that release in case there's any missing feature or so.
from datafaker.
@bodiam , didn't know that, great to hear! Will definitely try it out; thanks for sharing!
from datafaker.
Related Issues (20)
- Using with ColdFusion and/or Lucee CFML HOT 18
- Add getVersion() method HOT 3
- Dependabot is broken
- Specify locale when using Java Object transformer HOT 2
- POSIX character classes support HOT 2
- faker.internet().url() creates the hostname only without the scheme HOT 3
- How to support extensible fluent data providers HOT 6
- Dependency Dashboard
- CreditCardType is not accessible although it's used as a parameter to a public method HOT 13
- Tyre sizes HOT 12
- Custom provider not recognized in expression HOT 10
- Make providers SPI friendly
- Does version 2.0.1 work with GraalVM 22.3.2 Java 17 CE? HOT 5
- Email address is not unique.
- Defining a locale using expressions? HOT 9
- Memory leak in PROVIDERS_MAP HOT 2
- Random generator from java Enums HOT 9
- Question or Feature : JPA objects to Schema Object HOT 1
- [BUG]: ID_NUMBER flaky call of the method valid() HOT 3
- [discussion] Move username to internet? HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from datafaker.