-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(dynamite,dynamite_runtime,nextcloud): don't deserialize integers …
…into double Signed-off-by: Nikolas Rimikis <leptopoda@users.noreply.github.com>
- Loading branch information
1 parent
bfcc126
commit fae7e7b
Showing
35 changed files
with
219 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export 'src/built_value/content_string_serializer.dart'; | ||
export 'src/built_value/double_serializer.dart'; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export 'src/models/content_string.dart'; |
17 changes: 1 addition & 16 deletions
17
...amite_runtime/lib/src/content_string.dart → ...uilt_value/content_string_serializer.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
packages/dynamite/dynamite_runtime/lib/src/built_value/double_serializer.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
packages/dynamite/dynamite_runtime/lib/src/models/content_string.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
packages/dynamite/dynamite_runtime/test/double_serializer_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.