11package com .featureprobe .api .model ;
22
33import com .featureprobe .api .base .exception .ServerToggleBuildException ;
4+ import com .featureprobe .api .entity .Segment ;
45import com .featureprobe .api .mapper .JsonMapper ;
6+ import com .featureprobe .sdk .server .model .Condition ;
57import com .featureprobe .sdk .server .model .ConditionType ;
68import com .featureprobe .sdk .server .model .Rule ;
79import com .featureprobe .sdk .server .model .Toggle ;
810import com .google .common .collect .Maps ;
911import org .apache .commons .collections4 .CollectionUtils ;
1012import org .apache .commons .lang3 .BooleanUtils ;
11- import org .apache .commons .lang3 .StringUtils ;
1213
1314import java .math .BigDecimal ;
1415import java .util .Collections ;
@@ -21,9 +22,11 @@ public class ServerToggleBuilder {
2122 private Toggle toggle ;
2223 private Variation .ValueConverter variationValueConverter ;
2324 private TargetingContent targetingContent ;
25+ private Map <String , Segment > segments ;
2426
2527 private static Map <String , Variation .ValueConverter <?>> converters = Maps .newHashMap ();
2628
29+
2730 static {
2831 converters .put ("string" , value -> value );
2932 converters .put ("boolean" , value -> Boolean .valueOf (value ));
@@ -62,6 +65,11 @@ public ServerToggleBuilder rules(String rules) {
6265 return this ;
6366 }
6467
68+ public ServerToggleBuilder segments (Map <String , Segment > segments ) {
69+ this .segments = segments ;
70+ return this ;
71+ }
72+
6573 public ServerToggleBuilder returnType (String returnType ) {
6674 this .variationValueConverter = getReturnTypeConverter (returnType );
6775 return this ;
@@ -74,12 +82,11 @@ private Variation.ValueConverter<?> getReturnTypeConverter(String type) {
7482 throw new ServerToggleBuildException ("return type is unknown:" + type );
7583 }
7684
77- public Toggle build (String projectKey ) {
85+ public Toggle build () {
7886 this .setDisabledServe ();
7987 this .setDefaultServe ();
8088 this .setVariations ();
81- this .setRules (projectKey );
82-
89+ this .setRules ();
8390 return toggle ;
8491 }
8592
@@ -113,7 +120,7 @@ private void setVariations() {
113120 toggle .setVariations (variations );
114121 }
115122
116- private void setRules (String projectKey ) {
123+ private void setRules () {
117124 if (CollectionUtils .isEmpty (targetingContent .getRules ())) {
118125 toggle .setRules (Collections .emptyList ());
119126 return ;
@@ -122,10 +129,14 @@ private void setRules(String projectKey) {
122129 rule .toRule ()).collect (Collectors .toList ());
123130 rules .forEach (rule -> rule .getConditions ().forEach (condition -> {
124131 if (condition .getType () != ConditionType .SEGMENT ) return ;
125- condition .setObjects (condition .getObjects ().stream ().map (segmentKey ->
126- StringUtils .join (projectKey , '$' , segmentKey )).collect (Collectors .toList ()));
132+ replaceSegmentKeyToUniqueKey (condition );
127133 }));
128134 toggle .setRules (rules );
129135 }
130136
137+ private void replaceSegmentKeyToUniqueKey (Condition condition ) {
138+ condition .setObjects (condition .getObjects ().stream ().map (segmentKey ->
139+ segments .get (segmentKey ).getUniqueKey ()).collect (Collectors .toList ()));
140+ }
141+
131142}
0 commit comments