2016-02-26 17:27:38 +00:00
|
|
|
// Copyright © 2016 Abcum Ltd
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package uuid
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/satori/go.uuid"
|
|
|
|
)
|
|
|
|
|
2016-03-15 12:17:47 +00:00
|
|
|
var (
|
|
|
|
NamespaceDNS = uuid.NamespaceDNS
|
|
|
|
NamespaceURL = uuid.NamespaceURL
|
|
|
|
NamespaceOID = uuid.NamespaceOID
|
|
|
|
NamespaceX500 = uuid.NamespaceX500
|
|
|
|
)
|
|
|
|
|
|
|
|
type UUID struct {
|
|
|
|
uuid.UUID
|
|
|
|
}
|
|
|
|
|
2016-02-26 17:27:38 +00:00
|
|
|
// NewV1 returns a new UUID (Version 1) based on current timestamp and MAC address.
|
2016-03-15 12:17:47 +00:00
|
|
|
func NewV1() *UUID {
|
|
|
|
return &UUID{uuid.NewV1()}
|
2016-02-26 17:27:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewV2 returns a new DCE Security UUID (Version 2) based on POSIX UID/GID.
|
2016-03-15 12:17:47 +00:00
|
|
|
func NewV2(domain byte) *UUID {
|
|
|
|
return &UUID{uuid.NewV2(domain)}
|
2016-02-26 17:27:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewV3 returns a new UUID (Version 3) based on MD5 hash of namespace UUID and name.
|
2016-03-15 12:17:47 +00:00
|
|
|
func NewV3(ns uuid.UUID, name string) *UUID {
|
|
|
|
return &UUID{uuid.NewV3(ns, name)}
|
2016-02-26 17:27:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewV4 returns a new UUID (Version 4) using 16 random bytes or panics.
|
2016-03-15 12:17:47 +00:00
|
|
|
func NewV4() *UUID {
|
|
|
|
return &UUID{uuid.NewV4()}
|
2016-02-26 17:27:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewV5 returns a new UUID (Version 5) based on SHA-1 hash of namespace UUID and name.
|
2016-03-15 12:17:47 +00:00
|
|
|
func NewV5(ns uuid.UUID, name string) *UUID {
|
|
|
|
return &UUID{uuid.NewV5(ns, name)}
|
2016-02-26 17:27:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetUUID parses and checks for a valid UUID string, and returns Nil if not valid.
|
2016-03-15 12:17:47 +00:00
|
|
|
func GetUUID(input string) *UUID {
|
|
|
|
return &UUID{uuid.FromStringOrNil(input)}
|
2016-02-26 17:27:38 +00:00
|
|
|
}
|