Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

feat: UUID Generator #40

Merged
merged 8 commits into from
Oct 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
# Output of the go coverage tool, specifically when used with LiteIDE
*.out
cover.html
.vscode/


# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
.glide/
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ Supported tag:
* Amount
* Amount with Currency

**UUID :**
* UUID Digit (32 bytes)
* UUID Hyphenated (36 bytes)

**Skip :**
* \-

Expand Down Expand Up @@ -146,6 +150,8 @@ type SomeStruct struct {
Currency string `faker:"currency"`
Amount float64 `faker:"amount"`
AmountWithCurrency string `faker:"amount_with_currency"`
UUIDHypenated string `faker:"uuid_hyphenated"`
UUID string `faker:"uuid_digit"`
Skip string `faker:"-"`
}

Expand Down Expand Up @@ -198,6 +204,8 @@ func main() {
Currency: IRR,
Amount: 88.990000,
AmountWithCurrency: XBB 49257.100000,
UUIDHypenated: 8f8e4463-9560-4a38-9b0c-ef24481e4e27,
UUID: 90ea6479fd0e4940af741f0a87596b73,
Skip:
}
*/
Expand Down
4 changes: 4 additions & 0 deletions faker.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const (
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
tagName = "faker"
ID = "uuid_digit"
HyphenatedID = "uuid_hyphenated"
Email = "email"
MacAddress = "mac_address"
DomainName = "domain_name"
Expand Down Expand Up @@ -102,6 +104,8 @@ var mapperTag = map[string]interface{}{
Currency: GetPrice().Currency,
Amount: GetPrice().Amount,
AmountWithCurrency: GetPrice().AmountWithCurrency,
ID: GetIdentifier().Digit,
HyphenatedID: GetIdentifier().Hyphenated,
}

// Generic Error Messages for tags
Expand Down
7 changes: 7 additions & 0 deletions faker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ type SomeStruct struct {
Currency string `faker:"currency"`
Amount float64 `faker:"amount"`
AmountWithCurrency string `faker:"amount_with_currency"`
ID string `faker:"uuid_digit"`
HyphenatedID string `faker:"uuid_hyphenated"`

MapStringString map[string]string
MapStringStruct map[string]AStruct
Expand Down Expand Up @@ -107,6 +109,8 @@ type TaggedStruct struct {
Currency string `faker:"currency"`
Amount float32 `faker:"amount"`
AmountWithCurrency string `faker:"amount_with_currency"`
ID string `faker:"uuid_digit"`
HyphenatedID string `faker:"uuid_hyphenated"`
}

func (t TaggedStruct) String() string {
Expand Down Expand Up @@ -149,6 +153,8 @@ func (t TaggedStruct) String() string {
Currency: %s,
Amount: %f,
AmountWithCurrency: %s,
HyphenatedID: %s,
ID: %s,
}`, t.Latitude, t.Longitude, t.CreditCardNumber,
t.CreditCardType, t.Email, t.IPV4,
t.IPV6, t.Password, t.PhoneNumber, t.MacAddress,
Expand All @@ -160,6 +166,7 @@ func (t TaggedStruct) String() string {
t.DayOfMonth, t.Timestamp, t.Century, t.TimeZone,
t.TimePeriod, t.Word, t.Sentence, t.Paragraph,
t.Currency, t.Amount, t.AmountWithCurrency,
t.HyphenatedID, t.ID,
)
}

Expand Down
54 changes: 54 additions & 0 deletions uuid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package faker

import (
"crypto/rand"
"fmt"
"io"
)

var identifier Identifier

// GetIdentifier returns a new Identifier
func GetIdentifier() Identifier {
mu.Lock()
defer mu.Unlock()

if identifier == nil {
identifier = &UUID{}
}
return identifier
}

// Identifier ...
type Identifier interface {
Digit() string
Hyphenated() string
}

// UUID struct
type UUID struct{}

// createUUID returns a 16 byte slice with random values
func createUUID() []byte {
b := make([]byte, 16)
io.ReadFull(rand.Reader, b)
// variant bits; see section 4.1.1
b[8] = b[8]&^0xc0 | 0x80
// version 4 (pseudo-random); see section 4.1.3
b[6] = b[6]&^0xf0 | 0x40
return b
}

// Hyphenated returns a 36 byte hyphenated UUID
func (i UUID) Hyphenated() string {
b := createUUID()
uuid := fmt.Sprintf("%x-%x-%x-%x-%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
return uuid
}

// Digit returns a 32 bytes UUID
func (i UUID) Digit() string {
b := createUUID()
uuid := fmt.Sprintf("%x", b)
return uuid
}
33 changes: 33 additions & 0 deletions uuid_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package faker

import (
"fmt"
"regexp"
"testing"
)

func TestDigit(t *testing.T) {
p := GetIdentifier()
uuid := p.Digit()
if match, err := regexp.Match("^[a-zA-Z0-9]{32}$", []byte(uuid)); !match || err != nil {
t.Errorf("Could not match the UUID format, err: %+v, match: %+v", err, match)
}
}

func TestHyphenated(t *testing.T) {
p := GetIdentifier()
uuid := p.Hyphenated()
exp := "[a-zA-Z 0-9]"
pattern := fmt.Sprintf("^%s{8}-%s{4}-%s{4}-%s{4}-%s{12}$", exp, exp, exp, exp, exp)
if match, err := regexp.Match(pattern, []byte(uuid)); !match || err != nil {
t.Errorf("Could not match the UUID hyphenated format, err: %+v, match: %+v", err, match)
}

}

func TestGetIdentifier(t *testing.T) {
identifier := GetIdentifier()
if identifier == nil {
t.Fatalf("TestGetIdentifier failed because identifier was nil")
}
}