-
Notifications
You must be signed in to change notification settings - Fork 13
/
predicate.go
53 lines (44 loc) · 1.21 KB
/
predicate.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
package opslevel
import (
"encoding/json"
"fmt"
"slices"
"strconv"
)
type Predicate struct {
Type PredicateTypeEnum `graphql:"type"`
Value string `graphql:"value"`
}
var existsTypes = []PredicateTypeEnum{
PredicateTypeEnumDoesNotExist,
PredicateTypeEnumExists,
}
func (p *Predicate) Validate() error {
if slices.Contains(existsTypes, p.Type) && p.Value != "" {
return fmt.Errorf("Predicate type '%s' cannot have a value. Given value '%s'", p.Type, p.Value)
} else if !slices.Contains(existsTypes, p.Type) && p.Value == "" {
return fmt.Errorf("Predicate type '%s' requires a value", p.Type)
}
numericTypes := []PredicateTypeEnum{
PredicateTypeEnumGreaterThanOrEqualTo,
PredicateTypeEnumLessThanOrEqualTo,
}
if slices.Contains(numericTypes, p.Type) {
if _, err := strconv.Atoi(p.Value); err != nil {
return fmt.Errorf("FilterPredicate type '%s' requires a numeric value. Given '%s'", p.Type, p.Value)
}
}
return nil
}
func (p *PredicateUpdateInput) MarshalJSON() ([]byte, error) {
if p == nil || p.Type == nil || *p.Type == "" {
return []byte("null"), nil
}
m := map[string]string{
"type": string(*p.Type),
}
if p.Value != nil {
m["value"] = *p.Value
}
return json.Marshal(m)
}