Skip to content

Refactor all plugins to functional options pattern#326

Draft
Copilot wants to merge 1 commit into
refactor-pluginfrom
copilot/refactor-plugins-functional-options
Draft

Refactor all plugins to functional options pattern#326
Copilot wants to merge 1 commit into
refactor-pluginfrom
copilot/refactor-plugins-functional-options

Conversation

Copilot AI commented Apr 28, 2026

Copy link
Copy Markdown

All plugin constructors previously used direct struct instantiation with exported types, mixing JSON parsing, validation, and construction logic in a single new* function. This refactors every plugin in plugin/ subdirectories to match the functional options pattern established by plugin/singbox/srs_in.go / srs_out.go.

Changes

Pattern applied uniformly across all plugins

  • Struct types made unexported (lowercase)
  • New*(action, opts...) constructor accepting variadic lib.InputOption / lib.OutputOption
  • With*() option functions per configurable field (with validation via log.Fatalf for required/exclusive options, matching the reference)
  • New*FromBytes(action, data) for JSON-based construction, called from init()

Shared-constructor plugins (maxmind, plaintext)

These packages share one struct across multiple registered types (e.g. geoLite2CountryMMDBIn serves maxmindMMDB, dbipCountryMMDB, ipinfoCountryMMDB). Constructors and option functions take iType/iDesc as explicit parameters to carry type identity:

// common_in.go
func NewGeoLite2CountryMMDBIn(iType, iDesc string, action lib.Action, opts ...lib.InputOption) lib.InputConverter

// dbip_country_mmdb_in.go init()
lib.RegisterInputConfigCreator(TypeDBIPCountryMMDBIn, func(action lib.Action, data json.RawMessage) (lib.InputConverter, error) {
    return NewGeoLite2CountryMMDBInFromBytes(TypeDBIPCountryMMDBIn, DescDBIPCountryMMDBIn, action, data)
})

Callers updated

lookup.go and merge.go (which previously used struct literals with exported fields) are updated to use the new constructors.

Files changed

  • plugin/mihomo: mrs_in.go, mrs_out.go
  • plugin/v2ray: dat_in.go, dat_out.go
  • plugin/special: cutter.go, lookup.go, private.go, stdin.go, stdout.go, test.go
  • plugin/maxmind: common_in.go, common_out.go, all *_mmdb_{in,out}.go, maxmind_asn_csv_in.go, maxmind_country_csv_in.go
  • plugin/plaintext: common_{in,out}.go, text_{in,out}.go, clash_{in,out}.go, surge_{in,out}.go, json_in.go
  • Root: lookup.go, merge.go

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