-
Notifications
You must be signed in to change notification settings - Fork 13
/
utils.go
36 lines (30 loc) · 1.03 KB
/
utils.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
package opslevel
import (
"github.com/go-playground/validator/v10"
"github.com/opslevel/moredefaults"
)
var structValidator = validator.New(validator.WithRequiredStructEnabled())
// IsResourceValid runs validator.Validate on all `validate` struct tags
func IsResourceValid[T any](opslevelResource T) error {
return structValidator.Struct(opslevelResource)
}
// SetDefaultsFor applies all `default` struct tags
func SetDefaultsFor[T any](opslevelResource *T) {
validator.New(validator.WithRequiredStructEnabled())
if err := moredefaults.Set(opslevelResource); err != nil {
panic(err)
}
}
// SetExamplesFor applies all `example` struct tags
func SetExamplesFor[T any](opslevelResource *T) {
validator.New(validator.WithRequiredStructEnabled())
if err := moredefaults.Set(opslevelResource, "example"); err != nil {
panic(err)
}
}
// NewExampleOf makes a new OpsLevel resource with all `example` struct tags applied
func NewExampleOf[T any]() T {
var opslevelResource T
SetExamplesFor[T](&opslevelResource)
return opslevelResource
}