-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve DataSchemaValue handling
- Loading branch information
Showing
22 changed files
with
742 additions
and
247 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
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 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,63 @@ | ||
// Copyright 2023 Contributors to the Eclipse Foundation. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
// | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
|
||
import 'dart:convert'; | ||
|
||
import '../../definitions/data_schema.dart'; | ||
|
||
import '../../scripting_api/data_schema_value.dart'; | ||
import 'content_codec.dart'; | ||
|
||
const _utf8Coding = 'utf-8'; | ||
|
||
/// A [ContentCodec] that encodes and decodes plain text data. | ||
class TextCodec extends ContentCodec { | ||
@override | ||
List<int> valueToBytes( | ||
DataSchemaValue? dataSchemaValue, | ||
DataSchema? dataSchema, | ||
Map<String, String>? parameters, | ||
) { | ||
if (dataSchemaValue == null) { | ||
return []; | ||
} | ||
|
||
final rawValue = dataSchemaValue.value.toString(); | ||
|
||
final coding = parameters.coding; | ||
|
||
switch (coding) { | ||
case _utf8Coding: | ||
return utf8.encode(rawValue); | ||
default: | ||
throw FormatException('Encountered unsupported text coding $coding'); | ||
} | ||
} | ||
|
||
@override | ||
DataSchemaValue? bytesToValue( | ||
List<int> bytes, | ||
DataSchema? dataSchema, | ||
Map<String, String>? parameters, | ||
) { | ||
if (bytes.isEmpty) { | ||
return null; | ||
} | ||
|
||
final coding = parameters.coding; | ||
|
||
switch (coding) { | ||
case _utf8Coding: | ||
return DataSchemaValue.fromString(utf8.decoder.convert(bytes)); | ||
default: | ||
throw FormatException('Encountered unsupported text coding $coding'); | ||
} | ||
} | ||
} | ||
|
||
extension _ParametersExtension on Map<String, String>? { | ||
String get coding => this?['charset']?.toLowerCase() ?? _utf8Coding; | ||
} |
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.