use of com.google.protobuf.Descriptors.FieldDescriptor in project core-java by SpineEventEngine.
the class MessageValidator method validateFields.
private void validateFields(Message message, ImmutableList.Builder<ConstraintViolation> result) {
final Descriptor msgDescriptor = message.getDescriptorForType();
final List<FieldDescriptor> fields = msgDescriptor.getFields();
for (FieldDescriptor field : fields) {
final Object value = message.getField(field);
final FieldValidator<?> fieldValidator = FieldValidatorFactory.create(field, value, rootFieldPath);
final List<ConstraintViolation> violations = fieldValidator.validate();
result.addAll(violations);
}
}
use of com.google.protobuf.Descriptors.FieldDescriptor in project core-java by SpineEventEngine.
the class ReferenceValidatorShould method skip_mapping_if_no_mapping_function_is_defined.
@Test
public void skip_mapping_if_no_mapping_function_is_defined() {
final Enricher<?, ?> mockEnricher = mock(Enricher.class);
when(mockEnricher.functionFor(any(Class.class), any(Class.class))).thenReturn(Optional.<EnrichmentFunction<?, ?, ?>>absent());
final ReferenceValidator validator = new ReferenceValidator(mockEnricher, UserDeletedEvent.class, EnrichmentBoundWithMultipleFieldsWithDifferentNames.class);
final ValidationResult result = validator.validate();
final List<EnrichmentFunction<?, ?, ?>> functions = result.getFunctions();
assertTrue(functions.isEmpty());
final Multimap<FieldDescriptor, FieldDescriptor> fields = result.getFieldMap();
assertEmpty(fields);
}
use of com.google.protobuf.Descriptors.FieldDescriptor in project core-java by SpineEventEngine.
the class ReferenceValidatorShould method store_valid_map_of_enrichment_fields_after_validation.
@Test
public void store_valid_map_of_enrichment_fields_after_validation() {
final ReferenceValidator validator = new ReferenceValidator(enricher, UserDeletedEvent.class, EnrichmentBoundWithMultipleFieldsWithDifferentNames.class);
final ValidationResult result = validator.validate();
final Multimap<FieldDescriptor, FieldDescriptor> fieldMap = result.getFieldMap();
assertNotNull(fieldMap);
assertFalse(fieldMap.isEmpty());
assertSize(1, fieldMap);
final Iterator<? extends Map.Entry<?, ? extends Collection<?>>> fieldsIterator = fieldMap.asMap().entrySet().iterator();
assertTrue(fieldsIterator.hasNext());
final Map.Entry<?, ? extends Collection<?>> entry = fieldsIterator.next();
@SuppressWarnings("unchecked") final Map.Entry<FieldDescriptor, Collection<FieldDescriptor>> fieldEntry = (Map.Entry<FieldDescriptor, Collection<FieldDescriptor>>) entry;
final FieldDescriptor eventField = fieldEntry.getKey();
final String eventFieldName = eventField.getName();
assertEquals("deleted_uid", eventFieldName);
final Collection<FieldDescriptor> enrichmentFields = fieldEntry.getValue();
assertFalse(enrichmentFields.isEmpty());
assertSize(1, enrichmentFields);
final Iterator<FieldDescriptor> enrichmentFieldIterator = enrichmentFields.iterator();
assertTrue(enrichmentFieldIterator.hasNext());
final FieldDescriptor enrichmentField = enrichmentFieldIterator.next();
final String enrichmentFieldName = enrichmentField.getName();
assertEquals(USER_GOOGLE_UID_FIELD, enrichmentFieldName);
}
use of com.google.protobuf.Descriptors.FieldDescriptor in project core-java by SpineEventEngine.
the class Field method getFieldClass.
/**
* Returns the class of the Protobuf message field.
*
* @param field the field descriptor
* @return the class of the field
* @throws IllegalArgumentException if the field type is unknown
*/
// as each branch is a fairly simple.
@SuppressWarnings("OverlyComplexMethod")
public static Class<?> getFieldClass(FieldDescriptor field) {
checkNotNull(field);
final FieldDescriptor.JavaType javaType = field.getJavaType();
final TypeUrl typeUrl;
switch(javaType) {
case INT:
return Integer.class;
case LONG:
return Long.class;
case FLOAT:
return Float.class;
case DOUBLE:
return Double.class;
case BOOLEAN:
return Boolean.class;
case STRING:
return String.class;
case BYTE_STRING:
return ByteString.class;
case ENUM:
typeUrl = TypeUrl.from(field.getEnumType());
final Class<? extends Message> enumClass = typeUrl.getJavaClass();
return enumClass;
case MESSAGE:
typeUrl = TypeUrl.from(field.getMessageType());
final Class<? extends Message> msgClass = typeUrl.getJavaClass();
return msgClass;
default:
throw newIllegalArgumentException("Unknown field type discovered: %s", field.getFullName());
}
}
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