Source File
cidr_array.go
Belonging Package
github.com/jackc/pgtype
package pgtype
import (
)
type CIDRArray struct {
Elements []CIDR
Dimensions []ArrayDimension
Status Status
}
switch value := .(type) {
case []*net.IPNet:
if == nil {
* = CIDRArray{Status: Null}
} else if len() == 0 {
* = CIDRArray{Status: Present}
} else {
:= make([]CIDR, len())
for := range {
if := [].Set([]); != nil {
return
}
}
* = CIDRArray{
Elements: ,
Dimensions: []ArrayDimension{{Length: int32(len()), LowerBound: 1}},
Status: Present,
}
}
case []net.IP:
if == nil {
* = CIDRArray{Status: Null}
} else if len() == 0 {
* = CIDRArray{Status: Present}
} else {
:= make([]CIDR, len())
for := range {
if := [].Set([]); != nil {
return
}
}
* = CIDRArray{
Elements: ,
Dimensions: []ArrayDimension{{Length: int32(len()), LowerBound: 1}},
Status: Present,
}
}
case []*net.IP:
if == nil {
* = CIDRArray{Status: Null}
} else if len() == 0 {
* = CIDRArray{Status: Present}
} else {
:= make([]CIDR, len())
for := range {
if := [].Set([]); != nil {
return
}
}
* = CIDRArray{
Elements: ,
Dimensions: []ArrayDimension{{Length: int32(len()), LowerBound: 1}},
Status: Present,
}
}
case []CIDR:
if == nil {
* = CIDRArray{Status: Null}
} else if len() == 0 {
* = CIDRArray{Status: Present}
} else {
* = CIDRArray{
Elements: ,
Dimensions: []ArrayDimension{{Length: int32(len()), LowerBound: 1}},
Status: Present,
}
}
:= reflect.ValueOf()
if !.IsValid() || .IsZero() {
* = CIDRArray{Status: Null}
return nil
}
, , := findDimensionsFromValue(, nil, 0)
if ! {
return fmt.Errorf("cannot find dimensions of %v for CIDRArray", )
}
if == 0 {
* = CIDRArray{Status: Present}
return nil
}
if len() == 0 {
if , := underlyingSliceType(); {
return .()
}
return fmt.Errorf("cannot convert %v to CIDRArray", )
}
* = CIDRArray{
Elements: make([]CIDR, ),
Dimensions: ,
Status: Present,
}
, := .setRecursive(, 0, 0)
if len(.Dimensions) > 1 {
.Dimensions = .Dimensions[:len(.Dimensions)-1]
= 0
for , := range .Dimensions {
if == 0 {
= int(.Length)
} else {
*= int(.Length)
}
}
.Elements = make([]CIDR, )
, = .setRecursive(, 0, 0)
if != nil {
return
}
} else {
return
}
}
if != len(.Elements) {
return fmt.Errorf("cannot convert %v to CIDRArray, expected %d dst.Elements, but got %d instead", , len(.Elements), )
}
}
return nil
}
func ( *CIDRArray) ( reflect.Value, , int) (int, error) {
switch .Kind() {
case reflect.Array:
fallthrough
case reflect.Slice:
if len(.Dimensions) == {
break
}
:= .Len()
if int32() != .Dimensions[].Length {
return 0, fmt.Errorf("multidimensional arrays must have array expressions with matching dimensions")
}
for := 0; < ; ++ {
var error
, = .(.Index(), , +1)
if != nil {
return 0,
}
}
return , nil
}
if !.CanInterface() {
return 0, fmt.Errorf("cannot convert all values to CIDRArray")
}
if := .Elements[].Set(.Interface()); != nil {
return 0, fmt.Errorf("%v in CIDRArray", )
}
++
return , nil
}
func ( CIDRArray) () interface{} {
switch .Status {
case Present:
return
case Null:
return nil
default:
return .Status
}
}
func ( *CIDRArray) ( interface{}) error {
switch .Status {
case Present:
switch v := .(type) {
case *[]*net.IPNet:
* = make([]*net.IPNet, len(.Elements))
for := range .Elements {
if := .Elements[].AssignTo(&((*)[])); != nil {
return
}
}
return nil
case *[]net.IP:
* = make([]net.IP, len(.Elements))
for := range .Elements {
if := .Elements[].AssignTo(&((*)[])); != nil {
return
}
}
return nil
case *[]*net.IP:
* = make([]*net.IP, len(.Elements))
for := range .Elements {
if := .Elements[].AssignTo(&((*)[])); != nil {
return
}
}
return nil
}
}
if , := GetAssignToDstType(); {
return .()
}
:= reflect.ValueOf()
if .Kind() == reflect.Ptr {
= .Elem()
}
switch .Kind() {
case reflect.Array, reflect.Slice:
default:
return fmt.Errorf("cannot assign %T to %T", , )
}
if len(.Elements) == 0 {
if .Kind() == reflect.Slice {
.Set(reflect.MakeSlice(.Type(), 0, 0))
return nil
}
}
, := .assignToRecursive(, 0, 0)
if != nil {
return
}
if != len(.Elements) {
return fmt.Errorf("cannot assign %v, needed to assign %d elements, but only assigned %d", , len(.Elements), )
}
return nil
case Null:
return NullAssignTo()
}
return fmt.Errorf("cannot decode %#v into %T", , )
}
func ( *CIDRArray) ( reflect.Value, , int) (int, error) {
switch := .Kind(); {
case reflect.Array:
fallthrough
case reflect.Slice:
if len(.Dimensions) == {
break
}
:= int(.Dimensions[].Length)
if reflect.Array == {
:= .Type()
if .Len() != {
return 0, fmt.Errorf("expected size %d array, but %s has size %d array", , , .Len())
}
.Set(reflect.New().Elem())
} else {
.Set(reflect.MakeSlice(.Type(), , ))
}
var error
for := 0; < ; ++ {
, = .(.Index(), , +1)
if != nil {
return 0,
}
}
return , nil
}
if len(.Dimensions) != {
return 0, fmt.Errorf("incorrect dimensions, expected %d, found %d", len(.Dimensions), )
}
if !.CanAddr() {
return 0, fmt.Errorf("cannot assign all values from CIDRArray")
}
:= .Addr()
if !.CanInterface() {
return 0, fmt.Errorf("cannot assign all values from CIDRArray")
}
if := .Elements[].AssignTo(.Interface()); != nil {
return 0,
}
++
return , nil
}
func ( *CIDRArray) ( *ConnInfo, []byte) error {
if == nil {
* = CIDRArray{Status: Null}
return nil
}
, := ParseUntypedTextArray(string())
if != nil {
return
}
var []CIDR
if len(.Elements) > 0 {
= make([]CIDR, len(.Elements))
for , := range .Elements {
var CIDR
var []byte
if != "NULL" || .Quoted[] {
= []byte()
}
= .DecodeText(, )
if != nil {
return
}
[] =
}
}
* = CIDRArray{Elements: , Dimensions: .Dimensions, Status: Present}
return nil
}
func ( *CIDRArray) ( *ConnInfo, []byte) error {
if == nil {
* = CIDRArray{Status: Null}
return nil
}
var ArrayHeader
, := .DecodeBinary(, )
if != nil {
return
}
if len(.Dimensions) == 0 {
* = CIDRArray{Dimensions: .Dimensions, Status: Present}
return nil
}
:= .Dimensions[0].Length
for , := range .Dimensions[1:] {
*= .Length
}
:= make([]CIDR, )
for := range {
:= int(int32(binary.BigEndian.Uint32([:])))
+= 4
var []byte
if >= 0 {
= [ : +]
+=
}
= [].DecodeBinary(, )
if != nil {
return
}
}
* = CIDRArray{Elements: , Dimensions: .Dimensions, Status: Present}
return nil
}
func ( CIDRArray) ( *ConnInfo, []byte) ([]byte, error) {
switch .Status {
case Null:
return nil, nil
case Undefined:
return nil, errUndefined
}
if len(.Dimensions) == 0 {
return append(, '{', '}'), nil
}
= EncodeTextArrayDimensions(, .Dimensions)
:= make([]int, len(.Dimensions))
[len(.Dimensions)-1] = int(.Dimensions[len(.Dimensions)-1].Length)
for := len(.Dimensions) - 2; > -1; -- {
[] = int(.Dimensions[].Length) * [+1]
}
:= make([]byte, 0, 32)
for , := range .Elements {
if > 0 {
= append(, ',')
}
for , := range {
if % == 0 {
= append(, '{')
}
}
, := .EncodeText(, )
if != nil {
return nil,
}
if == nil {
= append(, `NULL`...)
} else {
= append(, QuoteArrayElementIfNeeded(string())...)
}
for , := range {
if (+1)% == 0 {
= append(, '}')
}
}
}
return , nil
}
func ( CIDRArray) ( *ConnInfo, []byte) ([]byte, error) {
switch .Status {
case Null:
return nil, nil
case Undefined:
return nil, errUndefined
}
:= ArrayHeader{
Dimensions: .Dimensions,
}
if , := .DataTypeForName("cidr"); {
.ElementOID = int32(.OID)
} else {
return nil, fmt.Errorf("unable to find oid for type name %v", "cidr")
}
for := range .Elements {
if .Elements[].Status == Null {
.ContainsNull = true
break
}
}
= .EncodeBinary(, )
for := range .Elements {
:= len()
= pgio.AppendInt32(, -1)
, := .Elements[].EncodeBinary(, )
if != nil {
return nil,
}
if != nil {
=
pgio.SetInt32([:], int32(len([:])-4))
}
}
return , nil
}
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |