-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmdget.go
109 lines (89 loc) · 2.87 KB
/
cmdget.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package redmos
import (
"encoding/json"
"errors"
"fmt"
"github.com/redis/go-redis/v9"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)
// Get 取值行為, 以索引字串到主要/次要資料庫中取得資料, 使用上有以下幾點須注意
// - 需要事先建立好資料結構, 並填寫到泛型類型T中, 請不要填入指標類型
// - 執行前設定好 MajorEnable, MinorEnable
// - 執行前設定好 Meta, 這需要事先建立好與 Metaer 介面符合的元資料結構
// - 執行前設定好 Key 並且不能為空字串
// - 執行前設定好 Data, 如果為nil, 則內部程序會自己建立
// - 執行後可用 Data 來取得資料
type Get[T any] struct {
Behave // 行為物件
MajorEnable bool // 啟用主要資料庫
MinorEnable bool // 啟用次要資料庫
Meta Metaer // 元資料
Key string // 索引值
Data *T // 資料物件
cmd *redis.StringCmd // 命令結果
}
// Prepare 前置處理
func (this *Get[T]) Prepare() error {
if this.Meta == nil {
return fmt.Errorf("get prepare: meta nil")
} // if
if this.Key == "" {
return fmt.Errorf("get prepare: key empty")
} // if
if this.MajorEnable {
key := this.Meta.MajorKey(this.Key)
this.cmd = this.Major().Get(this.Ctx(), key)
} // if
if this.MinorEnable {
if this.Meta.MinorTable() == "" {
return fmt.Errorf("get prepare: table empty")
} // if
if this.Meta.MinorField() == "" {
return fmt.Errorf("get prepare: field empty")
} // if
} // if
return nil
}
// Complete 完成處理
func (this *Get[T]) Complete() error {
if this.Meta == nil {
return fmt.Errorf("get complete: meta nil")
} // if
if this.MajorEnable {
data, err := this.cmd.Result()
if err != nil && err != redis.Nil {
return fmt.Errorf("get complete: %w: %v", err, this.Key)
} // if
if data != RedisNil {
if this.Data == nil {
this.Data = new(T)
} // if
if err = json.Unmarshal([]byte(data), this.Data); err != nil {
return fmt.Errorf("get complete: %w: %v", err, this.Key)
} // if
return nil // 如果主要資料庫讀取成功, 就不必執行次要資料庫了
} // if
} // if
if this.MinorEnable {
table := this.Meta.MinorTable()
field := this.Meta.MinorField()
key := this.Meta.MinorKey(this.Key)
filter := bson.D{{Key: field, Value: key}}
result := this.Minor().Collection(table).FindOne(this.Ctx(), filter)
err := result.Err()
empty := errors.Is(err, mongo.ErrNoDocuments)
if err != nil && empty == false {
return fmt.Errorf("get complete: %w: %v", err, this.Key)
} // if
if empty == false {
if this.Data == nil {
this.Data = new(T)
} // if
if err = result.Decode(this.Data); err != nil {
return fmt.Errorf("get complete: %w: %v", err, this.Key)
} // if
} // if
} // if
return nil
}