mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-18 22:49:52 +02:00
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:
@@ -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)
|
||||||
|
|||||||
Reference in New Issue
Block a user