-
Notifications
You must be signed in to change notification settings - Fork 1
/
arg.go
69 lines (64 loc) · 1.37 KB
/
arg.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* Package arg implements functions to validate arguments.
*
* Synopsis:
* var (
* host string = "127.0.0.1"
* status string = "connecting"
* requests int64 = 99
* )
*
* err := arg.Assert(
* arg.Strings.Assert(host, "host",
* arg.Strings.NonEmpty,
* ),
* arg.Strings.Assert(status, "status",
* arg.Strings.NonEmpty,
* arg.Strings.In("connecting", "closed", "aborted"),
* ),
* arg.Ints.Assert(requests, "requrests",
* arg.Ints.NonNegativeInteger,
* ),
* )
* fmt.Println(err) // Output: <nil>
*
* The Strings, Floats, Ints, Numbers, Values also offer `Assertor()`:
*
* err := arg.Strings.Assertor(host, "host").
* Assert(
* arg.Strings.NonEmpty,
* )
*
* is preferable to
*
* err := arg.Strings.Assert(host, "host",
* arg.Strings.NonEmpty,
* )
*/
package arg
import "github.com/Bofry/arg/content"
func Assert(errs ...error) error {
for _, err := range errs {
if err != nil {
return err
}
}
return nil
}
func AssertFunc(functions ...func() error) error {
for _, fn := range functions {
err := fn()
if err != nil {
return err
}
}
return nil
}
func ThrowError(name, reason string) error {
return &InvalidArgumentError{
Name: name,
Reason: reason,
}
}
func Args(target interface{}, opts ...content.ProcessorOption) *content.Processor {
return content.NewProcessor(target, opts...)
}