-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
lib/options: BlacklistIPs json encoding/decoding #2085
Conversation
Codecov Report
@@ Coverage Diff @@
## master #2085 +/- ##
==========================================
- Coverage 72.11% 72.10% -0.02%
==========================================
Files 180 180
Lines 14243 14251 +8
==========================================
+ Hits 10272 10276 +4
- Misses 3346 3349 +3
- Partials 625 626 +1
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
lib/options.go
Outdated
// UnmarshalJSON decodes a JSON representation of IPNet using CIDR notation. | ||
func (ipnet *IPNet) UnmarshalJSON(b []byte) error { | ||
// Remove quotes and fallback to unmarshal text | ||
return ipnet.UnmarshalText([]byte(strings.Trim(string(b), `"`))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is bytes.Trim.
lib/options.go
Outdated
// MarshalJSON returns the JSON representation of IPNet using CIDR notation. | ||
func (ipnet *IPNet) MarshalJSON() ([]byte, error) { | ||
return []byte(fmt.Sprintf("%q", ipnet.String())), nil | ||
} | ||
|
||
// UnmarshalJSON decodes a JSON representation of IPNet using CIDR notation. | ||
func (ipnet *IPNet) UnmarshalJSON(b []byte) error { | ||
// Remove quotes and fallback to unmarshal text | ||
return ipnet.UnmarshalText([]byte(strings.Trim(string(b), `"`))) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is fine, but in reality, all you need is MarshalText as it seems that we fall back to that and if you just replace all of that with MarshalText that just return []byte(ipnet.String())
it seems still to work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not certain why the only MarshalText works fine, but if it does and it's simpler it's probably better then adding 2 methods ;)
0e977d9
to
0942987
Compare
@mstoykov I agree, some tests I did confuse me. The problem is just in the encoding part and we can fall back on |
The BlacklistIP configuration uses the CIDR notation. Implemented the TextMarshaler interface to support correct encoding based on the notation.
Support JSON encoding for the
BlacklistIPs
option.Closes #2083