Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions cue/cuex/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,15 @@ func (in *Compiler) CompileString(ctx context.Context, src string) (cue.Value, e
type CompileConfig struct {
ResolveProviderFunctions bool
PreResolveMutators []func(context.Context, string) (string, error)
IntraResolveMutators []*withIntraResolveMutation
}

// NewCompileConfig create new CompileConfig
func NewCompileConfig(opts ...CompileOption) *CompileConfig {
cfg := &CompileConfig{
ResolveProviderFunctions: true,
PreResolveMutators: nil,
IntraResolveMutators: make([]*withIntraResolveMutation, 0),
}
for _, opt := range opts {
opt.ApplyTo(cfg)
Expand Down Expand Up @@ -105,6 +107,26 @@ func (in *withExtraData) ApplyTo(cfg *CompileConfig) {
})
}

// WithIntraResolveMutation - Allows to add a mutation function to the compile process.
// This runs after the initial parsing and before the resolution of provider functions (CueX).
// This is required when provider function resolution needs access to dynamically read values, such as from Config.
func WithIntraResolveMutation(name string, fn func(ctx context.Context, value cue.Value) (cue.Value, error)) CompileOption {
return &withIntraResolveMutation{
mutation: fn,
name: name,
}
}

type withIntraResolveMutation struct {
name string
mutation func(ctx context.Context, value cue.Value) (cue.Value, error)
}

// ApplyTo .
func (in *withIntraResolveMutation) ApplyTo(cfg *CompileConfig) {
cfg.IntraResolveMutators = append(cfg.IntraResolveMutators, in)
}

var _ CompileOption = DisableResolveProviderFunctions{}

// DisableResolveProviderFunctions disable ResolveProviderFunctions
Expand Down Expand Up @@ -134,6 +156,15 @@ func (in *Compiler) CompileStringWithOptions(ctx context.Context, src string, op
return cue.Value{}, err
}
val := cuecontext.New().BuildInstance(bi)
for _, irm := range cfg.IntraResolveMutators {
klog.V(1).Infof("Applying Intra Resolve Mutation: %s", irm.name)
result, err := irm.mutation(ctx, val)
if err != nil {
klog.V(1).ErrorS(err, "Couldn't apply Intra Resolve Mutation: %s", irm.name)
return val, err
}
val = result
}
if cfg.ResolveProviderFunctions {
return in.Resolve(ctx, val)
}
Expand Down
24 changes: 24 additions & 0 deletions cue/cuex/compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package cuex_test
import (
"context"
"fmt"
"github.com/stretchr/testify/assert"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -168,3 +170,25 @@ func TestWithExtraData(t *testing.T) {
})
}
}

func TestWithIntraResolveMutation(t *testing.T) {
addTestParam := func(ctx context.Context, value cue.Value) (cue.Value, error) {
return value.FillPath(cue.ParsePath("test"), "test"), nil
}

tmpl := strings.TrimSpace(`
"key": "value"
`)

compiler := cuex.NewCompilerWithDefaultInternalPackages()
val, err := compiler.CompileStringWithOptions(context.Background(), tmpl, cuex.WithIntraResolveMutation("test", addTestParam))
require.NoError(t, err)

testStr, err := val.LookupPath(cue.ParsePath("test")).String()
require.NoError(t, err)
assert.Equal(t, "test", testStr)

originalVal, err := val.LookupPath(cue.ParsePath("key")).String()
require.NoError(t, err)
assert.Equal(t, "value", originalVal)
}
Loading