-
Notifications
You must be signed in to change notification settings - Fork 43
/
batch.go
78 lines (74 loc) · 2.55 KB
/
batch.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
package entities
import (
"encoding/json"
"github.com/baetyl/baetyl-cloud/common"
"github.com/baetyl/baetyl-cloud/models"
"github.com/baetyl/baetyl-go/v2/log"
"time"
)
type Batch struct {
Name string `db:"name"`
Namespace string `db:"namespace"`
Description string `db:"description"`
QuotaNum int `db:"quota_num"`
EnableWhitelist int `db:"enable_whitelist"`
SecurityType common.Security `db:"security_type"`
SecurityKey string `db:"security_key"`
CallbackName string `db:"callback_name"`
CreateTime time.Time `db:"create_time"`
UpdateTime time.Time `db:"update_time"`
Labels string `db:"labels"`
Fingerprint string `db:"fingerprint"`
}
func ToBatchModel(batch *Batch) *models.Batch {
var labels map[string]string
if err := json.Unmarshal([]byte(batch.Labels), &labels); err != nil {
log.L().Error("batch db labels unmarshal error", log.Any("labels", batch.Labels))
}
var fp models.Fingerprint
if err := json.Unmarshal([]byte(batch.Fingerprint), &fp); err != nil {
log.L().Error("batch db fingerprint unmarshal error", log.Any("fingerprint", batch.Fingerprint))
}
res := &models.Batch{
Name: batch.Name,
Namespace: batch.Namespace,
Description: batch.Description,
QuotaNum: batch.QuotaNum,
EnableWhitelist: batch.EnableWhitelist,
SecurityType: batch.SecurityType,
SecurityKey: batch.SecurityKey,
CallbackName: batch.CallbackName,
CreateTime: batch.CreateTime,
UpdateTime: batch.UpdateTime,
Labels: labels,
Fingerprint: fp,
}
return res
}
func FromBatchModel(batch *models.Batch) *Batch {
labels, err := json.Marshal(batch.Labels)
if err != nil {
log.L().Error("batch labels marshal error", log.Any("labels", batch.Labels))
labels = []byte("{}")
}
fingerprint, err := json.Marshal(batch.Fingerprint)
if err != nil {
log.L().Error("batch fingerprint marshal error", log.Any("fingerprint", batch.Fingerprint))
fingerprint = []byte("{}")
}
res := &Batch{
Name: batch.Name,
Namespace: batch.Namespace,
Description: batch.Description,
QuotaNum: batch.QuotaNum,
EnableWhitelist: batch.EnableWhitelist,
SecurityType: batch.SecurityType,
SecurityKey: batch.SecurityKey,
CallbackName: batch.CallbackName,
CreateTime: batch.CreateTime,
UpdateTime: batch.UpdateTime,
Labels: string(labels),
Fingerprint: string(fingerprint),
}
return res
}