MCPcopy Create free account
hub / github.com/cel-expr/cel-go / SourceInfoToProto

Function SourceInfoToProto

common/ast/conversion.go:529–570  ·  view source on GitHub ↗

SourceInfoToProto serializes an ast.SourceInfo value to a protobuf SourceInfo object.

(info *SourceInfo)

Source from the content-addressed store, hash-verified

527
528// SourceInfoToProto serializes an ast.SourceInfo value to a protobuf SourceInfo object.
529func SourceInfoToProto(info *SourceInfo) (*exprpb.SourceInfo, error) {
530 if info == nil {
531 return &exprpb.SourceInfo{}, nil
532 }
533 sourceInfo := &exprpb.SourceInfo{
534 SyntaxVersion: info.SyntaxVersion(),
535 Location: info.Description(),
536 LineOffsets: info.LineOffsets(),
537 Positions: make(map[int64]int32, len(info.OffsetRanges())),
538 MacroCalls: make(map[int64]*exprpb.Expr, len(info.MacroCalls())),
539 }
540 for id, offset := range info.OffsetRanges() {
541 sourceInfo.Positions[id] = offset.Start
542 }
543 for id, e := range info.MacroCalls() {
544 call, err := ExprToProto(e)
545 if err != nil {
546 return nil, err
547 }
548 sourceInfo.MacroCalls[id] = call
549 }
550 for _, ext := range info.Extensions() {
551 var components []exprpb.SourceInfo_Extension_Component
552 for _, c := range ext.Components {
553 comp, found := componentPBMap[c]
554 if found {
555 components = append(components, comp)
556 }
557 }
558 ver := &exprpb.SourceInfo_Extension_Version{
559 Major: ext.Version.Major,
560 Minor: ext.Version.Minor,
561 }
562 pbExt := &exprpb.SourceInfo_Extension{
563 Id: ext.ID,
564 Version: ver,
565 AffectedComponents: components,
566 }
567 sourceInfo.Extensions = append(sourceInfo.Extensions, pbExt)
568 }
569 return sourceInfo, nil
570}
571
572// ProtoToSourceInfo deserializes the protobuf into a native SourceInfo value.
573func ProtoToSourceInfo(info *exprpb.SourceInfo) (*SourceInfo, error) {

Callers 4

TestOptimizeWithSourceFunction · 0.92
TestSourceInfoToProtoFunction · 0.92
ToProtoFunction · 0.85

Calls 7

ExprToProtoFunction · 0.85
SyntaxVersionMethod · 0.80
OffsetRangesMethod · 0.80
ExtensionsMethod · 0.80
DescriptionMethod · 0.65
LineOffsetsMethod · 0.65
MacroCallsMethod · 0.45

Tested by 3

TestOptimizeWithSourceFunction · 0.74
TestSourceInfoToProtoFunction · 0.74