Merge pull request #254 from trheyi/main
[add] yao.component.* & fix cloud props api bug
This commit is contained in:
commit
114f09ea32
9 changed files with 137 additions and 39 deletions
|
|
@ -185,7 +185,7 @@ func (dsl *DSL) Xgen() (map[string]interface{}, error) {
|
||||||
for _, cProp := range dsl.CProps {
|
for _, cProp := range dsl.CProps {
|
||||||
err := cProp.Replace(setting, func(cProp component.CloudPropsDSL) interface{} {
|
err := cProp.Replace(setting, func(cProp component.CloudPropsDSL) interface{} {
|
||||||
return map[string]interface{}{
|
return map[string]interface{}{
|
||||||
"api": fmt.Sprintf("/api/__yao/chart/%s/component/%s/%s", dsl.ID, cProp.Xpath, cProp.Name),
|
"api": fmt.Sprintf("/api/__yao/chart/%s%s", dsl.ID, cProp.Path()),
|
||||||
"params": cProp.Query,
|
"params": cProp.Query,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package chart
|
package chart
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net/url"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
@ -66,7 +67,7 @@ func TestProcessSetting(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
data := any.Of(res).MapStr().Dot()
|
data := any.Of(res).MapStr().Dot()
|
||||||
assert.Equal(t, "/api/__yao/chart/dashboard/component/fields.filter.状态.edit.props.xProps/remote", data.Get("fields.filter.状态.edit.props.xProps.remote.api"))
|
assert.Equal(t, "/api/__yao/chart/dashboard/component/fields.filter."+url.QueryEscape("状态")+".edit.props.xProps/remote", data.Get("fields.filter.状态.edit.props.xProps.remote.api"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestProcessXgen(t *testing.T) {
|
func TestProcessXgen(t *testing.T) {
|
||||||
|
|
@ -78,7 +79,7 @@ func TestProcessXgen(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
data := any.Of(res).MapStr().Dot()
|
data := any.Of(res).MapStr().Dot()
|
||||||
assert.Equal(t, "/api/__yao/chart/dashboard/component/fields.filter.状态.edit.props.xProps/remote", data.Get("fields.filter.状态.edit.props.xProps.remote.api"))
|
assert.Equal(t, "/api/__yao/chart/dashboard/component/fields.filter."+url.QueryEscape("状态")+".edit.props.xProps/remote", data.Get("fields.filter.状态.edit.props.xProps.remote.api"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func load(t *testing.T) {
|
func load(t *testing.T) {
|
||||||
|
|
|
||||||
|
|
@ -1,34 +1,109 @@
|
||||||
package component
|
package component
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
jsoniter "github.com/json-iterator/go"
|
||||||
"github.com/yaoapp/gou"
|
"github.com/yaoapp/gou"
|
||||||
|
"github.com/yaoapp/kun/any"
|
||||||
|
"github.com/yaoapp/kun/exception"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Export process
|
// Export process
|
||||||
func exportProcess() {
|
func exportProcess() {
|
||||||
gou.RegisterProcessHandler("yao.component.tagview", processTagView)
|
gou.RegisterProcessHandler("yao.component.selectoptions", processSelectOptions)
|
||||||
gou.RegisterProcessHandler("yao.component.tagedit", processTagEdit)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func processTagView(process *gou.Process) interface{} {
|
func processSelectOptions(process *gou.Process) interface{} {
|
||||||
process.ValidateArgNums(3)
|
process.ValidateArgNums(1)
|
||||||
value := process.Args[1]
|
query := process.ArgsMap(0, map[string]interface{}{})
|
||||||
switch value.(type) {
|
if !query.Has("model") {
|
||||||
|
exception.New("query.model required", 400).Throw()
|
||||||
|
}
|
||||||
|
|
||||||
|
model, ok := query.Get("model").(string)
|
||||||
|
if !ok {
|
||||||
|
exception.New("query.model must be a string", 400).Throw()
|
||||||
|
}
|
||||||
|
|
||||||
|
m := gou.Select(model)
|
||||||
|
|
||||||
|
valueField := query.Get("value")
|
||||||
|
if valueField == nil {
|
||||||
|
valueField = "id"
|
||||||
|
}
|
||||||
|
value, ok := valueField.(string)
|
||||||
|
if !ok {
|
||||||
|
exception.New("query.value must be a string", 400).Throw()
|
||||||
|
}
|
||||||
|
|
||||||
|
labelField := query.Get("label")
|
||||||
|
if labelField == nil {
|
||||||
|
labelField = "name"
|
||||||
|
}
|
||||||
|
label, ok := labelField.(string)
|
||||||
|
if !ok {
|
||||||
|
exception.New("query.label must be a string", 400).Throw()
|
||||||
|
}
|
||||||
|
|
||||||
|
limit := 500
|
||||||
|
if query.Get("limit") != nil {
|
||||||
|
v := any.Of(query.Get("limit"))
|
||||||
|
if v.IsInt() || v.IsString() {
|
||||||
|
limit = v.CInt()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
wheres := []gou.QueryWhere{}
|
||||||
|
switch input := query.Get("wheres").(type) {
|
||||||
case string:
|
case string:
|
||||||
return map[string]interface{}{"label": value, "color": "#FF0000"}
|
where := gou.QueryWhere{}
|
||||||
|
err := jsoniter.Unmarshal([]byte(input), &where)
|
||||||
|
if err != nil {
|
||||||
|
exception.New("query.wheres error %s", 400, err.Error()).Throw()
|
||||||
}
|
}
|
||||||
return map[string]interface{}{}
|
wheres = append(wheres, where)
|
||||||
}
|
break
|
||||||
|
|
||||||
func processTagEdit(process *gou.Process) interface{} {
|
case []string:
|
||||||
process.ValidateArgNums(3)
|
for _, data := range input {
|
||||||
value := process.Args[1]
|
where := gou.QueryWhere{}
|
||||||
switch value.(type) {
|
err := jsoniter.Unmarshal([]byte(data), &where)
|
||||||
case map[string]interface{}:
|
if err != nil {
|
||||||
if val, has := value.(map[string]interface{})["label"]; has {
|
exception.New("query.wheres error %s", 400, err.Error()).Throw()
|
||||||
return val
|
|
||||||
}
|
}
|
||||||
return nil
|
wheres = append(wheres, where)
|
||||||
}
|
}
|
||||||
return value
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
if data, ok := query.Get("wheres").(string); ok {
|
||||||
|
data = strings.TrimSpace(data)
|
||||||
|
if strings.HasPrefix(data, "{") && strings.HasSuffix(data, "}") {
|
||||||
|
data = fmt.Sprintf("[%s]", data)
|
||||||
|
}
|
||||||
|
err := jsoniter.Unmarshal([]byte(data), &wheres)
|
||||||
|
if err != nil {
|
||||||
|
exception.New("query.wheres error %s", 400, err.Error()).Throw()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
rows, err := m.Get(gou.QueryParam{
|
||||||
|
Select: []interface{}{valueField, labelField},
|
||||||
|
Wheres: wheres,
|
||||||
|
Limit: limit,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
exception.New("%s", 500, err.Error()).Throw()
|
||||||
|
}
|
||||||
|
|
||||||
|
res := []map[string]interface{}{}
|
||||||
|
for _, row := range rows {
|
||||||
|
res = append(res, map[string]interface{}{
|
||||||
|
"label": row.Get(label),
|
||||||
|
"value": row.Get(value),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return res
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package component
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
jsoniter "github.com/json-iterator/go"
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
|
@ -14,6 +15,11 @@ func (p PropsDSL) CloudProps(xpath string) (map[string]CloudPropsDSL, error) {
|
||||||
return p.parseCloudProps(xpath, p)
|
return p.parseCloudProps(xpath, p)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Path api path
|
||||||
|
func (cProp CloudPropsDSL) Path() string {
|
||||||
|
return fmt.Sprintf("/component/%s/%s", url.QueryEscape(cProp.Xpath), url.QueryEscape(cProp.Name))
|
||||||
|
}
|
||||||
|
|
||||||
// ExecQuery execute query
|
// ExecQuery execute query
|
||||||
func (cProp CloudPropsDSL) ExecQuery(process *gou.Process, query map[string]interface{}) (interface{}, error) {
|
func (cProp CloudPropsDSL) ExecQuery(process *gou.Process, query map[string]interface{}) (interface{}, error) {
|
||||||
|
|
||||||
|
|
@ -21,6 +27,14 @@ func (cProp CloudPropsDSL) ExecQuery(process *gou.Process, query map[string]inte
|
||||||
query = map[string]interface{}{}
|
query = map[string]interface{}{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// filter array
|
||||||
|
for key, value := range query {
|
||||||
|
if strings.HasSuffix(key, "[]") {
|
||||||
|
query[strings.TrimSuffix(key, "[]")] = value
|
||||||
|
delete(query, key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Process
|
// Process
|
||||||
name := cProp.Process
|
name := cProp.Process
|
||||||
if name == "" {
|
if name == "" {
|
||||||
|
|
|
||||||
|
|
@ -241,7 +241,7 @@ func (dsl *DSL) Xgen() (map[string]interface{}, error) {
|
||||||
for _, cProp := range dsl.CProps {
|
for _, cProp := range dsl.CProps {
|
||||||
err := cProp.Replace(setting, func(cProp component.CloudPropsDSL) interface{} {
|
err := cProp.Replace(setting, func(cProp component.CloudPropsDSL) interface{} {
|
||||||
return map[string]interface{}{
|
return map[string]interface{}{
|
||||||
"api": fmt.Sprintf("/api/__yao/form/%s/component/%s/%s", dsl.ID, cProp.Xpath, cProp.Name),
|
"api": fmt.Sprintf("/api/__yao/form/%s%s", dsl.ID, cProp.Path()),
|
||||||
"params": cProp.Query,
|
"params": cProp.Query,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package form
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/url"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
@ -180,7 +181,7 @@ func TestProcessSetting(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
data := any.Of(res).MapStr().Dot()
|
data := any.Of(res).MapStr().Dot()
|
||||||
assert.Equal(t, "/api/__yao/form/pet/component/fields.form.状态.edit.props.xProps/remote", data.Get("fields.form.状态.edit.props.xProps.remote.api"))
|
assert.Equal(t, "/api/__yao/form/pet/component/fields.form."+url.QueryEscape("状态")+".edit.props.xProps/remote", data.Get("fields.form.状态.edit.props.xProps.remote.api"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestProcessXgen(t *testing.T) {
|
func TestProcessXgen(t *testing.T) {
|
||||||
|
|
@ -194,7 +195,7 @@ func TestProcessXgen(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
data := any.Of(res).MapStr().Dot()
|
data := any.Of(res).MapStr().Dot()
|
||||||
assert.Equal(t, "/api/__yao/form/pet/component/fields.form.状态.edit.props.xProps/remote", data.Get("fields.form.状态.edit.props.xProps.remote.api"))
|
assert.Equal(t, "/api/__yao/form/pet/component/fields.form."+url.QueryEscape("状态")+".edit.props.xProps/remote", data.Get("fields.form.状态.edit.props.xProps.remote.api"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func load(t *testing.T) {
|
func load(t *testing.T) {
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package table
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/url"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
|
@ -42,8 +43,8 @@ func TestAPISetting(t *testing.T) {
|
||||||
data := any.Of(v).MapStr().Dot()
|
data := any.Of(v).MapStr().Dot()
|
||||||
assert.Equal(t, "/api/xiang/import/pet", data.Get("header.preset.import.api.import"))
|
assert.Equal(t, "/api/xiang/import/pet", data.Get("header.preset.import.api.import"))
|
||||||
// assert.Equal(t, "跳转", data.Get("header.preset.import.operation.0.title"))
|
// assert.Equal(t, "跳转", data.Get("header.preset.import.operation.0.title"))
|
||||||
assert.Equal(t, "/api/__yao/table/pet/component/fields.table.入院状态.view.props.xProps/remote", data.Get("fields.table.入院状态.view.props.xProps.remote.api"))
|
assert.Equal(t, "/api/__yao/table/pet/component/fields.table."+url.QueryEscape("入院状态")+".view.props.xProps/remote", data.Get("fields.table.入院状态.view.props.xProps.remote.api"))
|
||||||
assert.Equal(t, "/api/__yao/table/pet/component/fields.table.入院状态.edit.props.xProps/remote", data.Get("fields.table.入院状态.edit.props.xProps.remote.api"))
|
assert.Equal(t, "/api/__yao/table/pet/component/fields.table."+url.QueryEscape("入院状态")+".edit.props.xProps/remote", data.Get("fields.table.入院状态.edit.props.xProps.remote.api"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAPISearch(t *testing.T) {
|
func TestAPISearch(t *testing.T) {
|
||||||
|
|
|
||||||
|
|
@ -2,12 +2,12 @@ package table
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/url"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/yaoapp/gou"
|
"github.com/yaoapp/gou"
|
||||||
"github.com/yaoapp/kun/any"
|
"github.com/yaoapp/kun/any"
|
||||||
"github.com/yaoapp/kun/maps"
|
|
||||||
"github.com/yaoapp/yao/config"
|
"github.com/yaoapp/yao/config"
|
||||||
q "github.com/yaoapp/yao/query"
|
q "github.com/yaoapp/yao/query"
|
||||||
)
|
)
|
||||||
|
|
@ -308,7 +308,13 @@ func TestProcessComponent(t *testing.T) {
|
||||||
"pet",
|
"pet",
|
||||||
"fields.filter.状态.edit.props.xProps",
|
"fields.filter.状态.edit.props.xProps",
|
||||||
"remote",
|
"remote",
|
||||||
map[string]interface{}{"select": []string{"name", "status"}, "limit": 2},
|
map[string]interface{}{
|
||||||
|
"model": "pet",
|
||||||
|
"label": "name",
|
||||||
|
"value": "status",
|
||||||
|
"wheres[]": `{"column":"id","op":"ge","value":0}`,
|
||||||
|
"limit": "2",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
res, err := gou.NewProcess("yao.table.Component", args...).Exec()
|
res, err := gou.NewProcess("yao.table.Component", args...).Exec()
|
||||||
|
|
@ -316,13 +322,13 @@ func TestProcessComponent(t *testing.T) {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
pets, ok := res.([]maps.MapStr)
|
pets, ok := res.([]map[string]interface{})
|
||||||
assert.True(t, ok)
|
assert.True(t, ok)
|
||||||
assert.Equal(t, 2, len(pets))
|
assert.Equal(t, 2, len(pets))
|
||||||
assert.Equal(t, "Cookie", pets[0]["name"])
|
assert.Equal(t, "Cookie", pets[0]["label"])
|
||||||
assert.Equal(t, "checked", pets[0]["status"])
|
assert.Equal(t, "checked", pets[0]["value"])
|
||||||
assert.Equal(t, "Baby", pets[1]["name"])
|
assert.Equal(t, "Baby", pets[1]["label"])
|
||||||
assert.Equal(t, "checked", pets[1]["status"])
|
assert.Equal(t, "checked", pets[1]["value"])
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestProcessComponentError(t *testing.T) {
|
func TestProcessComponentError(t *testing.T) {
|
||||||
|
|
@ -353,8 +359,8 @@ func TestProcessSetting(t *testing.T) {
|
||||||
assert.Equal(t, "/api/xiang/import/pet", data.Get("header.preset.import.api.import"))
|
assert.Equal(t, "/api/xiang/import/pet", data.Get("header.preset.import.api.import"))
|
||||||
assert.Equal(t, "查看详情1", data.Get("header.preset.import.actions[0].title"))
|
assert.Equal(t, "查看详情1", data.Get("header.preset.import.actions[0].title"))
|
||||||
assert.Equal(t, "查看详情2", data.Get("header.preset.import.actions[1].title"))
|
assert.Equal(t, "查看详情2", data.Get("header.preset.import.actions[1].title"))
|
||||||
assert.Equal(t, "/api/__yao/table/pet/component/fields.table.入院状态.view.props.xProps/remote", data.Get("fields.table.入院状态.view.props.xProps.remote.api"))
|
assert.Equal(t, "/api/__yao/table/pet/component/fields.table."+url.QueryEscape("入院状态")+".view.props.xProps/remote", data.Get("fields.table.入院状态.view.props.xProps.remote.api"))
|
||||||
assert.Equal(t, "/api/__yao/table/pet/component/fields.table.入院状态.edit.props.xProps/remote", data.Get("fields.table.入院状态.edit.props.xProps.remote.api"))
|
assert.Equal(t, "/api/__yao/table/pet/component/fields.table."+url.QueryEscape("入院状态")+".edit.props.xProps/remote", data.Get("fields.table.入院状态.edit.props.xProps.remote.api"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestProcessXgen(t *testing.T) {
|
func TestProcessXgen(t *testing.T) {
|
||||||
|
|
@ -371,8 +377,8 @@ func TestProcessXgen(t *testing.T) {
|
||||||
assert.Equal(t, "/api/xiang/import/pet", data.Get("header.preset.import.api.import"))
|
assert.Equal(t, "/api/xiang/import/pet", data.Get("header.preset.import.api.import"))
|
||||||
assert.Equal(t, "查看详情1", data.Get("header.preset.import.actions[0].title"))
|
assert.Equal(t, "查看详情1", data.Get("header.preset.import.actions[0].title"))
|
||||||
assert.Equal(t, "查看详情2", data.Get("header.preset.import.actions[1].title"))
|
assert.Equal(t, "查看详情2", data.Get("header.preset.import.actions[1].title"))
|
||||||
assert.Equal(t, "/api/__yao/table/pet/component/fields.table.入院状态.view.props.xProps/remote", data.Get("fields.table.入院状态.view.props.xProps.remote.api"))
|
assert.Equal(t, "/api/__yao/table/pet/component/fields.table."+url.QueryEscape("入院状态")+".view.props.xProps/remote", data.Get("fields.table.入院状态.view.props.xProps.remote.api"))
|
||||||
assert.Equal(t, "/api/__yao/table/pet/component/fields.table.入院状态.edit.props.xProps/remote", data.Get("fields.table.入院状态.edit.props.xProps.remote.api"))
|
assert.Equal(t, "/api/__yao/table/pet/component/fields.table."+url.QueryEscape("入院状态")+".edit.props.xProps/remote", data.Get("fields.table.入院状态.edit.props.xProps.remote.api"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func load(t *testing.T) {
|
func load(t *testing.T) {
|
||||||
|
|
|
||||||
|
|
@ -272,7 +272,7 @@ func (dsl *DSL) Xgen() (map[string]interface{}, error) {
|
||||||
for _, cProp := range dsl.CProps {
|
for _, cProp := range dsl.CProps {
|
||||||
err := cProp.Replace(setting, func(cProp component.CloudPropsDSL) interface{} {
|
err := cProp.Replace(setting, func(cProp component.CloudPropsDSL) interface{} {
|
||||||
return map[string]interface{}{
|
return map[string]interface{}{
|
||||||
"api": fmt.Sprintf("/api/__yao/table/%s/component/%s/%s", dsl.ID, cProp.Xpath, cProp.Name),
|
"api": fmt.Sprintf("/api/__yao/table/%s%s", dsl.ID, cProp.Path()),
|
||||||
"params": cProp.Query,
|
"params": cProp.Query,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue