-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: simplify connect Resolve... funcs (#391)
## This PR <!-- add the description of the PR here --> - Reduces duplicated code in Resolve... funcs ### Related Issues <!-- add here the GitHub issue that this PR resolves if applicable --> Fixes #385 ### Notes <!-- any additional notes for this PR --> ### Follow-up Tasks <!-- anything that is related to this PR but not done here should be noted under this section --> <!-- if there is a need for a new issue, please link it here --> ### How to test <!-- if applicable, add testing instructions under this section --> Tested against integration tests. --------- Signed-off-by: Skye Gill <[email protected]>
- Loading branch information
Showing
3 changed files
with
146 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package service | ||
|
||
import ( | ||
"fmt" | ||
|
||
schemaV1 "buf.build/gen/go/open-feature/flagd/protocolbuffers/go/schema/v1" | ||
"github.com/bufbuild/connect-go" | ||
"google.golang.org/protobuf/types/known/structpb" | ||
) | ||
|
||
type response[T constraints] interface { | ||
SetResult(value T, variant, reason string) error | ||
} | ||
|
||
type constraints interface { | ||
bool | string | map[string]any | float64 | int64 | ||
} | ||
|
||
type booleanResponse struct { | ||
*connect.Response[schemaV1.ResolveBooleanResponse] | ||
} | ||
|
||
func (r *booleanResponse) SetResult(value bool, variant, reason string) error { | ||
r.Msg.Value = value | ||
r.Msg.Variant = variant | ||
r.Msg.Reason = reason | ||
return nil | ||
} | ||
|
||
type stringResponse struct { | ||
*connect.Response[schemaV1.ResolveStringResponse] | ||
} | ||
|
||
func (r *stringResponse) SetResult(value, variant, reason string) error { | ||
r.Msg.Value = value | ||
r.Msg.Variant = variant | ||
r.Msg.Reason = reason | ||
return nil | ||
} | ||
|
||
type floatResponse struct { | ||
*connect.Response[schemaV1.ResolveFloatResponse] | ||
} | ||
|
||
func (r *floatResponse) SetResult(value float64, variant, reason string) error { | ||
r.Msg.Value = value | ||
r.Msg.Variant = variant | ||
r.Msg.Reason = reason | ||
return nil | ||
} | ||
|
||
type intResponse struct { | ||
*connect.Response[schemaV1.ResolveIntResponse] | ||
} | ||
|
||
func (r *intResponse) SetResult(value int64, variant, reason string) error { | ||
r.Msg.Value = value | ||
r.Msg.Variant = variant | ||
r.Msg.Reason = reason | ||
return nil | ||
} | ||
|
||
type objectResponse struct { | ||
*connect.Response[schemaV1.ResolveObjectResponse] | ||
} | ||
|
||
func (r *objectResponse) SetResult(value map[string]any, variant, reason string) error { | ||
r.Msg.Reason = reason | ||
val, err := structpb.NewStruct(value) | ||
if err != nil { | ||
return fmt.Errorf("struct response construction: %w", err) | ||
} | ||
|
||
r.Msg.Value = val | ||
r.Msg.Variant = variant | ||
return nil | ||
} |