ExprToProto serializes an ast.Expr value to a protobuf Expr representation.
(e Expr)
| 280 | |
| 281 | // ExprToProto serializes an ast.Expr value to a protobuf Expr representation. |
| 282 | func ExprToProto(e Expr) (*exprpb.Expr, error) { |
| 283 | if e == nil { |
| 284 | return &exprpb.Expr{}, nil |
| 285 | } |
| 286 | switch e.Kind() { |
| 287 | case CallKind: |
| 288 | return protoCall(e.ID(), e.AsCall()) |
| 289 | case ComprehensionKind: |
| 290 | return protoComprehension(e.ID(), e.AsComprehension()) |
| 291 | case IdentKind: |
| 292 | return protoIdent(e.ID(), e.AsIdent()) |
| 293 | case ListKind: |
| 294 | return protoList(e.ID(), e.AsList()) |
| 295 | case LiteralKind: |
| 296 | return protoLiteral(e.ID(), e.AsLiteral()) |
| 297 | case MapKind: |
| 298 | return protoMap(e.ID(), e.AsMap()) |
| 299 | case SelectKind: |
| 300 | return protoSelect(e.ID(), e.AsSelect()) |
| 301 | case StructKind: |
| 302 | return protoStruct(e.ID(), e.AsStruct()) |
| 303 | case UnspecifiedExprKind: |
| 304 | // Handle the case where a macro reference may be getting translated. |
| 305 | // A nested macro 'pointer' is a non-zero expression id with no kind set. |
| 306 | if e.ID() != 0 { |
| 307 | return &exprpb.Expr{Id: e.ID()}, nil |
| 308 | } |
| 309 | return &exprpb.Expr{}, nil |
| 310 | } |
| 311 | return nil, fmt.Errorf("unsupported expr kind: %v", e) |
| 312 | } |
| 313 | |
| 314 | // EntryExprToProto converts an ast.EntryExpr to a protobuf CreateStruct entry |
| 315 | func EntryExprToProto(e EntryExpr) (*exprpb.Expr_CreateStruct_Entry, error) { |