diff options
| author | copilot-swe-agent[bot] <[email protected]> | 2026-03-09 06:35:47 +0000 |
|---|---|---|
| committer | copilot-swe-agent[bot] <[email protected]> | 2026-03-09 06:35:47 +0000 |
| commit | 2600825c50d7e7f2b4427674f1aefca270bcca27 (patch) | |
| tree | 18f216c6af28fd3975a0665d75ccf7b5c0078cb6 /plugin/special/lookup.go | |
| parent | 5a14eb90f575b983aa407341af91aa7654e65f12 (diff) | |
Refactor: all plugin subdirectories use option patterncopilot/modify-plugin-files-subdirectories
Apply the same functional options pattern from plugin/singbox to:
- plugin/mihomo (mrs_in.go, mrs_out.go)
- plugin/plaintext (text_in.go, common_in.go, common_out.go, text_out.go, clash_in.go, clash_out.go, json_in.go, surge_in.go, surge_out.go)
- plugin/maxmind (all input/output files)
- plugin/v2ray (dat_in.go, dat_out.go)
- plugin/special (cutter.go, lookup.go, private.go, stdin.go, stdout.go)
- lookup.go and merge.go updated to use new constructors
Co-authored-by: Loyalsoldier <[email protected]>
Diffstat (limited to 'plugin/special/lookup.go')
| -rw-r--r-- | plugin/special/lookup.go | 70 |
1 files changed, 48 insertions, 22 deletions
diff --git a/plugin/special/lookup.go b/plugin/special/lookup.go index 96735146..642c39e3 100644 --- a/plugin/special/lookup.go +++ b/plugin/special/lookup.go @@ -18,14 +18,50 @@ const ( func init() { lib.RegisterOutputConfigCreator(TypeLookup, func(action lib.Action, data json.RawMessage) (lib.OutputConverter, error) { - return newLookup(action, data) + return NewLookupFromBytes(action, data) }) - lib.RegisterOutputConverter(TypeLookup, &Lookup{ + lib.RegisterOutputConverter(TypeLookup, &lookup{ Description: DescLookup, }) } -func newLookup(action lib.Action, data json.RawMessage) (lib.OutputConverter, error) { +type lookup struct { + Type string + Action lib.Action + Description string + Search string + SearchList []string +} + +func NewLookup(action lib.Action, opts ...lib.OutputOption) lib.OutputConverter { + l := &lookup{ + Type: TypeLookup, + Action: action, + Description: DescLookup, + } + + for _, opt := range opts { + if opt != nil { + opt(l) + } + } + + return l +} + +func WithSearch(search string) lib.OutputOption { + return func(l lib.OutputConverter) { + l.(*lookup).Search = strings.TrimSpace(search) + } +} + +func WithSearchList(searchList []string) lib.OutputOption { + return func(l lib.OutputConverter) { + l.(*lookup).SearchList = searchList + } +} + +func NewLookupFromBytes(action lib.Action, data []byte) (lib.OutputConverter, error) { var tmp struct { Search string `json:"search"` SearchList []string `json:"searchList"` @@ -42,36 +78,26 @@ func newLookup(action lib.Action, data json.RawMessage) (lib.OutputConverter, er return nil, fmt.Errorf("❌ [type %s | action %s] please specify an IP or a CIDR as search target", TypeLookup, action) } - return &Lookup{ - Type: TypeLookup, - Action: action, - Description: DescLookup, - Search: tmp.Search, - SearchList: tmp.SearchList, - }, nil -} - -type Lookup struct { - Type string - Action lib.Action - Description string - Search string - SearchList []string + return NewLookup( + action, + WithSearch(tmp.Search), + WithSearchList(tmp.SearchList), + ), nil } -func (l *Lookup) GetType() string { +func (l *lookup) GetType() string { return l.Type } -func (l *Lookup) GetAction() lib.Action { +func (l *lookup) GetAction() lib.Action { return l.Action } -func (l *Lookup) GetDescription() string { +func (l *lookup) GetDescription() string { return l.Description } -func (l *Lookup) Output(container lib.Container) error { +func (l *lookup) Output(container lib.Container) error { switch strings.Contains(l.Search, "/") { case true: // CIDR if _, err := netip.ParsePrefix(l.Search); err != nil { |
