MCPcopy Create free account
hub / github.com/apecloud/myduckserver / NormalizeVal

Function NormalizeVal

pgtest/framework.go:260–319  ·  view source on GitHub ↗

NormalizeVal normalizes values to the Doltgres type expects, so it can be used to convert the values using the given Doltgres type. This is used to normalize array types as the type conversion expects certain type values.

(typ *pgtype.Type, v any)

Source from the content-addressed store, hash-verified

258// convert the values using the given Doltgres type. This is used to normalize array
259// types as the type conversion expects certain type values.
260func NormalizeVal(typ *pgtype.Type, v any) any {
261 switch strings.ToLower(typ.Name) {
262 case "json":
263 str, err := json.Marshal(v)
264 if err != nil {
265 panic(err)
266 }
267 return string(str)
268 case "jsonb":
269 bytes, err := defaultMap.Encode(typ.OID, pgtype.TextFormatCode, v, nil)
270 if err != nil {
271 panic(err)
272 }
273 var s string
274 if err := defaultMap.Scan(typ.OID, pgtype.TextFormatCode, bytes, &s); err != nil {
275 panic(err)
276 }
277 return s
278 }
279
280 switch val := v.(type) {
281 case pgtype.Numeric:
282 if val.NaN {
283 return math.NaN()
284 } else if val.InfinityModifier != pgtype.Finite {
285 return math.Inf(int(val.InfinityModifier))
286 } else if !val.Valid {
287 return nil
288 } else {
289 d := decimalFromNumeric(val)
290 return d
291 }
292 case pgtype.Time:
293 // This value type is used for TIME type.
294 var zero time.Time
295 return zero.Add(time.Duration(val.Microseconds) * time.Microsecond)
296 case pgtype.Interval:
297 // This value type is used for INTERVAL type.
298 // TODO(fan): Months
299 var zero time.Time
300 return zero.Add(time.Duration(val.Microseconds)*time.Microsecond).AddDate(0, 0, int(val.Days))
301 case [16]byte:
302 // This value type is used for UUID type.
303 u, err := uuid.FromBytes(val[:])
304 if err != nil {
305 panic(err)
306 }
307 return u
308 case []any:
309 baseType := typ.Codec.(*pgtype.ArrayCodec).ElementType
310 newVal := make([]any, len(val))
311 for i, el := range val {
312 newVal[i] = NormalizeVal(baseType, el)
313 }
314 return newVal
315 }
316 return v
317}

Callers 2

NormalizeValToStringFunction · 0.85
NormalizeArrayTypeFunction · 0.85

Calls 2

EncodeMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected