📥 BasicFetcher — 基础获取器
BasicFetcher 是最简单的 DataFetcher 实现:内嵌一个 *APIClient,单条获取弱点、类别、视图,或一次性拿弱点 + 直接父子关系。它实现了 DataFetcher 接口的 Fetch 方法。
源文件:data_fetcher.go。
🧱 结构体定义
go
type BasicFetcher struct {
client *APIClient
}🏗️ 构造函数
go
func NewBasicFetcher(client *APIClient) *BasicFetcher| 参数 | 类型 | 说明 |
|---|---|---|
client | *APIClient | API 客户端;传 nil 会自动 NewAPIClient() |
返回 *BasicFetcher。
📤 方法
| 方法 | 签名 | 委托 | 文档 |
|---|---|---|---|
Fetch | (ctx, id int) (*CWE, error) | client.GetWeakness | GetWeakness |
FetchCategory | (ctx, id int) (*Category, error) | client.GetCategory | GetCategory |
FetchView | (ctx, id int) (*View, error) | client.GetView | GetView |
FetchWithRelations | (ctx, id int, viewID ...int) (*CWE, error) | 组合 GetWeakness+GetParents+GetChildren | 见下 |
FetchWithRelations
go
func (f *BasicFetcher) FetchWithRelations(ctx context.Context, id int, viewID ...int) (*CWE, error)| 参数 | 类型 | 说明 |
|---|---|---|
id | int | 目标 CWE ID |
viewID | ...int | 可选视图过滤,透传给 GetParents/GetChildren |
流程:
GetWeakness(ctx, id)拿到本体。GetParents(ctx, id, viewID...)拿父级,每条Nature改写为RelationshipChildOf,追加到cwe.Relationships。GetChildren(ctx, id, viewID...)拿子级,每条Nature改写为RelationshipParentOf,追加到cwe.Relationships。- 父/子获取出错时静默跳过(
err == nil才追加),本体仍返回。
一步拿到邻居
FetchWithRelations 等价于「本体 + 一层父 + 一层子」三次 API 调用的组合,是浏览单条弱点时最常用的便捷方法。需要多层血缘用 TreeFetcher。
关系 Nature 被重写
GetParents/GetChildren 返回的 Nature 是服务端原始值;FetchWithRelations 会按方向覆盖为 RelationshipChildOf/RelationshipParentOf,便于在本体上统一表达「我 -> 父」「我 -> 子」。若需要原始 Nature,请直接调 GetParents。
🚀 可运行示例
go
package main
import (
"context"
"fmt"
"log"
cwe "github.com/scagogogo/cwe-skills"
)
func main() {
client := cwe.NewAPIClient()
defer client.Close()
fetcher := cwe.NewBasicFetcher(client)
w, err := fetcher.FetchWithRelations(context.Background(), 79, 1000)
if err != nil {
log.Fatal(err)
}
fmt.Printf("CWE-79: %s\n", w.Name)
fmt.Printf("关系数: %d\n", len(w.Relationships))
for _, rel := range w.Relationships {
fmt.Printf(" %s CWE-%d\n", rel.Nature, rel.CWEID)
}
}