diff --git a/pkg/cue/model/instance_test.go b/pkg/cue/model/instance_test.go index 3e3de47f7..8e9396113 100644 --- a/pkg/cue/model/instance_test.go +++ b/pkg/cue/model/instance_test.go @@ -18,8 +18,13 @@ package model import ( "fmt" + "strings" "testing" + "cuelang.org/go/cue/ast" + "cuelang.org/go/cue/parser" + "cuelang.org/go/cue/token" + "cuelang.org/go/cue" "github.com/stretchr/testify/assert" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" @@ -230,3 +235,56 @@ spec: { assert.Nil(t, o) assert.NotNil(t, err) } + +func TestX(t *testing.T) { + f, err := parser.ParseFile("-", `abc: 10`) + fmt.Println(err) + ast.Walk(f, func(node ast.Node) bool { + field, ok := node.(*ast.Field) + if ok { + v := field.Value + //if bin, ok := v.(*ast.BinaryExpr); ok { + // fmt.Printf("%#v\n", bin.X) + // return true + //} + lit, is := v.(*ast.BasicLit) + if is { + + switch lit.Kind { + case token.FLOAT: + + } + field.Value = ast.NewBinExpr(token.OR, &ast.UnaryExpr{X: lit, Op: token.MUL}, ast.NewIdent(strings.ToLower(lit.Kind.String()))) + } + + } + return true + }, nil) + + var r cue.Runtime + + linst, err := r.CompileFile(f) + fmt.Println(err) + fmt.Println(linst.Value()) + w, _ := r.Compile("-", ` +ss: *10|_ +_enable: *bool | string | int +{ +_enable1: _enable1 & bool +if _enable1 != _|_ { +outputs: "service": {} +} +} + +`) + base, _ := NewBase(w.Value()) + p, _ := r.Compile("-", ` +ss: "100" +_enable: "123" +`) + fmt.Println(base.String()) + + pr, _ := NewOther(p.Value()) + base.Unify(pr) + fmt.Println(base.String()) +} diff --git a/pkg/cue/model/sets/utils.go b/pkg/cue/model/sets/utils.go index 39273b15a..fc1488bf0 100644 --- a/pkg/cue/model/sets/utils.go +++ b/pkg/cue/model/sets/utils.go @@ -23,6 +23,8 @@ import ( "strconv" "strings" + "cuelang.org/go/cue/parser" + "cuelang.org/go/cue" "cuelang.org/go/cue/ast" "cuelang.org/go/cue/format" @@ -292,3 +294,27 @@ func OptBytesToString(node ast.Node) ast.Node { }) return node } + +// OpenBaiscLit make that the basicLit can be modified. +func OpenBaiscLit(s string) (string, error) { + f, err := parser.ParseFile("-", s, parser.ParseComments) + if err != nil { + return "", err + } + openBaiscLit(f) + b, err := format.Node(f) + return string(b), err +} + +func openBaiscLit(root ast.Node) { + ast.Walk(root, func(node ast.Node) bool { + field, ok := node.(*ast.Field) + if ok { + v := field.Value + if lit, ok := v.(*ast.BasicLit); ok { + field.Value = ast.NewBinExpr(token.OR, &ast.UnaryExpr{X: lit, Op: token.MUL}, ast.NewIdent("_")) + } + } + return true + }, nil) +} diff --git a/pkg/cue/model/sets/utils_test.go b/pkg/cue/model/sets/utils_test.go index 060fb2dfb..b3a6ac617 100644 --- a/pkg/cue/model/sets/utils_test.go +++ b/pkg/cue/model/sets/utils_test.go @@ -237,3 +237,26 @@ wait: { assert.Equal(t, string(bt), tCase.expectJson) } } + +func TestOpenBasicLit(t *testing.T) { + s, err := OpenBaiscLit(` +a: 10 +a1: int +b: "foo" +b1: string +c: true +c1: bool +top: _ +bottom: _|_ +`) + assert.NilError(t, err) + assert.Equal(t, s, `a: *10 | _ +a1: int +b: *"foo" | _ +b1: string +c: *true | _ +c1: bool +top: _ +bottom: _|_ +`) +} diff --git a/pkg/cue/model/value/value.go b/pkg/cue/model/value/value.go index abf0b0738..e9d2741fb 100644 --- a/pkg/cue/model/value/value.go +++ b/pkg/cue/model/value/value.go @@ -426,6 +426,23 @@ func (val *Value) GetBool(paths ...string) (bool, error) { return v.CueValue().Bool() } +// OpenCompleteValue make that the complete value can be modified. +func (val *Value) OpenCompleteValue() error { + s, err := val.String() + if err != nil { + return err + } + newS, err := sets.OpenBaiscLit(s) + if err != nil { + return err + } + v, err := val.MakeValue(newS) + if err != nil { + return err + } + val.v = v.CueValue() + return nil +} func isDef(s string) bool { return strings.HasPrefix(s, "#") } diff --git a/pkg/cue/model/value/value_test.go b/pkg/cue/model/value/value_test.go index 3c0578c4e..ed1500aec 100644 --- a/pkg/cue/model/value/value_test.go +++ b/pkg/cue/model/value/value_test.go @@ -702,3 +702,18 @@ id: op.context.stepSessionID assert.NilError(t, err) assert.Equal(t, id, "3w9qkdgn5w") } + +func TestOpenCompleteValue(t *testing.T) { + v, err := NewValue(` +x: 10 +y: "100" +`, nil, "") + assert.NilError(t, err) + err = v.OpenCompleteValue() + assert.NilError(t, err) + s, err := v.String() + assert.NilError(t, err) + assert.Equal(t, s, `x: *10 | _ +y: *"100" | _ +`) +} diff --git a/pkg/stdlib/op.cue b/pkg/stdlib/op.cue index cbd8f5739..4d82eb54f 100644 --- a/pkg/stdlib/op.cue +++ b/pkg/stdlib/op.cue @@ -35,7 +35,7 @@ import ( exceptions: [...string] _exceptions: {for c in exceptions {"\(c)": true}} - load: ws.#Load @step(1) + load: oam.#LoadComponets @step(1) components: #Steps & { for name, c in load.value { if _exceptions[name] == _|_ { diff --git a/pkg/workflow/providers/oam/apply.go b/pkg/workflow/providers/oam/apply.go index 7f96466ff..99dfcfd98 100644 --- a/pkg/workflow/providers/oam/apply.go +++ b/pkg/workflow/providers/oam/apply.go @@ -19,6 +19,8 @@ package oam import ( "encoding/json" + "github.com/oam-dev/kubevela/pkg/cue/model/sets" + "github.com/pkg/errors" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" @@ -92,7 +94,11 @@ func (p *provider) LoadComponent(ctx wfContext.Context, v *value.Value, act wfTy if err != nil { return err } - if err := v.FillRaw(string(jt), "value", comp.Name); err != nil { + vs := string(jt) + if s, err := sets.OpenBaiscLit(vs); err == nil { + vs = s + } + if err := v.FillRaw(vs, "value", comp.Name); err != nil { return err } } diff --git a/pkg/workflow/providers/oam/apply_test.go b/pkg/workflow/providers/oam/apply_test.go index 7112c77e0..39e132819 100644 --- a/pkg/workflow/providers/oam/apply_test.go +++ b/pkg/workflow/providers/oam/apply_test.go @@ -19,6 +19,10 @@ package oam import ( "testing" + "k8s.io/apimachinery/pkg/runtime" + + "github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1" + "gotest.tools/assert" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" @@ -77,6 +81,38 @@ metadata: { assert.Equal(t, act.phase, "") } +func TestLoadComponent(t *testing.T) { + p := &provider{ + app: &v1beta1.Application{ + Spec: v1beta1.ApplicationSpec{ + Components: []common.ApplicationComponent{ + { + Name: "c1", + Type: "web", + Properties: runtime.RawExtension{Raw: []byte(`{"image": "busybox"}`)}, + }, + }, + }, + }, + } + v, err := value.NewValue(``, nil, "") + assert.NilError(t, err) + err = p.LoadComponent(nil, v, nil) + assert.NilError(t, err) + s, err := v.String() + assert.NilError(t, err) + assert.Equal(t, s, `value: { + c1: { + name: *"c1" | _ + type: *"web" | _ + properties: { + image: *"busybox" | _ + } + } +} +`) +} + var testHealthy bool func simpleComponentApplyForTest(comp common.ApplicationComponent, _ *value.Value) (*unstructured.Unstructured, []*unstructured.Unstructured, bool, error) {