use of com.google.protobuf.Descriptors.FieldDescriptor in project core-java by SpineEventEngine.
the class MessageField method getAccessor.
private Method getAccessor(Message message) {
final Class<? extends Message> messageClass = message.getClass();
Method method = accessors.get(messageClass);
if (method == null) {
final FieldDescriptor fieldDescriptor = getFieldDescriptor(message, this.index);
final String fieldName = fieldDescriptor.getName();
final String methodName = toAccessorMethodName(fieldName);
try {
method = messageClass.getMethod(methodName);
method.setAccessible(true);
accessors.put(messageClass, method);
} catch (NoSuchMethodException e) {
throw new IllegalStateException(e);
}
}
return method;
}
use of com.google.protobuf.Descriptors.FieldDescriptor in project core-java by SpineEventEngine.
the class AlternativeFieldValidator method checkField.
private boolean checkField(Message message, String fieldName) {
final FieldDescriptor field = messageDescriptor.findFieldByName(fieldName);
if (field == null) {
ConstraintViolation notFound = ConstraintViolation.newBuilder().setMsgFormat("Field %s not found").addParam(fieldName).build();
violations.add(notFound);
return false;
}
Object fieldValue = message.getField(field);
final FieldValidator<?> fieldValidator = FieldValidatorFactory.createStrict(field, fieldValue, rootFieldPath);
final List<ConstraintViolation> violations = fieldValidator.validate();
return violations.isEmpty();
}
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 core-java by SpineEventEngine.
the class MessageFieldShould method return_field_descriptor.
@Test
public void return_field_descriptor() {
final FieldDescriptor descriptor = getFieldDescriptor(stringValue, STR_VALUE_FIELD_INDEX);
assertEquals(JavaType.STRING, descriptor.getJavaType());
}
use of com.google.protobuf.Descriptors.FieldDescriptor 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;
}
Aggregations