File

util/helpers.lua @ 12579:ca6a43fe0231 0.12

util.jsonschema: Fix validation to not assume presence of "type" field MattJ reported a curious issue where validation did not work as expected. Primarily that the "type" field was expected to be mandatory, and thus leaving it out would result in no checks being performed. This was likely caused by misreading during initial development. Spent some time testing against https://github.com/json-schema-org/JSON-Schema-Test-Suite.git and discovered a multitude of issues, far too many to bother splitting into separate commits. More than half of them fail. Many because of features not implemented, which have been marked NYI. For example, some require deep comparisons e.g. when objects or arrays are present in enums fields. Some because of quirks with how Lua differs from JavaScript, e.g. no distinct array or object types. Tests involving fractional floating point numbers. We're definitely not going to follow references to remote resources. Or deal with UTF-16 sillyness. One test asserted that 1.0 is an integer, where Lua 5.3+ will disagree.
author Kim Alvefur <zash@zash.se>
date Fri, 08 Jul 2022 14:38:23 +0200
parent 11059:ad89e3cc67b6
child 12975:d10957394a3c
line wrap: on
line source

-- Prosody IM
-- Copyright (C) 2008-2010 Matthew Wild
-- Copyright (C) 2008-2010 Waqas Hussain
--
-- This project is MIT/X11 licensed. Please see the
-- COPYING file in the source package for more information.
--

local debug = require "util.debug";

-- Helper functions for debugging

local log = require "util.logger".init("util.debug");

local function log_events(events, name, logger)
	local f = events.fire_event;
	if not f then
		error("Object does not appear to be a util.events object");
	end
	logger = logger or log;
	name = name or tostring(events);
	function events.fire_event(event, ...)
		logger("debug", "%s firing event: %s", name, event);
		return f(event, ...);
	end

	local function event_handler_hook(handler, event_name, event_data)
		logger("debug", "calling handler for %s: %s", event_name, handler);
		local ok, ret = pcall(handler, event_data);
		if not ok then
			logger("error", "error in event handler %s: %s", handler, ret);
			error(ret);
		end
		if ret ~= nil then
			logger("debug", "event chain ended for %s by %s with result: %s", event_name, handler, ret);
		end
		return ret;
	end
	events.set_debug_hook(event_handler_hook);
	events[events.fire_event] = f;
	return events;
end

local function revert_log_events(events)
	events.fire_event, events[events.fire_event] = events[events.fire_event], nil; -- :))
	events.set_debug_hook(nil);
end

local function log_host_events(host)
	return log_events(prosody.hosts[host].events, host);
end

local function revert_log_host_events(host)
	return revert_log_events(prosody.hosts[host].events);
end

local function show_events(events, specific_event)
	local event_handlers = events._handlers;
	local events_array = {};
	local event_handler_arrays = {};
	for event, priorities in pairs(events._event_map) do
		local handlers = event_handlers[event];
		if handlers and (event == specific_event or not specific_event) then
			table.insert(events_array, event);
			local handler_strings = {};
			for i, handler in ipairs(handlers) do
				local upvals = debug.string_from_var_table(debug.get_upvalues_table(handler));
				handler_strings[i] = "  "..(priorities[handler] or "?")..": "..tostring(handler)..(upvals and ("\n        "..upvals) or "");
			end
			event_handler_arrays[event] = handler_strings;
		end
	end
	table.sort(events_array);
	local i = 1;
	while i <= #events_array do
		local handlers = event_handler_arrays[events_array[i]];
		for j=#handlers, 1, -1 do
			table.insert(events_array, i+1, handlers[j]);
		end
		if i > 1 then events_array[i] = "\n"..events_array[i]; end
		i = i + #handlers + 1
	end
	return table.concat(events_array, "\n");
end

local function get_upvalue(f, get_name)
	local i, name, value = 0;
	repeat
		i = i + 1;
		name, value = debug.getupvalue(f, i);
	until name == get_name or name == nil;
	return value;
end

return {
	log_host_events = log_host_events;
	revert_log_host_events = revert_log_host_events;
	log_events = log_events;
	revert_log_events = revert_log_events;
	show_events = show_events;
	get_upvalue = get_upvalue;
};