NormalizeValToString normalizes values into types that can be compared. JSON types, any pg types and time and decimal type values are converted into string value. |normalizeNumeric| defines whether to normalize Numeric values into either Numeric type or string type. There are an infinite number of w
(typ *pgtype.Type, v any)
| 174 | // There are an infinite number of ways to represent the same value in-memory, |
| 175 | // so we must at least normalize Numeric values. |
| 176 | func NormalizeValToString(typ *pgtype.Type, v any) any { |
| 177 | switch strings.ToLower(typ.Name) { |
| 178 | case "json": |
| 179 | str, err := json.Marshal(v) |
| 180 | if err != nil { |
| 181 | panic(err) |
| 182 | } |
| 183 | bytes, err := defaultMap.Encode(typ.OID, pgtype.TextFormatCode, string(str), nil) |
| 184 | if err != nil { |
| 185 | panic(err) |
| 186 | } |
| 187 | return string(bytes) |
| 188 | case "jsonb": |
| 189 | bytes, err := defaultMap.Encode(typ.OID, pgtype.TextFormatCode, v, nil) |
| 190 | if err != nil { |
| 191 | panic(err) |
| 192 | } |
| 193 | var s string |
| 194 | if err := defaultMap.Scan(typ.OID, pgtype.TextFormatCode, bytes, &s); err != nil { |
| 195 | panic(err) |
| 196 | } |
| 197 | return s |
| 198 | case "interval", "time", "timestamp", "date", "uuid": |
| 199 | // These values need to be normalized into the appropriate types |
| 200 | // before being converted to string type using the Doltgres |
| 201 | // IoOutput method. |
| 202 | if v == nil { |
| 203 | return nil |
| 204 | } |
| 205 | v = NormalizeVal(typ, v) |
| 206 | bytes, err := defaultMap.Encode(typ.OID, pgtype.TextFormatCode, v, nil) |
| 207 | if err != nil { |
| 208 | panic(err) |
| 209 | } |
| 210 | return string(bytes) |
| 211 | |
| 212 | case "timestamptz": |
| 213 | // timestamptz returns a value in server timezone |
| 214 | _, offset := v.(time.Time).Zone() |
| 215 | if offset%3600 != 0 { |
| 216 | return v.(time.Time).Format("2006-01-02 15:04:05.999999999-07:00") |
| 217 | } else { |
| 218 | return v.(time.Time).Format("2006-01-02 15:04:05.999999999-07") |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | switch val := v.(type) { |
| 223 | case pgtype.Numeric: |
| 224 | if val.NaN { |
| 225 | return math.NaN() |
| 226 | } else if val.InfinityModifier != pgtype.Finite { |
| 227 | return math.Inf(int(val.InfinityModifier)) |
| 228 | } else if !val.Valid { |
| 229 | return nil |
| 230 | } else { |
| 231 | decStr := decimalFromNumeric(val).String() |
| 232 | return Numeric(decStr) |
| 233 | } |
no test coverage detected