Skip to content

Commit 70a9ead

Browse files
feat: Inline context for custom events (#199)
1 parent 01841d6 commit 70a9ead

File tree

4 files changed

+31
-16
lines changed

4 files changed

+31
-16
lines changed

apps/flutter_client_contract_test_service/bin/contract_test_service.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class TestApiImpl extends SdkTestApi {
2323
'tags',
2424
'client-independence',
2525
'context-comparison',
26-
'inline-context',
26+
'inline-context-all',
2727
'anonymous-redaction',
2828
'client-prereq-events',
2929
'auto-env-attributes',

packages/common/lib/src/events/default_event_processor.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ final class DefaultEventProcessor implements EventProcessor {
9595

9696
@override
9797
void processCustomEvent(CustomEvent event) {
98-
_enqueue(CustomEventSerialization.toJson(event));
98+
_enqueue(CustomEventSerialization.toJson(event,
99+
allAttributesPrivate: _allAttributesPrivate,
100+
globalPrivateAttributes: _globalPrivateAttributes));
99101
}
100102

101103
@override

packages/common/lib/src/serialization/event_serialization.dart

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ final class IdentifyEventSerialization {
1818
}
1919

2020
final class CustomEventSerialization {
21-
static Map<String, dynamic> toJson(CustomEvent event) {
21+
static Map<String, dynamic> toJson(CustomEvent event,
22+
{required bool allAttributesPrivate,
23+
required Set<AttributeReference> globalPrivateAttributes}) {
2224
final json = <String, dynamic>{};
2325

2426
json['kind'] = 'custom';
@@ -30,7 +32,10 @@ final class CustomEventSerialization {
3032
if (event.metricValue != null) {
3133
json['metricValue'] = event.metricValue;
3234
}
33-
json['contextKeys'] = event.context.keys;
35+
json['context'] = LDContextSerialization.toJson(event.context,
36+
isEvent: true,
37+
allAttributesPrivate: allAttributesPrivate,
38+
globalPrivateAttributes: globalPrivateAttributes);
3439

3540
return json;
3641
}

packages/common/test/serialization/event_serialization_test.dart

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,18 @@ void main() {
8888
context: LDContextBuilder().kind('user', 'user-key').build(),
8989
key: 'my-key');
9090

91-
final json = jsonEncode(CustomEventSerialization.toJson(event));
91+
final json = jsonEncode(CustomEventSerialization.toJson(event,
92+
allAttributesPrivate: false, globalPrivateAttributes: {}));
9293

9394
final jsonAsLdValue = LDValueSerialization.fromJson(jsonDecode(json));
9495

9596
final expectedLdValue = LDValueSerialization.fromJson(jsonDecode('{'
9697
'"kind": "custom",'
9798
'"key": "my-key",'
9899
'"creationDate": 0,'
99-
'"contextKeys": {'
100-
'"user": "user-key"'
100+
'"context": {'
101+
'"kind": "user",'
102+
'"key": "user-key"'
101103
'}'
102104
'}'));
103105

@@ -111,7 +113,8 @@ void main() {
111113
context: LDContextBuilder().kind('user', 'user-key').build(),
112114
key: 'my-key');
113115

114-
final json = jsonEncode(CustomEventSerialization.toJson(event));
116+
final json = jsonEncode(CustomEventSerialization.toJson(event,
117+
allAttributesPrivate: false, globalPrivateAttributes: {}));
115118

116119
final jsonAsLdValue = LDValueSerialization.fromJson(jsonDecode(json));
117120

@@ -120,8 +123,9 @@ void main() {
120123
'"metricValue": 100,'
121124
'"key": "my-key",'
122125
'"creationDate": 0,'
123-
'"contextKeys": {'
124-
'"user": "user-key"'
126+
'"context": {'
127+
'"kind": "user",'
128+
'"key": "user-key"'
125129
'}'
126130
'}'));
127131

@@ -135,7 +139,8 @@ void main() {
135139
context: LDContextBuilder().kind('user', 'user-key').build(),
136140
key: 'my-key');
137141

138-
final json = jsonEncode(CustomEventSerialization.toJson(event));
142+
final json = jsonEncode(CustomEventSerialization.toJson(event,
143+
allAttributesPrivate: false, globalPrivateAttributes: {}));
139144

140145
final jsonAsLdValue = LDValueSerialization.fromJson(jsonDecode(json));
141146

@@ -144,8 +149,9 @@ void main() {
144149
'"key": "my-key",'
145150
'"data": {"test": "value"},'
146151
'"creationDate": 0,'
147-
'"contextKeys": {'
148-
'"user": "user-key"'
152+
'"context": {'
153+
'"kind": "user",'
154+
'"key": "user-key"'
149155
'}'
150156
'}'));
151157

@@ -160,7 +166,8 @@ void main() {
160166
context: LDContextBuilder().kind('user', 'user-key').build(),
161167
key: 'my-key');
162168

163-
final json = jsonEncode(CustomEventSerialization.toJson(event));
169+
final json = jsonEncode(CustomEventSerialization.toJson(event,
170+
allAttributesPrivate: false, globalPrivateAttributes: {}));
164171

165172
final jsonAsLdValue = LDValueSerialization.fromJson(jsonDecode(json));
166173

@@ -170,8 +177,9 @@ void main() {
170177
'"data": {"test": "value"},'
171178
'"metricValue": 100,'
172179
'"creationDate": 0,'
173-
'"contextKeys": {'
174-
'"user": "user-key"'
180+
'"context": {'
181+
'"kind": "user",'
182+
'"key": "user-key"'
175183
'}'
176184
'}'));
177185

0 commit comments

Comments
 (0)