diff --git a/go.mod b/go.mod index b79edaf..2a47bca 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.23 require ( github.com/go-gl/mathgl v1.1.0 github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 + github.com/mitchellh/mapstructure v1.5.0 github.com/ojrac/opensimplex-go v1.0.2 github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c github.com/ungerik/go3d v0.0.0-20240502073936-1137f6adf7e9 @@ -30,6 +31,6 @@ require ( github.com/kr/pretty v0.1.0 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/unitoftime/cod v0.0.0-20240909130117-f553b1d09d22 // indirect - golang.org/x/sys v0.26.0 // indirect + golang.org/x/sys v0.27.0 // indirect gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect ) diff --git a/go.sum b/go.sum index 33c3125..099699d 100644 --- a/go.sum +++ b/go.sum @@ -19,6 +19,8 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= +github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/niubaoshu/goutils v0.0.0-20180828035119-e8e576f66c2b h1:T7vmCmpGIvqlOOp5SXatALP+HYc/40ZHbxmgy+p+sN0= github.com/niubaoshu/goutils v0.0.0-20180828035119-e8e576f66c2b/go.mod h1:aDwH4aWrEBXw/uvtSvwNwxdtnsx++aP8c8ad4AmlRCg= github.com/ojrac/opensimplex-go v1.0.2 h1:l4vs0D+JCakcu5OV0kJ99oEaWJfggSc9jiLpxaWvSzs= @@ -53,6 +55,8 @@ golang.org/x/image v0.21.0/go.mod h1:vUbsLavqK/W303ZroQQVKQ+Af3Yl6Uz1Ppu5J/cLz78 golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo= golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s= +golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= diff --git a/storage/storage_wasm.go b/storage/storage_wasm.go index 13103fc..505f843 100644 --- a/storage/storage_wasm.go +++ b/storage/storage_wasm.go @@ -5,13 +5,15 @@ package storage import ( "bytes" "encoding/base64" - "encoding/gob" + "encoding/json" "fmt" + "maps" "net/url" "runtime" "runtime/pprof" "syscall/js" + "github.com/mitchellh/mapstructure" "github.com/unitoftime/flow/browser" ) @@ -34,15 +36,13 @@ func GetItem[T any](key string) (*T, error) { } baseString := val.String() - gobDat, err := base64.StdEncoding.DecodeString(baseString) + jsonDat, err := base64.StdEncoding.DecodeString(baseString) if err != nil { return nil, err } var ret T - buf := bytes.NewBuffer(gobDat) - dec := gob.NewDecoder(buf) - err = dec.Decode(&ret) + err = json.Unmarshal(jsonDat, &ret) if err != nil { return nil, err } @@ -51,6 +51,12 @@ func GetItem[T any](key string) (*T, error) { } func GetItemWithDefault[T any](key string, def T) (*T, error) { + defaultMap := make(map[string]any) + err := mapstructure.Decode(def, &defaultMap) + if err != nil { + return nil, err + } + val := localStorage.Call("getItem", key) if val.IsNull() || val.IsUndefined() { return nil, nil @@ -60,30 +66,42 @@ func GetItemWithDefault[T any](key string, def T) (*T, error) { } baseString := val.String() - gobDat, err := base64.StdEncoding.DecodeString(baseString) + jsonDat, err := base64.StdEncoding.DecodeString(baseString) + if err != nil { + return nil, err + } + + decodedMap := make(map[string]any) + err = json.Unmarshal(jsonDat, &decodedMap) if err != nil { return nil, err } - buf := bytes.NewBuffer(gobDat) - dec := gob.NewDecoder(buf) - err = dec.Decode(&def) + maps.Copy(defaultMap, decodedMap) + + var ret T + err = mapstructure.Decode(decodedMap, &ret) if err != nil { return nil, err } - return &def, nil + return &ret, nil } + func SetItem(key string, val any) error { - buf := bytes.Buffer{} - enc := gob.NewEncoder(&buf) - err := enc.Encode(val) + valMap := make(map[string]any) + err := mapstructure.Decode(val, &valMap) + if err != nil { + return err + } + + buf, err := json.Marshal(valMap) if err != nil { return err } - baseString := base64.StdEncoding.EncodeToString(buf.Bytes()) + baseString := base64.StdEncoding.EncodeToString(buf) localStorage.Call("setItem", key, baseString) return nil