Skip to content

Commit 2828442

Browse files
committed
review changes
Signed-off-by: Kavindu Dodanduwa <[email protected]>
1 parent e5321cc commit 2828442

File tree

2 files changed

+24
-26
lines changed

2 files changed

+24
-26
lines changed

src/main/java/dev/openfeature/sdk/FlagMetadata.java

+16-19
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package dev.openfeature.sdk;
22

3-
import dev.openfeature.sdk.exceptions.GeneralError;
4-
import dev.openfeature.sdk.exceptions.ParseError;
3+
import lombok.extern.slf4j.Slf4j;
54

65
import java.util.HashMap;
76
import java.util.Map;
@@ -10,6 +9,7 @@
109
* Immutable Flag Metadata representation. Implementation is backed by a {@link Map} and immutability is provided
1110
* through builder and accessors.
1211
*/
12+
@Slf4j
1313
public class FlagMetadata {
1414
private final Map<String, Object> metadata;
1515

@@ -18,8 +18,8 @@ private FlagMetadata(Map<String, Object> metadata) {
1818
}
1919

2020
/**
21-
* Retrieve a {@link String} value for the given key. If a value is not found, {@link GeneralError} will be thrown.
22-
* If value exist but of another type, {@link ParseError} will be thrown.
21+
* Retrieve a {@link String} value for the given key. A {@code null} value is returned if the key does not exist
22+
* or if the value is of a different type.
2323
*
2424
* @param key flag metadata key to retrieve
2525
*/
@@ -28,9 +28,8 @@ public String getString(final String key) {
2828
}
2929

3030
/**
31-
* Retrieve an {@link Integer} value for the given key.
32-
* If a value is not found, {@link GeneralError} will be thrown.
33-
* If value exist but of another type, {@link ParseError} will be thrown.
31+
* Retrieve a {@link Integer} value for the given key. A {@code null} value is returned if the key does not exist
32+
* or if the value is of a different type.
3433
*
3534
* @param key flag metadata key to retrieve
3635
*/
@@ -39,8 +38,8 @@ public Integer getInteger(final String key) {
3938
}
4039

4140
/**
42-
* Retrieve a {@link Float} value for the given key. If a value is not found, {@link GeneralError} will be thrown.
43-
* If value exist but of another type, {@link ParseError} will be thrown.
41+
* Retrieve a {@link Float} value for the given key. A {@code null} value is returned if the key does not exist
42+
* or if the value is of a different type.
4443
*
4544
* @param key flag metadata key to retrieve
4645
*/
@@ -49,9 +48,8 @@ public Float getFloat(final String key) {
4948
}
5049

5150
/**
52-
* Retrieve a {@link Double} value for the given key.
53-
* If a value is not found, {@link GeneralError} will be thrown.
54-
* If value exist but of another type, {@link ParseError} will be thrown.
51+
* Retrieve a {@link Double} value for the given key. A {@code null} value is returned if the key does not exist
52+
* or if the value is of a different type.
5553
*
5654
* @param key flag metadata key to retrieve
5755
*/
@@ -60,9 +58,8 @@ public Double getDouble(final String key) {
6058
}
6159

6260
/**
63-
* Retrieve a {@link Boolean} value for the given key.
64-
* If a value is not found, {@link GeneralError} will be thrown.
65-
* If value exist but of another type, {@link ParseError} will be thrown.
61+
* Retrieve a {@link Boolean} value for the given key. A {@code null} value is returned if the key does not exist
62+
* or if the value is of a different type.
6663
*
6764
* @param key flag metadata key to retrieve
6865
*/
@@ -74,15 +71,15 @@ private <T> T getValue(final String key, final Class<T> type) {
7471
final Object o = metadata.get(key);
7572

7673
if (o == null) {
77-
throw new GeneralError("key " + key + " does not exist in metadata");
74+
log.debug("Metadata key "+ key+ "does not exist");
75+
return null;
7876
}
7977

8078
try {
8179
return type.cast(o);
8280
} catch (ClassCastException e) {
83-
throw new ParseError(
84-
"wrong type for key " + key
85-
+ ". Expected" + type.getSimpleName() + "but got " + o.getClass().getSimpleName(), e);
81+
log.debug("Error retrieving value for key "+ key, e);
82+
return null;
8683
}
8784
}
8885

src/test/java/dev/openfeature/sdk/FlagMetadataTest.java

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
package dev.openfeature.sdk;
22

3-
import dev.openfeature.sdk.exceptions.GeneralError;
4-
import dev.openfeature.sdk.exceptions.ParseError;
3+
import org.junit.jupiter.api.DisplayName;
54
import org.junit.jupiter.api.Test;
65

76
import static org.assertj.core.api.Assertions.assertThat;
8-
import static org.assertj.core.api.Assertions.assertThatThrownBy;
97

108
class FlagMetadataTest {
119

1210
@Test
11+
@DisplayName("Test metadata payload construction and retrieval")
1312
public void builder_validation() {
1413
// given
1514
FlagMetadata flagMetadata = FlagMetadata.builder()
@@ -29,22 +28,24 @@ public void builder_validation() {
2928
}
3029

3130
@Test
32-
public void parse_error_validation() {
31+
@DisplayName("Value type mismatch returns a null")
32+
public void value_type_validation() {
3333
// given
3434
FlagMetadata flagMetadata = FlagMetadata.builder()
3535
.addString("string", "string")
3636
.build();
3737

3838
// then
39-
assertThatThrownBy(() -> flagMetadata.getBoolean("string")).isInstanceOf(ParseError.class);
39+
assertThat(flagMetadata.getBoolean("string")).isNull();
4040
}
4141

4242
@Test
43+
@DisplayName("A null is returned if key does not exist")
4344
public void notfound_error_validation() {
4445
// given
4546
FlagMetadata flagMetadata = FlagMetadata.builder().build();
4647

4748
// then
48-
assertThatThrownBy(() -> flagMetadata.getBoolean("string")).isInstanceOf(GeneralError.class);
49+
assertThat(flagMetadata.getBoolean("string")).isNull();
4950
}
50-
}
51+
}

0 commit comments

Comments
 (0)