Skip to content

Commit

Permalink
fixup! feat: improve DataSchemaValue handling
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Dec 29, 2023
1 parent 52620cf commit 42461d2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 32 deletions.
43 changes: 12 additions & 31 deletions lib/src/scripting_api/data_schema_value.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,29 @@ sealed class DataSchemaValue<T> {
/// The raw value wrapped by this [DataSchemaValue] object.
T get value;

/// Creates a new typed [DataSchemaValue] representing `null`.
static NullValue fromNull() => NullValue._create();

/// Creates a new typed [DataSchemaValue] representing a [String].
static StringValue fromString(String value) => StringValue._fromValue(value);

/// Creates a new typed [DataSchemaValue] representing an [int].
static IntegerValue fromInteger(int value) => IntegerValue._fromValue(value);

static FloatValue fromNumber(double value) => FloatValue._fromValue(value);
/// Creates a new typed [DataSchemaValue] representing a [double].
static NumberValue fromNumber(double value) => NumberValue._fromValue(value);

/// Creates a new typed [DataSchemaValue] representing a [bool]ean [value].
static BooleanValue fromBool(bool value) => BooleanValue._fromValue(value);

/// Generates a [DataSchemaValue] from a list of [value]s.
/// Creates a [DataSchemaValue] from a list of [value]s.
///
/// Throws a [FormatException] if any element of the [value] list is not a
/// valid [DataSchemaValue].
static ArrayValue fromArray(List<Object?> value) =>
ArrayValue._fromValue(value);

/// Generates a [DataSchemaValue] from a map of [value]s.
/// Creates a [DataSchemaValue] from a map of [value]s.
///
/// Throws a [FormatException] if any entry of the [value] map is not a
/// valid [DataSchemaValue].
Expand Down Expand Up @@ -59,7 +64,7 @@ sealed class DataSchemaValue<T> {
}

if (value is double) {
return FloatValue._fromValue(value);
return NumberValue._fromValue(value);
}

if (value is List<dynamic>) {
Expand Down Expand Up @@ -120,9 +125,9 @@ final class IntegerValue extends DataSchemaValue<int> {
}

/// A [DataSchemaValue] object that wraps a [double].
final class FloatValue extends DataSchemaValue<double> {
/// Instantiates a new [FloatValue] object from a raw [value].
FloatValue._fromValue(this.value);
final class NumberValue extends DataSchemaValue<double> {
/// Instantiates a new [NumberValue] object from a raw [value].
NumberValue._fromValue(this.value);

@override
final double value;
Expand Down Expand Up @@ -184,27 +189,3 @@ final class ObjectValue extends DataSchemaValue<Map<String, Object?>> {
return ObjectValue._fromValue(result);
}
}

extension NullDataSchemaValueExtension on Null {
DataSchemaValue asDataSchemaValue() => DataSchemaValue.fromNull();
}

extension StringDataSchemaValueExtension on String {
DataSchemaValue asDataSchemaValue() => DataSchemaValue.fromString(this);
}

extension IntegerDataSchemaValueExtension on int {
DataSchemaValue asDataSchemaValue() => DataSchemaValue.fromInteger(this);
}

extension NumberDataSchemaValueExtension on double {
DataSchemaValue asDataSchemaValue() => DataSchemaValue.fromNumber(this);
}

extension ArrayDataSchemaValueExtension on List<Object?> {
DataSchemaValue asDataSchemaValue() => DataSchemaValue.fromArray(this);
}

extension ObjectDataSchemaValueExtension on Map<String, Object?> {
DataSchemaValue asDataSchemaValue() => DataSchemaValue.fromObject(this);
}
2 changes: 1 addition & 1 deletion test/core/content_serdes_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void main() {

expect(
await contentSerdes.contentToValue(testContent1, successfulSchema),
42.asDataSchemaValue(),
DataSchemaValue.fromInteger(42),
);

final testContent2 = _getTestContent('42');
Expand Down

0 comments on commit 42461d2

Please sign in to comment.