...

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

Documentation: github.com/growthbook/growthbook-golang

     1  package growthbook
     2  
     3  import "encoding/json"
     4  
     5  // Namespace specifies what part of a namespace an experiment
     6  // includes. If two experiments are in the same namespace and their
     7  // ranges don't overlap, they wil be mutually exclusive.
     8  type Namespace struct {
     9  	ID    string
    10  	Start float64
    11  	End   float64
    12  }
    13  
    14  // Determine whether a user's ID lies within a given namespace.
    15  func (namespace *Namespace) inNamespace(userID string) bool {
    16  	n := float64(hashFnv32a(userID+"__"+namespace.ID)%1000) / 1000
    17  	return n >= namespace.Start && n < namespace.End
    18  }
    19  
    20  // ParseNamespace creates a Namespace value from raw JSON input.
    21  func ParseNamespace(data []byte) *Namespace {
    22  	array := []interface{}{}
    23  	err := json.Unmarshal(data, &array)
    24  	if err != nil {
    25  		logError("Failed parsing JSON input", "Namespace")
    26  		return nil
    27  	}
    28  	return BuildNamespace(array)
    29  }
    30  
    31  // BuildNamespace creates a Namespace value from a generic JSON value.
    32  func BuildNamespace(val interface{}) *Namespace {
    33  	array, ok := val.([]interface{})
    34  	if !ok || len(array) != 3 {
    35  		logError("Invalid JSON data type", "Namespace")
    36  		return nil
    37  	}
    38  	id, ok1 := array[0].(string)
    39  	start, ok2 := array[1].(float64)
    40  	end, ok3 := array[2].(float64)
    41  	if !ok1 || !ok2 || !ok3 {
    42  		logError("Invalid JSON data type", "Namespace")
    43  		return nil
    44  	}
    45  	return &Namespace{id, start, end}
    46  }
    47  

View as plain text