/
timestamp.go
237 lines (218 loc) · 7.86 KB
/
timestamp.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// Copyright 2023 The Blocky Authors
//
// 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
//
// https://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 filtering
import (
"fmt"
"strings"
"time"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/types/known/timestamppb"
"github.com/blockysource/blocky-aip/expr"
"github.com/blockysource/blocky-aip/filtering/ast"
"github.com/blockysource/blocky-aip/token"
)
// TryParseTimestampField tries parsing a timestamp field value.
// The result depending on the input, could be a ValueExpr, ArrayExpr or
// if it accepts indirect values, a FunctionCallExpr or FieldSelectorExpr.
func (b *Interpreter) TryParseTimestampField(ctx *ParseContext, in TryParseValueInput) (TryParseValueResult, error) {
// Timestamp is encoded as an RFC 3339 date-time string.
if len(in.Args) > 0 {
if ctx.ErrHandler != nil {
return TryParseValueResult{ErrPos: in.Value.Position(), ErrMsg: fmt.Sprintf("field is of %q type, but provided value is not a valid %q value: '%s'", in.Field.Kind(), in.Field.Kind(), joinedName(in.Value, in.Args...))}, ErrInvalidValue
}
return TryParseValueResult{}, ErrInvalidValue
}
switch ft := in.Value.(type) {
case *ast.StringLiteral:
// String literal cannot be a timestamp value.
if ctx.ErrHandler != nil {
return TryParseValueResult{ErrPos: ft.Pos, ErrMsg: "field cannot accept string literal as a value"}, ErrInvalidValue
}
return TryParseValueResult{}, ErrInvalidValue
case *ast.TextLiteral:
if in.IsOptional && ft.Token == token.NULL {
ve := expr.AcquireValueExpr()
ve.Value = nil
return TryParseValueResult{Expr: ve}, nil
}
if ft.Token != token.TIMESTAMP {
if ctx.ErrHandler != nil {
return TryParseValueResult{ErrPos: ft.Pos, ErrMsg: fmt.Sprintf("field is of %q type, but provided value is not valid: '%s'", in.Field.Kind(), ft.Value)}, ErrInvalidValue
}
return TryParseValueResult{}, ErrInvalidValue
}
// String literal is the only valid value for timestamp.
t, err := time.Parse(time.RFC3339, ft.Value)
if err != nil {
if ctx.ErrHandler != nil {
return TryParseValueResult{ErrPos: ft.Pos, ErrMsg: fmt.Sprintf("field is of %q type, but provided value is not valid: '%s'", in.Field.Kind(), ft.Value)}, ErrInvalidValue
}
return TryParseValueResult{}, ErrInvalidValue
}
ve := expr.AcquireValueExpr()
ve.Value = t
return TryParseValueResult{Expr: ve}, nil
case *ast.StructExpr:
// Try to parse the time from the timestamppb.Timestamp struct.
// Check if the struct is a message or a map.
if ft.IsMap() {
// A map is not a valid duration value.
var res TryParseValueResult
if ctx.ErrHandler != nil {
res.ErrPos = ft.Position()
res.ErrMsg = fmt.Sprintf("field is of %q type, but provided value is not valid: '%s'", in.Field.Kind(), joinedName(in.Value, in.Args...))
}
return res, ErrInvalidValue
}
// If the struct name is a google.protobuf.Duration, parse it as a duration.
var sb strings.Builder
for i, nm := range ft.Name {
if i > 0 {
sb.WriteRune('.')
}
sb.WriteString(nm.String())
}
if sb.String() != "google.protobuf.Duration" {
// This is not a duration.
var res TryParseValueResult
if ctx.ErrHandler != nil {
res.ErrPos = ft.Position()
res.ErrMsg = fmt.Sprintf("field is of %q type, but provided value is not valid: '%s'", in.Field.Kind(), joinedName(in.Value, in.Args...))
}
return res, ErrInvalidValue
}
// This is a duration.
// Extract duration values from the struct.
var seconds, nanos int64
for _, field := range ft.Elements {
if len(field.Name) != 1 {
// This is not a valid durationpb. Invalid value.
var res TryParseValueResult
if ctx.ErrHandler != nil {
res.ErrPos = field.Position()
res.ErrMsg = fmt.Sprintf("field is of %q type, but provided value is not valid: '%s'", in.Field.Kind(), joinedName(in.Value, in.Args...))
}
return res, ErrInvalidValue
}
var isNanos bool
fieldName := field.Name[0].String()
switch fieldName {
case "seconds":
case "nanos":
isNanos = true
default:
// This is not a valid durationpb. Invalid value.
var res TryParseValueResult
if ctx.ErrHandler != nil {
res.ErrPos = field.Position()
res.ErrMsg = fmt.Sprintf("field is of %q type, but provided value is not valid: '%s'", in.Field.Kind(), joinedName(in.Value, in.Args...))
}
return res, ErrInvalidValue
}
sec := durationMsgDesc.Fields().ByName(protoreflect.Name(fieldName))
vi := TryParseValueInput{
Field: sec,
AllowIndirect: in.AllowIndirect,
IsOptional: false,
Value: field.Value,
Complexity: in.Complexity,
}
res, err := b.TryParseValue(ctx, vi)
if err != nil {
return res, err
}
if res.Expr == nil {
// This is internal error, return an error.
if ctx.ErrHandler != nil {
return TryParseValueResult{ErrPos: field.Position(), ErrMsg: "internal error: parsed expression is nil"}, ErrInternal
}
return TryParseValueResult{}, ErrInternal
}
ve, ok := res.Expr.(*expr.ValueExpr)
if !ok {
// Invalid seconds value.
res.Expr.Free()
var res TryParseValueResult
if ctx.ErrHandler != nil {
res.ErrPos = field.Position()
res.ErrMsg = fmt.Sprintf("field is of %q type, but provided value is not valid: '%s'", in.Field.Kind(), joinedName(in.Value, in.Args...))
}
return res, ErrInvalidValue
}
var v int64
v, ok = ve.Value.(int64)
if !ok {
var res TryParseValueResult
if ctx.ErrHandler != nil {
res.ErrPos = field.Position()
res.ErrMsg = fmt.Sprintf("field is of %q type, but provided value is not valid: '%s'", in.Field.Kind(), joinedName(in.Value, in.Args...))
}
return res, ErrInvalidValue
}
if isNanos {
nanos = v
} else {
seconds = v
}
// Free the expression.
res.Expr.Free()
}
ts := timestamppb.Timestamp{Seconds: seconds, Nanos: int32(nanos)}
ve := expr.AcquireValueExpr()
ve.Value = ts.AsTime()
return TryParseValueResult{Expr: ve}, nil
case *ast.ArrayExpr:
// An array can be parsed as a repeated field value.
ve := expr.AcquireArrayExpr()
for _, elem := range ft.Elements {
// Try parsing each element as a timestamp value.
res, err := b.TryParseValue(ctx, TryParseValueInput{
Field: in.Field,
AllowIndirect: in.AllowIndirect,
IsOptional: in.IsOptional,
Value: elem,
Complexity: in.Complexity,
})
if err != nil {
return res, err
}
if res.Expr == nil {
// This is internal error, return an error.
if ctx.ErrHandler != nil {
return TryParseValueResult{ErrPos: elem.Position(), ErrMsg: "internal error: parsed expression is nil"}, ErrInternal
}
return TryParseValueResult{}, ErrInternal
}
if !in.AllowIndirect {
switch res.Expr.(type) {
case *expr.FunctionCallExpr, *expr.FieldSelectorExpr:
ve.Free()
res.Expr.Free()
if ctx.ErrHandler != nil {
return TryParseValueResult{ErrPos: elem.Position(), ErrMsg: "field cannot accept function call or field selector expression as a value"}, ErrInvalidValue
}
return TryParseValueResult{}, ErrInternal
}
}
ve.Elements = append(ve.Elements, res.Expr)
}
return TryParseValueResult{Expr: ve}, nil
default:
// This is invalid AST node, return an error.
if ctx.ErrHandler != nil {
return TryParseValueResult{ErrPos: in.Value.Position(), ErrMsg: "invalid AST node"}, ErrInvalidAST
}
return TryParseValueResult{}, ErrInvalidAST
}
}