Skip to content

Commit

Permalink
refactor: unmarshal plugin info
Browse files Browse the repository at this point in the history
  • Loading branch information
bytemain committed Mar 10, 2024
1 parent 37a961c commit f43ddb9
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 28 deletions.
9 changes: 9 additions & 0 deletions internal/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,12 @@ type EnvKeysHookResultItem struct {
Key string `luai:"key"`
Value string `luai:"value"`
}

type LuaPluginInfo struct {
Name string `luai:"name"`
Author string `luai:"author"`
Version string `luai:"version"`
Description string `luai:"description"`
UpdateUrl string `luai:"updateUrl"`
MinRuntimeVersion string `luai:"minRuntimeVersion"`
}
42 changes: 14 additions & 28 deletions internal/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,8 @@ type LuaPlugin struct {
// plugin filename, this is also alias name, sdk-name
Filename string
// The name defined inside the plugin
Name string
Author string
Version string
Description string
UpdateUrl string
MinRuntimeVersion string

LuaPluginInfo
}

func (l *LuaPlugin) checkValid() error {
Expand Down Expand Up @@ -102,8 +98,6 @@ func (l *LuaPlugin) Available() ([]*Package, error) {
var result []*Package

for _, item := range hookResult {
fmt.Printf("item: %+v\n", item)

mainSdk := &Info{
Name: l.Name,
Version: Version(item.Version),
Expand Down Expand Up @@ -347,28 +341,20 @@ func NewLuaPlugin(content, path string, manager *Manager) (*LuaPlugin, error) {
return nil, err
}

if name := vm.GetTableString(PLUGIN, "name"); name != "" {
source.Name = name
if !isValidName(source.Name) {
return nil, fmt.Errorf("invalid plugin name")
}
} else {
return nil, fmt.Errorf("no plugin name provided")
}
if version := vm.GetTableString(PLUGIN, "version"); version != "" {
source.Version = version
}
if description := vm.GetTableString(PLUGIN, "description"); description != "" {
source.Description = description
}
if updateUrl := vm.GetTableString(PLUGIN, "updateUrl"); updateUrl != "" {
source.UpdateUrl = updateUrl
pluginInfo := LuaPluginInfo{}
err := luai.Unmarshal(PLUGIN, &pluginInfo)
if err != nil {
return nil, err
}
if author := vm.GetTableString(PLUGIN, "author"); author != "" {
source.Author = author

source.LuaPluginInfo = pluginInfo

if !isValidName(source.Name) {
return nil, fmt.Errorf("invalid plugin name")
}
if minRuntimeVersion := vm.GetTableString(PLUGIN, "minRuntimeVersion"); minRuntimeVersion != "" {
source.MinRuntimeVersion = minRuntimeVersion

if source.Name == "" {
return nil, fmt.Errorf("no plugin name provided")
}
return source, nil
}
Expand Down
44 changes: 44 additions & 0 deletions internal/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,50 @@ func TestPlugin(t *testing.T) {
teardownSuite := setupSuite(t)
defer teardownSuite(t)

t.Run("NewLuaPlugin", func(t *testing.T) {
manager := NewSdkManager()
plugin, err := NewLuaPlugin(pluginContent, pluginPath, manager)
if err != nil {
t.Fatal(err)
}

if plugin == nil {
t.Fatalf("expected plugin to be set, got nil")
}

if plugin.Filename != "java" {
t.Errorf("expected filename 'java', got '%s'", plugin.Filename)
}

if plugin.Filepath != pluginPath {
t.Errorf("expected filepath '%s', got '%s'", pluginPath, plugin.Filepath)
}

if plugin.Name != "java" {
t.Errorf("expected name 'java', got '%s'", plugin.Name)
}

if plugin.Version != "0.0.1" {
t.Errorf("expected version '0.0.1', got '%s'", plugin.Version)
}

if plugin.Description != "xxx" {
t.Errorf("expected description 'xxx', got '%s'", plugin.Description)
}

if plugin.Author != "Lihan" {
t.Errorf("expected author 'Lihan', got '%s'", plugin.Author)
}

if plugin.UpdateUrl != "{URL}/sdk.lua" {
t.Errorf("expected update url '{URL}/sdk.lua', got '%s'", plugin.UpdateUrl)
}

if plugin.MinRuntimeVersion != "0.2.2" {
t.Errorf("expected min runtime version '0.2.2', got '%s'", plugin.MinRuntimeVersion)
}
})

t.Run("Available", func(t *testing.T) {
manager := NewSdkManager()
plugin, err := NewLuaPlugin(pluginContent, pluginPath, manager)
Expand Down

0 comments on commit f43ddb9

Please sign in to comment.