Skip to content

Commit cf878a1

Browse files
committed
allow bucketing by int attribute (using its string representation)
1 parent d2cf96c commit cf878a1

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

lib/ldclient-rb/evaluation.rb

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

226-
id_hash = user_value(user, bucket_by)
226+
id_hash = bucketable_string_value(user_value(user, bucket_by))
227+
if id_hash.nil?
228+
return 0.0
229+
end
227230

228231
if user[:secondary]
229232
id_hash += "." + user[:secondary]
@@ -235,6 +238,12 @@ def bucket_user(user, key, bucket_by, salt)
235238
hash_val.to_i(16) / Float(0xFFFFFFFFFFFFFFF)
236239
end
237240

241+
def bucketable_string_value(value)
242+
return value if value.is_a? String
243+
return value.to_s if value.is_a? Integer
244+
nil
245+
end
246+
238247
def user_value(user, attribute)
239248
attribute = attribute.to_sym
240249

spec/evaluation_spec.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
require "spec_helper"
2+
3+
describe LaunchDarkly::Evaluation do
4+
subject { LaunchDarkly::Evaluation }
5+
6+
include LaunchDarkly::Evaluation
7+
8+
describe "bucket_user" do
9+
it "can bucket by int value (equivalent to string)" do
10+
user = {
11+
key: "userkey",
12+
custom: {
13+
stringAttr: "33333",
14+
intAttr: 33333
15+
}
16+
}
17+
stringResult = bucket_user(user, "key", "stringAttr", "salt")
18+
intResult = bucket_user(user, "key", "intAttr", "salt")
19+
expect(intResult).to eq(stringResult)
20+
end
21+
22+
it "cannot bucket by float value" do
23+
user = {
24+
key: "userkey",
25+
custom: {
26+
floatAttr: 33.5
27+
}
28+
}
29+
result = bucket_user(user, "key", "floatAttr", "salt")
30+
expect(result).to eq(0.0)
31+
end
32+
33+
34+
it "cannot bucket by bool value" do
35+
user = {
36+
key: "userkey",
37+
custom: {
38+
boolAttr: true
39+
}
40+
}
41+
result = bucket_user(user, "key", "boolAttr", "salt")
42+
expect(result).to eq(0.0)
43+
end
44+
end
45+
end

0 commit comments

Comments
 (0)