Skip to content

Commit 42adc78

Browse files
authored
refactor: update go feature flag provider to use not deprecated context transformer (open-feature#92)
Signed-off-by: Michael Beemer <[email protected]>
1 parent 4e9e6eb commit 42adc78

File tree

5 files changed

+226
-205
lines changed

5 files changed

+226
-205
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { EvaluationContext } from '@openfeature/nodejs-sdk';
2+
import { GoFeatureFlagUser } from './model';
3+
import { transformContext } from './context-transformer';
4+
5+
describe('contextTransformer', () => {
6+
it('should use the targetingKey as user key', () => {
7+
const got = transformContext({
8+
targetingKey: 'user-key',
9+
} as EvaluationContext);
10+
const want: GoFeatureFlagUser = {
11+
key: 'user-key',
12+
anonymous: false,
13+
custom: {},
14+
};
15+
expect(got).toEqual(want);
16+
});
17+
18+
it('should specify the anonymous field base on attributes', () => {
19+
const got = transformContext({
20+
targetingKey: 'user-key',
21+
anonymous: true,
22+
} as EvaluationContext);
23+
const want: GoFeatureFlagUser = {
24+
key: 'user-key',
25+
anonymous: true,
26+
custom: {},
27+
};
28+
expect(got).toEqual(want);
29+
});
30+
31+
it('should hash the context as key if no targetingKey provided', () => {
32+
const got = transformContext({
33+
anonymous: true,
34+
firstname: 'John',
35+
lastname: 'Doe',
36+
37+
} as EvaluationContext);
38+
39+
const want: GoFeatureFlagUser = {
40+
key: 'dd3027562879ff6857cc6b8b88ced570546d7c0c',
41+
anonymous: true,
42+
custom: {
43+
firstname: 'John',
44+
lastname: 'Doe',
45+
46+
},
47+
};
48+
expect(got).toEqual(want);
49+
});
50+
it('should fill custom fields if extra field are present', () => {
51+
const got = transformContext({
52+
targetingKey: 'user-key',
53+
anonymous: true,
54+
firstname: 'John',
55+
lastname: 'Doe',
56+
57+
} as EvaluationContext);
58+
59+
const want: GoFeatureFlagUser = {
60+
key: 'user-key',
61+
anonymous: true,
62+
custom: {
63+
firstname: 'John',
64+
lastname: 'Doe',
65+
66+
},
67+
};
68+
expect(got).toEqual(want);
69+
});
70+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { EvaluationContext } from '@openfeature/nodejs-sdk';
2+
import { sha1 } from 'object-hash';
3+
import { GoFeatureFlagUser } from './model';
4+
5+
/**
6+
* transformContext takes the raw OpenFeature context returns a GoFeatureFlagUser.
7+
* @param context - the context used for flag evaluation.
8+
* @returns {GoFeatureFlagUser} the user against who we will evaluate the flag.
9+
*/
10+
export function transformContext(
11+
context: EvaluationContext
12+
): GoFeatureFlagUser {
13+
const { targetingKey, ...attributes } = context;
14+
15+
// If we don't have a targetingKey we are using a hash of the object to build
16+
// a consistent key. If for some reason it fails we are using a constant string
17+
const key = targetingKey || sha1(context) || 'anonymous';
18+
19+
// Handle the special case of the anonymous field
20+
let anonymous = false;
21+
if (
22+
attributes !== undefined &&
23+
attributes !== null &&
24+
'anonymous' in attributes
25+
) {
26+
if (typeof attributes['anonymous'] === 'boolean') {
27+
anonymous = attributes['anonymous'];
28+
}
29+
delete attributes['anonymous'];
30+
}
31+
32+
return {
33+
key,
34+
anonymous,
35+
custom: attributes,
36+
};
37+
}

0 commit comments

Comments
 (0)