129 lines
3.1 KiB
Lua
129 lines
3.1 KiB
Lua
local Endpoint = require("../Endpoints")
|
|
local ErrorTypes = require("@lib/ErrorTypes")
|
|
|
|
export type Child = {
|
|
name: string,
|
|
object: Endpoint.Endpoint | Folder,
|
|
type: "folder" | "endpoint",
|
|
path: string?
|
|
}
|
|
|
|
export type Folder = {
|
|
name: string,
|
|
path: string,
|
|
children: { [string]: Child },
|
|
error: ErrorTypes.Error,
|
|
|
|
addChild: (self: Folder, child: Child) -> Folder,
|
|
removeChild: (self: Folder, name: string) -> Folder,
|
|
getChild: (self: Folder, path: string) -> Child?,
|
|
new: (name: string, path: string) -> Folder
|
|
}
|
|
|
|
local Folder = {}
|
|
Folder.__index = Folder
|
|
|
|
function Folder.new(name: string, path: string): Folder
|
|
local self = setmetatable({}, Folder)
|
|
|
|
self.name = name
|
|
self.path = path
|
|
self.children = {}
|
|
self.error = ErrorTypes.new({
|
|
name = "Folder",
|
|
types = {
|
|
"addChild",
|
|
"removeChild",
|
|
"getChild"
|
|
},
|
|
exitOnError = true
|
|
})
|
|
|
|
return self
|
|
end
|
|
|
|
function Folder:addChild(child: Child): Folder
|
|
self.error:assert(self.children[child.name] == nil, "addChild", `Child '{child.name}' already exists`)
|
|
|
|
child.path = self.path .. "/" .. child.name
|
|
self.children[child.name] = child
|
|
|
|
return self
|
|
end
|
|
|
|
function Folder:removeChild(name: string): Folder
|
|
self.error:assert(self.children[name] ~= nil, "removeChild", `Child '{name}' doesn't exist`)
|
|
|
|
self.children[name] = nil
|
|
return self
|
|
end
|
|
|
|
function Folder:registerEndpoint(name: string, endpointConfig: Endpoint.Endpoint): Endpoint.Endpoint
|
|
local Endpoint = table.clone(endpointConfig)
|
|
Endpoint.name = name
|
|
|
|
self:addChild({
|
|
name = name,
|
|
object = Endpoint,
|
|
type = "endpoint"
|
|
})
|
|
|
|
return Endpoint
|
|
end
|
|
|
|
function Folder:registerFolder(name: string): Folder
|
|
local newFolder = Folder.new(name, self.path .. "/" .. name)
|
|
|
|
self:addChild({
|
|
name = name,
|
|
object = newFolder,
|
|
type = "folder",
|
|
path = newFolder.path
|
|
})
|
|
|
|
return newFolder
|
|
end
|
|
|
|
function Folder:unregisterEndpoint(name: string): Folder
|
|
return self:removeChild(name)
|
|
end
|
|
|
|
function Folder:unregisterFolder(name: string): Folder
|
|
local Child = self:getChild(name)
|
|
|
|
if Child.type == 'folder' then
|
|
for Index, GrandChild in pairs(Child.children) do
|
|
if GrandChild.type == 'endpoint' then
|
|
Child:unregisterEndpoint(Index)
|
|
else
|
|
Child:unregisterFolder(Index)
|
|
end
|
|
end
|
|
|
|
Folder:removeChild(Child.name)
|
|
end
|
|
|
|
return self
|
|
end
|
|
|
|
function Folder:getChild(path: string): Child?
|
|
local Parts = string.split(path, "/")
|
|
local Current = self
|
|
|
|
for i = 1, #Parts do
|
|
local Part = Parts[i]
|
|
local Child = Current.children[Part]
|
|
|
|
if not Child then
|
|
return nil
|
|
elseif Child.type == "endpoint" then
|
|
return Child
|
|
else
|
|
Current = Child.object
|
|
end
|
|
end
|
|
|
|
return nil
|
|
end
|
|
|
|
return Folder |