Skip to content

Commit

Permalink
Change privatize UuidValue and add new factory constructor.
Browse files Browse the repository at this point in the history
  • Loading branch information
daegalus committed Sep 25, 2023
1 parent 2c9c74d commit 404a86a
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

v4.1.0

* **[BREAKING CHANGE]** In order to enforce lowercase strings in `UuidValue`, I have made the default const constructor private, and added a `fromString` factory constructor. Please migrate any direct `UuidValue()` usage to `UuidValue.fromString()` or `UuidValue.withValidation()`.

v4.0.0

* toBytes on UuidValue now does not validate by default, but has the option `validate` that can be set to true if you need validation when calling it. (Thanks @Erhannis)
Expand Down
18 changes: 9 additions & 9 deletions lib/uuid.dart
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,8 @@ class Uuid {
Map<String, dynamic>? options,
V1Options? config}) {
return config != null
? UuidValue(v1(config: config))
: UuidValue(v1(options: options));
? UuidValue.fromString(v1(config: config))
: UuidValue.fromString(v1(options: options));
}

/// Generates a RNG version 4 UUID
Expand Down Expand Up @@ -377,8 +377,8 @@ class Uuid {
Map<String, dynamic>? options,
V4Options? config}) {
return config != null
? UuidValue(v4(config: config))
: UuidValue(v4(options: options));
? UuidValue.fromString(v4(config: config))
: UuidValue.fromString(v4(options: options));
}

/// Generates a namespace & name-based version 5 UUID
Expand Down Expand Up @@ -475,8 +475,8 @@ class Uuid {
Map<String, dynamic>? options,
V5Options? config}) {
return config != null
? UuidValue(v5(namespace, name, config: config))
: UuidValue(v5(namespace, name, options: options));
? UuidValue.fromString(v5(namespace, name, config: config))
: UuidValue.fromString(v5(namespace, name, options: options));
}

/// Generates a draft time-based version 6 UUID
Expand Down Expand Up @@ -526,7 +526,7 @@ class Uuid {
///
/// https://datatracker.ietf.org/doc/html/draft-peabody-dispatch-new-uuid-format#section-4.3
UuidValue v6obj({V6Options? config}) {
return UuidValue(v6(config: config));
return UuidValue.fromString(v6(config: config));
}

/// Generates a draft time-based version 7 UUID as a [UuidValue] object
Expand Down Expand Up @@ -573,7 +573,7 @@ class Uuid {
///
/// https://www.ietf.org/archive/id/draft-peabody-dispatch-new-uuid-format-01.html#name-uuidv7-layout-and-bit-order
UuidValue v7obj({V7Options? config}) {
return UuidValue(v7(config: config));
return UuidValue.fromString(v7(config: config));
}

/// Generates a draft time-based version 8 UUID
Expand Down Expand Up @@ -620,6 +620,6 @@ class Uuid {
///
/// https://www.ietf.org/archive/id/draft-peabody-dispatch-new-uuid-format-01.html#name-uuidv7-layout-and-bit-order
UuidValue v8obj({V8Options? config}) {
return UuidValue(v8(config: config));
return UuidValue.fromString(v8(config: config));
}
}
8 changes: 8 additions & 0 deletions lib/uuid_value.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import 'validation.dart';
class UuidValue {
final String uuid;

/// fromString() creates a UuidValue from a [String] with no validation.
factory UuidValue.fromString(String uuid) {
return UuidValue(uuid.toLowerCase());
}

/// fromByteList() creates a UuidValue from a [Uint8List] of bytes.
factory UuidValue.fromByteList(Uint8List byteList, {int? offset}) {
return UuidValue(UuidParsing.unparse(byteList, offset: offset ?? 0));
Expand Down Expand Up @@ -36,6 +41,9 @@ class UuidValue {

/// UuidValue() Constructor for creating a uuid value.
///
/// WARNING: Please do not use this directly, use [UuidValue.fromString]
/// or [UuidValue.withValidation]
///
/// Takes in a string representation of a [uuid] to wrap.
const UuidValue(this.uuid);

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: uuid
version: 4.0.0
version: 4.1.0
description: >
RFC4122 (v1, v4, v5, v6, v7, v8) UUID Generator and Parser for Dart
documentation: https://daegalus.github.io/dart-uuid/index.html
Expand Down
4 changes: 2 additions & 2 deletions test/uuid_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ void main() {
expect(() => UuidValue.withValidation(invalidUUID),
throwsA(isA<FormatException>()));

final uuidval = UuidValue(invalidUUID);
final uuidval = UuidValue.fromString(invalidUUID);
expect(uuidval.uuid, invalidUUID);
});

Expand All @@ -493,7 +493,7 @@ void main() {
'The provided UUID is not RFC4122 compliant. It seems you might be using a Microsoft GUID. Try setting `validationMode = ValidationMode.nonStrict`',
)));

final uuidval = UuidValue(validGUID);
final uuidval = UuidValue.fromString(validGUID);
expect(uuidval.uuid, validGUID.toLowerCase());
});

Expand Down

0 comments on commit 404a86a

Please sign in to comment.