diff options
| author | copilot-swe-agent[bot] <[email protected]> | 2026-04-28 18:24:00 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2026-04-28 18:24:00 +0000 |
| commit | 8834c0be63ee88e0ad23fe621d5f5fe344b32089 (patch) | |
| tree | 49029d2fd927e8832d05420d1bf1cf850aa22325 /plugin/maxmind/common_out.go | |
| parent | 4f125e579472e5ed87fd052ef68ab80f5fe679b0 (diff) | |
Refactor all plugins to use functional options patterncopilot/refactor-plugins-functional-options
Agent-Logs-Url: https://github.com/Loyalsoldier/geoip/sessions/e2b66c9a-3d01-490c-9b31-32109cfe4feb
Co-authored-by: Loyalsoldier <[email protected]>
Diffstat (limited to 'plugin/maxmind/common_out.go')
| -rw-r--r-- | plugin/maxmind/common_out.go | 121 |
1 files changed, 89 insertions, 32 deletions
diff --git a/plugin/maxmind/common_out.go b/plugin/maxmind/common_out.go index 21063faa..9b809f74 100644 --- a/plugin/maxmind/common_out.go +++ b/plugin/maxmind/common_out.go @@ -100,7 +100,82 @@ func (d dbipCountry) HasData() bool { return d != zeroDBIPCountry } -func newGeoLite2CountryMMDBOut(iType string, iDesc string, action lib.Action, data json.RawMessage) (lib.OutputConverter, error) { +func NewGeoLite2CountryMMDBOut(iType string, iDesc string, action lib.Action, opts ...lib.OutputOption) lib.OutputConverter { + g := &geoLite2CountryMMDBOut{ + Type: iType, + Action: action, + Description: iDesc, + } + + for _, opt := range opts { + if opt != nil { + opt(g) + } + } + + return g +} + +func WithGeoLite2CountryMMDBOutOutputName(name string) lib.OutputOption { + return func(s lib.OutputConverter) { + name = strings.TrimSpace(name) + if name == "" { + name = defaultGeoLite2CountryMMDBOutputName + } + + s.(*geoLite2CountryMMDBOut).OutputName = name + } +} + +func WithGeoLite2CountryMMDBOutOutputDir(iType, dir string) lib.OutputOption { + return func(s lib.OutputConverter) { + dir = strings.TrimSpace(dir) + if dir == "" { + switch iType { + case TypeGeoLite2CountryMMDBOut: + dir = defaultMaxmindOutputDir + case TypeDBIPCountryMMDBOut: + dir = defaultDBIPOutputDir + case TypeIPInfoCountryMMDBOut: + dir = defaultIPInfoOutputDir + } + } + + s.(*geoLite2CountryMMDBOut).OutputDir = dir + } +} + +func WithGeoLite2CountryMMDBOutWantedList(lists []string) lib.OutputOption { + return func(s lib.OutputConverter) { + s.(*geoLite2CountryMMDBOut).Want = lists + } +} + +func WithGeoLite2CountryMMDBOutOverwriteList(lists []string) lib.OutputOption { + return func(s lib.OutputConverter) { + s.(*geoLite2CountryMMDBOut).Overwrite = lists + } +} + +func WithGeoLite2CountryMMDBOutExcludedList(lists []string) lib.OutputOption { + return func(s lib.OutputConverter) { + s.(*geoLite2CountryMMDBOut).Exclude = lists + } +} + +func WithGeoLite2CountryMMDBOutOnlyIPType(onlyIPType lib.IPType) lib.OutputOption { + return func(s lib.OutputConverter) { + s.(*geoLite2CountryMMDBOut).OnlyIPType = onlyIPType + } +} + +func WithGeoLite2CountryMMDBOutSourceMMDBURI(uri string) lib.OutputOption { + return func(s lib.OutputConverter) { + s.(*geoLite2CountryMMDBOut).SourceMMDBURI = uri + } +} + +func NewGeoLite2CountryMMDBOutFromBytes(iType string, iDesc string, action lib.Action, data []byte) (lib.OutputConverter, error) { var tmp struct { OutputName string `json:"outputName"` OutputDir string `json:"outputDir"` @@ -118,39 +193,21 @@ func newGeoLite2CountryMMDBOut(iType string, iDesc string, action lib.Action, da } } - if tmp.OutputName == "" { - tmp.OutputName = defaultGeoLite2CountryMMDBOutputName - } - - if tmp.OutputDir == "" { - switch iType { - case TypeGeoLite2CountryMMDBOut: - tmp.OutputDir = defaultMaxmindOutputDir - - case TypeDBIPCountryMMDBOut: - tmp.OutputDir = defaultDBIPOutputDir - - case TypeIPInfoCountryMMDBOut: - tmp.OutputDir = defaultIPInfoOutputDir - } - } - - return &GeoLite2CountryMMDBOut{ - Type: iType, - Action: action, - Description: iDesc, - OutputName: tmp.OutputName, - OutputDir: tmp.OutputDir, - Want: tmp.Want, - Overwrite: tmp.Overwrite, - Exclude: tmp.Exclude, - OnlyIPType: tmp.OnlyIPType, - - SourceMMDBURI: tmp.SourceMMDBURI, - }, nil + return NewGeoLite2CountryMMDBOut( + iType, + iDesc, + action, + WithGeoLite2CountryMMDBOutOutputName(tmp.OutputName), + WithGeoLite2CountryMMDBOutOutputDir(iType, tmp.OutputDir), + WithGeoLite2CountryMMDBOutWantedList(tmp.Want), + WithGeoLite2CountryMMDBOutOverwriteList(tmp.Overwrite), + WithGeoLite2CountryMMDBOutExcludedList(tmp.Exclude), + WithGeoLite2CountryMMDBOutOnlyIPType(tmp.OnlyIPType), + WithGeoLite2CountryMMDBOutSourceMMDBURI(tmp.SourceMMDBURI), + ), nil } -func (g *GeoLite2CountryMMDBOut) GetExtraInfo() (map[string]any, error) { +func (g *geoLite2CountryMMDBOut) GetExtraInfo() (map[string]any, error) { if strings.TrimSpace(g.SourceMMDBURI) == "" { return nil, nil } |
