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

feat: Define equality methods for LDContext and Reference #232

Merged
merged 1 commit into from
Dec 22, 2023
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
39 changes: 39 additions & 0 deletions contract-tests/client_entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,45 @@ def migration_operation(params)
end
end

def context_comparison(params)
context1 = build_context_from_params(params[:context1])
context2 = build_context_from_params(params[:context2])

context1 == context2
end

private def build_context_from_params(params)
return build_single_context_from_attribute_definitions(params[:single]) unless params[:single].nil?

contexts = params[:multi].map do |param|
build_single_context_from_attribute_definitions(param)
end

LaunchDarkly::LDContext.create_multi(contexts)
end

private def build_single_context_from_attribute_definitions(params)
context = {kind: params[:kind], key: params[:key]}

params[:attributes]&.each do |attribute|
context[attribute[:name]] = attribute[:value]
end

if params[:privateAttributes]
context[:_meta] = {
privateAttributes: params[:privateAttributes].map do |attribute|
if attribute[:literal]
LaunchDarkly::Reference.create_literal(attribute[:value])
else
LaunchDarkly::Reference.create(attribute[:value])
end
end,
}
end

LaunchDarkly::LDContext.create(context)
end

def secure_mode_hash(params)
@client.secure_mode_hash(params[:context])
end
Expand Down
4 changes: 4 additions & 0 deletions contract-tests/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
'tags',
'migrations',
'event-sampling',
'context-comparison',
],
}.to_json
end
Expand Down Expand Up @@ -109,6 +110,9 @@
when "migrationOperation"
response = {:result => client.migration_operation(params[:migrationOperation]).to_s}
return [200, nil, response.to_json]
when "contextComparison"
response = {:equals => client.context_comparison(params[:contextComparison])}
return [200, nil, response.to_json]
end

return [400, nil, {:error => "Unknown command requested"}.to_json]
Expand Down
44 changes: 39 additions & 5 deletions lib/ldclient-rb/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ class LDContext
# @return [String, nil] Returns the error associated with this LDContext if invalid
attr_reader :error

# @return [Array<Reference>] Returns the private attributes associated with this LDContext
attr_reader :private_attributes

#
# @private
# @param key [String, nil]
Expand All @@ -69,17 +66,27 @@ def initialize(key, fully_qualified_key, kind, name = nil, anonymous = nil, attr
@name = name
@anonymous = anonymous || false
@attributes = attributes
@private_attributes = []
@private_attributes = Set.new
(private_attributes || []).each do |attribute|
reference = Reference.create(attribute)
@private_attributes << reference if reference.error.nil?
@private_attributes.add(reference) if reference.error.nil?
end
@error = error
@contexts = contexts
@is_multi = !contexts.nil?
end
private_class_method :new

protected attr_reader :name, :anonymous, :attributes

#
# @return [Array<Reference>] Returns the private attributes associated with this LDContext
#
def private_attributes
# TODO(sc-227265): Return a set instead of an array.
@private_attributes.to_a
end

#
# @return [Boolean] Is this LDContext a multi-kind context?
#
Expand Down Expand Up @@ -274,6 +281,33 @@ def individual_context(kind)
nil
end

def ==(other)
return false unless self.kind == other.kind
return false unless self.valid? == other.valid?
return false unless self.error == other.error

return false unless self.individual_context_count == other.individual_context_count

if self.multi_kind?
self.kinds.each do |kind|
return false unless self.individual_context(kind) == other.individual_context(kind)
end

return true
end

return false unless self.key == other.key
return false unless self.name == other.name
return false unless self.anonymous == other.anonymous
return false unless self.attributes == other.attributes

# TODO(sc-227265): Calling .to_set is unnecessary once private_attributes are sets.
return false unless self.private_attributes.to_set == other.private_attributes.to_set

true
end
alias eql? ==

#
# Retrieve the value of any top level, addressable attribute.
#
Expand Down
2 changes: 1 addition & 1 deletion lib/ldclient-rb/impl/context_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,4 @@ def filter(context)
end
end
end
end
end
11 changes: 11 additions & 0 deletions lib/ldclient-rb/reference.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ def initialize(raw_path, components = [], error = nil)
end
private_class_method :new

protected attr_reader :components

#
# Creates a Reference from a string. For the supported syntax and examples,
# see comments on the Reference type.
Expand Down Expand Up @@ -227,6 +229,15 @@ def component(index)
@components[index]
end

def ==(other)
self.error == other.error && self.components == other.components
end
alias eql? ==

def hash
([error] + components).hash
end

#
# Performs unescaping of attribute reference path components:
#
Expand Down
63 changes: 63 additions & 0 deletions spec/context_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -296,5 +296,68 @@ module LaunchDarkly
end
end
end

describe "equality comparisons" do
it "single kind contexts are equal" do
original_context = subject.create(
{ key: 'context-key', kind: 'user', name: 'Example name', groups: ['test', 'it', 'here'], address: {street: '123 Easy St', city: 'Every Town'},
_meta: { privateAttributes: ['name', 'out of order attribute'] }
})
duplicate_context = subject.create(
{ key: 'context-key', kind: 'user', name: 'Example name', groups: ['test', 'it', 'here'], address: {street: '123 Easy St', city: 'Every Town'},
_meta: { privateAttributes: ['out of order attribute', 'name'] }
})
expect(original_context).to eq(duplicate_context)
end

it "multi kind contexts are equal" do
org_context = subject.create({ key: 'org-key', kind: 'org' })
user_context = subject.create({ key: 'user-key', kind: 'user' })
device_context = subject.create({ key: 'device-key', kind: 'device' })

original_context = subject.create_multi([org_context, user_context])
duplicate_context = subject.create_multi([user_context, org_context])

expect(original_context).to eq(duplicate_context)

superset_context = subject.create_multi([org_context, user_context, device_context])
expect(superset_context).not_to eq(duplicate_context)
end

it "mixed size contexts are not equal" do
org_context = subject.create({ key: 'org-key', kind: 'org' })
user_context = subject.create({ key: 'user-key', kind: 'user' })

flattened_multi = subject.create_multi([org_context])

expect(flattened_multi).to eq(org_context)

multi = subject.create_multi([org_context, user_context])
expect(multi).not_to eq(org_context)
expect(multi).not_to eq(user_context)
end

it "failed contexts can be equal" do
invalid_hash = subject.create(true)
invalid_kind = subject.create({ kind: 'this is not valid' })
invalid_key = subject.create({ key: nil })
invalid_name = subject.create({ key: 'user-key', name: true })
invalid_anonymous = subject.create({ key: 'user-key', anonymous: 'this is no boolean' })
invalid_private_attributes = subject.create({ key: 'user-key', _meta: { privateAttributes: 'this is no array' }})

expect(invalid_hash).not_to eq(invalid_kind)
expect(invalid_hash).not_to eq(invalid_key)
expect(invalid_hash).not_to eq(invalid_name)
expect(invalid_hash).not_to eq(invalid_anonymous)
expect(invalid_hash).not_to eq(invalid_private_attributes)

expect(invalid_hash).to eq(subject.create(true))
expect(invalid_kind).to eq(subject.create({ kind: 'this is not valid' }))
expect(invalid_key).to eq(subject.create({ key: nil }))
expect(invalid_name).to eq(subject.create({ key: 'user-key', name: true }))
expect(invalid_anonymous).to eq(subject.create({ key: 'user-key', anonymous: 'this is no boolean' }))
expect(invalid_private_attributes).to eq(subject.create({ key: 'user-key', _meta: { privateAttributes: 'this is no array' }}))
end
end
end
end
Loading