-
-
Notifications
You must be signed in to change notification settings - Fork 141
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
feat: added mapper for regex #427
Changes from 1 commit
f449f3d
8727b63
f068e3a
4f79649
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,9 +7,12 @@ import ( | |
"fmt" | ||
"io" | ||
"math/bits" | ||
"net" | ||
"net/netip" | ||
"net/url" | ||
"os" | ||
"reflect" | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
@@ -285,6 +288,11 @@ func (r *Registry) RegisterDefaults() *Registry { | |
RegisterType(reflect.TypeOf(time.Duration(0)), durationDecoder()). | ||
RegisterType(reflect.TypeOf(&url.URL{}), urlMapper()). | ||
RegisterType(reflect.TypeOf(&os.File{}), fileMapper(r)). | ||
RegisterType(reflect.TypeOf(®exp.Regexp{}), regexMapper()). | ||
RegisterType(reflect.TypeOf(&net.IP{}), netIPMapper()). | ||
RegisterType(reflect.TypeOf(&net.IPNet{}), netIPNetMapper()). | ||
RegisterType(reflect.TypeOf(netip.Addr{}), netipAddrMapper()). | ||
RegisterType(reflect.TypeOf(netip.Prefix{}), netipPrefixMapper()). | ||
RegisterName("path", pathMapper(r)). | ||
RegisterName("existingfile", existingFileMapper(r)). | ||
RegisterName("existingdir", existingDirMapper(r)). | ||
|
@@ -733,6 +741,102 @@ func fileContentMapper(r *Registry) MapperFunc { | |
} | ||
} | ||
|
||
func regexMapper() MapperFunc { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh boy, you're right! 🤦 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sorry for consuming you time 🤦 i removed all mappers. we can close those this pr. I added some regex tests in the last commit, if you have some time, maybe you can explain to me why the TestRegex,TestRegexSlice,TestRegexPointer are working but the TestRegexPointerSlice is not?
|
||
return func(ctx *DecodeContext, target reflect.Value) error { | ||
t, err := ctx.Scan.PopValue("regex") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var f *regexp.Regexp | ||
switch v := t.Value.(type) { | ||
case string: | ||
f, err = regexp.Compile(v) | ||
if err != nil { | ||
return fmt.Errorf("expected regular expression but got %q: %w", v, err) | ||
} | ||
default: | ||
return fmt.Errorf("expected string but got %q", v) | ||
} | ||
|
||
target.Set(reflect.ValueOf(f)) | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func netIPMapper() MapperFunc { | ||
return func(ctx *DecodeContext, target reflect.Value) error { | ||
var value string | ||
if err := ctx.Scan.PopValueInto("ip", &value); err != nil { | ||
return err | ||
} | ||
|
||
ip := net.ParseIP(value) | ||
if ip == nil { | ||
return fmt.Errorf("expected ip addresss but got %q", value) | ||
} | ||
|
||
target.Set(reflect.ValueOf(ip)) | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func netIPNetMapper() MapperFunc { | ||
return func(ctx *DecodeContext, target reflect.Value) error { | ||
var value string | ||
if err := ctx.Scan.PopValueInto("cidr", &value); err != nil { | ||
return err | ||
} | ||
|
||
_, ipnet, err := net.ParseCIDR(value) | ||
if err != nil { | ||
return fmt.Errorf("expected cidr but got %q: %w", value, err) | ||
} | ||
|
||
target.Set(reflect.ValueOf(ipnet)) | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func netipAddrMapper() MapperFunc { | ||
return func(ctx *DecodeContext, target reflect.Value) error { | ||
var value string | ||
if err := ctx.Scan.PopValueInto("ip", &value); err != nil { | ||
return err | ||
} | ||
|
||
ip, err := netip.ParseAddr(value) | ||
if err != nil { | ||
return fmt.Errorf("expected ip addresss but got %q: %w", value, err) | ||
} | ||
|
||
target.Set(reflect.ValueOf(ip)) | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func netipPrefixMapper() MapperFunc { | ||
return func(ctx *DecodeContext, target reflect.Value) error { | ||
var value string | ||
if err := ctx.Scan.PopValueInto("cidr", &value); err != nil { | ||
return err | ||
} | ||
|
||
prefix, err := netip.ParsePrefix(value) | ||
if err != nil { | ||
return fmt.Errorf("expected ipnet but got %q: %w", value, err) | ||
} | ||
|
||
target.Set(reflect.ValueOf(prefix)) | ||
|
||
return nil | ||
} | ||
} | ||
|
||
type ptrMapper struct { | ||
r *Registry | ||
} | ||
|
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.
sorry for the drive-by review, but are these two actually necessary? 🤔
they implement
enconding.TextUnmarshaller
and should work out of the box?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.
you are right, they work out of the box
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 removed the netip mappers from the pr, and cleanup my tests.
I thought about also removing the net mappers, and only add the regex mapper:
What do you think about that?
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.
Good question. Maybe both? I don't think there's any harm there.
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 like the idea of leading people to netip. Especially since the package already has convenience code for converting back and forth to net.IP and friends.
But that's just my 2¢