• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

looplab / eventhorizon / 12622512440

05 Jan 2025 07:58PM UTC coverage: 27.495% (-39.9%) from 67.361%
12622512440

Pull #419

github

web-flow
Merge b3c17d928 into ac3a97277
Pull Request #419: fix(ci): update to up/download-artifact v4

1769 of 6434 relevant lines covered (27.49%)

1.41 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/middleware/commandhandler/validate/middleware.go
1
// Copyright (c) 2018 - The Event Horizon authors.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15
package validate
16

17
import (
18
        "context"
19
        "fmt"
20

21
        eh "github.com/looplab/eventhorizon"
22
)
23

24
// Command is a command with its own validation method.
25
type Command interface {
26
        eh.Command
27

28
        // Validate returns the error when validating the command.
29
        Validate() error
30
}
31

32
// CommandWithValidation returns a wrapped command with a validation method.
33
func CommandWithValidation(cmd eh.Command, v func() error) Command {
×
34
        return &command{Command: cmd, validate: v}
×
35
}
×
36

37
// NewMiddleware returns a new middleware that validate commands with its own
38
// validation method; `Validate() error`. Commands without the validate method
39
// will not be validated.
40
func NewMiddleware() eh.CommandHandlerMiddleware {
×
41
        return eh.CommandHandlerMiddleware(func(h eh.CommandHandler) eh.CommandHandler {
×
42
                return eh.CommandHandlerFunc(func(ctx context.Context, cmd eh.Command) error {
×
43
                        // Call the validation method if it exists.
×
44
                        if c, ok := cmd.(Command); ok {
×
45
                                if err := c.Validate(); err != nil {
×
46
                                        return &Error{err}
×
47
                                }
×
48
                        }
49

50
                        // Immediate command execution.
51
                        return h.HandleCommand(ctx, cmd)
×
52
                })
53
        })
54
}
55

56
// Error is a validation error.
57
type Error struct {
58
        err error
59
}
60

61
// Error implements the Error method of the error interface.
62
func (e *Error) Error() string {
×
63
        return fmt.Sprintf("invalid command: %s", e.err.Error())
×
64
}
×
65

66
// Unwrap implements the errors.Unwrap method.
67
func (e *Error) Unwrap() error {
×
68
        return e.err
×
69
}
×
70

71
// Cause implements the github.com/pkg/errors Unwrap method.
72
func (e *Error) Cause() error {
×
73
        return e.Unwrap()
×
74
}
×
75

76
// private implementation to wrap ordinary commands and add a validation method.
77
type command struct {
78
        eh.Command
79
        validate func() error
80
}
81

82
// Validate implements the Validate method of the Command interface.
83
func (c *command) Validate() error {
×
84
        return c.validate()
×
85
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc