Skip to content

Migrate jackc/pgtype CIDR to its pgx/v5/pgtype alternative #1470

@antonprokopovich

Description

@antonprokopovich

I'm trying to migrate from pgx/v4 to pgx/v5 and in particular I need to rewrite code that uses CIDR type from jackc/pgtype to it's v5 alternative. I see v5/pgtype has the InetCodec type but I can't figure out how to use it instead of CIDR from v4.

Here the code I need to rewrite:

package main

import (
	"net"

	pgtypeV4 "github.com/jackc/pgtype" // TODO: Migrate to "github.com/jackc/pgx/v5/pgtype"
)

func toPgCIDRs(a []*net.IPNet) pgtypeV4.CIDRArray {
	elements := make([]pgtypeV4.CIDR, len(a))
	for i, e := range a {
		elements[i] = pgtypeV4.CIDR(pgtypeV4.Inet{IPNet: e, Status: pgtypeV4.Present})
	}

	return pgtypeV4.CIDRArray{
		Status:     pgtypeV4.Present,
		Elements:   elements,
		Dimensions: []pgtypeV4.ArrayDimension{{Length: int32(len(a)), LowerBound: 0}},
	}
}

Any kind of help would be greatly appreciated!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions