/
opType.go
69 lines (58 loc) · 1.31 KB
/
opType.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 enthistory
import (
"database/sql/driver"
"fmt"
"io"
"strconv"
)
type OpType string
const (
OpTypeInsert OpType = "INSERT"
OpTypeUpdate OpType = "UPDATE"
OpTypeDelete OpType = "DELETE"
)
var opTypes = []string{
OpTypeInsert.String(),
OpTypeUpdate.String(),
OpTypeDelete.String(),
}
// Values provides list valid values for Enum.
func (OpType) Values() (kinds []string) {
kinds = append(kinds, opTypes...)
return
}
func (op OpType) Value() (driver.Value, error) {
return op.String(), nil
}
func (op OpType) String() string {
return string(op)
}
func (op OpType) MarshalGQL(w io.Writer) {
_, _ = w.Write([]byte(strconv.Quote(op.String())))
}
// UnmarshalGQL implements graphql.Unmarshaler interface.
func (op *OpType) UnmarshalGQL(val any) error {
str, ok := val.(string)
if !ok {
return fmt.Errorf("enum %T must be a string", val)
}
*op = OpType(str)
switch *op {
case OpTypeInsert, OpTypeUpdate, OpTypeDelete:
return nil
default:
return fmt.Errorf("%s is not a valid history operation type", str)
}
}
func (op OpType) Name() (string, error) {
switch op {
case OpTypeInsert:
return "OpTypeInsert", nil
case OpTypeUpdate:
return "OpTypeUpdate", nil
case OpTypeDelete:
return "OpTypeDelete", nil
default:
return "", fmt.Errorf("%s is not a valid history operation type", op)
}
}