@@ -59,13 +59,15 @@ func (in *Compiler) CompileString(ctx context.Context, src string) (cue.Value, e
5959type CompileConfig struct {
6060 ResolveProviderFunctions bool
6161 PreResolveMutators []func (context.Context , string ) (string , error )
62+ IntraResolveMutators []* withIntraResolveMutation
6263}
6364
6465// NewCompileConfig create new CompileConfig
6566func NewCompileConfig (opts ... CompileOption ) * CompileConfig {
6667 cfg := & CompileConfig {
6768 ResolveProviderFunctions : true ,
6869 PreResolveMutators : nil ,
70+ IntraResolveMutators : make ([]* withIntraResolveMutation , 0 ),
6971 }
7072 for _ , opt := range opts {
7173 opt .ApplyTo (cfg )
@@ -105,6 +107,26 @@ func (in *withExtraData) ApplyTo(cfg *CompileConfig) {
105107 })
106108}
107109
110+ // WithIntraResolveMutation - Allows to add a mutation function to the compile process.
111+ // This runs after the initial parsing and before the resolution of provider functions (CueX).
112+ // This is required when provider function resolution needs access to dynamically read values, such as from Config.
113+ func WithIntraResolveMutation (name string , fn func (ctx context.Context , value cue.Value ) (cue.Value , error )) CompileOption {
114+ return & withIntraResolveMutation {
115+ mutation : fn ,
116+ name : name ,
117+ }
118+ }
119+
120+ type withIntraResolveMutation struct {
121+ name string
122+ mutation func (ctx context.Context , value cue.Value ) (cue.Value , error )
123+ }
124+
125+ // ApplyTo .
126+ func (in * withIntraResolveMutation ) ApplyTo (cfg * CompileConfig ) {
127+ cfg .IntraResolveMutators = append (cfg .IntraResolveMutators , in )
128+ }
129+
108130var _ CompileOption = DisableResolveProviderFunctions {}
109131
110132// DisableResolveProviderFunctions disable ResolveProviderFunctions
@@ -134,6 +156,15 @@ func (in *Compiler) CompileStringWithOptions(ctx context.Context, src string, op
134156 return cue.Value {}, err
135157 }
136158 val := cuecontext .New ().BuildInstance (bi )
159+ for _ , irm := range cfg .IntraResolveMutators {
160+ klog .V (1 ).Infof ("Applying Intra Resolve Mutation: %s" , irm .name )
161+ result , err := irm .mutation (ctx , val )
162+ if err != nil {
163+ klog .V (1 ).ErrorS (err , "Couldn't apply Intra Resolve Mutation: %s" , irm .name )
164+ return val , err
165+ }
166+ val = result
167+ }
137168 if cfg .ResolveProviderFunctions {
138169 return in .Resolve (ctx , val )
139170 }
0 commit comments