Files
ai_academy/internal/infrastructure/docparser/source_blocks.go
T
runny 7ec21f35b0 chore: 初始化仓库,导入项目基线代码
Academy v0.8.3 完整代码基线:Go 后端(internal/cmd/cli/mcp-server/docreader)、
Vue 前端(frontend)、数据库迁移(migrations)、部署编排(docker/helm/deploy)与文档。
今日开发的培训推送管理改动随后单独提交。
2026-11-16 18:12:23 +08:00

58 lines
1.5 KiB
Go

package docparser
import (
"encoding/json"
"academy/docreader/proto"
"academy/internal/types"
)
// wireSourceBlock is the transport shape of a source block: a markdown range
// plus the JSON-encoded locator, shared by the gRPC and HTTP docreaders.
type wireSourceBlock struct {
Start uint32 `json:"start"`
End uint32 `json:"end"`
LocatorJSON string `json:"locator_json"`
}
// decodeSourceBlock converts one transported block, dropping it when the
// range is empty or the locator cannot be decoded.
func decodeSourceBlock(b wireSourceBlock) (types.SourceBlock, bool) {
if b.End <= b.Start || b.LocatorJSON == "" {
return types.SourceBlock{}, false
}
var loc types.SourceLocator
if err := json.Unmarshal([]byte(b.LocatorJSON), &loc); err != nil || loc.Type == "" {
return types.SourceBlock{}, false
}
return types.SourceBlock{Start: int(b.Start), End: int(b.End), Locator: loc}, true
}
func sourceBlocksFromProto(pbs []*proto.SourceBlock) []types.SourceBlock {
if len(pbs) == 0 {
return nil
}
out := make([]types.SourceBlock, 0, len(pbs))
for _, pb := range pbs {
if b, ok := decodeSourceBlock(wireSourceBlock{
Start: pb.GetStart(), End: pb.GetEnd(), LocatorJSON: pb.GetLocatorJson(),
}); ok {
out = append(out, b)
}
}
return out
}
func sourceBlocksFromWire(wire []wireSourceBlock) []types.SourceBlock {
if len(wire) == 0 {
return nil
}
out := make([]types.SourceBlock, 0, len(wire))
for _, w := range wire {
if b, ok := decodeSourceBlock(w); ok {
out = append(out, b)
}
}
return out
}