Files
Alex ShtinandGitHub 846d88c8cb Refactor: use new serviceerror.New...f constructors (#7785)
## What changed?
Refactor: use new `serviceerror.New...f` constructors.

## Why?
Most of service errors now supports `fmt.Sprintf` format and they should
be used instead of `fmt.Sprintf`.

## How did you test it?
- [ ] built
- [ ] run locally and tested manually
- [x] covered by existing tests
- [ ] added new unit test(s)
- [ ] added new functional test(s)
2025-05-17 00:21:32 +00:00

87 lines
2.2 KiB
Go

package update
import (
"go.temporal.io/api/serviceerror"
updatepb "go.temporal.io/api/update/v1"
"google.golang.org/protobuf/proto"
)
func notZero[T comparable](v T, label string, msg proto.Message) func() error {
return func() error {
var zero T
if v == zero {
return serviceerror.NewInvalidArgumentf("invalid %T: %v is not set", msg, label)
}
return nil
}
}
func eq[T comparable](
left T,
leftLbl string,
right T,
rightLbl string,
msg proto.Message,
) func() error {
return func() error {
if left != right {
return serviceerror.NewInvalidArgumentf("invalid %T: %v != %v", msg, leftLbl, rightLbl)
}
return nil
}
}
func validate(vs ...func() error) error {
for _, v := range vs {
if err := v(); err != nil {
return err
}
}
return nil
}
func validateRequestMsg(updateID string, msg *updatepb.Request) error {
return validateRequestMsgPrefix(updateID, "", msg)
}
func validateRequestMsgPrefix(
updateID string,
prefix string,
msg *updatepb.Request,
) error {
return validate(
notZero(msg, prefix+"body", msg),
notZero(msg.GetMeta(), prefix+"meta", msg),
notZero(msg.GetMeta().GetUpdateId(), prefix+"meta.update_id", msg),
eq(msg.GetMeta().GetUpdateId(), prefix+"meta.update_id", updateID, updateID, msg),
notZero(msg.GetInput(), prefix+"input", msg),
notZero(msg.GetInput().GetName(), prefix+"input.name", msg),
)
}
func validateAcceptanceMsg(msg *updatepb.Acceptance) error {
return validate(
notZero(msg, "body", msg),
notZero(msg.GetAcceptedRequestSequencingEventId(), "accepted_request_sequencing_event_id", msg),
notZero(msg.GetAcceptedRequestMessageId(), "accepted_request_message_id", msg),
)
}
func validateResponseMsg(updateID string, msg *updatepb.Response) error {
return validate(
notZero(msg, "body", msg),
notZero(msg.GetMeta(), "meta", msg),
notZero(msg.GetMeta().GetUpdateId(), "meta.update_id", msg),
eq(msg.GetMeta().GetUpdateId(), "meta.update_id", updateID, updateID, msg),
notZero(msg.GetOutcome(), "outcome", msg),
)
}
// ValidateWorkflowExecutionUpdateRejectionMessage validates the presence of
// expected fields on updatepb.Rejection messages.
func validateRejectionMsg(msg *updatepb.Rejection) error {
return validate(
notZero(msg, "body", msg),
)
}