Nikolas Rimikis
2 years ago
8 changed files with 565 additions and 202 deletions
@ -0,0 +1 @@
|
||||
export 'src/helpers/content_string.dart'; |
@ -0,0 +1,60 @@
|
||||
// ignore_for_file: avoid_equals_and_hash_code_on_mutable_classes |
||||
|
||||
import 'dart:convert'; |
||||
|
||||
import 'package:built_value/built_value.dart'; |
||||
import 'package:built_value/serializer.dart'; |
||||
|
||||
part 'content_string.g.dart'; |
||||
|
||||
abstract class ContentString<T> implements Built<ContentString<T>, ContentStringBuilder<T>> { |
||||
factory ContentString([final void Function(ContentStringBuilder<T>)? b]) = _$ContentString<T>; |
||||
const ContentString._(); |
||||
|
||||
/// decoded contentString |
||||
T get content; |
||||
|
||||
static Serializer<ContentString<Object?>> get serializer => _$contentStringSerializer; |
||||
} |
||||
|
||||
class ContentStringPlugin implements SerializerPlugin { |
||||
const ContentStringPlugin(); |
||||
|
||||
@override |
||||
Object? afterDeserialize(final Object? object, final FullType specifiedType) => object; |
||||
|
||||
@override |
||||
Object? afterSerialize(final Object? object, final FullType specifiedType) { |
||||
if (!specifiedType.root.toString().contains('ContentString')) { |
||||
return object; |
||||
} |
||||
|
||||
if (object is! Map<String, dynamic>) { |
||||
throw ArgumentError('ContentStringPlugin can only be applied to Map<String, dynamic>. ' |
||||
'Please ensure the StandardJsonPlugin is applied and run before.'); |
||||
} |
||||
|
||||
final content = object['content']; |
||||
|
||||
return jsonEncode(content); |
||||
} |
||||
|
||||
@override |
||||
Object? beforeDeserialize(final Object? object, final FullType specifiedType) { |
||||
if (!specifiedType.root.toString().contains('ContentString')) { |
||||
return object; |
||||
} |
||||
|
||||
if (object is! String) { |
||||
throw ArgumentError('The serialized ContentString must be of type String. ' |
||||
'Please ensure the StandardJsonPlugin is applied and run before.'); |
||||
} |
||||
|
||||
final content = jsonDecode(object); |
||||
|
||||
return ['content', content]; |
||||
} |
||||
|
||||
@override |
||||
Object? beforeSerialize(final Object? object, final FullType specifiedType) => object; |
||||
} |
@ -0,0 +1,139 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND |
||||
|
||||
part of 'content_string.dart'; |
||||
|
||||
// ************************************************************************** |
||||
// BuiltValueGenerator |
||||
// ************************************************************************** |
||||
|
||||
Serializer<ContentString<Object?>> _$contentStringSerializer = _$ContentStringSerializer(); |
||||
|
||||
class _$ContentStringSerializer implements StructuredSerializer<ContentString<Object?>> { |
||||
@override |
||||
final Iterable<Type> types = const [ContentString, _$ContentString]; |
||||
@override |
||||
final String wireName = 'ContentString'; |
||||
|
||||
@override |
||||
Iterable<Object?> serialize(Serializers serializers, ContentString<Object?> object, |
||||
{FullType specifiedType = FullType.unspecified}) { |
||||
final isUnderspecified = specifiedType.isUnspecified || specifiedType.parameters.isEmpty; |
||||
if (!isUnderspecified) serializers.expectBuilder(specifiedType); |
||||
final parameterT = isUnderspecified ? FullType.object : specifiedType.parameters[0]; |
||||
|
||||
final result = <Object?>[ |
||||
'content', |
||||
serializers.serialize(object.content, specifiedType: parameterT), |
||||
]; |
||||
|
||||
return result; |
||||
} |
||||
|
||||
@override |
||||
ContentString<Object?> deserialize(Serializers serializers, Iterable<Object?> serialized, |
||||
{FullType specifiedType = FullType.unspecified}) { |
||||
final isUnderspecified = specifiedType.isUnspecified || specifiedType.parameters.isEmpty; |
||||
if (!isUnderspecified) serializers.expectBuilder(specifiedType); |
||||
final parameterT = isUnderspecified ? FullType.object : specifiedType.parameters[0]; |
||||
|
||||
final result = isUnderspecified |
||||
? ContentStringBuilder<Object?>() |
||||
: serializers.newBuilder(specifiedType) as ContentStringBuilder<Object?>; |
||||
|
||||
final iterator = serialized.iterator; |
||||
while (iterator.moveNext()) { |
||||
final key = iterator.current! as String; |
||||
iterator.moveNext(); |
||||
final Object? value = iterator.current; |
||||
switch (key) { |
||||
case 'content': |
||||
result.content = serializers.deserialize(value, specifiedType: parameterT); |
||||
break; |
||||
} |
||||
} |
||||
|
||||
return result.build(); |
||||
} |
||||
} |
||||
|
||||
class _$ContentString<T> extends ContentString<T> { |
||||
@override |
||||
final T content; |
||||
|
||||
factory _$ContentString([void Function(ContentStringBuilder<T>)? updates]) => |
||||
(ContentStringBuilder<T>()..update(updates))._build(); |
||||
|
||||
_$ContentString._({required this.content}) : super._() { |
||||
BuiltValueNullFieldError.checkNotNull(content, r'ContentString', 'content'); |
||||
if (T == dynamic) { |
||||
throw BuiltValueMissingGenericsError(r'ContentString', 'T'); |
||||
} |
||||
} |
||||
|
||||
@override |
||||
ContentString<T> rebuild(void Function(ContentStringBuilder<T>) updates) => (toBuilder()..update(updates)).build(); |
||||
|
||||
@override |
||||
ContentStringBuilder<T> toBuilder() => ContentStringBuilder<T>()..replace(this); |
||||
|
||||
@override |
||||
bool operator ==(Object other) { |
||||
if (identical(other, this)) return true; |
||||
return other is ContentString && content == other.content; |
||||
} |
||||
|
||||
@override |
||||
int get hashCode { |
||||
var _$hash = 0; |
||||
_$hash = $jc(_$hash, content.hashCode); |
||||
_$hash = $jf(_$hash); |
||||
return _$hash; |
||||
} |
||||
|
||||
@override |
||||
String toString() { |
||||
return (newBuiltValueToStringHelper(r'ContentString')..add('content', content)).toString(); |
||||
} |
||||
} |
||||
|
||||
class ContentStringBuilder<T> implements Builder<ContentString<T>, ContentStringBuilder<T>> { |
||||
_$ContentString<T>? _$v; |
||||
|
||||
T? _content; |
||||
T? get content => _$this._content; |
||||
set content(T? content) => _$this._content = content; |
||||
|
||||
ContentStringBuilder(); |
||||
|
||||
ContentStringBuilder<T> get _$this { |
||||
final $v = _$v; |
||||
if ($v != null) { |
||||
_content = $v.content; |
||||
_$v = null; |
||||
} |
||||
return this; |
||||
} |
||||
|
||||
@override |
||||
void replace(ContentString<T> other) { |
||||
ArgumentError.checkNotNull(other, 'other'); |
||||
_$v = other as _$ContentString<T>; |
||||
} |
||||
|
||||
@override |
||||
void update(void Function(ContentStringBuilder<T>)? updates) { |
||||
if (updates != null) updates(this); |
||||
} |
||||
|
||||
@override |
||||
ContentString<T> build() => _build(); |
||||
|
||||
_$ContentString<T> _build() { |
||||
final _$result = _$v ?? |
||||
_$ContentString<T>._(content: BuiltValueNullFieldError.checkNotNull(content, r'ContentString', 'content')); |
||||
replace(_$result); |
||||
return _$result; |
||||
} |
||||
} |
||||
|
||||
// ignore_for_file: deprecated_member_use_from_same_package,type=lint |
@ -0,0 +1,341 @@
|
||||
// ignore_for_file: avoid_redundant_argument_values |
||||
|
||||
import 'dart:convert'; |
||||
|
||||
import 'package:built_collection/built_collection.dart'; |
||||
import 'package:built_value/serializer.dart'; |
||||
import 'package:built_value/standard_json_plugin.dart'; |
||||
import 'package:dynamite/content_string.dart'; |
||||
import 'package:test/test.dart'; |
||||
|
||||
part 'content_string_test.g.dart'; |
||||
|
||||
@SerializersFor([ |
||||
ContentString, |
||||
]) |
||||
final Serializers serializers = (_$serializers.toBuilder() |
||||
..addPlugin(StandardJsonPlugin()) |
||||
..addPlugin(const ContentStringPlugin()) |
||||
..addBuilderFactory(const FullType(ContentString, [FullType(bool)]), ContentStringBuilder<bool>.new) |
||||
..addBuilderFactory(const FullType(ContentString, [FullType(double)]), ContentStringBuilder<double>.new) |
||||
..addBuilderFactory( |
||||
const FullType(ContentString, [ |
||||
FullType(BuiltList, [FullType(int)]) |
||||
]), |
||||
ContentStringBuilder<BuiltList<int>>.new, |
||||
) |
||||
..addBuilderFactory(const FullType(BuiltList, [FullType(int)]), ListBuilder<int>.new) |
||||
..addBuilderFactory( |
||||
const FullType(ContentString, [ |
||||
FullType(BuiltMap, [FullType(String), FullType(int)]) |
||||
]), |
||||
ContentStringBuilder<BuiltMap<String, int>>.new, |
||||
) |
||||
..addBuilderFactory(const FullType(BuiltMap, [FullType(String), FullType(int)]), MapBuilder<String, int>.new) |
||||
..addBuilderFactory(const FullType(ContentString, [FullType(int)]), ContentStringBuilder<int>.new) |
||||
..addBuilderFactory(const FullType(ContentString, [FullType(String)]), ContentStringBuilder<String>.new) |
||||
..addBuilderFactory( |
||||
const FullType(ContentString, [ |
||||
FullType(ContentString, [FullType(String)]) |
||||
]), |
||||
ContentStringBuilder<ContentString<String>>.new, |
||||
)) |
||||
.build(); |
||||
|
||||
void main() { |
||||
group('ContentString with known specifiedType holding bool', () { |
||||
final data = ContentString<bool>((final b) => b..content = true); |
||||
final serialized = json.encode(true); |
||||
const specifiedType = FullType(ContentString, [FullType(bool)]); |
||||
|
||||
test('can be serialized', () { |
||||
expect(serializers.serialize(data, specifiedType: specifiedType), serialized); |
||||
}); |
||||
|
||||
test('can be deserialized', () { |
||||
expect(serializers.deserialize(serialized, specifiedType: specifiedType), data); |
||||
}); |
||||
}); |
||||
|
||||
group( |
||||
'ContentString with unknown specifiedType holding bool', |
||||
() { |
||||
final data = ContentString((final b) => b..content = true); |
||||
final serialized = json.decode( |
||||
json.encode({ |
||||
r'$': 'ContentString', |
||||
'content': {r'$': 'bool', '': true} |
||||
}), |
||||
) as Object; |
||||
const specifiedType = FullType.unspecified; |
||||
|
||||
test('can be serialized', () { |
||||
expect(serializers.serialize(data, specifiedType: specifiedType), serialized); |
||||
}); |
||||
|
||||
test('can be deserialized', () { |
||||
expect(serializers.deserialize(serialized, specifiedType: specifiedType), data); |
||||
}); |
||||
}, |
||||
skip: true, |
||||
); |
||||
|
||||
group('ContentString with known specifiedType holding double', () { |
||||
final data = ContentString<double>((final b) => b..content = 42.5); |
||||
final serialized = json.encode(42.5); |
||||
const specifiedType = FullType(ContentString, [FullType(double)]); |
||||
|
||||
test('can be serialized', () { |
||||
expect(serializers.serialize(data, specifiedType: specifiedType), serialized); |
||||
}); |
||||
|
||||
test('can be deserialized', () { |
||||
expect(serializers.deserialize(serialized, specifiedType: specifiedType), data); |
||||
}); |
||||
}); |
||||
|
||||
group( |
||||
'ContentString with unknown specifiedType holding double', |
||||
() { |
||||
final data = ContentString((final b) => b..content = 42.5); |
||||
final serialized = json.decode( |
||||
json.encode({ |
||||
r'$': 'ContentString', |
||||
'content': {r'$': 'double', '': 42.5} |
||||
}), |
||||
) as Object; |
||||
const specifiedType = FullType.unspecified; |
||||
|
||||
test('can be serialized', () { |
||||
expect(serializers.serialize(data, specifiedType: specifiedType), serialized); |
||||
}); |
||||
|
||||
test('can be deserialized', () { |
||||
expect(serializers.deserialize(serialized, specifiedType: specifiedType), data); |
||||
}); |
||||
}, |
||||
skip: true, |
||||
); |
||||
|
||||
group('ContentString with known specifiedType holding list', () { |
||||
final data = ContentString<BuiltList<int>>((final b) => b..content = BuiltList([1, 2, 3])); |
||||
final serialized = json.encode([1, 2, 3]); |
||||
const specifiedType = FullType(ContentString, [ |
||||
FullType(BuiltList, [FullType(int)]) |
||||
]); |
||||
|
||||
test('can be serialized', () { |
||||
expect(serializers.serialize(data, specifiedType: specifiedType), serialized); |
||||
}); |
||||
|
||||
test('can be deserialized', () { |
||||
expect(serializers.deserialize(serialized, specifiedType: specifiedType), equals(data)); |
||||
}); |
||||
}); |
||||
|
||||
group( |
||||
'ContentString with unknown specifiedType holding list', |
||||
() { |
||||
final data = ContentString((final b) => b..content = BuiltList([1, 2, 3])); |
||||
final serialized = json.decode( |
||||
json.encode({ |
||||
r'$': 'ContentString', |
||||
'content': { |
||||
r'$': 'list', |
||||
'': [ |
||||
{r'$': 'int', '': 1}, |
||||
{r'$': 'int', '': 2}, |
||||
{r'$': 'int', '': 3} |
||||
] |
||||
} |
||||
}), |
||||
) as Object; |
||||
const specifiedType = FullType.unspecified; |
||||
|
||||
test('can be serialized', () { |
||||
expect(serializers.serialize(data, specifiedType: specifiedType), serialized); |
||||
}); |
||||
|
||||
test('can be deserialized', () { |
||||
expect(serializers.deserialize(serialized, specifiedType: specifiedType), data); |
||||
}); |
||||
}, |
||||
skip: true, |
||||
); |
||||
|
||||
group('ContentString with known specifiedType holding map', () { |
||||
final data = |
||||
ContentString<BuiltMap<String, int>>((final b) => b..content = BuiltMap({'one': 1, 'two': 2, 'three': 3})); |
||||
// while not being valid json it's what built_value expects. |
||||
// using the StandardJsonPlugin will encode it to a valid Map<String, int>. |
||||
final serialized = json.encode({'one': 1, 'two': 2, 'three': 3}); |
||||
const specifiedType = FullType(ContentString, [ |
||||
FullType(BuiltMap, [FullType(String), FullType(int)]) |
||||
]); |
||||
|
||||
test('can be serialized', () { |
||||
expect(serializers.serialize(data, specifiedType: specifiedType), serialized); |
||||
}); |
||||
|
||||
test('can be deserialized', () { |
||||
expect(serializers.deserialize(serialized, specifiedType: specifiedType), data); |
||||
}); |
||||
}); |
||||
|
||||
group( |
||||
'ContentString with unknown specifiedType holding map', |
||||
() { |
||||
final data = ContentString((final b) => b..content = BuiltMap({'one': 1, 'two': 2, 'three': 3})); |
||||
final serialized = json.decode( |
||||
json.encode({ |
||||
r'$': 'ContentString', |
||||
'content': { |
||||
r'$': 'encoded_map', |
||||
r'{"$":"String","":"one"}': {r'$': 'int', '': 1}, |
||||
r'{"$":"String","":"two"}': {r'$': 'int', '': 2}, |
||||
r'{"$":"String","":"three"}': {r'$': 'int', '': 3} |
||||
} |
||||
}), |
||||
) as Object; |
||||
const specifiedType = FullType.unspecified; |
||||
|
||||
test('can be serialized', () { |
||||
expect(serializers.serialize(data, specifiedType: specifiedType), serialized); |
||||
}); |
||||
|
||||
test('can be deserialized', () { |
||||
expect(serializers.deserialize(serialized, specifiedType: specifiedType), data); |
||||
}); |
||||
}, |
||||
skip: true, |
||||
); |
||||
|
||||
group( |
||||
'ContentString with known specifiedType holding int', |
||||
() { |
||||
final data = ContentString<int>((final b) => b..content = 42); |
||||
final serialized = json.encode(42); |
||||
const specifiedType = FullType(ContentString, [FullType(int)]); |
||||
|
||||
test('can be serialized', () { |
||||
expect(serializers.serialize(data, specifiedType: specifiedType), serialized); |
||||
}); |
||||
|
||||
test('can be deserialized', () { |
||||
expect(serializers.deserialize(serialized, specifiedType: specifiedType), data); |
||||
}); |
||||
}, |
||||
); |
||||
|
||||
group( |
||||
'ContentString with unknown specifiedType holding int', |
||||
() { |
||||
final data = ContentString((final b) => b..content = 42); |
||||
final serialized = json.decode( |
||||
json.encode({ |
||||
r'$': 'ContentString', |
||||
'content': {r'$': 'int', '': 42} |
||||
}), |
||||
) as Object; |
||||
const specifiedType = FullType.unspecified; |
||||
|
||||
test('can be serialized', () { |
||||
expect(serializers.serialize(data, specifiedType: specifiedType), serialized); |
||||
}); |
||||
|
||||
test('can be deserialized', () { |
||||
expect(serializers.deserialize(serialized, specifiedType: specifiedType), data); |
||||
}); |
||||
}, |
||||
skip: true, |
||||
); |
||||
|
||||
group('ContentString with known specifiedType holding String', () { |
||||
final data = ContentString<String>((final b) => b..content = 'test'); |
||||
final serialized = json.encode('test'); |
||||
const specifiedType = FullType(ContentString, [FullType(String)]); |
||||
|
||||
test('can be serialized', () { |
||||
expect(serializers.serialize(data, specifiedType: specifiedType), serialized); |
||||
}); |
||||
|
||||
test('can be deserialized', () { |
||||
expect(serializers.deserialize(serialized, specifiedType: specifiedType), data); |
||||
}); |
||||
}); |
||||
|
||||
group( |
||||
'ContentString with unknown specifiedType holding String', |
||||
() { |
||||
final data = ContentString((final b) => b..content = 'test'); |
||||
final serialized = json.decode( |
||||
json.encode({ |
||||
r'$': 'ContentString', |
||||
'content': {r'$': 'String', '': 'test'} |
||||
}), |
||||
) as Object; |
||||
const specifiedType = FullType.unspecified; |
||||
|
||||
test('can be serialized', () { |
||||
expect(serializers.serialize(data, specifiedType: specifiedType), serialized); |
||||
}); |
||||
|
||||
test('can be deserialized', () { |
||||
expect(serializers.deserialize(serialized, specifiedType: specifiedType), data); |
||||
}); |
||||
}, |
||||
skip: true, |
||||
); |
||||
|
||||
group('ContentString with known specifiedType holding ContentString', () { |
||||
final data = ContentString<ContentString<String>>( |
||||
(final b) => b |
||||
..content = ContentString<String>( |
||||
(final b) => b..content = 'test', |
||||
), |
||||
); |
||||
final serialized = json.encode(json.encode('test')); |
||||
const specifiedType = FullType(ContentString, [ |
||||
FullType(ContentString, [FullType(String)]) |
||||
]); |
||||
|
||||
test('can be serialized', () { |
||||
expect(serializers.serialize(data, specifiedType: specifiedType), serialized); |
||||
}); |
||||
|
||||
test('can be deserialized', () { |
||||
expect(serializers.deserialize(serialized, specifiedType: specifiedType), data); |
||||
}); |
||||
}); |
||||
|
||||
group( |
||||
'ContentString with unknown specifiedType holding ContentString', |
||||
() { |
||||
final data = ContentString<ContentString<String>>( |
||||
(final b) => b |
||||
..content = ContentString<String>( |
||||
(final b) => b..content = 'test', |
||||
), |
||||
); |
||||
final serialized = json.decode( |
||||
json.encode({ |
||||
r'$': 'ContentString', |
||||
'content': { |
||||
r'$': 'ContentString', |
||||
'content': {r'$': 'String', '': 'test'} |
||||
} |
||||
}), |
||||
) as Object; |
||||
const specifiedType = FullType.unspecified; |
||||
|
||||
test('can be serialized', () { |
||||
expect(serializers.serialize(data, specifiedType: specifiedType), serialized); |
||||
}); |
||||
|
||||
test('can be deserialized', () { |
||||
expect(serializers.deserialize(serialized, specifiedType: specifiedType), data); |
||||
}); |
||||
}, |
||||
skip: false, |
||||
); |
||||
} |
@ -0,0 +1,11 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND |
||||
|
||||
part of 'content_string_test.dart'; |
||||
|
||||
// ************************************************************************** |
||||
// BuiltValueGenerator |
||||
// ************************************************************************** |
||||
|
||||
Serializers _$serializers = (Serializers().toBuilder()..add(ContentString.serializer)).build(); |
||||
|
||||
// ignore_for_file: deprecated_member_use_from_same_package,type=lint |
Loading…
Reference in new issue