dataconv

package
v1.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 14, 2026 License: GPL-3.0 Imports: 6 Imported by: 0

README

dataconv

Import path: github.com/InsideGallery/core/dataconv

Overview

dataconv contains IP conversion helpers and a struct/map merge wrapper used by shared services.

Main APIs

  • IPV4ToIPV6, IPV6ToString, IP2Int, IPv4ToInt, IPv6ToInt, and IPv6ToBigInt convert IP values to string, integer, or high/low forms.
  • IntToIPv4, IntToIPv6, and BigIntToIPv6 convert integer forms back to net.IP.
  • ParseIP wraps net.ParseIP and also returns the IP byte length (net.IPv4len or net.IPv6len).
  • CutIP masks the last byte of a parsed IP address and returns an empty string for empty or invalid input.
  • MergeStruct wraps mergo.Merge to fill zero values in a destination from a source.
  • ErrInvalidIPAddress, ErrNotIPv4Address, and ErrNotIPv6Address identify parse and version failures.

Usage

ip := net.ParseIP("192.168.1.1")

value, err := dataconv.IPv4ToInt(ip)
if err != nil {
	return err
}

sameIP := dataconv.IntToIPv4(value)
masked := dataconv.CutIP(ip.String())

_ = sameIP
_ = masked

Notes

IPv4ToInt returns ErrNotIPv4Address when To4 fails. ParseIP returns ErrInvalidIPAddress with a nil IP and zero length for invalid input. MergeStruct requires a pointer destination; existing non-zero destination fields are preserved by mergo.

Documentation

Index

Constants

View Source
const (
	ArrayLenForIP = 8
	LowPosition   = 8
	HighPosition  = 16

	IPV6Prefix = "::ffff:"

	IPBytesCut = 15 // 15 bytes (120 bits)
)

Variables

View Source
var (
	ErrInvalidIPAddress = errors.New("invalid ip address")
	ErrNotIPv4Address   = errors.New("not an IPv4 address")
	ErrNotIPv6Address   = errors.New("not an IPv6 address")
)

Functions

func BigIntToIPv6

func BigIntToIPv6(ipaddr big.Int) net.IP

BigIntToIPv6 converts IP address of version 6 from big integer to net.IP representation.

func CutIP

func CutIP(ip string) string

func IP2Int

func IP2Int(ip net.IP) *big.Int

IP2Int convert any net.IP to uint64

func IPV4ToIPV6

func IPV4ToIPV6(ipv4 string) string

func IPV6ToString

func IPV6ToString(ipv6 *big.Int) string

func IPv4ToInt

func IPv4ToInt(ipaddr net.IP) (uint32, error)

IPv4ToInt converts IP address of version 4 from net.IP to uint32 representation.

func IPv6ToBigInt

func IPv6ToBigInt(ipaddr net.IP) *big.Int

IPv6ToBigInt converts IP address of version 6 from net.IP to math big integer representation.

func IPv6ToInt

func IPv6ToInt(ipaddr net.IP) ([2]uint64, error)

IPv6ToInt converts IP address of version 6 from net.IP to uint64 array representation. Return value contains high integer value on the first place and low integer value on second place.

func IntToIPv4

func IntToIPv4(ipaddr uint32) net.IP

IntToIPv4 converts IP address of version 4 from integer to net.IP representation.

func IntToIPv6

func IntToIPv6(high, low uint64) net.IP

IntToIPv6 converts IP address of version 6 from integer (high and low value) to net.IP representation.

func MergeStruct

func MergeStruct(record1 interface{}, record2 interface{}) error

MergeStruct help to fill all existing fields of record1 by record2

func ParseIP

func ParseIP(s string) (net.IP, int, error)

ParseIP implements extension of net.ParseIP. It returns additional information about IP address bytes length. In general, it works typically as standard net.ParseIP. So if IP is not valid, nil is returned.

Types

This section is empty.