Moving GenUUID into utils

This commit is contained in:
DanB
2013-06-11 16:09:35 +02:00
parent 198a53b7ed
commit 0fbd35627a
7 changed files with 29 additions and 21 deletions

View File

@@ -689,12 +689,6 @@ func TestActionResetCounterCREDIT(t *testing.T) {
}
}
func TestUUID(t *testing.T) {
uuid := GenUUID()
if len(uuid) == 0 {
t.Fatalf("GenUUID error %s", uuid)
}
}
func TestActionTriggerLogging(t *testing.T) {
at := &ActionTrigger{

View File

@@ -27,6 +27,7 @@ import (
"strconv"
"strings"
"time"
"github.com/cgrates/cgrates/utils"
)
type CSVReader struct {
@@ -375,7 +376,7 @@ func (csvr *CSVReader) LoadActions() (err error) {
return errors.New(fmt.Sprintf("Could not parse action weight: %v", err))
}
a = &Action{
Id: GenUUID(),
Id: utils.GenUUID(),
ActionType: record[1],
BalanceId: record[2],
Direction: record[3],
@@ -424,7 +425,7 @@ func (csvr *CSVReader) LoadActionTimings() (err error) {
}
for _, t := range ts {
at := &ActionTiming{
Id: GenUUID(),
Id: utils.GenUUID(),
Tag: record[2],
Weight: weight,
Timing: &Interval{
@@ -466,7 +467,7 @@ func (csvr *CSVReader) LoadActionTriggers() (err error) {
return errors.New(fmt.Sprintf("Could not parse action trigger weight: %v", err))
}
at := &ActionTrigger{
Id: GenUUID(),
Id: utils.GenUUID(),
BalanceId: record[1],
Direction: record[2],
ThresholdValue: value,

View File

@@ -23,6 +23,7 @@ import (
"fmt"
"log"
"time"
"github.com/cgrates/cgrates/utils"
)
type DbReader struct {
@@ -216,7 +217,7 @@ func (dbr *DbReader) LoadActionTimings() (err error) {
}
for _, t := range ts {
actTmg := &ActionTiming{
Id: GenUUID(),
Id: utils.GenUUID(),
Tag: at.Tag,
Weight: at.Weight,
Timing: &Interval{

View File

@@ -92,7 +92,7 @@ func (mys *MySQLStorage) GetAllActionTimings(tpid string) (ats map[string][]*Act
}
at := &ActionTiming{
Id: GenUUID(),
Id: utils.GenUUID(),
Tag: timings_tag,
Weight: weight,
ActionsId: actions_tag,
@@ -348,7 +348,7 @@ func (mys *MySQLStorage) GetAllActions(tpid string) (map[string][]*Action, error
price = rate
}
a = &Action{
Id: GenUUID(),
Id: utils.GenUUID(),
ActionType: action,
BalanceId: balances_tag,
Direction: direction,
@@ -381,7 +381,7 @@ func (mys *MySQLStorage) GetAllActionTriggers(tpid string) (map[string][]*Action
}
at := &ActionTrigger{
Id: GenUUID(),
Id: utils.GenUUID(),
BalanceId: balances_tag,
Direction: direction,
ThresholdValue: threshold,

View File

@@ -330,7 +330,7 @@ func (psl *PostgresStorage) GetAllActions(tpid string) (map[string][]*Action, er
price = rate
}
a = &Action{
Id: GenUUID(),
Id: utils.GenUUID(),
ActionType: action,
BalanceId: balances_tag,
Direction: direction,
@@ -363,7 +363,7 @@ func (psl *PostgresStorage) GetAllActionTriggers(tpid string) (map[string][]*Act
}
at := &ActionTrigger{
Id: GenUUID(),
Id: utils.GenUUID(),
BalanceId: balances_tag,
Direction: direction,
ThresholdValue: threshold,

View File

@@ -23,6 +23,8 @@ import (
"fmt"
"encoding/hex"
"crypto/rand"
"strconv"
"time"
)
// Returns first non empty string out of vals. Useful to extract defaults
@@ -43,18 +45,21 @@ func FSCgrId(uuid string) string {
func NewTPid() string {
hasher := sha1.New()
uuid,_ := GenUUID()
uuid := GenUUID()
hasher.Write([]byte(uuid))
return fmt.Sprintf("%x", hasher.Sum(nil))
}
func GenUUID() (string, error) {
// helper function for uuid generation
func GenUUID() string {
uuid := make([]byte, 16)
n, err := rand.Read(uuid)
if n != len(uuid) || err != nil {
return "", err
return strconv.FormatInt(time.Now().UnixNano(), 10)
}
uuid[8] = 0x80
uuid[4] = 0x40
return hex.EncodeToString(uuid), nil
// TODO: verify the two lines implement RFC 4122 correctly
uuid[8] = 0x80 // variant bits see page 5
uuid[4] = 0x40 // version 4 Pseudo Random, see page 7
return hex.EncodeToString(uuid)
}

View File

@@ -32,3 +32,10 @@ func TestFirstNonEmpty(t *testing.T) {
t.Error("Wrong elemnt returned: ", winnerElmnt)
}
}
func TestUUID(t *testing.T) {
uuid := GenUUID()
if len(uuid) == 0 {
t.Fatalf("GenUUID error %s", uuid)
}
}