Skip to content

Commit ff5986c

Browse files
authored
Merge pull request #38 from launchdarkly/eb/bucket-non-string
allow bucketing by int attribute (using its string representation)
2 parents 09b96ed + acdfa52 commit ff5986c

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

lib/ldclient-rb/evaluation.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,10 @@ def variation_for_user(rule, user, flag)
253253
def bucket_user(user, key, bucket_by, salt)
254254
return nil unless user[:key]
255255

256-
id_hash = user_value(user, bucket_by)
256+
id_hash = bucketable_string_value(user_value(user, bucket_by))
257+
if id_hash.nil?
258+
return 0.0
259+
end
257260

258261
if user[:secondary]
259262
id_hash += "." + user[:secondary]
@@ -265,6 +268,12 @@ def bucket_user(user, key, bucket_by, salt)
265268
hash_val.to_i(16) / Float(0xFFFFFFFFFFFFFFF)
266269
end
267270

271+
def bucketable_string_value(value)
272+
return value if value.is_a? String
273+
return value.to_s if value.is_a? Integer
274+
nil
275+
end
276+
268277
def user_value(user, attribute)
269278
attribute = attribute.to_sym
270279

spec/evaluation_spec.rb

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,58 @@
127127
end
128128
end
129129
end
130+
131+
describe "bucket_user" do
132+
it "gets expected bucket values for specific keys" do
133+
user = { key: "userKeyA" }
134+
bucket = bucket_user(user, "hashKey", "key", "saltyA")
135+
expect(bucket).to be_within(0.0000001).of(0.42157587);
136+
137+
user = { key: "userKeyB" }
138+
bucket = bucket_user(user, "hashKey", "key", "saltyA")
139+
expect(bucket).to be_within(0.0000001).of(0.6708485);
140+
141+
user = { key: "userKeyC" }
142+
bucket = bucket_user(user, "hashKey", "key", "saltyA")
143+
expect(bucket).to be_within(0.0000001).of(0.10343106);
144+
end
145+
146+
it "can bucket by int value (equivalent to string)" do
147+
user = {
148+
key: "userkey",
149+
custom: {
150+
stringAttr: "33333",
151+
intAttr: 33333
152+
}
153+
}
154+
stringResult = bucket_user(user, "hashKey", "stringAttr", "saltyA")
155+
intResult = bucket_user(user, "hashKey", "intAttr", "saltyA")
156+
157+
expect(intResult).to be_within(0.0000001).of(0.54771423)
158+
expect(intResult).to eq(stringResult)
159+
end
160+
161+
it "cannot bucket by float value" do
162+
user = {
163+
key: "userkey",
164+
custom: {
165+
floatAttr: 33.5
166+
}
167+
}
168+
result = bucket_user(user, "hashKey", "floatAttr", "saltyA")
169+
expect(result).to eq(0.0)
170+
end
171+
172+
173+
it "cannot bucket by bool value" do
174+
user = {
175+
key: "userkey",
176+
custom: {
177+
boolAttr: true
178+
}
179+
}
180+
result = bucket_user(user, "hashKey", "boolAttr", "saltyA")
181+
expect(result).to eq(0.0)
182+
end
183+
end
130184
end

0 commit comments

Comments
 (0)