New function for searching and importing plugin
This commit is contained in:
parent
4e3f5cfbc6
commit
33b3e1fc29
@ -4,10 +4,6 @@
|
|||||||
// operator-plugin.go
|
// operator-plugin.go
|
||||||
package expr
|
package expr
|
||||||
|
|
||||||
import (
|
|
||||||
"io"
|
|
||||||
)
|
|
||||||
|
|
||||||
//-------- plugin term
|
//-------- plugin term
|
||||||
|
|
||||||
func newPluginTerm(tk *Token) (inst *term) {
|
func newPluginTerm(tk *Token) (inst *term) {
|
||||||
@ -22,31 +18,13 @@ func newPluginTerm(tk *Token) (inst *term) {
|
|||||||
|
|
||||||
func evalPlugin(ctx ExprContext, opTerm *term) (v any, err error) {
|
func evalPlugin(ctx ExprContext, opTerm *term) (v any, err error) {
|
||||||
var childValue any
|
var childValue any
|
||||||
var moduleSpec any
|
var count int
|
||||||
|
|
||||||
if childValue, err = opTerm.evalPrefix(ctx); err != nil {
|
if childValue, err = opTerm.evalPrefix(ctx); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
dirList := buildSearchDirList("plugin", ENV_EXPR_PLUGIN_PATH)
|
if count, err = importPluginFromSearchPath(childValue); err == nil {
|
||||||
count := 0
|
|
||||||
it := NewAnyIterator(childValue)
|
|
||||||
for moduleSpec, err = it.Next(); err == nil; moduleSpec, err = it.Next() {
|
|
||||||
if module, ok := moduleSpec.(string); ok {
|
|
||||||
if err = importPlugin(dirList, module); err != nil {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
count++
|
|
||||||
} else {
|
|
||||||
err = opTerm.Errorf("expected string as item nr %d, got %s", it.Index()+1, TypeName(moduleSpec))
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if err == io.EOF {
|
|
||||||
err = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if err == nil {
|
|
||||||
v = int64(count)
|
v = int64(count)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
24
plugins.go
24
plugins.go
@ -6,6 +6,7 @@ package expr
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"plugin"
|
"plugin"
|
||||||
"strings"
|
"strings"
|
||||||
@ -90,6 +91,29 @@ func importPlugin( /*ctx ExprContext,*/ dirList []string, name string) (err erro
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func importPluginFromSearchPath(name any) (count int, err error) {
|
||||||
|
var moduleSpec any
|
||||||
|
|
||||||
|
dirList := buildSearchDirList("plugin", ENV_EXPR_PLUGIN_PATH)
|
||||||
|
count = 0
|
||||||
|
it := NewAnyIterator(name)
|
||||||
|
for moduleSpec, err = it.Next(); err == nil; moduleSpec, err = it.Next() {
|
||||||
|
if module, ok := moduleSpec.(string); ok {
|
||||||
|
if err = importPlugin(dirList, module); err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
count++
|
||||||
|
} else {
|
||||||
|
err = fmt.Errorf("expected string as item nr %d, got %s", it.Index()+1, TypeName(moduleSpec))
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err == io.EOF {
|
||||||
|
err = nil
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func loadModules(dirList []string, moduleNames []string) (err error) {
|
func loadModules(dirList []string, moduleNames []string) (err error) {
|
||||||
for _, name := range moduleNames {
|
for _, name := range moduleNames {
|
||||||
if err1 := importPlugin(dirList, name); err1 != nil {
|
if err1 := importPlugin(dirList, name); err1 != nil {
|
||||||
|
@ -8,9 +8,16 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func _TestImportPlugin(t *testing.T) {
|
func TestImportPlugin(t *testing.T) {
|
||||||
if err := importPlugin([]string{"test-resources"}, "json"); err != nil {
|
t.Setenv("PLUGINS", "${HOME}/go/src/git.portale-stac.it/go")
|
||||||
t.Errorf("importPlugin() failed: %v", err)
|
t.Setenv("EXPR_PLUGIN_PATH","${PLUGINS}/expr-json-plugin:${PLUGINS}/expr-csv-plugin")
|
||||||
|
|
||||||
|
gotCount, gotErr := importPluginFromSearchPath("json")
|
||||||
|
if gotCount != 1 {
|
||||||
|
t.Errorf("Import count: got=%d, want=1", gotCount)
|
||||||
|
}
|
||||||
|
if gotErr != nil {
|
||||||
|
t.Errorf("importPlugin() failed: %v", gotErr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user