Skip to content

Commit 9a48455

Browse files
committed
Add Parse*List and MustParse*List variants, replace ParseCIDRs
1 parent 07fddf8 commit 9a48455

File tree

3 files changed

+62
-57
lines changed

3 files changed

+62
-57
lines changed

net/v2/net.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,6 @@ import (
2525
"strconv"
2626
)
2727

28-
// ParseCIDRs parses a list of cidrs and return error if any is invalid.
29-
// order is maintained
30-
func ParseCIDRs(cidrsString []string) ([]*net.IPNet, error) {
31-
cidrs := make([]*net.IPNet, 0, len(cidrsString))
32-
for i, cidrString := range cidrsString {
33-
cidr, err := ParseIPNet(cidrString)
34-
if err != nil {
35-
return nil, fmt.Errorf("invalid CIDR[%d]: %v (%v)", i, cidr, err)
36-
}
37-
cidrs = append(cidrs, cidr)
38-
}
39-
return cidrs, nil
40-
}
41-
4228
// ParsePort parses a string representing an IP port. If the string is not a
4329
// valid port number, this returns an error.
4430
func ParsePort(port string, allowZero bool) (int, error) {

net/v2/net_test.go

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -20,49 +20,6 @@ import (
2020
"testing"
2121
)
2222

23-
func TestParseCIDRs(t *testing.T) {
24-
testCases := []struct {
25-
cidrs []string
26-
errString string
27-
errorExpected bool
28-
}{
29-
{
30-
cidrs: []string{},
31-
errString: "should not return an error for an empty slice",
32-
errorExpected: false,
33-
},
34-
{
35-
cidrs: []string{"10.0.0.0/8", "not-a-valid-cidr", "2000::/10"},
36-
errString: "should return error for bad cidr",
37-
errorExpected: true,
38-
},
39-
{
40-
cidrs: []string{"10.0.0.0/8", "2000::/10"},
41-
errString: "should not return error for good cidrs",
42-
errorExpected: false,
43-
},
44-
}
45-
46-
for _, tc := range testCases {
47-
cidrs, err := ParseCIDRs(tc.cidrs)
48-
if tc.errorExpected {
49-
if err == nil {
50-
t.Errorf("%v", tc.errString)
51-
}
52-
continue
53-
}
54-
if err != nil {
55-
t.Errorf("%v error:%v", tc.errString, err)
56-
}
57-
58-
// validate lengths
59-
if len(cidrs) != len(tc.cidrs) {
60-
t.Errorf("cidrs should be of the same lengths %v != %v", len(cidrs), len(tc.cidrs))
61-
}
62-
63-
}
64-
}
65-
6623
func TestParsePort(t *testing.T) {
6724
var tests = []struct {
6825
name string

net/v2/parse.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,3 +195,65 @@ var MustParseIPAsIPNet = must(ParseIPAsIPNet)
195195

196196
// MustParseAddrAsPrefix is like ParseAddrAsPrefix, but it panics on error instead of returning an error value.
197197
var MustParseAddrAsPrefix = must(ParseAddrAsPrefix)
198+
199+
type listParser[T any] func(...string) ([]T, error)
200+
201+
func list[T any](parse parser[T]) listParser[T] {
202+
return func(strs ...string) ([]T, error) {
203+
var err error
204+
ret := make([]T, len(strs))
205+
for i, str := range strs {
206+
ret[i], err = parse(str)
207+
if err != nil {
208+
return nil, err
209+
}
210+
}
211+
return ret, nil
212+
}
213+
}
214+
215+
// ParseIPList parses a list of strings with ParseIP and returns a []net.IP or an error.
216+
var ParseIPList = list(ParseIP)
217+
218+
// ParseIPNetList parses a list of strings with ParseIPNet and returns a []*net.IPNet or an error.
219+
var ParseIPNetList = list(ParseIPNet)
220+
221+
// ParseAddrList parses a list of strings with ParseAddr and returns a []netip.Addr or an error.
222+
var ParseAddrList = list(ParseAddr)
223+
224+
// ParsePrefixList parses a list of strings with ParsePrefix and returns a []netip.Prefix or an error.
225+
var ParsePrefixList = list(ParsePrefix)
226+
227+
// ParseIPAsIPNetList parses a list of strings with ParseIPAsIPNet and returns a []*net.IPNet or an error.
228+
var ParseIPAsIPNetList = list(ParseIPAsIPNet)
229+
230+
// ParseAddrAsPrefixList parses a list of strings with ParseAddrAsPrefix and returns a []netip.Prefix or an error.
231+
var ParseAddrAsPrefixList = list(ParseAddrAsPrefix)
232+
233+
func mustlist[T any](parse listParser[T]) func(...string) []T {
234+
return func(strs ...string) []T {
235+
ret, err := parse(strs...)
236+
if err != nil {
237+
panic(err)
238+
}
239+
return ret
240+
}
241+
}
242+
243+
// MustParseIPList parses a list of strings with ParseIP and returns a []net.IP or else panics on error.
244+
var MustParseIPList = mustlist(ParseIPList)
245+
246+
// MustParseIPNetList parses a list of strings with ParseIPNet and returns a []*net.IPNet or else panics on error.
247+
var MustParseIPNetList = mustlist(ParseIPNetList)
248+
249+
// MustParseAddrList parses a list of strings with ParseAddr and returns a []netip.Addr or else panics on error.
250+
var MustParseAddrList = mustlist(ParseAddrList)
251+
252+
// MustParsePrefixList parses a list of strings with ParsePrefix and returns a []netip.Prefix or else panics on error.
253+
var MustParsePrefixList = mustlist(ParsePrefixList)
254+
255+
// MustParseIPAsIPNetList parses a list of strings with ParseIPAsIPNet and returns a []*net.IPNet or else panics on error.
256+
var MustParseIPAsIPNetList = mustlist(ParseIPAsIPNetList)
257+
258+
// MustParseAddrAsPrefixList parses a list of strings with ParseAddrAsPrefix and returns a []netip.Prefix or else panics on error.
259+
var MustParseAddrAsPrefixList = mustlist(ParseAddrAsPrefixList)

0 commit comments

Comments
 (0)