feat: add mandatory flags property in bulk response (#1339)

This PR improves flagd bulk evaluation empty response by adding required
`flags` property.

Related - https://github.com/open-feature/protocol/pull/27

Signed-off-by: Kavindu Dodanduwa <kavindudodanduwa@gmail.com>
This commit is contained in:
Kavindu Dodanduwa 2024-06-27 06:34:22 -07:00 committed by GitHub
parent 83bdbb5e7e
commit b20266ed5e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 43 additions and 19 deletions

View File

@ -20,7 +20,7 @@ type EvaluationSuccess struct {
}
type BulkEvaluationResponse struct {
Flags []interface{} `json:"flags,omitempty"`
Flags []interface{} `json:"flags"`
}
type EvaluationError struct {

View File

@ -1,6 +1,7 @@
package ofrep
import (
"encoding/json"
"errors"
"reflect"
"testing"
@ -45,31 +46,54 @@ func TestSuccessResult(t *testing.T) {
}
}
func TestBulkEvaluationResult(t *testing.T) {
// given
values := []evaluator.AnyValue{
func TestBulkEvaluationResponse(t *testing.T) {
tests := []struct {
name string
input []evaluator.AnyValue
marshalledOutput string
}{
{
Value: false,
Variant: "false",
Reason: model.StaticReason,
FlagKey: "key",
Metadata: map[string]interface{}{
"key": "value",
},
name: "empty input",
input: nil,
marshalledOutput: "{\"flags\":[]}",
},
{
Value: false,
Variant: "false",
Reason: model.ErrorReason,
FlagKey: "errorFlag",
Error: errors.New(model.FlagNotFoundErrorCode),
name: "valid values",
input: []evaluator.AnyValue{
{
Value: false,
Variant: "false",
Reason: model.StaticReason,
FlagKey: "key",
Metadata: map[string]interface{}{
"key": "value",
},
},
{
Value: false,
Variant: "false",
Reason: model.ErrorReason,
FlagKey: "errorFlag",
Error: errors.New(model.FlagNotFoundErrorCode),
},
},
marshalledOutput: "{\"flags\":[{\"value\":false,\"key\":\"key\",\"reason\":\"STATIC\",\"variant\":\"false\",\"metadata\":{\"key\":\"value\"}},{\"key\":\"errorFlag\",\"errorCode\":\"FLAG_NOT_FOUND\",\"errorDetails\":\"flag `errorFlag` does not exist\"}]}",
},
}
bulkResponse := BulkEvaluationResponseFrom(values)
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
response := BulkEvaluationResponseFrom(test.input)
if len(bulkResponse.Flags) != 2 {
t.Errorf("expected bulk response to contain 2 results, but got %d", len(bulkResponse.Flags))
marshal, err := json.Marshal(response)
if err != nil {
t.Errorf("error marshalling the response: %v", err)
}
if test.marshalledOutput != string(marshal) {
t.Errorf("expected %s, got %s", test.marshalledOutput, string(marshal))
}
})
}
}