Debian Codegraph @

lualib/rspamadm/confighelp_plugins.lua

raw
1 --[[
2 Copyright (c) 2025, Vsevolod Stakhov <[email protected]>
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 ]]--
16
17 -- Convert plugin schemas to UCL format for confighelp
18
19 local function convert_schema(node)
20 if not node then return {} end
21
22 local res = {}
23 if node.summary then res.data = node.summary end
24 if node.description and not res.data then res.data = node.description end
25 if node.examples and node.examples[1] then res.example = node.examples[1] end
26 if node.default ~= nil then res.default = node.default end
27 if node.optional ~= nil then res.required = not node.optional end
28
29 if node.type == 'scalar' then
30 res.type = node.kind or 'string'
31 elseif node.type == 'table' then
32 res.type = 'object'
33 else
34 res.type = node.type
35 end
36
37 if node.type == 'table' then
38 local mixins = {}
39 local mixin_field_names = {}
40 for _, group in ipairs(node.mixin_groups or {}) do
41 local entry = { name = group.mixin_name or group.schema_id, schema_id = group.schema_id }
42 table.insert(mixins, entry)
43 for _, mixin_field in ipairs(group.fields or {}) do
44 mixin_field_names[mixin_field.name] = true
45 end
46 end
47 if #mixins > 0 then res.mixins = mixins end
48
49 for _, field in ipairs(node.fields or {}) do
50 if not mixin_field_names[field.name] then
51 local child = convert_schema(field.schema)
52 child.required = not field.optional
53 if field.schema and field.schema.summary and not child.data then
54 child.data = field.schema.summary
55 end
56 res[field.name] = child
57 end
58 end
59
60 if node.extra_schema then
61 local extra_child = convert_schema(node.extra_schema)
62 if not extra_child.data then extra_child.data = 'Entry schema' end
63 res['entry'] = extra_child
64 end
65
66 elseif node.type == 'array' and node.item_schema then
67 res.item = convert_schema(node.item_schema)
68
69 elseif node.type == 'one_of' then
70 local variants = {}
71 local common_fields = {}
72 local idx = 1
73
74 -- Extract fields common to all variants
75 local first_variant_fields = nil
76 local all_have_common = true
77 for _, variant in ipairs(node.variants or {}) do
78 if variant.type == 'table' and variant.fields then
79 local field_map = {}
80 for _, field in ipairs(variant.fields) do
81 field_map[field.name] = field
82 end
83 if first_variant_fields == nil then
84 first_variant_fields = field_map
85 else
86 -- Check which fields are common
87 for fname, _ in pairs(first_variant_fields) do
88 if not field_map[fname] then
89 first_variant_fields[fname] = nil
90 end
91 end
92 end
93 else
94 all_have_common = false
95 end
96 end
97
98 -- Convert common fields
99 if first_variant_fields and all_have_common then
100 for fname, field in pairs(first_variant_fields) do
101 local child = convert_schema(field.schema)
102 child.required = not field.optional
103 if field.schema and field.schema.summary and not child.data then
104 child.data = field.schema.summary
105 end
106 common_fields[fname] = true
107 res[fname] = child
108 end
109 end
110
111 -- Convert variants, excluding common fields
112 for _, variant in ipairs(node.variants or {}) do
113 local vname = variant.name or ('variant_' .. tostring(idx))
114 local converted_variant = convert_schema(variant)
115
116 -- Remove common fields from variant
117 for fname, _ in pairs(common_fields) do
118 converted_variant[fname] = nil
119 end
120
121 variants[vname] = converted_variant
122 idx = idx + 1
123 end
124 res.options = variants
125 end
126
127 return res
128 end
129
130 return function()
131 local Registry = require 'lua_shape.registry'
132 local docs = require 'lua_shape.docs'
133 local ucl = require 'ucl'
134
135 local reg = Registry.global()
136 if not reg then return nil end
137
138 local exported = docs.for_registry(reg)
139 if not exported or not exported.schemas then return nil end
140
141 local converted = {}
142 for id, schema in pairs(exported.schemas) do
143 converted[id] = convert_schema(schema)
144 end
145
146 return ucl.to_format({ schemas = converted }, 'json-compact')
147 end