ballerina/uuid Ballerina library
Overview
This module provides APIs to generate and inspect UUIDs (Universally Unique Identifier).
The UUIDs are generated based on the RFC 4122 standard. UUID module supports generating 4 versions of UUIDs.
Version 1
Generated using the MAC address of the computer and the time of generation.
Version 3
Cryptographic hashing and application-provided text strings are used to generate a UUID. MD5 hashing is used.
Version 4
Uses a pseudo-random number generator to generate the UUID. Every bit of the string is randomly generated.
Version 5
Similar to Version 3 but uses SHA-1 instead of MD5.
Other operations include validating a given UUID string and getting the version of a UUID string.
For information on the operations, which you can perform with the uuid
module, see the below Functions.
Functions![](/images/permalink.svg)
createType1AsRecord![](/images/permalink.svg)
Returns a UUID of type 1 as a UUID record.
uuid:Uuid uuid1 = check uuid:createType1AsRecord();
createType1AsString![](/images/permalink.svg)
function createType1AsString() returns string
Returns a UUID of type 1 as a string.
string uuid1 = uuid:createType1AsString();
Return Type
- string - UUID of type 1 as a string
createType3AsRecord![](/images/permalink.svg)
function createType3AsRecord(NamespaceUUID namespace, string name) returns Uuid|Error
Returns a UUID of type 3 as a UUID record.
uuid:Uuid uuid3 = check uuid:createType3AsRecord(uuid:NAME_SPACE_DNS, “ballerina.io”);
Parameters
- namespace NamespaceUUID - String representation for a pre-defined namespace UUID
- name string - A name within the namespace
createType3AsString![](/images/permalink.svg)
function createType3AsString(NamespaceUUID namespace, string name) returns string|Error
Returns a UUID of type 3 as a string.
string uuid3 = check uuid:createType3AsString(uuid:NAME_SPACE_DNS, “ballerina.io”);
Parameters
- namespace NamespaceUUID - String representation for a pre-defined namespace UUID
- name string - A name within the namespace
createType4AsRecord![](/images/permalink.svg)
Returns a UUID of type 4 as a UUID record.
uuid:Uuid uuid4 = check uuid:createType4AsRecord();
createType4AsString![](/images/permalink.svg)
function createType4AsString() returns string
Returns a UUID of type 4 as a string.
string uuid4 = uuid:createType4AsString();
Return Type
- string - UUID of type 4 as a string
createType5AsRecord![](/images/permalink.svg)
function createType5AsRecord(NamespaceUUID namespace, string name) returns Uuid|Error
Returns a UUID of type 5 as a UUID record.
uuid:Uuid uuid5 = check uuid:createType5AsRecord(uuid:NAME_SPACE_DNS, “ballerina.io”);
Parameters
- namespace NamespaceUUID - String representation for a pre-defined namespace UUID
- name string - A name within the namespace
createType5AsString![](/images/permalink.svg)
function createType5AsString(NamespaceUUID namespace, string name) returns string|Error
Returns a UUID of type 5 as a string.
string uuid5 = check uuid:createType5AsString(uuid:NAME_SPACE_DNS, “ballerina.io”);
Parameters
- namespace NamespaceUUID - String representation for a pre-defined namespace UUID
- name string - A name within the namespace
getVersion![](/images/permalink.svg)
Detect RFC version of a UUID. Returns an error if the UUID is invalid.
uuid:Version v = check uuid:getVersion("4397465e-35f9-11eb-adc1-0242ac120002");
Parameters
- uuid string - UUID string to be checked
nilAsRecord![](/images/permalink.svg)
function nilAsRecord() returns Uuid
Returns a nil UUID as a UUID record.
uuid:Uuid nilUUID = uuid:nilAsRecord();
Return Type
- Uuid - nil UUID
nilAsString![](/images/permalink.svg)
function nilAsString() returns string
Returns a nil UUID as a string.
string nilUUID = uuid:nilAsString();
Return Type
- string - nil UUID
toBytes![](/images/permalink.svg)
Converts to an array of bytes. Returns an error if the UUID is invalid.
byte[] b = check uuid:toBytes(“6ec0bd7f-11c0-43da-975e-2aesass0b”);
Return Type
- byte[]|Error - UUID as bytes or else a
uuid:Error
toRecord![](/images/permalink.svg)
Converts to a UUID record. Returns an error if the array is invalid.
uuid:Uuid r1 = check uuid:toRecord("4397465e-35f9-11eb-adc1-0242ac120002"); uuid:Uuid r2 = check uuid:toRecord([10, 20, 30]);
Parameters
- uuid string|byte[] - UUID to be converted
toString![](/images/permalink.svg)
Converts to a UUID string. Returns an error if the array is invalid.
string s = check uuid:toString([5, 12, 16, 35]);
Parameters
- uuid byte[]|Uuid - UUID to be converted
validate![](/images/permalink.svg)
Test a string to see if it is a valid UUID.
boolean valid = uuid:validate("4397465e-35f9-11eb-adc1-0242ac120002");
Parameters
- uuid string - UUID string to be validated
Return Type
- boolean - true if a valied UUID, false if not
Enums![](/images/permalink.svg)
uuid: NamespaceUUID![](/images/permalink.svg)
Represents UUIDs strings of well known namespace IDs.
Members
uuid: Version![](/images/permalink.svg)
Represents the UUID versions.
Members
Records![](/images/permalink.svg)
uuid: Uuid![](/images/permalink.svg)
Represents a UUID.
Fields
- timeLow Unsigned32 - The low field of the timestamp
- timeMid Unsigned16 - The middle field of the timestamp
- timeHiAndVersion Unsigned16 - The high field of the timestamp multiplexed with the version number
- clockSeqHiAndReserved Unsigned8 - The high field of the clock sequence multiplexed with the variant
- clockSeqLo Unsigned8 - The low field of the clock sequence
- node int - The spatially unique node identifier
Errors![](/images/permalink.svg)
uuid: Error![](/images/permalink.svg)
Represents UUID module related errors.
Import
import ballerina/uuid;
Metadata
Released date: almost 3 years ago
Version: 1.0.0
Compatibility
Platform: java11
Ballerina version: slbeta3
Pull count
Total: 23588
Current verison: 42
Weekly downloads
Dependencies