| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package cel |
| 16 |
|
| 17 |
import ( |
| 18 |
"fmt" |
| 19 |
"math" |
| 20 |
|
| 21 |
"github.com/google/cel-go/common" |
| 22 |
"github.com/google/cel-go/common/ast" |
| 23 |
"github.com/google/cel-go/common/decls" |
| 24 |
"github.com/google/cel-go/common/env" |
| 25 |
"github.com/google/cel-go/common/operators" |
| 26 |
"github.com/google/cel-go/common/overloads" |
| 27 |
"github.com/google/cel-go/common/stdlib" |
| 28 |
"github.com/google/cel-go/common/types" |
| 29 |
"github.com/google/cel-go/common/types/ref" |
| 30 |
"github.com/google/cel-go/common/types/traits" |
| 31 |
"github.com/google/cel-go/interpreter" |
| 32 |
"github.com/google/cel-go/parser" |
| 33 |
) |
| 34 |
|
| 35 |
const ( |
| 36 |
optMapMacro = "optMap" |
| 37 |
optFlatMapMacro = "optFlatMap" |
| 38 |
hasValueFunc = "hasValue" |
| 39 |
unwrapOptFunc = "unwrapOpt" |
| 40 |
optionalNoneFunc = "optional.none" |
| 41 |
optionalOfFunc = "optional.of" |
| 42 |
optionalOfNonZeroValueFunc = "optional.ofNonZeroValue" |
| 43 |
optionalUnwrapFunc = "optional.unwrap" |
| 44 |
valueFunc = "value" |
| 45 |
unusedIterVar = "#unused" |
| 46 |
) |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
type Library interface { |
| 55 |
|
| 56 |
|
| 57 |
CompileOptions() []EnvOption |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
ProgramOptions() []ProgramOption |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
type SingletonLibrary interface { |
| 67 |
Library |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
LibraryName() string |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
type LibraryAliaser interface { |
| 76 |
LibraryAlias() string |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
type LibrarySubsetter interface { |
| 81 |
LibrarySubset() *env.LibrarySubset |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
type LibraryVersioner interface { |
| 88 |
LibraryVersion() uint32 |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
func Lib(l Library) EnvOption { |
| 94 |
singleton, isSingleton := l.(SingletonLibrary) |
| 95 |
return func(e *Env) (*Env, error) { |
| 96 |
if isSingleton { |
| 97 |
if e.HasLibrary(singleton.LibraryName()) { |
| 98 |
return e, nil |
| 99 |
} |
| 100 |
e.libraries[singleton.LibraryName()] = singleton |
| 101 |
} |
| 102 |
var err error |
| 103 |
for _, opt := range l.CompileOptions() { |
| 104 |
e, err = opt(e) |
| 105 |
if err != nil { |
| 106 |
return nil, err |
| 107 |
} |
| 108 |
} |
| 109 |
e.progOpts = append(e.progOpts, l.ProgramOptions()...) |
| 110 |
return e, nil |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
type StdLibOption func(*stdLibrary) *stdLibrary |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
func StdLibSubset(subset *env.LibrarySubset) StdLibOption { |
| 123 |
return func(lib *stdLibrary) *stdLibrary { |
| 124 |
lib.subset = subset |
| 125 |
return lib |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
func StdLib(opts ...StdLibOption) EnvOption { |
| 131 |
lib := &stdLibrary{} |
| 132 |
for _, o := range opts { |
| 133 |
lib = o(lib) |
| 134 |
} |
| 135 |
return Lib(lib) |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
type stdLibrary struct { |
| 141 |
subset *env.LibrarySubset |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
func (*stdLibrary) LibraryName() string { |
| 146 |
return "cel.lib.std" |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
func (*stdLibrary) LibraryAlias() string { |
| 151 |
return "stdlib" |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
func (lib *stdLibrary) LibrarySubset() *env.LibrarySubset { |
| 156 |
return lib.subset |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
func (lib *stdLibrary) CompileOptions() []EnvOption { |
| 161 |
funcs := stdlib.Functions() |
| 162 |
macros := StandardMacros |
| 163 |
if lib.subset != nil { |
| 164 |
subMacros := []Macro{} |
| 165 |
for _, m := range macros { |
| 166 |
if lib.subset.SubsetMacro(m.Function()) { |
| 167 |
subMacros = append(subMacros, m) |
| 168 |
} |
| 169 |
} |
| 170 |
macros = subMacros |
| 171 |
subFuncs := []*decls.FunctionDecl{} |
| 172 |
for _, fn := range funcs { |
| 173 |
if f, include := lib.subset.SubsetFunction(fn); include { |
| 174 |
subFuncs = append(subFuncs, f) |
| 175 |
} |
| 176 |
} |
| 177 |
funcs = subFuncs |
| 178 |
} |
| 179 |
return []EnvOption{ |
| 180 |
func(e *Env) (*Env, error) { |
| 181 |
var err error |
| 182 |
if err = lib.subset.Validate(); err != nil { |
| 183 |
return nil, err |
| 184 |
} |
| 185 |
for _, fn := range funcs { |
| 186 |
existing, found := e.functions[fn.Name()] |
| 187 |
if found { |
| 188 |
fn, err = existing.Merge(fn) |
| 189 |
if err != nil { |
| 190 |
return nil, err |
| 191 |
} |
| 192 |
} |
| 193 |
e.functions[fn.Name()] = fn |
| 194 |
} |
| 195 |
return e, nil |
| 196 |
}, |
| 197 |
Macros(macros...), |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
func (*stdLibrary) ProgramOptions() []ProgramOption { |
| 203 |
return []ProgramOption{} |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
se |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
func OptionalTypes(opts ...OptionalTypesOption) EnvOption { |
| 362 |
lib := &optionalLib{version: math.MaxUint32} |
| 363 |
for _, opt := range opts { |
| 364 |
lib = opt(lib) |
| 365 |
} |
| 366 |
return Lib(lib) |
| 367 |
} |
| 368 |
|
| 369 |
type optionalLib struct { |
| 370 |
version uint32 |
| 371 |
} |
| 372 |
|
| 373 |
|
| 374 |
type OptionalTypesOption func(*optionalLib) *optionalLib |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
func OptionalTypesVersion(version uint32) OptionalTypesOption { |
| 386 |
return func(lib *optionalLib) *optionalLib { |
| 387 |
lib.version = version |
| 388 |
return lib |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
|
| 393 |
func (*optionalLib) LibraryName() string { |
| 394 |
return "cel.lib.optional" |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
func (*optionalLib) LibraryAlias() string { |
| 399 |
return "optional" |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
func (lib *optionalLib) LibraryVersion() uint32 { |
| 404 |
return lib.version |
| 405 |
} |
| 406 |
|
| 407 |
|
| 408 |
func (lib *optionalLib) CompileOptions() []EnvOption { |
| 409 |
paramTypeK := TypeParamType("K") |
| 410 |
paramTypeV := TypeParamType("V") |
| 411 |
optionalTypeV := OptionalType(paramTypeV) |
| 412 |
listTypeV := ListType(paramTypeV) |
| 413 |
mapTypeKV := MapType(paramTypeK, paramTypeV) |
| 414 |
listOptionalTypeV := ListType(optionalTypeV) |
| 415 |
|
| 416 |
opts := []EnvOption{ |
| 417 |
|
| 418 |
enableOptionalSyntax(), |
| 419 |
|
| 420 |
|
| 421 |
Types(types.OptionalType), |
| 422 |
|
| 423 |
|
| 424 |
Macros(ReceiverMacro(optMapMacro, 2, optMap, |
| 425 |
MacroDocs(`perform computation on the value if present and return the result as an optional`), |
| 426 |
MacroExamples( |
| 427 |
common.MultilineDescription( |
| 428 |
`// sub with the prefix 'dev.cel' or optional.none()`, |
| 429 |
`request.auth.tokens.?sub.optMap(id, 'dev.cel.' + id)`), |
| 430 |
`optional.none().optMap(i, i * 2) // optional.none()`))), |
| 431 |
|
| 432 |
|
| 433 |
Function(optionalOfFunc, |
| 434 |
FunctionDocs(`create a new optional_type(T) with a value where any value is considered valid`), |
| 435 |
Overload("optional_of", []*Type{paramTypeV}, optionalTypeV, |
| 436 |
OverloadExamples(`optional.of(1) // optional(1)`), |
| 437 |
UnaryBinding(func(value ref.Val) ref.Val { |
| 438 |
return types.OptionalOf(value) |
| 439 |
}))), |
| 440 |
Function(optionalOfNonZeroValueFunc, |
| 441 |
FunctionDocs(`create a new optional_type(T) with a value, if the value is not a zero or empty value`), |
| 442 |
Overload("optional_ofNonZeroValue", []*Type{paramTypeV}, optionalTypeV, |
| 443 |
OverloadExamples( |
| 444 |
`optional.ofNonZeroValue(null) // optional.none()`, |
| 445 |
`optional.ofNonZeroValue("") // optional.none()`, |
| 446 |
`optional.ofNonZeroValue("hello") // optional.of('hello')`), |
| 447 |
UnaryBinding(func(value ref.Val) ref.Val { |
| 448 |
v, isZeroer := value.(traits.Zeroer) |
| 449 |
if !isZeroer || !v.IsZeroValue() { |
| 450 |
return types.OptionalOf(value) |
| 451 |
} |
| 452 |
return types.OptionalNone |
| 453 |
}))), |
| 454 |
Function(optionalNoneFunc, |
| 455 |
FunctionDocs(`singleton value representing an optional without a value`), |
| 456 |
Overload("optional_none", []*Type{}, optionalTypeV, |
| 457 |
OverloadExamples(`optional.none()`), |
| 458 |
FunctionBinding(func(values ...ref.Val) ref.Val { |
| 459 |
return types.OptionalNone |
| 460 |
}))), |
| 461 |
Function(valueFunc, |
| 462 |
FunctionDocs(`obtain the value contained by the optional, error if optional.none()`), |
| 463 |
MemberOverload("optional_value", []*Type{optionalTypeV}, paramTypeV, |
| 464 |
OverloadExamples( |
| 465 |
`optional.of(1).value() // 1`, |
| 466 |
`optional.none().value() // error`), |
| 467 |
UnaryBinding(func(value ref.Val) ref.Val { |
| 468 |
opt := value.(*types.Optional) |
| 469 |
return opt.GetValue() |
| 470 |
}))), |
| 471 |
Function(hasValueFunc, |
| 472 |
FunctionDocs(`determine whether the optional contains a value`), |
| 473 |
MemberOverload("optional_hasValue", []*Type{optionalTypeV}, BoolType, |
| 474 |
OverloadExamples(`optional.of({1: 2}).hasValue() // true`), |
| 475 |
UnaryBinding(func(value ref.Val) ref.Val { |
| 476 |
opt := value.(*types.Optional) |
| 477 |
return types.Bool(opt.HasValue()) |
| 478 |
}))), |
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
Function("or", |
| 483 |
FunctionDocs(`chain optional expressions together, picking the first valued optional expression`), |
| 484 |
MemberOverload("optional_or_optional", []*Type{optionalTypeV, optionalTypeV}, optionalTypeV, |
| 485 |
OverloadExamples( |
| 486 |
`optional.none().or(optional.of(1)) // optional.of(1)`, |
| 487 |
common.MultilineDescription( |
| 488 |
`// either a value from the first list, a value from the second, or optional.none()`, |
| 489 |
`[1, 2, 3][?x].or([3, 4, 5][?y])`)))), |
| 490 |
Function("orValue", |
| 491 |
FunctionDocs(`chain optional expressions together picking the first valued optional or the default value`), |
| 492 |
MemberOverload("optional_orValue_value", []*Type{optionalTypeV, paramTypeV}, paramTypeV, |
| 493 |
OverloadExamples( |
| 494 |
common.MultilineDescription( |
| 495 |
`// pick the value for the given key if the key exists, otherwise return 'you'`, |
| 496 |
`{'hello': 'world', 'goodbye': 'cruel world'}[?greeting].orValue('you')`)))), |
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
Function(operators.OptSelect, |
| 501 |
FunctionDocs(`if the field is present create an optional of the field value, otherwise return optional.none()`), |
| 502 |
Overload("select_optional_field", []*Type{DynType, StringType}, optionalTypeV, |
| 503 |
OverloadExamples( |
| 504 |
`msg.?field // optional.of(field) if non-empty, otherwise optional.none()`, |
| 505 |
`msg.?field.?nested_field // optional.of(nested_field) if both field and nested_field are non-empty.`))), |
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
Function(operators.OptIndex, |
| 510 |
FunctionDocs(`if the index is present create an optional of the field value, otherwise return optional.none()`), |
| 511 |
Overload("list_optindex_optional_int", []*Type{listTypeV, IntType}, optionalTypeV, |
| 512 |
OverloadExamples(`[1, 2, 3][?x] // element value if x is in the list size, else optional.none()`)), |
| 513 |
Overload("optional_list_optindex_optional_int", []*Type{OptionalType(listTypeV), IntType}, optionalTypeV), |
| 514 |
Overload("map_optindex_optional_value", []*Type{mapTypeKV, paramTypeK}, optionalTypeV, |
| 515 |
OverloadExamples( |
| 516 |
`map_value[?key] // value at the key if present, else optional.none()`, |
| 517 |
common.MultilineDescription( |
| 518 |
`// map key-value if index is a valid map key, else optional.none()`, |
| 519 |
`{0: 2, 2: 4, 6: 8}[?index]`))), |
| 520 |
Overload("optional_map_optindex_optional_value", []*Type{OptionalType(mapTypeKV), paramTypeK}, optionalTypeV)), |
| 521 |
|
| 522 |
|
| 523 |
Function(operators.Index, |
| 524 |
Overload("optional_list_index_int", []*Type{OptionalType(listTypeV), IntType}, optionalTypeV), |
| 525 |
Overload("optional_map_index_value", []*Type{OptionalType(mapTypeKV), paramTypeK}, optionalTypeV)), |
| 526 |
} |
| 527 |
if lib.version >= 1 { |
| 528 |
opts = append(opts, Macros(ReceiverMacro(optFlatMapMacro, 2, optFlatMap, |
| 529 |
MacroDocs(`perform computation on the value if present and produce an optional value within the computation`), |
| 530 |
MacroExamples( |
| 531 |
common.MultilineDescription( |
| 532 |
`// m = {'key': {}}`, |
| 533 |
`m.?key.optFlatMap(k, k.?subkey) // optional.none()`), |
| 534 |
common.MultilineDescription( |
| 535 |
`// m = {'key': {'subkey': 'value'}}`, |
| 536 |
`m.?key.optFlatMap(k, k.?subkey) // optional.of('value')`), |
| 537 |
)))) |
| 538 |
} |
| 539 |
|
| 540 |
if lib.version >= 2 { |
| 541 |
opts = append(opts, Function("last", |
| 542 |
FunctionDocs(`return the last value in a list if present, otherwise optional.none()`), |
| 543 |
MemberOverload("list_last", []*Type{listTypeV}, optionalTypeV, |
| 544 |
OverloadExamples( |
| 545 |
`[].last() // optional.none()`, |
| 546 |
`[1, 2, 3].last() ? optional.of(3)`), |
| 547 |
UnaryBinding(func(v ref.Val) ref.Val { |
| 548 |
list := v.(traits.Lister) |
| 549 |
sz := list.Size().(types.Int) |
| 550 |
if sz == types.IntZero { |
| 551 |
return types.OptionalNone |
| 552 |
} |
| 553 |
return types.OptionalOf(list.Get(types.Int(sz - 1))) |
| 554 |
}), |
| 555 |
), |
| 556 |
)) |
| 557 |
|
| 558 |
opts = append(opts, Function("first", |
| 559 |
FunctionDocs(`return the first value in a list if present, otherwise optional.none()`), |
| 560 |
MemberOverload("list_first", []*Type{listTypeV}, optionalTypeV, |
| 561 |
OverloadExamples( |
| 562 |
`[].first() // optional.none()`, |
| 563 |
`[1, 2, 3].first() ? optional.of(1)`), |
| 564 |
UnaryBinding(func(v ref.Val) ref.Val { |
| 565 |
list := v.(traits.Lister) |
| 566 |
sz := list.Size().(types.Int) |
| 567 |
if sz == types.IntZero { |
| 568 |
return types.OptionalNone |
| 569 |
} |
| 570 |
return types.OptionalOf(list.Get(types.Int(0))) |
| 571 |
}), |
| 572 |
), |
| 573 |
)) |
| 574 |
|
| 575 |
opts = append(opts, Function(optionalUnwrapFunc, |
| 576 |
FunctionDocs(`convert a list of optional values to a list containing only value which are not optional.none()`), |
| 577 |
Overload("optional_unwrap", []*Type{listOptionalTypeV}, listTypeV, |
| 578 |
OverloadExamples(`optional.unwrap([optional.of(1), optional.none()]) // [1]`), |
| 579 |
UnaryBinding(optUnwrap)))) |
| 580 |
opts = append(opts, Function(unwrapOptFunc, |
| 581 |
FunctionDocs(`convert a list of optional values to a list containing only value which are not optional.none()`), |
| 582 |
MemberOverload("optional_unwrapOpt", []*Type{listOptionalTypeV}, listTypeV, |
| 583 |
OverloadExamples(`[optional.of(1), optional.none()].unwrapOpt() // [1]`), |
| 584 |
UnaryBinding(optUnwrap)))) |
| 585 |
} |
| 586 |
|
| 587 |
return opts |
| 588 |
} |
| 589 |
|
| 590 |
|
| 591 |
func (lib *optionalLib) ProgramOptions() []ProgramOption { |
| 592 |
return []ProgramOption{ |
| 593 |
CustomDecorator(decorateOptionalOr), |
| 594 |
} |
| 595 |
} |
| 596 |
|
| 597 |
|
| 598 |
func (lib *optionalLib) Version() uint32 { |
| 599 |
return lib.version |
| 600 |
} |
| 601 |
|
| 602 |
func optMap(meh MacroExprFactory, target ast.Expr, args []ast.Expr) (ast.Expr, *Error) { |
| 603 |
varIdent := args[0] |
| 604 |
varName := "" |
| 605 |
switch varIdent.Kind() { |
| 606 |
case ast.IdentKind: |
| 607 |
varName = varIdent.AsIdent() |
| 608 |
default: |
| 609 |
return nil, meh.NewError(varIdent.ID(), "optMap() variable name must be a simple identifier") |
| 610 |
} |
| 611 |
mapExpr := args[1] |
| 612 |
return meh.NewCall( |
| 613 |
operators.Conditional, |
| 614 |
meh.NewMemberCall(hasValueFunc, target), |
| 615 |
meh.NewCall(optionalOfFunc, |
| 616 |
meh.NewComprehension( |
| 617 |
meh.NewList(), |
| 618 |
unusedIterVar, |
| 619 |
varName, |
| 620 |
meh.NewMemberCall(valueFunc, meh.Copy(target)), |
| 621 |
meh.NewLiteral(types.False), |
| 622 |
meh.NewIdent(varName), |
| 623 |
mapExpr, |
| 624 |
), |
| 625 |
), |
| 626 |
meh.NewCall(optionalNoneFunc), |
| 627 |
), nil |
| 628 |
} |
| 629 |
|
| 630 |
func optFlatMap(meh MacroExprFactory, target ast.Expr, args []ast.Expr) (ast.Expr, *Error) { |
| 631 |
varIdent := args[0] |
| 632 |
varName := "" |
| 633 |
switch varIdent.Kind() { |
| 634 |
case ast.IdentKind: |
| 635 |
varName = varIdent.AsIdent() |
| 636 |
default: |
| 637 |
return nil, meh.NewError(varIdent.ID(), "optFlatMap() variable name must be a simple identifier") |
| 638 |
} |
| 639 |
mapExpr := args[1] |
| 640 |
return meh.NewCall( |
| 641 |
operators.Conditional, |
| 642 |
meh.NewMemberCall(hasValueFunc, target), |
| 643 |
meh.NewComprehension( |
| 644 |
meh.NewList(), |
| 645 |
unusedIterVar, |
| 646 |
varName, |
| 647 |
meh.NewMemberCall(valueFunc, meh.Copy(target)), |
| 648 |
meh.NewLiteral(types.False), |
| 649 |
meh.NewIdent(varName), |
| 650 |
mapExpr, |
| 651 |
), |
| 652 |
meh.NewCall(optionalNoneFunc), |
| 653 |
), nil |
| 654 |
} |
| 655 |
|
| 656 |
func optUnwrap(value ref.Val) ref.Val { |
| 657 |
list := value.(traits.Lister) |
| 658 |
var unwrappedList []ref.Val |
| 659 |
iter := list.Iterator() |
| 660 |
for iter.HasNext() == types.True { |
| 661 |
val := iter.Next() |
| 662 |
opt, isOpt := val.(*types.Optional) |
| 663 |
if !isOpt { |
| 664 |
return types.WrapErr(fmt.Errorf("value %v is not optional", val)) |
| 665 |
} |
| 666 |
if opt.HasValue() { |
| 667 |
unwrappedList = append(unwrappedList, opt.GetValue()) |
| 668 |
} |
| 669 |
} |
| 670 |
return types.DefaultTypeAdapter.NativeToValue(unwrappedList) |
| 671 |
} |
| 672 |
|
| 673 |
func enableOptionalSyntax() EnvOption { |
| 674 |
return func(e *Env) (*Env, error) { |
| 675 |
e.prsrOpts = append(e.prsrOpts, parser.EnableOptionalSyntax(true)) |
| 676 |
return e, nil |
| 677 |
} |
| 678 |
} |
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
func EnableErrorOnBadPresenceTest(value bool) EnvOption { |
| 683 |
return features(featureEnableErrorOnBadPresenceTest, value) |
| 684 |
} |
| 685 |
|
| 686 |
func decorateOptionalOr(i interpreter.Interpretable) (interpreter.Interpretable, error) { |
| 687 |
call, ok := i.(interpreter.InterpretableCall) |
| 688 |
if !ok { |
| 689 |
return i, nil |
| 690 |
} |
| 691 |
args := call.Args() |
| 692 |
if len(args) != 2 { |
| 693 |
return i, nil |
| 694 |
} |
| 695 |
switch call.Function() { |
| 696 |
case "or": |
| 697 |
if call.OverloadID() != "" && call.OverloadID() != "optional_or_optional" { |
| 698 |
return i, nil |
| 699 |
} |
| 700 |
return &evalOptionalOr{ |
| 701 |
id: call.ID(), |
| 702 |
lhs: args[0], |
| 703 |
rhs: args[1], |
| 704 |
}, nil |
| 705 |
case "orValue": |
| 706 |
if call.OverloadID() != "" && call.OverloadID() != "optional_orValue_value" { |
| 707 |
return i, nil |
| 708 |
} |
| 709 |
return &evalOptionalOrValue{ |
| 710 |
id: call.ID(), |
| 711 |
lhs: args[0], |
| 712 |
rhs: args[1], |
| 713 |
}, nil |
| 714 |
default: |
| 715 |
return i, nil |
| 716 |
} |
| 717 |
} |
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
type evalOptionalOr struct { |
| 722 |
id int64 |
| 723 |
lhs interpreter.Interpretable |
| 724 |
rhs interpreter.Interpretable |
| 725 |
} |
| 726 |
|
| 727 |
|
| 728 |
func (opt *evalOptionalOr) ID() int64 { |
| 729 |
return opt.id |
| 730 |
} |
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
func (opt *evalOptionalOr) Eval(ctx interpreter.Activation) ref.Val { |
| 735 |
|
| 736 |
optLHS := opt.lhs.Eval(ctx) |
| 737 |
optVal, ok := optLHS.(*types.Optional) |
| 738 |
if !ok { |
| 739 |
return optLHS |
| 740 |
} |
| 741 |
if optVal.HasValue() { |
| 742 |
return optVal |
| 743 |
} |
| 744 |
return opt.rhs.Eval(ctx) |
| 745 |
} |
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
type evalOptionalOrValue struct { |
| 750 |
id int64 |
| 751 |
lhs interpreter.Interpretable |
| 752 |
rhs interpreter.Interpretable |
| 753 |
} |
| 754 |
|
| 755 |
|
| 756 |
func (opt *evalOptionalOrValue) ID() int64 { |
| 757 |
return opt.id |
| 758 |
} |
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
func (opt *evalOptionalOrValue) Eval(ctx interpreter.Activation) ref.Val { |
| 763 |
|
| 764 |
optLHS := opt.lhs.Eval(ctx) |
| 765 |
optVal, ok := optLHS.(*types.Optional) |
| 766 |
if !ok { |
| 767 |
return optLHS |
| 768 |
} |
| 769 |
if optVal.HasValue() { |
| 770 |
return optVal.GetValue() |
| 771 |
} |
| 772 |
return opt.rhs.Eval(ctx) |
| 773 |
} |
| 774 |
|
| 775 |
type timeLegacyLibrary struct{} |
| 776 |
|
| 777 |
func (timeLegacyLibrary) CompileOptions() []EnvOption { |
| 778 |
return timeOverloadDeclarations |
| 779 |
} |
| 780 |
|
| 781 |
func (timeLegacyLibrary) ProgramOptions() []ProgramOption { |
| 782 |
return []ProgramOption{} |
| 783 |
} |
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
var ( |
| 788 |
timeOverloadDeclarations = []EnvOption{ |
| 789 |
Function(overloads.TimeGetFullYear, |
| 790 |
MemberOverload(overloads.TimestampToYear, []*Type{TimestampType}, IntType, |
| 791 |
UnaryBinding(func(ts ref.Val) ref.Val { |
| 792 |
t := ts.(types.Timestamp) |
| 793 |
return t.Receive(overloads.TimeGetFullYear, overloads.TimestampToYear, []ref.Val{}) |
| 794 |
}), |
| 795 |
), |
| 796 |
), |
| 797 |
Function(overloads.TimeGetMonth, |
| 798 |
MemberOverload(overloads.TimestampToMonth, []*Type{TimestampType}, IntType, |
| 799 |
UnaryBinding(func(ts ref.Val) ref.Val { |
| 800 |
t := ts.(types.Timestamp) |
| 801 |
return t.Receive(overloads.TimeGetMonth, overloads.TimestampToMonth, []ref.Val{}) |
| 802 |
}), |
| 803 |
), |
| 804 |
), |
| 805 |
Function(overloads.TimeGetDayOfYear, |
| 806 |
MemberOverload(overloads.TimestampToDayOfYear, []*Type{TimestampType}, IntType, |
| 807 |
UnaryBinding(func(ts ref.Val) ref.Val { |
| 808 |
t := ts.(types.Timestamp) |
| 809 |
return t.Receive(overloads.TimeGetDayOfYear, overloads.TimestampToDayOfYear, []ref.Val{}) |
| 810 |
}), |
| 811 |
), |
| 812 |
), |
| 813 |
Function(overloads.TimeGetDayOfMonth, |
| 814 |
MemberOverload(overloads.TimestampToDayOfMonthZeroBased, []*Type{TimestampType}, IntType, |
| 815 |
UnaryBinding(func(ts ref.Val) ref.Val { |
| 816 |
t := ts.(types.Timestamp) |
| 817 |
return t.Receive(overloads.TimeGetDayOfMonth, overloads.TimestampToDayOfMonthZeroBased, []ref.Val{}) |
| 818 |
}), |
| 819 |
), |
| 820 |
), |
| 821 |
Function(overloads.TimeGetDate, |
| 822 |
MemberOverload(overloads.TimestampToDayOfMonthOneBased, []*Type{TimestampType}, IntType, |
| 823 |
UnaryBinding(func(ts ref.Val) ref.Val { |
| 824 |
t := ts.(types.Timestamp) |
| 825 |
return t.Receive(overloads.TimeGetDate, overloads.TimestampToDayOfMonthOneBased, []ref.Val{}) |
| 826 |
}), |
| 827 |
), |
| 828 |
), |
| 829 |
Function(overloads.TimeGetDayOfWeek, |
| 830 |
MemberOverload(overloads.TimestampToDayOfWeek, []*Type{TimestampType}, IntType, |
| 831 |
UnaryBinding(func(ts ref.Val) ref.Val { |
| 832 |
t := ts.(types.Timestamp) |
| 833 |
return t.Receive(overloads.TimeGetDayOfWeek, overloads.TimestampToDayOfWeek, []ref.Val{}) |
| 834 |
}), |
| 835 |
), |
| 836 |
), |
| 837 |
Function(overloads.TimeGetHours, |
| 838 |
MemberOverload(overloads.TimestampToHours, []*Type{TimestampType}, IntType, |
| 839 |
UnaryBinding(func(ts ref.Val) ref.Val { |
| 840 |
t := ts.(types.Timestamp) |
| 841 |
return t.Receive(overloads.TimeGetHours, overloads.TimestampToHours, []ref.Val{}) |
| 842 |
}), |
| 843 |
), |
| 844 |
), |
| 845 |
Function(overloads.TimeGetMinutes, |
| 846 |
MemberOverload(overloads.TimestampToMinutes, []*Type{TimestampType}, IntType, |
| 847 |
UnaryBinding(func(ts ref.Val) ref.Val { |
| 848 |
t := ts.(types.Timestamp) |
| 849 |
return t.Receive(overloads.TimeGetMinutes, overloads.TimestampToMinutes, []ref.Val{}) |
| 850 |
}), |
| 851 |
), |
| 852 |
), |
| 853 |
Function(overloads.TimeGetSeconds, |
| 854 |
MemberOverload(overloads.TimestampToSeconds, []*Type{TimestampType}, IntType, |
| 855 |
UnaryBinding(func(ts ref.Val) ref.Val { |
| 856 |
t := ts.(types.Timestamp) |
| 857 |
return t.Receive(overloads.TimeGetSeconds, overloads.TimestampToSeconds, []ref.Val{}) |
| 858 |
}), |
| 859 |
), |
| 860 |
), |
| 861 |
Function(overloads.TimeGetMilliseconds, |
| 862 |
MemberOverload(overloads.TimestampToMilliseconds, []*Type{TimestampType}, IntType, |
| 863 |
UnaryBinding(func(ts ref.Val) ref.Val { |
| 864 |
t := ts.(types.Timestamp) |
| 865 |
return t.Receive(overloads.TimeGetMilliseconds, overloads.TimestampToMilliseconds, []ref.Val{}) |
| 866 |
}), |
| 867 |
), |
| 868 |
), |
| 869 |
} |
| 870 |
) |