본문으로 이동

모듈:reference section/debug

위키낱말사전, 말과 글의 누리

이 모듈에 대한 설명문서는 모듈:reference section/debug/설명문서에서 만들 수 있습니다

local export = {}

local function resolve_alias(key, params)
    -- alias_of와 other_aliases를 순차적으로 확인하여 실제 키를 반환
    if params[key] and params[key].alias_of then
        return resolve_alias(params[key].alias_of, params)
    end
    return key
end

local function validate_params(args, params)
    local debug_info = ""
    
    -- args 테이블의 디버그 정보를 생성
    for k, v in pairs(args) do
        debug_info = debug_info .. k .. ": " .. tostring(v) .. "<br>"
    end
    
    -- 필수 필드를 체크하고 타입 검사를 수행
    for k, v in pairs(params) do
        local resolved_key = resolve_alias(k, params)
        
        -- required 플래그가 있는 경우에 대해서도 확인
        if v.required and (args[resolved_key] == nil or type(args[resolved_key]) ~= "string") then
            return "Error: " .. resolved_key .. " must be provided and must be a string<br>" .. debug_info
        end
        
        -- 필드가 존재하고 타입이 맞는지 확인
        if args[resolved_key] ~= nil and type(args[resolved_key]) ~= "string" then
            return "Error: " .. resolved_key .. " must be a string<br>" .. debug_info
        end
        
        -- other_aliases 필드를 확인
        if v.other_aliases then
        	if type(v.other_aliases) == "string" then
				local resolved_alias = resolve_alias(alias, params)
					if args[resolved_alias] ~= nil and type(args[resolved_alias]) ~= "string" then
                    	return "Error: " .. resolved_alias .. " must be a string<br>" .. debug_info
	            end
        	else
        		for _, alias in ipairs(v.other_aliases) do
	                local resolved_alias = resolve_alias(alias, params)
	                if args[resolved_alias] ~= nil and type(args[resolved_alias]) ~= "string" then
	                    return "Error: " .. resolved_alias .. " must be a string<br>" .. debug_info
	                end
                end
			end
        end
    end
    
    return debug_info
end

function export.process_debug(args, params)
	return validate_params(args, params)
end

return export