...

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

Documentation: github.com/growthbook/growthbook-golang

     1  package growthbook
     2  
     3  // Result records the result of running an Experiment given a specific
     4  // Context.
     5  type Result struct {
     6  	Value         FeatureValue
     7  	VariationID   int
     8  	Key           string
     9  	Name          string
    10  	Bucket        *float64
    11  	Passthrough   bool
    12  	InExperiment  bool
    13  	HashUsed      bool
    14  	HashAttribute string
    15  	HashValue     string
    16  	FeatureID     string
    17  }
    18  
    19  // BuildResult creates an Result value from a JSON object represented
    20  // as a Go map.
    21  func BuildResult(dict map[string]interface{}) *Result {
    22  	res := Result{}
    23  	for k, v := range dict {
    24  		switch k {
    25  		case "value":
    26  			res.Value = v
    27  		case "variationId":
    28  			variationID, ok := jsonInt(v, "Result", "variationId")
    29  			if !ok {
    30  				return nil
    31  			}
    32  			res.VariationID = variationID
    33  		case "inExperiment":
    34  			inExperiment, ok := jsonBool(v, "Result", "inExperiment")
    35  			if !ok {
    36  				return nil
    37  			}
    38  			res.InExperiment = inExperiment
    39  		case "hashUsed":
    40  			hashUsed, ok := jsonBool(v, "Result", "hashUsed")
    41  			if !ok {
    42  				return nil
    43  			}
    44  			res.HashUsed = hashUsed
    45  		case "hashAttribute":
    46  			hashAttribute, ok := jsonString(v, "Result", "hashAttribute")
    47  			if !ok {
    48  				return nil
    49  			}
    50  			res.HashAttribute = hashAttribute
    51  		case "hashValue":
    52  			tmp, ok := convertHashValue(v)
    53  			if !ok {
    54  				logError("Invalid JSON data type", "Result", "hashValue")
    55  				return nil
    56  			}
    57  			res.HashValue = tmp
    58  		case "featureId":
    59  			featureID, ok := jsonString(v, "Result", "featureId")
    60  			if !ok {
    61  				return nil
    62  			}
    63  			res.FeatureID = featureID
    64  		case "bucket":
    65  			bucket, ok := jsonMaybeFloat(v, "Result", "bucket")
    66  			if !ok {
    67  				return nil
    68  			}
    69  			res.Bucket = bucket
    70  		case "key":
    71  			key, ok := jsonString(v, "Result", "key")
    72  			if !ok {
    73  				return nil
    74  			}
    75  			res.Key = key
    76  		case "name":
    77  			name, ok := jsonString(v, "Result", "name")
    78  			if !ok {
    79  				return nil
    80  			}
    81  			res.Name = name
    82  		case "passthrough":
    83  			passthrough, ok := jsonBool(v, "Result", "passthrough")
    84  			if !ok {
    85  				return nil
    86  			}
    87  			res.Passthrough = passthrough
    88  		default:
    89  			logWarn("Unknown key in JSON data", "Result", k)
    90  		}
    91  	}
    92  	return &res
    93  }
    94  

View as plain text