Skip to content

Commit

Permalink
Merge pull request #57 from trocco-io/issues/28789-fix-bug-seed_user_…
Browse files Browse the repository at this point in the history
…list_ids-throws-error

fix bug: `user_list.lookalike_user_list.seed_user_list_ids` throws error
  • Loading branch information
kentoyoshida authored Nov 26, 2024
2 parents 3adca23 + 83fb4ed commit b326418
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions src/main/java/org/embulk/input/google_ads/GoogleAdsReporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Arrays;
import java.util.Set;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -119,6 +122,10 @@ public String convertLeafNodeValue(Map<Descriptors.FieldDescriptor, Object> fiel
return convertMessageType(key, fields);
} else if (key.getType() == Descriptors.FieldDescriptor.Type.ENUM) {
return convertEnumType(key, fields);
} else if (isDoubleType(key.getJavaType())) {
return convertNumericType(key, fields, Double.class);
} else if (isLongType(key.getJavaType())) {
return convertNumericType(key, fields, Long.class);
}
return convertNonMessageType(key, fields);
}
Expand Down Expand Up @@ -159,6 +166,35 @@ public String convertNonMessageType(Descriptors.FieldDescriptor key, Map<Descrip
}
}

public <T extends Number> String convertNumericType(Descriptors.FieldDescriptor key, Map<Descriptors.FieldDescriptor, Object> fields, Class<T> type)
{
if (type != Long.class && type != Double.class) {
throw new IllegalArgumentException("Unsupported type: " + type);
}

if (key.isRepeated()) {
List<T> values = (List<T>) fields.get(key);
ArrayNode arrayNode = mapper.createArrayNode();
if (type == Long.class) {
for (Long val : (List<Long>) values) {
arrayNode.add(val);
}
} else {
for (Double val : (List<Double>) values) {
arrayNode.add(val);
}
}

try {
return mapper.writeValueAsString(arrayNode);
} catch (JsonProcessingException ignored) {
return null;
}
}

return String.valueOf(fields.get(key));
}

public String convertMessageType(Descriptors.FieldDescriptor key, Map<Descriptors.FieldDescriptor, Object> fields)
{
if (key.isRepeated()) {
Expand Down Expand Up @@ -387,4 +423,24 @@ private String buildWhereClauseConditionsForChangeEvent(String startDateTime)

return dateSb.toString();
}

private boolean isDoubleType(Descriptors.FieldDescriptor.JavaType type)
{
Set<String> javaTypeNumber = new HashSet<>(Arrays.asList(
Descriptors.FieldDescriptor.JavaType.FLOAT.name(),
Descriptors.FieldDescriptor.JavaType.DOUBLE.name()
));

return javaTypeNumber.contains(type.name());
}

private boolean isLongType(Descriptors.FieldDescriptor.JavaType type)
{
Set<String> javaTypeNumber = new HashSet<>(Arrays.asList(
Descriptors.FieldDescriptor.JavaType.INT.name(),
Descriptors.FieldDescriptor.JavaType.LONG.name()
));

return javaTypeNumber.contains(type.name());
}
}

0 comments on commit b326418

Please sign in to comment.