MCPcopy
hub / github.com/jackc/pgx / Encode

Method Encode

pgtype/multirange.go:87–133  ·  view source on GitHub ↗
(value any, buf []byte)

Source from the content-addressed store, hash-verified

85}
86
87func (p *encodePlanMultirangeCodecText) Encode(value any, buf []byte) (newBuf []byte, err error) {
88 multirange := value.(MultirangeGetter)
89
90 if multirange.IsNull() {
91 return nil, nil
92 }
93
94 elementCount := multirange.Len()
95
96 buf = append(buf, '{')
97
98 var encodePlan EncodePlan
99 var lastElemType reflect.Type
100 inElemBuf := make([]byte, 0, 32)
101 for i := range elementCount {
102 if i > 0 {
103 buf = append(buf, ',')
104 }
105
106 elem := multirange.Index(i)
107 var elemBuf []byte
108 if elem != nil {
109 elemType := reflect.TypeOf(elem)
110 if lastElemType != elemType {
111 lastElemType = elemType
112 encodePlan = p.m.PlanEncode(p.ac.ElementType.OID, TextFormatCode, elem)
113 if encodePlan == nil {
114 return nil, fmt.Errorf("unable to encode %v", multirange.Index(i))
115 }
116 }
117 elemBuf, err = encodePlan.Encode(elem, inElemBuf)
118 if err != nil {
119 return nil, err
120 }
121 }
122
123 if elemBuf == nil {
124 return nil, fmt.Errorf("multirange cannot contain NULL element")
125 } else {
126 buf = append(buf, elemBuf...)
127 }
128 }
129
130 buf = append(buf, '}')
131
132 return buf, nil
133}
134
135type encodePlanMultirangeCodecBinary struct {
136 ac *MultirangeCodec

Callers

nothing calls this directly

Calls 5

EncodeMethod · 0.95
IsNullMethod · 0.65
LenMethod · 0.65
IndexMethod · 0.65
PlanEncodeMethod · 0.65

Tested by

no test coverage detected