if (typeToUse.isMap()) { return (S) readMap(typeToUse, bson, path);
} else { Document document = new Document(); TypeInformation<?> valueTypeInfo = propertyType.isMap() ? propertyType.getMapValueType() : ClassTypeInformation.OBJECT; writeInternal(val, document, valueTypeInfo);
if (valueType.isMap()) { Bson mapDbObj = createMap((Map<Object, Object>) obj, prop); accessor.put(prop, mapDbObj);
if (typeToUse.isMap()) { return (S) readMap(typeToUse, bson, path);
} else { Document document = new Document(); TypeInformation<?> valueTypeInfo = propertyType.isMap() ? propertyType.getMapValueType() : ClassTypeInformation.OBJECT; writeInternal(val, document, valueTypeInfo);
if (valueType.isMap()) { Bson mapDbObj = createMap((Map<Object, Object>) obj, prop); accessor.put(prop, mapDbObj);
@Nullable private DataType getUserDataType(TypeInformation<?> property, @Nullable DataType elementType) { if (property.isCollectionLike()) { if (List.class.isAssignableFrom(property.getType())) { return DataType.list(elementType); } if (Set.class.isAssignableFrom(property.getType())) { return DataType.set(elementType); } } return !(property.isCollectionLike() || property.isMap()) ? elementType : null; }
/** * Get the {@link EmbeddedType} of a given {@link TypeInformation}. * @param typeInformation the given type metadata to check for embedded type. * @return the embedded type. */ public static EmbeddedType of(TypeInformation typeInformation) { EmbeddedType embeddedType; if (typeInformation.isMap()) { embeddedType = EmbeddedType.EMBEDDED_MAP; } else if ((typeInformation.isCollectionLike() && typeInformation.getComponentType().getType().isAnnotationPresent( org.springframework.cloud.gcp.data.datastore.core.mapping.Entity.class)) || typeInformation.getType().isAnnotationPresent( org.springframework.cloud.gcp.data.datastore.core.mapping.Entity.class)) { embeddedType = EmbeddedType.EMBEDDED_ENTITY; } else { embeddedType = EmbeddedType.NOT_EMBEDDED; } return embeddedType; }
/** * Get the {@link EmbeddedType} of a given {@link TypeInformation}. * @param typeInformation the given type metadata to check for embedded type. * @return the embedded type. */ public static EmbeddedType of(TypeInformation typeInformation) { EmbeddedType embeddedType; if (typeInformation.isMap()) { embeddedType = EmbeddedType.EMBEDDED_MAP; } else if ((typeInformation.isCollectionLike() && typeInformation.getComponentType().getType().isAnnotationPresent( org.springframework.cloud.gcp.data.datastore.core.mapping.Entity.class)) || typeInformation.getType().isAnnotationPresent( org.springframework.cloud.gcp.data.datastore.core.mapping.Entity.class)) { embeddedType = EmbeddedType.EMBEDDED_ENTITY; } else { embeddedType = EmbeddedType.NOT_EMBEDDED; } return embeddedType; }
private Object getNonSimpleValueToWrite(Object value, TypeInformation<?> type) { TypeInformation<?> valueType = ClassTypeInformation.from(value.getClass()); if (valueType.isCollectionLike()) { return convertCollection(asCollection(value), type); } if (valueType.isMap()) { return convertMap(asMap(value), type); } Class<?> basicTargetType = conversions.getCustomWriteTarget(value.getClass()); if (basicTargetType != null) { return conversionService.convert(value, basicTargetType); } return convertCustomType(value, valueType); }
} else { CouchbaseDocument embeddedDoc = new CouchbaseDocument(); TypeInformation<?> valueTypeInfo = type.isMap() ? type.getMapValueType() : ClassTypeInformation.OBJECT; writeInternal(val, embeddedDoc, valueTypeInfo); target.put(simpleKey, embeddedDoc);
private <T> T readValue(Object source, TypeInformation<?> propertyType) { Assert.notNull(propertyType, "Target type must not be null!"); if (source == null) { return null; } Class<?> targetClass = propertyType.getType(); if (conversions.hasCustomReadTarget(source.getClass(), targetClass)) { return (T) conversionService.convert(source, targetClass); } else if (propertyType.isCollectionLike()) { return convertCollection((Collection) source, propertyType); } else if (propertyType.isMap()) { return (T) convertMap((Map<String, Object>) source, propertyType); } else if (source instanceof Map) { // custom type return convertCustomType((Map<String, Object>) source, propertyType); } return (T) convertIfNeeded(source, targetClass); }
public AbstractPersistentProperty(Property property, PersistentEntity<?, P> owner, SimpleTypeHolder simpleTypeHolder) { Assert.notNull(simpleTypeHolder, "SimpleTypeHolder must not be null!"); Assert.notNull(owner, "Owner entity must not be null!"); this.name = property.getName(); this.information = owner.getTypeInformation().getRequiredProperty(getName()); this.rawType = this.information.getType(); this.property = property; this.association = Lazy.of(() -> isAssociation() ? createAssociation() : null); this.owner = owner; this.hashCode = Lazy.of(property::hashCode); this.usePropertyAccess = Lazy.of(() -> owner.getType().isInterface() || CAUSE_FIELD.equals(getField())); this.entityTypeInformation = Lazy.of(() -> Optional.ofNullable(information.getActualType())// .filter(it -> !simpleTypeHolder.isSimpleType(it.getType()))// .filter(it -> !it.isCollectionLike())// .filter(it -> !it.isMap())); this.getter = property.getGetter().orElse(null); this.setter = property.getSetter().orElse(null); this.field = property.getField().orElse(null); this.wither = property.getWither().orElse(null); if (setter == null && (field == null || Modifier.isFinal(field.getModifiers()))) { this.immutable = true; } else { this.immutable = false; } }
@Nullable @Override public Object invoke(@SuppressWarnings("null") @Nonnull MethodInvocation invocation) throws Throwable { Object result = delegate.invoke(invocation); if (result == null) { return null; } TypeInformation<?> type = ClassTypeInformation.fromReturnTypeOf(invocation.getMethod()); Class<?> rawType = type.getType(); if (type.isCollectionLike() && !ClassUtils.isPrimitiveArray(rawType)) { return projectCollectionElements(asCollection(result), type); } else if (type.isMap()) { return projectMapValues((Map<?, ?>) result, type); } else if (conversionRequiredAndPossible(result, rawType)) { return conversionService.convert(result, rawType); } else { return getProjection(result, rawType); } }
/** * Read an incoming {@link CouchbaseDocument} into the target entity. * * @param type the type information of the target entity. * @param source the document to convert. * @param parent an optional parent object. * @param <R> the entity type. * @return the converted entity. */ @SuppressWarnings("unchecked") protected <R> R read(final TypeInformation<R> type, final CouchbaseDocument source, final Object parent) { if (source == null) { return null; } TypeInformation<? extends R> typeToUse = typeMapper.readType(source, type); Class<? extends R> rawType = typeToUse.getType(); if (conversions.hasCustomReadTarget(source.getClass(), rawType)) { return conversionService.convert(source, rawType); } if (typeToUse.isMap()) { return (R) readMap(typeToUse, source, parent); } CouchbasePersistentEntity<R> entity = (CouchbasePersistentEntity<R>) mappingContext.getRequiredPersistentEntity(typeToUse); return read(entity, source, parent); }
@SuppressWarnings("unchecked") private <S> S read(TypeInformation<S> type, Object source) { SecretDocument secretDocument = getSecretDocument(source); TypeInformation<? extends S> typeToUse = secretDocument != null ? typeMapper .readType(secretDocument.getBody(), type) : (TypeInformation) ClassTypeInformation.OBJECT; Class<? extends S> rawType = typeToUse.getType(); if (conversions.hasCustomReadTarget(source.getClass(), rawType)) { return conversionService.convert(source, rawType); } if (SecretDocument.class.isAssignableFrom(rawType)) { return (S) source; } if (Map.class.isAssignableFrom(rawType) && secretDocument != null) { return (S) secretDocument.getBody(); } if (typeToUse.isMap() && secretDocument != null) { return (S) readMap(typeToUse, secretDocument.getBody()); } if (typeToUse.equals(ClassTypeInformation.OBJECT)) { return (S) source; } return read( (VaultPersistentEntity<S>) mappingContext .getRequiredPersistentEntity(typeToUse), secretDocument); }
private static boolean hasAnnotation(AnnotatedType type, Class<? extends Annotation> annotationType, TypeInformation<?> typeInformation) { if (AnnotatedElementUtils.hasAnnotation(type, annotationType)) { return true; } if (type instanceof AnnotatedParameterizedType) { AnnotatedParameterizedType parameterizedType = (AnnotatedParameterizedType) type; AnnotatedType[] arguments = parameterizedType.getAnnotatedActualTypeArguments(); if (typeInformation.isCollectionLike() && arguments.length == 1) { return AnnotatedElementUtils.hasAnnotation(arguments[0], annotationType); } if (typeInformation.isMap() && arguments.length == 2) { return AnnotatedElementUtils.hasAnnotation(arguments[0], annotationType) || AnnotatedElementUtils.hasAnnotation(arguments[1], annotationType); } } return false; } }
/** * Read an incoming {@link CouchbaseDocument} into the target entity. * * @param type the type information of the target entity. * @param source the document to convert. * @param parent an optional parent object. * @param <R> the entity type. * @return the converted entity. */ @SuppressWarnings("unchecked") protected <R> R read(final TypeInformation<R> type, final CouchbaseDocument source, final Object parent) { if (source == null) { return null; } TypeInformation<? extends R> typeToUse = typeMapper.readType(source, type); Class<? extends R> rawType = typeToUse.getType(); if (conversions.hasCustomReadTarget(source.getClass(), rawType)) { return conversionService.convert(source, rawType); } if (typeToUse.isMap()) { return (R) readMap(typeToUse, source, parent); } CouchbasePersistentEntity<R> entity = (CouchbasePersistentEntity<R>) mappingContext.getRequiredPersistentEntity(typeToUse); return read(entity, source, parent); }
@Nullable @SuppressWarnings("unchecked") private Object convertReadValue(Object value, TypeInformation<?> typeInformation) { if (typeInformation.isCollectionLike() && value instanceof Collection) { return readCollectionOrArrayInternal((Collection<?>) value, typeInformation); } if (typeInformation.isMap() && value instanceof Map) { return readMapInternal((Map<Object, Object>) value, typeInformation); } if (value instanceof TupleValue) { BasicCassandraPersistentEntity<?> tupleEntity = getMappingContext() .getPersistentEntity(typeInformation.getRequiredActualType()); if (tupleEntity != null) { return readEntityFromTuple(tupleEntity, (TupleValue) value); } } if (value instanceof UDTValue) { BasicCassandraPersistentEntity<?> udtEntity = getMappingContext() .getPersistentEntity(typeInformation.getRequiredActualType()); if (udtEntity != null && udtEntity.isUserDefinedType()) { return readEntityFromUdt(udtEntity, (UDTValue) value); } } return getPotentiallyConvertedSimpleRead(value, typeInformation.getType()); }
/** * Read a {@link Row} into the requested target {@link Class type}. * * @param type must not be {@literal null}. * @param row must not be {@literal null}. * @return the converted valued. */ @SuppressWarnings("unchecked") public <R> R readRow(Class<R> type, Row row) { Class<R> beanClassLoaderClass = transformClassToBeanClassLoaderClass(type); TypeInformation<? extends R> typeInfo = ClassTypeInformation.from(beanClassLoaderClass); Class<? extends R> rawType = typeInfo.getType(); if (Row.class.isAssignableFrom(rawType)) { return (R) row; } if (getCustomConversions().hasCustomReadTarget(Row.class, rawType) || getConversionService().canConvert(Row.class, rawType)) { return getConversionService().convert(row, rawType); } if (typeInfo.isCollectionLike() || typeInfo.isMap()) { return getConversionService().convert(row, type); } CassandraPersistentEntity<R> persistentEntity = (CassandraPersistentEntity<R>) getMappingContext() .getRequiredPersistentEntity(typeInfo); return readEntityFromRow(persistentEntity, row); }