Skip to content

Commit

Permalink
Add string.format.
Browse files Browse the repository at this point in the history
  • Loading branch information
DangerOnTheRanger committed Feb 14, 2023
1 parent f636d42 commit 83a4e49
Show file tree
Hide file tree
Showing 59 changed files with 18,855 additions and 2 deletions.
26 changes: 26 additions & 0 deletions cel/decls.go
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,32 @@ func FunctionBinding(binding functions.FunctionOp) OverloadOpt {
}
}

// LocaleOptionBinding conditionally adds a new binding based on locale, passing newOp an additional final
// argument that represents the configured locale as a string. If no locale was configured, LocaleOptionBinding
// will do nothing. If no name/overloadID match is made, then LocaleOptionBinding will return an error.
func LocaleOptionBinding(name, overloadID string, newOp functions.FunctionOp) EnvOption {
return func(e *Env) (*Env, error) {
if e.locale == "" {
return e, nil
}
if function, found := e.functions[name]; found {
for _, overload := range function.overloads {
if overload.id == overloadID {
overload.functionOp = func(values ...ref.Val) ref.Val {
values = append(values, types.String(e.locale))
return newOp(values...)
}
return e, nil
}
return nil, fmt.Errorf("could not find overloadID: %s", overloadID)
}
} else {
return nil, fmt.Errorf("could not find function: %s", name)
}
return nil, nil
}
}

// OverloadIsNonStrict enables the function to be called with error and unknown argument values.
//
// Note: do not use this option unless absoluately necessary as it should be an uncommon feature.
Expand Down
2 changes: 2 additions & 0 deletions cel/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ type Env struct {

// Program options tied to the environment
progOpts []ProgramOption

locale string
}

// NewEnv creates a program environment configured with the standard library of CEL functions and
Expand Down
14 changes: 14 additions & 0 deletions cel/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package cel
import (
"fmt"

"golang.org/x/text/language"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protodesc"
"google.golang.org/protobuf/reflect/protoreflect"
Expand Down Expand Up @@ -328,6 +329,19 @@ func registerFiles(reg ref.TypeRegistry, files *protoregistry.Files) error {
return err
}

// Locale configures the Env with the given locale tag. If this is not passed,
// then string.format will behave as if the locale was set to en_US.
func Locale(locale string) EnvOption {
return func(e *Env) (*Env, error) {
_, err := language.Parse(locale)
if err != nil {
return nil, fmt.Errorf("failed to parse locale: %w", err)
}
e.locale = locale
return e, nil
}
}

// ProgramOption is a functional interface for configuring evaluation bindings and behaviors.
type ProgramOption func(p *prog) (*prog, error)

Expand Down
Loading

0 comments on commit 83a4e49

Please sign in to comment.