Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.lightswitch.sdk.model;

import org.lightswitch.sdk.user.LightSwitchUser;

import java.util.Collections;
import java.util.Map;

public class FeatureFlag {

private final String key;
private final Object defaultValue;
private final Map<Map<String, String>, Object> filters; // filtering rules
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that the filters have been added to flag, it seems like similar modifications should be needed in the backend entity and API spec as well. It would be good to review and align everything before moving forward.


public FeatureFlag(String key, Object defaultValue, Map<Map<String, String>, Object> filters) {
this.key = key;
this.defaultValue = defaultValue;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't defaultValue also require Null check?

this.filters = filters != null ? filters : Collections.emptyMap();
}

public String getKey() {
return key;
}

public Object getDefaultValue() {
return defaultValue;
}

public Map<Map<String, String>, Object> getConditions() {
return Collections.unmodifiableMap(filters);
}

/**
* Find and return appropriate flag values based on user properties
*/
public Object evaluate(LightSwitchUser user) {
for (Map.Entry<Map<String, String>, Object> entry : filters.entrySet()) {
Map<String, String> conditions = entry.getKey();
boolean match = conditions.entrySet().stream()
.allMatch(cond -> user.getAttributes().getOrDefault(cond.getKey(), "").equals(cond.getValue()));

if (match) {
return entry.getValue();
}
}
return defaultValue;
Comment on lines +36 to +45
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we simplify the logic using Streams, it might be more readable than using a for loop and manually iterating.

return filters.entrySet().stream()
        .filter(entry -> entry.getKey().entrySet().stream()
                .allMatch(cond -> user.getAttributes().getOrDefault(cond.getKey(), "").equals(cond.getValue())))
        .map(Map.Entry::getValue)
        .findFirst()
        .orElse(defaultValue);

}

@Override
public String toString() {
return "FeatureFlag{" +
"key='" + key + '\'' +
", defaultValue=" + defaultValue +
", filters=" + filters +
'}';
}
}