Feat: Make applicationComponent can be modified in step (#2304)

* Feat: component properties can be modfiled in step

* Fix: op.#ApplyRemaining
This commit is contained in:
Jian.Li
2021-09-15 13:25:22 +08:00
committed by GitHub
parent fbac93665d
commit 4ff4afde51
8 changed files with 183 additions and 2 deletions
+58
View File
@@ -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())
}
+26
View File
@@ -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)
}
+23
View File
@@ -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: _|_
`)
}
+17
View File
@@ -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, "#")
}
+15
View File
@@ -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" | _
`)
}
+1 -1
View File
@@ -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] == _|_ {
+7 -1
View File
@@ -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
}
}
+36
View File
@@ -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) {