-
-
Notifications
You must be signed in to change notification settings - Fork 48
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
Support for Protocol Buffers #606
Comments
I haven't done a very thorough analysis, but the main problems seem to be with protobuf So far, it seems like these fields always have the name class ProtobufGeneratorProvider : InstancioServiceProvider.GeneratorProvider {
override fun getGenerator(node: Node, generators: Generators): GeneratorSpec<*>? {
val field = node.field ?: return null
// Containing type check
if(!GeneratedMessageV3::class.java.isAssignableFrom(field.declaringClass))
return null
// Check field type
if(field.type != java.lang.Object::class.java)
return null
// Check field name -- we assume here that all protobuf generated internal fields
// end with an underscore.
if(!field.name.endsWith("_"))
return null;
// We assume in this case we have a generated string
return generators.string()
}
} This can be made a bit more robust by checking the existence and presence of the appropriate getter method ( Because of the annoying fun <T : GeneratedMessageV3, V> KFunction1<T, V>.protoField(): TargetSelector {
val fieldName = name.substringAfter("get").lowercase() + "_"
return Select.field(fieldName)
} As for an implementation in the main library, I guess it boils down to detecting the class being an extension of |
@FWest98 thank you for providing the analysis. It sounds like an interesting approach. Any chance you could put together a sample project? It might be useful as a starting point for others looking for a solution to this. As for library support, I don't have a solution to this yet, but I'm keeping this issue open for now. |
#767 added enhancements to Will keep the issue open in case somebody is interested in implementing this in a fork or as an extension project. |
Protobuf support would indeed be an incredible nice to have. Proto strings as raised by @FWest98 are causing issues |
@DorianNaaji protobuf strings can be created using a custom generator, e.g. public class ByteStringGenerator implements Generator<ByteString> {
@Override
public ByteString generate(final Random random) {
final int length = random.intRange(
Keys.STRING_MIN_LENGTH.defaultValue(),
Keys.STRING_MAX_LENGTH.defaultValue());
return ByteString.copyFromUtf8(random.upperCaseAlphabetic(length));
}
@Override
public Hints hints() {
return Hints.afterGenerate(AfterGenerate.DO_NOT_MODIFY);
}
} I created a sample project here: https://github.com/instancio/instancio-github-issues/tree/main/issue-606 The sample doesn't support:
Hope it helps as a starting point despite the limitations. |
Feature description
Investigate adding support for protobufs, as suggested in the following discussion:
The text was updated successfully, but these errors were encountered: