Skip to content

Commit

Permalink
Make the standard environment initialization lazy (#520)
Browse files Browse the repository at this point in the history
  • Loading branch information
TristonianJones committed Mar 25, 2022
1 parent 1287953 commit 829e322
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions cel/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,11 @@ func NewEnv(opts ...EnvOption) (*Env, error) {
// releases. The user provided options can easily re-enable the eager validation as they are
// processed after this default option.
stdOpts := append([]EnvOption{EagerlyValidateDeclarations(false)}, opts...)
return stdEnv.Extend(stdOpts...)
env, err := getStdEnv()
if err != nil {
return nil, err
}
return env.Extend(stdOpts...)
}

// NewCustomEnv creates a custom program environment which is not automatically configured with the
Expand Down Expand Up @@ -531,12 +535,16 @@ func (i *Issues) String() string {
return i.errs.ToDisplayString()
}

var stdEnv *Env

func init() {
var err error
stdEnv, err = NewCustomEnv(StdLib(), EagerlyValidateDeclarations(true))
if err != nil {
panic(err)
}
// getStdEnv lazy initializes the CEL standard environment.
func getStdEnv() (*Env, error) {
stdEnvInit.Do(func() {
stdEnv, stdEnvErr = NewCustomEnv(StdLib(), EagerlyValidateDeclarations(true))
})
return stdEnv, stdEnvErr
}

var (
stdEnvInit sync.Once
stdEnv *Env
stdEnvErr error
)

0 comments on commit 829e322

Please sign in to comment.