summaryrefslogtreecommitdiff
path: root/plugin/maxmind/common_in.go
diff options
context:
space:
mode:
authoropenai-code-agent[bot] <[email protected]>2026-04-28 17:48:08 +0000
committerGitHub <[email protected]>2026-04-28 17:48:08 +0000
commitbf3a08f53f060f9ff3503d204f9dc15cee0ff2a5 (patch)
treecdc7dc6acdeaca6d5185f1b1824e332bfc982caf /plugin/maxmind/common_in.go
parente25f9fcf96d9971e90c3a5df9087d882e4a997a7 (diff)
refactor(maxmind): use functional options for mmdb/csv pluginscodex/refactor-plugins-functional-options
Co-authored-by: Loyalsoldier <[email protected]>
Diffstat (limited to 'plugin/maxmind/common_in.go')
-rw-r--r--plugin/maxmind/common_in.go55
1 files changed, 38 insertions, 17 deletions
diff --git a/plugin/maxmind/common_in.go b/plugin/maxmind/common_in.go
index 3d97388c..1f2db85d 100644
--- a/plugin/maxmind/common_in.go
+++ b/plugin/maxmind/common_in.go
@@ -2,19 +2,48 @@ package maxmind
import (
"encoding/json"
+ "log"
"path/filepath"
"strings"
"github.com/Loyalsoldier/geoip/lib"
)
+func WithMMDBInURI(uri string) lib.InputOption {
+ return func(c lib.InputConverter) {
+ uri = strings.TrimSpace(uri)
+ if uri == "" {
+ log.Fatalf("❌ [type %s | action %s] missing uri", c.GetType(), c.GetAction())
+ }
+ c.(*GeoLite2CountryMMDBIn).URI = uri
+ }
+}
+
+func WithMMDBInWantedList(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.(*GeoLite2CountryMMDBIn).Want = wantList
+ }
+}
+
+func WithMMDBInOnlyIPType(onlyIPType lib.IPType) lib.InputOption {
+ return func(c lib.InputConverter) {
+ c.(*GeoLite2CountryMMDBIn).OnlyIPType = onlyIPType
+ }
+}
+
var (
defaultGeoLite2CountryMMDBFile = filepath.Join("./", "geolite2", "GeoLite2-Country.mmdb")
defaultDBIPCountryMMDBFile = filepath.Join("./", "db-ip", "dbip-country-lite.mmdb")
defaultIPInfoCountryMMDBFile = filepath.Join("./", "ipinfo", "country.mmdb")
)
-func newGeoLite2CountryMMDBIn(iType string, iDesc string, action lib.Action, data json.RawMessage) (lib.InputConverter, error) {
+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"`
@@ -40,20 +69,12 @@ func newGeoLite2CountryMMDBIn(iType string, iDesc string, action lib.Action, dat
}
}
- // Filter want list
- wantList := make(map[string]bool)
- for _, want := range tmp.Want {
- if want = strings.ToUpper(strings.TrimSpace(want)); want != "" {
- wantList[want] = true
- }
- }
-
- return &GeoLite2CountryMMDBIn{
- Type: iType,
- Action: action,
- Description: iDesc,
- URI: tmp.URI,
- Want: wantList,
- OnlyIPType: tmp.OnlyIPType,
- }, nil
+ return NewGeoLite2CountryMMDBIn(
+ iType,
+ iDesc,
+ action,
+ WithMMDBInURI(tmp.URI),
+ WithMMDBInWantedList(tmp.Want),
+ WithMMDBInOnlyIPType(tmp.OnlyIPType),
+ ), nil
}