summaryrefslogtreecommitdiff
path: root/plugin/maxmind/common_in.go
diff options
context:
space:
mode:
authorcopilot-swe-agent[bot] <[email protected]>2026-04-28 18:24:00 +0000
committerGitHub <[email protected]>2026-04-28 18:24:00 +0000
commit8834c0be63ee88e0ad23fe621d5f5fe344b32089 (patch)
tree49029d2fd927e8832d05420d1bf1cf850aa22325 /plugin/maxmind/common_in.go
parent4f125e579472e5ed87fd052ef68ab80f5fe679b0 (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_in.go')
-rw-r--r--plugin/maxmind/common_in.go90
1 files changed, 61 insertions, 29 deletions
diff --git a/plugin/maxmind/common_in.go b/plugin/maxmind/common_in.go
index 3d97388c..a937ef98 100644
--- a/plugin/maxmind/common_in.go
+++ b/plugin/maxmind/common_in.go
@@ -14,46 +14,78 @@ var (
defaultIPInfoCountryMMDBFile = filepath.Join("./", "ipinfo", "country.mmdb")
)
-func newGeoLite2CountryMMDBIn(iType string, iDesc string, action lib.Action, data json.RawMessage) (lib.InputConverter, error) {
- var tmp struct {
- URI string `json:"uri"`
- Want []string `json:"wantedList"`
- OnlyIPType lib.IPType `json:"onlyIPType"`
+func NewGeoLite2CountryMMDBIn(iType string, iDesc string, action lib.Action, opts ...lib.InputOption) lib.InputConverter {
+ g := &geoLite2CountryMMDBIn{
+ Type: iType,
+ Action: action,
+ Description: iDesc,
}
- if len(data) > 0 {
- if err := json.Unmarshal(data, &tmp); err != nil {
- return nil, err
+ for _, opt := range opts {
+ if opt != nil {
+ opt(g)
}
}
- if tmp.URI == "" {
- switch iType {
- case TypeGeoLite2CountryMMDBIn:
- tmp.URI = defaultGeoLite2CountryMMDBFile
+ return g
+}
+
+func WithGeoLite2CountryMMDBInURI(iType, uri string) lib.InputOption {
+ return func(s lib.InputConverter) {
+ uri = strings.TrimSpace(uri)
+ if uri == "" {
+ switch iType {
+ case TypeGeoLite2CountryMMDBIn:
+ uri = defaultGeoLite2CountryMMDBFile
+ case TypeDBIPCountryMMDBIn:
+ uri = defaultDBIPCountryMMDBFile
+ case TypeIPInfoCountryMMDBIn:
+ uri = defaultIPInfoCountryMMDBFile
+ }
+ }
- case TypeDBIPCountryMMDBIn:
- tmp.URI = defaultDBIPCountryMMDBFile
+ s.(*geoLite2CountryMMDBIn).URI = uri
+ }
+}
- case TypeIPInfoCountryMMDBIn:
- tmp.URI = defaultIPInfoCountryMMDBFile
+func WithGeoLite2CountryMMDBInWantedList(lists []string) lib.InputOption {
+ return func(s lib.InputConverter) {
+ wantList := make(map[string]bool)
+ for _, want := range lists {
+ if want = strings.ToUpper(strings.TrimSpace(want)); want != "" {
+ wantList[want] = true
+ }
}
+
+ s.(*geoLite2CountryMMDBIn).Want = wantList
+ }
+}
+
+func WithGeoLite2CountryMMDBInOnlyIPType(onlyIPType lib.IPType) lib.InputOption {
+ return func(s lib.InputConverter) {
+ s.(*geoLite2CountryMMDBIn).OnlyIPType = onlyIPType
+ }
+}
+
+func NewGeoLite2CountryMMDBInFromBytes(iType string, iDesc string, action lib.Action, data []byte) (lib.InputConverter, error) {
+ var tmp struct {
+ URI string `json:"uri"`
+ Want []string `json:"wantedList"`
+ OnlyIPType lib.IPType `json:"onlyIPType"`
}
- // Filter want list
- wantList := make(map[string]bool)
- for _, want := range tmp.Want {
- if want = strings.ToUpper(strings.TrimSpace(want)); want != "" {
- wantList[want] = true
+ if len(data) > 0 {
+ if err := json.Unmarshal(data, &tmp); err != nil {
+ return nil, err
}
}
- return &GeoLite2CountryMMDBIn{
- Type: iType,
- Action: action,
- Description: iDesc,
- URI: tmp.URI,
- Want: wantList,
- OnlyIPType: tmp.OnlyIPType,
- }, nil
+ return NewGeoLite2CountryMMDBIn(
+ iType,
+ iDesc,
+ action,
+ WithGeoLite2CountryMMDBInURI(iType, tmp.URI),
+ WithGeoLite2CountryMMDBInWantedList(tmp.Want),
+ WithGeoLite2CountryMMDBInOnlyIPType(tmp.OnlyIPType),
+ ), nil
}