...

Source file src/github.com/jackc/pgtype/aclitem.go

Documentation: github.com/jackc/pgtype

     1  package pgtype
     2  
     3  import (
     4  	"database/sql/driver"
     5  	"fmt"
     6  )
     7  
     8  // ACLItem is used for PostgreSQL's aclitem data type. A sample aclitem
     9  // might look like this:
    10  //
    11  //	postgres=arwdDxt/postgres
    12  //
    13  // Note, however, that because the user/role name part of an aclitem is
    14  // an identifier, it follows all the usual formatting rules for SQL
    15  // identifiers: if it contains spaces and other special characters,
    16  // it should appear in double-quotes:
    17  //
    18  //	postgres=arwdDxt/"role with spaces"
    19  //
    20  type ACLItem struct {
    21  	String string
    22  	Status Status
    23  }
    24  
    25  func (dst *ACLItem) Set(src interface{}) error {
    26  	if src == nil {
    27  		*dst = ACLItem{Status: Null}
    28  		return nil
    29  	}
    30  
    31  	if value, ok := src.(interface{ Get() interface{} }); ok {
    32  		value2 := value.Get()
    33  		if value2 != value {
    34  			return dst.Set(value2)
    35  		}
    36  	}
    37  
    38  	switch value := src.(type) {
    39  	case string:
    40  		*dst = ACLItem{String: value, Status: Present}
    41  	case *string:
    42  		if value == nil {
    43  			*dst = ACLItem{Status: Null}
    44  		} else {
    45  			*dst = ACLItem{String: *value, Status: Present}
    46  		}
    47  	default:
    48  		if originalSrc, ok := underlyingStringType(src); ok {
    49  			return dst.Set(originalSrc)
    50  		}
    51  		return fmt.Errorf("cannot convert %v to ACLItem", value)
    52  	}
    53  
    54  	return nil
    55  }
    56  
    57  func (dst ACLItem) Get() interface{} {
    58  	switch dst.Status {
    59  	case Present:
    60  		return dst.String
    61  	case Null:
    62  		return nil
    63  	default:
    64  		return dst.Status
    65  	}
    66  }
    67  
    68  func (src *ACLItem) AssignTo(dst interface{}) error {
    69  	switch src.Status {
    70  	case Present:
    71  		switch v := dst.(type) {
    72  		case *string:
    73  			*v = src.String
    74  			return nil
    75  		default:
    76  			if nextDst, retry := GetAssignToDstType(dst); retry {
    77  				return src.AssignTo(nextDst)
    78  			}
    79  			return fmt.Errorf("unable to assign to %T", dst)
    80  		}
    81  	case Null:
    82  		return NullAssignTo(dst)
    83  	}
    84  
    85  	return fmt.Errorf("cannot decode %#v into %T", src, dst)
    86  }
    87  
    88  func (dst *ACLItem) DecodeText(ci *ConnInfo, src []byte) error {
    89  	if src == nil {
    90  		*dst = ACLItem{Status: Null}
    91  		return nil
    92  	}
    93  
    94  	*dst = ACLItem{String: string(src), Status: Present}
    95  	return nil
    96  }
    97  
    98  func (src ACLItem) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
    99  	switch src.Status {
   100  	case Null:
   101  		return nil, nil
   102  	case Undefined:
   103  		return nil, errUndefined
   104  	}
   105  
   106  	return append(buf, src.String...), nil
   107  }
   108  
   109  // Scan implements the database/sql Scanner interface.
   110  func (dst *ACLItem) Scan(src interface{}) error {
   111  	if src == nil {
   112  		*dst = ACLItem{Status: Null}
   113  		return nil
   114  	}
   115  
   116  	switch src := src.(type) {
   117  	case string:
   118  		return dst.DecodeText(nil, []byte(src))
   119  	case []byte:
   120  		srcCopy := make([]byte, len(src))
   121  		copy(srcCopy, src)
   122  		return dst.DecodeText(nil, srcCopy)
   123  	}
   124  
   125  	return fmt.Errorf("cannot scan %T", src)
   126  }
   127  
   128  // Value implements the database/sql/driver Valuer interface.
   129  func (src ACLItem) Value() (driver.Value, error) {
   130  	switch src.Status {
   131  	case Present:
   132  		return src.String, nil
   133  	case Null:
   134  		return nil, nil
   135  	default:
   136  		return nil, errUndefined
   137  	}
   138  }
   139  

View as plain text