-
Notifications
You must be signed in to change notification settings - Fork 1
/
pt_orn.go
81 lines (71 loc) · 1.41 KB
/
pt_orn.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
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"regexp"
"strconv"
)
func init() {
RegisterPropertyType(&propertyTypeORN{})
}
var reORN = regexp.MustCompile("^o:[^:]*:[^:]*:[^:]*:[^:*](/.*)?$")
var ErrIsNotORN = ObserveError{Msg: "value is not an orn"}
type propertyTypeORN struct{}
func (*propertyTypeORN) TypeName() string { return "orn" }
func (*propertyTypeORN) Present(i any) (string, error) {
if i == nil {
return "", nil
}
str, is := i.(string)
if !is {
return "", ErrIsNotORN
}
if !reORN.MatchString(str) {
return "", ErrIsNotORN
}
return str, nil
}
func (*propertyTypeORN) ToString(i any) (string, error) {
if i == nil {
return "", nil
}
switch v := i.(type) {
case *string:
if v == nil {
return "", nil
}
if !reORN.MatchString(*v) {
return "", ErrIsNotORN
}
return strconv.Quote(*v), nil
case string:
if !reORN.MatchString(v) {
return "", ErrIsNotORN
}
return strconv.Quote(v), nil
default:
return "", ErrIsNotString
}
}
func (*propertyTypeORN) FromString(s string) (any, error) {
if s == "" {
return nil, nil
}
if len(s) < 2 {
return nil, ErrIsNotORN
}
if s[0] != '"' || s[len(s)-1] != '"' {
return nil, ErrIsNotORN
}
if str, err := strconv.Unquote(s); err != nil {
return nil, ErrIsNotORN
} else if !reORN.MatchString(str) {
return nil, ErrIsNotORN
} else {
return str, nil
}
}
func (*propertyTypeORN) FromGQL(v any) any {
if v == nil {
return nil
}
return v.(string)
}