[add] migrate connector

This commit is contained in:
Max 2023-02-02 15:12:23 +08:00
parent a9f304060b
commit 44bbe5b383
3 changed files with 20 additions and 30 deletions

View file

@ -9,7 +9,7 @@ COMMIT := $(shell git log | head -n 1 | awk '{print substr($$2, 0, 12)}')
NOW := $(shell date +"%FT%T%z") NOW := $(shell date +"%FT%T%z")
# ROOT_DIR := $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST)))) # ROOT_DIR := $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))
TESTFOLDER := $(shell $(GO) list ./... | grep -E 'api|model|flow|script|fs|i18n' | grep -vE 'examples|tests*|config|widgets') TESTFOLDER := $(shell $(GO) list ./... | grep -E 'api|model|flow|script|fs|i18n|connector' | grep -vE 'examples|tests*|config|widgets')
TESTTAGS ?= "" TESTTAGS ?= ""
# TESTWIDGETS := $(shell $(GO) list ./widgets/...) # TESTWIDGETS := $(shell $(GO) list ./widgets/...)

View file

@ -1,36 +1,20 @@
package connector package connector
import ( import (
"fmt" "github.com/yaoapp/gou/application"
"path/filepath"
"github.com/yaoapp/gou/connector" "github.com/yaoapp/gou/connector"
"github.com/yaoapp/kun/log"
"github.com/yaoapp/yao/config" "github.com/yaoapp/yao/config"
"github.com/yaoapp/yao/share" "github.com/yaoapp/yao/share"
) )
// Load load store // Load load store
func Load(cfg config.Config) error { func Load(cfg config.Config) error {
var root = filepath.Join(cfg.Root, "connectors") exts := []string{"*.yao", "*.json", "*.jsonc"}
return LoadFrom(root, "") return application.App.Walk("connectors", func(root, file string, isdir bool) error {
} if isdir {
return nil
// LoadFrom load from dir
func LoadFrom(dir string, prefix string) error {
if share.DirNotExists(dir) {
return fmt.Errorf("%s does not exists", dir)
}
err := share.Walk(dir, ".json", func(root, filename string) {
name := prefix + share.SpecName(root, filename)
content := share.ReadFile(filename)
_, err := connector.Load(string(content), name)
if err != nil {
log.With(log.F{"root": root, "file": filename}).Error(err.Error())
} }
}) _, err := connector.Load(file, share.ID(root, file))
return err
return err }, exts...)
} }

View file

@ -6,18 +6,24 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/yaoapp/gou/connector" "github.com/yaoapp/gou/connector"
"github.com/yaoapp/yao/config" "github.com/yaoapp/yao/config"
"github.com/yaoapp/yao/test"
) )
func TestLoad(t *testing.T) { func TestLoad(t *testing.T) {
test.Prepare(t, config.Conf)
defer test.Clean()
Load(config.Conf) Load(config.Conf)
LoadFrom("not a path", "404.")
check(t) check(t)
} }
func check(t *testing.T) { func check(t *testing.T) {
keys := []string{} ids := map[string]bool{}
for key := range connector.Connectors { for id := range connector.Connectors {
keys = append(keys, key) ids[id] = true
} }
assert.Equal(t, 4, len(keys)) assert.True(t, ids["mongo"])
assert.True(t, ids["mysql"])
assert.True(t, ids["redis"])
assert.True(t, ids["sqlite"])
} }