From c3dd0bbaa7a06dbfc2c263c1412ab5290bfa7872 Mon Sep 17 00:00:00 2001 From: "openai-code-agent[bot]" <242516109+Codex@users.noreply.github.com> Date: Tue, 28 Apr 2026 17:40:50 +0000 Subject: refactor(mihomo): use functional options for MRSOut Co-authored-by: Loyalsoldier <10487845+Loyalsoldier@users.noreply.github.com> --- plugin/mihomo/mrs_out.go | 68 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 53 insertions(+), 15 deletions(-) (limited to 'plugin') diff --git a/plugin/mihomo/mrs_out.go b/plugin/mihomo/mrs_out.go index e8d65e9f..27e8c624 100644 --- a/plugin/mihomo/mrs_out.go +++ b/plugin/mihomo/mrs_out.go @@ -27,14 +27,58 @@ var ( func init() { lib.RegisterOutputConfigCreator(TypeMRSOut, func(action lib.Action, data json.RawMessage) (lib.OutputConverter, error) { - return newMRSOut(action, data) + return NewMRSOutFromBytes(action, data) }) lib.RegisterOutputConverter(TypeMRSOut, &MRSOut{ Description: DescMRSOut, }) } -func newMRSOut(action lib.Action, data json.RawMessage) (lib.OutputConverter, error) { +func NewMRSOut(action lib.Action, opts ...lib.OutputOption) lib.OutputConverter { + m := &MRSOut{ + Type: TypeMRSOut, + Action: action, + Description: DescMRSOut, + OutputDir: defaultOutputDir, + } + + for _, opt := range opts { + if opt != nil { + opt(m) + } + } + + return m +} + +func WithMRSOutputDir(dir string) lib.OutputOption { + return func(c lib.OutputConverter) { + dir = strings.TrimSpace(dir) + if dir != "" { + c.(*MRSOut).OutputDir = dir + } + } +} + +func WithMRSOutputWantedList(lists []string) lib.OutputOption { + return func(c lib.OutputConverter) { + c.(*MRSOut).Want = lists + } +} + +func WithMRSOutputExcludedList(lists []string) lib.OutputOption { + return func(c lib.OutputConverter) { + c.(*MRSOut).Exclude = lists + } +} + +func WithMRSOutputOnlyIPType(onlyIPType lib.IPType) lib.OutputOption { + return func(c lib.OutputConverter) { + c.(*MRSOut).OnlyIPType = onlyIPType + } +} + +func NewMRSOutFromBytes(action lib.Action, data []byte) (lib.OutputConverter, error) { var tmp struct { OutputDir string `json:"outputDir"` Want []string `json:"wantedList"` @@ -48,19 +92,13 @@ func newMRSOut(action lib.Action, data json.RawMessage) (lib.OutputConverter, er } } - if tmp.OutputDir == "" { - tmp.OutputDir = defaultOutputDir - } - - return &MRSOut{ - Type: TypeMRSOut, - Action: action, - Description: DescMRSOut, - OutputDir: tmp.OutputDir, - Want: tmp.Want, - Exclude: tmp.Exclude, - OnlyIPType: tmp.OnlyIPType, - }, nil + return NewMRSOut( + action, + WithMRSOutputDir(tmp.OutputDir), + WithMRSOutputWantedList(tmp.Want), + WithMRSOutputExcludedList(tmp.Exclude), + WithMRSOutputOnlyIPType(tmp.OnlyIPType), + ), nil } type MRSOut struct { -- cgit v1.3.1