Loading server/core_notification.go +14 −0 Original line number Diff line number Diff line Loading @@ -147,6 +147,20 @@ LIMIT $4 if err := gob.NewEncoder(cursorBuf).Encode(newCursor); err != nil { n.logger.Error("Could not create new cursor.", zap.Error(err)) } return notifications, cursorBuf.Bytes(), nil } else { if len(cursor) != 0 { return notifications, cursor, nil } } newCursor := ¬ificationResumableCursor{ Expiry: expiryNow, NotificationID: make([]byte, 0), } if err := gob.NewEncoder(cursorBuf).Encode(newCursor); err != nil { n.logger.Error("Could not create new cursor.", zap.Error(err)) } return notifications, cursorBuf.Bytes(), nil Loading server/runtime_lua_context.go +1 −0 Original line number Diff line number Diff line Loading @@ -75,6 +75,7 @@ func NewLuaContext(l *lua.LState, env *lua.LTable, mode ExecutionMode, uid uuid. func ConvertMap(l *lua.LState, data map[string]interface{}) *lua.LTable { lt := l.NewTable() for k, v := range data { lt.RawSetString(k, convertValue(l, v)) } Loading server/runtime_nakama_module.go +19 −4 Original line number Diff line number Diff line Loading @@ -588,7 +588,17 @@ func (n *NakamaModule) storageFetch(l *lua.LState) int { v.UserId = []byte(uid.String()) } vm := structs.Map(v) lv.RawSetInt(i+1, convertValue(l, vm)) valueMap := make(map[string]interface{}) err = json.Unmarshal(v.Value, &valueMap) if err != nil { l.RaiseError(fmt.Sprintf("failed to convert value to json: %s", err.Error())) return 0 } lt := ConvertMap(l, vm) lt.RawSetString("Value", ConvertMap(l, valueMap)) lv.RawSetInt(i+1, lt) } l.Push(lv) Loading Loading @@ -660,11 +670,16 @@ func (n *NakamaModule) storageWrite(l *lua.LState) int { l.ArgError(1, "expects a value in each key") return 0 } else { if vs, ok := v.(string); !ok { l.ArgError(1, "value must be a string") if vs, ok := v.(map[string]interface{}); !ok { l.ArgError(1, "value must be a table") return 0 } else { value = []byte(vs) dataJson, err := json.Marshal(vs) if err != nil { l.RaiseError("could not convert value to JSON: %v", err.Error()) return 0 } value = dataJson } } var userID []byte Loading server/runtime_nakamax_module.go +8 −9 Original line number Diff line number Diff line Loading @@ -24,10 +24,11 @@ import ( "encoding/base64" "encoding/hex" "github.com/satori/go.uuid" "github.com/yuin/gopher-lua" "go.uber.org/zap" "encoding/hex" ) type NakamaxModule struct { Loading Loading @@ -131,17 +132,16 @@ func (nx *NakamaxModule) httpRequest(l *lua.LState) int { } func (nx *NakamaxModule) jsonEncode(l *lua.LState) int { // TODO allow top-level arrays or primitives? jsonTable := l.CheckTable(1) jsonTable := l.Get(1) if jsonTable == nil { l.ArgError(1, "Expects a table to encode") l.ArgError(1, "Expects a non-nil value to encode") return 0 } jsonData := ConvertLuaTable(jsonTable) jsonData := convertLuaValue(jsonTable) jsonBytes, err := json.Marshal(jsonData) if err != nil { l.ArgError(1, "Error encoding to JSON") l.RaiseError("Error encoding to JSON: %v", err.Error()) return 0 } Loading @@ -156,14 +156,13 @@ func (nx *NakamaxModule) jsonDecode(l *lua.LState) int { return 0 } // TODO allow top-level arrays or primitives? var jsonData map[string]interface{} var jsonData interface{} if err := json.Unmarshal([]byte(jsonString), &jsonData); err != nil { l.RaiseError("Not a valid JSON string: %v", err.Error()) return 0 } l.Push(ConvertMap(l, jsonData)) l.Push(convertValue(l, jsonData)) return 1 } Loading tests/modules/e2e_runtime.lua +24 −0 Original line number Diff line number Diff line Loading @@ -139,12 +139,36 @@ do assert(object.hello == "world", "'object.hello' must equal 'world'") end -- json_decode_array do local object = nx.json_decode('[{"hello": "world"}, {"hello": "world"}]') assert(#object == 2) end -- json_decode_primitive do local object = nx.json_decode('"hello"') assert(object == "hello") end -- json_encode do local json = nx.json_encode({["id"] = "blah"}) assert(json == '{"id":"blah"}', '"json" must equal "{"id":"blah"}"') end -- json_encode_array do local json = nx.json_encode({{["id"] = "blah"},{["id"] = "blah"}}) assert(json == '[{"id":"blah"},{"id":"blah"}]', '"json" must equal "[{"id":"blah"}",{"id":"blah"}]') end -- json_encode_primitive do local json = nx.json_encode("hello") assert(json == '"hello"') end -- base64_encode_decode do local objectEncode = nx.base64_encode('{"hello": "world"}') Loading Loading
server/core_notification.go +14 −0 Original line number Diff line number Diff line Loading @@ -147,6 +147,20 @@ LIMIT $4 if err := gob.NewEncoder(cursorBuf).Encode(newCursor); err != nil { n.logger.Error("Could not create new cursor.", zap.Error(err)) } return notifications, cursorBuf.Bytes(), nil } else { if len(cursor) != 0 { return notifications, cursor, nil } } newCursor := ¬ificationResumableCursor{ Expiry: expiryNow, NotificationID: make([]byte, 0), } if err := gob.NewEncoder(cursorBuf).Encode(newCursor); err != nil { n.logger.Error("Could not create new cursor.", zap.Error(err)) } return notifications, cursorBuf.Bytes(), nil Loading
server/runtime_lua_context.go +1 −0 Original line number Diff line number Diff line Loading @@ -75,6 +75,7 @@ func NewLuaContext(l *lua.LState, env *lua.LTable, mode ExecutionMode, uid uuid. func ConvertMap(l *lua.LState, data map[string]interface{}) *lua.LTable { lt := l.NewTable() for k, v := range data { lt.RawSetString(k, convertValue(l, v)) } Loading
server/runtime_nakama_module.go +19 −4 Original line number Diff line number Diff line Loading @@ -588,7 +588,17 @@ func (n *NakamaModule) storageFetch(l *lua.LState) int { v.UserId = []byte(uid.String()) } vm := structs.Map(v) lv.RawSetInt(i+1, convertValue(l, vm)) valueMap := make(map[string]interface{}) err = json.Unmarshal(v.Value, &valueMap) if err != nil { l.RaiseError(fmt.Sprintf("failed to convert value to json: %s", err.Error())) return 0 } lt := ConvertMap(l, vm) lt.RawSetString("Value", ConvertMap(l, valueMap)) lv.RawSetInt(i+1, lt) } l.Push(lv) Loading Loading @@ -660,11 +670,16 @@ func (n *NakamaModule) storageWrite(l *lua.LState) int { l.ArgError(1, "expects a value in each key") return 0 } else { if vs, ok := v.(string); !ok { l.ArgError(1, "value must be a string") if vs, ok := v.(map[string]interface{}); !ok { l.ArgError(1, "value must be a table") return 0 } else { value = []byte(vs) dataJson, err := json.Marshal(vs) if err != nil { l.RaiseError("could not convert value to JSON: %v", err.Error()) return 0 } value = dataJson } } var userID []byte Loading
server/runtime_nakamax_module.go +8 −9 Original line number Diff line number Diff line Loading @@ -24,10 +24,11 @@ import ( "encoding/base64" "encoding/hex" "github.com/satori/go.uuid" "github.com/yuin/gopher-lua" "go.uber.org/zap" "encoding/hex" ) type NakamaxModule struct { Loading Loading @@ -131,17 +132,16 @@ func (nx *NakamaxModule) httpRequest(l *lua.LState) int { } func (nx *NakamaxModule) jsonEncode(l *lua.LState) int { // TODO allow top-level arrays or primitives? jsonTable := l.CheckTable(1) jsonTable := l.Get(1) if jsonTable == nil { l.ArgError(1, "Expects a table to encode") l.ArgError(1, "Expects a non-nil value to encode") return 0 } jsonData := ConvertLuaTable(jsonTable) jsonData := convertLuaValue(jsonTable) jsonBytes, err := json.Marshal(jsonData) if err != nil { l.ArgError(1, "Error encoding to JSON") l.RaiseError("Error encoding to JSON: %v", err.Error()) return 0 } Loading @@ -156,14 +156,13 @@ func (nx *NakamaxModule) jsonDecode(l *lua.LState) int { return 0 } // TODO allow top-level arrays or primitives? var jsonData map[string]interface{} var jsonData interface{} if err := json.Unmarshal([]byte(jsonString), &jsonData); err != nil { l.RaiseError("Not a valid JSON string: %v", err.Error()) return 0 } l.Push(ConvertMap(l, jsonData)) l.Push(convertValue(l, jsonData)) return 1 } Loading
tests/modules/e2e_runtime.lua +24 −0 Original line number Diff line number Diff line Loading @@ -139,12 +139,36 @@ do assert(object.hello == "world", "'object.hello' must equal 'world'") end -- json_decode_array do local object = nx.json_decode('[{"hello": "world"}, {"hello": "world"}]') assert(#object == 2) end -- json_decode_primitive do local object = nx.json_decode('"hello"') assert(object == "hello") end -- json_encode do local json = nx.json_encode({["id"] = "blah"}) assert(json == '{"id":"blah"}', '"json" must equal "{"id":"blah"}"') end -- json_encode_array do local json = nx.json_encode({{["id"] = "blah"},{["id"] = "blah"}}) assert(json == '[{"id":"blah"},{"id":"blah"}]', '"json" must equal "[{"id":"blah"}",{"id":"blah"}]') end -- json_encode_primitive do local json = nx.json_encode("hello") assert(json == '"hello"') end -- base64_encode_decode do local objectEncode = nx.base64_encode('{"hello": "world"}') Loading