Skip to content

Commit

Permalink
feat: support marshal nil
Browse files Browse the repository at this point in the history
  • Loading branch information
bytemain committed Mar 8, 2024
1 parent 2cae92c commit dc5a407
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 55 deletions.
39 changes: 16 additions & 23 deletions internal/plugin/interfaces.go → internal/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package plugin
package internal

type LuaCheckSum struct {
Sha256 string `luai:"sha256"`
Expand All @@ -23,13 +23,6 @@ type LuaCheckSum struct {
Md5 string `luai:"md5"`
}

type LuaSDKInfo struct {
Name string `luai:"name"`
Version string `luai:"version"`
Path string `luai:"path"`
Note string `luai:"note"`
}

type AvailableHookCtx struct {
RuntimeVersion string `luai:"runtimeVersion"`
}
Expand All @@ -38,7 +31,7 @@ type AvailableHookResultItem struct {
Version string `luai:"version"`
Note string `luai:"note"`

Addition []*LuaSDKInfo `luai:"addition"`
Addition []*Info `luai:"addition"`
}

type PreInstallHookCtx struct {
Expand All @@ -57,34 +50,34 @@ type PreInstallHookResult struct {
Url string `luai:"url"`
LuaCheckSum

Addition []*LuaSDKInfo `luai:"addition"`
Addition []*Info `luai:"addition"`
}

type PreUseHookCtx struct {
RuntimeVersion string `luai:"runtimeVersion"`
Cwd string `luai:"cwd"`
Scope string `luai:"scope"`
Version string `luai:"version"`
PreviousVersion string `luai:"previousVersion"`
InstalledSdks map[string]*LuaSDKInfo `luai:"installedSdks"`
RuntimeVersion string `luai:"runtimeVersion"`
Cwd string `luai:"cwd"`
Scope string `luai:"scope"`
Version string `luai:"version"`
PreviousVersion string `luai:"previousVersion"`
InstalledSdks map[string]*Info `luai:"installedSdks"`
}

type PreUseHookResult struct {
Version string `luai:"version"`
}

type PostInstallHookCtx struct {
RuntimeVersion string `luai:"runtimeVersion"`
RootPath string `luai:"rootPath"`
SdkInfo map[string]*LuaSDKInfo `luai:"sdkInfo"`
RuntimeVersion string `luai:"runtimeVersion"`
RootPath string `luai:"rootPath"`
SdkInfo map[string]*Info `luai:"sdkInfo"`
}

type EnvKeysHookCtx struct {
RuntimeVersion string `luai:"runtimeVersion"`
Main *LuaSDKInfo `luai:"main"`
RuntimeVersion string `luai:"runtimeVersion"`
Main *Info `luai:"main"`
// TODO Will be deprecated in future versions
Path string `luai:"path"`
SdkInfo map[string]*LuaSDKInfo `luai:"sdkInfo"`
Path string `luai:"path"`
SdkInfo map[string]*Info `luai:"sdkInfo"`
}

type EnvKeysHookResultItem struct {
Expand Down
28 changes: 26 additions & 2 deletions internal/luai/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ func Marshal(state *lua.LState, v any) (lua.LValue, error) {
reflected = reflected.Elem()
}

if !reflected.IsValid() {
return lua.LNil, nil
}

switch reflected.Kind() {
case reflect.Struct:
table := state.NewTable()
Expand All @@ -44,12 +48,20 @@ func Marshal(state *lua.LState, v any) (lua.LValue, error) {
if tag == "" {
tag = fieldType.Name
}
logger.Debugf("marshal: field: %v, tag: %v, kind: %s\n", field, tag, field.Kind())

if !field.IsValid() {
logger.Debugf("marshal: field %s is invalid\n", tag)
continue
}

sub, err := Marshal(state, field.Interface())
if err != nil {
return nil, err
}

logger.Debugf("marshal: field: %v, tag: %v, sub: %v, kind: %s\n", field, tag, sub, field.Kind())

table.RawSetString(tag, sub)
}
return table, nil
Expand All @@ -66,7 +78,13 @@ func Marshal(state *lua.LState, v any) (lua.LValue, error) {
case reflect.Array, reflect.Slice:
table := state.NewTable()
for i := 0; i < reflected.Len(); i++ {
value, err := Marshal(state, reflected.Index(i).Interface())
field := reflected.Index(i)
if !field.IsValid() {
logger.Debugf("marshal: field %d is invalid\n", i)
continue
}

value, err := Marshal(state, field.Interface())
if err != nil {
return nil, err
}
Expand All @@ -76,7 +94,13 @@ func Marshal(state *lua.LState, v any) (lua.LValue, error) {
case reflect.Map:
table := state.NewTable()
for _, key := range reflected.MapKeys() {
value, err := Marshal(state, reflected.MapIndex(key).Interface())
field := reflected.MapIndex(key)
if !field.IsValid() {
logger.Debugf("marshal: field %s is invalid\n", key.String())
continue
}

value, err := Marshal(state, field.Interface())
if err != nil {
return nil, err
}
Expand Down
11 changes: 0 additions & 11 deletions internal/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ package internal

import (
"path/filepath"

"github.com/version-fox/vfox/internal/plugin"
)

type Package struct {
Expand All @@ -45,12 +43,3 @@ func (i *Info) storagePath(parentDir string) string {
}
return filepath.Join(parentDir, i.Name+"-"+string(i.Version))
}

func NewLuaSDKInfo(info *Info) *plugin.LuaSDKInfo {
return &plugin.LuaSDKInfo{
Name: info.Name,
Version: string(info.Version),
Path: info.Path,
Note: info.Note,
}
}
37 changes: 18 additions & 19 deletions internal/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"github.com/version-fox/vfox/internal/env"
"github.com/version-fox/vfox/internal/logger"
"github.com/version-fox/vfox/internal/luai"
"github.com/version-fox/vfox/internal/plugin"
lua "github.com/yuin/gopher-lua"
)

Expand Down Expand Up @@ -76,7 +75,7 @@ func (l *LuaPlugin) Close() {

func (l *LuaPlugin) Available() ([]*Package, error) {
L := l.vm.Instance
ctxTable, err := luai.Marshal(L, plugin.AvailableHookCtx{
ctxTable, err := luai.Marshal(L, AvailableHookCtx{
RuntimeVersion: RuntimeVersion,
})

Expand All @@ -94,7 +93,7 @@ func (l *LuaPlugin) Available() ([]*Package, error) {
return []*Package{}, nil
}

hookResult := []plugin.AvailableHookResultItem{}
hookResult := []AvailableHookResultItem{}
err = luai.Unmarshal(table, &hookResult)
if err != nil {
return nil, errors.New("failed to unmarshal the return value: " + err.Error())
Expand Down Expand Up @@ -136,7 +135,7 @@ func (l *LuaPlugin) Available() ([]*Package, error) {
}

func (l *LuaPlugin) Checksum(table *lua.LTable) *Checksum {
luaCheckSum := plugin.LuaCheckSum{}
luaCheckSum := LuaCheckSum{}
err := luai.Unmarshal(table, luaCheckSum)
if err != nil {
// todo: logger error
Expand Down Expand Up @@ -166,7 +165,7 @@ func (l *LuaPlugin) Checksum(table *lua.LTable) *Checksum {

func (l *LuaPlugin) PreInstall(version Version) (*Package, error) {
L := l.vm.Instance
ctxTable, err := luai.Marshal(L, plugin.PreInstallHookCtx{
ctxTable, err := luai.Marshal(L, PreInstallHookCtx{
Version: string(version),
RuntimeVersion: RuntimeVersion,
})
Expand Down Expand Up @@ -221,7 +220,7 @@ func (l *LuaPlugin) PreInstall(version Version) (*Package, error) {
}

func (l *LuaPlugin) parseInfo(table *lua.LTable) (*Info, error) {
info := &plugin.LuaSDKInfo{}
info := &Info{}
err := luai.Unmarshal(table, info)
if err != nil {
return nil, err
Expand Down Expand Up @@ -249,14 +248,14 @@ func (l *LuaPlugin) PostInstall(rootPath string, sdks []*Info) error {
return nil
}

ctx := &plugin.PostInstallHookCtx{
ctx := &PostInstallHookCtx{
RuntimeVersion: RuntimeVersion,
RootPath: rootPath,
SdkInfo: make(map[string]*plugin.LuaSDKInfo),
SdkInfo: make(map[string]*Info),
}

for _, v := range sdks {
ctx.SdkInfo[v.Name] = NewLuaSDKInfo(v)
ctx.SdkInfo[v.Name] = v
}

ctxTable, err := luai.Marshal(L, ctx)
Expand All @@ -275,16 +274,16 @@ func (l *LuaPlugin) EnvKeys(sdkPackage *Package) (env.Envs, error) {
L := l.vm.Instance
mainInfo := sdkPackage.Main

ctx := &plugin.EnvKeysHookCtx{
ctx := &EnvKeysHookCtx{
// TODO Will be deprecated in future versions
Path: mainInfo.Path,
RuntimeVersion: RuntimeVersion,
Main: NewLuaSDKInfo(mainInfo),
SdkInfo: make(map[string]*plugin.LuaSDKInfo),
Main: (mainInfo),
SdkInfo: make(map[string]*Info),
}

for _, v := range sdkPackage.Additions {
ctx.SdkInfo[v.Name] = NewLuaSDKInfo(v)
ctx.SdkInfo[v.Name] = v
}

ctxTable, err := luai.Marshal(L, ctx)
Expand All @@ -304,7 +303,7 @@ func (l *LuaPlugin) EnvKeys(sdkPackage *Package) (env.Envs, error) {

envKeys := make(env.Envs)

items := []*plugin.EnvKeysHookResultItem{}
items := []*EnvKeysHookResultItem{}
err = luai.Unmarshal(table, &items)
if err != nil {
return nil, err
Expand All @@ -328,18 +327,18 @@ func (l *LuaPlugin) HasFunction(name string) bool {
func (l *LuaPlugin) PreUse(version Version, previousVersion Version, scope UseScope, cwd string, installedSdks []*Package) (Version, error) {
L := l.vm.Instance

ctx := plugin.PreUseHookCtx{
ctx := PreUseHookCtx{
RuntimeVersion: RuntimeVersion,
Cwd: cwd,
Scope: scope.String(),
Version: string(version),
PreviousVersion: string(previousVersion),
InstalledSdks: make(map[string]*plugin.LuaSDKInfo),
InstalledSdks: make(map[string]*Info),
}

for _, v := range installedSdks {
lSdk := NewLuaSDKInfo(v.Main)
ctx.InstalledSdks[lSdk.Version] = lSdk
lSdk := v.Main
ctx.InstalledSdks[string(lSdk.Version)] = lSdk
}

logger.Debugf("PreUseHookCtx: %+v", ctx)
Expand All @@ -363,7 +362,7 @@ func (l *LuaPlugin) PreUse(version Version, previousVersion Version, scope UseSc
return "", nil
}

result := &plugin.PreUseHookResult{}
result := &PreUseHookResult{}

if err := luai.Unmarshal(table, result); err != nil {
return "", err
Expand Down
13 changes: 13 additions & 0 deletions internal/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,26 @@ import (
"testing"

_ "embed"

"github.com/version-fox/vfox/internal/logger"
)

//go:embed testdata/plugins/java.lua
var pluginContent string
var pluginPath = "testdata/plugins/java.lua"

func setupSuite(tb testing.TB) func(tb testing.TB) {
logger.SetLevel(logger.DebugLevel)

return func(tb testing.TB) {
logger.SetLevel(logger.InfoLevel)
}
}

func TestPlugin(t *testing.T) {
teardownSuite := setupSuite(t)
defer teardownSuite(t)

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

0 comments on commit dc5a407

Please sign in to comment.