MCPcopy Index your code
hub / github.com/jackc/pgx / Encode

Method Encode

pgtype/array_codec.go:92–169  ·  view source on GitHub ↗
(value any, buf []byte)

Source from the content-addressed store, hash-verified

90}
91
92func (p *encodePlanArrayCodecText) Encode(value any, buf []byte) (newBuf []byte, err error) {
93 array := value.(ArrayGetter)
94
95 dimensions := array.Dimensions()
96 if dimensions == nil {
97 return nil, nil
98 }
99
100 elementCount := cardinality(dimensions)
101 if elementCount == 0 {
102 return append(buf, '{', '}'), nil
103 }
104
105 buf = encodeTextArrayDimensions(buf, dimensions)
106
107 // dimElemCounts is the multiples of elements that each array lies on. For
108 // example, a single dimension array of length 4 would have a dimElemCounts of
109 // [4]. A multi-dimensional array of lengths [3,5,2] would have a
110 // dimElemCounts of [30,10,2]. This is used to simplify when to render a '{'
111 // or '}'.
112 dimElemCounts := make([]int, len(dimensions))
113 dimElemCounts[len(dimensions)-1] = int(dimensions[len(dimensions)-1].Length)
114 for i := len(dimensions) - 2; i > -1; i-- {
115 dimElemCounts[i] = int(dimensions[i].Length) * dimElemCounts[i+1]
116 }
117
118 var encodePlan EncodePlan
119 var lastElemType reflect.Type
120 inElemBuf := make([]byte, 0, 32)
121 for i := range elementCount {
122 if i > 0 {
123 buf = append(buf, ',')
124 }
125
126 for _, dec := range dimElemCounts {
127 if i%dec == 0 {
128 buf = append(buf, '{')
129 }
130 }
131
132 elem := array.Index(i)
133 var elemBuf []byte
134 isNil, callNilDriverValuer := isNilDriverValuer(elem)
135 if !isNil {
136 elemType := reflect.TypeOf(elem)
137 if lastElemType != elemType {
138 lastElemType = elemType
139 encodePlan = p.m.PlanEncode(p.ac.ElementType.OID, TextFormatCode, elem)
140 if encodePlan == nil {
141 return nil, fmt.Errorf("unable to encode %v", array.Index(i))
142 }
143 }
144 elemBuf, err = encodePlan.Encode(elem, inElemBuf)
145 if err != nil {
146 return nil, err
147 }
148 } else if callNilDriverValuer {
149 elemBuf, err = (&encodePlanDriverValuer{m: p.m, oid: p.ac.ElementType.OID, formatCode: TextFormatCode}).Encode(elem, inElemBuf)

Callers

nothing calls this directly

Calls 8

EncodeMethod · 0.95
cardinalityFunction · 0.85
isNilDriverValuerFunction · 0.85
DimensionsMethod · 0.65
IndexMethod · 0.65
PlanEncodeMethod · 0.65

Tested by

no test coverage detected