Search in sources :

Example 26 with Descriptor

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;
}
Also used : Descriptor(com.google.protobuf.Descriptors.Descriptor) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) HashSet(java.util.HashSet)

Example 27 with Descriptor

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;
}
Also used : Descriptor(com.google.protobuf.Descriptors.Descriptor) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor)

Example 28 with Descriptor

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;
}
Also used : MappingNode(org.yaml.snakeyaml.nodes.MappingNode) Descriptor(com.google.protobuf.Descriptors.Descriptor) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) NullConfigNode(com.google.api.codegen.configgen.nodes.NullConfigNode) ConfigNode(com.google.api.codegen.configgen.nodes.ConfigNode) ListItemConfigNode(com.google.api.codegen.configgen.nodes.ListItemConfigNode) ScalarConfigNode(com.google.api.codegen.configgen.nodes.ScalarConfigNode) FieldConfigNode(com.google.api.codegen.configgen.nodes.FieldConfigNode) NullConfigNode(com.google.api.codegen.configgen.nodes.NullConfigNode) NodeTuple(org.yaml.snakeyaml.nodes.NodeTuple)

Example 29 with Descriptor

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;
}
Also used : Message(com.google.protobuf.Message) Descriptor(com.google.protobuf.Descriptors.Descriptor) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) List(java.util.List) ArrayList(java.util.ArrayList) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) VisitsBefore(com.google.api.tools.framework.util.VisitsBefore)

Example 30 with Descriptor

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);
    }
}
Also used : RpcFrameworkException(com.quancheng.saluki.core.grpc.exception.RpcFrameworkException) DynamicMessage(com.google.protobuf.DynamicMessage) Message(com.google.protobuf.Message) Descriptor(com.google.protobuf.Descriptors.Descriptor) MethodDescriptor(io.grpc.MethodDescriptor) RpcServiceException(com.quancheng.saluki.core.grpc.exception.RpcServiceException) DynamicMessage(com.google.protobuf.DynamicMessage) IOException(java.io.IOException)

Aggregations

Descriptor (com.google.protobuf.Descriptors.Descriptor)30 FieldDescriptor (com.google.protobuf.Descriptors.FieldDescriptor)26 EnumDescriptor (com.google.protobuf.Descriptors.EnumDescriptor)13 EnumValueDescriptor (com.google.protobuf.Descriptors.EnumValueDescriptor)13 ExtensionRegistry (com.google.protobuf.ExtensionRegistry)4 ArrayList (java.util.ArrayList)4 TypicalData (protos.TypicalData)4 Message (com.google.protobuf.Message)3 HashSet (java.util.HashSet)3 TypicalDataMessage (protos.TypicalDataMessage)3 GenericDescriptor (com.google.protobuf.Descriptors.GenericDescriptor)2 GeneratedMessage (com.google.protobuf.GeneratedMessage)2 ConfigNode (com.google.api.codegen.configgen.nodes.ConfigNode)1 FieldConfigNode (com.google.api.codegen.configgen.nodes.FieldConfigNode)1 ListItemConfigNode (com.google.api.codegen.configgen.nodes.ListItemConfigNode)1 NullConfigNode (com.google.api.codegen.configgen.nodes.NullConfigNode)1 ScalarConfigNode (com.google.api.codegen.configgen.nodes.ScalarConfigNode)1 VisitsBefore (com.google.api.tools.framework.util.VisitsBefore)1 ByteString (com.google.protobuf.ByteString)1 Descriptors (com.google.protobuf.Descriptors)1