Skip to content

Commit

Permalink
feat: add validate logic in getMetaByMap
Browse files Browse the repository at this point in the history
  • Loading branch information
JustKode committed Aug 5, 2023
1 parent 892b5c2 commit 9030bae
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 32 deletions.
33 changes: 27 additions & 6 deletions src/main/java/justkode/kafka/event/producer/meta/DoubleMeta.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static java.lang.Math.abs;

Expand Down Expand Up @@ -41,15 +42,35 @@ public Double getRandomValue() {

public static DoubleMeta getMetaByMap(String key, Map<String, Object> map) {
Boolean isManual = (Boolean) map.get("is_manual");
List<Double> manualValues = (List<Double>) map.get("manual_doubles");
Double minValue = (Double) map.get("min_value");
Double maxValue = (Double) map.get("max_value");
List<Double> inputManualValues = (List<Double>) map.get("manual_values");
Double inputMinValue = (Double) map.get("min_value");
Double inputMaxValue = (Double) map.get("max_value");
DoubleMeta doubleMeta;

if (isManual && manualValues == null) {
throw new RuntimeException("manual_integers doesn't exists.");
if (isManual) {
if (inputManualValues == null) {
throw new RuntimeException("manual_values of " + key + " doesn't exists.");
}

doubleMeta = DoubleMeta.builder()
.isManual(true)
.manualValues(inputManualValues)
.build();
} else {
if (inputMinValue == null) {
throw new RuntimeException("min_value of " + key + " doesn't exists.");
}
else if (inputMaxValue == null) {
throw new RuntimeException("min_value of " + key + " doesn't exists.");
}

doubleMeta = DoubleMeta.builder()
.isManual(false)
.minValue(inputMinValue)
.maxValue(inputMaxValue)
.build();
}

DoubleMeta doubleMeta = new DoubleMeta(isManual, manualValues, minValue, maxValue);
doubleMeta.validCheck(key);
return doubleMeta;
}
Expand Down
38 changes: 29 additions & 9 deletions src/main/java/justkode/kafka/event/producer/meta/FloatMeta.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static java.lang.Math.abs;

Expand Down Expand Up @@ -40,17 +41,36 @@ public Float getRandomValue() {

public static FloatMeta getMetaByMap(String key, Map<String, Object> map) {
Boolean isManual = (Boolean) map.get("is_manual");
List<Float> manualValues = (List<Float>) map.get("manual_values");
Float minValue = (Float) map.get("min_value");
Float maxValue = (Float) map.get("max_value");
List<Double> inputManualValues = (List<Double>) map.get("manual_values");
Double inputMinValue = (Double) map.get("min_value");
Double inputMaxValue = (Double) map.get("max_value");
FloatMeta floatMeta;

if (isManual && manualValues == null) {
throw new RuntimeException("manual_integers doesn't exists.");
}
if (isManual) {
if (inputManualValues == null) {
throw new RuntimeException("manual_values of " + key + " doesn't exists.");
}

FloatMeta doubleMeta = new FloatMeta(isManual, manualValues, minValue, maxValue);
doubleMeta.validCheck(key);
floatMeta = FloatMeta.builder()
.isManual(true)
.manualValues(inputManualValues.stream().map(Double::floatValue).collect(Collectors.toList()))
.build();
} else {
if (inputMinValue == null) {
throw new RuntimeException("min_value of " + key + " doesn't exists.");
}
else if (inputMaxValue == null) {
throw new RuntimeException("min_value of " + key + " doesn't exists.");
}

floatMeta = FloatMeta.builder()
.isManual(false)
.minValue(inputMinValue.floatValue())
.maxValue(inputMaxValue.floatValue())
.build();
}

return doubleMeta;
floatMeta.validCheck(key);
return floatMeta;
}
}
35 changes: 28 additions & 7 deletions src/main/java/justkode/kafka/event/producer/meta/IntegerMeta.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Builder
@Getter
Expand Down Expand Up @@ -39,16 +40,36 @@ public Integer getRandomValue() {

public static IntegerMeta getMetaByMap(String key, Map<String, Object> map) {
Boolean isManual = (Boolean) map.get("is_manual");
List<Integer> manualValues = (List<Integer>) map.get("manual_values");
Integer minValue = (Integer) map.get("min_value");
Integer maxValue = (Integer) map.get("max_value");
List<Double> inputManualValues = (List<Double>) map.get("manual_values");
Double inputMinValue = (Double) map.get("min_value");
Double inputMaxValue = (Double) map.get("max_value");
IntegerMeta integerMeta;

if (isManual && manualValues == null) {
throw new RuntimeException("manual_values doesn't exists.");
if (isManual) {
if (inputManualValues == null) {
throw new RuntimeException("manual_values of " + key + " doesn't exists.");
}

integerMeta = IntegerMeta.builder()
.isManual(true)
.manualValues(inputManualValues.stream().map(Double::intValue).collect(Collectors.toList()))
.build();
} else {
if (inputMinValue == null) {
throw new RuntimeException("min_value of " + key + " doesn't exists.");
}
else if (inputMaxValue == null) {
throw new RuntimeException("min_value of " + key + " doesn't exists.");
}

integerMeta = IntegerMeta.builder()
.isManual(false)
.minValue(inputMinValue.intValue())
.maxValue(inputMaxValue.intValue())
.build();
}
IntegerMeta integerMeta = new IntegerMeta(isManual, manualValues, minValue, maxValue);
integerMeta.validCheck(key);

integerMeta.validCheck(key);
return integerMeta;
}
}
35 changes: 28 additions & 7 deletions src/main/java/justkode/kafka/event/producer/meta/LongMeta.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static java.lang.Math.abs;

Expand Down Expand Up @@ -41,16 +42,36 @@ public Long getRandomValue() {

public static LongMeta getMetaByMap(String key, Map<String, Object> map) {
Boolean isManual = (Boolean) map.get("is_manual");
List<Long> manualValues = (List<Long>) map.get("manual_values");
Long minValue = (Long) map.get("min_value");
Long maxValue = (Long) map.get("max_value");
List<Double> inputManualValues = (List<Double>) map.get("manual_values");
Double inputMinValue = (Double) map.get("min_value");
Double inputMaxValue = (Double) map.get("max_value");
LongMeta longMeta;

if (isManual && manualValues == null) {
throw new RuntimeException("manual_integers doesn't exists.");
if (isManual) {
if (inputManualValues == null) {
throw new RuntimeException("manual_values of " + key + " doesn't exists.");
}

longMeta = LongMeta.builder()
.isManual(true)
.manualValues(inputManualValues.stream().map(Double::longValue).collect(Collectors.toList()))
.build();
} else {
if (inputMinValue == null) {
throw new RuntimeException("min_value of " + key + " doesn't exists.");
}
else if (inputMaxValue == null) {
throw new RuntimeException("min_value of " + key + " doesn't exists.");
}

longMeta = LongMeta.builder()
.isManual(false)
.minValue(inputMinValue.longValue())
.maxValue(inputMaxValue.longValue())
.build();
}
LongMeta longMeta = new LongMeta(isManual, manualValues, minValue, maxValue);
longMeta.validCheck(key);

longMeta.validCheck(key);
return longMeta;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ public String getRandomValue() {
public static StringMeta getMetaByMap(String key, Map<String, Object> map) {
Boolean isManual = (Boolean) map.get("is_manual");
List<String> manualValues = (List<String>) map.get("manual_values");
Integer minLength = (Integer) map.get("min_length");
Integer maxLength = (Integer) map.get("max_length");
Double minLength = (Double) map.get("min_length");
Double maxLength = (Double) map.get("max_length");

if (isManual && manualValues == null) {
throw new RuntimeException("manual_integers doesn't exists.");
}
StringMeta stringMeta = new StringMeta(isManual, manualValues, minLength, maxLength);
StringMeta stringMeta = new StringMeta(isManual, manualValues, minLength.intValue(), maxLength.intValue());
stringMeta.validCheck(key);

return stringMeta;
Expand Down

0 comments on commit 9030bae

Please sign in to comment.