Return the peripheral name when wrapping (#436)

This commit is contained in:
Drew Lemmy 2020-05-03 06:53:42 +01:00 committed by GitHub
parent fb64b6017b
commit a565a571f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 63 additions and 16 deletions

View File

@ -64,24 +64,33 @@ function isPresent(name)
return false
end
--- Get the type of the peripheral with the given name.
--- Get the type of a wrapped peripheral, or a peripheral with the given name.
--
-- @tparam string name The name of the peripheral to find.
-- @tparam string|table peripheral The name of the peripheral to find, or a
-- wrapped peripheral instance.
-- @treturn string|nil The peripheral's type, or `nil` if it is not present.
function getType(name)
expect(1, name, "string")
if native.isPresent(name) then
return native.getType(name)
end
for n = 1, #sides do
local side = sides[n]
if native.getType(side) == "modem" and not native.call(side, "isWireless") and
native.call(side, "isPresentRemote", name)
then
return native.call(side, "getTypeRemote", name)
function getType(peripheral)
expect(1, peripheral, "string", "table")
if type(peripheral) == "string" then -- Peripheral name passed
if native.isPresent(peripheral) then
return native.getType(peripheral)
end
for n = 1, #sides do
local side = sides[n]
if native.getType(side) == "modem" and not native.call(side, "isWireless") and
native.call(side, "isPresentRemote", peripheral)
then
return native.call(side, "getTypeRemote", peripheral)
end
end
return nil
else
local mt = getmetatable(peripheral)
if not mt or mt.__name ~= "peripheral" or type(mt.type) ~= "string" then
error("bad argument #1 (table is not a peripheral)", 2)
end
return mt.type
end
return nil
end
--- Get all available methods for the peripheral with the given name.
@ -105,6 +114,19 @@ function getMethods(name)
return nil
end
--- Get the name of a peripheral wrapped with @{peripheral.wrap}.
--
-- @tparam table peripheral The peripheral to get the name of.
-- @treturn string The name of the given peripheral.
function getName(peripheral)
expect(1, peripheral, "table")
local mt = getmetatable(peripheral)
if not mt or mt.__name ~= "peripheral" or type(mt.name) ~= "string" then
error("bad argument #1 (table is not a peripheral)", 2)
end
return mt.name
end
--- Call a method on the peripheral with the given name.
--
-- @tparam string name The name of the peripheral to invoke the method on.
@ -148,7 +170,11 @@ function wrap(name)
return nil
end
local result = {}
local result = setmetatable({}, {
__name = "peripheral",
name = name,
type = peripheral.getType(name),
})
for _, method in ipairs(methods) do
result[method] = function(...)
return peripheral.call(name, method, ...)

View File

@ -3,6 +3,7 @@ The peripheral API is for interacting with external peripheral devices. Type "he
Functions in the peripheral API:
peripheral.getNames()
peripheral.isPresent( name )
peripheral.getName( peripheral )
peripheral.getType( name )
peripheral.getMethods( name )
peripheral.call( name, methodName, param1, param2, etc )

View File

@ -8,10 +8,30 @@ describe("The peripheral library", function()
end)
end)
describe("peripheral.getName", function()
it("validates arguments", function()
expect.error(peripheral.getName, nil):eq("bad argument #1 (expected table, got nil)")
expect.error(peripheral.getName, {}):eq("bad argument #1 (table is not a peripheral)")
end)
it_modem("can get the name of a wrapped peripheral", function()
expect(peripheral.getName(peripheral.wrap("top"))):eq("top")
end)
end)
describe("peripheral.getType", function()
it("validates arguments", function()
peripheral.getType("")
expect.error(peripheral.getType, nil):eq("bad argument #1 (expected string, got nil)")
expect.error(peripheral.getType, nil):eq("bad argument #1 (expected string or table, got nil)")
expect.error(peripheral.getType, {}):eq("bad argument #1 (table is not a peripheral)")
end)
it_modem("can get the type of a peripheral by side", function()
expect(peripheral.getType("top")):eq("modem")
end)
it_modem("can get the type of a wrapped peripheral", function()
expect(peripheral.getType(peripheral.wrap("top"))):eq("modem")
end)
end)