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

Java Object transformation to list #979

Closed
albilu opened this issue Oct 2, 2023 · 6 comments · Fixed by #980
Closed

Java Object transformation to list #979

albilu opened this issue Oct 2, 2023 · 6 comments · Fixed by #980

Comments

@albilu
Copy link

albilu commented Oct 2, 2023

Is Java object transformation to list possible??

From the documentation here https://www.datafaker.net/documentation/schemas/#java-object-transformation it seems only singleton is posssible unless i missed something.

Please advise. Here is my iomplementation:

Faker faker = new Faker();

Schema<Object, ?> schema = Schema.of(
                Field.field("name", () -> faker.name().firstName()),
                Field.field("email", () -> faker.internet().emailAddress())
        );

BaseFaker bfaker = new BaseFaker();
Client client = bfaker.populate(Client.class, schema); //Here i would like to have a collection of fake Clients instead
@snuyanzin
Copy link
Collaborator

snuyanzin commented Oct 2, 2023

you could use FakeCollection or FakeSequence https://www.datafaker.net/documentation/sequences/
and just use populate as a supplier for these Collection/Sequence
to do it like

List<DefaultPerson> list = new FakeCollection.Builder<DefaultPerson>()
            .suppliers(() -> Faker.populate(DefaultPerson.class)).generate();

        System.out.println(list);

so in your case it could be something like

Faker faker = new Faker();

Schema<Object, ?> schema = Schema.of(
                Field.field("name", () -> faker.name().firstName()),
                Field.field("email", () -> faker.internet().emailAddress())
        );

BaseFaker bfaker = new BaseFaker();
List<Client> list = new FakeCollection.Builder<Client>()
            .suppliers(() -> Faker.populate(Client.class, schema)).len(<min>, <max>).generate();

@albilu
Copy link
Author

albilu commented Oct 2, 2023

@snuyanzin Thanks for the feedback.
Issue now is the Client objects have the same fields values. Goal is to have random value for each instance.

List<Client> client = new FakeCollection.Builder<Client>().suppliers(()
                -> Faker.populate(Client.class, schema))
                .len(5).generate();

client2.forEach((t) -> {
            System.out.println(t.getName() + " " + t.getEmail());

        });

Output

Although it works with simple Collection generation

List<String> names = 
    faker.collection(
            () -> faker.name().firstName(), 
            () -> faker.name().lastName())
        .len(5)
        .generate();

Output

Corkery
Otha
Moore
Tisa
Sawayn

@snuyanzin
Copy link
Collaborator

Can you share the complete code snippet?

@bodiam
Copy link
Contributor

bodiam commented Oct 2, 2023

Hi @albilu , could you give the snapshot release a try?

@albilu
Copy link
Author

albilu commented Oct 3, 2023

Hi @albilu , could you give the snapshot release a try?

@bodiam works as expected thanks! any date on when it will be release?

@bodiam
Copy link
Contributor

bodiam commented Oct 3, 2023

Hi @albilu , could you give the snapshot release a try?

@bodiam works as expected thanks! any date on when it will be release?

It's released, you can use the 2.0.2 now.

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 a pull request may close this issue.

3 participants