|
| 1 | +package io.odpf.depot.config.converter; |
| 2 | + |
| 3 | +import com.google.common.base.Strings; |
| 4 | +import com.google.gson.Gson; |
| 5 | +import com.google.gson.reflect.TypeToken; |
| 6 | + |
| 7 | +import java.lang.reflect.Method; |
| 8 | +import java.lang.reflect.Type; |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.HashSet; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Map; |
| 13 | +import java.util.Properties; |
| 14 | +import java.util.Set; |
| 15 | +import java.util.function.Consumer; |
| 16 | +import java.util.stream.Stream; |
| 17 | + |
| 18 | + |
| 19 | +public class JsonToPropertiesConverter implements org.aeonbits.owner.Converter<Properties> { |
| 20 | + private static final Gson GSON = new Gson(); |
| 21 | + |
| 22 | + @Override |
| 23 | + public Properties convert(Method method, String input) { |
| 24 | + if (Strings.isNullOrEmpty(input)) { |
| 25 | + return null; |
| 26 | + } |
| 27 | + Type type = new TypeToken<Map<String, Object>>() { |
| 28 | + }.getType(); |
| 29 | + Map<String, Object> m = GSON.fromJson(input, type); |
| 30 | + Properties properties = getProperties(m); |
| 31 | + validate(properties); |
| 32 | + return properties; |
| 33 | + } |
| 34 | + |
| 35 | + private Properties getProperties(Map<String, Object> inputMap) { |
| 36 | + Properties properties = new Properties(); |
| 37 | + for (String key : inputMap.keySet()) { |
| 38 | + Object value = inputMap.get(key); |
| 39 | + if (value instanceof String) { |
| 40 | + properties.put(key, value); |
| 41 | + } else if (value instanceof Map) { |
| 42 | + properties.put(key, getProperties((Map) value)); |
| 43 | + } |
| 44 | + } |
| 45 | + return properties; |
| 46 | + } |
| 47 | + |
| 48 | + private void validate(Properties properties) { |
| 49 | + DuplicateFinder duplicateFinder = flattenValues(properties) |
| 50 | + .collect(DuplicateFinder::new, DuplicateFinder::accept, DuplicateFinder::combine); |
| 51 | + if (duplicateFinder.duplicates.size() > 0) { |
| 52 | + throw new IllegalArgumentException("duplicates found in SINK_REDIS_HASHSET_FIELD_TO_COLUMN_MAPPING for : " + duplicateFinder.duplicates); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + private Stream<String> flattenValues(Properties properties) { |
| 57 | + return properties |
| 58 | + .values() |
| 59 | + .stream() |
| 60 | + .flatMap(v -> { |
| 61 | + if (v instanceof String) { |
| 62 | + return Stream.of((String) v); |
| 63 | + } else if (v instanceof Properties) { |
| 64 | + return flattenValues((Properties) v); |
| 65 | + } else { |
| 66 | + return Stream.empty(); |
| 67 | + } |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + private static class DuplicateFinder implements Consumer<String> { |
| 72 | + private final Set<String> processedValues = new HashSet<>(); |
| 73 | + private final List<String> duplicates = new ArrayList<>(); |
| 74 | + |
| 75 | + @Override |
| 76 | + public void accept(String o) { |
| 77 | + if (processedValues.contains(o)) { |
| 78 | + duplicates.add(o); |
| 79 | + } else { |
| 80 | + processedValues.add(o); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + void combine(DuplicateFinder other) { |
| 85 | + other.processedValues |
| 86 | + .forEach(v -> { |
| 87 | + if (processedValues.contains(v)) { |
| 88 | + duplicates.add(v); |
| 89 | + } else { |
| 90 | + processedValues.add(v); |
| 91 | + } |
| 92 | + }); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments