Commit f02f02d6 authored by Mo Firouz's avatar Mo Firouz Committed by Andrei Mihu
Browse files

Runtime Enhancements. Merge #94

parent 9844eaa8
Loading
Loading
Loading
Loading
+14 −0
Original line number Diff line number Diff line
@@ -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 := &notificationResumableCursor{
		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
+1 −0
Original line number Diff line number Diff line
@@ -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))
	}
+19 −4
Original line number Diff line number Diff line
@@ -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)
@@ -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
+8 −9
Original line number Diff line number Diff line
@@ -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 {
@@ -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
	}

@@ -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
}

+24 −0
Original line number Diff line number Diff line
@@ -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