Repository navigation
feat: implement xsd:float codec
#70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,94 @@ | ||
| import 'dart:convert'; | ||
| import 'dart:typed_data'; | ||
|
|
||
| import 'package:xsd/src/helpers/whitespace.dart'; | ||
|
|
||
| /// A [Codec] that converts between XSD `float` strings and [double] values. | ||
| /// | ||
| /// Note: Dart uses 64-bit doubles for floating point numbers. This codec | ||
| /// uses [double] to represent XSD `float` values, which are 32-bit. It will | ||
| /// throw an error if a value cannot be represented as a 32-bit float. | ||
| class XsdFloatCodec extends Codec<double, String> { | ||
| const XsdFloatCodec(); | ||
|
|
||
| @override | ||
| Converter<double, String> get encoder => const XsdFloatEncoder(); | ||
|
|
||
| @override | ||
| Converter<String, double> get decoder => const XsdFloatDecoder(); | ||
| } | ||
|
|
||
| class XsdFloatEncoder extends Converter<double, String> { | ||
| const XsdFloatEncoder(); | ||
|
|
||
| @override | ||
| String convert(double input) { | ||
| // Validate that the double can be represented as a 32-bit float. | ||
| if (input.isFinite) { | ||
| final f32 = Float32List(1); | ||
| f32[0] = input; | ||
| // An overflow results in INFINITY, which changes the meaning of the value. | ||
| if (f32[0].isInfinite) { | ||
| throw FormatException( | ||
| 'Value $input overflows the 32-bit float range and cannot be encoded.', | ||
| ); | ||
| } | ||
|
|
||
| // An underflow to 0 also changes the meaning of the value. | ||
| if (input != 0.0 && f32[0] == 0.0) { | ||
| throw FormatException( | ||
| 'Value $input underflows the 32-bit float range and cannot be encoded.', | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| if (input.isNaN) return 'NaN'; | ||
| if (input == double.infinity) return 'INF'; | ||
| if (input == double.negativeInfinity) return '-INF'; | ||
| // XSD canonical form uses 'E'. | ||
| return input.toString().toUpperCase(); | ||
| } | ||
| } | ||
|
|
||
| class XsdFloatDecoder extends Converter<String, double> { | ||
| const XsdFloatDecoder(); | ||
|
|
||
| @override | ||
| double convert(String input) { | ||
| final trimmed = processWhiteSpace(input, Whitespace.collapse); | ||
| if (trimmed == 'INF') return double.infinity; | ||
| if (trimmed == '-INF') return double.negativeInfinity; | ||
| if (trimmed == 'NaN') return double.nan; | ||
|
|
||
| final value = double.tryParse(trimmed); | ||
|
|
||
| if (value == null) { | ||
| throw FormatException('Invalid XSD float value: "$input"'); | ||
| } | ||
|
|
||
| // Dart's double is 64-bit. We need to validate that the value | ||
| // fits within the 32-bit float value space as defined by XSD. | ||
| if (value.isFinite) { | ||
| // Use Float32List to check for overflow. | ||
| final f32 = Float32List(1); | ||
| f32[0] = value; | ||
| final floatValue = f32[0]; | ||
|
|
||
| // If the conversion results in infinity, but the original was not, it's an overflow. | ||
| if (floatValue.isInfinite) { | ||
| throw FormatException( | ||
| 'The literal "$input" is outside the value range of float (overflow).', | ||
| ); | ||
| } | ||
|
|
||
| // If the conversion results in 0, but the original was not, it's an underflow. | ||
| if (value != 0.0 && floatValue == 0.0) { | ||
| throw FormatException( | ||
| 'The literal "$input" is outside the value range of float (underflow).', | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| return value; | ||
| } | ||
| } | ||
This file contains hidden or 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,104 @@ | ||
| import 'package:test/test.dart'; | ||
| import 'package:xsd/xsd.dart'; | ||
|
|
||
| void main() { | ||
| group('XsdFloatCodec', () { | ||
| const codec = XsdFloatCodec(); | ||
|
|
||
| group('encoder', () { | ||
| test('should encode valid float values', () { | ||
| expect(codec.encode(123.45), '123.45'); | ||
| expect(codec.encode(-123.45), '-123.45'); | ||
| expect(codec.encode(0.0), '0.0'); | ||
| expect(codec.encode(-0.0), '-0.0'); | ||
| expect(codec.encode(1.23e4), '12300.0'); | ||
| expect(codec.encode(double.infinity), 'INF'); | ||
| expect(codec.encode(double.negativeInfinity), '-INF'); | ||
| expect(codec.encode(double.nan), 'NaN'); | ||
| }); | ||
|
|
||
| test('should throw on values that overflow the float range', () { | ||
| // double.maxFinite is much larger than what a 32-bit float can hold. | ||
| expect( | ||
| () => codec.encode(double.maxFinite), | ||
| throwsA( | ||
| isA<FormatException>().having( | ||
| (e) => e.message, | ||
| 'message', | ||
| contains('overflows the 32-bit float range'), | ||
| ), | ||
| ), | ||
| ); | ||
| }); | ||
|
mark-dropbear marked this conversation as resolved.
|
||
|
|
||
| test('should throw on values that underflow the float range', () { | ||
| // A value that underflows to 0 in 32-bit float. | ||
| expect( | ||
| () => codec.encode(1.0e-46), | ||
| throwsA( | ||
| isA<FormatException>().having( | ||
| (e) => e.message, | ||
| 'message', | ||
| contains('underflows the 32-bit float range'), | ||
| ), | ||
| ), | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| group('decoder', () { | ||
| test('should decode valid float strings', () { | ||
| expect(codec.decode('123.45'), 123.45); | ||
| expect(codec.decode(' -123.45 '), -123.45); | ||
| expect(codec.decode('INF'), double.infinity); | ||
| expect(codec.decode(' -INF '), double.negativeInfinity); | ||
| expect(codec.decode('NaN').isNaN, isTrue); | ||
| expect(codec.decode('1.2E3'), 1200.0); | ||
| expect(codec.decode('0'), 0.0); | ||
| expect(codec.decode('-0'), -0.0); | ||
| }); | ||
|
|
||
| test('should throw FormatException for invalid string formats', () { | ||
| expect(() => codec.decode('abc'), throwsFormatException); | ||
| expect(() => codec.decode('1.2.3'), throwsFormatException); | ||
| expect(() => codec.decode('INFinity'), throwsFormatException); | ||
|
mark-dropbear marked this conversation as resolved.
|
||
| expect(() => codec.decode('inf'), throwsFormatException); | ||
| expect(() => codec.decode('-inf'), throwsFormatException); | ||
| }); | ||
|
|
||
| test( | ||
| 'should throw FormatException on values that overflow the float range', | ||
| () { | ||
| // A value larger than the max float value (~3.4e38) | ||
| expect( | ||
| () => codec.decode('3.5e38'), | ||
| throwsA( | ||
| isA<FormatException>().having( | ||
| (e) => e.message, | ||
| 'message', | ||
| contains('outside the value range of float (overflow)'), | ||
| ), | ||
| ), | ||
| ); | ||
| }, | ||
| ); | ||
|
|
||
| test( | ||
| 'should throw FormatException on values that underflow the float range', | ||
| () { | ||
| // A value smaller than the min float value (~1.4e-45) | ||
| expect( | ||
| () => codec.decode('1.0e-46'), | ||
| throwsA( | ||
| isA<FormatException>().having( | ||
| (e) => e.message, | ||
| 'message', | ||
| contains('outside the value range of float (underflow)'), | ||
| ), | ||
| ), | ||
| ); | ||
| }, | ||
| ); | ||
| }); | ||
| }); | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.