use of com.google.protobuf.Descriptors.Descriptor in project core-java by SpineEventEngine.
the class ReferenceValidator method findSourceFieldsByNames.
private Collection<FieldDescriptor> findSourceFieldsByNames(String[] names, FieldDescriptor enrichmentField) {
checkArgument(names.length > 0, "Names may not be empty");
checkArgument(names.length > 1, "Enrichment target field names may not be a singleton array. " + "Use findSourceFieldByName().");
final Collection<FieldDescriptor> result = new HashSet<>(names.length);
FieldDescriptor.Type basicType = null;
Descriptor messageType = null;
for (String name : names) {
final FieldDescriptor field = findSourceFieldByName(name, enrichmentField, false);
if (field == null) {
/* We don't know at this stage the type of the event.
The enrichment is to be included anyway,
but by other ReferenceValidator instance */
continue;
}
if (basicType == null) {
// Get type of the first field
basicType = field.getType();
if (basicType == MESSAGE) {
messageType = field.getMessageType();
}
} else {
// Compare the type with each of the next
checkState(basicType == field.getType(), differentTypesErrorMessage(enrichmentField));
if (basicType == MESSAGE) {
checkState(messageType.equals(field.getMessageType()), differentTypesErrorMessage(enrichmentField));
}
}
final boolean noDuplicateFiled = result.add(field);
checkState(noDuplicateFiled, "Enrichment target field names may contain no duplicates. " + "Found duplicate field: %s", name);
}
return result;
}
use of com.google.protobuf.Descriptors.Descriptor in project core-java by SpineEventEngine.
the class Sample method builderForType.
/**
* Generates a new stub {@link Message.Builder} with all the fields set to
* {@link Random random} values.
*
* <p> All the fields are guaranteed to be not {@code null} and not default.
* Number and {@code boolean} fields may or may not have their default values ({@code 0} and
* {@code false}).
*
* @param clazz Java class of the stub message
* @param <M> type of the required message
* @param <B> type of the {@link Message.Builder} for the message
* @return new instance of the {@link Message.Builder} for given type
* @see #valueFor(FieldDescriptor)
*/
public static <M extends Message, B extends Message.Builder> B builderForType(Class<M> clazz) {
checkClass(clazz);
final B builder = builderFor(clazz);
final Descriptor builderDescriptor = builder.getDescriptorForType();
final Collection<FieldDescriptor> fields = builderDescriptor.getFields();
for (FieldDescriptor field : fields) {
final Object value = valueFor(field);
if (field.isRepeated()) {
builder.addRepeatedField(field, value);
} else {
builder.setField(field, value);
}
}
return builder;
}
use of com.google.protobuf.Descriptors.Descriptor in project toolkit by googleapis.
the class ConfigYamlNodeReader method readMapNode.
private ConfigNode readMapNode(int prevLine, Node node, FieldDescriptor field) {
if (isEmpty(node)) {
return new NullConfigNode();
}
if (!(node instanceof MappingNode)) {
helper.error(node, "Expected a map to merge with '%s', found '%s'.", field.getFullName(), node.getNodeId());
return null;
}
Descriptor messageType = field.getMessageType();
ConfigNode configNode = new NullConfigNode();
ConfigNode prev = new NullConfigNode();
for (NodeTuple entry : ((MappingNode) node).getValue()) {
ConfigNode entryNode = readMapNodeEntry(entry, messageType);
ConfigNode commentNode = readCommentNode(prevLine, entry.getKeyNode(), entryNode);
prevLine = entry.getValueNode().getEndMark().getLine() + 1;
if (entryNode == null) {
continue;
}
prev.insertNext(commentNode);
prev = entryNode;
if (!configNode.isPresent()) {
configNode = commentNode;
}
}
return configNode;
}
use of com.google.protobuf.Descriptors.Descriptor in project toolkit by googleapis.
the class MessageGenerator method generate.
@VisitsBefore
boolean generate(FieldConfigNode node) {
String name = node.getText();
Descriptor messageType = messageBuilder.getDescriptorForType();
if (field != null && field.isMapField()) {
FieldDescriptor keyField = messageType.findFieldByName("key");
if (keyField == null) {
values = null;
return false;
}
messageBuilder.setField(keyField, name);
FieldDescriptor valueField = messageType.findFieldByName("value");
if (valueField == null) {
values = null;
return false;
}
Message.Builder childBuilder = valueField.getType() == FieldDescriptor.Type.MESSAGE ? messageBuilder.newBuilderForField(valueField) : null;
Object value = generateSingularValue(node.getChild(), valueField, childBuilder);
if (value == null) {
values = null;
return false;
}
messageBuilder.setField(valueField, value);
values.add(messageBuilder.build());
messageBuilder.clear();
} else {
if (name.equals(TYPE_KEY) && node.getChild().getText().equals(messageType.getFullName())) {
return true;
}
FieldDescriptor childField = messageType.findFieldByName(name);
if (childField == null) {
messageBuilder = null;
return false;
}
Message.Builder childBuilder = childField.getType() == FieldDescriptor.Type.MESSAGE ? messageBuilder.newBuilderForField(childField) : null;
MessageGenerator messageGenerator = new MessageGenerator(childBuilder, childField);
if (childField.isRepeated() || childField.getType() != FieldDescriptor.Type.MESSAGE) {
messageGenerator.values = new ArrayList<>();
}
messageGenerator.visit(node.getChild());
Object value = messageGenerator.getValue();
if (value == null) {
messageBuilder = null;
return false;
}
if (childField.isRepeated() || childField.getType() == FieldDescriptor.Type.MESSAGE) {
messageBuilder.setField(childField, value);
} else {
List<?> childValues = (List<?>) value;
if (childValues.size() != 1) {
messageBuilder = null;
return false;
}
messageBuilder.setField(childField, childValues.get(0));
}
}
return true;
}
use of com.google.protobuf.Descriptors.Descriptor in project tesla by linking12.
the class DynamicGrpcClient method doRemoteCall.
@Override
public String doRemoteCall(final FilterRpcDO rpcDo, final String jsonInput) {
try {
final String serviceName = rpcDo.getServiceName();
final String methodName = rpcDo.getMethodName();
final String group = rpcDo.getServiceGroup();
final String version = rpcDo.getServiceVersion();
Pair<Descriptor, Descriptor> inOutType = ProtobufUtil.resolveServiceInputOutputType(rpcDo);
Descriptor inPutType = inOutType.getLeft();
Descriptor outPutType = inOutType.getRight();
MethodDescriptor<DynamicMessage, DynamicMessage> methodDesc = this.createGrpcMethodDescriptor(serviceName, methodName, inPutType, outPutType);
DynamicMessage message = this.createGrpcDynamicMessage(inPutType, jsonInput);
Message response = (Message) genricService.$invoke(serviceName, group, version, methodName, methodDesc, message);
return JSON2PROTOBUF.printToString(response);
} catch (IOException e) {
throw new RpcServiceException(String.format("json covert to DynamicMessage failed! the json is :%s, the protobuf type is: %s", jsonInput), e);
} catch (Throwable e) {
throw new RpcFrameworkException(String.format("service definition is wrong,please check the proto file you update,service is %s, method is %s", rpcDo.getServiceName(), rpcDo.getMethodName()), e);
}
}
Aggregations