...

Source file src/github.com/launchdarkly/go-server-sdk-evaluation/v2/ldbuilders/flag_builder.go

Documentation: github.com/launchdarkly/go-server-sdk-evaluation/v2/ldbuilders

     1  package ldbuilders
     2  
     3  import (
     4  	"github.com/launchdarkly/go-sdk-common/v3/ldattr"
     5  	"github.com/launchdarkly/go-sdk-common/v3/ldcontext"
     6  	"github.com/launchdarkly/go-sdk-common/v3/ldtime"
     7  	"github.com/launchdarkly/go-sdk-common/v3/ldvalue"
     8  	"github.com/launchdarkly/go-server-sdk-evaluation/v2/ldmodel"
     9  )
    10  
    11  // Bucket constructs a WeightedVariation with the specified variation index and weight.
    12  func Bucket(variationIndex int, weight int) ldmodel.WeightedVariation {
    13  	return ldmodel.WeightedVariation{Variation: variationIndex, Weight: weight}
    14  }
    15  
    16  // BucketUntracked constructs a WeightedVariation with the specified variation index and weight, where users in this
    17  // bucket will not have tracking events sent.
    18  func BucketUntracked(variationIndex int, weight int) ldmodel.WeightedVariation {
    19  	return ldmodel.WeightedVariation{Variation: variationIndex, Weight: weight, Untracked: true}
    20  }
    21  
    22  // Rollout constructs a VariationOrRollout with the specified buckets.
    23  func Rollout(buckets ...ldmodel.WeightedVariation) ldmodel.VariationOrRollout {
    24  	return ldmodel.VariationOrRollout{Rollout: ldmodel.Rollout{Kind: ldmodel.RolloutKindRollout, Variations: buckets}}
    25  }
    26  
    27  // Experiment constructs a VariationOrRollout representing an experiment with the specified buckets.
    28  func Experiment(seed ldvalue.OptionalInt, buckets ...ldmodel.WeightedVariation) ldmodel.VariationOrRollout {
    29  	return ldmodel.VariationOrRollout{
    30  		Rollout: ldmodel.Rollout{
    31  			Kind:       ldmodel.RolloutKindExperiment,
    32  			Variations: buckets,
    33  			Seed:       seed,
    34  		},
    35  	}
    36  }
    37  
    38  // Variation constructs a VariationOrRollout with the specified variation index.
    39  func Variation(variationIndex int) ldmodel.VariationOrRollout {
    40  	return ldmodel.VariationOrRollout{Variation: ldvalue.NewOptionalInt(variationIndex)}
    41  }
    42  
    43  // FlagBuilder provides a builder pattern for FeatureFlag.
    44  type FlagBuilder struct {
    45  	flag ldmodel.FeatureFlag
    46  }
    47  
    48  // RuleBuilder provides a builder pattern for FlagRule.
    49  type RuleBuilder struct {
    50  	rule ldmodel.FlagRule
    51  }
    52  
    53  // NewFlagBuilder creates a FlagBuilder.
    54  func NewFlagBuilder(key string) *FlagBuilder {
    55  	return &FlagBuilder{flag: ldmodel.FeatureFlag{
    56  		Key:                    key,
    57  		ClientSideAvailability: ldmodel.ClientSideAvailability{UsingMobileKey: true},
    58  	}}
    59  }
    60  
    61  // Build returns the configured FeatureFlag.
    62  func (b *FlagBuilder) Build() ldmodel.FeatureFlag {
    63  	f := b.flag
    64  	ldmodel.PreprocessFlag(&f)
    65  	return f
    66  }
    67  
    68  // AddPrerequisite adds a flag prerequisite.
    69  func (b *FlagBuilder) AddPrerequisite(key string, variationIndex int) *FlagBuilder {
    70  	b.flag.Prerequisites = append(b.flag.Prerequisites, ldmodel.Prerequisite{Key: key, Variation: variationIndex})
    71  	return b
    72  }
    73  
    74  // AddRule adds a flag rule.
    75  func (b *FlagBuilder) AddRule(r *RuleBuilder) *FlagBuilder {
    76  	b.flag.Rules = append(b.flag.Rules, r.Build())
    77  	return b
    78  }
    79  
    80  // AddTarget adds a user target set.
    81  func (b *FlagBuilder) AddTarget(variationIndex int, keys ...string) *FlagBuilder {
    82  	b.flag.Targets = append(b.flag.Targets, ldmodel.Target{Values: keys, Variation: variationIndex})
    83  	return b
    84  }
    85  
    86  // AddContextTarget adds a target set for any context kind.
    87  func (b *FlagBuilder) AddContextTarget(kind ldcontext.Kind, variationIndex int, keys ...string) *FlagBuilder {
    88  	b.flag.ContextTargets = append(b.flag.ContextTargets,
    89  		ldmodel.Target{ContextKind: kind, Values: keys, Variation: variationIndex})
    90  	return b
    91  }
    92  
    93  // ClientSideUsingEnvironmentID sets the flag's ClientSideAvailability.UsingEnvironmentID property.
    94  // By default, this is false. Setting this property also forces the flag to use the newer serialization
    95  // schema so both UsingEnvironmentID and UsingMobileKey will be explicitly specified.
    96  func (b *FlagBuilder) ClientSideUsingEnvironmentID(value bool) *FlagBuilder {
    97  	b.flag.ClientSideAvailability.UsingEnvironmentID = value
    98  	b.flag.ClientSideAvailability.Explicit = true
    99  	return b
   100  }
   101  
   102  // ClientSideUsingMobileKey sets the flag's ClientSideAvailability.UsingMobileKey property. By default,
   103  // this is true. Setting this property also forces the flag to use the newer serialization schema so
   104  // both UsingEnvironmentID and UsingMobileKey will be explicitly specified.
   105  func (b *FlagBuilder) ClientSideUsingMobileKey(value bool) *FlagBuilder {
   106  	b.flag.ClientSideAvailability.UsingMobileKey = value
   107  	b.flag.ClientSideAvailability.Explicit = true
   108  	return b
   109  }
   110  
   111  // DebugEventsUntilDate sets the flag's DebugEventsUntilDate property.
   112  func (b *FlagBuilder) DebugEventsUntilDate(t ldtime.UnixMillisecondTime) *FlagBuilder {
   113  	b.flag.DebugEventsUntilDate = t
   114  	return b
   115  }
   116  
   117  // Deleted sets the flag's Deleted property.
   118  func (b *FlagBuilder) Deleted(value bool) *FlagBuilder {
   119  	b.flag.Deleted = value
   120  	return b
   121  }
   122  
   123  // Fallthrough sets the flag's Fallthrough property.
   124  func (b *FlagBuilder) Fallthrough(vr ldmodel.VariationOrRollout) *FlagBuilder {
   125  	b.flag.Fallthrough = vr
   126  	return b
   127  }
   128  
   129  // FallthroughVariation sets the flag's Fallthrough property to a fixed variation.
   130  func (b *FlagBuilder) FallthroughVariation(variationIndex int) *FlagBuilder {
   131  	return b.Fallthrough(Variation(variationIndex))
   132  }
   133  
   134  // OffVariation sets the flag's OffVariation property.
   135  func (b *FlagBuilder) OffVariation(variationIndex int) *FlagBuilder {
   136  	b.flag.OffVariation = ldvalue.NewOptionalInt(variationIndex)
   137  	return b
   138  }
   139  
   140  // On sets the flag's On property.
   141  func (b *FlagBuilder) On(value bool) *FlagBuilder {
   142  	b.flag.On = value
   143  	return b
   144  }
   145  
   146  // Salt sets the flag's Salt property.
   147  func (b *FlagBuilder) Salt(value string) *FlagBuilder {
   148  	b.flag.Salt = value
   149  	return b
   150  }
   151  
   152  // SingleVariation configures the flag to have only one variation value which it always returns.
   153  func (b *FlagBuilder) SingleVariation(value ldvalue.Value) *FlagBuilder {
   154  	return b.Variations(value).OffVariation(0).On(false)
   155  }
   156  
   157  // TrackEvents sets the flag's TrackEvents property.
   158  func (b *FlagBuilder) TrackEvents(value bool) *FlagBuilder {
   159  	b.flag.TrackEvents = value
   160  	return b
   161  }
   162  
   163  // TrackEventsFallthrough sets the flag's TrackEventsFallthrough property.
   164  func (b *FlagBuilder) TrackEventsFallthrough(value bool) *FlagBuilder {
   165  	b.flag.TrackEventsFallthrough = value
   166  	return b
   167  }
   168  
   169  // Variations sets the flag's list of variation values.
   170  func (b *FlagBuilder) Variations(values ...ldvalue.Value) *FlagBuilder {
   171  	b.flag.Variations = values
   172  	return b
   173  }
   174  
   175  // Version sets the flag's Version property.
   176  func (b *FlagBuilder) Version(value int) *FlagBuilder {
   177  	b.flag.Version = value
   178  	return b
   179  }
   180  
   181  // NewRuleBuilder creates a RuleBuilder.
   182  func NewRuleBuilder() *RuleBuilder {
   183  	return &RuleBuilder{}
   184  }
   185  
   186  // Build returns the configured FlagRule.
   187  func (b *RuleBuilder) Build() ldmodel.FlagRule {
   188  	return b.rule
   189  }
   190  
   191  // Clauses sets the rule's list of clauses.
   192  func (b *RuleBuilder) Clauses(clauses ...ldmodel.Clause) *RuleBuilder {
   193  	b.rule.Clauses = clauses
   194  	return b
   195  }
   196  
   197  // ID sets the rule's ID property.
   198  func (b *RuleBuilder) ID(id string) *RuleBuilder {
   199  	b.rule.ID = id
   200  	return b
   201  }
   202  
   203  // TrackEvents sets the rule's TrackEvents property.
   204  func (b *RuleBuilder) TrackEvents(value bool) *RuleBuilder {
   205  	b.rule.TrackEvents = value
   206  	return b
   207  }
   208  
   209  // Variation sets the rule to use a fixed variation.
   210  func (b *RuleBuilder) Variation(variationIndex int) *RuleBuilder {
   211  	return b.VariationOrRollout(Variation(variationIndex))
   212  }
   213  
   214  // VariationOrRollout sets the rule to use either a variation or a percentage rollout.
   215  func (b *RuleBuilder) VariationOrRollout(vr ldmodel.VariationOrRollout) *RuleBuilder {
   216  	b.rule.VariationOrRollout = vr
   217  	return b
   218  }
   219  
   220  // Clause constructs a basic Clause. The attr parameter is assumed to be a simple attribute name
   221  // rather than a path reference.
   222  func Clause(attr string, op ldmodel.Operator, values ...ldvalue.Value) ldmodel.Clause {
   223  	return ldmodel.Clause{Attribute: ldattr.NewLiteralRef(attr), Op: op, Values: values}
   224  }
   225  
   226  // ClauseWithKind is like Clause, but also specifies a context kind.
   227  func ClauseWithKind(
   228  	contextKind ldcontext.Kind,
   229  	attr string,
   230  	op ldmodel.Operator,
   231  	values ...ldvalue.Value,
   232  ) ldmodel.Clause {
   233  	return ldmodel.Clause{
   234  		ContextKind: contextKind,
   235  		Attribute:   ldattr.NewLiteralRef(attr),
   236  		Op:          op,
   237  		Values:      values,
   238  	}
   239  }
   240  
   241  // ClauseRef constructs a basic Clause, using the ldattr.Ref type for the attribute reference.
   242  func ClauseRef(attrRef ldattr.Ref, op ldmodel.Operator, values ...ldvalue.Value) ldmodel.Clause {
   243  	return ldmodel.Clause{Attribute: attrRef, Op: op, Values: values}
   244  }
   245  
   246  // ClauseRefWithKind is like ClauseRef, but also specifies a context kind.
   247  func ClauseRefWithKind(
   248  	contextKind ldcontext.Kind,
   249  	attrRef ldattr.Ref,
   250  	op ldmodel.Operator,
   251  	values ...ldvalue.Value,
   252  ) ldmodel.Clause {
   253  	return ldmodel.Clause{ContextKind: contextKind, Attribute: attrRef, Op: op, Values: values}
   254  }
   255  
   256  // Negate returns the same Clause with the Negated property set to true.
   257  func Negate(c ldmodel.Clause) ldmodel.Clause {
   258  	c.Negate = true
   259  	return c
   260  }
   261  
   262  // SegmentMatchClause constructs a Clause that uses the segmentMatch operator.
   263  func SegmentMatchClause(segmentKeys ...string) ldmodel.Clause {
   264  	clause := ldmodel.Clause{Op: ldmodel.OperatorSegmentMatch}
   265  	for _, key := range segmentKeys {
   266  		clause.Values = append(clause.Values, ldvalue.String(key))
   267  	}
   268  	return clause
   269  }
   270  

View as plain text