Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

on_message_publish username is a table, not string #111

Open
saeid-ir opened this issue Aug 6, 2020 · 2 comments
Open

on_message_publish username is a table, not string #111

saeid-ir opened this issue Aug 6, 2020 · 2 comments

Comments

@saeid-ir
Copy link

saeid-ir commented Aug 6, 2020

Hi,
As what the docs say username for on_message_publish function should be string, but when I debug it using print and some helper functions I realize that its a table without any data in it.
This is a function I used to print username and it gives me an empty table.

function print_table(node)
    local cache, stack, output = {},{},{}
    local depth = 1
    local output_str = "{\n"

    while true do
        local size = 0
        for k,v in pairs(node) do
            size = size + 1
        end

        local cur_index = 1
        for k,v in pairs(node) do
            if (cache[node] == nil) or (cur_index >= cache[node]) then

                if (string.find(output_str,"}",output_str:len())) then
                    output_str = output_str .. ",\n"
                elseif not (string.find(output_str,"\n",output_str:len())) then
                    output_str = output_str .. "\n"
                end

                -- This is necessary for working with HUGE tables otherwise we run out of memory using concat on huge strings
                table.insert(output,output_str)
                output_str = ""

                local key
                if (type(k) == "number" or type(k) == "boolean") then
                    key = "["..tostring(k).."]"
                else
                    key = "['"..tostring(k).."']"
                end

                if (type(v) == "number" or type(v) == "boolean") then
                    output_str = output_str .. string.rep('\t',depth) .. key .. " = "..tostring(v)
                elseif (type(v) == "table") then
                    output_str = output_str .. string.rep('\t',depth) .. key .. " = {\n"
                    table.insert(stack,node)
                    table.insert(stack,v)
                    cache[node] = cur_index+1
                    break
                else
                    output_str = output_str .. string.rep('\t',depth) .. key .. " = '"..tostring(v).."'"
                end

                if (cur_index == size) then
                    output_str = output_str .. "\n" .. string.rep('\t',depth-1) .. "}"
                else
                    output_str = output_str .. ","
                end
            else
                -- close the table
                if (cur_index == size) then
                    output_str = output_str .. "\n" .. string.rep('\t',depth-1) .. "}"
                end
            end

            cur_index = cur_index + 1
        end

        if (size == 0) then
            output_str = output_str .. "\n" .. string.rep('\t',depth-1) .. "}"
        end

        if (#stack > 0) then
            node = stack[#stack]
            stack[#stack] = nil
            depth = cache[node] == nil and depth + 1 or depth - 1
        else
            break
        end
    end

    -- This is necessary for working with HUGE tables otherwise we run out of memory using concat on huge strings
    table.insert(output,output_str)
    output_str = table.concat(output)

    print(output_str)
end

And this is my on_message_publish :

function on_message_publish(clientid, username, topic, payload, qos, retain)
    print_table(username)
    -- do your job here
    return topic, payload, qos, retain
end
@ghost
Copy link

ghost commented Aug 6, 2020

I also facing same issue in string handling. I need to remove some character from my string then return to the topic but it seems that gmatch function or for loop statement do not work on lua_hook plugins.

function on_message_publish(clientid, username, topic, payload, qos, retain)
  dataStr = payload:match("(%[.-%])")
  string = ''
  for data in dataStr:gmatch("%b{}") do
   string = string .. data:match(':"(.-)",') .. ';' .. data:match('e":(.-)}') .. ','
  end
    return topic, string, qos, retain
  end

@saeid-ir
Copy link
Author

saeid-ir commented Aug 6, 2020

I also find that this problem is related to all plugins even plugins written in Erlang
emqx/emqx#3652

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant