Skip to content

Commit

Permalink
Shorten icingadb/v1.AddressBin#Value()
Browse files Browse the repository at this point in the history
  • Loading branch information
Al2Klimov committed Sep 5, 2024
1 parent 277a61d commit d611632
Showing 1 changed file with 2 additions and 10 deletions.
12 changes: 2 additions & 10 deletions pkg/icingadb/v1/host.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
}
}

Expand Down

0 comments on commit d611632

Please sign in to comment.