-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathzero.go
More file actions
30 lines (24 loc) · 743 Bytes
/
zero.go
File metadata and controls
30 lines (24 loc) · 743 Bytes
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
// Copyright 2025 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package tc
import "reflect"
// IsZero is a Checker that ensures the obtained value is the zero value for the
// obtained type.
var IsZero Checker = &zeroChecker{
&CheckerInfo{Name: "IsZero", Params: []string{"obtained"}},
}
// NotZero is a Checker that ensures the obtained value is not the zero value
// for the obtained type.
var NotZero Checker = Not(IsZero)
type zeroChecker struct {
*CheckerInfo
}
func (c *zeroChecker) Check(params []any, names []string) (bool, string) {
obtained := params[0]
value := reflect.ValueOf(obtained)
if !value.IsValid() {
// untyped nil is a zero any.
return true, ""
}
return value.IsZero(), ""
}