diff --git a/UUID.go b/UUID.go deleted file mode 100644 index cf380b1..0000000 --- a/UUID.go +++ /dev/null @@ -1,100 +0,0 @@ -// Note: this file needs to be extracted into a separate library (by the RJ team) -package main - -import ( - "crypto/md5" - "errors" - "strconv" - "strings" -) - -// UUID is modeled after java.util.UUID -// Holds the most significant and least significant bits of a UUID -type UUID struct { - msb uint64 - lsb uint64 -} - -// NewNameUUIDFromBytes creates a type 3 - name based - UUID -// based on the specified byte array. -// Corresponds to java.util.UUID#nameUUIDFromBytes -func NewNameUUIDFromBytes(bytes []byte) *UUID { - md5Hash := md5.Sum(bytes) - md5Hash[6] &= 0x0f /* clear version */ - md5Hash[6] |= 0x30 /* set to version 3 */ - md5Hash[8] &= 0x3f /* clear variant */ - md5Hash[8] |= 0x80 /* set to IETF variant */ - - var msb uint64 - var lsb uint64 - - for i := 0; i < 8; i++ { - msb = (msb << 8) | (uint64(md5Hash[i]) & 0xff) - } - for i := 8; i < 16; i++ { - lsb = (lsb << 8) | (uint64(md5Hash[i]) & 0xff) - } - - return &UUID{msb, lsb} -} - -// NewUUIDFromString creates a UUID from the standard string representation -// Corresponds to java.util.UUID#fromString -func NewUUIDFromString(uuidString string) (*UUID, error) { - components := strings.Split(uuidString, "-") - if len(components) != 5 { - return &UUID{0, 0}, errors.New("Invalid UUID string") - } - - intComponents, err := hexToInt(components) - - if err != nil { - return &UUID{0, 0}, err - } - - msb := intComponents[0] - msb <<= 16 - msb |= intComponents[1] - msb <<= 16 - msb |= intComponents[2] - - lsb := intComponents[3] - lsb <<= 48 - lsb |= intComponents[4] - - return &UUID{msb, lsb}, nil -} - -// String returns a string representing this UUID. -// Corresponds to java.util.UUID#toString -func (uuid *UUID) String() string { - parts := make([]string, 5) - parts[0] = digits(uuid.msb>>32, 8) - parts[1] = digits(uuid.msb>>16, 4) - parts[2] = digits(uuid.msb, 4) - parts[3] = digits(uuid.lsb>>48, 4) - parts[4] = digits(uuid.lsb, 12) - - return strings.Join(parts, "-") -} - -// Corresponds to java.util.UUID#digits -func digits(val uint64, digits uint) string { - hi := 1 << (digits * 4) - result := uint64(hi) | (val & (uint64(hi) - uint64(1))) - return strconv.FormatInt(int64(result), 16)[1:] -} - -func hexToInt(hexStrings []string) ([]uint64, error) { - ints := make([]uint64, len(hexStrings)) - - for i, hexString := range hexStrings { - result, err := strconv.ParseUint(hexString, 16, 0) - if err != nil { - return nil, errors.New("Errors converting hex string: [" + hexString + "].") - } - ints[i] = result - } - - return ints, nil -} diff --git a/imagesetUUIDGenerator.go b/imagesetUUIDGenerator.go deleted file mode 100644 index 2a35839..0000000 --- a/imagesetUUIDGenerator.go +++ /dev/null @@ -1,33 +0,0 @@ -// Note: this file needs to be extracted into a separate library (by the RJ team) -package main - -import ( - "strconv" - "strings" - - "github.com/willf/bitset" -) - -var magic = toBitSet(NewNameUUIDFromBytes([]byte("imageset")).lsb) - -// GenerateImageSetUUID generated the image set UUID corresponding to the given -// image UUID -func GenerateUUID(imageUUID UUID) (UUID, error) { - uuidBits := toBitSet(imageUUID.lsb) - uuidBits.InPlaceSymmetricDifference(&magic) //XOR - - lsb, err := strconv.ParseUint(strings.TrimSuffix(uuidBits.DumpAsBits(), "."), 2, 64) - return UUID{imageUUID.msb, uint64(lsb)}, err -} - -func toBitSet(number uint64) bitset.BitSet { - lsbString := strconv.FormatUint(number, 2) - - var b bitset.BitSet - for i, character := range lsbString { - if character == '1' { - b.Set(uint(len(lsbString)-1) - uint(i)) - } - } - return b -} diff --git a/imagesetUUIDGenerator_test.go b/imagesetUUIDGenerator_test.go deleted file mode 100644 index 0aa050f..0000000 --- a/imagesetUUIDGenerator_test.go +++ /dev/null @@ -1,37 +0,0 @@ -package main - -import ( - "testing" -) - -func TestGenerateImageSetUUID(t *testing.T) { - expectedImagesetUUIDString := "f622bfc6-4931-11e4-9d7e-00144feab7de" - imageUUIDString := "f622bfc6-4931-11e4-0318-978e959e1c97" - imageUUID, _ := NewUUIDFromString(imageUUIDString) - - imagesetUUID, err := GenerateUUID(*imageUUID) - - if err != nil { - t.Error("Returned error for valid input") - } - actualImagesetUUIDString := imagesetUUID.String() - if expectedImagesetUUIDString != actualImagesetUUIDString { - t.Error("Imageset UUID was not generated correctly.") - } -} - -func TestGenerateImageUUIDFromImageSetUUID(t *testing.T) { - imagesetUUIDString := "f622bfc6-4931-11e4-0318-978e959e1c97" - expectedImageUUIDString := "f622bfc6-4931-11e4-9d7e-00144feab7de" - imagesetUUID, _ := NewUUIDFromString(imagesetUUIDString) - - imageUUID, err := GenerateUUID(*imagesetUUID) - - if err != nil { - t.Error("Returned error for valid input") - } - actualImageUUIDString := imageUUID.String() - if expectedImageUUIDString != actualImageUUIDString { - t.Error("Image UUID was not generated correctly.") - } -} diff --git a/model.go b/model.go index ea04d7a..1f7d7cf 100644 --- a/model.go +++ b/model.go @@ -5,6 +5,7 @@ import ( log "github.com/Sirupsen/logrus" "strings" "time" + "github.com/Financial-Times/uuid-utils-go" ) const ( @@ -254,12 +255,12 @@ func convertToESContentModel(enrichedContent enrichedContentModel, contentType s if enrichedContent.Content.MainImage != "" { esModel.ThumbnailURL = new(string) - var imageID UUID + var imageID *uuidutils.UUID //Generate the actual image UUID from the received image set UUID - imageSetUUID, err := NewUUIDFromString(enrichedContent.Content.MainImage) + imageSetUUID, err := uuidutils.NewUUIDFromString(enrichedContent.Content.MainImage) if err == nil { - imageID, err = GenerateUUID(*imageSetUUID) + imageID, err = uuidutils.NewUUIDDeriverWith(uuidutils.IMAGE_SET).From(imageSetUUID) } if err != nil { diff --git a/vendor/vendor.json b/vendor/vendor.json index 83507b6..7007e12 100644 --- a/vendor/vendor.json +++ b/vendor/vendor.json @@ -44,6 +44,14 @@ "revision": "df2f00c734957c9dd651ce23ab0e0902504c7636", "revisionTime": "2017-03-28T16:39:54Z" }, + { + "checksumSHA1": "OctXH9nxfVzFlVWD7h4K5e+x5/M=", + "path": "github.com/Financial-Times/uuid-utils-go", + "revision": "e22658edd0f130936e99079bf844bde7087c914c", + "revisionTime": "2017-05-16T11:04:27Z", + "version": "1.0.1", + "versionExact": "1.0.1" + }, { "checksumSHA1": "ZKxETlJdB2XubMrZnXB0FQimVA8=", "path": "github.com/Sirupsen/logrus", @@ -126,7 +134,7 @@ "revisionTime": "2017-01-30T11:31:45Z" }, { - "checksumSHA1": "xegYczAmM0AnZfE/Salv75CB1R0=", + "checksumSHA1": "6KZEap6v/5gD+tjiNbpD81kD61o=", "path": "github.com/willf/bitset", "revision": "988f4f24992fc745de53c42df0da6581e42a6686", "revisionTime": "2017-05-05T19:16:46Z"