-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.go
39 lines (34 loc) · 864 Bytes
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package patcher
import (
"errors"
"reflect"
)
// ptr returns a pointer to the value passed in.
func ptr[T any](v T) *T {
return &v
}
func isPointerToStruct[T any](t T) bool {
rv := reflect.ValueOf(t)
if rv.Kind() != reflect.Ptr || rv.IsNil() {
return false
}
return rv.Elem().Kind() == reflect.Struct
}
// IgnoreNoChangesErr ignores the ErrNoChanges error. This is useful when you want to ignore the error when no changes
// were made. Please ensure that you are still handling the errors as needed. We will return a "nil" patch when there
// are no changes as the ErrNoChanges error is returned.
//
// Example:
//
// err := report.Patch(db, newReport)
// if patcher.IgnoreNoChangesErr(err) != nil {
// return err
// }
func IgnoreNoChangesErr(err error) error {
switch {
case errors.Is(err, ErrNoChanges):
return nil
default:
return err
}
}