Skip to content

Refactor plugins to use validated functional options - #359

Draft
Loyalsoldier with Copilot wants to merge 8 commits into
refactor-plugin-2from
copilot/featureoptions-pattern-change
Draft

Refactor plugins to use validated functional options#359
Loyalsoldier with Copilot wants to merge 8 commits into
refactor-plugin-2from
copilot/featureoptions-pattern-change

Conversation

Copilot AI commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Plugin construction mixed JSON decoding, defaults, and validation across exported implementations. This refactor applies functional options to every converter and validates documented requirements before returning.

  • Construction

    • Add New*, With*, and New*FromBytes APIs.
    • Make converter implementations private.
    • Preserve format-specific defaults and JSON behavior.
  • Validation

    • Pair name and uri in one option.
    • Make inputDir the mutually exclusive alternative.
    • Validate required fields such as jsonPath, wantedList, search, and V2Ray uri.
  • Integration

    • Replace direct struct construction in lookup and merge commands.
    • Ensure directory lookup supplies only inputDir.
input := mihomo.NewMRSIn(
    lib.ActionAdd,
    mihomo.WithNameAndURI("cn", uri),
    mihomo.WithInputWantedList([]string{"cn"}),
)

@Loyalsoldier

Copy link
Copy Markdown
Owner

@claude[agent] Review this PR, try to refine it and find potential bugs.

@Loyalsoldier

Copy link
Copy Markdown
Owner

@codex[agent] Review this PR, try to refine it and find potential bugs.

Claude AI and others added 2 commits July 29, 2026 14:35
Agent-Logs-Url: https://github.com/Loyalsoldier/geoip/sessions/a71b4444-20f8-444c-bb11-bb3f97beddc3

Co-authored-by: Loyalsoldier <10487845+Loyalsoldier@users.noreply.github.com>
@Claude

Claude AI commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

@claude[agent] Review this PR, try to refine it and find potential bugs.

Reviewed the whole diff. One real regression found and fixed in f91e24c:

lookup -d <dir> with a single-file format (maxmindMMDB, dbipCountryMMDB, ipinfoCountryMMDB, v2rayGeoIPDat) silently ignored dir. Previously those built a struct literal with an empty URI and failed; now they go through New*In, which applies the plugin's default input path, so it would silently read e.g. ./geolite2/GeoLite2-Country.mmdb instead. Those formats now reject dir explicitly.

Two things I left as-is but worth your call:

  1. New*FromBytes still returns error, but all validation failures inside New* go through log.Fatal, so config errors abort the process instead of propagating to lib/config.go. This convention comes from the base branch (refactor-plugin-2), so I did not change it. If you want config-driven errors to stay recoverable, the validation blocks could be split into a validate() error used by New*FromBytes while New* keeps fatal behavior.
  2. Option functions use bare type assertions (e.g. special.WithName only works with *stdin, plaintext.WithJSONPath only with *text_in), so misuse panics rather than failing at compile time. That's inherent to lib.InputOption/lib.OutputOption being interface-typed; the type-switch + panic("unsupported input converter") in maxmind/common_in.go and special/cutter.go is at least explicit.

Verified with go build ./..., go vet ./..., go test ./..., plus lookup smoke tests for both -u and -d modes.

Co-authored-by: Loyalsoldier <10487845+Loyalsoldier@users.noreply.github.com>
@Codex

Codex AI commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

@codex[agent] Review this PR, try to refine it and find potential bugs.

Reviewed the PR and fixed a validation regression in b3bf254: JSON config creators now return validation errors instead of exiting via public constructors. Local go test ./..., go build ./, secret scan, and CodeQL validation passed.

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.

4 participants