|
| 1 | +package fr.maif.openfeatures; |
| 2 | + |
| 3 | +import static dev.openfeature.sdk.Structure.mapToStructure; |
| 4 | +import static fr.maif.FeatureClientErrorStrategy.defaultValueStrategy; |
| 5 | + |
| 6 | +import java.math.BigDecimal; |
| 7 | +import java.time.Instant; |
| 8 | +import java.util.List; |
| 9 | +import java.util.Map; |
| 10 | +import java.util.Optional; |
| 11 | +import java.util.stream.Collectors; |
| 12 | + |
| 13 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 14 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 15 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 16 | + |
| 17 | +import dev.openfeature.sdk.EvaluationContext; |
| 18 | +import dev.openfeature.sdk.FeatureProvider; |
| 19 | +import dev.openfeature.sdk.Metadata; |
| 20 | +import dev.openfeature.sdk.ProviderEvaluation; |
| 21 | +import dev.openfeature.sdk.Structure; |
| 22 | +import dev.openfeature.sdk.Value; |
| 23 | +import dev.openfeature.sdk.exceptions.TypeMismatchError; |
| 24 | +import fr.maif.IzanamiClient; |
| 25 | +import fr.maif.requests.SingleFeatureRequest; |
| 26 | + |
| 27 | +public class IzanamiOpenFeatureProvider implements FeatureProvider { |
| 28 | + private final IzanamiClient izanamiClient; |
| 29 | + private final ObjectMapper mapper = new ObjectMapper(); |
| 30 | + |
| 31 | + public IzanamiOpenFeatureProvider(IzanamiClient izanamiClient) { |
| 32 | + this.izanamiClient = izanamiClient; |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public Metadata getMetadata() { |
| 37 | + return () -> "Izanami provider"; |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public void initialize(EvaluationContext evaluationContext) throws Exception {} |
| 42 | + |
| 43 | + @Override |
| 44 | + public void shutdown() { |
| 45 | + izanamiClient.close().join(); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public ProviderEvaluation<Boolean> getBooleanEvaluation(String key, Boolean defaultValue, EvaluationContext ctx) { |
| 50 | + var izanamiContext = IzanamiEvaluationContext.fromContext(ctx); |
| 51 | + var result = izanamiClient.booleanValue(SingleFeatureRequest.newSingleFeatureRequest(key) |
| 52 | + .withContext(izanamiContext.context) |
| 53 | + .withUser(izanamiContext.user) |
| 54 | + .withErrorStrategy(defaultValueStrategy(defaultValue, null, null)) |
| 55 | + ).join(); |
| 56 | + return ProviderEvaluation.<Boolean>builder().value(result).build(); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public ProviderEvaluation<String> getStringEvaluation(String key, String defaultValue, EvaluationContext ctx) { |
| 61 | + var izanamiContext = IzanamiEvaluationContext.fromContext(ctx); |
| 62 | + var result = izanamiClient.stringValue(SingleFeatureRequest.newSingleFeatureRequest(key) |
| 63 | + .withContext(izanamiContext.context) |
| 64 | + .withUser(izanamiContext.user) |
| 65 | + .withErrorStrategy(defaultValueStrategy(false, defaultValue, null)) |
| 66 | + ).join(); |
| 67 | + return ProviderEvaluation.<String>builder().value(result).build(); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public ProviderEvaluation<Integer> getIntegerEvaluation(String key, Integer defaultValue, EvaluationContext ctx) { |
| 72 | + var izanamiContext = IzanamiEvaluationContext.fromContext(ctx); |
| 73 | + var result = izanamiClient.numberValue(SingleFeatureRequest.newSingleFeatureRequest(key) |
| 74 | + .withContext(izanamiContext.context) |
| 75 | + .withUser(izanamiContext.user) |
| 76 | + .withErrorStrategy(defaultValueStrategy(false, null, BigDecimal.valueOf(defaultValue))) |
| 77 | + ).join(); |
| 78 | + return ProviderEvaluation.<Integer>builder().value(result.intValue()).build(); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public ProviderEvaluation<Double> getDoubleEvaluation(String key, Double defaultValue, EvaluationContext ctx) { |
| 83 | + var izanamiContext = IzanamiEvaluationContext.fromContext(ctx); |
| 84 | + var result = izanamiClient.numberValue(SingleFeatureRequest.newSingleFeatureRequest(key) |
| 85 | + .withContext(izanamiContext.context) |
| 86 | + .withUser(izanamiContext.user) |
| 87 | + .withErrorStrategy(defaultValueStrategy(false, null, BigDecimal.valueOf(defaultValue))) |
| 88 | + ).join(); |
| 89 | + return ProviderEvaluation.<Double>builder().value(result.doubleValue()).build(); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public ProviderEvaluation<Value> getObjectEvaluation(String key, Value defaultValue, EvaluationContext ctx) { |
| 94 | + var izanamiContext = IzanamiEvaluationContext.fromContext(ctx); |
| 95 | + var result = izanamiClient.stringValue(SingleFeatureRequest.newSingleFeatureRequest(key) |
| 96 | + .withContext(izanamiContext.context) |
| 97 | + .withUser(izanamiContext.user) |
| 98 | + .withErrorStrategy( |
| 99 | + defaultValueStrategy( |
| 100 | + defaultValue.asBoolean(), |
| 101 | + defaultValue.asString(), |
| 102 | + Optional.ofNullable(defaultValue.asDouble()).map(BigDecimal::valueOf) |
| 103 | + .or(() -> Optional.ofNullable(defaultValue.asInteger()).map(BigDecimal::valueOf)) |
| 104 | + .orElse(null) |
| 105 | + ) |
| 106 | + ) |
| 107 | + ).join(); |
| 108 | + try { |
| 109 | + Object tree = mapper.readValue(result, Object.class); |
| 110 | + return ProviderEvaluation.<Value>builder().value(objectToValue(tree)).build(); |
| 111 | + } catch (JsonProcessingException e) { |
| 112 | + throw new TypeMismatchError("Flag " + key + " evaluation returned invalid json."); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + |
| 117 | + private Value objectToValue(Object object) { |
| 118 | + if (object instanceof Value) { |
| 119 | + return (Value) object; |
| 120 | + } else if (object == null) { |
| 121 | + return null; |
| 122 | + } else if (object instanceof String) { |
| 123 | + return new Value((String) object); |
| 124 | + } else if (object instanceof Boolean) { |
| 125 | + return new Value((Boolean) object); |
| 126 | + } else if (object instanceof Integer) { |
| 127 | + return new Value((Integer) object); |
| 128 | + } else if (object instanceof Double) { |
| 129 | + return new Value((Double) object); |
| 130 | + } else if (object instanceof Structure) { |
| 131 | + return new Value((Structure) object); |
| 132 | + } else if (object instanceof List) { |
| 133 | + // need to translate each elem in list to a value |
| 134 | + return new Value( |
| 135 | + ((List<Object>) object).stream().map(this::objectToValue).collect(Collectors.toList())); |
| 136 | + } else if (object instanceof Instant) { |
| 137 | + return new Value((Instant) object); |
| 138 | + } else if (object instanceof Map) { |
| 139 | + return new Value(mapToStructure((Map<String, Object>) object)); |
| 140 | + } else if (object instanceof ObjectNode) { |
| 141 | + ObjectNode objectNode = (ObjectNode) object; |
| 142 | + return objectToValue(new ObjectMapper().convertValue(objectNode, Object.class)); |
| 143 | + } else { |
| 144 | + throw new TypeMismatchError("Flag value " + object + " had unexpected type " + object.getClass() + "."); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + |
| 149 | +} |
0 commit comments