Git Product home page Git Product logo

Comments (5)

MatthiasOs avatar MatthiasOs commented on June 2, 2024 1

Ok thanks.
The suggested solutions works with version 4.1.0
(i got some exception when using 3.6.0 in our main project, so I just bumped up the version)

from instancio.

armandino avatar armandino commented on June 2, 2024

Thanks for raising this issue and creating a sample project. It helps a lot!

Unfortunately, this usage is not supported because CollectionGenerator and Global are internal classes. Anything under internal package or annotated with @InternalApi is not intended to be used by users.

Based on the sample project, I assume the goal is to generate a collection of size 1 (by default) for MyClass.values. If that's the case, you could use the provided Generators instance in your SPI implementation:

public class MyClassGenerator implements InstancioServiceProvider {

    @Override
    public InstancioServiceProvider.GeneratorProvider getGeneratorProvider() {
        return (node, generators) -> {
            if (matchesNode(node)) {
                // 'generators' provides access to all built-in generators
                return generators.collection().size(1);
            }
            return null;
        };
    }

    private boolean matchesNode(Node node) {
        Field field = node.getField();
        return field != null
                && MyClass.class.isAssignableFrom(field.getDeclaringClass())
                && node.getTargetClass() == List.class;
    }
}

Does this work for you use case?

from instancio.

MatthiasOs avatar MatthiasOs commented on June 2, 2024

Hey,

thanks for the answer, i did not see the annotation.
Just to clarify: All custom Generators should just implement the Generator interface, correct?
(My code provided is not a real Generator since it just delegates, so it shouldnt implement the interface)

from instancio.

armandino avatar armandino commented on June 2, 2024

All custom Generators should just implement the Generator interface, correct?

That's right, all generators should implement the Generator interface. The Global class contains an internal instance of Random and Settings. Since you already get those objects using public APIs, the Global class shouldn't be needed for a custom generator. To delegate to the collection generator, you can use the collection() method as above.

from instancio.

armandino avatar armandino commented on June 2, 2024

i got some exception when using 3.6.0 in our main project, so I just bumped up the version

Thanks for the update. This makes sense now. There was a fix for this bug not too long ago:

Let me know if you run into other issues.

from instancio.

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.