...

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

Documentation: github.com/growthbook/growthbook-golang

     1  package growthbook
     2  
     3  import "encoding/json"
     4  
     5  // Attributes is an arbitrary JSON object containing user and request
     6  // attributes.
     7  type Attributes map[string]interface{}
     8  
     9  // FeatureMap is a map of feature objects, keyed by string feature
    10  // IDs.
    11  type FeatureMap map[string]*Feature
    12  
    13  // ParseFeatureMap creates a FeatureMap value from raw JSON input.
    14  func ParseFeatureMap(data []byte) FeatureMap {
    15  	dict := map[string]interface{}{}
    16  	err := json.Unmarshal(data, &dict)
    17  	if err != nil {
    18  		logError("Failed parsing JSON input", "FeatureMap")
    19  		return nil
    20  	}
    21  	return BuildFeatureMap(dict)
    22  }
    23  
    24  // BuildFeatureMap creates a FeatureMap value from a JSON object
    25  // represented as a Go map.
    26  func BuildFeatureMap(dict map[string]interface{}) FeatureMap {
    27  	fmap := FeatureMap{}
    28  	for k, v := range dict {
    29  		feature := BuildFeature(v)
    30  		if feature != nil {
    31  			fmap[k] = feature
    32  		}
    33  	}
    34  	return fmap
    35  }
    36  
    37  // ForcedVariationsMap is a map that forces an Experiment to always
    38  // assign a specific variation. Useful for QA.
    39  //
    40  // Keys are the experiment key, values are the array index of the
    41  // variation.
    42  type ForcedVariationsMap map[string]int
    43  
    44  // URL matching supports regular expressions or simple string matches.
    45  type URLTargetType uint
    46  
    47  const (
    48  	RegexURLTarget  URLTargetType = iota
    49  	SimpleURLTarget               = iota
    50  )
    51  
    52  // URL match target.
    53  type URLTarget struct {
    54  	Type    URLTargetType
    55  	Include bool
    56  	Pattern string
    57  }
    58  
    59  // FeatureResultSource is an enumerated type representing the source
    60  // of a FeatureResult.
    61  type FeatureResultSource uint
    62  
    63  // FeatureResultSource values.
    64  const (
    65  	UnknownResultSource FeatureResultSource = iota + 1
    66  	DefaultValueResultSource
    67  	ForceResultSource
    68  	ExperimentResultSource
    69  	OverrideResultSource
    70  )
    71  
    72  // ParseFeatureResultSource creates a FeatureResultSource value from
    73  // its string representation.
    74  func ParseFeatureResultSource(source string) FeatureResultSource {
    75  	switch source {
    76  	case "", "defaultValue":
    77  		return DefaultValueResultSource
    78  	case "force":
    79  		return ForceResultSource
    80  	case "experiment":
    81  		return ExperimentResultSource
    82  	case "override":
    83  		return OverrideResultSource
    84  	default:
    85  		return UnknownResultSource
    86  	}
    87  }
    88  

View as plain text