Skip to content

Commit

Permalink
feat: change signature of decoder match to canDecode(path: String, ta…
Browse files Browse the repository at this point in the history
…gs: Tags, configNode:ConfigNode, TypeCapture<?> klass) so we have more information to (#124)

feat: change signature of decoder match to canDecode(path: String, tags: Tags, configNode:ConfigNode, TypeCapture<?> klass) so we have more information to decide how to decode the class.
  • Loading branch information
credmond-git authored Dec 4, 2023
1 parent 07485b1 commit b76d114
Show file tree
Hide file tree
Showing 90 changed files with 442 additions and 406 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -995,10 +995,13 @@ You can create your own decoder by implementing the Decoder interface. By return
/**
* true if this decoder matches the type capture.
*
* @param klass TypeCapture we are looking for a decoder.
* @param path the current path
* @param tags the tags for the current request
* @param node the current node we are decoding.
* @param type the type of object we are decoding.
* @return true if this decoder matches the type capture
*/
boolean matches(TypeCapture<?> klass);
boolean canDecode(path: String, tags: Tags, configNode:ConfigNode, TypeCapture<?> klass);

/**
* Decode the current node. If the current node is a class or list we may need to decode sub nodes.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public String name() {
}

@Override
public boolean matches(TypeCapture<?> type) {
public boolean canDecode(String path, Tags tags, ConfigNode node, TypeCapture<?> type) {
return type.isArray();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.github.gestalt.config.entity.ValidationError;
import org.github.gestalt.config.node.ConfigNode;
import org.github.gestalt.config.reflect.TypeCapture;
import org.github.gestalt.config.tag.Tags;
import org.github.gestalt.config.utils.StringUtils;
import org.github.gestalt.config.utils.ValidateOf;

Expand All @@ -26,8 +27,8 @@ public String name() {
}

@Override
public boolean matches(TypeCapture<?> klass) {
return BigDecimal.class.isAssignableFrom(klass.getRawType());
public boolean canDecode(String path, Tags tags, ConfigNode node, TypeCapture<?> type) {
return BigDecimal.class.isAssignableFrom(type.getRawType());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.github.gestalt.config.entity.ValidationError;
import org.github.gestalt.config.node.ConfigNode;
import org.github.gestalt.config.reflect.TypeCapture;
import org.github.gestalt.config.tag.Tags;
import org.github.gestalt.config.utils.StringUtils;
import org.github.gestalt.config.utils.ValidateOf;

Expand All @@ -26,8 +27,8 @@ public String name() {
}

@Override
public boolean matches(TypeCapture<?> klass) {
return BigInteger.class.isAssignableFrom(klass.getRawType());
public boolean canDecode(String path, Tags tags, ConfigNode node, TypeCapture<?> type) {
return BigInteger.class.isAssignableFrom(type.getRawType());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.github.gestalt.config.node.ConfigNode;
import org.github.gestalt.config.reflect.TypeCapture;
import org.github.gestalt.config.tag.Tags;
import org.github.gestalt.config.utils.ValidateOf;

/**
Expand All @@ -22,8 +23,8 @@ public String name() {
}

@Override
public boolean matches(TypeCapture<?> klass) {
return Boolean.class.isAssignableFrom(klass.getRawType()) || boolean.class.isAssignableFrom(klass.getRawType());
public boolean canDecode(String path, Tags tags, ConfigNode node, TypeCapture<?> type) {
return Boolean.class.isAssignableFrom(type.getRawType()) || boolean.class.isAssignableFrom(type.getRawType());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.github.gestalt.config.entity.ValidationError;
import org.github.gestalt.config.node.ConfigNode;
import org.github.gestalt.config.reflect.TypeCapture;
import org.github.gestalt.config.tag.Tags;
import org.github.gestalt.config.utils.ValidateOf;

import java.nio.charset.Charset;
Expand All @@ -25,8 +26,8 @@ public String name() {
}

@Override
public boolean matches(TypeCapture<?> klass) {
return Byte.class.isAssignableFrom(klass.getRawType()) || byte.class.isAssignableFrom(klass.getRawType());
public boolean canDecode(String path, Tags tags, ConfigNode node, TypeCapture<?> type) {
return Byte.class.isAssignableFrom(type.getRawType()) || byte.class.isAssignableFrom(type.getRawType());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.github.gestalt.config.entity.ValidationError;
import org.github.gestalt.config.node.ConfigNode;
import org.github.gestalt.config.reflect.TypeCapture;
import org.github.gestalt.config.tag.Tags;
import org.github.gestalt.config.utils.ValidateOf;

import java.util.ArrayList;
Expand All @@ -26,8 +27,8 @@ public String name() {
}

@Override
public boolean matches(TypeCapture<?> klass) {
return Character.class.isAssignableFrom(klass.getRawType()) || char.class.isAssignableFrom(klass.getRawType());
public boolean canDecode(String path, Tags tags, ConfigNode node, TypeCapture<?> type) {
return Character.class.isAssignableFrom(type.getRawType()) || char.class.isAssignableFrom(type.getRawType());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.github.gestalt.config.entity.ValidationError;
import org.github.gestalt.config.node.ConfigNode;
import org.github.gestalt.config.reflect.TypeCapture;
import org.github.gestalt.config.tag.Tags;
import org.github.gestalt.config.utils.ValidateOf;

import java.time.Instant;
Expand Down Expand Up @@ -60,8 +61,8 @@ public String name() {
}

@Override
public boolean matches(TypeCapture<?> klass) {
return Date.class.isAssignableFrom(klass.getRawType());
public boolean canDecode(String path, Tags tags, ConfigNode node, TypeCapture<?> type) {
return Date.class.isAssignableFrom(type.getRawType());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,13 @@ default void applyConfig(GestaltConfig config) {
/**
* true if this decoder matches the type capture.
*
* @param klass TypeCapture we are looking for a decoder.
* @param path the current path
* @param tags the tags for the current request
* @param node the current node we are decoding.
* @param type the type of object we are decoding.
* @return true if this decoder matches the type capture
*/
boolean matches(TypeCapture<?> klass);
boolean canDecode(String path, Tags tags, ConfigNode node, TypeCapture<?> type);

/**
* Decode the current node. If the current node is a class or list we may need to decode sub nodes.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ public void setPathMappers(List<PathMapper> pathMappers) {
* @return a list of decoders that match the class
*/
@SuppressWarnings("rawtypes")
<T> List<Decoder> getDecoderForClass(TypeCapture<T> klass) {
<T> List<Decoder> getDecoderForClass(String path, Tags tags, ConfigNode configNode, TypeCapture<T> klass) {
return decoders
.stream()
.filter(decoder -> decoder.matches(klass))
.filter(decoder -> decoder.canDecode(path, tags, configNode, klass))
.collect(Collectors.toList());
}

Expand All @@ -121,7 +121,7 @@ public <T> ValidateOf<T> decodeNode(String path, Tags tags, String configNode, T
@SuppressWarnings({"rawtypes", "unchecked"})
public <T> ValidateOf<T> decodeNode(String path, Tags tags, ConfigNode configNode, TypeCapture<T> klass,
DecoderContext decoderContext) {
List<Decoder> classDecoder = getDecoderForClass(klass);
List<Decoder> classDecoder = getDecoderForClass(path, tags, configNode, klass);
classDecoder.sort(Comparator.comparingInt(v -> v.priority().ordinal()));
if (classDecoder.isEmpty()) {
return ValidateOf.inValid(new ValidationError.NoDecodersFound(klass.getName()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.github.gestalt.config.entity.ValidationError;
import org.github.gestalt.config.node.ConfigNode;
import org.github.gestalt.config.reflect.TypeCapture;
import org.github.gestalt.config.tag.Tags;
import org.github.gestalt.config.utils.StringUtils;
import org.github.gestalt.config.utils.ValidateOf;

Expand All @@ -24,8 +25,8 @@ public String name() {
}

@Override
public boolean matches(TypeCapture<?> klass) {
return Double.class.isAssignableFrom(klass.getRawType()) || double.class.isAssignableFrom(klass.getRawType());
public boolean canDecode(String path, Tags tags, ConfigNode node, TypeCapture<?> type) {
return Double.class.isAssignableFrom(type.getRawType()) || double.class.isAssignableFrom(type.getRawType());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.github.gestalt.config.entity.ValidationError;
import org.github.gestalt.config.node.ConfigNode;
import org.github.gestalt.config.reflect.TypeCapture;
import org.github.gestalt.config.tag.Tags;
import org.github.gestalt.config.utils.StringUtils;
import org.github.gestalt.config.utils.ValidateOf;

Expand All @@ -26,8 +27,8 @@ public String name() {
}

@Override
public boolean matches(TypeCapture<?> klass) {
return Duration.class.isAssignableFrom(klass.getRawType());
public boolean canDecode(String path, Tags tags, ConfigNode node, TypeCapture<?> type) {
return Duration.class.isAssignableFrom(type.getRawType());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.github.gestalt.config.entity.ValidationError;
import org.github.gestalt.config.node.ConfigNode;
import org.github.gestalt.config.reflect.TypeCapture;
import org.github.gestalt.config.tag.Tags;
import org.github.gestalt.config.utils.ValidateOf;

import java.lang.reflect.InvocationTargetException;
Expand All @@ -26,8 +27,8 @@ public String name() {
}

@Override
public boolean matches(TypeCapture<?> klass) {
return klass.getRawType().isEnum();
public boolean canDecode(String path, Tags tags, ConfigNode node, TypeCapture<?> type) {
return type.getRawType().isEnum();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.github.gestalt.config.node.ConfigNode;
import org.github.gestalt.config.reflect.TypeCapture;
import org.github.gestalt.config.tag.Tags;
import org.github.gestalt.config.utils.ValidateOf;

import java.io.File;
Expand All @@ -26,8 +27,8 @@ public String name() {
}

@Override
public boolean matches(TypeCapture<?> klass) {
return File.class.isAssignableFrom(klass.getRawType());
public boolean canDecode(String path, Tags tags, ConfigNode node, TypeCapture<?> type) {
return File.class.isAssignableFrom(type.getRawType());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.github.gestalt.config.entity.ValidationError;
import org.github.gestalt.config.node.ConfigNode;
import org.github.gestalt.config.reflect.TypeCapture;
import org.github.gestalt.config.tag.Tags;
import org.github.gestalt.config.utils.StringUtils;
import org.github.gestalt.config.utils.ValidateOf;

Expand All @@ -24,8 +25,8 @@ public String name() {
}

@Override
public boolean matches(TypeCapture<?> klass) {
return Float.class.isAssignableFrom(klass.getRawType()) || float.class.isAssignableFrom(klass.getRawType());
public boolean canDecode(String path, Tags tags, ConfigNode node, TypeCapture<?> type) {
return Float.class.isAssignableFrom(type.getRawType()) || float.class.isAssignableFrom(type.getRawType());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.github.gestalt.config.entity.ValidationError;
import org.github.gestalt.config.node.ConfigNode;
import org.github.gestalt.config.reflect.TypeCapture;
import org.github.gestalt.config.tag.Tags;
import org.github.gestalt.config.utils.ValidateOf;

import java.time.Instant;
Expand All @@ -26,8 +27,8 @@ public String name() {
}

@Override
public boolean matches(TypeCapture<?> klass) {
return Instant.class.isAssignableFrom(klass.getRawType());
public boolean canDecode(String path, Tags tags, ConfigNode node, TypeCapture<?> type) {
return Instant.class.isAssignableFrom(type.getRawType());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.github.gestalt.config.entity.ValidationError;
import org.github.gestalt.config.node.ConfigNode;
import org.github.gestalt.config.reflect.TypeCapture;
import org.github.gestalt.config.tag.Tags;
import org.github.gestalt.config.utils.StringUtils;
import org.github.gestalt.config.utils.ValidateOf;

Expand All @@ -24,8 +25,8 @@ public String name() {
}

@Override
public boolean matches(TypeCapture<?> klass) {
return Integer.class.isAssignableFrom(klass.getRawType()) || int.class.isAssignableFrom(klass.getRawType());
public boolean canDecode(String path, Tags tags, ConfigNode node, TypeCapture<?> type) {
return Integer.class.isAssignableFrom(type.getRawType()) || int.class.isAssignableFrom(type.getRawType());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public String name() {
}

@Override
public boolean matches(TypeCapture<?> klass) {
return List.class.isAssignableFrom(klass.getRawType()) && klass.hasParameter();
public boolean canDecode(String path, Tags tags, ConfigNode node, TypeCapture<?> type) {
return List.class.isAssignableFrom(type.getRawType()) && type.hasParameter();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.github.gestalt.config.entity.ValidationError;
import org.github.gestalt.config.node.ConfigNode;
import org.github.gestalt.config.reflect.TypeCapture;
import org.github.gestalt.config.tag.Tags;
import org.github.gestalt.config.utils.ValidateOf;

import java.time.LocalDate;
Expand Down Expand Up @@ -57,8 +58,8 @@ public String name() {
}

@Override
public boolean matches(TypeCapture<?> klass) {
return LocalDate.class.isAssignableFrom(klass.getRawType());
public boolean canDecode(String path, Tags tags, ConfigNode node, TypeCapture<?> type) {
return LocalDate.class.isAssignableFrom(type.getRawType());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.github.gestalt.config.entity.ValidationError;
import org.github.gestalt.config.node.ConfigNode;
import org.github.gestalt.config.reflect.TypeCapture;
import org.github.gestalt.config.tag.Tags;
import org.github.gestalt.config.utils.ValidateOf;

import java.time.LocalDateTime;
Expand Down Expand Up @@ -57,8 +58,8 @@ public String name() {
}

@Override
public boolean matches(TypeCapture<?> klass) {
return LocalDateTime.class.isAssignableFrom(klass.getRawType());
public boolean canDecode(String path, Tags tags, ConfigNode node, TypeCapture<?> type) {
return LocalDateTime.class.isAssignableFrom(type.getRawType());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.github.gestalt.config.entity.ValidationError;
import org.github.gestalt.config.node.ConfigNode;
import org.github.gestalt.config.reflect.TypeCapture;
import org.github.gestalt.config.tag.Tags;
import org.github.gestalt.config.utils.StringUtils;
import org.github.gestalt.config.utils.ValidateOf;

Expand All @@ -24,8 +25,8 @@ public String name() {
}

@Override
public boolean matches(TypeCapture<?> klass) {
return Long.class.isAssignableFrom(klass.getRawType()) || long.class.isAssignableFrom(klass.getRawType());
public boolean canDecode(String path, Tags tags, ConfigNode node, TypeCapture<?> type) {
return Long.class.isAssignableFrom(type.getRawType()) || long.class.isAssignableFrom(type.getRawType());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public String name() {
}

@Override
public boolean matches(TypeCapture<?> type) {
public boolean canDecode(String path, Tags tags, ConfigNode node, TypeCapture<?> type) {
return Map.class.isAssignableFrom(type.getRawType()) && type.hasParameter();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ public String name() {
}

@Override
public boolean matches(TypeCapture<?> klass) {
return !klass.getRawType().isPrimitive() && !klass.isArray() && !klass.isEnum() &&
!klass.hasParameter() && !klass.isInterface() && !ignoreTypes.contains(klass.getRawType());
public boolean canDecode(String path, Tags tags, ConfigNode node, TypeCapture<?> type) {
return !type.getRawType().isPrimitive() && !type.isArray() && !type.isEnum() &&
!type.hasParameter() && !type.isInterface() && !ignoreTypes.contains(type.getRawType());
}

private Set<Class<?>> getIgnoreTypes() {
Expand Down
Loading

0 comments on commit b76d114

Please sign in to comment.