Skip to content

Commit

Permalink
add DataType so that we can differentiate between decimals and integers
Browse files Browse the repository at this point in the history
  • Loading branch information
NielsCW committed Dec 20, 2024
1 parent 457e461 commit f753eb6
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,12 @@
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.context.MessageSourceResolvable;
import org.springframework.core.io.support.SpringFactoriesLoader;
import org.springframework.data.rest.webmvc.RootResourceInformation;
import org.springframework.hateoas.mediatype.InputTypeFactory;
import org.springframework.hateoas.mediatype.MessageResolver;

@RequiredArgsConstructor
public class AttributeRepresentationModelAssembler {

private static final InputTypeFactory INPUT_TYPE_FACTORY;

static {
INPUT_TYPE_FACTORY = SpringFactoriesLoader.loadFactories(InputTypeFactory.class,
AttributeRepresentationModelAssembler.class.getClassLoader()).get(0);
}

private final CollectionFiltersMapping collectionFiltersMapping;
private final MessageResolver messageResolver;

Expand Down Expand Up @@ -93,12 +84,11 @@ public Optional<AttributeRepresentationModel> toModel(RootResourceInformation in
}

private String getType(Property property) {
// TODO: How to distinguish between decimals and integers?
var type = INPUT_TYPE_FACTORY.getInputType(property.getTypeInformation().getType());
var type = DataType.from(property.getTypeInformation().getType());
if (type == null && property.findAnnotation(Embedded.class).isPresent()) {
type = "object";
type = DataType.OBJECT;
}
return type;
return type == null ? null : type.name().toLowerCase();
}

private String readDescription(RootResourceInformation information, List<Property> properties) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.contentgrid.spring.data.rest.webmvc.blueprint;

import java.math.BigDecimal;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
import java.util.Collection;
import java.util.List;
import java.util.UUID;
import org.springframework.lang.Nullable;

public enum DataType {
STRING,
LONG,
DOUBLE,
BOOLEAN,
DATETIME,
OBJECT;

private static final Collection<Class<?>> STRING_TYPES = List.of(String.class, UUID.class);
private static final Collection<Class<?>> LONG_TYPES = List.of(int.class, long.class, short.class,
Integer.class, Long.class, Short.class);
private static final Collection<Class<?>> DOUBLE_TYPES = List.of(float.class, double.class,
Float.class, Double.class, BigDecimal.class);
private static final Collection<Class<?>> BOOLEAN_TYPES = List.of(boolean.class, Boolean.class);
private static final Collection<Class<?>> DATETIME_TYPES = List.of(Instant.class, LocalDateTime.class,
OffsetDateTime.class, ZonedDateTime.class);

@Nullable
public static DataType from(Class<?> type) {

if (STRING_TYPES.contains(type)) {
return STRING;
}

if (LONG_TYPES.contains(type)) {
return LONG;
}

if (DOUBLE_TYPES.contains(type)) {
return DOUBLE;
}

if (BOOLEAN_TYPES.contains(type)) {
return BOOLEAN;
}

if (DATETIME_TYPES.contains(type)) {
return DATETIME;
}

return null;
}
}

0 comments on commit f753eb6

Please sign in to comment.