From f8e0d5e0a9c4faa8fa9e39ba328853c3a1e41522 Mon Sep 17 00:00:00 2001 From: Aoang Date: Thu, 17 Oct 2024 13:55:16 +0800 Subject: [PATCH] Add support type for net/netip.Addr and net/netip.Prefix (#1028) * feat(schema): add support type for net/netip.Addr and net/netip.Prefix * fix(schema): net.IPNet(not ptr) is not implement fmt.Stringer --- schema/append_value.go | 13 ++++++------- schema/reflect.go | 3 +++ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/schema/append_value.go b/schema/append_value.go index 48a0761be..a67b41e38 100644 --- a/schema/append_value.go +++ b/schema/append_value.go @@ -99,10 +99,10 @@ func appender(dialect Dialect, typ reflect.Type) AppenderFunc { return appendTimeValue case timePtrType: return PtrAppender(appendTimeValue) - case ipType: - return appendIPValue case ipNetType: return appendIPNetValue + case ipType, netipPrefixType, netipAddrType: + return appendStringer case jsonRawMessageType: return appendJSONRawMessageValue } @@ -247,16 +247,15 @@ func appendTimeValue(fmter Formatter, b []byte, v reflect.Value) []byte { return fmter.Dialect().AppendTime(b, tm) } -func appendIPValue(fmter Formatter, b []byte, v reflect.Value) []byte { - ip := v.Interface().(net.IP) - return fmter.Dialect().AppendString(b, ip.String()) -} - func appendIPNetValue(fmter Formatter, b []byte, v reflect.Value) []byte { ipnet := v.Interface().(net.IPNet) return fmter.Dialect().AppendString(b, ipnet.String()) } +func appendStringer(fmter Formatter, b []byte, v reflect.Value) []byte { + return fmter.Dialect().AppendString(b, v.Interface().(fmt.Stringer).String()) +} + func appendJSONRawMessageValue(fmter Formatter, b []byte, v reflect.Value) []byte { bytes := v.Bytes() if bytes == nil { diff --git a/schema/reflect.go b/schema/reflect.go index 89be8eeb6..75980b102 100644 --- a/schema/reflect.go +++ b/schema/reflect.go @@ -4,6 +4,7 @@ import ( "database/sql/driver" "encoding/json" "net" + "net/netip" "reflect" "time" ) @@ -14,6 +15,8 @@ var ( timeType = timePtrType.Elem() ipType = reflect.TypeOf((*net.IP)(nil)).Elem() ipNetType = reflect.TypeOf((*net.IPNet)(nil)).Elem() + netipPrefixType = reflect.TypeOf((*netip.Prefix)(nil)).Elem() + netipAddrType = reflect.TypeOf((*netip.Addr)(nil)).Elem() jsonRawMessageType = reflect.TypeOf((*json.RawMessage)(nil)).Elem() driverValuerType = reflect.TypeOf((*driver.Valuer)(nil)).Elem()