Giter VIP home page Giter VIP logo

Comments (15)

bodiam avatar bodiam commented on May 26, 2024 1

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.

bodiam avatar bodiam commented on May 26, 2024 1

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.

bodiam avatar bodiam commented on May 26, 2024 1

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.

bodiam avatar bodiam commented on May 26, 2024 1
    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.

snuyanzin avatar snuyanzin commented on May 26, 2024 1

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.

snuyanzin avatar snuyanzin commented on May 26, 2024 1

Current version does not support usage of Fakers functionality for marshalling data from custom yml.
In theory it could be improved

from datafaker.

snuyanzin avatar snuyanzin commented on May 26, 2024 1

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

dnicholas2001 avatar dnicholas2001 commented on May 26, 2024

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.

dnicholas2001 avatar dnicholas2001 commented on May 26, 2024

I agree, I like the idea of using Generics!

from datafaker.

dnicholas2001 avatar dnicholas2001 commented on May 26, 2024

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

bodiam avatar bodiam commented on May 26, 2024

What do you think, can this issue be closed?

from datafaker.

snuyanzin avatar snuyanzin commented on May 26, 2024

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.

dnicholas2001 avatar dnicholas2001 commented on May 26, 2024

Looks good to me, thanks very much!!

Do we know when this feature will be released?

from datafaker.

bodiam avatar bodiam commented on May 26, 2024

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

dnicholas2001 avatar dnicholas2001 commented on May 26, 2024

@bodiam , didn't know that, great to hear! Will definitely try it out; thanks for sharing!

from datafaker.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.