Skip to content

Commit eeb20f6

Browse files
authored
feat: Add support for intra resolve compiler mutations (#118)
Signed-off-by: Brian Kane <briankane1@gmail.com>
1 parent ef68242 commit eeb20f6

2 files changed

Lines changed: 55 additions & 0 deletions

File tree

‎cue/cuex/compiler.go‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,15 @@ func (in *Compiler) CompileString(ctx context.Context, src string) (cue.Value, e
5959
type CompileConfig struct {
6060
ResolveProviderFunctions bool
6161
PreResolveMutators []func(context.Context, string) (string, error)
62+
IntraResolveMutators []*withIntraResolveMutation
6263
}
6364

6465
// NewCompileConfig create new CompileConfig
6566
func 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+
108130
var _ 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
}

‎cue/cuex/compiler_test.go‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ package cuex_test
1919
import (
2020
"context"
2121
"fmt"
22+
"github.com/stretchr/testify/assert"
23+
"strings"
2224
"testing"
2325
"time"
2426

@@ -168,3 +170,25 @@ func TestWithExtraData(t *testing.T) {
168170
})
169171
}
170172
}
173+
174+
func TestWithIntraResolveMutation(t *testing.T) {
175+
addTestParam := func(ctx context.Context, value cue.Value) (cue.Value, error) {
176+
return value.FillPath(cue.ParsePath("test"), "test"), nil
177+
}
178+
179+
tmpl := strings.TrimSpace(`
180+
"key": "value"
181+
`)
182+
183+
compiler := cuex.NewCompilerWithDefaultInternalPackages()
184+
val, err := compiler.CompileStringWithOptions(context.Background(), tmpl, cuex.WithIntraResolveMutation("test", addTestParam))
185+
require.NoError(t, err)
186+
187+
testStr, err := val.LookupPath(cue.ParsePath("test")).String()
188+
require.NoError(t, err)
189+
assert.Equal(t, "test", testStr)
190+
191+
originalVal, err := val.LookupPath(cue.ParsePath("key")).String()
192+
require.NoError(t, err)
193+
assert.Equal(t, "value", originalVal)
194+
}

0 commit comments

Comments
 (0)