Skip to content

Commit

Permalink
Fix UUID bugs and refactor. Closes #54.
Browse files Browse the repository at this point in the history
* Update UUID namespace handling

* Fix bugs in UUID implementation

* Fix tests
  • Loading branch information
ryanleary committed Aug 25, 2017
1 parent ea00ddb commit 9ddea19
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 113 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -296,7 +296,7 @@ For UUIDv3 & UUIDV5 are a bit more complex. These require a Name Space which is
"spec":{
"doc.uuid":{
"version":5,
"nameSpace":"DNS",
"namespace":"DNS",
"names":[
{"path":"doc.author_name", "default":"some string"},
{"path":"doc.type", "default":"another string"},
Expand Down
15 changes: 11 additions & 4 deletions transform/util.go
Expand Up @@ -40,7 +40,10 @@ type Config struct {
InPlace bool `json:"inplace,omitempty"`
}

var jsonPathRe = regexp.MustCompile("([^\\[\\]]+)\\[(.*?)\\]")
var (
NonExistentPath = RequireError("Path does not exist")
jsonPathRe = regexp.MustCompile("([^\\[\\]]+)\\[(.*?)\\]")
)

// Given a json byte slice `data` and a kazaam `path` string, return the object at the path in data if it exists.
func getJSONRaw(data []byte, path string, pathRequired bool) ([]byte, error) {
Expand Down Expand Up @@ -70,7 +73,7 @@ func getJSONRaw(data []byte, path string, pathRequired bool) ([]byte, error) {
}, beforePath...)
if err == jsonparser.KeyPathNotFoundError {
if pathRequired {
return nil, RequireError("Path does not exist")
return nil, NonExistentPath
}
} else if err != nil {
return nil, err
Expand All @@ -80,7 +83,11 @@ func getJSONRaw(data []byte, path string, pathRequired bool) ([]byte, error) {
if newPath != "" {
for i, value := range results {
intermediate, err := getJSONRaw(value, newPath, pathRequired)
if err != nil {
if err == jsonparser.KeyPathNotFoundError {
if pathRequired {
return nil, NonExistentPath
}
} else if err != nil {
return nil, err
}
results[i] = intermediate
Expand Down Expand Up @@ -121,7 +128,7 @@ func getJSONRaw(data []byte, path string, pathRequired bool) ([]byte, error) {
}
if err == jsonparser.KeyPathNotFoundError {
if pathRequired {
return nil, RequireError("Path does not exist")
return nil, NonExistentPath
}
} else if err != nil {
return nil, err
Expand Down
121 changes: 61 additions & 60 deletions transform/uuid.go
Expand Up @@ -18,108 +18,109 @@ func UUID(spec *Config, data []byte) ([]byte, error) {
for k, v := range *spec.Spec {
outPath := strings.Split(k, ".")

// convert to corrct type
// convert spec to correct type
uuidSpec, ok := v.(map[string]interface{})
if !ok {
return nil, SpecError("Invalid Spec for UUID")
}

//grab version
version, ok := uuidSpec["version"]

if !ok {
version := getUUIDVersion(uuidSpec)
if version < 3 || version > 5 {
return nil, versionError
}

var u uuid.UUID
var err error

switch version {
case 4.0:
case 4:
u = uuid.NewV4()

case 3.0, 5.0:
case 3, 5:
// choose the correct UUID function
var NewUUID func(uuid.UUID, string) uuid.UUID
NewUUID = uuid.NewV3
if version == 5 {
NewUUID = uuid.NewV5
}

// pull required configuration from spec and do validation
names, ok := uuidSpec["names"]
if !ok {
return nil, SpecError("Must provide names field")
}

nameSpace, ok := uuidSpec["nameSpace"].(string)
namespaceString, ok := uuidSpec["namespace"].(string)
if !ok {
return nil, SpecError("Must provide namesapce, Must be a string")
}

var nameSpaceUUID uuid.UUID

// swtich on the namespace
switch nameSpace {
case "DNS":
nameSpaceUUID = uuid.NamespaceDNS
case "URL":
nameSpaceUUID = uuid.NamespaceURL
case "OID":
nameSpaceUUID = uuid.NamespaceOID
case "X500":
nameSpaceUUID = uuid.NamespaceX500
default:
nameSpaceUUID, err = uuid.FromString(nameSpace)
if err != nil {
return nil, SpecError("nameSpace is not a valid UUID or is not DNS, URL, OID, X500")
}
return nil, SpecError("Must provide `namespace` as a string")
}

nameFields, ok := names.([]interface{})
if !ok {
return nil, SpecError("Spec is invalid")
return nil, SpecError("Spec is invalid. `Names` field must be an array.")
}

// generate the required namespace
u, err = namespaceFromString(namespaceString)
if err != nil {
return nil, SpecError("namespace is not a valid UUID or is not DNS, URL, OID, X500")
}

// loop over the names field
for _, field := range nameFields {
p, _ := field.(map[string]interface{})["path"].(string)

name, err := getJSONRaw(data, p, false)
if err == jsonparser.KeyPathNotFoundError {

d, ok := field.(map[string]interface{})["default"].(string)
name, pathErr := getJSONRaw(data, p, true)
// if a string, remove the heading and trailing quote
nameString := strings.TrimPrefix(strings.TrimSuffix(string(name), "\""), "\"")
if pathErr == NonExistentPath {
nameString, ok = field.(map[string]interface{})["default"].(string)
if !ok {
return nil, SpecError("Spec is invalid")

return nil, SpecError("Spec is invalid. Unable to get path or default")
}
name = []byte(d)

}

// check if there is an empty uuid & version 3
if u.String() == "00000000-0000-0000-0000-000000000000" && version == 3.0 {

u = uuid.NewV3(nameSpaceUUID, string(name))

// same as above except version 5
} else if u.String() == "00000000-0000-0000-0000-000000000000" && version == 5.0 {

u = uuid.NewV5(nameSpaceUUID, string(name))

} else if version == 3.0 {

u = uuid.NewV3(u, string(name))

} else if version == 5.0 {

u = uuid.NewV3(u, string(name))
}
u = NewUUID(u, nameString)
}

default:
return nil, versionError

}
// set the uuid in the appropraite place
d, err := jsonparser.Set(data, bookend([]byte(u.String()), '"', '"'), outPath...)
if err != nil {
return nil, err
}

return d, nil
}
return nil, SpecError("Spec invalid for UUID")
}

func namespaceFromString(namespace string) (uuid.UUID, error) {
var u uuid.UUID
var err error
switch namespace {
case "DNS":
u = uuid.NamespaceDNS
case "URL":
u = uuid.NamespaceURL
case "OID":
u = uuid.NamespaceOID
case "X500":
u = uuid.NamespaceX500
default:
u, err = uuid.FromString(namespace)
}
return u, err
}

func getUUIDVersion(uuidSpec map[string]interface{}) int {
var version int
versionInterface, ok := uuidSpec["version"]
if !ok {
return -1
}
versionFloat, ok := versionInterface.(float64)
version = int(versionFloat)
if !ok || version < 3 || version > 5 {
return -2
}
return version
}

0 comments on commit 9ddea19

Please sign in to comment.