Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions internal/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,7 @@ func mvOptionsPath(projectID, featureID int) string {

// CreateMVOption adds a multivariate option (variant) to a feature.
func (c *Client) CreateMVOption(ctx context.Context, projectID, featureID int, in MultivariateOption) (*MultivariateOption, error) {
in.Feature = featureID // the mv-options serializer requires it in the body
return send[MultivariateOption](ctx, c, http.MethodPost, mvOptionsPath(projectID, featureID), in)
}

Expand Down
62 changes: 57 additions & 5 deletions internal/cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6363,6 +6363,30 @@ func TestFeatureCreate(t *testing.T) {
if o1["type"] != "int" || o1["integer_value"] != float64(42) || o1["default_percentage_allocation"] != float64(70) {
t.Errorf("variant 1 = %+v", o1)
}
if _, ok := o0["key"]; ok {
t.Errorf("variant 0 = %+v, want no key when none is given", o0)
}
})

t.Run("multivariate inline variants keep their keys", func(t *testing.T) {
f := flagUpdateEnv(t)
withFeatures(f)
variants := `[{"key":"annual_first","value":"annual_first","weight":50}]`
if _, err := run("", "feature", "create", "pricing-layout", "--value", "monthly_first", "--variants", variants); err != nil {
t.Fatalf("feature create --variants: %v", err)
}
// The API drops `key` on nested variants, so keyed ones go to mv-options.
// The fake keeps the create body as the stored feature and appends each
// mv-options POST to it, so exactly one stored option, carrying its key,
// means the create sent none nested and mv-options got it once.
opts, _ := f.lastFeatureBody["multivariate_options"].([]any)
if len(opts) != 1 || opts[0].(map[string]any)["key"] != "annual_first" {
t.Errorf("stored options = %+v, want only the keyed variant, added once through mv-options", opts)
}
o := f.lastMVBody
if o["key"] != "annual_first" || o["string_value"] != "annual_first" || o["default_percentage_allocation"] != float64(50) {
t.Errorf("mv body = %+v, want the keyed variant posted to mv-options", o)
}
})
}

Expand Down Expand Up @@ -6503,12 +6527,40 @@ func TestFeatureVariant(t *testing.T) {
if _, err := run("", "feature", "variant", "update", "banner-copy", "hero", "--weight", "40"); err != nil {
t.Fatalf("variant update: %v", err)
}
if f.lastMVBody["default_percentage_allocation"] != float64(40) {
t.Errorf("mv body = %+v", f.lastMVBody)
// The API rejects a partial PATCH of a multivariate option, so the whole
// option goes out: the new weight plus everything it already had.
b := f.lastMVBody
if b["default_percentage_allocation"] != float64(40) || b["feature"] != float64(91) ||
b["type"] != "unicode" || b["string_value"] != "headline" || b["key"] != "hero" {
t.Errorf("mv body = %+v, want the full option with the new weight", b)
}
})

t.Run("update only the key keeps the value and weight", func(t *testing.T) {
f := flagUpdateEnv(t)
withFeatures(f)
if _, err := run("", "feature", "variant", "update", "banner-copy", "201", "--key", "headline_a"); err != nil {
t.Fatalf("variant update: %v", err)
}
b := f.lastMVBody
if b["key"] != "headline_a" || b["string_value"] != "headline" ||
b["default_percentage_allocation"] != float64(30) || b["feature"] != float64(91) {
t.Errorf("mv body = %+v, want the full option with the new key", b)
}
})

t.Run("update the value replaces the old value fields", func(t *testing.T) {
f := flagUpdateEnv(t)
withFeatures(f)
if _, err := run("", "feature", "variant", "update", "banner-copy", "hero", "--value", "7"); err != nil {
t.Fatalf("variant update: %v", err)
}
b := f.lastMVBody
if b["type"] != "int" || b["integer_value"] != float64(7) || b["key"] != "hero" {
t.Errorf("mv body = %+v", b)
}
// value untouched (only weight sent)
if _, ok := f.lastMVBody["string_value"]; ok {
t.Errorf("mv body = %+v, want only the weight sent", f.lastMVBody)
if _, ok := b["string_value"]; ok {
t.Errorf("mv body = %+v, want the old string value dropped", b)
}
})

Expand Down
42 changes: 37 additions & 5 deletions internal/cmd/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,16 +224,42 @@ var featureCreateCmd = &cobra.Command{
if cmd.Flags().Changed("enabled") {
in.DefaultEnabled = &featureEnabledFlag
}
// The API ignores `key` on variants nested in a feature create; it is
// only writable through the mv-options endpoint. Keyed variants are
// added there once the feature exists.
var keyed []api.MultivariateOption
if cmd.Flags().Changed("variants") {
if in.MultivariateOptions, err = variantsForWrite(cmd, featureVariantsFlag); err != nil {
opts, err := variantsForWrite(cmd, featureVariantsFlag)
if err != nil {
return err
}
for _, o := range opts {
if o.Key != "" {
keyed = append(keyed, o)
} else {
in.MultivariateOptions = append(in.MultivariateOptions, o)
}
}
}
feat, err := cred.client().CreateFeature(cmd.Context(), projectID, in)
client := cred.client()
feat, err := client.CreateFeature(cmd.Context(), projectID, in)
if err != nil {
return err
}
output.Success(cmd.ErrOrStderr(), "Created feature %s", label(feat.Name, feat.ID))
if len(keyed) == 0 {
return renderFeature(cmd, feat)
}
for _, o := range keyed {
if _, err := client.CreateMVOption(cmd.Context(), projectID, feat.ID, o); err != nil {
return hintf(
fmt.Errorf("feature %s was created, but adding variant %q failed: %w", feat.Name, o.Key, err),
"Add the remaining variants with `flagsmith feature variant add %s --key <key> --value <value> --weight <weight>`.", feat.Name)
}
}
if feat, err = client.GetFeature(cmd.Context(), projectID, feat.ID); err != nil {
return err
}
return renderFeature(cmd, feat)
},
}
Expand Down Expand Up @@ -319,6 +345,7 @@ var featureDeleteCmd = &cobra.Command{
type variantInput struct {
Value any `json:"value"`
Weight float64 `json:"weight"`
Key string `json:"key"`
}

// mvOptionFromJSON types a variant from its JSON value: bool → boolean,
Expand Down Expand Up @@ -352,6 +379,7 @@ func variantsForWrite(cmd *cobra.Command, arg string) ([]api.MultivariateOption,
opts := make([]api.MultivariateOption, len(inputs))
for i, in := range inputs {
opts[i] = mvOptionFromJSON(in.Value, in.Weight)
opts[i].Key = in.Key
}
return opts, nil
}
Expand Down Expand Up @@ -507,11 +535,15 @@ var featureVariantUpdateCmd = &cobra.Command{
fmt.Errorf("variant %q not found on %s", args[1], args[0]),
"Run `flagsmith feature variant list %s` to see its variants.", args[0])
}
o := api.MultivariateOption{}
// Send the whole option, not just the changed fields: the API's
// serializer rejects a partial PATCH of a multivariate option (500).
o := *variant
if cmd.Flags().Changed("value") {
if o, err = mvOptionFromFlag(featureVariantValueFlag, featureVariantTypeFlag); err != nil {
v, err := mvOptionFromFlag(featureVariantValueFlag, featureVariantTypeFlag)
if err != nil {
return err
}
o.Type, o.StringValue, o.IntegerValue, o.BooleanValue = v.Type, v.StringValue, v.IntegerValue, v.BooleanValue
}
if cmd.Flags().Changed("weight") {
w := featureVariantWeightFlag
Expand Down Expand Up @@ -571,7 +603,7 @@ func init() {
_ = featureCreateCmd.Flags().MarkHidden("default-value")
featureCreateCmd.Flags().BoolVar(&featureEnabledFlag, "enabled", false, "enable the feature by default")
featureCreateCmd.Flags().StringVar(&featureDescriptionFlag, "description", "", "feature description")
featureCreateCmd.Flags().StringVar(&featureVariantsFlag, "variants", "", "multivariate variants: @file, -, or inline JSON")
featureCreateCmd.Flags().StringVar(&featureVariantsFlag, "variants", "", "multivariate variants as a JSON array of {value, weight, key}: @file, -, or inline JSON")
featureUpdateCmd.Flags().StringVar(&featureDescriptionFlag, "description", "", "feature description")
featureUpdateCmd.Flags().BoolVar(&featureArchiveFlag, "archive", false, "archive the feature")
featureUpdateCmd.Flags().BoolVar(&featureUnarchiveFlag, "unarchive", false, "unarchive the feature")
Expand Down