Search in sources :

Example 21 with FieldDescriptor

use of com.google.protobuf.Descriptors.FieldDescriptor in project core-java by SpineEventEngine.

the class AlternativeFieldValidator method validate.

List<? extends ConstraintViolation> validate(Message message) {
    final Map<FieldDescriptor, Object> options = messageDescriptor.getOptions().getAllFields();
    for (FieldDescriptor optionDescriptor : options.keySet()) {
        if (OPTION_REQUIRED_FIELD.equals(optionDescriptor.getName())) {
            final JavaType optionType = optionDescriptor.getJavaType();
            if (optionType == JavaType.STRING) {
                final String requiredFieldExpression = (String) options.get(optionDescriptor);
                final ImmutableList<RequiredFieldOption> fieldOptions = parse(requiredFieldExpression);
                if (!alternativeFound(message, fieldOptions)) {
                    final String msgFormat = "None of the fields match the `required_field` definition: %s";
                    ConstraintViolation requiredFieldNotFound = ConstraintViolation.newBuilder().setMsgFormat(msgFormat).addParam(requiredFieldExpression).build();
                    violations.add(requiredFieldNotFound);
                }
            } else {
                log().warn("`{}` is not of string type. Found: {}", OPTION_REQUIRED_FIELD, optionType);
            }
        }
    }
    return violations.build();
}
Also used : JavaType(com.google.protobuf.Descriptors.FieldDescriptor.JavaType) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor)

Example 22 with FieldDescriptor

use of com.google.protobuf.Descriptors.FieldDescriptor in project google-cloud-java by GoogleCloudPlatform.

the class AnnotateImage method main.

public static void main(String... args) throws Exception {
    // Instantiates a client
    ImageAnnotatorClient vision = ImageAnnotatorClient.create();
    // The path to the image file to annotate
    // for example "./resources/wakeupcat.jpg";
    String fileName = "your/image/path.jpg";
    // Reads the image file into memory
    Path path = Paths.get(fileName);
    byte[] data = Files.readAllBytes(path);
    ByteString imgBytes = ByteString.copyFrom(data);
    // Builds the image annotation request
    List<AnnotateImageRequest> requests = new ArrayList<>();
    Image img = Image.newBuilder().setContent(imgBytes).build();
    Feature feat = Feature.newBuilder().setType(Type.LABEL_DETECTION).build();
    AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
    requests.add(request);
    // Performs label detection on the image file
    BatchAnnotateImagesResponse response = vision.batchAnnotateImages(requests);
    List<AnnotateImageResponse> responses = response.getResponsesList();
    for (AnnotateImageResponse res : responses) {
        if (res.hasError()) {
            System.out.printf("Error: %s\n", res.getError().getMessage());
            return;
        }
        for (EntityAnnotation annotation : res.getLabelAnnotationsList()) {
            for (Map.Entry<FieldDescriptor, Object> entry : annotation.getAllFields().entrySet()) {
                System.out.printf("%s : %s\n", entry.getKey(), entry.getValue());
            }
        }
    }
}
Also used : Path(java.nio.file.Path) ByteString(com.google.protobuf.ByteString) ImageAnnotatorClient(com.google.cloud.vision.spi.v1.ImageAnnotatorClient) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) Image(com.google.cloud.vision.v1.Image) Feature(com.google.cloud.vision.v1.Feature) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) EntityAnnotation(com.google.cloud.vision.v1.EntityAnnotation) Map(java.util.Map) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse)

Example 23 with FieldDescriptor

use of com.google.protobuf.Descriptors.FieldDescriptor in project toolkit by googleapis.

the class ConfigYamlNodeReader method readMapNodeEntry.

private ConfigNode readMapNodeEntry(NodeTuple entry, Descriptor messageType) {
    Node keyNode = entry.getKeyNode();
    if (!(keyNode instanceof ScalarNode)) {
        helper.error(keyNode, "Expected a scalar value for key in '%s', found '%s'.", messageType.getFullName(), keyNode.getNodeId());
        return null;
    }
    String key = ((ScalarNode) keyNode).getValue();
    FieldDescriptor valueField = messageType.findFieldByName("value");
    ConfigNode valueConfigNode = readField(keyNode.getEndMark().getLine() + 1, entry.getValueNode(), valueField);
    if (valueConfigNode == null) {
        return null;
    }
    return new FieldConfigNode(helper.getStartLine(keyNode), key).setChild(valueConfigNode);
}
Also used : FieldConfigNode(com.google.api.codegen.configgen.nodes.FieldConfigNode) ScalarNode(org.yaml.snakeyaml.nodes.ScalarNode) SequenceNode(org.yaml.snakeyaml.nodes.SequenceNode) MappingNode(org.yaml.snakeyaml.nodes.MappingNode) ConfigNode(com.google.api.codegen.configgen.nodes.ConfigNode) Node(org.yaml.snakeyaml.nodes.Node) 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) ScalarNode(org.yaml.snakeyaml.nodes.ScalarNode) 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) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor)

Example 24 with FieldDescriptor

use of com.google.protobuf.Descriptors.FieldDescriptor in project toolkit by googleapis.

the class ConfigYamlNodeReader method readMessageNodeEntry.

private ConfigNode readMessageNodeEntry(NodeTuple entry, Descriptor messageType) {
    Node keyNode = entry.getKeyNode();
    if (!(keyNode instanceof ScalarNode)) {
        helper.error(keyNode, "Expected a scalar value for key in '%s', found '%s'.", messageType.getFullName(), keyNode.getNodeId());
        return null;
    }
    String key = ((ScalarNode) keyNode).getValue();
    FieldDescriptor field = messageType.findFieldByName(key);
    ConfigNode valueConfigNode = null;
    if (field != null) {
        valueConfigNode = readField(keyNode.getEndMark().getLine() + 1, entry.getValueNode(), field);
    } else if (key.equals(TYPE_KEY)) {
        valueConfigNode = readScalarNode(entry.getValueNode(), field);
    } else {
        helper.error(keyNode, "Found field '%s' which is unknown in '%s'.", key, messageType.getFullName());
    }
    if (valueConfigNode == null) {
        return null;
    }
    return new FieldConfigNode(helper.getStartLine(keyNode), key).setChild(valueConfigNode);
}
Also used : FieldConfigNode(com.google.api.codegen.configgen.nodes.FieldConfigNode) ScalarNode(org.yaml.snakeyaml.nodes.ScalarNode) SequenceNode(org.yaml.snakeyaml.nodes.SequenceNode) MappingNode(org.yaml.snakeyaml.nodes.MappingNode) ConfigNode(com.google.api.codegen.configgen.nodes.ConfigNode) Node(org.yaml.snakeyaml.nodes.Node) 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) ScalarNode(org.yaml.snakeyaml.nodes.ScalarNode) 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) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor)

Example 25 with FieldDescriptor

use of com.google.protobuf.Descriptors.FieldDescriptor in project core-java by SpineEventEngine.

the class MessageEnrichment method setFields.

@SuppressWarnings({ "ConstantConditions", /* it is assured that collections are not null, and
                                    after validation maps have required entries. */
"MethodWithMultipleLoops" })
private void setFields(Message.Builder builder, S sourceMessage, C context) {
    for (FieldDescriptor srcField : fieldMap.keySet()) {
        final Object srcFieldValue = getSrcFieldValue(srcField, sourceMessage, context);
        final Class<?> sourceFieldClass = srcFieldValue.getClass();
        final Collection<EnrichmentFunction<?, ?, ?>> functions = fieldFunctions.get(sourceFieldClass);
        final Collection<FieldDescriptor> targetFields = fieldMap.get(srcField);
        for (FieldDescriptor targetField : targetFields) {
            final Optional<EnrichmentFunction<?, ?, ?>> function = firstThat(functions, SupportsFieldConversion.of(sourceFieldClass, Field.getFieldClass(targetField)));
            final EnrichmentFunction fieldEnrichment = function.get();
            @SuppressWarnings("unchecked") final Object targetValue = fieldEnrichment.apply(srcFieldValue, context);
            if (targetValue != null) {
                builder.setField(targetField, targetValue);
            }
        }
    }
}
Also used : FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor)

Aggregations

FieldDescriptor (com.google.protobuf.Descriptors.FieldDescriptor)58 Descriptor (com.google.protobuf.Descriptors.Descriptor)22 Test (org.junit.Test)17 EnumDescriptor (com.google.protobuf.Descriptors.EnumDescriptor)11 EnumValueDescriptor (com.google.protobuf.Descriptors.EnumValueDescriptor)11 BooleanFieldValidator (io.spine.validate.BooleanFieldValidator)9 ByteStringFieldValidator (io.spine.validate.ByteStringFieldValidator)9 FloatFieldValidator (io.spine.validate.FloatFieldValidator)9 LongFieldValidator (io.spine.validate.LongFieldValidator)9 StringFieldValidator (io.spine.validate.StringFieldValidator)9 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 TypicalData (protos.TypicalData)5 Collection (java.util.Collection)4 ConfigNode (com.google.api.codegen.configgen.nodes.ConfigNode)3 FieldConfigNode (com.google.api.codegen.configgen.nodes.FieldConfigNode)3 ListItemConfigNode (com.google.api.codegen.configgen.nodes.ListItemConfigNode)3 NullConfigNode (com.google.api.codegen.configgen.nodes.NullConfigNode)3 ScalarConfigNode (com.google.api.codegen.configgen.nodes.ScalarConfigNode)3 ByteString (com.google.protobuf.ByteString)3