-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathoptime.go
54 lines (49 loc) · 1.18 KB
/
optime.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
// Copyright 2019-present Kuei-chun Chen. All rights reserved.
// optime.go
package ftdc
import (
"encoding/json"
"log"
"go.mongodb.org/mongo-driver/bson/primitive"
)
// OptimeDoc -
type OptimeDoc struct {
T int64 `json:"t" bson:"t"`
TS int64 `json:"ts" bson:"ts"`
}
// GetOptime -
func GetOptime(optime interface{}) int64 {
var ts int64
switch doc := optime.(type) {
case primitive.D:
for _, elem := range doc {
if elem.Key == "ts" {
b, _ := json.Marshal(elem.Value)
var optm OptimeDoc
json.Unmarshal(b, &optm)
ts = int64(optm.T)
break
}
}
case primitive.Timestamp:
b, _ := json.Marshal(doc)
var optm OptimeDoc
json.Unmarshal(b, &optm)
ts = int64(optm.T)
case map[string]interface{}:
d := primitive.D{{Key: optime.(map[string]interface{})["Key"].(string), Value: optime.(map[string]interface{})["Value"]}}
return GetOptime(d)
case []interface{}:
for _, intr := range optime.([]interface{}) {
if optm, ok := intr.(map[string]interface{}); !ok {
continue
} else if optm["Key"].(string) != "ts" {
continue
}
return GetOptime(intr)
}
default:
log.Printf("default => type: %T, value: %v\n", optime, optime)
}
return ts
}