Skip to content

Commit 645e677

Browse files
refactor!: Re-packaging (#143)
Release-As: 0.1.0
1 parent 051b2d6 commit 645e677

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+455
-163
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
# specific files), please put them in your ~/.global-gitignore and not here!
44
.*
55
!.git*
6-
/target/
6+
**/target/
77
*.iml
88
**dependency-reduced-pom.xml

openfeature-provider/pom.xml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>com.spotify.confidence</groupId>
8+
<artifactId>confidence-sdk-java</artifactId>
9+
<version>0.0.16-SNAPSHOT</version>
10+
</parent>
11+
12+
<artifactId>openfeature-provider</artifactId>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>com.spotify.confidence</groupId>
17+
<artifactId>sdk-java</artifactId>
18+
<version>0.0.16-SNAPSHOT</version>
19+
</dependency>
20+
<dependency>
21+
<groupId>dev.openfeature</groupId>
22+
<artifactId>sdk</artifactId>
23+
<version>1.6.1</version>
24+
</dependency>
25+
<dependency>
26+
<groupId>com.google.protobuf</groupId>
27+
<artifactId>protobuf-java-util</artifactId>
28+
<version>${protobuf.version}</version>
29+
<scope>test</scope>
30+
</dependency>
31+
32+
</dependencies>
33+
34+
</project>

src/main/java/com/spotify/confidence/ConfidenceFeatureProvider.java renamed to openfeature-provider/src/main/java/com/spotify/confidence/ConfidenceFeatureProvider.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.spotify.confidence;
22

3-
import static com.spotify.confidence.ConfidenceUtils.FlagPath.getPath;
4-
import static com.spotify.confidence.OpenFeatureUtils.getValueForPath;
3+
import static com.spotify.confidence.FlagResolverClientImpl.OPEN_FEATURE_RESOLVE_CONTEXT_KEY;
54

65
import com.google.protobuf.Struct;
76
import com.spotify.confidence.ConfidenceUtils.FlagPath;
@@ -28,7 +27,6 @@
2827
/** OpenFeature Provider for feature flagging with the Confidence platform */
2928
public class ConfidenceFeatureProvider implements FeatureProvider {
3029

31-
public static final String OPEN_FEATURE_RESOLVE_CONTEXT_KEY = "open-feature";
3230
private final Confidence confidence;
3331

3432
/**
@@ -147,7 +145,7 @@ public ProviderEvaluation<Value> getObjectEvaluation(
147145

148146
final FlagPath flagPath;
149147
try {
150-
flagPath = getPath(key);
148+
flagPath = FlagPath.getPath(key);
151149
} catch (IllegalValuePath e) {
152150
log.warn(e.getMessage());
153151
throw new RuntimeException(e);
@@ -201,7 +199,7 @@ public ProviderEvaluation<Value> getObjectEvaluation(
201199
OpenFeatureTypeMapper.from(resolvedFlag.getValue(), resolvedFlag.getFlagSchema());
202200

203201
// if a path is given, extract expected portion from the structured value
204-
Value value = getValueForPath(flagPath.getPath(), fullValue);
202+
Value value = OpenFeatureUtils.getValueForPath(flagPath.getPath(), fullValue);
205203

206204
if (value.isNull()) {
207205
value = defaultValue;

src/main/java/com/spotify/confidence/OpenFeatureTypeMapper.java renamed to openfeature-provider/src/main/java/com/spotify/confidence/OpenFeatureTypeMapper.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package com.spotify.confidence;
22

3+
import com.google.protobuf.ListValue;
4+
import com.google.protobuf.NullValue;
35
import com.google.protobuf.Struct;
4-
import com.google.protobuf.util.Values;
56
import com.spotify.confidence.shaded.flags.types.v1.FlagSchema;
67
import com.spotify.confidence.shaded.flags.types.v1.FlagSchema.SchemaTypeCase;
78
import com.spotify.confidence.shaded.flags.types.v1.FlagSchema.StructFlagSchema;
@@ -103,25 +104,33 @@ public static Value from(Struct struct, StructFlagSchema schema) {
103104

104105
public static com.google.protobuf.Value from(Value val) {
105106
if (val.isNumber()) {
106-
return Values.of(val.asDouble());
107+
return com.google.protobuf.Value.newBuilder().setNumberValue(val.asDouble()).build();
107108
} else if (val.isBoolean()) {
108-
return Values.of(val.asBoolean());
109+
return com.google.protobuf.Value.newBuilder().setBoolValue(val.asBoolean()).build();
109110
} else if (val.isNull()) {
110-
return Values.ofNull();
111+
return com.google.protobuf.Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build();
111112
} else if (val.isInstant()) {
112113
throw new ValueNotConvertableError("Converting Instant Value is currently not supported");
113114
} else if (val.isString()) {
114-
return Values.of(val.asString());
115+
return com.google.protobuf.Value.newBuilder().setStringValue(val.asString()).build();
115116
} else if (val.isList()) {
116-
final List<Value> values = val.asList();
117-
return Values.of(
118-
values.stream().map(OpenFeatureTypeMapper::from).collect(Collectors.toList()));
117+
return com.google.protobuf.Value.newBuilder()
118+
.setListValue(
119+
ListValue.newBuilder()
120+
.addAllValues(
121+
val.asList().stream()
122+
.map(OpenFeatureTypeMapper::from)
123+
.collect(Collectors.toList()))
124+
.build())
125+
.build();
119126
} else if (val.isStructure()) {
120127
final Structure structure = val.asStructure();
121128
final Map<String, com.google.protobuf.Value> protoMap =
122129
structure.asMap().keySet().stream()
123130
.collect(Collectors.toMap(key -> key, key -> from(structure.getValue(key))));
124-
return Values.of(Struct.newBuilder().putAllFields(protoMap).build());
131+
return com.google.protobuf.Value.newBuilder()
132+
.setStructValue(Struct.newBuilder().putAllFields(protoMap).build())
133+
.build();
125134
}
126135
throw new ValueNotConvertableError("Unknown Value type");
127136
}

src/main/java/com/spotify/confidence/OpenFeatureUtils.java renamed to openfeature-provider/src/main/java/com/spotify/confidence/OpenFeatureUtils.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22

33
import com.google.common.annotations.Beta;
44
import com.google.protobuf.Struct;
5-
import com.google.protobuf.util.Values;
65
import dev.openfeature.sdk.EvaluationContext;
76
import dev.openfeature.sdk.Structure;
87
import dev.openfeature.sdk.Value;
98
import dev.openfeature.sdk.exceptions.TypeMismatchError;
10-
import io.grpc.netty.shaded.io.netty.util.internal.StringUtil;
119
import java.util.List;
1210
import org.slf4j.Logger;
1311

@@ -36,9 +34,13 @@ static Struct convertToProto(EvaluationContext evaluationContext) {
3634
protoEvaluationContext.putFields(mapKey, OpenFeatureTypeMapper.from(mapValue));
3735
});
3836
// add targeting key as a regular value to proto struct
39-
if (!StringUtil.isNullOrEmpty(evaluationContext.getTargetingKey())) {
37+
if (evaluationContext.getTargetingKey() != null
38+
&& !evaluationContext.getTargetingKey().isEmpty()) {
4039
protoEvaluationContext.putFields(
41-
TARGETING_KEY, Values.of(evaluationContext.getTargetingKey()));
40+
TARGETING_KEY,
41+
com.google.protobuf.Value.newBuilder()
42+
.setStringValue(evaluationContext.getTargetingKey())
43+
.build());
4244
}
4345
return protoEvaluationContext.build();
4446
}

src/main/resources/version.properties renamed to openfeature-provider/src/main/resources/version.properties

File renamed without changes.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.spotify.confidence;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.junit.jupiter.api.Assertions.*;
5+
6+
import dev.openfeature.sdk.ErrorCode;
7+
import dev.openfeature.sdk.FlagEvaluationDetails;
8+
import dev.openfeature.sdk.ImmutableContext;
9+
import dev.openfeature.sdk.OpenFeatureAPI;
10+
import dev.openfeature.sdk.Reason;
11+
import dev.openfeature.sdk.Value;
12+
import java.io.IOException;
13+
import java.util.Map;
14+
import org.junit.jupiter.api.BeforeEach;
15+
import org.junit.jupiter.api.Test;
16+
17+
class ConfidenceFeatureProviderTest {
18+
19+
private Confidence root;
20+
private FakeEventSenderEngine fakeEngine;
21+
private ResolverClientTestUtils.FakeFlagResolverClient fakeFlagResolverClient;
22+
23+
@BeforeEach
24+
public void setup() {
25+
fakeEngine = new FakeEventSenderEngine(new FakeClock());
26+
fakeFlagResolverClient = new ResolverClientTestUtils.FakeFlagResolverClient();
27+
root = Confidence.create(fakeEngine, fakeFlagResolverClient);
28+
}
29+
30+
@Test
31+
public void testCloseChildShouldReturnDefaultsFromOpenFeatureApi() throws IOException {
32+
final Confidence child = root.withContext(Map.of("child-key", ConfidenceValue.of("child")));
33+
OpenFeatureAPI.getInstance().setProvider(new ConfidenceFeatureProvider(child));
34+
child.close();
35+
final boolean defaultValue = false;
36+
final FlagEvaluationDetails<Boolean> booleanDetails =
37+
OpenFeatureAPI.getInstance()
38+
.getClient()
39+
.getBooleanDetails(
40+
"some-flag",
41+
defaultValue,
42+
new ImmutableContext("some-key", Map.of("some", new Value("value"))));
43+
assertThat(booleanDetails.getValue()).isEqualTo(defaultValue);
44+
assertThat(booleanDetails.getReason()).isEqualTo(Reason.ERROR.name());
45+
assertThat(booleanDetails.getErrorCode()).isEqualTo(ErrorCode.GENERAL);
46+
assertThat(booleanDetails.getErrorMessage()).isEqualTo("Resource closed");
47+
}
48+
}

src/test/java/com/spotify/confidence/FakeClock.java renamed to openfeature-provider/src/test/java/com/spotify/confidence/FakeClock.java

File renamed without changes.

src/test/java/com/spotify/confidence/FakeEventSenderEngine.java renamed to openfeature-provider/src/test/java/com/spotify/confidence/FakeEventSenderEngine.java

File renamed without changes.

0 commit comments

Comments
 (0)