Skip to content

Commit

Permalink
fix: Get Timestamp from String value. (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
EdwinBetanc0urt authored Jan 26, 2024
1 parent b086fb0 commit b6b31bd
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,16 @@ public static int getIntFromString(String stringValue) {
}
return integerValue;
}


/**
* Get String value from Int
* @param stringValue
* @return
*/
public static String getIntToString(int intValue) {
return String.valueOf(intValue);
}

public static BigDecimal convertFromValueToDecimal(Value value) {
if(value.hasStringValue()) {
return NumberManager.getBigDecimalFromString(
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/org/spin/service/grpc/util/value/TimeManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,22 @@ public static String getTimestampToString(Timestamp date) {
return new SimpleDateFormat(TIME_FORMAT).format(date);
}


public static Timestamp getTimestampFromLong(long value) {
if (value > 0) {
return new Timestamp(value);
}
return null;
}

public static Timestamp getTimestampFromDouble(double value) {
if (value > 0.0) {
return new Timestamp(
(int) value
);
}
return null;
}

public static Timestamp getTimestampFromInteger(int value) {
if (value > 0) {
return new Timestamp(value);
Expand Down Expand Up @@ -157,4 +165,5 @@ public static Timestamp convertValueToDate(com.google.protobuf.Timestamp value)
public static com.google.protobuf.Timestamp convertDateToValue(Timestamp value) {
return ValueManager.getTimestampFromDate(value);
}

}
50 changes: 31 additions & 19 deletions src/main/java/org/spin/service/grpc/util/value/ValueManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,6 @@ public static Timestamp getDateFromTimestampDate(com.google.protobuf.Timestamp d
return Timestamp.valueOf(dateTime);
}



/**
* Get Date from a value
* @param dateValue
Expand All @@ -326,28 +324,42 @@ public static Timestamp getTimestampFromValue(Value dateValue) {
|| !(dateValue.hasStringValue() || dateValue.hasNumberValue() || dateValue.hasStructValue())) {
return null;
}
Map<String, Value> values = dateValue.getStructValue().getFieldsMap();
if(values == null) {
return null;

if (dateValue.hasStructValue()) {
Map<String, Value> values = dateValue.getStructValue().getFieldsMap();
if(values == null) {
return null;
}
Value type = values.get(TYPE_KEY);
Value value = values.get(VALUE_KEY);
if(type == null || value == null) {
return null;
}
String validType = Optional.ofNullable(type.getStringValue()).orElse("");
String validValue = Optional.ofNullable(value.getStringValue()).orElse("");
if((!validType.equals(TYPE_DATE)
&& !validType.equals(TYPE_DATE_TIME))
|| validValue.length() == 0) {
return null;
}
return TimeManager.getTimestampFromString(
validValue
);
}
Value type = values.get(TYPE_KEY);
Value value = values.get(VALUE_KEY);
if(type == null
|| value == null) {
return null;
if (dateValue.hasStringValue()) {
return TimeManager.getTimestampFromString(
dateValue.getStringValue()
);
}
String validType = Optional.ofNullable(type.getStringValue()).orElse("");
String validValue = Optional.ofNullable(value.getStringValue()).orElse("");
if((!validType.equals(TYPE_DATE)
&& !validType.equals(TYPE_DATE_TIME))
|| validValue.length() == 0) {
return null;
if (dateValue.hasNumberValue()) {
return TimeManager.getTimestampFromDouble(
dateValue.getNumberValue()
);
}
return TimeManager.getTimestampFromString(
validValue
);
return null;
}


/**
* Get value from a date
* @param value
Expand Down

0 comments on commit b6b31bd

Please sign in to comment.