-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.go
55 lines (50 loc) · 1.47 KB
/
helpers.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package fireorm
import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"reflect"
)
// SetIDField tries to set the "ID" field if it exists and is of type string.
func SetIDField(model interface{}, id string) {
v := reflect.ValueOf(model)
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
field := v.FieldByName("ID")
if field.IsValid() && field.CanSet() && field.Kind() == reflect.String {
field.SetString(id)
}
}
// StructToMap converts a struct to a map (for Firestore), using the "firestore" tag for field names.
func StructToMap(model interface{}) (map[string]interface{}, error) {
data := make(map[string]interface{})
v := reflect.ValueOf(model)
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
t := v.Type()
for i := 0; i < t.NumField(); i++ {
fieldDef := t.Field(i)
firestoreTag := fieldDef.Tag.Get("firestore")
if firestoreTag == "" || firestoreTag == "-" {
continue
}
fieldVal := v.Field(i)
data[firestoreTag] = fieldVal.Interface()
}
return data, nil
}
// IsNotFoundError checks if the provided error corresponds to a 'NotFound' or 'Unknown' gRPC status code.
//
// Parameters:
// - err: The error to be checked, which is expected to be a gRPC error.
//
// Returns:
// - bool: Returns true if the error has a gRPC status code of 'NotFound' or 'Unknown', otherwise false.
func IsNotFoundError(err error) bool {
if err == nil {
return false
}
statusCode := status.Code(err)
return statusCode == codes.NotFound || statusCode == codes.Unknown
}