Skip to content

refactor: adopt lua-resty-etcd code style conventions#2

Merged
nic-6443 merged 4 commits intomainfrom
refactor/adopt-etcd-code-style
Apr 21, 2026
Merged

refactor: adopt lua-resty-etcd code style conventions#2
nic-6443 merged 4 commits intomainfrom
refactor/adopt-etcd-code-style

Conversation

@jarvis9443
Copy link
Copy Markdown
Contributor

@jarvis9443 jarvis9443 commented Apr 21, 2026

Refactor all source code to follow lua-resty-etcd code style conventions.

Source Code Style

  • Align all imports with = signs, localize builtins at file top
  • Use prefixed aliases (tab_insert, str_find, sub_str, etc.) instead of short names
  • Replace OOP string method calls (s:gsub(), s:gmatch()) with localized functions
  • Remove LDoc annotations (--- @param, --- @return), use simple -- comments
  • Rename metatables: _VMT_validator_mt, _MT_router_mt
  • Use inline version field ({version = 0.1})
  • Rename skip options to snake_case: readOnlyread_only, writeOnlywrite_only

Infrastructure

  • Add .luacheckrc with std = "ngx_lua"
  • Add luacheck lint step to CI workflow
  • Rewrite Makefile with self-documenting ### target: pattern
  • Add install, dev, help targets
  • Add master rockspec for development
  • Add lua-cjson to rockspec dependencies

Documentation

  • Rewrite README with rst-style headers, Table of Contents, badges
  • Create separate api.md with detailed API documentation
  • Fix README to match actual code behavior (removed undocumented coerce_types/fail_fast options)
  • Add readOnly/writeOnly and form-urlencoded to feature matrix

All existing tests pass. Zero lint warnings.

Summary by CodeRabbit

  • New Features

    • Expanded validation: form-urlencoded bodies, readOnly/writeOnly handling, and basic multipart/form-data support
    • Public API docs for compile/validate usage and skip options (read_only/read_only)
  • Documentation

    • Reorganized README with TOC, clearer install (LuaRocks/source), updated Quick Start and Benchmark
    • Added API reference
  • Chores

    • Added install/dev/help Make targets, Luacheck config, and a rockspec
    • CI: added lint step
  • Tests

    • Conformance tests updated to use read_only option name

- Align imports with = signs, localize all builtins at file top
- Use prefixed aliases (tab_insert, str_find, sub_str, etc.)
- Replace OOP string method calls with localized functions
- Remove LDoc annotations, use simple -- comments
- Rename metatables (_VMT -> _validator_mt, _MT -> _router_mt)
- Use inline version field (version = 0.1)

Infrastructure:
- Add .luacheckrc (std=ngx_lua, ignore 542)
- Rewrite Makefile with self-documenting ### target: pattern
- Add install, dev, help targets
- Add master rockspec

Documentation:
- Rewrite README with rst-style headers, TOC, badges
- Create separate api.md with detailed API documentation
- Fix README to match actual code (remove undocumented options)
- Add readOnly/writeOnly and form-urlencoded to feature matrix
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 21, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 93685ba9-d523-486a-9502-7413ce743868

📥 Commits

Reviewing files that changed from the base of the PR and between bf34ebd and aa7be90.

📒 Files selected for processing (1)
  • t/conformance/test_validate_readonly.lua

📝 Walkthrough

Walkthrough

Adds lint/config and packaging files, expands Makefile targets, adds API docs and README rework, applies local aliasing and metatable renames across core modules, updates tests to use renamed skip option read_only, and adds a CI lint step. Public API signatures are unchanged.

Changes

Cohort / File(s) Summary
Build & packaging
Makefile, .luacheckrc, rockspec/lua-resty-openapi-validator-master-0.1-0.rockspec, rockspec/lua-resty-openapi-validator-0.1.0-1.rockspec
Added .luacheckrc; extended Makefile with dev, install, help, and install-path variables; added master rockspec and added lua-cjson dependency to existing rockspec.
Documentation
README.md, api.md
Rewrote and reorganized README (TOC, install, quick start, benchmark); added api.md documenting compile and validate_request signatures, parameters, return values, and skip options.
Core library modules
lib/resty/openapi_validator/...
body.lua, errors.lua, init.lua, loader.lua, normalize.lua, params.lua, refs.lua, router.lua
Replaced direct globals with cached local aliases (e.g., str_find, sub_str, tab_insert), minor formatting and binding changes, renamed internal metatables (_VMT_validator_mt, _MT_router_mt), adjusted module version field and mapped skip-option key names; no public function signature changes.
Tests & CI
.github/workflows/test.yml, t/conformance/...
t/conformance/test_issue689.lua, t/conformance/test_validate_readonly.lua
Added CI lint step that installs/runs luacheck; updated conformance tests to pass read_only instead of readOnly in validate_request skip options.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

🚥 Pre-merge checks | ✅ 6
✅ Passed checks (6 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title 'refactor: adopt lua-resty-etcd code style conventions' accurately describes the primary objective of the changeset—a comprehensive style refactor aligning the codebase with lua-resty-etcd conventions.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
E2e Test Quality Review ✅ Passed PR is a focused refactoring with comprehensive E2E test coverage (26 conformance tests); test changes (2 lines each in 2 files) correctly update API parameter keys to match refactored code.
Security Check ✅ Passed Pure code style refactoring with no security vulnerabilities identified in OpenAPI request validator module.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch refactor/adopt-etcd-code-style

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🧹 Nitpick comments (3)
Makefile (1)

10-29: Add an all phony alias to satisfy tooling and conventions.

This removes checkmake noise and gives a predictable default target.

♻️ Proposed update
-.PHONY: test test-unit test-conformance benchmark lint dev install clean help
+.PHONY: all test test-unit test-conformance benchmark lint dev install clean help
+
+### all:           Default target
+all: test
 
 ### help:          Show Makefile rules
 help:
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@Makefile` around lines 10 - 29, Add a conventional phony "all" target that
invokes the default build/test (make all -> make test) and include it in the
.PHONY list; specifically update the .PHONY line to include "all" and add an
"all:" target that depends on "test" (so the existing "test" target runs when
users or tooling invoke the default target).
api.md (1)

13-33: Keep compile argument naming consistent in docs.

The syntax uses spec_str while the example uses spec_json. Using one name throughout improves scanability.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@api.md` around lines 13 - 33, The docs use two different parameter names for
ov.compile's first argument (`spec_str` in the signature and `spec_json` in the
example); update the example to use the same name as the signature (use
`spec_str` everywhere) so ov.compile, the parameter description, and the example
consistently reference `spec_str` for the raw OpenAPI JSON input.
lib/resty/openapi_validator/params.lua (1)

158-170: Make split delimiter-length aware.

from = pos + 1 only works for single-character delimiters. Advancing by #delim makes the helper correct for all delimiters.

♻️ Proposed refactor
 local function split(s, delim)
     local result = {}
     local from = 1
     local pos
     while true do
         pos = str_find(s, delim, from, true)
         if not pos then
             tab_insert(result, sub_str(s, from))
             break
         end
         tab_insert(result, sub_str(s, from, pos - 1))
-        from = pos + 1
+        from = pos + `#delim`
     end
     return result
 end
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@lib/resty/openapi_validator/params.lua` around lines 158 - 170, The split
function advances the search pointer by 1 which only works for single-character
delimiters; update the loop in function split to advance from by the delimiter
length instead of 1 (i.e., set from = pos + `#delim` or equivalent) so
multi-character delimiters are handled correctly—adjust references around
str_find, delim, pos and from accordingly.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@lib/resty/openapi_validator/router.lua`:
- Around line 88-99: The content-type matching loop in router.lua can miss
case-variant keys (e.g., "Application/JSON") and then falls back to unordered
pairs; normalize header keys to lowercase before matching: in the loop that
iterates over content (variables ct, media) compute a lowercase version of ct
(using string.lower) and test if ct_lower == "*/*" or string.find(ct_lower,
"json") (instead of using str_find on the original ct); set primary_schema from
media.schema when matched and retain the existing fallback loop unchanged.

In `@README.md`:
- Around line 47-50: The shell example in README.md uses leading "$ " prompts
which triggers markdownlint MD014; edit the fenced code block containing the
commands (the lines showing git clone, cd, make dev, sudo make install) to
remove the "$ " prefixes so the block contains plain commands (git clone ..., cd
..., make dev, sudo make install) without shell prompts, ensuring the example
passes MD014.

In `@rockspec/lua-resty-openapi-validator-master-0.1-0.rockspec`:
- Around line 16-20: The rockspec is missing the lua-cjson dependency which
causes runtime failures because lib/resty/openapi_validator/loader.lua and
lib/resty/openapi_validator/body.lua unconditionally require "cjson.safe";
update the dependencies block in the rockspec to include "lua-cjson" (add
"lua-cjson" to the dependencies table) so the package manager installs cjson, or
alternatively modify the require calls in loader.lua and body.lua to use pcall
like lib/resty/openapi_validator/params.lua does (wrap require("cjson.safe") in
pcall and handle absence gracefully).

---

Nitpick comments:
In `@api.md`:
- Around line 13-33: The docs use two different parameter names for ov.compile's
first argument (`spec_str` in the signature and `spec_json` in the example);
update the example to use the same name as the signature (use `spec_str`
everywhere) so ov.compile, the parameter description, and the example
consistently reference `spec_str` for the raw OpenAPI JSON input.

In `@lib/resty/openapi_validator/params.lua`:
- Around line 158-170: The split function advances the search pointer by 1 which
only works for single-character delimiters; update the loop in function split to
advance from by the delimiter length instead of 1 (i.e., set from = pos + `#delim`
or equivalent) so multi-character delimiters are handled correctly—adjust
references around str_find, delim, pos and from accordingly.

In `@Makefile`:
- Around line 10-29: Add a conventional phony "all" target that invokes the
default build/test (make all -> make test) and include it in the .PHONY list;
specifically update the .PHONY line to include "all" and add an "all:" target
that depends on "test" (so the existing "test" target runs when users or tooling
invoke the default target).
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 2c5e845d-6632-404e-8e1f-0f1f97658140

📥 Commits

Reviewing files that changed from the base of the PR and between 279e47d and 7d0b650.

📒 Files selected for processing (13)
  • .luacheckrc
  • Makefile
  • README.md
  • api.md
  • lib/resty/openapi_validator/body.lua
  • lib/resty/openapi_validator/errors.lua
  • lib/resty/openapi_validator/init.lua
  • lib/resty/openapi_validator/loader.lua
  • lib/resty/openapi_validator/normalize.lua
  • lib/resty/openapi_validator/params.lua
  • lib/resty/openapi_validator/refs.lua
  • lib/resty/openapi_validator/router.lua
  • rockspec/lua-resty-openapi-validator-master-0.1-0.rockspec

Comment thread lib/resty/openapi_validator/router.lua
Comment thread README.md Outdated
Comment thread rockspec/lua-resty-openapi-validator-master-0.1-0.rockspec
- router: use case-insensitive content-type matching for JSON detection
- readme: remove $ prompts from shell examples (MD014)
- rockspec: add lua-cjson to dependencies in both rockspecs
Comment thread .luacheckrc
Comment thread api.md Outdated
- Add luacheck lint step to test.yml CI workflow
- Rename skip.readOnly -> skip.read_only, skip.writeOnly -> skip.write_only
  to follow Lua snake_case convention
- Update api.md and tests accordingly
Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
.github/workflows/test.yml (1)

45-48: Pin luacheck version to ensure CI reproducibility.

At line 47, installing the latest luacheck without pinning can introduce nondeterministic CI behavior—each run may fetch a different version if upstream releases occur between runs. Upstream changes to lint rules or strictness could cause unexpected failures. Pin to a known-good version (e.g., 1.2.0-1, the latest stable release on LuaRocks) using sudo luarocks install luacheck 1.2.0-1.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In @.github/workflows/test.yml around lines 45 - 48, The CI step named "Lint"
installs luacheck without a version pin which can make builds non-reproducible;
update the workflow to pin luacheck to a stable version (e.g., "1.2.0-1") by
changing the install command in the Lint job to install that specific version so
all CI runs use the same luacheck release.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@t/conformance/test_validate_readonly.lua`:
- Around line 61-63: Update the test description string in the T.describe(...)
call so it matches the renamed skip key used in the test body: change the phrase
"skip.readOnly" to "skip.read_only" (the test uses read_only when calling
v:validate_request and make_spec("readOnly")), ensuring the human-readable
description aligns with the actual skip key name.

---

Nitpick comments:
In @.github/workflows/test.yml:
- Around line 45-48: The CI step named "Lint" installs luacheck without a
version pin which can make builds non-reproducible; update the workflow to pin
luacheck to a stable version (e.g., "1.2.0-1") by changing the install command
in the Lint job to install that specific version so all CI runs use the same
luacheck release.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 89138be6-d7e3-4869-8377-ff6262ed7144

📥 Commits

Reviewing files that changed from the base of the PR and between 31d0682 and bf34ebd.

📒 Files selected for processing (5)
  • .github/workflows/test.yml
  • api.md
  • lib/resty/openapi_validator/init.lua
  • t/conformance/test_issue689.lua
  • t/conformance/test_validate_readonly.lua
✅ Files skipped from review due to trivial changes (1)
  • api.md
🚧 Files skipped from review as they are similar to previous changes (1)
  • lib/resty/openapi_validator/init.lua

Comment thread t/conformance/test_validate_readonly.lua Outdated
@nic-6443 nic-6443 merged commit 04a4a1f into main Apr 21, 2026
3 checks passed
@nic-6443 nic-6443 deleted the refactor/adopt-etcd-code-style branch April 21, 2026 08:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants