Skip to content

Commit

Permalink
feat(reader): add CSVNetipAddrPorts method
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Nov 26, 2023
1 parent 06abcf0 commit 793cd95
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
24 changes: 23 additions & 1 deletion internal/parse/netip.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,32 @@ func NetipPrefix(sources []Source, key string,
// - Trim quotes.
// - Force lowercase.
func CSVNetipAddresses(sources []Source, key string,
options ...Option) (prefixes []netip.Addr, err error) {
options ...Option) (addresses []netip.Addr, err error) {
return CSVParse(sources, key, netip.ParseAddr, options...)
}

// CSVNetipAddrPorts returns a slice of netip.AddrPort from the
// first comma separated string value found at the given key
// from the given sources in order, and returns an error if
// any value is not a valid netip.AddrPort string.
//
// The slice is returned as `nil` if:
// - the key given is NOT set in any of the sources.
// - By default and unless changed by the AcceptEmpty option,
// if the key is set and its corresponding value is empty.
//
// The entire CSV string value may be modified depending on the
// parse default settings and the parse options given.
// The parse default settings are to:
// - Trim line endings suffixes \r\n and \n.
// - Trim spaces.
// - Trim quotes.
// - Force lowercase.
func CSVNetipAddrPorts(sources []Source, key string,
options ...Option) (addrPorts []netip.AddrPort, err error) {
return CSVParse(sources, key, netip.ParseAddrPort, options...)
}

// CSVNetipPrefixes returns a slice of netip.Prefix from
// the first comma separated string value found at the given
// key from the given sources in order, and returns an error
Expand Down
24 changes: 23 additions & 1 deletion reader/netip.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,33 @@ func (r *Reader) NetipPrefix(key string, options ...Option) (
// - Trim quotes.
// - Force lowercase.
func (r *Reader) CSVNetipAddresses(key string, options ...Option) (
prefixes []netip.Addr, err error) {
addresses []netip.Addr, err error) {
parseOptions := r.makeParseOptions(options)
return parse.CSVNetipAddresses(r.sources, key, parseOptions...)
}

// CSVNetipAddrPorts returns a slice of netip.AddrPort from a
// comma separated value found at the given key and returns an
// error if any value is not a valid netip.AddrPort string.
//
// The slice is returned as `nil` if:
// - the given key is NOT set.
// - By default and unless changed by the AcceptEmpty option,
// if the key is set and its corresponding value is empty.
//
// The entire CSV string value may be modified depending on the
// parse default settings and the parse options given.
// The parse default settings are to:
// - Trim line endings suffixes \r\n and \n.
// - Trim spaces.
// - Trim quotes.
// - Force lowercase.
func (r *Reader) CSVNetipAddrPorts(key string, options ...Option) (
addrPorts []netip.AddrPort, err error) {
parseOptions := r.makeParseOptions(options)
return parse.CSVNetipAddrPorts(r.sources, key, parseOptions...)
}

// CSVNetipPrefixes returns a slice of netip.Prefix from a comma separated value
// found at the given key and returns an error if any value
// is not a valid netip.Prefix string.
Expand Down

0 comments on commit 793cd95

Please sign in to comment.