-
|
I have this object: now I wanna populate it with some fix values. Can I create a customGenerator/Setter to intercept the var type and fill it for me? Ex: So here I dont need to use scope everytime and check the type of var. my issue today is I have a lot of vars in the same class that sometimes is CodeDto and sometimes is String so I need to check it before using a fix values to set the vars for each set. I checked the supplies and customGenerator but I think the custom generator I cant pass fixed values. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
|
@gabryellr if i understand correctly, your classes can contain multiple class Example {
CodeDto foo;
CodeDto bar;
//...
}There's no workaround that i can think of that doesn't require using scopes. One option is to create a utility selector like this: public class SelectCode {
public static <T,R> TargetSelector of(ScopeableSelector selector) {
final Scope scope = selector.toScope();
return Select.field(CodeDto::getCode).within(scope);
}
}
// Usage:
var result = Instancio.of(Example.class)
.set(SelectCode.of(field(Example::getFoo)), "foo")
.set(SelectCode.of(field(Example::getBar)), "bar")
.create();You could also name the method Hope this solves your issue. |
Beta Was this translation helpful? Give feedback.
@gabryellr if i understand correctly, your classes can contain multiple
CodeDtofields:There's no workaround that i can think of that doesn't require using scopes. One option is to create a utility selector like this:
You could also name the …