Skip to content
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
9 changes: 1 addition & 8 deletions lib/prefab/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,6 @@ def reportable_tree
end

def to_proto(namespace)
prefab_context = {
'current-time' => ConfigValueWrapper.wrap(Prefab::TimeHelpers.now_in_ms)
}

prefab_context['namespace'] = ConfigValueWrapper.wrap(namespace) if namespace&.length&.positive?

reportable_contexts = {}

reportable_tree.each do |ctx|
Expand All @@ -217,8 +211,7 @@ def to_proto(namespace)
PrefabProto::ContextSet.new(
contexts: reportable_contexts.map do |name, context|
context.to_proto
end.concat([PrefabProto::Context.new(type: 'prefab',
values: prefab_context)])
end
)
end

Expand Down
16 changes: 9 additions & 7 deletions lib/prefab/criteria_evaluator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,7 @@ def HIERARCHICAL_MATCH(criterion, properties)
end

def IN_INT_RANGE(criterion, properties)
value = if criterion.property_name == 'prefab.current-time'
Time.now.utc.to_i * 1000
else
value_from_properties(criterion, properties)
end

value = value_from_properties(criterion, properties)
value && value >= criterion.value_to_match.int_range.start && value < criterion.value_to_match.int_range.end
end

Expand Down Expand Up @@ -150,7 +145,14 @@ def PROP_SEMVER_GREATER_THAN(criterion, properties)
end

def value_from_properties(criterion, properties)
criterion.property_name == NAMESPACE_KEY ? @namespace : properties.get(criterion.property_name)
case criterion.property_name
when NAMESPACE_KEY
@namespace
when 'prefab.current-time'
Time.now.utc.to_i * 1000
else
properties.get(criterion.property_name)
end
end

COMPARE_TO_OPERATORS = {
Expand Down
1 change: 0 additions & 1 deletion test/support/common_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ def setup
Prefab::Context.default_context.clear
SemanticLogger.add_appender(io: $logs, filter: Prefab.log_filter)
SemanticLogger.sync!
Timecop.freeze('2023-08-09 15:18:12 -0400')
end

def teardown
Expand Down
15 changes: 0 additions & 15 deletions test/test_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,6 @@ def test_to_proto
"id" => PrefabProto::ConfigValue.new(int: 2),
"name" => PrefabProto::ConfigValue.new(string: "team-name")
}
),

PrefabProto::Context.new(
type: "prefab",
values: {
'current-time' => PrefabProto::ConfigValue.new(int: Prefab::TimeHelpers.now_in_ms),
'namespace' => PrefabProto::ConfigValue.new(string: namespace)
}
)
]
), contexts.to_proto(namespace)
Expand Down Expand Up @@ -226,13 +218,6 @@ def test_to_proto_with_parent
"name" => PrefabProto::ConfigValue.new(string: "team-name")
}
),
# via to_proto
PrefabProto::Context.new(
type: "prefab",
values: {
'current-time' => PrefabProto::ConfigValue.new(int: Prefab::TimeHelpers.now_in_ms),
}
)
]
)

Expand Down
22 changes: 22 additions & 0 deletions test/test_criteria_evaluator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1068,6 +1068,28 @@ def test_semver_greater_than
assert_equal DESIRED_VALUE, evaluator.evaluate(context(user:{version: "3.0.0"})).unwrapped_value
end

def test_date_before_with_current_time
future_time = Time.now.utc + 3600 # 1 hour in the future
config = create_prefab_config(
operator: PrefabProto::Criterion::CriterionOperator::PROP_BEFORE,
property_name: 'prefab.current-time',
value_to_match: future_time.iso8601
)
evaluator = Prefab::CriteriaEvaluator.new(config, project_env_id: PROJECT_ENV_ID, resolver: nil, base_client: @base_client, namespace: nil)
assert_equal DESIRED_VALUE, evaluator.evaluate(context({})).unwrapped_value
end

def test_date_after_with_current_time
past_time = Time.now.utc - 3600 # 1 hour in the past
config = create_prefab_config(
operator: PrefabProto::Criterion::CriterionOperator::PROP_AFTER,
property_name: 'prefab.current-time',
value_to_match: past_time.iso8601
)
evaluator = Prefab::CriteriaEvaluator.new(config, project_env_id: PROJECT_ENV_ID, resolver: nil, base_client: @base_client, namespace: nil)
assert_equal DESIRED_VALUE, evaluator.evaluate(context({})).unwrapped_value
end

private

def string_list(values)
Expand Down