-
Notifications
You must be signed in to change notification settings - Fork 0
/
select_all_ids.go
45 lines (43 loc) · 1.03 KB
/
select_all_ids.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
package dalgo2datastore
import (
"cloud.google.com/go/datastore"
"errors"
"fmt"
"reflect"
"strconv"
)
func idFromDatastoreKey(key *datastore.Key, idKind reflect.Kind) (id any, err error) {
if key.Incomplete() {
return nil, errors.New("datastore key is incomplete: neither key.Name nor key.ID is set")
}
switch idKind {
case reflect.Invalid:
return nil, errors.New("id kind is 0 e.g. 'reflect.Invalid'")
case reflect.String:
if key.Name == "" {
return strconv.FormatInt(key.ID, 10), nil
}
return key.Name, nil
default:
id := key.ID
if id == 0 {
if id, err = strconv.ParseInt(key.Name, 10, 64); err != nil {
return nil, fmt.Errorf("failed to autoconvert key.Name to int: %w", err)
}
}
switch idKind {
case reflect.Int64:
return id, nil
case reflect.Int:
return int(id), nil
case reflect.Int32:
return int(id), nil
case reflect.Int16:
return int(id), nil
case reflect.Int8:
return int(id), nil
default:
return key, fmt.Errorf("unsupported id type: %T=%v", idKind, idKind)
}
}
}