package visamasteractions import ( "encoding/json" "reflect" ) // Normalize round-trips values through JSON, so Go values (pointers, raw // JSON bytes, float64s) compare equal to the same values read back from a // jsonb snapshot of the row. func Normalize(values map[string]interface{}) (map[string]interface{}, error) { if values == nil { return nil, nil } data, err := json.Marshal(values) if err != nil { return nil, err } var normalized map[string]interface{} if err := json.Unmarshal(data, &normalized); err != nil { return nil, err } return normalized, nil } // Diff keeps only the columns whose value differs between before and // after: original gets their before values and changes their after // values. A column missing on one side counts as null there. Both are nil // when nothing changed. func Diff(before, after map[string]interface{}) (original, changes map[string]interface{}) { for key, value := range after { if old, ok := before[key]; ok && reflect.DeepEqual(old, value) { continue } if original == nil { original, changes = map[string]interface{}{}, map[string]interface{}{} } original[key] = before[key] changes[key] = value } for key, old := range before { if _, ok := after[key]; ok || old == nil { continue } if original == nil { original, changes = map[string]interface{}{}, map[string]interface{}{} } original[key] = old changes[key] = nil } return original, changes }