CompileOptions implements the Library interface method.
()
| 389 | |
| 390 | // CompileOptions implements the Library interface method. |
| 391 | func (lib *stringLib) CompileOptions() []cel.EnvOption { |
| 392 | formatLocale := "en_US" |
| 393 | if lib.version < 4 && lib.locale != "" { |
| 394 | // ensure locale is properly-formed if set |
| 395 | _, err := language.Parse(lib.locale) |
| 396 | if err != nil { |
| 397 | return []cel.EnvOption{ |
| 398 | func(e *cel.Env) (*cel.Env, error) { |
| 399 | return nil, fmt.Errorf("failed to parse locale: %w", err) |
| 400 | }, |
| 401 | } |
| 402 | } |
| 403 | formatLocale = lib.locale |
| 404 | } |
| 405 | |
| 406 | opts := []cel.EnvOption{ |
| 407 | cel.Function("charAt", |
| 408 | cel.MemberOverload("string_char_at_int", []*cel.Type{cel.StringType, cel.IntType}, cel.StringType, |
| 409 | cel.BinaryBinding(func(str, ind ref.Val) ref.Val { |
| 410 | s := str.(types.String) |
| 411 | i := ind.(types.Int) |
| 412 | return stringOrError(charAt(string(s), int64(i))) |
| 413 | }))), |
| 414 | cel.Function("indexOf", |
| 415 | cel.MemberOverload("string_index_of_string", []*cel.Type{cel.StringType, cel.StringType}, cel.IntType, |
| 416 | cel.BinaryBinding(func(str, substr ref.Val) ref.Val { |
| 417 | s := str.(types.String) |
| 418 | sub := substr.(types.String) |
| 419 | return intOrError(indexOf(string(s), string(sub))) |
| 420 | })), |
| 421 | cel.MemberOverload("string_index_of_string_int", []*cel.Type{cel.StringType, cel.StringType, cel.IntType}, cel.IntType, |
| 422 | cel.FunctionBinding(func(args ...ref.Val) ref.Val { |
| 423 | s := args[0].(types.String) |
| 424 | sub := args[1].(types.String) |
| 425 | offset := args[2].(types.Int) |
| 426 | return intOrError(indexOfOffset(string(s), string(sub), int64(offset))) |
| 427 | }))), |
| 428 | cel.Function("lastIndexOf", |
| 429 | cel.MemberOverload("string_last_index_of_string", []*cel.Type{cel.StringType, cel.StringType}, cel.IntType, |
| 430 | cel.BinaryBinding(func(str, substr ref.Val) ref.Val { |
| 431 | s := str.(types.String) |
| 432 | sub := substr.(types.String) |
| 433 | return intOrError(lastIndexOf(string(s), string(sub))) |
| 434 | })), |
| 435 | cel.MemberOverload("string_last_index_of_string_int", []*cel.Type{cel.StringType, cel.StringType, cel.IntType}, cel.IntType, |
| 436 | cel.FunctionBinding(func(args ...ref.Val) ref.Val { |
| 437 | s := args[0].(types.String) |
| 438 | sub := args[1].(types.String) |
| 439 | offset := args[2].(types.Int) |
| 440 | return intOrError(lastIndexOfOffset(string(s), string(sub), int64(offset))) |
| 441 | }))), |
| 442 | cel.Function("lowerAscii", |
| 443 | cel.MemberOverload("string_lower_ascii", []*cel.Type{cel.StringType}, cel.StringType, |
| 444 | cel.UnaryBinding(func(str ref.Val) ref.Val { |
| 445 | s := str.(types.String) |
| 446 | return stringOrError(lowerASCII(string(s))) |
| 447 | }))), |
| 448 | cel.Function("replace", |
nothing calls this directly
no test coverage detected