refactor: use online query method template instead of backend.query todo online query

This commit is contained in:
JuanZoran
2023-03-18 21:31:14 +08:00
parent b2cafe3448
commit 52d2741804
7 changed files with 119 additions and 205 deletions

View File

@@ -2,11 +2,18 @@ local Trans = require('Trans')
---@class TransBackend
---@field query fun(data: TransData)---@async
---@field no_wait? boolean whether need to wait for the result
---@field all_name string[] @all backend name
---@field name string @backend name
---@class TransOnlineBackend: TransBackend
---@field uri string @request uri
---@field method 'get' | 'post' @request method
---@field formatter fun(body: table, data: TransData): TransResult|false|nil @formatter
---@field get_query fun(data: TransData): table<string, string> @get query
---@field header? table<string, string> | fun(data: TransData): table<string, string> @request header
---@field debug? fun(body: table?) @debug
local conf = Trans.conf
--- INFO :Parse online engine keys config file
@@ -21,7 +28,8 @@ if file then
end
local all_name = {}
for name, opts in pairs(result) do
local backend_order = conf.backend_order or vim.tbl_keys(result)
for name, opts in pairs(backend_order) do
if opts.disable then
result[name] = nil
else
@@ -33,7 +41,7 @@ end
---@class Trans
---@field backend table<string, TransBackend>
return setmetatable({
all_name = Trans.conf.backend_order or all_name,
all_name = all_name,
}, {
__index = function(self, name)
---@type TransBackend

View File

@@ -9,6 +9,7 @@ local Trans = require('Trans')
---@field mode string @The mode of the str
---@field result table<string, TransResult|nil|false> @The result of the translation
---@field frontend TransFrontend
---@field trace table<string, string> debug message
---@field backends table<string, TransBackend>
local M = {}
M.__index = M
@@ -28,6 +29,7 @@ function M.new(opts)
str = str,
mode = mode,
result = {},
trace = {},
}, M)

View File

@@ -13,53 +13,32 @@ local function init_opts(opts)
end
---To Do Online Query
---@param data TransData @data
---@param backend TransOnlineBackend @backend
local function do_query(data, backend)
-- TODO : template method for online query
local name = backend.name
local uri = backend.uti
local uri = backend.uri
local method = backend.method
local formatter = backend.formatter
local query = backend.get_query(data)
local header
if backend.header then
if type(backend.header) == "function" then
header = backend.header(data)
else
header = backend.header
end
end
local header = type(backend.header) == "function" and backend.header(data) or backend.header
local function handle(output)
local status, body = pcall(vim.json.decode, output.body)
-- -- vim.print(body)
if not status or not body or body.errorCode ~= "0" then
if not Trans.conf.debug then backend.debug(body) end
if not status or not body then
if not Trans.conf.debug then
backend.debug(body)
data.trace[name] = output
end
data.result[name] = false
data[#data + 1] = output
return
end
-- check_untracked_field(body)
-- if not body.isWord then
-- data.result.youdao = {
-- title = body.query,
-- [data.from == 'en' and 'translation' or 'definition'] = body.translation,
-- }
-- return
-- end
-- local tmp = {
-- title = {
-- word = body.query,
-- phonetic = body.basic.phonetic,
-- },
-- web = body.web,
-- explains = body.basic.explains,
-- -- phrases = body.phrases,
-- -- synonyms = body.synonyms,
-- -- sentenceSample = body.sentenceSample,
-- [data.from == 'en' and 'translation' or 'definition'] = body.translation,
-- }
data.result[name] = formatter and formatter(output) or output
-- vim.print(data.result[name])
data.result[name] = formatter(body, data)
end
Trans.curl[method](uri, {
@@ -70,7 +49,6 @@ local function do_query(data, backend)
-- Hook ?
end
---@type table<string, fun(data: TransData): true | nil>
local strategy = {
fallback = function(data)
@@ -81,15 +59,13 @@ local strategy = {
local update = data.frontend:wait()
for _, backend in ipairs(data.backends) do
do_query(data, backend)
---@cast backend TransBackend
backend.query(data)
local name = backend.name
while result[name] == nil do
if not update() then return end
while result[backend.name] == nil do
if not update() then break end
end
if result[name] then return true end
if result[backend.name] then return true end
end
end,
--- TODO :More Strategys