Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/logisland 592 #593

Merged
merged 5 commits into from
Sep 17, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.hurence.logisland.record.FieldDictionary;
import com.hurence.logisland.record.Record;
import com.hurence.logisland.record.RecordUtils;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.joda.time.format.DateTimeFormatter;
Expand Down Expand Up @@ -112,7 +113,7 @@ static String convertToString(Record record, String geolocationFieldLabel) {
document.field(fieldName, field.asBoolean().booleanValue());
break;
case RECORD:
Map<String, Object> map = toMap(field.asRecord(), true);
Map<String, Object> map = RecordUtils.toMap(field.asRecord(), true);
document.field(fieldName, map);
break;
case ARRAY:
Expand All @@ -135,7 +136,7 @@ static String convertToString(Record record, String geolocationFieldLabel) {
final List list = new ArrayList(collection.size());
for (final Object item : collection) {
if (item instanceof Record) {
list.add(toMap((Record) item, true));
list.add(RecordUtils.toMap((Record) item, true));
} else {
list.add(item);
}
Expand Down Expand Up @@ -175,65 +176,6 @@ static String convertToString(Record record, String geolocationFieldLabel) {
* @return the conversion of a record to a map where all {@code null} values were removed.
*/
private static Map<String, Object> toMap(final Record record) {
return toMap(record, false);
return RecordUtils.toMap(record, false);
}

/**
* Returns the conversion of a record to a map where all {@code null} values were removed.
*
* @param record the record to convert.
* @param filterInnerRecord if {@code true} special dictionnary fields are ignored; included otherwise.
*
* @return the conversion of a record to a map where all {@code null} values were removed.
*/
private static Map<String, Object> toMap(final Record record,
final boolean filterInnerRecord) {
try {
final Map<String, Object> result = new HashMap<>();

record.getFieldsEntrySet()
.stream()
.forEach(entry ->
{
if (!filterInnerRecord || (filterInnerRecord && !FieldDictionary.contains(entry.getKey()))) {
Object value = entry.getValue().getRawValue();
if (value != null) {
switch (entry.getValue().getType()) {
case RECORD:
value = toMap((Record) value, true);
break;
case ARRAY:
Collection collection;
if (value.getClass().isArray()) {
collection = new ArrayList<>();
for (int i = 0; i < Array.getLength(value); i++) {
collection.add(Array.get(value, i));
}
} else if (value instanceof Collection) {
collection = (Collection) value;
} else {
collection = Arrays.asList(value);
}
final List list = new ArrayList(collection.size());
for (final Object item : collection) {
if (item instanceof Record) {
list.add(toMap((Record) item, true));
} else {
list.add(item);
}
}
value = list;
break;
default:
}
result.put(entry.getKey(), value);
}
}
});
return result;
} catch (Exception e) {
throw new RuntimeException(e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
*/
package com.hurence.logisland.record;

import java.lang.reflect.Array;
import java.util.*;

public final class RecordUtils {


public static Record getKeyValueRecord(String key, String value) {
final Record record = new StandardRecord("kv_record");
record.setStringField(FieldDictionary.RECORD_KEY, key);
Expand All @@ -41,4 +42,62 @@ public static Record getRecordOfString(String... kvs) {
}
return record;
}

/**
* Returns the conversion of a record to a map where all {@code null} values were removed.
*
* @param record the record to convert.
* @param filterInnerRecord if {@code true} special dictionnary fields are ignored; included otherwise.
*
* @return the conversion of a record to a map where all {@code null} values were removed.
*/
public static Map<String, Object> toMap(final Record record,
final boolean filterInnerRecord) {
try {
final Map<String, Object> result = new HashMap<>();

record.getFieldsEntrySet()
.stream()
.forEach(entry ->
{
if (!filterInnerRecord || (filterInnerRecord && !FieldDictionary.contains(entry.getKey()))) {
Object value = entry.getValue().getRawValue();
if (value != null) {
switch (entry.getValue().getType()) {
case RECORD:
value = toMap((Record) value, true);
break;
case ARRAY:
Collection collection;
if (value.getClass().isArray()) {
collection = new ArrayList<>();
for (int i = 0; i < Array.getLength(value); i++) {
collection.add(Array.get(value, i));
}
} else if (value instanceof Collection) {
collection = (Collection) value;
} else {
collection = Arrays.asList(value);
}
final List list = new ArrayList(collection.size());
for (final Object item : collection) {
if (item instanceof Record) {
list.add(toMap((Record) item, true));
} else {
list.add(item);
}
}
value = list;
break;
default:
}
result.put(entry.getKey(), value);
}
}
});
return result;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
Loading