Skip to content

Commit

Permalink
feat(schema): add support type for net/netip.Addr and net/netip.Prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
Aoang committed Oct 14, 2024
1 parent 760de7d commit 3abcdc4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 13 deletions.
17 changes: 4 additions & 13 deletions schema/append_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package schema
import (
"database/sql/driver"
"fmt"
"net"
"reflect"
"strconv"
"strings"
Expand Down Expand Up @@ -99,10 +98,8 @@ 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, ipNetType, netipPrefixType, netipAddrType:
return appendStringer
case jsonRawMessageType:
return appendJSONRawMessageValue
}
Expand Down Expand Up @@ -247,14 +244,8 @@ 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 {
Expand Down
3 changes: 3 additions & 0 deletions schema/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"database/sql/driver"
"encoding/json"
"net"
"net/netip"
"reflect"
"time"
)
Expand All @@ -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()
Expand Down

0 comments on commit 3abcdc4

Please sign in to comment.