Skip to content

Commit f0a53b7

Browse files
committed
feat: json-path-lib-implementation
1 parent 3bad95b commit f0a53b7

File tree

4 files changed

+11
-14
lines changed

4 files changed

+11
-14
lines changed

Gemfile.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ PATH
44
flagsmith (4.3.0)
55
faraday (>= 2.0.1)
66
faraday-retry
7+
jsonpath (~> 1.1)
78
semantic
89

910
GEM
@@ -21,8 +22,11 @@ GEM
2122
faraday (~> 2.0)
2223
gem-release (2.2.2)
2324
json (2.7.1)
25+
jsonpath (1.1.5)
26+
multi_json
2427
language_server-protocol (3.17.0.3)
2528
method_source (1.0.0)
29+
multi_json (1.17.0)
2630
net-http (0.4.1)
2731
uri
2832
parallel (1.24.0)

flagsmith.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Gem::Specification.new do |spec|
3434

3535
spec.add_dependency 'faraday', '>= 2.0.1'
3636
spec.add_dependency 'faraday-retry'
37+
spec.add_dependency 'jsonpath', '~> 1.1'
3738
spec.add_dependency 'semantic'
3839
spec.metadata['rubygems_mfa_required'] = 'true'
3940
end

lib/flagsmith/engine/segments/evaluator.rb

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# frozen_string_literal: true
22

3+
require 'json'
4+
require 'jsonpath'
35
require_relative 'constants'
46
require_relative 'models'
57
require_relative '../utils/hash_func'
@@ -200,25 +202,15 @@ def get_trait_value(property, context)
200202
traits[property] || traits[property.to_sym]
201203
end
202204

203-
# Get value from context using JSONPath-like syntax
205+
# Get value from context using JSONPath syntax
204206
#
205207
# @param json_path [String] JSONPath expression (e.g., '$.identity.identifier')
206208
# @param context [Hash] The evaluation context
207209
# @return [Object, nil] The value at the path or nil
208210
def get_context_value(json_path, context)
209211
return nil unless context && json_path&.start_with?('$.')
210-
211-
# Simple JSONPath implementation - handle basic cases
212-
path_parts = json_path.sub('$.', '').split('.')
213-
current = context
214-
215-
path_parts.each do |part|
216-
return nil unless current.is_a?(Hash)
217-
218-
current = current[part.to_sym]
219-
end
220-
221-
current
212+
results = JsonPath.new(json_path, use_symbols: true).on(context)
213+
results.first
222214
rescue StandardError
223215
nil
224216
end

0 commit comments

Comments
 (0)