Skip to content
Draft
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
40 changes: 19 additions & 21 deletions cue/cuex/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ package cuex

import (
"context"
"cuelang.org/go/cue/build"
compilercontext "github.com/kubevela/pkg/cue/cuex/context"
"github.com/kubevela/pkg/cue/cuex/interfaces"
"github.com/kubevela/pkg/cue/cuex/types"
"strings"
"time"

"cuelang.org/go/cue"
"cuelang.org/go/cue/build"
"cuelang.org/go/cue/cuecontext"
"cuelang.org/go/cue/parser"
"github.com/spf13/pflag"
Expand Down Expand Up @@ -55,15 +58,9 @@ func (in *Compiler) CompileString(ctx context.Context, src string) (cue.Value, e
return in.CompileStringWithOptions(ctx, src)
}

// CompileConfig config for running compile process
type CompileConfig struct {
ResolveProviderFunctions bool
PreResolveMutators []func(context.Context, string) (string, error)
}

// NewCompileConfig create new CompileConfig
func NewCompileConfig(opts ...CompileOption) *CompileConfig {
cfg := &CompileConfig{
func NewCompileConfig(opts ...interfaces.CompileOption) *types.CompileConfig {
cfg := &types.CompileConfig{
ResolveProviderFunctions: true,
PreResolveMutators: nil,
}
Expand All @@ -73,13 +70,8 @@ func NewCompileConfig(opts ...CompileOption) *CompileConfig {
return cfg
}

// CompileOption options for compile cue string
type CompileOption interface {
ApplyTo(*CompileConfig)
}

// WithExtraData fill the cue.Value before resolve
func WithExtraData(key string, data interface{}) CompileOption {
func WithExtraData(key string, data interface{}) interfaces.CompileOption {
return &withExtraData{
key: key,
data: data,
Expand All @@ -92,7 +84,7 @@ type withExtraData struct {
}

// ApplyTo .
func (in *withExtraData) ApplyTo(cfg *CompileConfig) {
func (in *withExtraData) ApplyTo(cfg *types.CompileConfig) {
cfg.PreResolveMutators = append(cfg.PreResolveMutators, func(_ context.Context, template string) (string, error) {
val, path := cuecontext.New().CompileString(""), cue.ParsePath(in.key)
if runtime.IsNil(in.data) {
Expand All @@ -105,19 +97,20 @@ func (in *withExtraData) ApplyTo(cfg *CompileConfig) {
})
}

var _ CompileOption = DisableResolveProviderFunctions{}
var _ interfaces.CompileOption = DisableResolveProviderFunctions{}

// DisableResolveProviderFunctions disable ResolveProviderFunctions
type DisableResolveProviderFunctions struct{}

// ApplyTo .
func (in DisableResolveProviderFunctions) ApplyTo(cfg *CompileConfig) {
func (in DisableResolveProviderFunctions) ApplyTo(cfg *types.CompileConfig) {
cfg.ResolveProviderFunctions = false
}

// CompileStringWithOptions compile given cue string with extra options
func (in *Compiler) CompileStringWithOptions(ctx context.Context, src string, opts ...CompileOption) (cue.Value, error) {
func (in *Compiler) CompileStringWithOptions(ctx context.Context, src string, opts ...interfaces.CompileOption) (cue.Value, error) {
var err error
compilercontext.WithCompiler(ctx, in) // set context for nested compilations
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// Store compiler in context for nested compilations; capture returned ctx.
ctx = compilercontext.WithCompiler(ctx, in)

cfg := NewCompileConfig(opts...)
bi := build.NewContext().NewInstance("", nil)
bi.Imports = in.PackageManager.GetImports()
Expand All @@ -133,7 +126,12 @@ func (in *Compiler) CompileStringWithOptions(ctx context.Context, src string, op
if err = bi.AddSyntax(f); err != nil {
return cue.Value{}, err
}
val := cuecontext.New().BuildInstance(bi)
cueCtx := compilercontext.GetCueContext(ctx)
if cueCtx == nil {
cueCtx = cuecontext.New()
compilercontext.WithCueContext(ctx, cueCtx) // set the cue context for nested compilations
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

	ctx = compilercontext.WithCueContext(ctx, cueCtx)

}
val := cueCtx.BuildInstance(bi)
if cfg.ResolveProviderFunctions {
return in.Resolve(ctx, val)
}
Expand Down Expand Up @@ -239,6 +237,6 @@ func CompileString(ctx context.Context, src string) (cue.Value, error) {
}

// CompileStringWithOptions use cuex default compiler to compile cue string with options
func CompileStringWithOptions(ctx context.Context, src string, opts ...CompileOption) (cue.Value, error) {
func CompileStringWithOptions(ctx context.Context, src string, opts ...interfaces.CompileOption) (cue.Value, error) {
return DefaultCompiler.Get().CompileStringWithOptions(ctx, src, opts...)
}
32 changes: 32 additions & 0 deletions cue/cuex/context/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package context

import (
"context"
"cuelang.org/go/cue"
"github.com/kubevela/pkg/cue/cuex/interfaces"
)

type CompilerKey struct{}

Check warning on line 9 in cue/cuex/context/context.go

View workflow job for this annotation

GitHub Actions / unit-test

exported: exported type CompilerKey should have comment or be unexported (revive)

Check failure on line 9 in cue/cuex/context/context.go

View workflow job for this annotation

GitHub Actions / check-diff

exported: exported type CompilerKey should have comment or be unexported (revive)
type CueContextKey struct{}

Check warning on line 10 in cue/cuex/context/context.go

View workflow job for this annotation

GitHub Actions / unit-test

exported: exported type CueContextKey should have comment or be unexported (revive)

Check failure on line 10 in cue/cuex/context/context.go

View workflow job for this annotation

GitHub Actions / check-diff

exported: exported type CueContextKey should have comment or be unexported (revive)

func WithCueContext(ctx context.Context, cueCtx *cue.Context) context.Context {

Check warning on line 12 in cue/cuex/context/context.go

View workflow job for this annotation

GitHub Actions / unit-test

exported: exported function WithCueContext should have comment or be unexported (revive)

Check failure on line 12 in cue/cuex/context/context.go

View workflow job for this annotation

GitHub Actions / check-diff

exported: exported function WithCueContext should have comment or be unexported (revive)
return context.WithValue(ctx, CueContextKey{}, cueCtx)
}

func GetCueContext(ctx context.Context) *cue.Context {

Check warning on line 16 in cue/cuex/context/context.go

View workflow job for this annotation

GitHub Actions / unit-test

exported: exported function GetCueContext should have comment or be unexported (revive)

Check failure on line 16 in cue/cuex/context/context.go

View workflow job for this annotation

GitHub Actions / check-diff

exported: exported function GetCueContext should have comment or be unexported (revive)
if c, ok := ctx.Value(CueContextKey{}).(*cue.Context); ok {
return c
}
return nil
}

func WithCompiler(ctx context.Context, c interfaces.CueXCompiler) context.Context {

Check warning on line 23 in cue/cuex/context/context.go

View workflow job for this annotation

GitHub Actions / unit-test

exported: exported function WithCompiler should have comment or be unexported (revive)

Check failure on line 23 in cue/cuex/context/context.go

View workflow job for this annotation

GitHub Actions / check-diff

exported: exported function WithCompiler should have comment or be unexported (revive)
return context.WithValue(ctx, CompilerKey{}, c)
}

func GetCompiler(ctx context.Context) interfaces.CueXCompiler {

Check warning on line 27 in cue/cuex/context/context.go

View workflow job for this annotation

GitHub Actions / unit-test

exported: exported function GetCompiler should have comment or be unexported (revive)

Check failure on line 27 in cue/cuex/context/context.go

View workflow job for this annotation

GitHub Actions / check-diff

exported: exported function GetCompiler should have comment or be unexported (revive)
if c, ok := ctx.Value(CompilerKey{}).(interfaces.CueXCompiler); ok {
return c
}
return nil
}
17 changes: 17 additions & 0 deletions cue/cuex/interfaces/compiler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package interfaces

import (
"context"
"cuelang.org/go/cue"
"github.com/kubevela/pkg/cue/cuex/types"
)

type CueXCompiler interface {

Check warning on line 9 in cue/cuex/interfaces/compiler.go

View workflow job for this annotation

GitHub Actions / unit-test

exported: exported type CueXCompiler should have comment or be unexported (revive)

Check failure on line 9 in cue/cuex/interfaces/compiler.go

View workflow job for this annotation

GitHub Actions / check-diff

exported: exported type CueXCompiler should have comment or be unexported (revive)
CompileString(ctx context.Context, src string) (cue.Value, error)
CompileStringWithOptions(ctx context.Context, src string, opts ...CompileOption) (cue.Value, error)
}

// CompileOption options for compile cue string
type CompileOption interface {
ApplyTo(config *types.CompileConfig)
}
9 changes: 9 additions & 0 deletions cue/cuex/types/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package types

import "context"

// CompileConfig config for running compile process
type CompileConfig struct {
ResolveProviderFunctions bool
PreResolveMutators []func(context.Context, string) (string, error)
}
Loading