Skip to content

Commit bf01e16

Browse files
authored
fix: RedisEntity value fixes (#67)
* feat: protoparsedmessage.getfieldbyname should return ProtoField * tests: adding unit tests * tests: unit tests * test: fix tests * feat: add json friendly strings for default * fix: mapfield with message * fix: simplify string conversion * feat: add list json * tests: default fields * test: add jsonassert * fix: remove extra code * chore: version bump * fix: template should also parse complex data
1 parent d2c3d2d commit bf01e16

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+916
-194
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ plugins {
2222
}
2323

2424
group 'io.odpf'
25-
version '0.3.4'
25+
version '0.3.5'
2626

2727
repositories {
2828
mavenCentral()

src/main/java/io/odpf/depot/bigtable/parser/BigTableRecordParser.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010
import io.odpf.depot.exception.DeserializerException;
1111
import io.odpf.depot.exception.EmptyMessageException;
1212
import io.odpf.depot.message.OdpfMessage;
13-
import io.odpf.depot.message.ParsedOdpfMessage;
14-
import io.odpf.depot.message.OdpfMessageSchema;
1513
import io.odpf.depot.message.OdpfMessageParser;
14+
import io.odpf.depot.message.OdpfMessageSchema;
15+
import io.odpf.depot.message.ParsedOdpfMessage;
1616
import io.odpf.depot.message.SinkConnectorSchemaMessageMode;
17+
import io.odpf.depot.message.field.GenericFieldFactory;
1718
import lombok.extern.slf4j.Slf4j;
1819

1920
import java.io.IOException;
@@ -61,7 +62,7 @@ private BigTableRecord createRecord(OdpfMessage message, long index) {
6162
.getColumns(columnFamily)
6263
.forEach(column -> {
6364
String fieldName = bigTableSchema.getField(columnFamily, column);
64-
String value = String.valueOf(parsedOdpfMessage.getFieldByName(fieldName, schema));
65+
String value = GenericFieldFactory.getField(parsedOdpfMessage.getFieldByName(fieldName, schema)).getString();
6566
rowMutationEntry.setCell(columnFamily, column, value);
6667
}));
6768
BigTableRecord bigTableRecord = new BigTableRecord(rowMutationEntry, index, null, message.getMetadata());

src/main/java/io/odpf/depot/common/Template.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import io.odpf.depot.exception.InvalidTemplateException;
55
import io.odpf.depot.message.OdpfMessageSchema;
66
import io.odpf.depot.message.ParsedOdpfMessage;
7+
import io.odpf.depot.message.field.GenericFieldFactory;
8+
import io.odpf.depot.message.proto.converter.fields.ProtoField;
79
import io.odpf.depot.utils.StringUtils;
810

911
import java.util.ArrayList;
@@ -36,8 +38,16 @@ private void validate() throws InvalidTemplateException {
3638
public String parse(ParsedOdpfMessage parsedOdpfMessage, OdpfMessageSchema schema) {
3739
Object[] patternVariableData = patternVariableFieldNames
3840
.stream()
39-
.map(fieldName -> parsedOdpfMessage.getFieldByName(fieldName, schema))
41+
.map(fieldName -> fetchInternalValue(parsedOdpfMessage.getFieldByName(fieldName, schema)))
4042
.toArray();
4143
return String.format(templatePattern, patternVariableData);
4244
}
45+
46+
private Object fetchInternalValue(Object ob) {
47+
if (ob instanceof ProtoField) {
48+
return GenericFieldFactory.getField(ob).getString();
49+
} else {
50+
return ob;
51+
}
52+
}
4353
}

src/main/java/io/odpf/depot/exception/EmptyMessageException.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ public EmptyMessageException() {
88
super("log message is empty");
99
}
1010
}
11+
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package io.odpf.depot.message.field;
2+
3+
import com.google.gson.Gson;
4+
import com.google.gson.GsonBuilder;
5+
6+
import java.util.Collection;
7+
import java.util.function.Function;
8+
import java.util.stream.Collectors;
9+
10+
public class FieldUtils {
11+
private static final Gson GSON = new GsonBuilder().create();
12+
13+
public static String convertToStringForMessageTypes(Object value, Function<Object, String> toStringFunc) {
14+
if (value instanceof Collection<?>) {
15+
return "[" + ((Collection<?>) value)
16+
.stream()
17+
.map(toStringFunc)
18+
.collect(Collectors.joining(",")) + "]";
19+
}
20+
return toStringFunc.apply(value);
21+
}
22+
23+
/**
24+
* This method is used to convert default types which string formats are not in json.
25+
* for example: a list of doubles, strings, enum etc.
26+
*/
27+
public static String convertToString(Object value) {
28+
if (value instanceof Collection<?>) {
29+
return GSON.toJson(value);
30+
} else {
31+
return value.toString();
32+
}
33+
}
34+
35+
public static String convertToStringForSpecialTypes(Object value, Function<Object, String> toStringFunc) {
36+
if (value instanceof Collection<?>) {
37+
return GSON.toJson(((Collection<?>) value)
38+
.stream()
39+
.map(toStringFunc)
40+
.collect(Collectors.toList()));
41+
}
42+
return toStringFunc.apply(value);
43+
}
44+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package io.odpf.depot.message.field;
2+
3+
public interface GenericField {
4+
String getString();
5+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package io.odpf.depot.message.field;
2+
3+
import io.odpf.depot.message.field.json.JsonFieldFactory;
4+
import io.odpf.depot.message.field.proto.ProtoFieldFactory;
5+
import io.odpf.depot.message.proto.converter.fields.ProtoField;
6+
7+
public class GenericFieldFactory {
8+
9+
public static GenericField getField(Object field) {
10+
if (field instanceof ProtoField) {
11+
return ProtoFieldFactory.getField((ProtoField) field);
12+
} else {
13+
return JsonFieldFactory.getField(field);
14+
}
15+
16+
}
17+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package io.odpf.depot.message.field.json;
2+
3+
import io.odpf.depot.message.field.GenericField;
4+
5+
public class DefaultField implements GenericField {
6+
private final Object value;
7+
8+
public DefaultField(Object field) {
9+
this.value = field;
10+
}
11+
12+
@Override
13+
public String getString() {
14+
return value.toString();
15+
}
16+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package io.odpf.depot.message.field.json;
2+
3+
import io.odpf.depot.message.field.GenericField;
4+
5+
public class JsonFieldFactory {
6+
public static GenericField getField(Object field) {
7+
return new DefaultField(field);
8+
}
9+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package io.odpf.depot.message.field.proto;
2+
3+
import io.odpf.depot.message.field.FieldUtils;
4+
import io.odpf.depot.message.field.GenericField;
5+
6+
public class DefaultField implements GenericField {
7+
private final Object value;
8+
9+
public DefaultField(Object value) {
10+
this.value = value;
11+
}
12+
13+
@Override
14+
public String getString() {
15+
return FieldUtils.convertToString(value);
16+
}
17+
}

0 commit comments

Comments
 (0)