Skip to content

Commit 5b9ee91

Browse files
authored
Merge pull request #30 from sidhant92/remove_jackson
Remove Jackson Object Mapper
2 parents 62307b1 + a2ad8c3 commit 5b9ee91

10 files changed

Lines changed: 141 additions & 85 deletions

File tree

build.gradle

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ repositories {
3232

3333
dependencies {
3434
implementation 'ch.qos.logback:logback-classic:1.2.3'
35-
implementation 'ch.qos.logback.contrib:logback-json-classic:0.1.5'
36-
implementation 'ch.qos.logback.contrib:logback-jackson:0.1.5'
37-
implementation 'net.logstash.logback:logstash-logback-encoder:5.2'
3835
implementation 'org.apache.maven:maven-artifact:3.5.2'
3936
implementation 'org.antlr:antlr4-runtime:4.13.1'
4037
implementation 'io.vavr:vavr:0.10.4'
@@ -122,7 +119,7 @@ nexusPublishing {
122119

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

125-
task generateVersionTxt() {
122+
task generateVersionTxt() {
126123
ext.outputFile = file("$genOutputDir/version.txt")
127124
outputs.file(outputFile)
128125
doLast {

src/main/java/com/github/sidhant92/boolparser/application/BooleanExpressionEvaluator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ private boolean evaluateNumericRangeToken(final NumericRangeNode numericRangeTok
8585

8686
private boolean evaluateInToken(final InNode inToken, final Map<String, Object> data) {
8787
final Object fieldData = ValueUtils.getValueFromMap(inToken.getField(), data).orElseThrow(DataNotFoundException::new);
88-
final DataType dataType = inToken.getItems().get(0).getLeft();
88+
final DataType dataType = ValueUtils.getDataType(fieldData);
8989
final Object[] values = inToken.getItems()
9090
.stream()
9191
.map(Pair::getRight).toArray();

src/main/java/com/github/sidhant92/boolparser/datatype/AbstractDataType.java

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.github.sidhant92.boolparser.datatype;
22

33
import java.util.Optional;
4-
import com.fasterxml.jackson.databind.ObjectMapper;
54
import com.github.sidhant92.boolparser.constant.DataType;
65
import lombok.extern.slf4j.Slf4j;
76

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

20-
protected boolean defaultIsValid(final Object value, final ObjectMapper objectMapper) {
21-
return defaultIsValid(value, objectMapper, false);
19+
protected boolean defaultIsValid(final Object value) {
20+
return defaultIsValid(value, false);
2221
}
2322

24-
protected boolean defaultIsValid(final Object value, final ObjectMapper objectMapper, final boolean useStrictValidation) {
25-
try {
26-
if (clazz.isInstance(value)) {
27-
return true;
28-
}
29-
if (useStrictValidation) {
30-
return false;
31-
}
32-
return objectMapper.convertValue(value, clazz) != null;
33-
} catch (final Exception ex) {
34-
log.error("Unable to convert value = {} to type = {}", value, clazz);
35-
}
36-
return false;
23+
protected boolean defaultIsValid(final Object value, final boolean useStrictValidation) {
24+
return clazz.isInstance(value);
3725
}
3826

39-
protected Optional<T> defaultGetValue(final Object value, final ObjectMapper objectMapper) {
40-
try {
41-
if (clazz.isInstance(value)) {
42-
return Optional.of(clazz.cast(value));
43-
}
44-
return Optional.of(objectMapper.convertValue(value, clazz));
45-
} catch (final Exception ex) {
46-
log.error("Unable to convert value = {} to type = {}", value, clazz);
27+
protected Optional<T> defaultGetValue(final Object value) {
28+
if (clazz.isInstance(value)) {
29+
return Optional.of(clazz.cast(value));
4730
}
4831
return Optional.empty();
4932
}
Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
package com.github.sidhant92.boolparser.datatype;
22

33
import java.util.Optional;
4-
import com.fasterxml.jackson.databind.ObjectMapper;
54
import com.github.sidhant92.boolparser.constant.DataType;
65

76
/**
87
* @author sidhant.aggarwal
98
* @since 05/03/2023
109
*/
1110
public class BooleanDataType extends AbstractDataType<Boolean> {
12-
private final ObjectMapper objectMapper;
13-
14-
public BooleanDataType(final ObjectMapper objectMapper) {
11+
public BooleanDataType() {
1512
super(Boolean.class);
16-
this.objectMapper = objectMapper;
1713
}
1814

1915
@Override
@@ -23,16 +19,31 @@ public DataType getDataType() {
2319

2420
@Override
2521
public boolean isValid(final Object value) {
26-
return super.defaultIsValid(value, objectMapper);
22+
boolean isValid = super.defaultIsValid(value);
23+
if (!isValid) {
24+
final String lowercase = value.toString().toLowerCase();
25+
return lowercase.equals("true") || lowercase.equals("false");
26+
}
27+
return true;
2728
}
2829

2930
@Override
3031
public boolean isValid(final Object value, final boolean useStrictValidation) {
31-
return super.defaultIsValid(value, objectMapper, useStrictValidation);
32+
if (!useStrictValidation) {
33+
return isValid(value);
34+
}
35+
return super.defaultIsValid(value);
3236
}
3337

3438
@Override
3539
public Optional<Boolean> getValue(Object value) {
36-
return defaultGetValue(value, objectMapper);
40+
final Optional<Boolean> result = defaultGetValue(value);
41+
if (result.isPresent()) {
42+
return result;
43+
}
44+
if (this.isValid(value)) {
45+
return Optional.of(Boolean.parseBoolean(value.toString()));
46+
}
47+
return Optional.empty();
3748
}
3849
}

src/main/java/com/github/sidhant92/boolparser/datatype/DataTypeFactory.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.util.EnumMap;
44
import java.util.Map;
5-
import com.fasterxml.jackson.databind.ObjectMapper;
65
import com.github.sidhant92.boolparser.constant.DataType;
76

87
/**
@@ -17,13 +16,12 @@ private DataTypeFactory() {
1716
}
1817

1918
public static void initialize() {
20-
final ObjectMapper objectMapper = new ObjectMapper();
21-
abstractDataTypeMap.put(DataType.STRING, new StringDataType(objectMapper));
22-
abstractDataTypeMap.put(DataType.INTEGER, new IntegerDataType(objectMapper));
23-
abstractDataTypeMap.put(DataType.DECIMAL, new DecimalDataType(objectMapper));
24-
abstractDataTypeMap.put(DataType.LONG, new LongDataType(objectMapper));
25-
abstractDataTypeMap.put(DataType.VERSION, new VersionDataType(objectMapper));
26-
abstractDataTypeMap.put(DataType.BOOLEAN, new BooleanDataType(objectMapper));
19+
abstractDataTypeMap.put(DataType.STRING, new StringDataType());
20+
abstractDataTypeMap.put(DataType.INTEGER, new IntegerDataType());
21+
abstractDataTypeMap.put(DataType.DECIMAL, new DecimalDataType());
22+
abstractDataTypeMap.put(DataType.LONG, new LongDataType());
23+
abstractDataTypeMap.put(DataType.VERSION, new VersionDataType());
24+
abstractDataTypeMap.put(DataType.BOOLEAN, new BooleanDataType());
2725
}
2826

2927
public static AbstractDataType getDataType(final DataType dataType) {
Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
package com.github.sidhant92.boolparser.datatype;
22

33
import java.util.Optional;
4-
import com.fasterxml.jackson.databind.ObjectMapper;
54
import com.github.sidhant92.boolparser.constant.DataType;
65

76
/**
87
* @author sidhant.aggarwal
98
* @since 05/03/2023
109
*/
1110
public class DecimalDataType extends AbstractDataType<Double> {
12-
private final ObjectMapper objectMapper;
13-
14-
public DecimalDataType(final ObjectMapper objectMapper) {
11+
public DecimalDataType() {
1512
super(Double.class);
16-
this.objectMapper = objectMapper;
1713
}
1814

1915
@Override
@@ -23,16 +19,36 @@ public DataType getDataType() {
2319

2420
@Override
2521
public boolean isValid(final Object value) {
26-
return super.defaultIsValid(value, objectMapper);
22+
boolean isValid = super.defaultIsValid(value);
23+
if (!isValid) {
24+
try {
25+
Double.parseDouble(value.toString());
26+
return true;
27+
} catch (Exception ex) {
28+
return false;
29+
}
30+
}
31+
return true;
2732
}
2833

2934
@Override
3035
public boolean isValid(final Object value, final boolean useStrictValidation) {
31-
return super.defaultIsValid(value, objectMapper, useStrictValidation);
36+
if (!useStrictValidation) {
37+
return isValid(value);
38+
}
39+
return super.defaultIsValid(value);
3240
}
3341

3442
@Override
3543
public Optional<Double> getValue(Object value) {
36-
return defaultGetValue(value, objectMapper);
44+
final Optional<Double> result = defaultGetValue(value);
45+
if (result.isPresent()) {
46+
return result;
47+
}
48+
try {
49+
return Optional.of(Double.parseDouble(value.toString()));
50+
} catch (final Exception ignored) {
51+
}
52+
return Optional.empty();
3753
}
3854
}
Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
package com.github.sidhant92.boolparser.datatype;
22

33
import java.util.Optional;
4-
import com.fasterxml.jackson.databind.ObjectMapper;
54
import com.github.sidhant92.boolparser.constant.DataType;
65

76
/**
87
* @author sidhant.aggarwal
98
* @since 05/03/2023
109
*/
1110
public class IntegerDataType extends AbstractDataType<Integer> {
12-
private final ObjectMapper objectMapper;
13-
14-
public IntegerDataType(final ObjectMapper objectMapper) {
11+
public IntegerDataType() {
1512
super(Integer.class);
16-
this.objectMapper = objectMapper;
1713
}
1814

1915
@Override
@@ -23,16 +19,36 @@ public DataType getDataType() {
2319

2420
@Override
2521
public boolean isValid(final Object value) {
26-
return super.defaultIsValid(value, objectMapper);
22+
boolean isValid = super.defaultIsValid(value);
23+
if (!isValid) {
24+
try {
25+
Integer.parseInt(value.toString());
26+
return true;
27+
} catch (Exception ex) {
28+
return false;
29+
}
30+
}
31+
return true;
2732
}
2833

2934
@Override
3035
public boolean isValid(final Object value, final boolean useStrictValidation) {
31-
return super.defaultIsValid(value, objectMapper, useStrictValidation);
36+
if (!useStrictValidation) {
37+
return isValid(value);
38+
}
39+
return super.defaultIsValid(value);
3240
}
3341

3442
@Override
3543
public Optional<Integer> getValue(Object value) {
36-
return defaultGetValue(value, objectMapper);
44+
final Optional<Integer> result = defaultGetValue(value);
45+
if (result.isPresent()) {
46+
return result;
47+
}
48+
try {
49+
return Optional.of(Integer.parseInt(value.toString()));
50+
} catch (final Exception ignored) {
51+
}
52+
return Optional.empty();
3753
}
3854
}
Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
package com.github.sidhant92.boolparser.datatype;
22

33
import java.util.Optional;
4-
import com.fasterxml.jackson.databind.ObjectMapper;
54
import com.github.sidhant92.boolparser.constant.DataType;
65

76
/**
87
* @author sidhant.aggarwal
98
* @since 05/03/2023
109
*/
1110
public class LongDataType extends AbstractDataType<Long> {
12-
private final ObjectMapper objectMapper;
13-
14-
public LongDataType(final ObjectMapper objectMapper) {
11+
public LongDataType() {
1512
super(Long.class);
16-
this.objectMapper = objectMapper;
1713
}
1814

1915
@Override
@@ -23,16 +19,36 @@ public DataType getDataType() {
2319

2420
@Override
2521
public boolean isValid(final Object value) {
26-
return super.defaultIsValid(value, objectMapper);
22+
boolean isValid = super.defaultIsValid(value);
23+
if (!isValid) {
24+
try {
25+
Long.parseLong(value.toString());
26+
return true;
27+
} catch (Exception ex) {
28+
return false;
29+
}
30+
}
31+
return true;
2732
}
2833

2934
@Override
3035
public boolean isValid(final Object value, final boolean useStrictValidation) {
31-
return super.defaultIsValid(value, objectMapper, useStrictValidation);
36+
if (!useStrictValidation) {
37+
return isValid(value);
38+
}
39+
return super.defaultIsValid(value);
3240
}
3341

3442
@Override
3543
public Optional<Long> getValue(Object value) {
36-
return defaultGetValue(value, objectMapper);
44+
final Optional<Long> result = defaultGetValue(value);
45+
if (result.isPresent()) {
46+
return result;
47+
}
48+
try {
49+
return Optional.of(Long.parseLong(value.toString()));
50+
} catch (final Exception ignored) {
51+
}
52+
return Optional.empty();
3753
}
3854
}
Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
package com.github.sidhant92.boolparser.datatype;
22

33
import java.util.Optional;
4-
import com.fasterxml.jackson.databind.ObjectMapper;
54
import com.github.sidhant92.boolparser.constant.DataType;
65

76
/**
87
* @author sidhant.aggarwal
98
* @since 05/03/2023
109
*/
1110
public class StringDataType extends AbstractDataType<String> {
12-
private final ObjectMapper objectMapper;
13-
14-
public StringDataType(final ObjectMapper objectMapper) {
11+
public StringDataType() {
1512
super(String.class);
16-
this.objectMapper = objectMapper;
1713
}
1814

1915
@Override
@@ -23,16 +19,23 @@ public DataType getDataType() {
2319

2420
@Override
2521
public boolean isValid(final Object value) {
26-
return super.defaultIsValid(value, objectMapper);
22+
return true;
2723
}
2824

2925
@Override
3026
public boolean isValid(final Object value, final boolean useStrictValidation) {
31-
return super.defaultIsValid(value, objectMapper, useStrictValidation);
27+
if (!useStrictValidation) {
28+
return isValid(value);
29+
}
30+
return super.defaultIsValid(value);
3231
}
3332

3433
@Override
3534
public Optional<String> getValue(Object value) {
36-
return defaultGetValue(value, objectMapper);
35+
final Optional<String> result = defaultGetValue(value);
36+
if (result.isPresent()) {
37+
return result;
38+
}
39+
return Optional.of(value.toString());
3740
}
3841
}

0 commit comments

Comments
 (0)