Skip to content

Commit

Permalink
Add way to get constant Namespace UUIDValues, and added extra utility…
Browse files Browse the repository at this point in the history
… functions to UuidValue
  • Loading branch information
daegalus committed Sep 26, 2024
1 parent 62cd4f7 commit 51fde46
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 16 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

v4.5.1

* Add `UuidValue.fromNamespace` to let you make constant variants of the namespace, as due to language limitations on Enums, `Namespace.*.uuidValue` can't be constant.
* Added `isV#()`, `isNil()`, and `isMax()` functions to `UuidValue` for matching Dart standards.

v4.5.0

* Change to CryptoRNG by default, you will now need to use MathRNG explicitly if you want speed over security. (thanks @Rexios80)
Expand Down
25 changes: 25 additions & 0 deletions lib/constants.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class InternalConstants {
/// The string constant for DNS, primarily for internal use, do not use,
/// no guarantees this won't be renamed or removed.
static const zDNS = '6ba7b810-9dad-11d1-80b4-00c04fd430c8';

/// The string constant for URL, primarily for internal use, do not use,
/// no guarantees this won't be renamed or removed.
static const zURL = '6ba7b811-9dad-11d1-80b4-00c04fd430c8';

/// The string constant for OID, primarily for internal use, do not use,
/// no guarantees this won't be renamed or removed.
static const zOID = '6ba7b812-9dad-11d1-80b4-00c04fd430c8';

/// The string constant for X500, primarily for internal use, do not use,
/// no guarantees this won't be renamed or removed.
static const zX500 = '6ba7b814-9dad-11d1-80b4-00c04fd430c8';

/// The string constant for NIL, primarily for internal use, do not use,
/// no guarantees this won't be renamed or removed.
static const zNIL = '00000000-0000-0000-0000-000000000000';

/// The string constant for MAX, primarily for internal use, do not use,
/// no guarantees this won't be renamed or removed.
static const zMAX = 'ffffffff-ffff-ffff-ffff-ffffffffffff';
}
43 changes: 29 additions & 14 deletions lib/enums.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// ignore_for_file: constant_identifier_names

import 'package:uuid/constants.dart';
import 'package:uuid/uuid.dart';

/// The options for UUID Validation strictness
Expand All @@ -9,32 +10,46 @@ enum ValidationMode { nonStrict, strictRFC4122 }
enum Namespace {
@Deprecated(
"Please use the lowercase variant to follow Dart style guidelines.")
DNS("6ba7b810-9dad-11d1-80b4-00c04fd430c8"),
DNS(value: InternalConstants.zDNS),
@Deprecated(
"Please use the lowercase variant to follow Dart style guidelines.")
URL("6ba7b811-9dad-11d1-80b4-00c04fd430c8"),
URL(value: InternalConstants.zURL),
@Deprecated(
"Please use the lowercase variant to follow Dart style guidelines.")
OID("6ba7b812-9dad-11d1-80b4-00c04fd430c8"),
OID(value: InternalConstants.zOID),
@Deprecated(
"Please use the lowercase variant to follow Dart style guidelines.")
X500("6ba7b814-9dad-11d1-80b4-00c04fd430c8"),
X500(value: InternalConstants.zX500),
@Deprecated(
"Please use the lowercase variant to follow Dart style guidelines.")
NIL("00000000-0000-0000-0000-000000000000"),
NIL(value: InternalConstants.zNIL),
@Deprecated(
"Please use the lowercase variant to follow Dart style guidelines.")
MAX("ffffffff-ffff-ffff-ffff-ffffffffffff"),
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"),
max("ffffffff-ffff-ffff-ffff-ffffffffffff");
MAX(value: InternalConstants.zMAX),
dns(value: InternalConstants.zDNS),
url(value: InternalConstants.zURL),
oid(value: InternalConstants.zOID),
x500(value: InternalConstants.zX500),
nil(value: InternalConstants.zNIL),
max(value: InternalConstants.zMAX);

const Namespace({required this.value});

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

UuidValue get uuidValue => UuidValue.raw(value);
UuidValue get uuidValue => value == InternalConstants.zNIL
? const UuidValue.raw(InternalConstants.zNIL)
: value == InternalConstants.zDNS
? const UuidValue.raw(InternalConstants.zDNS)
: value == InternalConstants.zURL
? const UuidValue.raw(InternalConstants.zURL)
: value == InternalConstants.zOID
? const UuidValue.raw(InternalConstants.zOID)
: value == InternalConstants.zX500
? const UuidValue.raw(InternalConstants.zX500)
: value == InternalConstants.zMAX
? const UuidValue.raw(InternalConstants.zMAX)
: const UuidValue.raw(InternalConstants.zNIL);

List<int> get bytes => Uuid.parse(value, validate: false);
}
2 changes: 1 addition & 1 deletion lib/uuid.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// ignore_for_file: deprecated_member_use_from_same_package
// TODO: Remove this ignore when we remove the deprecated options.

library uuid;
library;

import 'dart:typed_data';

Expand Down
42 changes: 42 additions & 0 deletions lib/uuid_value.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:typed_data';

import 'package:meta/meta.dart';
import 'package:uuid/constants.dart';

import 'parsing.dart';
import 'uuid.dart';
Expand Down Expand Up @@ -36,6 +37,22 @@ class UuidValue {
return UuidValue.raw(UuidParsing.unparse(byteList, offset: offset ?? 0));
}

/// fromNamespace() creates a UuidValue from a [Namespace] enum.
const UuidValue.fromNamespace(Namespace ns)
: uuid = ns == Namespace.nil
? InternalConstants.zNIL
: ns == Namespace.dns
? InternalConstants.zDNS
: ns == Namespace.url
? InternalConstants.zURL
: ns == Namespace.oid
? InternalConstants.zOID
: ns == Namespace.x500
? InternalConstants.zX500
: ns == Namespace.max
? InternalConstants.zMAX
: InternalConstants.zNIL;

/// withValidation() creates a UuidValue from a [uuid] string.
/// Optionally, you can provide a [validationMode] to use when validating
/// the uuid string.
Expand Down Expand Up @@ -102,5 +119,30 @@ class UuidValue {

// version gets the version of the UUID
int get version => int.parse(String.fromCharCode(uuid.codeUnitAt(14)));

// Unimplemented
int get time => -1;

// isV1() checks if the UUID is a version 1 UUID.
bool get isV1 => version == 1;

// isV4() checks if the UUID is a version 2 UUID.
bool get isV4 => version == 4;

// isV5() checks if the UUID is a version 5 UUID.
bool get isV5 => version == 5;

// isV6() checks if the UUID is a version 6 UUID.
bool get isV6 => version == 6;

// isV7() checks if the UUID is a version 7 UUID.
bool get isV7 => version == 7;

// isV8() checks if the UUID is a version 8 UUID.
bool get isV8 => version == 8;

// isNil() checks if the UUID is a nil UUID (00000000-0000-0000-0000-000000000000).
bool get isNil => uuid == Namespace.nil.value;

// isMAX() checks if the UUID is a MAX UUID (ffffffff-ffff-ffff-ffff-ffffffffffff).
}
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.5.0
version: 4.5.1
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
7 changes: 7 additions & 0 deletions test/uuid_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,13 @@ void main() {
UuidValue.withValidation(validGUID, ValidationMode.nonStrict);
expect(uuidval.uuid, validGUID.toLowerCase());
});

test('const namespace regression catcher', () {
const ns = Namespace.dns;
expect(ns.uuidValue, UuidValue.fromNamespace(ns));
const uuidval = UuidValue.fromNamespace(Namespace.url);
expect(uuidval.uuid, Namespace.url.value);
});
});

group('[Validation Test]', () {
Expand Down

0 comments on commit 51fde46

Please sign in to comment.