diff options
| author | anthropic-code-agent[bot] <[email protected]> | 2026-04-28 18:08:20 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2026-04-28 18:08:20 +0000 |
| commit | fbc2f20cdfc138b0146fcc61bcb7ed2c1776a076 (patch) | |
| tree | fada69f06625575fbda4a0533dd95316d7d0d2b6 /plugin/special/cutter.go | |
| parent | 6fbcfeba5b73e4ac50ab0257040103cadf524ea7 (diff) | |
Complete functional options pattern refactoring for special pluginclaude/refactor-plugins-functional-options
Agent-Logs-Url: https://github.com/Loyalsoldier/geoip/sessions/0483b641-15da-4630-a3ad-53f8d1c65fe9
Co-authored-by: Loyalsoldier <[email protected]>
Diffstat (limited to 'plugin/special/cutter.go')
| -rw-r--r-- | plugin/special/cutter.go | 62 |
1 files changed, 47 insertions, 15 deletions
diff --git a/plugin/special/cutter.go b/plugin/special/cutter.go index 509cddbb..0ef88ea5 100644 --- a/plugin/special/cutter.go +++ b/plugin/special/cutter.go @@ -15,14 +15,48 @@ const ( func init() { lib.RegisterInputConfigCreator(TypeCutter, func(action lib.Action, data json.RawMessage) (lib.InputConverter, error) { - return newCutter(action, data) + return NewCutterFromBytes(action, data) }) - lib.RegisterInputConverter(TypeCutter, &Cutter{ + lib.RegisterInputConverter(TypeCutter, &cutter{ Description: DescCutter, }) } -func newCutter(action lib.Action, data json.RawMessage) (lib.InputConverter, error) { +func NewCutter(action lib.Action, opts ...lib.InputOption) lib.InputConverter { + c := &cutter{ + Type: TypeCutter, + Action: action, + Description: DescCutter, + } + + for _, opt := range opts { + if opt != nil { + opt(c) + } + } + + return c +} + +func WithCutterWantedList(lists []string) lib.InputOption { + return func(c lib.InputConverter) { + wantList := make(map[string]bool) + for _, want := range lists { + if want = strings.ToUpper(strings.TrimSpace(want)); want != "" { + wantList[want] = true + } + } + c.(*cutter).Want = wantList + } +} + +func WithCutterOnlyIPType(onlyIPType lib.IPType) lib.InputOption { + return func(c lib.InputConverter) { + c.(*cutter).OnlyIPType = onlyIPType + } +} + +func NewCutterFromBytes(action lib.Action, data []byte) (lib.InputConverter, error) { var tmp struct { Want []string `json:"wantedList"` OnlyIPType lib.IPType `json:"onlyIPType"` @@ -50,16 +84,14 @@ func newCutter(action lib.Action, data json.RawMessage) (lib.InputConverter, err return nil, fmt.Errorf("❌ [type %s] wantedList must be specified", TypeCutter) } - return &Cutter{ - Type: TypeCutter, - Action: action, - Description: DescCutter, - Want: wantList, - OnlyIPType: tmp.OnlyIPType, - }, nil + return NewCutter( + action, + WithCutterWantedList(tmp.Want), + WithCutterOnlyIPType(tmp.OnlyIPType), + ), nil } -type Cutter struct { +type cutter struct { Type string Action lib.Action Description string @@ -67,19 +99,19 @@ type Cutter struct { OnlyIPType lib.IPType } -func (c *Cutter) GetType() string { +func (c *cutter) GetType() string { return c.Type } -func (c *Cutter) GetAction() lib.Action { +func (c *cutter) GetAction() lib.Action { return c.Action } -func (c *Cutter) GetDescription() string { +func (c *cutter) GetDescription() string { return c.Description } -func (c *Cutter) Input(container lib.Container) (lib.Container, error) { +func (c *cutter) Input(container lib.Container) (lib.Container, error) { ignoreIPType := lib.GetIgnoreIPType(c.OnlyIPType) for entry := range container.Loop() { |
