Skip to content

Commit

Permalink
Merge pull request #20 from knocklabs/matgar26/KNO-5878-error-with-pr…
Browse files Browse the repository at this point in the history
…eferenceSet

KNO-5878: Error with preference set
  • Loading branch information
matgar26 committed May 3, 2024
2 parents 751a5c7 + 71cfe58 commit 84c5bc6
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 57 deletions.
60 changes: 31 additions & 29 deletions lib/src/model/preferences.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ class WorkflowPreferenceSetting with _$WorkflowPreferenceSetting {
}) = _WorkflowPreferenceSetting;
}

// This class is only used for JSON encoding/decoding. The channel_types value
// is what will be exposed in the public SDK.
@freezed
class _ChannelTypesJson with _$ChannelTypesJson {
@JsonSerializable(explicitToJson: true)
Expand All @@ -61,15 +59,13 @@ class _ChannelTypesJson with _$ChannelTypesJson {
toJson: _nonNullChannelTypePreferencesToJson,
fromJson: _nonNullChannelTypePreferencesFromJson,
)
required ChannelTypePreferences channelTypes,
required dynamic channelTypes,
}) = __ChannelTypesJson;

factory _ChannelTypesJson.fromJson(Map<String, dynamic> json) =>
_$ChannelTypesJsonFromJson(json);
}

// This class is only used for JSON encoding/decoding. The conditions value
// is what will be exposed in the public SDK.
@freezed
class _ConditionsJson with _$ConditionsJson {
@JsonSerializable(explicitToJson: true)
Expand Down Expand Up @@ -153,16 +149,18 @@ dynamic _workflowPreferencesToJson(WorkflowPreferences? value) {
final channelTypes = value.channelTypePreferences;
final conditions = value.conditions;

final channelTypesJson = channelTypes == null
? <String, dynamic>{}
: _ChannelTypesJson(channelTypes: channelTypes).toJson();
final conditionsJson = conditions == null
? <String, dynamic>{}
: _ConditionsJson(conditions: conditions).toJson();
json = Map.fromEntries([
...channelTypesJson.entries,
...conditionsJson.entries,
]);
if (channelTypes != null && conditions != null) {
json = {
..._ChannelTypesJson(channelTypes: channelTypes).toJson(),
..._ConditionsJson(conditions: conditions).toJson(),
};
} else if (channelTypes != null) {
json = _ChannelTypesJson(channelTypes: channelTypes).toJson();
} else if (conditions != null) {
json = _ConditionsJson(conditions: conditions).toJson();
} else {
json = null;
}
}

return MapEntry(key, json);
Expand Down Expand Up @@ -200,21 +198,25 @@ dynamic _nonNullChannelTypePreferencesToJson(ChannelTypePreferences value) {
});
}

ChannelTypePreferences _nonNullChannelTypePreferencesFromJson(
Map<String, dynamic> json,
ChannelTypePreferences? _nonNullChannelTypePreferencesFromJson(
Map<String, dynamic>? json,
) {
return json.map((key, value) {
final ChannelTypePreference setting;
if (value is bool) {
setting = ChannelTypePreference(value: value);
} else {
final parsed = _ConditionsJson.fromJson(value);
final conditions = parsed.conditions ?? [];
setting = ChannelTypePreference(conditions: conditions);
}

return MapEntry(ChannelType._valueOf(key), setting);
});
if (json != null) {
return json.map((key, value) {
final ChannelTypePreference setting;
if (value is bool) {
setting = ChannelTypePreference(value: value);
} else {
final parsed = _ConditionsJson.fromJson(value);
final conditions = parsed.conditions ?? [];
setting = ChannelTypePreference(conditions: conditions);
}

return MapEntry(ChannelType._valueOf(key), setting);
});
} else {
return null;
}
}

dynamic _channelTypePreferencesToJson(ChannelTypePreferences? value) {
Expand Down
40 changes: 16 additions & 24 deletions lib/src/model/preferences.freezed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -402,8 +402,7 @@ mixin _$ChannelTypesJson {
name: 'channel_types',
toJson: _nonNullChannelTypePreferencesToJson,
fromJson: _nonNullChannelTypePreferencesFromJson)
Map<ChannelType, ChannelTypePreference> get channelTypes =>
throw _privateConstructorUsedError;
dynamic get channelTypes => throw _privateConstructorUsedError;

Map<String, dynamic> toJson() => throw _privateConstructorUsedError;
@JsonKey(ignore: true)
Expand All @@ -422,7 +421,7 @@ abstract class _$ChannelTypesJsonCopyWith<$Res> {
name: 'channel_types',
toJson: _nonNullChannelTypePreferencesToJson,
fromJson: _nonNullChannelTypePreferencesFromJson)
Map<ChannelType, ChannelTypePreference> channelTypes});
dynamic channelTypes});
}

/// @nodoc
Expand All @@ -438,13 +437,13 @@ class __$ChannelTypesJsonCopyWithImpl<$Res, $Val extends _ChannelTypesJson>
@pragma('vm:prefer-inline')
@override
$Res call({
Object? channelTypes = null,
Object? channelTypes = freezed,
}) {
return _then(_value.copyWith(
channelTypes: null == channelTypes
channelTypes: freezed == channelTypes
? _value.channelTypes
: channelTypes // ignore: cast_nullable_to_non_nullable
as Map<ChannelType, ChannelTypePreference>,
as dynamic,
) as $Val);
}
}
Expand All @@ -462,7 +461,7 @@ abstract class _$$_ChannelTypesJsonImplCopyWith<$Res>
name: 'channel_types',
toJson: _nonNullChannelTypePreferencesToJson,
fromJson: _nonNullChannelTypePreferencesFromJson)
Map<ChannelType, ChannelTypePreference> channelTypes});
dynamic channelTypes});
}

/// @nodoc
Expand All @@ -476,13 +475,13 @@ class __$$_ChannelTypesJsonImplCopyWithImpl<$Res>
@pragma('vm:prefer-inline')
@override
$Res call({
Object? channelTypes = null,
Object? channelTypes = freezed,
}) {
return _then(_$_ChannelTypesJsonImpl(
channelTypes: null == channelTypes
? _value._channelTypes
channelTypes: freezed == channelTypes
? _value.channelTypes
: channelTypes // ignore: cast_nullable_to_non_nullable
as Map<ChannelType, ChannelTypePreference>,
as dynamic,
));
}
}
Expand All @@ -496,23 +495,17 @@ class _$_ChannelTypesJsonImpl implements __ChannelTypesJson {
name: 'channel_types',
toJson: _nonNullChannelTypePreferencesToJson,
fromJson: _nonNullChannelTypePreferencesFromJson)
required final Map<ChannelType, ChannelTypePreference> channelTypes})
: _channelTypes = channelTypes;
required this.channelTypes});

factory _$_ChannelTypesJsonImpl.fromJson(Map<String, dynamic> json) =>
_$$_ChannelTypesJsonImplFromJson(json);

final Map<ChannelType, ChannelTypePreference> _channelTypes;
@override
@JsonKey(
name: 'channel_types',
toJson: _nonNullChannelTypePreferencesToJson,
fromJson: _nonNullChannelTypePreferencesFromJson)
Map<ChannelType, ChannelTypePreference> get channelTypes {
if (_channelTypes is EqualUnmodifiableMapView) return _channelTypes;
// ignore: implicit_dynamic_type
return EqualUnmodifiableMapView(_channelTypes);
}
final dynamic channelTypes;

@override
String toString() {
Expand All @@ -525,13 +518,13 @@ class _$_ChannelTypesJsonImpl implements __ChannelTypesJson {
(other.runtimeType == runtimeType &&
other is _$_ChannelTypesJsonImpl &&
const DeepCollectionEquality()
.equals(other._channelTypes, _channelTypes));
.equals(other.channelTypes, channelTypes));
}

@JsonKey(ignore: true)
@override
int get hashCode => Object.hash(
runtimeType, const DeepCollectionEquality().hash(_channelTypes));
runtimeType, const DeepCollectionEquality().hash(channelTypes));

@JsonKey(ignore: true)
@override
Expand All @@ -554,8 +547,7 @@ abstract class __ChannelTypesJson implements _ChannelTypesJson {
name: 'channel_types',
toJson: _nonNullChannelTypePreferencesToJson,
fromJson: _nonNullChannelTypePreferencesFromJson)
required final Map<ChannelType, ChannelTypePreference>
channelTypes}) = _$_ChannelTypesJsonImpl;
required final dynamic channelTypes}) = _$_ChannelTypesJsonImpl;

factory __ChannelTypesJson.fromJson(Map<String, dynamic> json) =
_$_ChannelTypesJsonImpl.fromJson;
Expand All @@ -565,7 +557,7 @@ abstract class __ChannelTypesJson implements _ChannelTypesJson {
name: 'channel_types',
toJson: _nonNullChannelTypePreferencesToJson,
fromJson: _nonNullChannelTypePreferencesFromJson)
Map<ChannelType, ChannelTypePreference> get channelTypes;
dynamic get channelTypes;
@override
@JsonKey(ignore: true)
_$$_ChannelTypesJsonImplCopyWith<_$_ChannelTypesJsonImpl> get copyWith =>
Expand Down
2 changes: 1 addition & 1 deletion lib/src/model/preferences.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: knock_flutter
description: A client-side Flutter library to interact with user-facing Knock features, such as feeds.
version: 0.1.3
version: 0.1.4
homepage: https://github.com/knocklabs/knock-flutter

environment:
Expand Down
71 changes: 69 additions & 2 deletions test/src/model/preferences_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ void main() {
),
},
conditions: [
// Workflows and categories have the same backing structure so
// Workflows and categories have the same backing structure so
// we do not need to recheck the parsing in categories, but we
// will ensure that parsing works correct when there is not
// will ensure that parsing works correct when there is not
// conditions provided.
const PreferenceCondition(
variable: 'v4',
Expand Down Expand Up @@ -176,6 +176,42 @@ void main() {
equals({'park-wide': WorkflowPreferenceSetting(value: false)}),
);
});

test('correctly when no categories only have conditions', () async {
final json = jsonDecode('''
{
"id": "default",
"categories": {
"park-wide": {
"conditions": [
{"variable": "v3", "operator": "o3", "argument": "a3"}
]
}
},
"workflows": null,
"channel_types": null
}
''');

final preferenceSet = PreferenceSet.fromJson(json);
expect(preferenceSet.id, 'default');
expect(preferenceSet.channelTypes, isNull);
expect(preferenceSet.workflows, isNull);
expect(
preferenceSet.categories,
equals({
'park-wide': WorkflowPreferenceSetting(
conditions: [
const PreferenceCondition(
variable: 'v3',
operator: 'o3',
argument: 'a3',
),
],
),
}),
);
});
});

group('SetPreferencesProperties serializes', () {
Expand Down Expand Up @@ -315,5 +351,36 @@ void main() {
},
);
});

test('correctly when categories only has conditions', () async {
expect(
SetPreferencesProperties(
categories: {
'park-wide': WorkflowPreferenceSetting(
conditions: [
const PreferenceCondition(
variable: 'v3',
operator: 'o3',
argument: 'a3',
),
],
),
},
workflows: null,
channelTypes: null,
).toJson(),
{
'categories': {
'park-wide': {
'conditions': [
{'variable': 'v3', 'operator': 'o3', 'argument': 'a3'},
],
},
},
'workflows': null,
'channel_types': null,
},
);
});
});
}

0 comments on commit 84c5bc6

Please sign in to comment.