...

Source file src/github.com/growthbook/growthbook-golang/hash.go

Documentation: github.com/growthbook/growthbook-golang

     1  package growthbook
     2  
     3  import (
     4  	"fmt"
     5  	"hash/fnv"
     6  )
     7  
     8  // Convert integer or string hash values to strings.
     9  func convertHashValue(vin interface{}) (string, bool) {
    10  	hashString, stringOK := vin.(string)
    11  	if stringOK {
    12  		if hashString == "" {
    13  			logInfo("Skip because of empty hash attribute")
    14  			return "", false
    15  		}
    16  		return hashString, true
    17  	}
    18  	hashInt, intOK := vin.(int)
    19  	if intOK {
    20  		return fmt.Sprint(hashInt), true
    21  	}
    22  	hashFloat, floatOK := vin.(float64)
    23  	if floatOK {
    24  		return fmt.Sprint(int(hashFloat)), true
    25  	}
    26  	return "", false
    27  }
    28  
    29  // Simple wrapper around Go standard library FNV32a hash function.
    30  func hashFnv32a(s string) uint32 {
    31  	hash := fnv.New32a()
    32  	hash.Write([]byte(s))
    33  	return hash.Sum32()
    34  }
    35  
    36  // Main hash function.
    37  func hash(seed string, value string, version int) *float64 {
    38  	switch version {
    39  	case 2:
    40  		v := float64(hashFnv32a(fmt.Sprint(hashFnv32a(seed+value)))%10000) / 10000
    41  		return &v
    42  	case 1:
    43  		v := float64(hashFnv32a(value+seed)%1000) / 1000
    44  		return &v
    45  	default:
    46  		return nil
    47  	}
    48  }
    49  

View as plain text