Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added greater than or equal to semver matcher #517

Merged
merged 3 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions lib/splitclient-rb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
require 'splitclient-rb/engine/matchers/matches_string_matcher'
require 'splitclient-rb/engine/matchers/semver'
require 'splitclient-rb/engine/matchers/equal_to_semver_matcher'
require 'splitclient-rb/engine/matchers/greater_than_or_equal_to_semver_matcher'
require 'splitclient-rb/engine/evaluator/splitter'
require 'splitclient-rb/engine/impressions/noop_unique_keys_tracker'
require 'splitclient-rb/engine/impressions/unique_keys_tracker'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

module SplitIoClient
class GreaterThanOrEqualToSemverMatcher < Matcher
MATCHER_TYPE = 'GREATER_THAN_OR_EQUAL_TO_SEMVER'

attr_reader :attribute

def initialize(attribute, string_value, logger, validator)
super(logger)
@validator = validator
@attribute = attribute
@semver = SplitIoClient::Semver.build(string_value, logger)
@logger = logger
end

def match?(args)
@logger.log_if_debug('[GreaterThanOrEqualsToSemverMatcher] evaluating value and attributes.')
return false unless @validator.valid_matcher_arguments(args) && args[:attributes][@attribute.to_sym].is_a?(String)

value_to_match = SplitIoClient::Semver.build(args[:attributes][@attribute.to_sym], @logger)
unless !value_to_match.nil? && !@semver.nil?
@logger.log_if_debug('stringMatcherData is required for GREATER_THAN_OR_EQUAL_TO_SEMVER matcher type')
return false
end
matches = [0, 1].include?(value_to_match.compare(@semver))
@logger.log_if_debug("[GreaterThanOrEqualsToSemverMatcher] #{value_to_match} matches -> #{matches}")
matches
end
end
end
8 changes: 8 additions & 0 deletions lib/splitclient-rb/engine/parser/condition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,14 @@ def matcher_equal_to_semver(params)
)
end

def matcher_greater_than_or_equal_to_semver(params)
GreaterThanOrEqualToSemverMatcher.new(
params[:matcher][:keySelector][:attribute],
params[:matcher][:stringMatcherData],
@config.split_logger, @config.split_validator
)
end

#
# @return [object] the negate value for this condition
def negate
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

require 'spec_helper'

describe SplitIoClient::GreaterThanOrEqualToSemverMatcher do
let(:raw) { {
'negate': false,
'matcherType': 'GREATER_THAN_OR_EQUAL_TO_SEMVER',
'stringMatcherData': "2.1.8"
} }
let(:config) { SplitIoClient::SplitConfig.new }

it 'initilized params' do
matcher = described_class.new("version", raw[:stringMatcherData], config.split_logger, config.split_validator)
expect(matcher.attribute).to eq("version")
semver = matcher.instance_variable_get(:@semver)
expect(semver.instance_variable_get(:@old_version)).to eq("2.1.8")
end

it 'matches' do
matcher = described_class.new("version", raw[:stringMatcherData], config.split_logger, config.split_validator)
expect(matcher.match?(:attributes=>{"version": "2.1.8+rc"})).to eq(true)
expect(matcher.match?(:attributes=>{"version": "2.1.8"})).to eq(true)
expect(matcher.match?(:attributes=>{"version": "2.1.11"})).to eq(true)
expect(matcher.match?(:attributes=>{"version": "2.2.0"})).to eq(true)
end

it 'does not match' do
matcher = described_class.new("version", raw[:stringMatcherData], config.split_logger, config.split_validator)
expect(matcher.match?(:attributes=>{"version": "2.1.5"})).to eq(false)
expect(matcher.match?(:attributes=>{"version": "2.1.5-rc1"})).to eq(false)
end

it 'invalid attribute' do
matcher = described_class.new("version", raw[:stringMatcherData], config.split_logger, config.split_validator)
expect(matcher.match?(:attributes=>{"version": 2.1})).to eq(false)
expect(matcher.match?(:attributes=>{"version": nil})).to eq(false)
end

end