|
| 1 | +package dev.openfeature.contrib.providers.flagd.resolver.common; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.List; |
| 5 | +import java.util.Map; |
| 6 | +import java.util.stream.Collectors; |
| 7 | + |
| 8 | +import com.google.protobuf.Descriptors; |
| 9 | +import com.google.protobuf.ListValue; |
| 10 | +import com.google.protobuf.Message; |
| 11 | +import com.google.protobuf.NullValue; |
| 12 | +import com.google.protobuf.Struct; |
| 13 | + |
| 14 | +import dev.openfeature.sdk.EvaluationContext; |
| 15 | +import dev.openfeature.sdk.MutableStructure; |
| 16 | +import dev.openfeature.sdk.Structure; |
| 17 | +import dev.openfeature.sdk.Value; |
| 18 | + |
| 19 | +/** |
| 20 | + * gRPC type conversion utils. |
| 21 | + */ |
| 22 | +public class Convert { |
| 23 | + /** |
| 24 | + * Recursively convert protobuf structure to openfeature value. |
| 25 | + */ |
| 26 | + public static Value convertObjectResponse(Struct protobuf) { |
| 27 | + return convertProtobufMap(protobuf.getFieldsMap()); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Recursively convert the Evaluation context to a protobuf structure. |
| 32 | + */ |
| 33 | + public static Struct convertContext(EvaluationContext ctx) { |
| 34 | + Map<String, Value> ctxMap = ctx.asMap(); |
| 35 | + // asMap() does not provide explicitly set targeting key (ex:- new |
| 36 | + // ImmutableContext("TargetingKey") ). |
| 37 | + // Hence, we add this explicitly here for targeting rule processing. |
| 38 | + ctxMap.put("targetingKey", new Value(ctx.getTargetingKey())); |
| 39 | + |
| 40 | + return convertMap(ctxMap).getStructValue(); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Convert any openfeature value to a protobuf value. |
| 45 | + */ |
| 46 | + public static com.google.protobuf.Value convertAny(Value value) { |
| 47 | + if (value.isList()) { |
| 48 | + return convertList(value.asList()); |
| 49 | + } else if (value.isStructure()) { |
| 50 | + return convertMap(value.asStructure().asMap()); |
| 51 | + } else { |
| 52 | + return convertPrimitive(value); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Convert any protobuf value to {@link Value}. |
| 58 | + */ |
| 59 | + public static Value convertAny(com.google.protobuf.Value protobuf) { |
| 60 | + if (protobuf.hasListValue()) { |
| 61 | + return convertList(protobuf.getListValue()); |
| 62 | + } else if (protobuf.hasStructValue()) { |
| 63 | + return convertProtobufMap(protobuf.getStructValue().getFieldsMap()); |
| 64 | + } else { |
| 65 | + return convertPrimitive(protobuf); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Convert OpenFeature map to protobuf {@link com.google.protobuf.Value}. |
| 71 | + */ |
| 72 | + public static com.google.protobuf.Value convertMap(Map<String, Value> map) { |
| 73 | + Map<String, com.google.protobuf.Value> values = new HashMap<>(); |
| 74 | + |
| 75 | + map.keySet().forEach((String key) -> { |
| 76 | + Value value = map.get(key); |
| 77 | + values.put(key, convertAny(value)); |
| 78 | + }); |
| 79 | + Struct struct = Struct.newBuilder() |
| 80 | + .putAllFields(values).build(); |
| 81 | + return com.google.protobuf.Value.newBuilder().setStructValue(struct).build(); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Convert protobuf map with {@link com.google.protobuf.Value} to OpenFeature |
| 86 | + * map. |
| 87 | + */ |
| 88 | + public static Value convertProtobufMap(Map<String, com.google.protobuf.Value> map) { |
| 89 | + return new Value(convertProtobufMapToStructure(map)); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Convert protobuf map with {@link com.google.protobuf.Value} to OpenFeature |
| 94 | + * map. |
| 95 | + */ |
| 96 | + public static Structure convertProtobufMapToStructure(Map<String, com.google.protobuf.Value> map) { |
| 97 | + Map<String, Value> values = new HashMap<>(); |
| 98 | + |
| 99 | + map.keySet().forEach((String key) -> { |
| 100 | + com.google.protobuf.Value value = map.get(key); |
| 101 | + values.put(key, convertAny(value)); |
| 102 | + }); |
| 103 | + return new MutableStructure(values); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Convert OpenFeature list to protobuf {@link com.google.protobuf.Value}. |
| 108 | + */ |
| 109 | + public static com.google.protobuf.Value convertList(List<Value> values) { |
| 110 | + ListValue list = ListValue.newBuilder() |
| 111 | + .addAllValues(values.stream() |
| 112 | + .map(v -> convertAny(v)).collect(Collectors.toList())) |
| 113 | + .build(); |
| 114 | + return com.google.protobuf.Value.newBuilder().setListValue(list).build(); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Convert protobuf list to OpenFeature {@link com.google.protobuf.Value}. |
| 119 | + */ |
| 120 | + public static Value convertList(ListValue protobuf) { |
| 121 | + return new Value(protobuf.getValuesList().stream().map(p -> convertAny(p)).collect(Collectors.toList())); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Convert OpenFeature {@link Value} to protobuf |
| 126 | + * {@link com.google.protobuf.Value}. |
| 127 | + */ |
| 128 | + public static com.google.protobuf.Value convertPrimitive(Value value) { |
| 129 | + com.google.protobuf.Value.Builder builder = com.google.protobuf.Value.newBuilder(); |
| 130 | + |
| 131 | + if (value.isBoolean()) { |
| 132 | + builder.setBoolValue(value.asBoolean()); |
| 133 | + } else if (value.isString()) { |
| 134 | + builder.setStringValue(value.asString()); |
| 135 | + } else if (value.isNumber()) { |
| 136 | + builder.setNumberValue(value.asDouble()); |
| 137 | + } else { |
| 138 | + builder.setNullValue(NullValue.NULL_VALUE); |
| 139 | + } |
| 140 | + return builder.build(); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Convert protobuf {@link com.google.protobuf.Value} to OpenFeature |
| 145 | + * {@link Value}. |
| 146 | + */ |
| 147 | + public static Value convertPrimitive(com.google.protobuf.Value protobuf) { |
| 148 | + final Value value; |
| 149 | + if (protobuf.hasBoolValue()) { |
| 150 | + value = new Value(protobuf.getBoolValue()); |
| 151 | + } else if (protobuf.hasStringValue()) { |
| 152 | + value = new Value(protobuf.getStringValue()); |
| 153 | + } else if (protobuf.hasNumberValue()) { |
| 154 | + value = new Value(protobuf.getNumberValue()); |
| 155 | + } else { |
| 156 | + value = new Value(); |
| 157 | + } |
| 158 | + |
| 159 | + return value; |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Get the specified protobuf field from the message. |
| 164 | + * |
| 165 | + * @param <T> type |
| 166 | + * @param message protobuf message |
| 167 | + * @param name field name |
| 168 | + * @return field value |
| 169 | + */ |
| 170 | + public static <T> T getField(Message message, String name) { |
| 171 | + return (T) message.getField(getFieldDescriptor(message, name)); |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Get the specified protobuf field descriptor from the message. |
| 176 | + * |
| 177 | + * @param message protobuf message |
| 178 | + * @param name field name |
| 179 | + * @return field descriptor |
| 180 | + */ |
| 181 | + public static Descriptors.FieldDescriptor getFieldDescriptor(Message message, String name) { |
| 182 | + return message.getDescriptorForType().findFieldByName(name); |
| 183 | + } |
| 184 | +} |
0 commit comments