Skip to content

Commit

Permalink
feat(meta): return plugin type for list
Browse files Browse the repository at this point in the history
Signed-off-by: Jiyong Huang <[email protected]>
  • Loading branch information
ngjaying committed Aug 20, 2024
1 parent 956aa43 commit ddd5987
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 24 deletions.
26 changes: 18 additions & 8 deletions internal/meta/sinkMeta.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 EMQ Technologies Co., Ltd.
// Copyright 2021-2024 EMQ Technologies Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -20,8 +20,10 @@ import (
"path"
"strings"

"github.com/lf-edge/ekuiper/internal/binder/io"
"github.com/lf-edge/ekuiper/internal/conf"
"github.com/lf-edge/ekuiper/internal/pkg/filex"
"github.com/lf-edge/ekuiper/internal/plugin"
"github.com/lf-edge/ekuiper/pkg/cast"
"github.com/lf-edge/ekuiper/pkg/errorx"
)
Expand Down Expand Up @@ -104,6 +106,7 @@ type (
Libs []string `json:"libs"`
Fields []field `json:"properties"`
Node interface{} `json:"node"`
Type string `json:"type,omitempty"`
}
)

Expand Down Expand Up @@ -280,31 +283,38 @@ func GetSinkMeta(pluginName, language string) (s *uiSink, err error) {
if !ok || data == nil {
return nil, fmt.Errorf(`%s%s`, getMsg(language, sink, "not_found_plugin"), pluginName)
}
t, _, _ := io.GetSinkPlugin(pluginName)
data.Type = plugin.ExtensionTypes[t]
return data, nil
}

type pluginfo struct {
Name string `json:"name"`
About *about `json:"about"`
Type string `json:"type,omitempty"`
}

func GetSinks() (sinks []*pluginfo) {
sinkMeta := gSinkmetadata
for fileName, v := range sinkMeta {
node := new(pluginfo)
node.Name = strings.TrimSuffix(fileName, `.json`)
node.About = v.About
name := strings.TrimSuffix(fileName, `.json`)
t, _, _ := io.GetSinkPlugin(name)
n := &pluginfo{
Name: name,
About: v.About,
Type: plugin.ExtensionTypes[t],
}
i := 0
for ; i < len(sinks); i++ {
if node.Name <= sinks[i].Name {
sinks = append(sinks, node)
if n.Name <= sinks[i].Name {
sinks = append(sinks, n)
copy(sinks[i+1:], sinks[i:])
sinks[i] = node
sinks[i] = n
break
}
}
if len(sinks) == i {
sinks = append(sinks, node)
sinks = append(sinks, n)
}
}
return sinks
Expand Down
48 changes: 47 additions & 1 deletion internal/meta/sinkMeta_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 EMQ Technologies Co., Ltd.
// Copyright 2021-2024 EMQ Technologies Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,7 @@ package meta
import (
"fmt"
"path"
"sort"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -51,3 +52,48 @@ func TestMetaError(t *testing.T) {
require.True(t, ok)
require.Equal(t, errorx.ConfKeyError, ewc.Code())
}

func TestGetSinkMeta(t *testing.T) {
commonAbout := &about{Installed: true}
gSinkmetadata = map[string]*uiSink{
"mqtt.json": {
About: commonAbout,
},
}
expected := &uiSink{
About: commonAbout,
Type: "internal",
}
actual, err := GetSinkMeta("mqtt", "")
require.NoError(t, err)
require.Equal(t, expected, actual)
}

func TestGetSinks(t *testing.T) {
commonAbout := &about{Installed: true}
gSinkmetadata = map[string]*uiSink{
"mqtt.json": {
About: commonAbout,
},
"print.json": {
About: commonAbout,
},
}
expected := []*pluginfo{
{
Name: "mqtt",
About: commonAbout,
Type: "internal",
},
{
Name: "print",
About: commonAbout,
Type: "none",
},
}
sinks := GetSinks()
sort.SliceStable(sinks, func(i, j int) bool {
return sinks[i].Name < sinks[j].Name
})
require.Equal(t, expected, sinks)
}
31 changes: 17 additions & 14 deletions internal/meta/sourceMeta.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2022 EMQ Technologies Co., Ltd.
// Copyright 2021-2024 EMQ Technologies Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -21,8 +21,10 @@ import (
"strings"
"sync"

"github.com/lf-edge/ekuiper/internal/binder/io"
"github.com/lf-edge/ekuiper/internal/conf"
"github.com/lf-edge/ekuiper/internal/pkg/filex"
"github.com/lf-edge/ekuiper/internal/plugin"
"github.com/lf-edge/ekuiper/pkg/ast"
"github.com/lf-edge/ekuiper/pkg/errorx"
)
Expand All @@ -41,6 +43,7 @@ type (
DataSource interface{} `json:"dataSource,omitempty"`
ConfKeys map[string][]field `json:"properties"`
Node interface{} `json:"node"`
Type string `json:"type,omitempty"`
isScan bool
isLookup bool
}
Expand Down Expand Up @@ -200,8 +203,10 @@ func GetSourceMeta(sourceName, language string) (ptrSourceProperty *uiSource, er
return nil, fmt.Errorf(`%s%s`, getMsg(language, source, "not_found_plugin"), sourceName)
}

ui := new(uiSource)
ui := &uiSource{}
*ui = *v
t, _, _ := io.GetSourcePlugin(sourceName)
ui.Type = plugin.ExtensionTypes[t]
return ui, nil
}

Expand All @@ -215,26 +220,24 @@ func GetSourcesPlugins(kind string) (sources []*pluginfo) {
} else if kind == ast.StreamKindScan && !v.isScan {
continue
}
node := new(pluginfo)
node.Name = strings.TrimSuffix(fileName, `.json`)
if nil == v {
continue
}
if nil == v.About {
continue
name := strings.TrimSuffix(fileName, `.json`)
t, _, _ := io.GetSourcePlugin(name)
n := &pluginfo{
Name: name,
About: v.About,
Type: plugin.ExtensionTypes[t],
}
node.About = v.About
i := 0
for ; i < len(sources); i++ {
if node.Name <= sources[i].Name {
sources = append(sources, node)
if n.Name <= sources[i].Name {
sources = append(sources, n)
copy(sources[i+1:], sources[i:])
sources[i] = node
sources[i] = n
break
}
}
if len(sources) == i {
sources = append(sources, node)
sources = append(sources, n)
}
}
return sources
Expand Down
43 changes: 42 additions & 1 deletion internal/meta/sourceMeta_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2022 EMQ Technologies Co., Ltd.
// Copyright 2021-2024 EMQ Technologies Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -17,8 +17,10 @@ package meta
import (
"fmt"
"path"
"sort"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/lf-edge/ekuiper/internal/conf"
Expand Down Expand Up @@ -68,6 +70,8 @@ func TestGetSqlSourceMeta(t *testing.T) {
t.Error(err)
}

assert.Equal(t, "internal", showMeta.Type)

fields := showMeta.ConfKeys["default"]

for _, value := range fields {
Expand Down Expand Up @@ -111,3 +115,40 @@ func TestSourceMeta(t *testing.T) {
require.True(t, ok)
require.Equal(t, errorx.ConfKeyError, ewc.Code())
}

func TestGetSource(t *testing.T) {
commonAbout := &about{Installed: true}
gSourcemetadata = map[string]*uiSource{
"mqtt.json": {
About: commonAbout,
},
"random.json": {
About: commonAbout,
},
"pyjson.json": {
About: commonAbout,
},
}
expected := []*pluginfo{
{
Name: "mqtt",
About: commonAbout,
Type: "internal",
},
{
Name: "pyjson",
About: commonAbout,
Type: "none",
},
{
Name: "random",
About: commonAbout,
Type: "none",
},
}
sources := GetSourcesPlugins("stream")
sort.SliceStable(sources, func(i, j int) bool {
return sources[i].Name < sources[j].Name
})
require.Equal(t, expected, sources)
}
4 changes: 4 additions & 0 deletions internal/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,7 @@ const (
WASM_EXTENSION
JS_EXTENSION
)

var ExtensionTypes = []string{
"none", "internal", "native", "portable", "service", "wasm", "js",
}

0 comments on commit ddd5987

Please sign in to comment.