Skip to content

Commit

Permalink
Merge pull request #32336 from vespa-engine/hakonhall/also-accept-who…
Browse files Browse the repository at this point in the history
…le-numbers-as-serialized-values-for-double-flags

Also accept whole numbers as serialized values for double flags
  • Loading branch information
arnej27959 authored Sep 5, 2024
2 parents 8b6eb7d + 5af87c7 commit ec0e290
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public UnboundDoubleFlag(FlagId id, double defaultValue) {

public UnboundDoubleFlag(FlagId id, Double defaultValue, FetchVector defaultFetchVector) {
super(id, defaultValue, defaultFetchVector,
new SimpleFlagSerializer<>(DoubleNode::new, JsonNode::isFloatingPointNumber, JsonNode::asDouble),
new SimpleFlagSerializer<>(DoubleNode::new, JsonNode::isNumber, JsonNode::asDouble),
UnboundDoubleFlag::new, DoubleFlag::new);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,31 @@

import com.fasterxml.jackson.databind.node.JsonNodeType;
import com.yahoo.test.json.Jackson;
import com.yahoo.vespa.flags.FetchVector;
import com.yahoo.vespa.flags.FlagId;
import com.yahoo.vespa.flags.FlagSource;
import com.yahoo.vespa.flags.Flags;
import com.yahoo.vespa.flags.RawFlag;
import com.yahoo.vespa.flags.UnboundDoubleFlag;
import com.yahoo.vespa.flags.UnboundFlag;
import com.yahoo.vespa.flags.file.FlagDbFile;
import com.yahoo.vespa.flags.json.wire.WireCondition;
import com.yahoo.vespa.flags.json.wire.WireFlagData;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.nio.file.FileSystem;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Supplier;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
Expand Down Expand Up @@ -143,4 +157,37 @@ void jsonWithStrayFields() {

assertThat(FlagData.deserialize(json).serializeToJson(), equalTo("{\"id\":\"id3\"}"));
}

@Test
void deserializationOfDouble() {
assertEquals(3.0, deserializedValue("3"), 1e-4);
assertEquals(3.0, deserializedValue("3.0"), 1e-4);
}

private double deserializedValue(String jsonValue) {
try (var cleanup = Flags.clearFlagsForTesting()) {
String id = "id1";
UnboundFlag<Double, ?, ?> doubleFlag = Flags.defineDoubleFlag(id, 1.2, List.of(), "1970-01-01", "2100-01-01", "description", "modification effect");
String json = """
{
"id": "%s",
"rules": [
{
"value": %s
}
]
}
""".formatted(id, jsonValue);
FlagData data = FlagData.deserialize(json);
FlagSource flagSource = new SimpleFlagSource(data);
return doubleFlag.bindTo(flagSource).boxedValue();
}
}

public record SimpleFlagSource(FlagData flagData) implements FlagSource {
@Override
public Optional<RawFlag> fetch(FlagId id, FetchVector vector) {
return flagData.id().equals(id) ? flagData.resolve(vector) : Optional.empty();
}
}
}

0 comments on commit ec0e290

Please sign in to comment.