Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ repositories {

dependencies {
implementation 'ch.qos.logback:logback-classic:1.2.3'
implementation 'ch.qos.logback.contrib:logback-json-classic:0.1.5'
implementation 'ch.qos.logback.contrib:logback-jackson:0.1.5'
implementation 'net.logstash.logback:logstash-logback-encoder:5.2'
implementation 'org.apache.maven:maven-artifact:3.5.2'
implementation 'org.antlr:antlr4-runtime:4.13.1'
implementation 'io.vavr:vavr:0.10.4'
Expand Down Expand Up @@ -122,7 +119,7 @@ nexusPublishing {

ext.genOutputDir = file("$buildDir/generated-resources")

task generateVersionTxt() {
task generateVersionTxt() {
ext.outputFile = file("$genOutputDir/version.txt")
outputs.file(outputFile)
doLast {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private boolean evaluateNumericRangeToken(final NumericRangeNode numericRangeTok

private boolean evaluateInToken(final InNode inToken, final Map<String, Object> data) {
final Object fieldData = ValueUtils.getValueFromMap(inToken.getField(), data).orElseThrow(DataNotFoundException::new);
final DataType dataType = inToken.getItems().get(0).getLeft();
final DataType dataType = ValueUtils.getDataType(fieldData);
final Object[] values = inToken.getItems()
.stream()
.map(Pair::getRight).toArray();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.github.sidhant92.boolparser.datatype;

import java.util.Optional;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.sidhant92.boolparser.constant.DataType;
import lombok.extern.slf4j.Slf4j;

Expand All @@ -17,33 +16,17 @@ public AbstractDataType(final Class<T> clazz) {
this.clazz = clazz;
}

protected boolean defaultIsValid(final Object value, final ObjectMapper objectMapper) {
return defaultIsValid(value, objectMapper, false);
protected boolean defaultIsValid(final Object value) {
return defaultIsValid(value, false);
}

protected boolean defaultIsValid(final Object value, final ObjectMapper objectMapper, final boolean useStrictValidation) {
try {
if (clazz.isInstance(value)) {
return true;
}
if (useStrictValidation) {
return false;
}
return objectMapper.convertValue(value, clazz) != null;
} catch (final Exception ex) {
log.error("Unable to convert value = {} to type = {}", value, clazz);
}
return false;
protected boolean defaultIsValid(final Object value, final boolean useStrictValidation) {
return clazz.isInstance(value);
}

protected Optional<T> defaultGetValue(final Object value, final ObjectMapper objectMapper) {
try {
if (clazz.isInstance(value)) {
return Optional.of(clazz.cast(value));
}
return Optional.of(objectMapper.convertValue(value, clazz));
} catch (final Exception ex) {
log.error("Unable to convert value = {} to type = {}", value, clazz);
protected Optional<T> defaultGetValue(final Object value) {
if (clazz.isInstance(value)) {
return Optional.of(clazz.cast(value));
}
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
package com.github.sidhant92.boolparser.datatype;

import java.util.Optional;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.sidhant92.boolparser.constant.DataType;

/**
* @author sidhant.aggarwal
* @since 05/03/2023
*/
public class BooleanDataType extends AbstractDataType<Boolean> {
private final ObjectMapper objectMapper;

public BooleanDataType(final ObjectMapper objectMapper) {
public BooleanDataType() {
super(Boolean.class);
this.objectMapper = objectMapper;
}

@Override
Expand All @@ -23,16 +19,31 @@ public DataType getDataType() {

@Override
public boolean isValid(final Object value) {
return super.defaultIsValid(value, objectMapper);
boolean isValid = super.defaultIsValid(value);
if (!isValid) {
final String lowercase = value.toString().toLowerCase();
return lowercase.equals("true") || lowercase.equals("false");
}
return true;
}

@Override
public boolean isValid(final Object value, final boolean useStrictValidation) {
return super.defaultIsValid(value, objectMapper, useStrictValidation);
if (!useStrictValidation) {
return isValid(value);
}
return super.defaultIsValid(value);
}

@Override
public Optional<Boolean> getValue(Object value) {
return defaultGetValue(value, objectMapper);
final Optional<Boolean> result = defaultGetValue(value);
if (result.isPresent()) {
return result;
}
if (this.isValid(value)) {
return Optional.of(Boolean.parseBoolean(value.toString()));
}
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.EnumMap;
import java.util.Map;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.sidhant92.boolparser.constant.DataType;

/**
Expand All @@ -17,13 +16,12 @@ private DataTypeFactory() {
}

public static void initialize() {
final ObjectMapper objectMapper = new ObjectMapper();
abstractDataTypeMap.put(DataType.STRING, new StringDataType(objectMapper));
abstractDataTypeMap.put(DataType.INTEGER, new IntegerDataType(objectMapper));
abstractDataTypeMap.put(DataType.DECIMAL, new DecimalDataType(objectMapper));
abstractDataTypeMap.put(DataType.LONG, new LongDataType(objectMapper));
abstractDataTypeMap.put(DataType.VERSION, new VersionDataType(objectMapper));
abstractDataTypeMap.put(DataType.BOOLEAN, new BooleanDataType(objectMapper));
abstractDataTypeMap.put(DataType.STRING, new StringDataType());
abstractDataTypeMap.put(DataType.INTEGER, new IntegerDataType());
abstractDataTypeMap.put(DataType.DECIMAL, new DecimalDataType());
abstractDataTypeMap.put(DataType.LONG, new LongDataType());
abstractDataTypeMap.put(DataType.VERSION, new VersionDataType());
abstractDataTypeMap.put(DataType.BOOLEAN, new BooleanDataType());
}

public static AbstractDataType getDataType(final DataType dataType) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
package com.github.sidhant92.boolparser.datatype;

import java.util.Optional;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.sidhant92.boolparser.constant.DataType;

/**
* @author sidhant.aggarwal
* @since 05/03/2023
*/
public class DecimalDataType extends AbstractDataType<Double> {
private final ObjectMapper objectMapper;

public DecimalDataType(final ObjectMapper objectMapper) {
public DecimalDataType() {
super(Double.class);
this.objectMapper = objectMapper;
}

@Override
Expand All @@ -23,16 +19,36 @@ public DataType getDataType() {

@Override
public boolean isValid(final Object value) {
return super.defaultIsValid(value, objectMapper);
boolean isValid = super.defaultIsValid(value);
if (!isValid) {
try {
Double.parseDouble(value.toString());
return true;
} catch (Exception ex) {
return false;
}
}
return true;
}

@Override
public boolean isValid(final Object value, final boolean useStrictValidation) {
return super.defaultIsValid(value, objectMapper, useStrictValidation);
if (!useStrictValidation) {
return isValid(value);
}
return super.defaultIsValid(value);
}

@Override
public Optional<Double> getValue(Object value) {
return defaultGetValue(value, objectMapper);
final Optional<Double> result = defaultGetValue(value);
if (result.isPresent()) {
return result;
}
try {
return Optional.of(Double.parseDouble(value.toString()));
} catch (final Exception ignored) {
}
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
package com.github.sidhant92.boolparser.datatype;

import java.util.Optional;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.sidhant92.boolparser.constant.DataType;

/**
* @author sidhant.aggarwal
* @since 05/03/2023
*/
public class IntegerDataType extends AbstractDataType<Integer> {
private final ObjectMapper objectMapper;

public IntegerDataType(final ObjectMapper objectMapper) {
public IntegerDataType() {
super(Integer.class);
this.objectMapper = objectMapper;
}

@Override
Expand All @@ -23,16 +19,36 @@ public DataType getDataType() {

@Override
public boolean isValid(final Object value) {
return super.defaultIsValid(value, objectMapper);
boolean isValid = super.defaultIsValid(value);
if (!isValid) {
try {
Integer.parseInt(value.toString());
return true;
} catch (Exception ex) {
return false;
}
}
return true;
}

@Override
public boolean isValid(final Object value, final boolean useStrictValidation) {
return super.defaultIsValid(value, objectMapper, useStrictValidation);
if (!useStrictValidation) {
return isValid(value);
}
return super.defaultIsValid(value);
}

@Override
public Optional<Integer> getValue(Object value) {
return defaultGetValue(value, objectMapper);
final Optional<Integer> result = defaultGetValue(value);
if (result.isPresent()) {
return result;
}
try {
return Optional.of(Integer.parseInt(value.toString()));
} catch (final Exception ignored) {
}
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
package com.github.sidhant92.boolparser.datatype;

import java.util.Optional;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.sidhant92.boolparser.constant.DataType;

/**
* @author sidhant.aggarwal
* @since 05/03/2023
*/
public class LongDataType extends AbstractDataType<Long> {
private final ObjectMapper objectMapper;

public LongDataType(final ObjectMapper objectMapper) {
public LongDataType() {
super(Long.class);
this.objectMapper = objectMapper;
}

@Override
Expand All @@ -23,16 +19,36 @@ public DataType getDataType() {

@Override
public boolean isValid(final Object value) {
return super.defaultIsValid(value, objectMapper);
boolean isValid = super.defaultIsValid(value);
if (!isValid) {
try {
Long.parseLong(value.toString());
return true;
} catch (Exception ex) {
return false;
}
}
return true;
}

@Override
public boolean isValid(final Object value, final boolean useStrictValidation) {
return super.defaultIsValid(value, objectMapper, useStrictValidation);
if (!useStrictValidation) {
return isValid(value);
}
return super.defaultIsValid(value);
}

@Override
public Optional<Long> getValue(Object value) {
return defaultGetValue(value, objectMapper);
final Optional<Long> result = defaultGetValue(value);
if (result.isPresent()) {
return result;
}
try {
return Optional.of(Long.parseLong(value.toString()));
} catch (final Exception ignored) {
}
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
package com.github.sidhant92.boolparser.datatype;

import java.util.Optional;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.sidhant92.boolparser.constant.DataType;

/**
* @author sidhant.aggarwal
* @since 05/03/2023
*/
public class StringDataType extends AbstractDataType<String> {
private final ObjectMapper objectMapper;

public StringDataType(final ObjectMapper objectMapper) {
public StringDataType() {
super(String.class);
this.objectMapper = objectMapper;
}

@Override
Expand All @@ -23,16 +19,23 @@ public DataType getDataType() {

@Override
public boolean isValid(final Object value) {
return super.defaultIsValid(value, objectMapper);
return true;
}

@Override
public boolean isValid(final Object value, final boolean useStrictValidation) {
return super.defaultIsValid(value, objectMapper, useStrictValidation);
if (!useStrictValidation) {
return isValid(value);
}
return super.defaultIsValid(value);
}

@Override
public Optional<String> getValue(Object value) {
return defaultGetValue(value, objectMapper);
final Optional<String> result = defaultGetValue(value);
if (result.isPresent()) {
return result;
}
return Optional.of(value.toString());
}
}
Loading