codeberg.org/gruf/go-mangler@v1.3.0/extras/extras.go (about)

     1  package extras
     2  
     3  import (
     4  	"net"
     5  	"net/netip"
     6  	"net/url"
     7  	"reflect"
     8  	"time"
     9  	"unsafe"
    10  	_ "unsafe"
    11  
    12  	"codeberg.org/gruf/go-mangler"
    13  )
    14  
    15  func init() {
    16  	// Register standard library performant manglers.
    17  	mangler.Register(reflect.TypeOf(net.IPAddr{}), mangle_ipaddr)
    18  	mangler.Register(reflect.TypeOf(&net.IPAddr{}), mangle_ipaddr_ptr)
    19  	mangler.Register(reflect.TypeOf(netip.Addr{}), mangle_addr)
    20  	mangler.Register(reflect.TypeOf(&netip.Addr{}), mangle_addr_ptr)
    21  	mangler.Register(reflect.TypeOf(netip.AddrPort{}), mangle_addrport)
    22  	mangler.Register(reflect.TypeOf(&netip.AddrPort{}), mangle_addrport_ptr)
    23  	mangler.Register(reflect.TypeOf(time.Time{}), mangle_time)
    24  	mangler.Register(reflect.TypeOf(&time.Time{}), mangle_time_ptr)
    25  	mangler.Register(reflect.TypeOf(url.URL{}), mangle_url)
    26  	mangler.Register(reflect.TypeOf(&url.URL{}), mangle_url_ptr)
    27  }
    28  
    29  //go:linkname eface_data codeberg.org/gruf/go-mangler.eface_data
    30  func eface_data(a any) unsafe.Pointer
    31  
    32  //go:linkname append_uint32 codeberg.org/gruf/go-mangler.append_uint32
    33  func append_uint32([]byte, uint32) []byte
    34  
    35  //go:linkname append_uint64 codeberg.org/gruf/go-mangler.append_uint64
    36  func append_uint64([]byte, uint64) []byte
    37  
    38  //go:linkname time_sec time.(*Time).sec
    39  func time_sec(*time.Time) int64
    40  
    41  //go:linkname time_nsec time.(*Time).nsec
    42  func time_nsec(*time.Time) int32
    43  
    44  //go:linkname time_stripMono time.(*Time).stripMono
    45  func time_stripMono(*time.Time)
    46  
    47  func mangle_url(buf []byte, a any) []byte {
    48  	u := (*url.URL)(eface_data(a))
    49  	return append(buf, u.String()...)
    50  }
    51  
    52  func mangle_url_ptr(buf []byte, a any) []byte {
    53  	if ptr := (*url.URL)(eface_data(a)); ptr != nil {
    54  		s := ptr.String()
    55  		buf = append(buf, '1')
    56  		return append(buf, s...)
    57  	}
    58  	buf = append(buf, '0')
    59  	return buf
    60  }
    61  
    62  func mangle_time(buf []byte, a any) []byte {
    63  	t := *(*time.Time)(eface_data(a))
    64  	time_stripMono(&t) // force non-monotonic time value.
    65  	buf = append_uint64(buf, uint64(time_sec(&t)))
    66  	buf = append_uint32(buf, uint32(time_nsec(&t)))
    67  	return buf
    68  }
    69  
    70  func mangle_time_ptr(buf []byte, a any) []byte {
    71  	if ptr := (*time.Time)(eface_data(a)); ptr != nil {
    72  		t := *ptr
    73  		buf = append(buf, '1')
    74  		time_stripMono(&t) // force non-monotonic time value.
    75  		buf = append_uint64(buf, uint64(time_sec(&t)))
    76  		buf = append_uint32(buf, uint32(time_nsec(&t)))
    77  		return buf
    78  	}
    79  	buf = append(buf, '0')
    80  	return buf
    81  }
    82  
    83  func mangle_ipaddr(buf []byte, a any) []byte {
    84  	i := *(*net.IPAddr)(eface_data(a))
    85  	buf = append(buf, i.IP...)
    86  	buf = append(buf, i.Zone...)
    87  	return buf
    88  }
    89  
    90  func mangle_ipaddr_ptr(buf []byte, a any) []byte {
    91  	if ptr := (*net.IPAddr)(eface_data(a)); ptr != nil {
    92  		buf = append(buf, '1')
    93  		buf = append(buf, ptr.IP...)
    94  		buf = append(buf, ptr.Zone...)
    95  		return buf
    96  	}
    97  	buf = append(buf, '0')
    98  	return buf
    99  }
   100  
   101  func mangle_addr(buf []byte, a any) []byte {
   102  	i := (*netip.Addr)(eface_data(a))
   103  	b, _ := i.MarshalBinary()
   104  	return append(buf, b...)
   105  }
   106  
   107  func mangle_addr_ptr(buf []byte, a any) []byte {
   108  	if ptr := (*netip.Addr)(eface_data(a)); ptr != nil {
   109  		buf = append(buf, '1')
   110  		b, _ := ptr.MarshalBinary()
   111  		return append(buf, b...)
   112  	}
   113  	buf = append(buf, '0')
   114  	return buf
   115  }
   116  
   117  func mangle_addrport(buf []byte, a any) []byte {
   118  	i := (*netip.AddrPort)(eface_data(a))
   119  	b, _ := i.MarshalBinary()
   120  	return append(buf, b...)
   121  }
   122  
   123  func mangle_addrport_ptr(buf []byte, a any) []byte {
   124  	if ptr := (*netip.AddrPort)(eface_data(a)); ptr != nil {
   125  		buf = append(buf, '1')
   126  		b, _ := ptr.MarshalBinary()
   127  		return append(buf, b...)
   128  	}
   129  	buf = append(buf, '0')
   130  	return buf
   131  }