Skip to content

Commit 8933bb9

Browse files
authored
feat!: update to sdk 0.3.0 (#116)
feat!: udpate to sdk 0.3.0
1 parent e3bb962 commit 8933bb9

File tree

8 files changed

+113
-141
lines changed

8 files changed

+113
-141
lines changed

hooks/open-telemetry/src/main/java/dev/openfeature/contrib/hooks/otel/OpenTelemetryHook.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package dev.openfeature.contrib.hooks.otel;
22

3-
import dev.openfeature.javasdk.Client;
4-
import dev.openfeature.javasdk.NoOpProvider;
5-
import dev.openfeature.javasdk.OpenFeatureAPI;
3+
import dev.openfeature.sdk.Client;
4+
import dev.openfeature.sdk.NoOpProvider;
5+
import dev.openfeature.sdk.OpenFeatureAPI;
66

77
/**
88
* A placeholder.

pom.xml

+4-4
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@
4848

4949
<dependencies>
5050
<dependency>
51-
<!-- provided -->
51+
<!-- provided -->
5252
<groupId>dev.openfeature</groupId>
53-
<artifactId>javasdk</artifactId>
54-
<!-- 0.2.0 or greater -->
55-
<version>[0.2,)</version>
53+
<artifactId>sdk</artifactId>
54+
<!-- 0.3.0 or greater -->
55+
<version>[0.3,)</version>
5656
<!-- use the version provided at runtime -->
5757
<scope>provided</scope>
5858
</dependency>

providers/flagd/src/main/java/dev/openfeature/contrib/providers/flagd/FlagdProvider.java

+12-24
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
import java.util.Map;
66
import java.util.stream.Collectors;
77

8-
import org.apache.commons.lang3.EnumUtils;
9-
108
import dev.openfeature.flagd.grpc.Schema.ResolveBooleanRequest;
119
import dev.openfeature.flagd.grpc.Schema.ResolveBooleanResponse;
1210
import dev.openfeature.flagd.grpc.Schema.ResolveFloatRequest;
@@ -19,13 +17,12 @@
1917
import dev.openfeature.flagd.grpc.Schema.ResolveStringResponse;
2018
import dev.openfeature.flagd.grpc.ServiceGrpc;
2119
import dev.openfeature.flagd.grpc.ServiceGrpc.ServiceBlockingStub;
22-
import dev.openfeature.javasdk.EvaluationContext;
23-
import dev.openfeature.javasdk.FeatureProvider;
24-
import dev.openfeature.javasdk.Metadata;
25-
import dev.openfeature.javasdk.ProviderEvaluation;
26-
import dev.openfeature.javasdk.Reason;
27-
import dev.openfeature.javasdk.Structure;
28-
import dev.openfeature.javasdk.Value;
20+
import dev.openfeature.sdk.EvaluationContext;
21+
import dev.openfeature.sdk.FeatureProvider;
22+
import dev.openfeature.sdk.Metadata;
23+
import dev.openfeature.sdk.MutableStructure;
24+
import dev.openfeature.sdk.ProviderEvaluation;
25+
import dev.openfeature.sdk.Value;
2926
import io.grpc.ManagedChannelBuilder;
3027
import lombok.extern.slf4j.Slf4j;
3128

@@ -97,7 +94,7 @@ public ProviderEvaluation<Boolean> getBooleanEvaluation(String key, Boolean defa
9794
return ProviderEvaluation.<Boolean>builder()
9895
.value(r.getValue())
9996
.variant(r.getVariant())
100-
.reason(this.mapReason(r.getReason()))
97+
.reason(r.getReason())
10198
.build();
10299
}
103100

@@ -110,7 +107,7 @@ public ProviderEvaluation<String> getStringEvaluation(String key, String default
110107
ResolveStringResponse r = this.serviceStub.resolveString(request);
111108
return ProviderEvaluation.<String>builder().value(r.getValue())
112109
.variant(r.getVariant())
113-
.reason(this.mapReason(r.getReason()))
110+
.reason(r.getReason())
114111
.build();
115112
}
116113

@@ -125,7 +122,7 @@ public ProviderEvaluation<Double> getDoubleEvaluation(String key, Double default
125122
return ProviderEvaluation.<Double>builder()
126123
.value(r.getValue())
127124
.variant(r.getVariant())
128-
.reason(this.mapReason(r.getReason()))
125+
.reason(r.getReason())
129126
.build();
130127
}
131128

@@ -140,7 +137,7 @@ public ProviderEvaluation<Integer> getIntegerEvaluation(String key, Integer defa
140137
return ProviderEvaluation.<Integer>builder()
141138
.value((int) r.getValue())
142139
.variant(r.getVariant())
143-
.reason(this.mapReason(r.getReason()))
140+
.reason(r.getReason())
144141
.build();
145142
}
146143

@@ -155,19 +152,10 @@ public ProviderEvaluation<Value> getObjectEvaluation(String key, Value defaultVa
155152
return ProviderEvaluation.<Value>builder()
156153
.value(this.convertObjectResponse(r.getValue()))
157154
.variant(r.getVariant())
158-
.reason(this.mapReason(r.getReason()))
155+
.reason(r.getReason())
159156
.build();
160157
}
161158

162-
// Map FlagD reasons to Java SDK reasons.
163-
private Reason mapReason(String flagdReason) {
164-
if (!EnumUtils.isValidEnum(Reason.class, flagdReason)) {
165-
return Reason.UNKNOWN;
166-
} else {
167-
return Reason.valueOf(flagdReason);
168-
}
169-
}
170-
171159
/**
172160
* Recursively convert protobuf structure to openfeature value.
173161
*/
@@ -233,7 +221,7 @@ private Value convertProtobufMap(Map<String, com.google.protobuf.Value> map) {
233221
com.google.protobuf.Value value = map.get(key);
234222
values.put(key, this.convertAny(value));
235223
});
236-
return new Value(new Structure(values));
224+
return new Value(new MutableStructure(values));
237225
}
238226

239227
/**

providers/flagd/src/test/java/dev/openfeature/contrib/providers/flagd/FlagdProviderTest.java

+21-18
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@
2020
import dev.openfeature.flagd.grpc.Schema.ResolveObjectResponse;
2121
import dev.openfeature.flagd.grpc.Schema.ResolveStringResponse;
2222
import dev.openfeature.flagd.grpc.ServiceGrpc.ServiceBlockingStub;
23-
import dev.openfeature.javasdk.EvaluationContext;
24-
import dev.openfeature.javasdk.FlagEvaluationDetails;
25-
import dev.openfeature.javasdk.OpenFeatureAPI;
26-
import dev.openfeature.javasdk.Reason;
27-
import dev.openfeature.javasdk.Structure;
28-
import dev.openfeature.javasdk.Value;
23+
import dev.openfeature.sdk.EvaluationContext;
24+
import dev.openfeature.sdk.FlagEvaluationDetails;
25+
import dev.openfeature.sdk.MutableContext;
26+
import dev.openfeature.sdk.MutableStructure;
27+
import dev.openfeature.sdk.OpenFeatureAPI;
28+
import dev.openfeature.sdk.Reason;
29+
import dev.openfeature.sdk.Structure;
30+
import dev.openfeature.sdk.Value;
2931

3032
class FlagdProviderTest {
3133

@@ -40,7 +42,7 @@ class FlagdProviderTest {
4042
static final Double DOUBLE_VALUE = .5d;
4143
static final String INNER_STRUCT_KEY = "inner_key";
4244
static final String INNER_STRUCT_VALUE = "inner_value";
43-
static final Structure OBJECT_VALUE = new Structure() {{
45+
static final Structure OBJECT_VALUE = new MutableStructure() {{
4446
add(INNER_STRUCT_KEY, INNER_STRUCT_VALUE);
4547
}};
4648
static final com.google.protobuf.Struct PROTOBUF_STRUCTURE_VALUE = com.google.protobuf.Struct.newBuilder()
@@ -104,28 +106,28 @@ void resolvers_call_grpc_service_and_return_details() {
104106
FlagEvaluationDetails<Boolean> booleanDetails = api.getClient().getBooleanDetails(FLAG_KEY, false);
105107
assertTrue(booleanDetails.getValue());
106108
assertEquals(BOOL_VARIANT, booleanDetails.getVariant());
107-
assertEquals(DEFAULT, booleanDetails.getReason());
109+
assertEquals(DEFAULT.toString(), booleanDetails.getReason());
108110

109111
FlagEvaluationDetails<String> stringDetails = api.getClient().getStringDetails(FLAG_KEY, "wrong");
110112
assertEquals(STRING_VALUE, stringDetails.getValue());
111113
assertEquals(STRING_VARIANT, stringDetails.getVariant());
112-
assertEquals(DEFAULT, stringDetails.getReason());
114+
assertEquals(DEFAULT.toString(), stringDetails.getReason());
113115

114116
FlagEvaluationDetails<Integer> intDetails = api.getClient().getIntegerDetails(FLAG_KEY, 0);
115117
assertEquals(INT_VALUE, intDetails.getValue());
116118
assertEquals(INT_VARIANT, intDetails.getVariant());
117-
assertEquals(DEFAULT, intDetails.getReason());
119+
assertEquals(DEFAULT.toString(), intDetails.getReason());
118120

119121
FlagEvaluationDetails<Double> floatDetails = api.getClient().getDoubleDetails(FLAG_KEY, 0.1);
120122
assertEquals(DOUBLE_VALUE, floatDetails.getValue());
121123
assertEquals(DOUBLE_VARIANT, floatDetails.getVariant());
122-
assertEquals(DEFAULT, floatDetails.getReason());
124+
assertEquals(DEFAULT.toString(), floatDetails.getReason());
123125

124126
FlagEvaluationDetails<Value> objectDetails = api.getClient().getObjectDetails(FLAG_KEY, new Value());
125127
assertEquals(INNER_STRUCT_VALUE, objectDetails.getValue().asStructure()
126128
.asMap().get(INNER_STRUCT_KEY).asString());
127129
assertEquals(OBJECT_VARIANT, objectDetails.getVariant());
128-
assertEquals(DEFAULT, objectDetails.getReason());
130+
assertEquals(DEFAULT.toString(), objectDetails.getReason());
129131
}
130132

131133
@Test
@@ -147,7 +149,7 @@ void context_is_parsed_and_passed_to_grpc_service() {
147149
add(new Value(1));
148150
}};
149151
final String STRUCT_ATTR_INNER_VALUE = "struct-inner-value";
150-
final Structure STRUCT_ATTR_VALUE = new Structure().add(STRUCT_ATTR_INNER_KEY, STRUCT_ATTR_INNER_VALUE);
152+
final Structure STRUCT_ATTR_VALUE = new MutableStructure().add(STRUCT_ATTR_INNER_KEY, STRUCT_ATTR_INNER_VALUE);
151153
final String DEFAULT_STRING = "DEFAULT";
152154

153155
ResolveBooleanResponse booleanResponse = ResolveBooleanResponse.newBuilder()
@@ -170,7 +172,7 @@ void context_is_parsed_and_passed_to_grpc_service() {
170172

171173
OpenFeatureAPI.getInstance().setProvider(new FlagdProvider(serviceBlockingStubMock));
172174

173-
EvaluationContext context = new EvaluationContext();
175+
MutableContext context = new MutableContext();
174176
context.add(BOOLEAN_ATTR_KEY, BOOLEAN_ATTR_VALUE);
175177
context.add(INT_ATTR_KEY, INT_ATTR_VALUE);
176178
context.add(DOUBLE_ATTR_KEY, DOUBLE_ATTR_VALUE);
@@ -181,15 +183,16 @@ void context_is_parsed_and_passed_to_grpc_service() {
181183
FlagEvaluationDetails<Boolean> booleanDetails = api.getClient().getBooleanDetails(FLAG_KEY, false, context);
182184
assertTrue(booleanDetails.getValue());
183185
assertEquals(BOOL_VARIANT, booleanDetails.getVariant());
184-
assertEquals(DEFAULT, booleanDetails.getReason()); // reason should be converted from STATIC -> DEFAULT
186+
assertEquals(DEFAULT.toString(), booleanDetails.getReason());
185187
}
186188

189+
//TODO: update this to be able unknown codes
187190
@Test
188191
void reason_mapped_correctly_if_unknown() {
189192
ResolveBooleanResponse badReasonResponse = ResolveBooleanResponse.newBuilder()
190193
.setValue(true)
191194
.setVariant(BOOL_VARIANT)
192-
.setReason("NOT_A_REAL_REASON") // set an invalid reason string
195+
.setReason("UNKNOWN") // set an invalid reason string
193196
.build();
194197

195198
ServiceBlockingStub serviceBlockingStubMock = mock(ServiceBlockingStub.class);
@@ -198,7 +201,7 @@ void reason_mapped_correctly_if_unknown() {
198201
OpenFeatureAPI.getInstance().setProvider(new FlagdProvider(serviceBlockingStubMock));
199202

200203
FlagEvaluationDetails<Boolean> booleanDetails = api.getClient()
201-
.getBooleanDetails(FLAG_KEY, false, new EvaluationContext());
202-
assertEquals(Reason.UNKNOWN, booleanDetails.getReason()); // reason should be converted to UNKNOWN
204+
.getBooleanDetails(FLAG_KEY, false, new MutableContext());
205+
assertEquals(Reason.UNKNOWN.toString(), booleanDetails.getReason()); // reason should be converted to UNKNOWN
203206
}
204207
}

providers/go-feature-flag/src/main/java/dev/openfeature/contrib/providers/gofeatureflag/GoFeatureFlagProvider.java

+28-42
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,37 @@
11
package dev.openfeature.contrib.providers.gofeatureflag;
22

3+
import static java.net.HttpURLConnection.HTTP_BAD_REQUEST;
4+
5+
import java.io.IOException;
6+
import java.time.Instant;
7+
import java.util.List;
8+
import java.util.Map;
9+
import java.util.concurrent.TimeUnit;
10+
import java.util.stream.Collectors;
11+
312
import com.fasterxml.jackson.databind.ObjectMapper;
413
import com.fasterxml.jackson.databind.SerializationFeature;
514
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
15+
616
import dev.openfeature.contrib.providers.gofeatureflag.bean.GoFeatureFlagRequest;
717
import dev.openfeature.contrib.providers.gofeatureflag.bean.GoFeatureFlagResponse;
818
import dev.openfeature.contrib.providers.gofeatureflag.bean.GoFeatureFlagUser;
919
import dev.openfeature.contrib.providers.gofeatureflag.exception.InvalidEndpoint;
1020
import dev.openfeature.contrib.providers.gofeatureflag.exception.InvalidOptions;
11-
import dev.openfeature.javasdk.ErrorCode;
12-
import dev.openfeature.javasdk.EvaluationContext;
13-
import dev.openfeature.javasdk.FeatureProvider;
14-
import dev.openfeature.javasdk.Hook;
15-
import dev.openfeature.javasdk.Metadata;
16-
import dev.openfeature.javasdk.ProviderEvaluation;
17-
import dev.openfeature.javasdk.Reason;
18-
import dev.openfeature.javasdk.Structure;
19-
import dev.openfeature.javasdk.Value;
20-
import dev.openfeature.javasdk.exceptions.FlagNotFoundError;
21-
import dev.openfeature.javasdk.exceptions.GeneralError;
22-
import dev.openfeature.javasdk.exceptions.OpenFeatureError;
23-
import dev.openfeature.javasdk.exceptions.TypeMismatchError;
21+
import dev.openfeature.sdk.ErrorCode;
22+
import dev.openfeature.sdk.EvaluationContext;
23+
import dev.openfeature.sdk.FeatureProvider;
24+
import dev.openfeature.sdk.Hook;
25+
import dev.openfeature.sdk.Metadata;
26+
import dev.openfeature.sdk.MutableStructure;
27+
import dev.openfeature.sdk.ProviderEvaluation;
28+
import dev.openfeature.sdk.Reason;
29+
import dev.openfeature.sdk.Structure;
30+
import dev.openfeature.sdk.Value;
31+
import dev.openfeature.sdk.exceptions.FlagNotFoundError;
32+
import dev.openfeature.sdk.exceptions.GeneralError;
33+
import dev.openfeature.sdk.exceptions.OpenFeatureError;
34+
import dev.openfeature.sdk.exceptions.TypeMismatchError;
2435
import okhttp3.ConnectionPool;
2536
import okhttp3.HttpUrl;
2637
import okhttp3.MediaType;
@@ -30,15 +41,6 @@
3041
import okhttp3.Response;
3142
import okhttp3.ResponseBody;
3243

33-
import java.io.IOException;
34-
import java.time.Instant;
35-
import java.util.List;
36-
import java.util.Map;
37-
import java.util.concurrent.TimeUnit;
38-
import java.util.stream.Collectors;
39-
40-
import static java.net.HttpURLConnection.HTTP_BAD_REQUEST;
41-
4244
/**
4345
* GoFeatureFlagProvider is the JAVA provider implementation for the feature flag solution GO Feature Flag.
4446
*/
@@ -199,7 +201,8 @@ private <T> ProviderEvaluation<T> resolveEvaluationGoFeatureFlagProxy(
199201
if (Reason.DISABLED.name().equalsIgnoreCase(goffResp.getReason())) {
200202
// we don't set a variant since we are using the default value, and we are not able to know
201203
// which variant it is.
202-
return ProviderEvaluation.<T>builder().value(defaultValue).reason(Reason.DISABLED).build();
204+
return ProviderEvaluation.<T>builder().value(defaultValue).reason(Reason.DISABLED.toString())
205+
.build();
203206
}
204207

205208
if (ErrorCode.FLAG_NOT_FOUND.name().equalsIgnoreCase(goffResp.getErrorCode())) {
@@ -215,8 +218,7 @@ private <T> ProviderEvaluation<T> resolveEvaluationGoFeatureFlagProxy(
215218
}
216219

217220
return ProviderEvaluation.<T>builder()
218-
.errorCode(goffResp.getErrorCode())
219-
.reason(mapReason(goffResp.getReason()))
221+
.reason(goffResp.getReason())
220222
.value(flagValue)
221223
.variant(goffResp.getVariationType())
222224
.build();
@@ -227,22 +229,6 @@ private <T> ProviderEvaluation<T> resolveEvaluationGoFeatureFlagProxy(
227229
}
228230
}
229231

230-
231-
/**
232-
* mapReason is mapping the reason in string received by the API to our internal SDK reason enum.
233-
*
234-
* @param reason - string of the reason received from the API
235-
* @return an item from the enum
236-
*/
237-
private Reason mapReason(String reason) {
238-
try {
239-
return Reason.valueOf(reason);
240-
} catch (IllegalArgumentException e) {
241-
return Reason.UNKNOWN;
242-
}
243-
}
244-
245-
246232
/**
247233
* convertValue is converting the object return by the proxy response in the right type.
248234
*
@@ -303,7 +289,7 @@ private Value objectToValue(Object object) {
303289
* @return a Structure object in the SDK format
304290
*/
305291
private Structure mapToStructure(Map<String, Object> map) {
306-
return new Structure(
292+
return new MutableStructure(
307293
map.entrySet().stream()
308294
.filter(e -> e.getValue() != null)
309295
.collect(Collectors.toMap(Map.Entry::getKey, e -> objectToValue(e.getValue()))));

providers/go-feature-flag/src/main/java/dev/openfeature/contrib/providers/gofeatureflag/bean/GoFeatureFlagUser.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package dev.openfeature.contrib.providers.gofeatureflag.bean;
22

33
import dev.openfeature.contrib.providers.gofeatureflag.exception.InvalidTargetingKey;
4-
import dev.openfeature.javasdk.EvaluationContext;
5-
import dev.openfeature.javasdk.Value;
4+
import dev.openfeature.sdk.EvaluationContext;
5+
import dev.openfeature.sdk.Value;
66
import lombok.Builder;
77
import lombok.Getter;
88

providers/go-feature-flag/src/main/java/dev/openfeature/contrib/providers/gofeatureflag/exception/InvalidTargetingKey.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package dev.openfeature.contrib.providers.gofeatureflag.exception;
22

3-
import dev.openfeature.javasdk.ErrorCode;
4-
import dev.openfeature.javasdk.exceptions.OpenFeatureError;
3+
import dev.openfeature.sdk.ErrorCode;
4+
import dev.openfeature.sdk.exceptions.OpenFeatureError;
55
import lombok.experimental.StandardException;
66

77
/**

0 commit comments

Comments
 (0)