Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
diegomarquezp committed Dec 13, 2024
1 parent 195fca8 commit 290c7a2
Show file tree
Hide file tree
Showing 72 changed files with 1,366 additions and 1,191 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
hints
.reflection()
.registerTypes(
Arrays.asList(
TypeReference.of(java.util.HashMap.class)),
Arrays.asList(TypeReference.of(java.util.HashMap.class)),
hint ->
hint.withMembers(
MemberCategory.INVOKE_DECLARED_CONSTRUCTORS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
hints
.reflection()
.registerTypes(
Arrays.asList(
TypeReference.of(DatastorePageable.class)),
Arrays.asList(TypeReference.of(DatastorePageable.class)),
hint ->
hint.withMembers(
MemberCategory.INVOKE_DECLARED_CONSTRUCTORS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ public interface DatastoreOperations {
<T> Iterable<T> saveAll(Iterable<T> entities, Key... ancestors);

/**
* Inserts an instance of an object to Cloud Datastore. Throws a DatastoreException if an entry with same ID
* already exists. Ancestors can be added only to entries with Key ids.
* Inserts an instance of an object to Cloud Datastore. Throws a DatastoreException if an entry
* with same ID already exists. Ancestors can be added only to entries with Key ids.
*
* @param instance the instance to save.
* @param ancestors ancestors that should be added to the entry
Expand All @@ -85,8 +85,8 @@ public interface DatastoreOperations {
<T> T insert(T instance, Key... ancestors);

/**
* Saves multiple instances of objects to Cloud Datastore. Throws a DatastoreException if any entry with one of
* the IDs already exists. Ancestors can be added only to entries with Key ids.
* Saves multiple instances of objects to Cloud Datastore. Throws a DatastoreException if any
* entry with one of the IDs already exists. Ancestors can be added only to entries with Key ids.
*
* @param entities the objects to save.
* @param ancestors ancestors that should be added to each entry
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ public <T> Iterable<T> insertAll(final Iterable<T> entities, final Key... ancest
return entities;
}

private <T> void insertOrSaveEntities(Iterable<T> iterable, Key[] ancestors, Consumer<FullEntity<?>[]> consumer) {
private <T> void insertOrSaveEntities(
Iterable<T> iterable, Key[] ancestors, Consumer<FullEntity<?>[]> consumer) {
List<T> instances;
if (iterable instanceof List) {
instances = (List<T>) iterable;
Expand Down Expand Up @@ -255,16 +256,17 @@ private void performDelete(Key[] keys, Iterable ids, Iterable entities, Class en

@Override
public long count(Class<?> entityClass) {
KeyQuery baseQuery = Query.newKeyQueryBuilder()
.setKind(getPersistentEntity(entityClass).kindName())
.build();
KeyQuery baseQuery =
Query.newKeyQueryBuilder().setKind(getPersistentEntity(entityClass).kindName()).build();

AggregationQuery countAggregationQuery = Query.newAggregationQueryBuilder()
.over(baseQuery)
.addAggregation(Aggregation.count().as("total_count"))
.build();
AggregationQuery countAggregationQuery =
Query.newAggregationQueryBuilder()
.over(baseQuery)
.addAggregation(Aggregation.count().as("total_count"))
.build();

AggregationResults aggregationResults = getDatastoreReadWriter().runAggregation(countAggregationQuery);
AggregationResults aggregationResults =
getDatastoreReadWriter().runAggregation(countAggregationQuery);
maybeEmitEvent(new AfterQueryEvent(aggregationResults, countAggregationQuery));
AggregationResult aggregationResult = Iterables.getOnlyElement(aggregationResults);
return aggregationResult.get("total_count");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,7 @@ public void write(Object source, @NonNull BaseEntity.Builder sink) {
List<String> discriminationValues = persistentEntity.getCompatibleDiscriminationValues();
if (!discriminationValues.isEmpty() || discriminationFieldName != null) {
sink.set(
discriminationFieldName,
discriminationValues.stream().map(StringValue::of).toList());
discriminationFieldName, discriminationValues.stream().map(StringValue::of).toList());
}
PersistentPropertyAccessor accessor = persistentEntity.getPropertyAccessor(source);
persistentEntity.doWithColumnBackedProperties(
Expand Down Expand Up @@ -269,8 +268,7 @@ private Value setExcludeFromIndexes(Value convertedVal) {
} else if (convertedVal.getClass().equals(ListValue.class)) {
return ListValue.of(
(List)
((ListValue) convertedVal)
.get().stream().map(this::setExcludeFromIndexes).toList());
((ListValue) convertedVal).get().stream().map(this::setExcludeFromIndexes).toList());
} else {
return convertedVal.toBuilder().setExcludeFromIndexes(true).build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,18 @@ private <T> T convertOnRead(
if (val == null) {
return null;
}
BiFunction<Object, TypeInformation<?>, ?> readConverter = switch (embeddedType) {
case EMBEDDED_MAP -> (x, typeInformation) ->
convertOnReadSingleEmbeddedMap(
x,
Objects.requireNonNull(typeInformation.getComponentType()).getType(),
typeInformation.getMapValueType(),
targetComponentType);
case EMBEDDED_ENTITY -> this::convertOnReadSingleEmbedded;
case NOT_EMBEDDED -> this::convertOnReadSingle;
};
BiFunction<Object, TypeInformation<?>, ?> readConverter =
switch (embeddedType) {
case EMBEDDED_MAP ->
(x, typeInformation) ->
convertOnReadSingleEmbeddedMap(
x,
Objects.requireNonNull(typeInformation.getComponentType()).getType(),
typeInformation.getMapValueType(),
targetComponentType);
case EMBEDDED_ENTITY -> this::convertOnReadSingleEmbedded;
case NOT_EMBEDDED -> this::convertOnReadSingle;
};

if (ValueUtil.isCollectionLike(val.getClass())
&& targetCollectionType != null
Expand Down Expand Up @@ -191,8 +193,7 @@ private <T> T convertOnReadSingleEmbedded(
Object value, TypeInformation<?> targetTypeInformation) {
Assert.notNull(value, "Cannot convert a null value.");
if (value instanceof BaseEntity<?> baseEntity) {
return (T)
this.datastoreEntityConverter.read(targetTypeInformation.getType(), baseEntity);
return (T) this.datastoreEntityConverter.read(targetTypeInformation.getType(), baseEntity);
}
throw new DatastoreDataException(
"Embedded entity was expected, but " + value.getClass() + " found");
Expand Down Expand Up @@ -261,12 +262,15 @@ private Value convertOnWrite(

Function<Object, Value> writeConverter = this::convertOnWriteSingle;
if (proppertyVal != null) {
writeConverter = switch (embeddedType) {
case EMBEDDED_MAP -> x -> convertOnWriteSingleEmbeddedMap(x, fieldName,
typeInformation.getMapValueType());
case EMBEDDED_ENTITY -> x -> convertOnWriteSingleEmbedded(x, fieldName);
case NOT_EMBEDDED -> this::convertOnWriteSingle;
};
writeConverter =
switch (embeddedType) {
case EMBEDDED_MAP ->
x ->
convertOnWriteSingleEmbeddedMap(
x, fieldName, typeInformation.getMapValueType());
case EMBEDDED_ENTITY -> x -> convertOnWriteSingleEmbedded(x, fieldName);
case NOT_EMBEDDED -> this::convertOnWriteSingle;
};
}

val = ValueUtil.toListIfArray(val);
Expand All @@ -289,16 +293,16 @@ private EntityValue applyEntityValueBuilder(
builder = FullEntity.newBuilder();
} else {
/* The following does 3 sequential null checks. We only want an ID value if the object isn't null,
has an ID property, and the ID property isn't null.
*/
has an ID property, and the ID property isn't null.
*/
Optional idProp =
Optional.ofNullable(val)
.map(v -> this.datastoreMappingContext.getPersistentEntity(v.getClass()))
.map(PersistentEntity::getIdProperty)
.map(
id ->
Objects.requireNonNull(this.datastoreMappingContext
.getPersistentEntity(val.getClass()))
Objects.requireNonNull(
this.datastoreMappingContext.getPersistentEntity(val.getClass()))
.getPropertyAccessor(val)
.getProperty(id));

Expand All @@ -321,8 +325,7 @@ private EntityValue convertOnWriteSingleEmbeddedMap(
for (Map.Entry<?, ?> e : ((Map<?, ?>) val).entrySet()) {
String field =
convertOnReadSingle(
convertOnWriteSingle(e.getKey()).get(),
TypeInformation.of(String.class));
convertOnWriteSingle(e.getKey()).get(), TypeInformation.of(String.class));
builder.set(
field,
convertOnWrite(
Expand Down Expand Up @@ -412,8 +415,7 @@ public void registerEntityConverter(DatastoreEntityConverter datastoreEntityConv
this.datastoreEntityConverter = datastoreEntityConverter;
}

private record TypeTargets(Class<?> firstStepTarget,
Class<?> secondStepTarget) {
private record TypeTargets(Class<?> firstStepTarget, Class<?> secondStepTarget) {

Class<?> getFirstStepTarget() {
return this.firstStepTarget;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import java.util.function.Consumer;

/**
* @since 1.2
* @since 1.2
*/
public final class SliceUtil {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected BeanDefinitionBuilder getAuditHandlerBeanDefinitionBuilder(
AuditingConfiguration configuration) {
return configureDefaultAuditHandlerAttributes(
configuration, BeanDefinitionBuilder.rootBeanDefinition(AuditingHandler.class))
.addConstructorArgValue(PersistentEntities.of(new DatastoreMappingContext()));
.addConstructorArgValue(PersistentEntities.of(new DatastoreMappingContext()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,8 @@
import org.springframework.data.repository.query.ParameterAccessor;
import org.springframework.data.repository.query.Parameters;
import org.springframework.data.repository.query.ParametersParameterAccessor;
import org.springframework.data.repository.query.SpelEvaluator;
import org.springframework.data.repository.query.SpelQueryContext;
import org.springframework.data.repository.query.ValueExpressionDelegate;
import org.springframework.data.repository.query.ValueExpressionQueryRewriter;
import org.springframework.expression.spel.standard.SpelCompiler;
import org.springframework.util.StringUtils;

/**
Expand All @@ -86,7 +83,8 @@ public class GqlDatastoreQuery<T> extends AbstractDatastoreQuery<T> {

private final ValueExpressionDelegate valueExpressionDelegate;

private ValueExpressionQueryRewriter.EvaluatingValueExpressionQueryRewriter valueExpressionQueryRewriter;
private ValueExpressionQueryRewriter.EvaluatingValueExpressionQueryRewriter
valueExpressionQueryRewriter;

/**
* Constructor.
Expand Down Expand Up @@ -316,16 +314,19 @@ private void setGqlResolvedEntityClassName() {

private void setEvaluatingSpelQueryContext() {
Set<String> originalTags = new HashSet<>(GqlDatastoreQuery.this.originalParamTags);
GqlDatastoreQuery.this.valueExpressionQueryRewriter = ValueExpressionQueryRewriter.of(valueExpressionDelegate,
(counter, spelExpression) -> {
String newTag;
do {
counter++;
newTag = "@SpELtag" + counter;
} while (originalTags.contains(newTag));
originalTags.add(newTag);
return newTag;
}, String::concat)
GqlDatastoreQuery.this.valueExpressionQueryRewriter =
ValueExpressionQueryRewriter.of(
valueExpressionDelegate,
(counter, spelExpression) -> {
String newTag;
do {
counter++;
newTag = "@SpELtag" + counter;
} while (originalTags.contains(newTag));
originalTags.add(newTag);
return newTag;
},
String::concat)
.withEvaluationContextAccessor(valueExpressionDelegate.getEvaluationContextAccessor());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,14 @@
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.expression.BeanFactoryAccessor;
import org.springframework.context.expression.BeanFactoryResolver;
import org.springframework.data.mapping.MappingException;
import org.springframework.data.repository.core.EntityInformation;
import org.springframework.data.repository.core.RepositoryInformation;
import org.springframework.data.repository.core.RepositoryMetadata;
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
import org.springframework.data.repository.query.Parameters;
import org.springframework.data.repository.query.QueryLookupStrategy;
import org.springframework.data.repository.query.QueryLookupStrategy.Key;
import org.springframework.data.repository.query.QueryMethodEvaluationContextProvider;
import org.springframework.data.repository.query.ValueExpressionDelegate;
import org.springframework.data.spel.ExpressionDependencies;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

Expand Down Expand Up @@ -103,9 +96,7 @@ protected Optional<QueryLookupStrategy> getQueryLookupStrategy(

return Optional.of(
new DatastoreQueryLookupStrategy(
this.datastoreMappingContext,
this.datastoreOperations,
valueExpressionDelegate));
this.datastoreMappingContext, this.datastoreOperations, valueExpressionDelegate));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@
import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.aot.hint.TypeReference;

/**
* Runtime hints for integration test.
*/
/** Runtime hints for integration test. */
public class TestRuntimeHints implements RuntimeHintsRegistrar {

@Override
Expand All @@ -65,12 +63,24 @@ public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
TypeReference.of(ServiceConfiguration.class),
TypeReference.of(SubEntity.class),
TypeReference.of(TreeCollection.class),
TypeReference.of(com.google.cloud.spring.data.datastore.it.subclasses.references.testdomains.EntityA.class),
TypeReference.of(com.google.cloud.spring.data.datastore.it.subclasses.references.testdomains.EntityB.class),
TypeReference.of(com.google.cloud.spring.data.datastore.it.subclasses.references.testdomains.EntityC.class),
TypeReference.of(com.google.cloud.spring.data.datastore.it.subclasses.descendants.testdomains.EntityA.class),
TypeReference.of(com.google.cloud.spring.data.datastore.it.subclasses.descendants.testdomains.EntityB.class),
TypeReference.of(com.google.cloud.spring.data.datastore.it.subclasses.descendants.testdomains.EntityC.class)),
TypeReference.of(
com.google.cloud.spring.data.datastore.it.subclasses.references.testdomains
.EntityA.class),
TypeReference.of(
com.google.cloud.spring.data.datastore.it.subclasses.references.testdomains
.EntityB.class),
TypeReference.of(
com.google.cloud.spring.data.datastore.it.subclasses.references.testdomains
.EntityC.class),
TypeReference.of(
com.google.cloud.spring.data.datastore.it.subclasses.descendants.testdomains
.EntityA.class),
TypeReference.of(
com.google.cloud.spring.data.datastore.it.subclasses.descendants.testdomains
.EntityB.class),
TypeReference.of(
com.google.cloud.spring.data.datastore.it.subclasses.descendants.testdomains
.EntityC.class)),
hint ->
hint.withMembers(
MemberCategory.INVOKE_DECLARED_CONSTRUCTORS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ private static class TestEntity {

@LastModifiedBy String lastUser;

@LastModifiedDate
Instant lastTouched;
@LastModifiedDate Instant lastTouched;
}
}
Loading

0 comments on commit 290c7a2

Please sign in to comment.