diff --git a/dd-openfeature/README.md b/dd-openfeature/README.md index 60c735c7cb1..6f8e7e0718c 100644 --- a/dd-openfeature/README.md +++ b/dd-openfeature/README.md @@ -300,6 +300,8 @@ tracing remain active. Previously recorded or queued data is not removed. * The provider moved to `com.datadog.openfeature.Provider`, and the exposure hook to `com.datadog.openfeature.ExposureHook`. `datadog.trace.api.openfeature.Provider` and `datadog.trace.api.openfeature.ExposureHook` are kept as deprecated aliases. * The SDK no longer requires `dd-java-agent` and no longer links against agent classes. * `dd-openfeature` releases before 1.69.0 link to agent classes that `dd-java-agent` 1.69.0 removes: with `dd-java-agent` 1.69.0 or later, they return the caller default values. Upgrade `dd-openfeature` to 1.69.0 or later together with `dd-java-agent`. +* JSON flag values are now shared and read-only. Changing the returned structure, list, or `asObjectMap()` map throws `UnsupportedOperationException`. Copy the value before you change it, for example with `asMap()` or `new HashMap<>(structure.asObjectMap())`. The copy is shallow: also copy each nested map or list that you change. A hook that changes the value in `after` makes the evaluation return the default value with the `GENERAL` error code. +* Integers in JSON flag values are now `Integer` or `Long` (for example `5`, not `5.0`), and integers up to `Long.MAX_VALUE` keep their exact value. This also applies to `Value.asObject()` and `Value.equals`. To read a number as a double, use `Value.asDouble()` or `((Number) map.get("n")).doubleValue()`. ## Design notes for contributors diff --git a/dd-openfeature/src/main/java/com/datadog/openfeature/internal/evaluation/FlagEvaluator.java b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/evaluation/FlagEvaluator.java index 55ba7dc45ce..e4645cd7704 100644 --- a/dd-openfeature/src/main/java/com/datadog/openfeature/internal/evaluation/FlagEvaluator.java +++ b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/evaluation/FlagEvaluator.java @@ -476,7 +476,11 @@ private ProviderEvaluation resolveVariant( final T mappedValue; try { - mappedValue = mapValue(target, variant.value); + // An object or array variant is converted once, at parse time: share it, not a new copy. + mappedValue = + target == Value.class && variant.frozenValue != null + ? target.cast(variant.frozenValue) + : mapValue(target, variant.value); } catch (final NumberFormatException e) { return error( defaultValue, diff --git a/dd-openfeature/src/main/java/com/datadog/openfeature/internal/ufc/FrozenValues.java b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/ufc/FrozenValues.java new file mode 100644 index 00000000000..635a10d9ece --- /dev/null +++ b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/ufc/FrozenValues.java @@ -0,0 +1,130 @@ +package com.datadog.openfeature.internal.ufc; + +import dev.openfeature.sdk.MutableStructure; +import dev.openfeature.sdk.Structure; +import dev.openfeature.sdk.Value; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Builds the OpenFeature {@link Value} of a JSON variant once, when the configuration is parsed, so + * that each evaluation returns the same instance instead of a new deep copy. + * + *

Each JSON object becomes a {@link FrozenStructure}: a {@link MutableStructure} over an + * unmodifiable map, so {@code getValue} returns the shared child without a copy and {@code add} + * throws {@link UnsupportedOperationException}. It keeps the equality of {@link + * Value#objectToValue}. Each JSON array becomes an unmodifiable list. + */ +final class FrozenValues { + /** Tells whether the loaded OpenFeature SDK accepts {@link Long} values (1.22.0 or later). */ + static final boolean LONG_VALUES = hasLongValues(); + + private FrozenValues() {} + + private static boolean hasLongValues() { + try { + Value.class.getConstructor(Long.class); + return true; + } catch (final NoSuchMethodException e) { + return false; + } + } + + /** Tells whether the value is a JSON object or array, the values that are frozen. */ + static boolean isContainer(final Object value) { + return value instanceof Map || value instanceof List; + } + + /** + * Converts a parsed JSON tree to a {@link Value} that cannot change. The result is equal to + * {@code Value.objectToValue(tree)}. + */ + static Value freeze(final Object tree) { + if (tree instanceof Map) { + final Map source = (Map) tree; + final Map attributes = new HashMap<>(capacity(source.size())); + for (final Map.Entry entry : source.entrySet()) { + attributes.put((String) entry.getKey(), freeze(entry.getValue())); + } + return new Value(new FrozenStructure(Collections.unmodifiableMap(attributes))); + } + if (tree instanceof List) { + final List source = (List) tree; + final List items = new ArrayList<>(source.size()); + for (final Object element : source) { + items.add(freeze(element)); + } + return new Value(Collections.unmodifiableList(items)); + } + return Value.objectToValue(tree); + } + + /** Returns the plain form of a frozen value, equal to the tree that was frozen. */ + static Object plain(final Value frozen) { + return frozen.isStructure() + ? frozen.asStructure().asObjectMap() + : FrozenStructure.toObject(frozen); + } + + private static int capacity(final int size) { + return (int) Math.min(Integer.MAX_VALUE, ((long) size * 4 + 2) / 3); + } + + /** + * A structure that cannot change. It also keeps its plain-map form, because applications often + * read a JSON flag with {@link #asObjectMap()}. + */ + static final class FrozenStructure extends MutableStructure { + private volatile Map objectMap; + + FrozenStructure(final Map attributes) { + super(attributes); + } + + /** + * Returns the plain-map form, built on the first call and then shared. The map and its nested + * maps and lists cannot change. + */ + @Override + public Map asObjectMap() { + Map map = this.objectMap; + if (map == null) { + map = toObjectMap(this); + // A race builds equal immutable maps; any of them can be kept. + this.objectMap = map; + } + return map; + } + + private static Map toObjectMap(final Structure structure) { + final Map attributes = structure.asUnmodifiableMap(); + final Map map = new HashMap<>(capacity(attributes.size())); + for (final Map.Entry entry : attributes.entrySet()) { + map.put(entry.getKey(), toObject(entry.getValue())); + } + return Collections.unmodifiableMap(map); + } + + /** + * The same conversion as {@link Structure#convertValue}, with unmodifiable containers. {@link + * #freeze} gives each child a non-null {@link Value}, and each nested structure is frozen. + */ + private static Object toObject(final Value value) { + if (value.isStructure()) { + return value.asStructure().asObjectMap(); + } + if (value.isList()) { + final List source = value.asList(); + final List items = new ArrayList<>(source.size()); + for (final Value item : source) { + items.add(toObject(item)); + } + return Collections.unmodifiableList(items); + } + return value.asObject(); + } + } +} diff --git a/dd-openfeature/src/main/java/com/datadog/openfeature/internal/ufc/UniversalFlagConfigParser.java b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/ufc/UniversalFlagConfigParser.java index d6fb0e435f4..0886116c64e 100644 --- a/dd-openfeature/src/main/java/com/datadog/openfeature/internal/ufc/UniversalFlagConfigParser.java +++ b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/ufc/UniversalFlagConfigParser.java @@ -7,6 +7,7 @@ import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonToken; +import dev.openfeature.sdk.Value; import java.io.IOException; import java.time.Instant; import java.time.format.DateTimeFormatter; @@ -23,7 +24,8 @@ * Parses Universal Flag Configuration (UFC) documents. * *

The document is first read as a generic JSON tree (objects, arrays, strings, {@link Double} - * numbers, booleans and nulls), then mapped onto the UFC model. Malformed flags are dropped + * numbers, booleans and nulls; an integer in a variant value that fits in a long is read as {@link + * Integer} or {@link Long}), then mapped onto the UFC model. Malformed flags are dropped * individually and reported through {@link ServerConfiguration#invalidFlags}; a malformed envelope * fails the whole document. */ @@ -85,7 +87,7 @@ private static Object readDocument(final byte[] content) throws IOException { if (parser.nextToken() == null) { throw new JsonParseException(parser, "Empty document"); } - final Object document = readValue(parser, 1); + final Object document = readValue(parser, 1, Scope.UFC); if (parser.nextToken() != null) { throw new JsonParseException(parser, "Unexpected content after the document"); } @@ -93,7 +95,43 @@ private static Object readDocument(final byte[] content) throws IOException { } } - private static Object readValue(final JsonParser parser, final int depth) throws IOException { + /** + * Where the reader is in the document. Numbers are read as {@link Double}, except integers in + * {@code flags..variations..value} that fit in a long: those are read as {@link + * Integer} or {@link Long}, so JSON variants keep exact integers. + */ + private enum Scope { + UFC, + FLAGS, + FLAG, + VARIATIONS, + VARIANT, + VARIANT_VALUE; + + Scope field(final String name) { + switch (this) { + case UFC: + return "flags".equals(name) ? FLAGS : UFC; + case FLAGS: + return FLAG; + case FLAG: + return "variations".equals(name) ? VARIATIONS : UFC; + case VARIATIONS: + return VARIANT; + case VARIANT: + return "value".equals(name) ? VARIANT_VALUE : UFC; + default: + return VARIANT_VALUE; + } + } + + Scope element() { + return this == VARIANT_VALUE ? VARIANT_VALUE : UFC; + } + } + + private static Object readValue(final JsonParser parser, final int depth, final Scope scope) + throws IOException { final JsonToken token = parser.currentToken(); if ((token == JsonToken.START_OBJECT || token == JsonToken.START_ARRAY) && depth > MAX_NESTING_DEPTH) { @@ -105,24 +143,36 @@ private static Object readValue(final JsonParser parser, final int depth) throws while (parser.nextToken() == JsonToken.FIELD_NAME) { final String name = parser.currentName(); parser.nextToken(); - object.put(name, readValue(parser, depth + 1)); + object.put(name, readValue(parser, depth + 1, scope.field(name))); } return object; case START_ARRAY: final List array = new ArrayList<>(); while (parser.nextToken() != JsonToken.END_ARRAY) { - array.add(readValue(parser, depth + 1)); + array.add(readValue(parser, depth + 1, scope.element())); } return array; case VALUE_STRING: return parser.getText(); case VALUE_NUMBER_INT: - case VALUE_NUMBER_FLOAT: - final double number = parser.getDoubleValue(); - if (!Double.isFinite(number)) { - throw new JsonParseException(parser, "Non-finite JSON number"); + if (scope == Scope.VARIANT_VALUE) { + // OpenFeature values hold Integer, Long or Double: larger integers stay doubles. + switch (parser.getNumberType()) { + case INT: + return parser.getIntValue(); + case LONG: + // OpenFeature SDKs before 1.22.0 have no Long values: keep the double there. + if (FrozenValues.LONG_VALUES) { + return parser.getLongValue(); + } + break; + default: + break; + } } - return number; + return finiteDouble(parser); + case VALUE_NUMBER_FLOAT: + return finiteDouble(parser); case VALUE_TRUE: return Boolean.TRUE; case VALUE_FALSE: @@ -134,6 +184,14 @@ private static Object readValue(final JsonParser parser, final int depth) throws } } + private static double finiteDouble(final JsonParser parser) throws IOException { + final double number = parser.getDoubleValue(); + if (!Double.isFinite(number)) { + throw new JsonParseException(parser, "Non-finite JSON number"); + } + return number; + } + @Nullable private static ServerConfiguration toServerConfiguration(@Nullable final Object value) throws IOException { @@ -200,33 +258,66 @@ private static Flag toFlag(@Nullable final Object value) { return null; } final Map json = asObject(value, "flag"); + final ValueType variationType = + asEnum(json.get("variationType"), ValueType.class, "variationType"); return new Flag( asString(json.get("key"), "key"), strictBoolean(json, "enabled"), - asEnum(json.get("variationType"), ValueType.class, "variationType"), - toVariants(json.get("variations")), + variationType, + toVariants(json.get("variations"), variationType), toList(json.get("allocations"), "allocations", UniversalFlagConfigParser::toAllocation)); } @Nullable - private static Map toVariants(@Nullable final Object value) { + private static Map toVariants( + @Nullable final Object value, @Nullable final ValueType variationType) { if (value == null) { return null; } final Map variants = new HashMap<>(); for (final Map.Entry entry : asObject(value, "variations").entrySet()) { - variants.put((String) entry.getKey(), toVariant(entry.getValue())); + variants.put((String) entry.getKey(), toVariant(entry.getValue(), variationType)); } return variants; } @Nullable - private static Variant toVariant(@Nullable final Object value) { + private static Variant toVariant( + @Nullable final Object value, @Nullable final ValueType variationType) { if (value == null) { return null; } final Map json = asObject(value, "variant"); - return new Variant(asString(json.get("key"), "key"), json.get("value")); + final String key = asString(json.get("key"), "key"); + final Object variantValue = json.get("value"); + if (variationType != ValueType.JSON) { + // Only JSON flags keep exact integers. + return new Variant(key, withDoubles(variantValue), null); + } + if (!FrozenValues.isContainer(variantValue)) { + return new Variant(key, variantValue, null); + } + // Freeze once and share it. Keep its plain form instead of the parsed tree. + final Value frozen = FrozenValues.freeze(variantValue); + return new Variant(key, FrozenValues.plain(frozen), frozen); + } + + /** Replaces each Integer or Long in a variant value with the equal Double. */ + private static Object withDoubles(@Nullable final Object value) { + if (value instanceof Integer || value instanceof Long) { + return ((Number) value).doubleValue(); + } + if (value instanceof Map) { + final Map map = new LinkedHashMap<>(); + ((Map) value).forEach((k, v) -> map.put(k, withDoubles(v))); + return map; + } + if (value instanceof List) { + final List list = new ArrayList<>(); + ((List) value).forEach(v -> list.add(withDoubles(v))); + return list; + } + return value; } @Nullable diff --git a/dd-openfeature/src/main/java/com/datadog/openfeature/internal/ufc/Variant.java b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/ufc/Variant.java index 4d9246c5965..fac2db6a9fb 100644 --- a/dd-openfeature/src/main/java/com/datadog/openfeature/internal/ufc/Variant.java +++ b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/ufc/Variant.java @@ -1,11 +1,21 @@ package com.datadog.openfeature.internal.ufc; +import dev.openfeature.sdk.Value; + public final class Variant { public final String key; public final Object value; + /** The shared, read-only value of a JSON object or array variant; otherwise {@code null}. */ + public final Value frozenValue; + public Variant(final String key, final Object value) { + this(key, value, null); + } + + public Variant(final String key, final Object value, final Value frozenValue) { this.key = key; this.value = value; + this.frozenValue = frozenValue; } } diff --git a/dd-openfeature/src/test/java/com/datadog/openfeature/CanonicalFixtureTest.java b/dd-openfeature/src/test/java/com/datadog/openfeature/CanonicalFixtureTest.java index 787fefa1c11..755b55236f6 100644 --- a/dd-openfeature/src/test/java/com/datadog/openfeature/CanonicalFixtureTest.java +++ b/dd-openfeature/src/test/java/com/datadog/openfeature/CanonicalFixtureTest.java @@ -288,7 +288,10 @@ private static Class targetType(final String variationType) { } } - /** Normalizes the integral numbers read as Long to Double, as the UFC parser does. */ + /** + * Normalizes the integral numbers read as Long to Double, as the UFC parser does outside JSON + * variant values. + */ private static Object normalize(final Object value) { if (value instanceof Long) { return ((Long) value).doubleValue(); @@ -306,6 +309,28 @@ private static Object normalize(final Object value) { return value; } + /** + * Maps the integral numbers read as Long to Integer when they fit, as the UFC parser does for + * JSON variant values. + */ + private static Object exactIntegers(final Object value) { + if (value instanceof Long) { + final long number = (Long) value; + return number == (int) number ? (Object) (int) number : value; + } + if (value instanceof Map) { + final Map map = new java.util.LinkedHashMap<>(); + ((Map) value).forEach((k, v) -> map.put((String) k, exactIntegers(v))); + return map; + } + if (value instanceof List) { + final List list = new ArrayList<>(); + ((List) value).forEach(v -> list.add(exactIntegers(v))); + return list; + } + return value; + } + static final class FixtureCase { final Map json; final String fileName; @@ -334,10 +359,14 @@ static final class FixtureCase { this.index = index; final Object attributes = normalize(json.get("attributes")); this.attributes = attributes == null ? emptyMap() : (Map) attributes; - this.defaultValue = normalize(json.get("defaultValue")); this.flag = (String) json.get("flag"); this.targetingKey = (String) json.get("targetingKey"); this.variationType = (String) json.get("variationType"); + // The default value of a JSON case uses the same number model as its result. + this.defaultValue = + "JSON".equals(this.variationType) + ? exactIntegers(json.get("defaultValue")) + : normalize(json.get("defaultValue")); final Map result = (Map) json.get("result"); requireSupportedKeys( result, @@ -356,7 +385,10 @@ static final class FixtureCase { if (result.containsKey("doLog")) { assertNotNull(this.resultDoLog, "A doLog expectation must be a boolean"); } - this.resultValue = normalize(result.get("value")); + this.resultValue = + "JSON".equals(this.variationType) + ? exactIntegers(result.get("value")) + : normalize(result.get("value")); this.resultReason = (String) result.get("reason"); this.resultErrorCode = (String) result.get("errorCode"); if (result.containsKey("errorCode")) { diff --git a/dd-openfeature/src/test/java/com/datadog/openfeature/internal/evaluation/FlagEvaluatorJsonValueTest.java b/dd-openfeature/src/test/java/com/datadog/openfeature/internal/evaluation/FlagEvaluatorJsonValueTest.java new file mode 100644 index 00000000000..15e5bb5e189 --- /dev/null +++ b/dd-openfeature/src/test/java/com/datadog/openfeature/internal/evaluation/FlagEvaluatorJsonValueTest.java @@ -0,0 +1,186 @@ +package com.datadog.openfeature.internal.evaluation; + +import static java.nio.charset.StandardCharsets.UTF_8; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import com.datadog.openfeature.internal.ufc.ServerConfiguration; +import com.datadog.openfeature.internal.ufc.UniversalFlagConfigParser; +import dev.openfeature.sdk.MutableContext; +import dev.openfeature.sdk.MutableStructure; +import dev.openfeature.sdk.ProviderEvaluation; +import dev.openfeature.sdk.Value; +import java.util.List; +import java.util.Map; +import org.junit.jupiter.api.Test; + +/** JSON variants are converted once, at parse time, and shared by every evaluation. */ +class FlagEvaluatorJsonValueTest { + private static final String VARIANT = + "{\"name\":\"blue\",\"count\":5,\"big\":9007199254740993,\"ratio\":1.5,\"on\":true," + + "\"none\":null,\"tags\":[\"a\",\"b\"],\"nested\":{\"items\":[1,{\"n\":2}]}}"; + + private final FlagEvaluator evaluator = new FlagEvaluator(false, (f, s, a, v, id) -> false); + + @Test + void returnsTheSameValueForEachEvaluation() throws Exception { + final ServerConfiguration config = config(VARIANT); + + final Value first = evaluate(config).getValue(); + final Value second = evaluate(config).getValue(); + + assertSame(first, second); + } + + @Test + void returnsTheValueThatObjectToValueReturns() throws Exception { + final ServerConfiguration config = config(VARIANT); + final Object tree = config.flags.get("f").variations.get("on").value; + + final Value value = evaluate(config).getValue(); + + assertEquals(Value.objectToValue(tree), value); + assertEquals( + Value.objectToValue(tree).asStructure().asObjectMap(), value.asStructure().asObjectMap()); + assertTrue(value.asStructure() instanceof MutableStructure); + } + + @Test + void sharesThePlainMapForm() throws Exception { + final Value value = evaluate(config(VARIANT)).getValue(); + + assertSame(value.asStructure().asObjectMap(), value.asStructure().asObjectMap()); + } + + @Test + void keepsIntegersExact() throws Exception { + final Value value = evaluate(config(VARIANT)).getValue(); + final Map map = value.asStructure().asObjectMap(); + + assertEquals(5, map.get("count")); + assertEquals(9007199254740993L, map.get("big")); + assertEquals(1.5, map.get("ratio")); + assertEquals(5, value.asStructure().getValue("count").asObject()); + } + + @Test + void cannotBeChangedByTheCaller() throws Exception { + final Value value = evaluate(config(VARIANT)).getValue(); + final MutableStructure structure = (MutableStructure) value.asStructure(); + final Map map = structure.asObjectMap(); + @SuppressWarnings("unchecked") + final Map nested = (Map) map.get("nested"); + @SuppressWarnings("unchecked") + final List items = (List) nested.get("items"); + + assertThrows(UnsupportedOperationException.class, () -> structure.add("k", "v")); + assertThrows( + UnsupportedOperationException.class, + () -> structure.getValue("tags").asList().add(new Value("c"))); + assertThrows(UnsupportedOperationException.class, () -> map.put("k", "v")); + assertThrows(UnsupportedOperationException.class, () -> nested.put("k", "v")); + assertThrows(UnsupportedOperationException.class, () -> items.add(3)); + } + + @Test + void returnsAJsonArrayVariant() throws Exception { + final Value value = evaluate(config("[1,\"two\",{\"three\":3}]")).getValue(); + + assertTrue(value.isList()); + assertEquals(1, value.asList().get(0).asObject()); + assertEquals(3, value.asList().get(2).asStructure().getValue("three").asObject()); + assertThrows(UnsupportedOperationException.class, () -> value.asList().add(new Value())); + } + + @Test + void keepsDoublesOutsideVariantValues() throws Exception { + final ServerConfiguration config = + UniversalFlagConfigParser.parse( + ("{\"flags\":{\"f\":{\"key\":\"f\",\"enabled\":true,\"variationType\":\"JSON\"," + + "\"variations\":{\"on\":{\"key\":\"on\",\"value\":{\"n\":1}}}," + + "\"allocations\":[{\"key\":\"a\",\"rules\":[{\"conditions\":[" + + "{\"attribute\":\"age\",\"operator\":\"GTE\",\"value\":18}]}]," + + "\"splits\":[{\"variationKey\":\"on\",\"shards\":[]}]}]}}}") + .getBytes(UTF_8)); + + final Object conditionValue = + config.flags.get("f").allocations.get(0).rules.get(0).conditions.get(0).value; + + assertEquals(18.0, conditionValue); + } + + @Test + void keepsIntegersExactForAFlagKeyedVariations() throws Exception { + final ServerConfiguration config = config("variations", "JSON", "{\"n\":5}"); + + final Value value = + evaluator + .evaluate(config, Value.class, "variations", new Value(), new MutableContext("user")) + .getValue(); + + assertEquals(5, value.asStructure().asObjectMap().get("n")); + } + + @Test + void keepsIntegersExactInTheJsonApiEnvelope() throws Exception { + final ServerConfiguration config = + UniversalFlagConfigParser.parseJsonApi( + ("{\"data\":{\"type\":\"universal-flag-configuration\",\"attributes\":" + + "{\"createdAt\":\"2024-04-17T19:40:53.716Z\"," + + "\"environment\":{\"name\":\"Test\"}," + + flags("f", "JSON", "{\"big\":9007199254740993}") + + "}}}") + .getBytes(UTF_8)); + + assertEquals( + 9007199254740993L, evaluate(config).getValue().asStructure().getValue("big").asObject()); + } + + @Test + void keepsDoublesInOtherFlagTypes() throws Exception { + final ServerConfiguration integer = config("f", "INTEGER", "5"); + final ServerConfiguration string = config("f", "STRING", "{\"n\":5,\"l\":[1]}"); + + assertEquals(5.0, integer.flags.get("f").variations.get("on").value); + assertEquals( + "{n=5.0, l=[1.0]}", String.valueOf(string.flags.get("f").variations.get("on").value)); + assertNull(string.flags.get("f").variations.get("on").frozenValue); + } + + @Test + void keepsDoublesForIntegersBeyondTheLongRange() throws Exception { + final Value value = evaluate(config("{\"n\":9223372036854775808}")).getValue(); + + assertEquals(9.223372036854776E18, value.asStructure().getValue("n").asObject()); + } + + private ProviderEvaluation evaluate(final ServerConfiguration config) { + return evaluator.evaluate(config, Value.class, "f", new Value(), new MutableContext("user")); + } + + private static ServerConfiguration config(final String variantValue) throws Exception { + return config("f", "JSON", variantValue); + } + + private static ServerConfiguration config( + final String key, final String type, final String variantValue) throws Exception { + return UniversalFlagConfigParser.parse( + ("{" + flags(key, type, variantValue) + "}").getBytes(UTF_8)); + } + + private static String flags(final String key, final String type, final String variantValue) { + return "\"flags\":{\"" + + key + + "\":{\"key\":\"" + + key + + "\",\"enabled\":true,\"variationType\":\"" + + type + + "\",\"variations\":{\"on\":{\"key\":\"on\",\"value\":" + + variantValue + + "}},\"allocations\":[{\"key\":\"a\",\"splits\":[{\"variationKey\":\"on\"," + + "\"shards\":[]}]}]}}"; + } +}