Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flag variant attachment #685

Merged
merged 13 commits into from
Feb 10, 2022
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ _obj
_test
.DS_Store
.envrc
.idea
.ruby-version
.vagrant
.vscode/*
Expand Down
2 changes: 2 additions & 0 deletions cmd/flipt/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type Variant struct {
Key string `yaml:"key,omitempty"`
Name string `yaml:"name,omitempty"`
Description string `yaml:"description,omitempty"`
Attachment string `yaml:"attachment,omitempty"`
}

type Rule struct {
Expand Down Expand Up @@ -149,6 +150,7 @@ func runExport(_ []string) error {
Key: v.Key,
Name: v.Name,
Description: v.Description,
Attachment: v.Attachment,
})

variantKeys[v.Id] = v.Key
Expand Down
1 change: 1 addition & 0 deletions cmd/flipt/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ func runImport(args []string) error {
Key: v.Key,
Name: v.Name,
Description: v.Description,
Attachment: v.Attachment,
})

if err != nil {
Expand Down
1 change: 1 addition & 0 deletions config/migrations/mysql/1_variants_attachment.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE variants DROP COLUMN attachment;
1 change: 1 addition & 0 deletions config/migrations/mysql/1_variants_attachment.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE variants ADD COLUMN attachment JSON AFTER description;
1 change: 1 addition & 0 deletions config/migrations/postgres/3_variants_attachment.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE variants DROP COLUMN attachment;
1 change: 1 addition & 0 deletions config/migrations/postgres/3_variants_attachment.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE variants ADD attachment JSONB;
1 change: 1 addition & 0 deletions config/migrations/sqlite3/3_variants_attachment.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE variants DROP COLUMN attachment;
1 change: 1 addition & 0 deletions config/migrations/sqlite3/3_variants_attachment.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE variants ADD COLUMN attachment TEXT AFTER description;
54 changes: 47 additions & 7 deletions rpc/flipt/flipt.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions rpc/flipt/flipt.proto
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ message EvaluationResponse {
google.protobuf.Timestamp timestamp = 7;
string value = 8;
double request_duration_millis = 9;
string attachment = 10;
}

message BatchEvaluationResponse {
Expand Down Expand Up @@ -143,6 +144,7 @@ message Variant {
string description = 5;
google.protobuf.Timestamp created_at = 6;
google.protobuf.Timestamp updated_at = 7;
string attachment = 8;
}

message CreateVariantRequest {
Expand All @@ -156,6 +158,7 @@ message CreateVariantRequest {
string key = 2;
string name = 3;
string description = 4;
string attachment = 5;
}

message UpdateVariantRequest {
Expand All @@ -170,6 +173,7 @@ message UpdateVariantRequest {
string key = 3;
string name = 4;
string description = 5;
string attachment = 6;
}

message DeleteVariantRequest {
Expand Down
30 changes: 30 additions & 0 deletions rpc/flipt/validation.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,41 @@
package flipt

import (
"encoding/json"
"fmt"
"regexp"
"strings"

"github.com/markphelps/flipt/errors"
)

const MAX_VARIANT_ATTACHMENT_SIZE = 10000

// Validator validates types
type Validator interface {
Validate() error
}

// Evaluate

func validateAttachment(attachment string) error {
if attachment == "" {
return nil
}

bytes := []byte(attachment)
if !json.Valid(bytes) {
return errors.InvalidFieldError("attachment", "must be a json string")
}

if len(bytes) > MAX_VARIANT_ATTACHMENT_SIZE {
return errors.InvalidFieldError("attachment",
fmt.Sprintf("must be less than %d KB", MAX_VARIANT_ATTACHMENT_SIZE),
)
}
return nil
}

func (req *EvaluationRequest) Validate() error {
if req.FlagKey == "" {
return errors.EmptyFieldError("flagKey")
Expand Down Expand Up @@ -83,6 +105,10 @@ func (req *CreateVariantRequest) Validate() error {
return errors.EmptyFieldError("key")
}

if err := validateAttachment(req.Attachment); err != nil {
return err
}

return nil
}

Expand All @@ -99,6 +125,10 @@ func (req *UpdateVariantRequest) Validate() error {
return errors.EmptyFieldError("key")
}

if err := validateAttachment(req.Attachment); err != nil {
return err
}

return nil
}

Expand Down
Loading