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();
}
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());
}
}
}
}
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);
}
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);
}
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);
}
}
}
}
Aggregations