summaryrefslogtreecommitdiff
path: root/server/src/core/find_lib.lua
blob: 12eaa2fe71b61f70091224f2e0e51176222dd190 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
local function findLib(value)
    local lib = value:getLib()
    if not lib then
        return nil
    end
    if lib.parent then
        local res
        for _, parent in ipairs(lib.parent) do
            if parent.type == value.parentType then
                res = parent
            end
        end
        if not res then
            res = lib.parent[1]
        end
        if res.type == 'object' then
            local fullKey = ('*%s:%s'):format(res.nick or res.name, lib.name)
            return lib, fullKey, true
        else
            local fullKey = ('%s.%s'):format(res.nick or res.name, lib.name)
            return lib, fullKey, false
        end
    else
        local res
        if not lib.source then
            return lib, lib.nick or lib.name, false
        end
        for _, source in ipairs(lib.source) do
            if source.type == value.parentType then
                res = source
            end
        end
        if not res then
            return lib, lib.nick or lib.name, false
        end
        return lib, res.nick or res.name or lib.nick or lib.name, false
    end
end

return function (source)
    if source:bindValue() then
        local lib, fullKey, oo = findLib(source:bindValue())
        return lib, fullKey, oo
    end
    return nil
end