github.com/resonatecoop/user-api@v1.0.0-13.0.20220915120639-05dc9c04014a/pkg/uuid/uuid.go (about)

     1  package uuidpkg
     2  
     3  import (
     4  	uuid "github.com/google/uuid"
     5  )
     6  
     7  func IsValidUUID(u string) bool {
     8  	_, err := uuid.Parse(u)
     9  	return err == nil
    10  }
    11  
    12  //ConvertUUIDToStrArray returns a slice of uuids for given slive of strings
    13  func ConvertUUIDToStrArray(uuids []uuid.UUID) []string {
    14  	strArray := make([]string, len(uuids))
    15  	for i := range uuids {
    16  		strArray[i] = uuids[i].String()
    17  	}
    18  	return strArray
    19  }
    20  
    21  // // GetUUIDFromString returns id as string and returns error if not a valid uuid
    22  // func GetUUIDFromString(id string) (uuid.UUID, twirp.Error) {
    23  // 	uid, err := uuid.FromString(id)
    24  // 	if err != nil {
    25  // 		return uuid.UUID{}, twirp.InvalidArgumentError("id", "must be a valid uuid")
    26  // 	}
    27  // 	return uid, nil
    28  // }
    29  
    30  // Difference returns difference between two slices of uuids
    31  func Difference(a, b []uuid.UUID) []uuid.UUID {
    32  	mb := map[uuid.UUID]bool{}
    33  	for _, x := range b {
    34  		mb[x] = true
    35  	}
    36  	ab := []uuid.UUID{}
    37  	for _, x := range a {
    38  		if _, ok := mb[x]; !ok {
    39  			ab = append(ab, x)
    40  		}
    41  	}
    42  	return ab
    43  }
    44  
    45  // Equal compares two uuid slices and returns true if equal
    46  func Equal(a, b []uuid.UUID) bool {
    47  	if len(a) != len(b) {
    48  		return false
    49  	}
    50  	for i, v := range a {
    51  		if v != b[i] {
    52  			return false
    53  		}
    54  	}
    55  	return true
    56  }
    57  
    58  // RemoveDuplicates returns slices of uuid without duplicates
    59  func RemoveDuplicates(elements []uuid.UUID) []uuid.UUID {
    60  	// Use map to record duplicates as we find them
    61  	encountered := map[uuid.UUID]bool{}
    62  	result := []uuid.UUID{}
    63  
    64  	for v := range elements {
    65  		if encountered[elements[v]] == true {
    66  			// Do not add duplicate
    67  		} else {
    68  			// Record this element as an encountered element
    69  			encountered[elements[v]] = true
    70  			// Append to result slice
    71  			result = append(result, elements[v])
    72  		}
    73  	}
    74  	// Return the new slice.
    75  	return result
    76  }