fix(acl): correctly marshal matchers instead of plain '{}'

- Introduced a raw field in the Matcher struct to store the original string representation.
- Implemented MarshalText method for Matcher
This commit is contained in:
yusing
2026-01-21 22:53:00 +08:00
parent 110fe4b0aa
commit 732376328c

View File

@@ -1,6 +1,7 @@
package acl package acl
import ( import (
"bytes"
"net" "net"
"strings" "strings"
@@ -12,6 +13,7 @@ type MatcherFunc func(*maxmind.IPInfo) bool
type Matcher struct { type Matcher struct {
match MatcherFunc match MatcherFunc
raw string
} }
type Matchers []Matcher type Matchers []Matcher
@@ -46,6 +48,7 @@ func (matcher *Matcher) Parse(s string) error {
if len(parts) != 2 { if len(parts) != 2 {
return errSyntax return errSyntax
} }
matcher.raw = s
switch parts[0] { switch parts[0] {
case MatcherTypeIP: case MatcherTypeIP:
@@ -79,6 +82,18 @@ func (matchers Matchers) Match(ip *maxmind.IPInfo) bool {
return false return false
} }
func (matchers Matchers) MarshalText() ([]byte, error) {
if len(matchers) == 0 {
return []byte("[]"), nil
}
var buf bytes.Buffer
for _, m := range matchers {
buf.WriteString(m.raw)
buf.WriteByte('\n')
}
return buf.Bytes(), nil
}
func matchIP(ip net.IP) MatcherFunc { func matchIP(ip net.IP) MatcherFunc {
return func(ip2 *maxmind.IPInfo) bool { return func(ip2 *maxmind.IPInfo) bool {
return ip.Equal(ip2.IP) return ip.Equal(ip2.IP)