-
Notifications
You must be signed in to change notification settings - Fork 0
/
aliases.go
64 lines (57 loc) · 1.09 KB
/
aliases.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
package osv_schema
import (
"database/sql"
"database/sql/driver"
"encoding/json"
"strings"
)
// Aliases 一般可能会放漏洞编号啥的
type Aliases []string
var _ sql.Scanner = &Aliases{}
var _ driver.Valuer = &Aliases{}
// GetCVE 获取别名中的CVE编号
func (x Aliases) GetCVE() string {
for _, s := range x {
s = strings.ToUpper(s)
if strings.HasPrefix(s, "CVE-") {
return s
}
}
return ""
}
// Filter 过滤出需要的编号
func (x Aliases) Filter(filterFunc func(alias string) bool) Aliases {
slice := make([]string, 0)
for _, alias := range x {
if filterFunc(alias) {
slice = append(slice, alias)
}
}
return slice
}
func (x *Aliases) Scan(src any) error {
if src == nil {
return nil
}
bytes, ok := src.([]byte)
if !ok {
return wrapScanError(src, x)
}
if len(bytes) == 0 {
return nil
}
return json.Unmarshal(bytes, &x)
}
func (x Aliases) Value() (driver.Value, error) {
if len(x) == 0 {
return nil, nil
}
marshal, err := json.Marshal(x)
if err != nil {
return nil, err
}
if len(marshal) == 0 {
return nil, nil
}
return string(marshal), nil
}