Skip to content

Commit be50bd1

Browse files
committed
change(topic): parsing topics distinguish no-match from error
1 parent b44634a commit be50bd1

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

mqtt/init.lua

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -179,27 +179,27 @@ end
179179
-- stored in this field for future use (cache).
180180
-- @tparam array opts.keys array of field names. The order must be the same as the
181181
-- order of the wildcards in `topic`
182-
-- @return[1] `fields` table: the array part will have the values of the wildcards, in
182+
-- @treturn[1] table `fields`: the array part will have the values of the wildcards, in
183183
-- the order they appeared. The hash part, will have the field names provided
184184
-- in `opts.keys`, with the values of the corresponding wildcard. If a `#`
185185
-- wildcard was used, that one will be the last in the table.
186-
-- @return[1] `varargs` table: will only be returned if the wildcard topic contained the
187-
-- `#` wildcard. The returned table is an array, with all segments that were
188-
-- matched by the `#` wildcard.
189-
-- @return[2] false+err on error, eg. topic didn't match or pattern was invalid.
186+
-- @treturn[1] `varargs`: The returned table is an array, with all segments that were
187+
-- matched by the `#` wildcard (empty if there was no `#` wildcard).
188+
-- @treturn[2] boolean `false` if there was no match
189+
-- @return[3] `false`+err on error, eg. pattern was invalid.
190190
-- @usage
191191
-- local opts = {
192192
-- topic = "homes/+/+/#",
193193
-- keys = { "homeid", "roomid", "varargs"},
194194
-- }
195-
-- local fields, varargs = topic_match("homes/myhome/living/mainlights/brightness", opts)
195+
-- local fields, varargst = topic_match("homes/myhome/living/mainlights/brightness", opts)
196196
--
197197
-- print(fields[1], fields.homeid) -- "myhome myhome"
198198
-- print(fields[2], fields.roomid) -- "living living"
199199
-- print(fields[3], fields.varargs) -- "mainlights/brightness mainlights/brightness"
200200
--
201-
-- print(varargs[1]) -- "mainlights"
202-
-- print(varargs[2]) -- "brightness"
201+
-- print(varargst[1]) -- "mainlights"
202+
-- print(varargst[2]) -- "brightness"
203203
function mqtt.topic_match(topic, opts)
204204
if type(topic) ~= "string" then
205205
return false, "expected topic to be a string"
@@ -223,7 +223,7 @@ function mqtt.topic_match(topic, opts)
223223
end
224224
local values = { topic:match(pattern) }
225225
if values[1] == nil then
226-
return false, "topic does not match wildcard pattern"
226+
return false
227227
end
228228
local keys = opts.keys
229229
if keys ~= nil then
@@ -240,7 +240,7 @@ function mqtt.topic_match(topic, opts)
240240
end
241241
if not pattern:find("%(%.[%-%+]%)%$$") then -- pattern for "#" as last char
242242
-- we're done
243-
return values
243+
return values, {}
244244
end
245245
-- we have a '#' wildcard
246246
local vararg = values[#values]

tests/spec/09-topics.lua

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ describe("topics", function()
160160
pattern = nil,
161161
keys = { "hello", "world"}
162162
}
163-
local res, err = mqtt.topic_match("hello/world", opts)
164-
assert.is_nil(err)
163+
local res, varargs = mqtt.topic_match("hello/world", opts)
164+
assert.is_same({}, varargs)
165165
assert.same(res, {
166166
"hello", "world",
167167
hello = "hello",
@@ -201,7 +201,7 @@ describe("topics", function()
201201
}
202202
local ok, err = mqtt.topic_match("hello/world", opts)
203203
assert.is_false(ok)
204-
assert.is_string(err)
204+
assert.is_nil(err)
205205
end)
206206

207207
it("pattern '+' works", function()
@@ -211,8 +211,8 @@ describe("topics", function()
211211
keys = { "hello" }
212212
}
213213
-- matches topic
214-
local res, err = mqtt.topic_match("hello", opts)
215-
assert.is_nil(err)
214+
local res, varargs = mqtt.topic_match("hello", opts)
215+
assert.are.same({}, varargs)
216216
assert.same(res, {
217217
"hello",
218218
hello = "hello",
@@ -225,8 +225,8 @@ describe("topics", function()
225225
pattern = nil,
226226
keys = { "hello", "there", "world"}
227227
}
228-
local res, err = mqtt.topic_match("//", opts)
229-
assert.is_nil(err)
228+
local res, varargs = mqtt.topic_match("//", opts)
229+
assert.are.same({}, varargs)
230230
assert.same(res, {
231231
"", "", "",
232232
hello = "",

0 commit comments

Comments
 (0)