实现基本的匹配条件管理

This commit is contained in:
刘祥超
2020-09-29 11:28:39 +08:00
parent 4fac83b66c
commit 300d2dd288
19 changed files with 613 additions and 23 deletions

View File

@@ -0,0 +1,48 @@
package condutils
import (
"encoding/json"
"github.com/iwind/TeaGo/Tea"
_ "github.com/iwind/TeaGo/bootstrap"
"github.com/iwind/TeaGo/files"
"github.com/iwind/TeaGo/logs"
"path/filepath"
)
type CondJSComponent struct {
Type string `json:"type"`
Name string `json:"name"`
Description string `json:"description"`
Component string `json:"component"`
}
// 读取所有可用的条件
func ReadAllAvailableCondTypes() []*CondJSComponent {
result := []*CondJSComponent{}
dir := Tea.Root + "/web/"
if Tea.IsTesting() {
dir = filepath.Dir(Tea.Root) + "/web"
}
dir += "/public/js/conds/"
jsonFiles := files.NewFile(dir).List()
for _, file := range jsonFiles {
if file.Ext() == ".json" {
data, err := file.ReadAll()
if err != nil {
logs.Println("[COND]read data from json file: " + err.Error())
continue
}
c := []*CondJSComponent{}
err = json.Unmarshal(data, &c)
if err != nil {
logs.Println("[COND]decode json failed: " + err.Error())
continue
}
result = append(result, c...)
}
}
return result
}

View File

@@ -0,0 +1,7 @@
package condutils
import "testing"
func TestReadAllAvailableCondTypes(t *testing.T) {
t.Log(ReadAllAvailableCondTypes())
}