SourceInfoToProto serializes an ast.SourceInfo value to a protobuf SourceInfo object.
(info *SourceInfo)
| 527 | |
| 528 | // SourceInfoToProto serializes an ast.SourceInfo value to a protobuf SourceInfo object. |
| 529 | func 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. |
| 573 | func ProtoToSourceInfo(info *exprpb.SourceInfo) (*SourceInfo, error) { |