Skip to content

Commit

Permalink
use enumerations instead of multiple separate definition of Namespace
Browse files Browse the repository at this point in the history
Signed-off-by: bymoye <[email protected]>
  • Loading branch information
bymoye authored and daegalus committed Jul 29, 2024
1 parent db18117 commit 42b2b47
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 36 deletions.
2 changes: 1 addition & 1 deletion example/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void main() {
// -> '110ec58a-a0f2-4ac4-8393-c866d813b8d1'

// Generate a v5 (namespace-name-sha1-based) id
var v5 = uuid.v5(Uuid.NAMESPACE_URL, 'www.google.com');
var v5 = uuid.v5(Namespace.URL.value, 'www.google.com');
// -> 'c74a196f-f19d-5ea9-bffd-a2742432fc9c'

// Generate a v6 (time-based) id
Expand Down
23 changes: 15 additions & 8 deletions lib/enums.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
// ignore_for_file: constant_identifier_names

class Namespace {
// RFC4122 provided namespaces for v3 and v5 namespace based UUIDs
static const DNS = '6ba7b810-9dad-11d1-80b4-00c04fd430c8';
static const URL = '6ba7b811-9dad-11d1-80b4-00c04fd430c8';
static const OID = '6ba7b812-9dad-11d1-80b4-00c04fd430c8';
static const X500 = '6ba7b814-9dad-11d1-80b4-00c04fd430c8';
static const NIL = '00000000-0000-0000-0000-000000000000';
}
import 'package:uuid/uuid_value.dart';

/// The options for UUID Validation strictness
enum ValidationMode { nonStrict, strictRFC4122 }

enum Namespace {
// RFC4122 provided namespaces for v3 and v5 namespace based UUIDs
DNS("6ba7b810-9dad-11d1-80b4-00c04fd430c8"),
URL("6ba7b811-9dad-11d1-80b4-00c04fd430c8"),
OID("6ba7b812-9dad-11d1-80b4-00c04fd430c8"),
X500("6ba7b814-9dad-11d1-80b4-00c04fd430c8"),
NIL("00000000-0000-0000-0000-000000000000");

const Namespace(this.value);
final String value;

UuidValue get uuidValue => UuidValue.raw(value);
}
7 changes: 0 additions & 7 deletions lib/uuid.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,6 @@ export 'enums.dart';
/// Released under MIT License.
class Uuid {
// RFC4122 provided namespaces for v3 and v5 namespace based UUIDs
static const NAMESPACE_DNS = '6ba7b810-9dad-11d1-80b4-00c04fd430c8';
static const NAMESPACE_URL = '6ba7b811-9dad-11d1-80b4-00c04fd430c8';
static const NAMESPACE_OID = '6ba7b812-9dad-11d1-80b4-00c04fd430c8';
static const NAMESPACE_X500 = '6ba7b814-9dad-11d1-80b4-00c04fd430c8';
static const NAMESPACE_NIL = '00000000-0000-0000-0000-000000000000';

final GlobalOptions? goptions;

/// Creates a new instance of the Uuid class.
Expand Down
6 changes: 0 additions & 6 deletions lib/uuid_value.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,6 @@ class UuidValue {
return uuidValue;
}

static const dns = UuidValue.raw(Uuid.NAMESPACE_DNS);
static const url = UuidValue.raw(Uuid.NAMESPACE_URL);
static const oid = UuidValue.raw(Uuid.NAMESPACE_OID);
static const x500 = UuidValue.raw(Uuid.NAMESPACE_X500);
static const nil = UuidValue.raw(Uuid.NAMESPACE_NIL);

/// Creates a UuidValue by taking directly the internal string representation of the [uuid],
/// which is expected to be lowercase.
///
Expand Down
9 changes: 1 addition & 8 deletions lib/v5.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,6 @@ import 'v4.dart';
import 'package:crypto/crypto.dart' as crypto;

class UuidV5 {
// RFC4122 provided namespaces for v3 and v5 namespace based UUIDs
static const NAMESPACE_DNS = '6ba7b810-9dad-11d1-80b4-00c04fd430c8';
static const NAMESPACE_URL = '6ba7b811-9dad-11d1-80b4-00c04fd430c8';
static const NAMESPACE_OID = '6ba7b812-9dad-11d1-80b4-00c04fd430c8';
static const NAMESPACE_X500 = '6ba7b814-9dad-11d1-80b4-00c04fd430c8';
static const NAMESPACE_NIL = '00000000-0000-0000-0000-000000000000';

final GlobalOptions? goptions;

const UuidV5({this.goptions});
Expand All @@ -37,7 +30,7 @@ class UuidV5 {
// If useRandom is true, generate UUIDv4, else use NIL
var blankNS = useRandom
? UuidV4(goptions: goptions).generate(options: options?.v4options)
: Namespace.NIL;
: Namespace.NIL.value;

// Use provided namespace, or use whatever is decided by options.
namespace = (namespace != null) ? namespace : blankNS;
Expand Down
2 changes: 1 addition & 1 deletion lib/validation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class UuidValidation {
fromString = UuidParsing.unparse(fromByteList);
}
// UUID of all 0s is ok.
if (fromString == Namespace.NIL) {
if (fromString == Namespace.NIL.value) {
return true;
}

Expand Down
10 changes: 5 additions & 5 deletions test/uuid_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,8 @@ void main() {

group('[Version 5 Tests]', () {
test('Using URL namespace and custom name', () {
var u0 = uuid.v5(Uuid.NAMESPACE_URL, 'www.google.com');
var u1 = uuid.v5(Uuid.NAMESPACE_URL, 'www.google.com');
var u0 = uuid.v5(Namespace.URL.value, 'www.google.com');
var u1 = uuid.v5(Namespace.URL.value, 'www.google.com');

expect(u0, equals(u1));
});
Expand Down Expand Up @@ -439,11 +439,11 @@ void main() {
'offset 16 bytes before the end': size - 16,
}.entries) {
test(testCase.key, () {
final v = Uuid.parse(Uuid.NAMESPACE_OID,
final v = Uuid.parse(Namespace.OID.value,
buffer: buffer, offset: testCase.value);

expect(Uuid.unparse(v, offset: testCase.value),
equals(Uuid.NAMESPACE_OID));
equals(Namespace.OID.value));
});
}
});
Expand All @@ -457,7 +457,7 @@ void main() {
}.entries) {
test(testCase.key, () {
expect(
() => Uuid.parse(Uuid.NAMESPACE_OID,
() => Uuid.parse(Namespace.OID.value,
buffer: buffer, offset: testCase.value),
throwsA(isA<RangeError>()));
});
Expand Down

0 comments on commit 42b2b47

Please sign in to comment.