Skip to content

Commit

Permalink
Merge pull request #259 from saary/master
Browse files Browse the repository at this point in the history
Add support for arbitrary JSON in attachments
  • Loading branch information
zhouzhuojie authored May 30, 2019
2 parents 1293e27 + a86d8c7 commit 5b923b8
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 24 deletions.
2 changes: 1 addition & 1 deletion pkg/entity/fixture.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func GenFixtureFlag() Flag {
Model: gorm.Model{ID: 301},
FlagID: 100,
Key: "treatment",
Attachment: map[string]string{
Attachment: map[string]interface{}{
"value": "321",
},
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/entity/variant.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (v *Variant) Validate() error {
}

// Attachment supports dynamic configuration in variant
type Attachment map[string]string
type Attachment map[string]interface{}

// Scan implements scanner interface
func (a *Attachment) Scan(value interface{}) error {
Expand Down
41 changes: 27 additions & 14 deletions pkg/handler/crud_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,33 @@ func TestCrudVariants(t *testing.T) {
})
assert.Equal(t, *res.(*variant.PutVariantOK).Payload.Key, "another_control")

res = c.PutVariant(variant.PutVariantParams{
FlagID: int64(1),
VariantID: int64(1),
Body: &models.PutVariantRequest{
Key: util.StringPtr("another_control"),
Attachment: map[string]interface{}{
"valid_int_value": 1,
},
},
})
assert.Equal(t, *res.(*variant.PutVariantOK).Payload.Key, "another_control")

res = c.PutVariant(variant.PutVariantParams{
FlagID: int64(1),
VariantID: int64(1),
Body: &models.PutVariantRequest{
Key: util.StringPtr("another_control"),
Attachment: map[string]interface{}{
"valid_structured_value": map[string]interface{}{
"string_value": "string",
"int_value": 1,
},
},
},
})
assert.Equal(t, *res.(*variant.PutVariantOK).Payload.Key, "another_control")

// step 4. it should be able to delete the variant
res = c.DeleteVariant(variant.DeleteVariantParams{
FlagID: int64(1),
Expand Down Expand Up @@ -849,20 +876,6 @@ func TestCrudVariantsWithFailures(t *testing.T) {
assert.NotZero(t, *res.(*variant.PutVariantDefault).Payload)
})

t.Run("PutVariant - put invalid attachment", func(t *testing.T) {
res = c.PutVariant(variant.PutVariantParams{
FlagID: int64(1),
VariantID: int64(1),
Body: &models.PutVariantRequest{
Key: util.StringPtr("another_control"),
Attachment: map[string]interface{}{
"invalid_int_value": 1,
},
},
})
assert.NotZero(t, *res.(*variant.PutVariantDefault).Payload)
})

t.Run("PutVariant - put validation error", func(t *testing.T) {
res = c.PutVariant(variant.PutVariantParams{
FlagID: int64(1),
Expand Down
10 changes: 2 additions & 8 deletions pkg/mapper/entity_restapi/r2e/r2e.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,9 @@ func MapAttachment(a interface{}) (entity.Attachment, error) {
if a != nil {
m, ok := a.(map[string]interface{})
if !ok {
return e, fmt.Errorf("all key/value pairs should be string/string. invalid attachment format %s", spew.Sdump(a))
}
for k, v := range m {
s, ok := v.(string)
if !ok {
return e, fmt.Errorf("all key/value pairs should be string/string. invalid attachment format %s", spew.Sdump(a))
}
e[k] = s
return e, fmt.Errorf("Make sure JSON is properly formatted into key/value pairs. Invalid attachment format %s", spew.Sdump(a))
}
e = m
}
return e, nil
}

0 comments on commit 5b923b8

Please sign in to comment.