Skip to content
This repository was archived by the owner on Dec 6, 2024. It is now read-only.

Commit 04a15e3

Browse files
prepare 2.3.2 release (#31)
1 parent a3cf131 commit 04a15e3

File tree

2 files changed

+45
-11
lines changed

2 files changed

+45
-11
lines changed

src/evaluate.c

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,27 @@ addValue(
112112
return LDBooleanTrue;
113113
}
114114

115+
static const char *
116+
LDi_getBucketAttribute(const struct LDJSON *const obj)
117+
{
118+
const struct LDJSON *bucketBy;
119+
120+
LD_ASSERT(obj);
121+
LD_ASSERT(LDJSONGetType(obj) == LDObject);
122+
123+
bucketBy = LDObjectLookup(obj, "bucketBy");
124+
125+
if (!LDi_notNull(bucketBy)) {
126+
return "key";
127+
}
128+
129+
if (LDJSONGetType(bucketBy) != LDText) {
130+
return NULL;
131+
}
132+
133+
return LDGetText(bucketBy);
134+
}
135+
115136
EvalStatus
116137
LDi_evaluate(
117138
struct LDClient *const client,
@@ -914,19 +935,18 @@ LDi_segmentRuleMatchUser(
914935

915936
{
916937
float bucket;
938+
const char *attribute;
917939

918-
const struct LDJSON *const bucketBy =
919-
LDObjectLookup(segmentRule, "bucketBy");
920-
921-
const char *const attribute =
922-
LDi_notNull(bucketBy) ? LDGetText(bucketBy) : "key";
940+
attribute = LDi_getBucketAttribute(segmentRule);
923941

924-
if (!LDi_bucketUser(user, segmentKey, attribute, salt, &bucket)) {
925-
LD_LOG(LD_LOG_ERROR, "LDi_bucketUser error");
942+
if (attribute == NULL) {
943+
LD_LOG(LD_LOG_ERROR, "failed to parse bucketBy");
926944

927-
return EVAL_MEM;
945+
return EVAL_SCHEMA;
928946
}
929947

948+
LDi_bucketUser(user, segmentKey, attribute, salt, &bucket);
949+
930950
if (bucket < LDGetNumber(weight) / 100000) {
931951
return EVAL_MATCH;
932952
} else {
@@ -1127,6 +1147,7 @@ LDi_bucketUser(
11271147
LD_ASSERT(bucket);
11281148

11291149
attributeValue = NULL;
1150+
*bucket = 0;
11301151

11311152
if ((attributeValue = LDi_valueOfAttribute(user, attribute))) {
11321153
char raw[256], bucketableBuffer[256];
@@ -1261,10 +1282,18 @@ LDi_variationIndexForUser(
12611282
variation = LDGetIter(variations);
12621283
LD_ASSERT(variation);
12631284

1264-
if (!LDi_bucketUser(user, key, "key", salt, &userBucket)) {
1265-
LD_LOG(LD_LOG_ERROR, "failed to bucket user");
1285+
{
1286+
const char *attribute;
12661287

1267-
return LDBooleanFalse;
1288+
attribute = LDi_getBucketAttribute(rollout);
1289+
1290+
if (attribute == NULL) {
1291+
LD_LOG(LD_LOG_ERROR, "failed to parse bucketBy");
1292+
1293+
return LDBooleanFalse;
1294+
}
1295+
1296+
LDi_bucketUser(user, key, attribute, salt, &userBucket);
12681297
}
12691298

12701299
for (; variation; variation = LDIterNext(variation)) {

tests/test-eval.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,6 +1152,11 @@ testLDi_bucketUserByKey()
11521152
LD_ASSERT(LDi_bucketUser(user, "hashKey", "key", "saltyA", &bucket))
11531153
LD_ASSERT(floateq(0.10343106, bucket));
11541154
LDUserFree(user);
1155+
1156+
LD_ASSERT(user = LDUserNew("userKeyC"));
1157+
LD_ASSERT(!LDi_bucketUser(user, "hashKey", "unknown", "saltyA", &bucket))
1158+
LD_ASSERT(floateq(0.0, bucket));
1159+
LDUserFree(user);
11551160
}
11561161

11571162
int

0 commit comments

Comments
 (0)