forked from google/cel-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.go
143 lines (130 loc) · 3.53 KB
/
string.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package types
import (
"fmt"
"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/struct"
"github.com/google/cel-go/common/types/ref"
"github.com/google/cel-go/common/types/traits"
"reflect"
"regexp"
"strconv"
"strings"
"time"
)
// String type implementation which supports addition, comparison, matching,
// and size functions.
type String string
var (
// StringType singleton.
StringType = NewTypeValue("string",
traits.AdderType,
traits.ComparerType,
traits.MatcherType,
traits.SizerType)
)
func (s String) Add(other ref.Value) ref.Value {
if StringType != other.Type() {
return NewErr("unsupported overload")
}
return s + other.(String)
}
func (s String) Compare(other ref.Value) ref.Value {
if StringType != other.Type() {
return NewErr("unsupported overload")
}
return Int(strings.Compare(string(s), string(other.(String))))
}
func (s String) ConvertToNative(typeDesc reflect.Type) (interface{}, error) {
switch typeDesc.Kind() {
case reflect.String:
return string(s), nil
case reflect.Ptr:
if typeDesc == jsonValueType {
return &structpb.Value{
Kind: &structpb.Value_StringValue{
StringValue: s.Value().(string)}}, nil
}
if typeDesc.Elem().Kind() == reflect.String {
p := string(s)
return &p, nil
}
case reflect.Interface:
if reflect.TypeOf(s).Implements(typeDesc) {
return s, nil
}
}
return nil, fmt.Errorf(
"unsupported native conversion from string to '%v'", typeDesc)
}
func (s String) ConvertToType(typeVal ref.Type) ref.Value {
switch typeVal {
case IntType:
if n, err := strconv.ParseInt(string(s), 10, 64); err == nil {
return Int(n)
}
case UintType:
if n, err := strconv.ParseUint(string(s), 10, 64); err == nil {
return Uint(n)
}
case DoubleType:
if n, err := strconv.ParseFloat(string(s), 64); err == nil {
return Double(n)
}
case BoolType:
if b, err := strconv.ParseBool(string(s)); err == nil {
return Bool(b)
}
case BytesType:
return Bytes(s)
case DurationType:
if d, err := time.ParseDuration(string(s)); err == nil {
return Duration{ptypes.DurationProto(d)}
}
case TimestampType:
if t, err := time.Parse(time.RFC3339, string(s)); err == nil {
if ts, err := ptypes.TimestampProto(t); err == nil {
return Timestamp{ts}
}
}
case StringType:
return s
case TypeType:
return StringType
}
return NewErr("type conversion error from '%s' to '%s'", StringType, typeVal)
}
func (s String) Equal(other ref.Value) ref.Value {
return Bool(StringType == other.Type() && s.Value() == other.Value())
}
func (s String) Match(pattern ref.Value) ref.Value {
if pattern.Type() != StringType {
return NewErr("unsupported overload")
}
matched, err := regexp.MatchString(string(pattern.(String)), string(s))
if err != nil {
return &Err{err}
}
return Bool(matched)
}
func (s String) Size() ref.Value {
return Int(len(string(s)))
}
func (s String) Type() ref.Type {
return StringType
}
func (s String) Value() interface{} {
return string(s)
}