...

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

Documentation: github.com/growthbook/growthbook-golang

     1  package growthbook
     2  
     3  import "encoding/json"
     4  
     5  // FeatureValue is a wrapper around an arbitrary type representing the
     6  // value of a feature. Features can return any kinds of values, so
     7  // this is an alias for interface{}.
     8  type FeatureValue interface{}
     9  
    10  // Feature has a default value plus rules than can override the
    11  // default.
    12  type Feature struct {
    13  	DefaultValue FeatureValue   `json:"defaultValue"`
    14  	Rules        []*FeatureRule `json:"rules"`
    15  }
    16  
    17  // ParseFeature creates a single Feature value from raw JSON input.
    18  func ParseFeature(data []byte) *Feature {
    19  	dict := make(map[string]interface{})
    20  	err := json.Unmarshal(data, &dict)
    21  	if err != nil {
    22  		logError("Failed parsing JSON input", "Feature")
    23  		return nil
    24  	}
    25  	return BuildFeature(dict)
    26  }
    27  
    28  // BuildFeature creates a Feature value from a generic JSON value.
    29  func BuildFeature(val interface{}) *Feature {
    30  	feature := Feature{}
    31  	dict, ok := val.(map[string]interface{})
    32  	if !ok {
    33  		logError("Invalid JSON data type", "Feature")
    34  		return nil
    35  	}
    36  	defaultValue, ok := dict["defaultValue"]
    37  	if ok {
    38  		feature.DefaultValue = defaultValue
    39  	}
    40  	rules, ok := dict["rules"]
    41  	if ok {
    42  		var rulesArray []interface{}
    43  		if rules == nil {
    44  			rulesArray = []interface{}{}
    45  		} else {
    46  			rulesArray, ok = rules.([]interface{})
    47  			if !ok {
    48  				logError("Invalid JSON data type", "Feature")
    49  				return nil
    50  			}
    51  		}
    52  		feature.Rules = make([]*FeatureRule, len(rulesArray))
    53  		for i := range rulesArray {
    54  			rule := BuildFeatureRule(rulesArray[i])
    55  			if rule == nil {
    56  				return nil
    57  			}
    58  			feature.Rules[i] = rule
    59  		}
    60  	}
    61  	return &feature
    62  }
    63  
    64  // BuildFeatureValues creates a FeatureValue array from a generic JSON
    65  // value.
    66  func BuildFeatureValues(val interface{}) []FeatureValue {
    67  	vals, ok := val.([]interface{})
    68  	if !ok {
    69  		logError("Invalid JSON data type", "FeatureValue")
    70  		return nil
    71  	}
    72  	result := make([]FeatureValue, len(vals))
    73  	for i, v := range vals {
    74  		// FeatureValue is just an alias for interface{}.
    75  		result[i] = v
    76  	}
    77  	return result
    78  }
    79  
    80  // BuildFeatures creates a Feature array from a generic JSON value.
    81  func BuildFeatures(v interface{}) map[string]*Feature {
    82  	dict, ok := v.(map[string]interface{})
    83  	if !ok {
    84  		logError("Invalid JSON data type", "Features")
    85  		return nil
    86  	}
    87  	result := make(map[string]*Feature, len(dict))
    88  	for k, v := range dict {
    89  		feature := BuildFeature(v)
    90  		if feature != nil {
    91  			result[k] = feature
    92  		}
    93  	}
    94  	return result
    95  }
    96  

View as plain text