Skip to content

Commit 1405c65

Browse files
authored
Prerequisite refeat (#38)
✨ feat: prerequisite refactor
1 parent 8585416 commit 1405c65

File tree

3 files changed

+71
-22
lines changed

3 files changed

+71
-22
lines changed

pom.xml

+19-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>com.featureprobe</groupId>
77
<artifactId>server-sdk-java</artifactId>
8-
<version>2.1.0</version>
8+
<version>2.1.1</version>
99
<name>server-sdk-java</name>
1010
<description>FeatureProbe Server Side SDK for Java</description>
1111

@@ -302,16 +302,29 @@
302302
</plugin>
303303
<plugin>
304304
<groupId>org.apache.maven.plugins</groupId>
305-
<artifactId>maven-dependency-plugin</artifactId>
306-
<version>3.3.0</version>
305+
<artifactId>maven-assembly-plugin</artifactId>
306+
<version>3.4.1</version>
307+
<configuration>
308+
<appendAssemblyId>false</appendAssemblyId>
309+
<descriptorRefs>
310+
<descriptorRef>jar-with-dependencies</descriptorRef>
311+
</descriptorRefs>
312+
<archive>
313+
<manifest>
314+
<addClasspath>true</addClasspath>
315+
<classpathPrefix>lib/</classpathPrefix>
316+
<mainClass>com.featureprobe.sdk.example.FeatureProbeDemo</mainClass>
317+
</manifest>
318+
</archive>
319+
</configuration>
307320
<executions>
308321
<execution>
309-
<id>analyze-only-in-package</id>
322+
<id>make-assembly</id>
310323
<goals>
311-
<goal>analyze-only</goal>
324+
<goal>single</goal>
312325
</goals>
313326
<phase>package</phase>
314-
<configuration />
327+
315328
</execution>
316329
</executions>
317330
</plugin>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package com.featureprobe.sdk.server.exceptions;
19+
20+
public class PrerequisiteException extends RuntimeException {
21+
22+
public PrerequisiteException(String message) {
23+
super(message);
24+
}
25+
26+
}

src/main/java/com/featureprobe/sdk/server/model/Toggle.java

+26-16
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import com.featureprobe.sdk.server.EvaluationResult;
2121
import com.featureprobe.sdk.server.FPUser;
2222
import com.featureprobe.sdk.server.HitResult;
23-
import com.featureprobe.sdk.server.exceptions.PrerequisitesDeepOverflowException;
23+
import com.featureprobe.sdk.server.exceptions.PrerequisiteException;
2424

2525
import java.util.List;
2626
import java.util.Map;
@@ -53,6 +53,20 @@ public final class Toggle {
5353

5454
public EvaluationResult eval(FPUser user, Map<String, Toggle> toggles, Map<String, Segment> segments,
5555
Object defaultValue, int deep) {
56+
EvaluationResult result = createDefaultResult(user, key, defaultValue, "");
57+
try {
58+
return doEval(user, toggles, segments, defaultValue, deep);
59+
} catch (PrerequisiteException e) {
60+
result.setReason(e.getMessage());
61+
} catch (Exception e) {
62+
throw e;
63+
}
64+
65+
return result;
66+
}
67+
68+
public EvaluationResult doEval(FPUser user, Map<String, Toggle> toggles, Map<String, Segment> segments,
69+
Object defaultValue, int deep) {
5670

5771
String warning = "";
5872

@@ -61,7 +75,7 @@ public EvaluationResult eval(FPUser user, Map<String, Toggle> toggles, Map<Strin
6175
}
6276

6377
if (deep <= 0) {
64-
throw new PrerequisitesDeepOverflowException("prerequisite deep overflow");
78+
throw new PrerequisiteException("prerequisite deep overflow");
6579
}
6680

6781
if (!prerequisite(user, toggles, segments, deep)) {
@@ -85,7 +99,7 @@ public EvaluationResult eval(FPUser user, Map<String, Toggle> toggles, Map<Strin
8599
private EvaluationResult createDisabledResult(FPUser user, String toggleKey, Object defaultValue) {
86100
EvaluationResult disabledResult = hitValue(disabledServe.evalIndex(user, this.key),
87101
defaultValue, Optional.empty());
88-
disabledResult.setReason("Toggle disabled");
102+
disabledResult.setReason("Toggle disabled.");
89103
return disabledResult;
90104
}
91105

@@ -100,19 +114,15 @@ private boolean prerequisite(FPUser user, Map<String, Toggle> toggles, Map<Strin
100114
if (Objects.isNull(prerequisites) || prerequisites.isEmpty()) {
101115
return true;
102116
}
103-
try {
104-
for (Prerequisite prerequisite : prerequisites) {
105-
Toggle toggle = toggles.get(prerequisite.getKey());
106-
if (Objects.isNull(toggle))
107-
return false;
108-
EvaluationResult eval = toggle.eval(user, toggles, segments, null, deep - 1);
109-
if (Objects.isNull(eval.getValue()))
110-
return false;
111-
if (!eval.getValue().equals(prerequisite.getValue()))
112-
return false;
113-
}
114-
} catch (PrerequisitesDeepOverflowException e) {
115-
throw e;
117+
for (Prerequisite prerequisite : prerequisites) {
118+
Toggle toggle = toggles.get(prerequisite.getKey());
119+
if (Objects.isNull(toggle))
120+
throw new PrerequisiteException("prerequisite not exist: " + this.key);
121+
EvaluationResult eval = toggle.doEval(user, toggles, segments, null, deep - 1);
122+
if (Objects.isNull(eval.getValue()))
123+
return false;
124+
if (!eval.getValue().equals(prerequisite.getValue()))
125+
return false;
116126
}
117127
return true;
118128
}

0 commit comments

Comments
 (0)