"Fossies" - the Fresh Open Source Software Archive

Member "vault-1.2.4/vendor/github.com/jackc/pgx/pgtype/aclitem_array.go" (7 Nov 2019, 4511 Bytes) of package /linux/misc/vault-1.2.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Go source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 package pgtype
    2 
    3 import (
    4     "database/sql/driver"
    5 
    6     "github.com/pkg/errors"
    7 )
    8 
    9 type ACLItemArray struct {
   10     Elements   []ACLItem
   11     Dimensions []ArrayDimension
   12     Status     Status
   13 }
   14 
   15 func (dst *ACLItemArray) Set(src interface{}) error {
   16     // untyped nil and typed nil interfaces are different
   17     if src == nil {
   18         *dst = ACLItemArray{Status: Null}
   19         return nil
   20     }
   21 
   22     switch value := src.(type) {
   23 
   24     case []string:
   25         if value == nil {
   26             *dst = ACLItemArray{Status: Null}
   27         } else if len(value) == 0 {
   28             *dst = ACLItemArray{Status: Present}
   29         } else {
   30             elements := make([]ACLItem, len(value))
   31             for i := range value {
   32                 if err := elements[i].Set(value[i]); err != nil {
   33                     return err
   34                 }
   35             }
   36             *dst = ACLItemArray{
   37                 Elements:   elements,
   38                 Dimensions: []ArrayDimension{{Length: int32(len(elements)), LowerBound: 1}},
   39                 Status:     Present,
   40             }
   41         }
   42 
   43     default:
   44         if originalSrc, ok := underlyingSliceType(src); ok {
   45             return dst.Set(originalSrc)
   46         }
   47         return errors.Errorf("cannot convert %v to ACLItemArray", value)
   48     }
   49 
   50     return nil
   51 }
   52 
   53 func (dst *ACLItemArray) Get() interface{} {
   54     switch dst.Status {
   55     case Present:
   56         return dst
   57     case Null:
   58         return nil
   59     default:
   60         return dst.Status
   61     }
   62 }
   63 
   64 func (src *ACLItemArray) AssignTo(dst interface{}) error {
   65     switch src.Status {
   66     case Present:
   67         switch v := dst.(type) {
   68 
   69         case *[]string:
   70             *v = make([]string, len(src.Elements))
   71             for i := range src.Elements {
   72                 if err := src.Elements[i].AssignTo(&((*v)[i])); err != nil {
   73                     return err
   74                 }
   75             }
   76             return nil
   77 
   78         default:
   79             if nextDst, retry := GetAssignToDstType(dst); retry {
   80                 return src.AssignTo(nextDst)
   81             }
   82         }
   83     case Null:
   84         return NullAssignTo(dst)
   85     }
   86 
   87     return errors.Errorf("cannot decode %#v into %T", src, dst)
   88 }
   89 
   90 func (dst *ACLItemArray) DecodeText(ci *ConnInfo, src []byte) error {
   91     if src == nil {
   92         *dst = ACLItemArray{Status: Null}
   93         return nil
   94     }
   95 
   96     uta, err := ParseUntypedTextArray(string(src))
   97     if err != nil {
   98         return err
   99     }
  100 
  101     var elements []ACLItem
  102 
  103     if len(uta.Elements) > 0 {
  104         elements = make([]ACLItem, len(uta.Elements))
  105 
  106         for i, s := range uta.Elements {
  107             var elem ACLItem
  108             var elemSrc []byte
  109             if s != "NULL" {
  110                 elemSrc = []byte(s)
  111             }
  112             err = elem.DecodeText(ci, elemSrc)
  113             if err != nil {
  114                 return err
  115             }
  116 
  117             elements[i] = elem
  118         }
  119     }
  120 
  121     *dst = ACLItemArray{Elements: elements, Dimensions: uta.Dimensions, Status: Present}
  122 
  123     return nil
  124 }
  125 
  126 func (src *ACLItemArray) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
  127     switch src.Status {
  128     case Null:
  129         return nil, nil
  130     case Undefined:
  131         return nil, errUndefined
  132     }
  133 
  134     if len(src.Dimensions) == 0 {
  135         return append(buf, '{', '}'), nil
  136     }
  137 
  138     buf = EncodeTextArrayDimensions(buf, src.Dimensions)
  139 
  140     // dimElemCounts is the multiples of elements that each array lies on. For
  141     // example, a single dimension array of length 4 would have a dimElemCounts of
  142     // [4]. A multi-dimensional array of lengths [3,5,2] would have a
  143     // dimElemCounts of [30,10,2]. This is used to simplify when to render a '{'
  144     // or '}'.
  145     dimElemCounts := make([]int, len(src.Dimensions))
  146     dimElemCounts[len(src.Dimensions)-1] = int(src.Dimensions[len(src.Dimensions)-1].Length)
  147     for i := len(src.Dimensions) - 2; i > -1; i-- {
  148         dimElemCounts[i] = int(src.Dimensions[i].Length) * dimElemCounts[i+1]
  149     }
  150 
  151     inElemBuf := make([]byte, 0, 32)
  152     for i, elem := range src.Elements {
  153         if i > 0 {
  154             buf = append(buf, ',')
  155         }
  156 
  157         for _, dec := range dimElemCounts {
  158             if i%dec == 0 {
  159                 buf = append(buf, '{')
  160             }
  161         }
  162 
  163         elemBuf, err := elem.EncodeText(ci, inElemBuf)
  164         if err != nil {
  165             return nil, err
  166         }
  167         if elemBuf == nil {
  168             buf = append(buf, `NULL`...)
  169         } else {
  170             buf = append(buf, QuoteArrayElementIfNeeded(string(elemBuf))...)
  171         }
  172 
  173         for _, dec := range dimElemCounts {
  174             if (i+1)%dec == 0 {
  175                 buf = append(buf, '}')
  176             }
  177         }
  178     }
  179 
  180     return buf, nil
  181 }
  182 
  183 // Scan implements the database/sql Scanner interface.
  184 func (dst *ACLItemArray) Scan(src interface{}) error {
  185     if src == nil {
  186         return dst.DecodeText(nil, nil)
  187     }
  188 
  189     switch src := src.(type) {
  190     case string:
  191         return dst.DecodeText(nil, []byte(src))
  192     case []byte:
  193         srcCopy := make([]byte, len(src))
  194         copy(srcCopy, src)
  195         return dst.DecodeText(nil, srcCopy)
  196     }
  197 
  198     return errors.Errorf("cannot scan %T", src)
  199 }
  200 
  201 // Value implements the database/sql/driver Valuer interface.
  202 func (src *ACLItemArray) Value() (driver.Value, error) {
  203     buf, err := src.EncodeText(nil, nil)
  204     if err != nil {
  205         return nil, err
  206     }
  207     if buf == nil {
  208         return nil, nil
  209     }
  210 
  211     return string(buf), nil
  212 }