Nikolas Rimikis
1 year ago
committed by
jld3103
35 changed files with 219 additions and 44 deletions
@ -0,0 +1,2 @@ |
|||||||
|
export 'src/built_value/content_string_serializer.dart'; |
||||||
|
export 'src/built_value/double_serializer.dart'; |
@ -1 +0,0 @@ |
|||||||
export 'src/content_string.dart'; |
|
@ -0,0 +1 @@ |
|||||||
|
export 'src/models/content_string.dart'; |
@ -1,22 +1,7 @@ |
|||||||
import 'dart:convert'; |
import 'dart:convert'; |
||||||
|
|
||||||
import 'package:built_value/built_value.dart'; |
|
||||||
import 'package:built_value/serializer.dart'; |
import 'package:built_value/serializer.dart'; |
||||||
|
import 'package:dynamite_runtime/src/models/content_string.dart'; |
||||||
part 'content_string.g.dart'; |
|
||||||
|
|
||||||
/// Json data encoded in a `String` as defined by [json-schema](https://json-schema.org/understanding-json-schema/reference/non_json_data.html#contentschema). |
|
||||||
abstract class ContentString<T> implements Built<ContentString<T>, ContentStringBuilder<T>> { |
|
||||||
/// Creates a new content `String`. |
|
||||||
factory ContentString([final void Function(ContentStringBuilder<T>)? b]) = _$ContentString<T>; |
|
||||||
const ContentString._(); |
|
||||||
|
|
||||||
/// The decoded value of the content `String`. |
|
||||||
T get content; |
|
||||||
|
|
||||||
/// The serializer for a content `String`. |
|
||||||
static Serializer<ContentString<Object?>> get serializer => _$contentStringSerializer; |
|
||||||
} |
|
||||||
|
|
||||||
/// Serialization plugin for decoding [ContentString]s. |
/// Serialization plugin for decoding [ContentString]s. |
||||||
/// |
/// |
@ -0,0 +1,48 @@ |
|||||||
|
import 'package:built_collection/built_collection.dart'; |
||||||
|
import 'package:built_value/serializer.dart'; |
||||||
|
|
||||||
|
/// A built_value serializer for `double` values that does not accept integers. |
||||||
|
class DynamiteDoubleSerializer implements PrimitiveSerializer<double> { |
||||||
|
// Constant names match those in [double]. |
||||||
|
static const String _nan = 'NaN'; |
||||||
|
static const String _infinity = 'INF'; |
||||||
|
static const String _negativeInfinity = '-INF'; |
||||||
|
|
||||||
|
@override |
||||||
|
final Iterable<Type> types = BuiltList<Type>([double]); |
||||||
|
|
||||||
|
@override |
||||||
|
final String wireName = 'double'; |
||||||
|
|
||||||
|
@override |
||||||
|
Object serialize( |
||||||
|
final Serializers serializers, |
||||||
|
final double aDouble, { |
||||||
|
final FullType specifiedType = FullType.unspecified, |
||||||
|
}) { |
||||||
|
if (aDouble.isNaN) { |
||||||
|
return _nan; |
||||||
|
} else if (aDouble.isInfinite) { |
||||||
|
return aDouble.isNegative ? _negativeInfinity : _infinity; |
||||||
|
} else { |
||||||
|
return aDouble; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@override |
||||||
|
double deserialize( |
||||||
|
final Serializers serializers, |
||||||
|
final Object serialized, { |
||||||
|
final FullType specifiedType = FullType.unspecified, |
||||||
|
}) { |
||||||
|
if (serialized == _nan) { |
||||||
|
return double.nan; |
||||||
|
} else if (serialized == _negativeInfinity) { |
||||||
|
return double.negativeInfinity; |
||||||
|
} else if (serialized == _infinity) { |
||||||
|
return double.infinity; |
||||||
|
} else { |
||||||
|
return serialized as double; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,17 @@ |
|||||||
|
import 'package:built_value/built_value.dart'; |
||||||
|
import 'package:built_value/serializer.dart'; |
||||||
|
|
||||||
|
part 'content_string.g.dart'; |
||||||
|
|
||||||
|
/// Json data encoded in a `String` as defined by [json-schema](https://json-schema.org/understanding-json-schema/reference/non_json_data.html#contentschema). |
||||||
|
abstract class ContentString<T> implements Built<ContentString<T>, ContentStringBuilder<T>> { |
||||||
|
/// Creates a new content `String`. |
||||||
|
factory ContentString([final void Function(ContentStringBuilder<T>)? b]) = _$ContentString<T>; |
||||||
|
const ContentString._(); |
||||||
|
|
||||||
|
/// The decoded value of the content `String`. |
||||||
|
T get content; |
||||||
|
|
||||||
|
/// The serializer for a content `String`. |
||||||
|
static Serializer<ContentString<Object?>> get serializer => _$contentStringSerializer; |
||||||
|
} |
@ -0,0 +1,95 @@ |
|||||||
|
// ignore_for_file: avoid_redundant_argument_values |
||||||
|
|
||||||
|
import 'dart:convert'; |
||||||
|
|
||||||
|
import 'package:built_value/serializer.dart'; |
||||||
|
import 'package:dynamite_runtime/built_value.dart'; |
||||||
|
import 'package:test/test.dart'; |
||||||
|
|
||||||
|
void main() { |
||||||
|
final serializers = (Serializers().toBuilder()..add(DynamiteDoubleSerializer())).build(); |
||||||
|
|
||||||
|
group('double with known specifiedType', () { |
||||||
|
const data = 3.141592653589793; |
||||||
|
const serialized = data; |
||||||
|
const specifiedType = 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('double with unknown specifiedType', () { |
||||||
|
const data = 3.141592653589793; |
||||||
|
final serialized = json.decode(json.encode(['double', data])) 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); |
||||||
|
}); |
||||||
|
}); |
||||||
|
|
||||||
|
group('integer with known specifiedType', () { |
||||||
|
const data = 3; |
||||||
|
const serialized = data; |
||||||
|
const specifiedType = FullType(double); |
||||||
|
|
||||||
|
test('can not be deserialized', () { |
||||||
|
expect( |
||||||
|
() => serializers.deserialize(serialized, specifiedType: specifiedType), |
||||||
|
throwsA(isA<DeserializationError>()), |
||||||
|
); |
||||||
|
}); |
||||||
|
}); |
||||||
|
|
||||||
|
group('double with NaN value', () { |
||||||
|
const data = double.nan; |
||||||
|
const serialized = 'NaN'; |
||||||
|
const specifiedType = FullType(double); |
||||||
|
|
||||||
|
test('can be serialized', () { |
||||||
|
expect(serializers.serialize(data, specifiedType: specifiedType), serialized); |
||||||
|
}); |
||||||
|
|
||||||
|
test('can be deserialized', () { |
||||||
|
// Compare using toString as NaN != NaN. |
||||||
|
expect(serializers.deserialize(serialized, specifiedType: specifiedType).toString(), data.toString()); |
||||||
|
}); |
||||||
|
}); |
||||||
|
|
||||||
|
group('double with -INF value', () { |
||||||
|
const data = double.negativeInfinity; |
||||||
|
const serialized = '-INF'; |
||||||
|
const specifiedType = 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('double with INF value', () { |
||||||
|
const data = double.infinity; |
||||||
|
const serialized = 'INF'; |
||||||
|
const specifiedType = FullType(double); |
||||||
|
|
||||||
|
test('can be serialized', () { |
||||||
|
expect(serializers.serialize(data, specifiedType: specifiedType), serialized); |
||||||
|
}); |
||||||
|
|
||||||
|
test('can be deserialized', () { |
||||||
|
expect(serializers.deserialize(serialized, specifiedType: specifiedType), data); |
||||||
|
}); |
||||||
|
}); |
||||||
|
} |
Loading…
Reference in new issue