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

fix: add validation to 'prepare' methods in requestor #200

Merged
merged 1 commit into from
Feb 27, 2023
Merged
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
7 changes: 6 additions & 1 deletion momento/list_push_back.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,19 @@ func (r *ListPushBackRequest) initGrpcRequest(client scsDataClient) error {
return err
}

var value []byte
if value, err = prepareValue(r); err != nil {
return err
}

var ttl uint64
if ttl, err = prepareTTL(r, client.defaultTtl); err != nil {
return err
}

r.grpcRequest = &pb.XListPushBackRequest{
ListName: []byte(r.ListName),
Value: r.Value.asBytes(),
Value: value,
TtlMilliseconds: ttl,
RefreshTtl: r.CollectionTTL.RefreshTtl,
TruncateFrontToSize: r.TruncateFrontToSize,
Expand Down
31 changes: 28 additions & 3 deletions momento/requester.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,34 @@ func prepareFields(r hasFields) ([][]byte, error) {
}

func prepareValue(r hasValue) ([]byte, momentoerrors.MomentoSvcErr) {
if r.value() == nil {
return []byte{}, momentoerrors.NewMomentoSvcErr(
momentoerrors.InvalidArgumentError,
"value may not be nil",
nil,
)
}
return r.value().asBytes(), nil
}

func prepareValues(r hasValues) ([][]byte, momentoerrors.MomentoSvcErr) {
return momentoValuesToPrimitiveByteList(r.values()), nil
values, err := momentoValuesToPrimitiveByteList(r.values())
if err != nil {
return [][]byte{}, err
}
return values, nil
}

func prepareItems(r hasItems) (map[string][]byte, error) {
retMap := make(map[string][]byte)
for k, v := range r.items() {
if v == nil {
return map[string][]byte{}, momentoerrors.NewMomentoSvcErr(
momentoerrors.InvalidArgumentError,
"item values may not be nil",
nil,
)
}
if err := validateNotEmpty([]byte(k), "item keys"); err != nil {
return nil, err
}
Expand All @@ -146,12 +164,19 @@ func prepareTTL(r hasTTL, defaultTtl time.Duration) (uint64, error) {
return uint64(ttl.Milliseconds()), nil
}

func momentoValuesToPrimitiveByteList(i []Value) [][]byte {
func momentoValuesToPrimitiveByteList(i []Value) ([][]byte, momentoerrors.MomentoSvcErr) {
var rList [][]byte
for _, mb := range i {
if mb == nil {
return [][]byte{}, momentoerrors.NewMomentoSvcErr(
momentoerrors.InvalidArgumentError,
"values may not be nil",
nil,
)
}
rList = append(rList, mb.asBytes())
}
return rList
return rList, nil
}

func validateNotEmpty(field []byte, label string) error {
Expand Down
7 changes: 6 additions & 1 deletion momento/set_add_elements.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,14 @@ func (r *SetAddElementsRequest) initGrpcRequest(client scsDataClient) error {
return err
}

elements, err := momentoValuesToPrimitiveByteList(r.Elements)
if err != nil {
return err
}

r.grpcRequest = &pb.XSetUnionRequest{
SetName: []byte(r.SetName),
Elements: momentoValuesToPrimitiveByteList(r.Elements),
Elements: elements,
TtlMilliseconds: ttl,
RefreshTtl: r.CollectionTTL.RefreshTtl,
}
Expand Down
7 changes: 6 additions & 1 deletion momento/set_remove_elements.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,18 @@ func (r *SetRemoveElementsRequest) initGrpcRequest(client scsDataClient) error {
return err
}

elements, err := momentoValuesToPrimitiveByteList(r.Elements)
if err != nil {
return err
}

r.grpcRequest = &pb.XSetDifferenceRequest{
SetName: []byte(r.SetName),
Difference: &pb.XSetDifferenceRequest_Subtrahend{
Subtrahend: &pb.XSetDifferenceRequest_XSubtrahend{
SubtrahendSet: &pb.XSetDifferenceRequest_XSubtrahend_Set{
Set: &pb.XSetDifferenceRequest_XSubtrahend_XSet{
Elements: momentoValuesToPrimitiveByteList(r.Elements),
Elements: elements,
},
},
},
Expand Down
7 changes: 6 additions & 1 deletion momento/sorted_set_get_score.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,14 @@ func (r *SortedSetGetScoreRequest) initGrpcRequest(scsDataClient) error {
return err
}

elementNames, err := momentoValuesToPrimitiveByteList(r.ElementNames)
if err != nil {
return err
}

resp := &pb.XSortedSetGetScoreRequest{
SetName: []byte(r.SetName),
ElementName: momentoValuesToPrimitiveByteList(r.ElementNames),
ElementName: elementNames,
}

r.grpcRequest = resp
Expand Down
12 changes: 10 additions & 2 deletions momento/sorted_set_remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,23 @@ func (r *SortedSetRemoveRequest) initGrpcRequest(scsDataClient) error {
case *RemoveAllElements:
grpcReq.RemoveElements = &pb.XSortedSetRemoveRequest_All{}
case RemoveSomeElements:
elemToRemove, err := momentoValuesToPrimitiveByteList(toRemove.Elements)
if err != nil {
return err
}
grpcReq.RemoveElements = &pb.XSortedSetRemoveRequest_Some{
Some: &pb.XSortedSetRemoveRequest_XSome{
ElementName: momentoValuesToPrimitiveByteList(toRemove.Elements),
ElementName: elemToRemove,
},
}
case *RemoveSomeElements:
elemToRemove, err := momentoValuesToPrimitiveByteList(toRemove.Elements)
if err != nil {
return err
}
grpcReq.RemoveElements = &pb.XSortedSetRemoveRequest_Some{
Some: &pb.XSortedSetRemoveRequest_XSome{
ElementName: momentoValuesToPrimitiveByteList(toRemove.Elements),
ElementName: elemToRemove,
},
}
default:
Expand Down