Skip to content

Commit 84dbe94

Browse files
houmkhTieske
authored andcommitted
optimize code
1 parent e776eaa commit 84dbe94

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed

src/resty/ljsonschema/init.lua

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,8 @@ function codectx_mt:merge_child_errors(indent, err_var, path_prefix, instance_pr
238238

239239
self:stmt(sformat('%s for _, sub_err in ipairs(%s) do', indent, err_var))
240240
if path_prefix and path_prefix ~= '' then
241-
local formatted_prefix = path_prefix:match("^/") and path_prefix or ("/" .. path_prefix)
241+
-- 47 is the ASCII code for '/'
242+
local formatted_prefix = string.byte(path_prefix, 1) == 47 and path_prefix or ("/" .. path_prefix)
242243
self:stmt(sformat('%s local new_path = sub_err.schema_path == "" and %q or (%q .. sub_err.schema_path)', indent, formatted_prefix, formatted_prefix))
243244
if instance_prefix and instance_prefix ~= '' then
244245
self:stmt(sformat('%s local new_instance_path = sub_err.instance_path == "" and %s or (%s .. sub_err.instance_path)', indent, instance_prefix, instance_prefix))
@@ -251,7 +252,7 @@ function codectx_mt:merge_child_errors(indent, err_var, path_prefix, instance_pr
251252
end
252253
self:stmt(sformat('%s end', indent))
253254
self:stmt(sformat('%selse', indent))
254-
local formatted_prefix = (path_prefix and path_prefix ~= '') and (path_prefix:match("^/") and path_prefix or ("/" .. path_prefix)) or ""
255+
local formatted_prefix = (path_prefix and path_prefix ~= '') and (string.byte(path_prefix, 1) == 47 and path_prefix or ("/" .. path_prefix)) or ""
255256
local formatted_instance = instance_prefix or '""'
256257
self:stmt(sformat('%s table.insert(errors, {schema_path = %q, instance_path = %s, error = %s})', indent, formatted_prefix, formatted_instance, err_var))
257258
self:stmt(sformat('%send', indent))
@@ -1113,7 +1114,6 @@ generate_validator = function(ctx, schema)
11131114
ctx:stmt(') then')
11141115
if ctx._root._collect_all_errors then
11151116
-- When collect_all_errors is enabled, collect errors from all failed schemas
1116-
ctx:stmt(' local any_of_errors = {}')
11171117
ctx:stmt(' do')
11181118
for i, subschema in ipairs(schema.anyOf) do
11191119
ctx:stmt( ' local was_matched, error_message')
@@ -1123,17 +1123,14 @@ generate_validator = function(ctx, schema)
11231123
ctx:stmt( ' if type(error_message) == "table" then')
11241124
ctx:stmt( ' for _, sub_err in ipairs(error_message) do')
11251125
ctx:stmt(sformat(' local new_path = sub_err.schema_path == "" and "/anyOf/%d" or ("/anyOf/%d" .. sub_err.schema_path)', i, i))
1126-
ctx:stmt( ' table.insert(any_of_errors, {schema_path = new_path, instance_path = sub_err.instance_path, error = sub_err.error})')
1126+
ctx:stmt( ' table.insert(errors, {schema_path = new_path, instance_path = sub_err.instance_path, error = sub_err.error})')
11271127
ctx:stmt( ' end')
11281128
ctx:stmt( ' else')
1129-
ctx:stmt(sformat(' table.insert(any_of_errors, {schema_path = "/anyOf/%d", instance_path = "", error = error_message})', i))
1129+
ctx:stmt(sformat(' table.insert(errors, {schema_path = "/anyOf/%d", instance_path = "", error = error_message})', i))
11301130
ctx:stmt( ' end')
11311131
ctx:stmt( ' end')
11321132
end
11331133
ctx:stmt(' end')
1134-
ctx:stmt(' for _, err in ipairs(any_of_errors) do')
1135-
ctx:stmt(' table.insert(errors, err)')
1136-
ctx:stmt(' end')
11371134
else
11381135
-- When collect_all_errors is disabled, use the original string concatenation approach
11391136
ctx:stmt(' local unmatched, i = nil, 0')

0 commit comments

Comments
 (0)