Skip to content

Commit 2163c63

Browse files
authored
Merge pull request #31 from Flagsmith/feat/segment-in-operator
feat: implement `IN` operator
2 parents fabd7fb + 5ef5c87 commit 2163c63

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

lib/flagsmith/engine/segments/constants.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ module Constants
2525
IS_SET = 'IS_SET'
2626
IS_NOT_SET = 'IS_NOT_SET'
2727
MODULO = 'MODULO'
28+
IN = 'IN'
2829

2930
CONDITION_OPERATORS = [
3031
EQUAL,
@@ -37,7 +38,8 @@ module Constants
3738
NOT_EQUAL,
3839
REGEX,
3940
PERCENTAGE_SPLIT,
40-
MODULO
41+
MODULO,
42+
IN
4143
].freeze
4244
end
4345
end

lib/flagsmith/engine/segments/models.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ def match_trait_value?(trait_value)
5858
# handle some exceptions
5959
trait_value = Semantic::Version.new(trait_value.gsub(/:semver$/, '')) if @value.is_a?(String) && @value.match?(/:semver$/)
6060

61+
return match_in_value(trait_value) if @operator == IN
6162
return match_modulo_value(trait_value) if @operator == MODULO
6263

6364
type_as_trait_value = format_to_type_of(trait_value)
@@ -86,6 +87,12 @@ def match_modulo_value(trait_value)
8687
false
8788
end
8889

90+
def match_in_value(trait_value)
91+
return @value.split(',').include?(trait_value.to_s) if trait_value.is_a?(String) || trait_value.is_a?(Integer)
92+
93+
false
94+
end
95+
8996
class << self
9097
def build(json)
9198
new(**json.slice(:operator, :value).merge(property: json[:property_]))

spec/engine/unit/segments/models_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,18 @@
7979
['MODULO', 2.0, '2|0', true],
8080
['MODULO', 'foo', '2|0', false],
8181
['MODULO', 'foo', 'foo|bar', false],
82+
['IN', 'foo', '', false],
83+
['IN', 'foo', 'foo,bar', true],
84+
['IN', 'bar', 'foo,bar', true],
85+
['IN', 'ba', 'foo,bar', false],
86+
['IN', 'foo', 'foo', true],
87+
['IN', 1, '1,2,3,4', true],
88+
['IN', 1, '', false],
89+
['IN', 1, '1', true],
90+
# Flagsmith's engine does not evaluate `IN` condition for floats/doubles and booleans
91+
# due to ambiguous serialization across supported platforms.
92+
['IN', 1.5, '1.5', false],
93+
['IN', false, 'false', false],
8294
].freeze
8395

8496
RSpec.describe Flagsmith::Engine::Segments::Condition do

0 commit comments

Comments
 (0)