增加FixObject,用于隐藏gojsObjectId
This commit is contained in:
parent
5fe831d950
commit
d94fa1e4c8
37
gojs.go
37
gojs.go
@ -7,6 +7,7 @@ import (
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"strings"
|
||||
@ -1216,11 +1217,13 @@ func ExportForDev() string {
|
||||
return strings.Join(imports, "\n")
|
||||
}
|
||||
|
||||
// SetUserPath 设置用户路径,确保在沙盒中从 userPath 中查找文件
|
||||
func (rt *Runtime) SetUserPath(filename string) string {
|
||||
rt.SetGoData("userPath", u.GetAbsFilename(filename))
|
||||
return filename
|
||||
}
|
||||
|
||||
// FixPath 修复文件路径,确保在沙盒中从 userPath 中查找文件
|
||||
func FixPath(vm *goja.Runtime, filename string) string {
|
||||
// 使用 userPath 作为文件系统沙盒
|
||||
rootPath := u.String(vm.GetData("userPath"))
|
||||
@ -1247,3 +1250,37 @@ func FixPath(vm *goja.Runtime, filename string) string {
|
||||
}
|
||||
return filename
|
||||
}
|
||||
|
||||
// FixObject 修复对象,不显示 gojsObjectId
|
||||
var GojsObjectIdKeyRef = reflect.ValueOf("gojsObjectId")
|
||||
|
||||
func FixObject(o any) any {
|
||||
v := reflect.ValueOf(o)
|
||||
if v.Kind() != reflect.Map {
|
||||
return o
|
||||
}
|
||||
|
||||
gojsObjectIdV := v.MapIndex(GojsObjectIdKeyRef)
|
||||
if !gojsObjectIdV.IsValid() {
|
||||
return o
|
||||
}
|
||||
|
||||
// 创建新的map,类型与原map相同
|
||||
newMap := reflect.MakeMapWithSize(v.Type(), v.Len()-1)
|
||||
|
||||
// 遍历原map的所有键值对
|
||||
iter := v.MapRange()
|
||||
for iter.Next() {
|
||||
key := iter.Key()
|
||||
|
||||
// 跳过 gojsObjectId 键
|
||||
if key.Interface() == "gojsObjectId" {
|
||||
continue
|
||||
}
|
||||
|
||||
// 将其他键值对复制到新map中
|
||||
newMap.SetMapIndex(key, iter.Value())
|
||||
}
|
||||
|
||||
return newMap.Interface()
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user