| 19 | } |
| 20 | |
| 21 | func diffValues(left, right any, table Table) audit.Map { |
| 22 | var ( |
| 23 | baseDiff = audit.Map{} |
| 24 | rightT = reflect.TypeOf(right) |
| 25 | leftV = reflect.ValueOf(left) |
| 26 | rightV = reflect.ValueOf(right) |
| 27 | |
| 28 | diffKey = table[structName(rightT)] |
| 29 | ) |
| 30 | |
| 31 | if diffKey == nil { |
| 32 | panic(fmt.Sprintf("dev error: type %q (type %T) attempted audit but not auditable", rightT.Name(), right)) |
| 33 | } |
| 34 | |
| 35 | // allFields contains all top level fields of the struct. |
| 36 | allFields, err := flattenStructFields(leftV, rightV) |
| 37 | if err != nil { |
| 38 | // This should never happen. Only structs should be flattened. If an |
| 39 | // error occurs, an unsupported or non-struct type was passed in. |
| 40 | panic(fmt.Sprintf("dev error: failed to flatten struct fields: %v", err)) |
| 41 | } |
| 42 | |
| 43 | for _, field := range allFields { |
| 44 | var ( |
| 45 | leftF = field.LeftF |
| 46 | rightF = field.RightF |
| 47 | |
| 48 | leftI = leftF.Interface() |
| 49 | rightI = rightF.Interface() |
| 50 | ) |
| 51 | |
| 52 | diffName := field.FieldType.Tag.Get("json") |
| 53 | diffName = strings.TrimSuffix(diffName, ",omitempty") |
| 54 | |
| 55 | atype, ok := diffKey[diffName] |
| 56 | if !ok { |
| 57 | panic(fmt.Sprintf("dev error: field %q lacks audit information", diffName)) |
| 58 | } |
| 59 | |
| 60 | if atype == ActionIgnore { |
| 61 | continue |
| 62 | } |
| 63 | |
| 64 | // coerce struct types that would produce bad diffs. |
| 65 | if leftI, rightI, ok = convertDiffType(leftI, rightI); ok { |
| 66 | leftF, rightF = reflect.ValueOf(leftI), reflect.ValueOf(rightI) |
| 67 | } |
| 68 | |
| 69 | // If the field is a pointer, dereference it. Nil pointers are coerced |
| 70 | // to the zero value of their underlying type. |
| 71 | if leftF.Kind() == reflect.Ptr && rightF.Kind() == reflect.Ptr { |
| 72 | leftF, rightF = derefPointer(leftF), derefPointer(rightF) |
| 73 | leftI, rightI = leftF.Interface(), rightF.Interface() |
| 74 | } |
| 75 | |
| 76 | if !reflect.DeepEqual(leftI, rightI) { |
| 77 | switch atype { |
| 78 | case ActionTrack: |