MCPcopy Create free account
hub / github.com/dolthub/doltgresql / NormalizeVal

Function NormalizeVal

testing/go/framework.go:763–818  ·  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.

(dt *types.DoltgresType, v any)

Source from the content-addressed store, hash-verified

761// convert the values using the given Doltgres type. This is used to normalize array
762// types as the type conversion expects certain type values.
763func NormalizeVal(dt *types.DoltgresType, v any) any {
764 switch dt.ID {
765 case types.Json.ID:
766 str, err := json.Marshal(v)
767 if err != nil {
768 panic(err)
769 }
770 return string(str)
771 case types.JsonB.ID:
772 return gmstypes.JSONDocument{Val: v}
773 case types.Oid.ID, types.Regclass.ID, types.Regproc.ID, types.Regtype.ID:
774 if uval, ok := v.(uint32); ok {
775 if internalID := id.Cache().ToInternal(uval); internalID.IsValid() {
776 return internalID
777 }
778 return id.NewOID(uval).AsId()
779 }
780 }
781
782 switch val := v.(type) {
783 case pgtype.Numeric:
784 if val.NaN {
785 return math.NaN()
786 } else if val.InfinityModifier != pgtype.Finite {
787 return math.Inf(int(val.InfinityModifier))
788 } else if !val.Valid {
789 return nil
790 } else {
791 return apd.New(val.Int.Int64(), val.Exp)
792 }
793 case pgtype.Time:
794 // This value type is used for TIME type.
795 return timeofday.FromInt(val.Microseconds)
796 case pgtype.Interval:
797 // This value type is used for INTERVAL type.
798 return duration.MakeDuration(val.Microseconds*functions.NanosPerMicro, int64(val.Days), int64(val.Months))
799 case [16]byte:
800 // This value type is used for UUID type.
801 u, err := uuid.FromBytes(val[:])
802 if err != nil {
803 panic(err)
804 }
805 return u
806 case []any:
807 baseType := dt
808 if baseType.IsArrayType() {
809 baseType = baseType.ArrayBaseType()
810 }
811 newVal := make([]any, len(val))
812 for i, el := range val {
813 newVal[i] = NormalizeVal(baseType, el)
814 }
815 return newVal
816 }
817 return v
818}
819
820// NormalizeIntsAndFloats normalizes all int and float types

Callers 2

NormalizeValToStringFunction · 0.85
NormalizeArrayTypeFunction · 0.85

Calls 12

CacheFunction · 0.92
NewOIDFunction · 0.92
FromIntFunction · 0.92
MakeDurationFunction · 0.92
FromBytesFunction · 0.92
ToInternalMethod · 0.80
IsArrayTypeMethod · 0.80
ArrayBaseTypeMethod · 0.80
MarshalMethod · 0.45
IsValidMethod · 0.45
AsIdMethod · 0.45
Int64Method · 0.45

Tested by

no test coverage detected