From d611632acc1745679574ff20b3821a4abb96380f Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Thu, 5 Sep 2024 11:04:18 +0200 Subject: [PATCH] Shorten icingadb/v1.AddressBin#Value() --- pkg/icingadb/v1/host.go | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/pkg/icingadb/v1/host.go b/pkg/icingadb/v1/host.go index 47aaa7869..75e2d1752 100644 --- a/pkg/icingadb/v1/host.go +++ b/pkg/icingadb/v1/host.go @@ -1,7 +1,6 @@ package v1 import ( - "bytes" "database/sql/driver" "github.com/icinga/icinga-go-library/database" "github.com/icinga/icinga-go-library/types" @@ -28,23 +27,16 @@ type AddressBin struct { Host *Host `db:"-"` } -var v4InV6Prefix = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff} - // Value implements the driver.Valuer interface. func (ab AddressBin) Value() (driver.Value, error) { if ab.Host == nil { return nil, nil } - ip := net.ParseIP(ab.Host.Address) - if ip == nil { + if ip := net.ParseIP(ab.Host.Address).To4(); ip == nil { return nil, nil - } - - if ip = bytes.TrimPrefix(ip, v4InV6Prefix); len(ip) == 4 { - return []byte(ip), nil } else { - return nil, nil + return []byte(ip), nil } }