@@ -355,6 +355,58 @@ absl::StatusOr<Reference> ConvertProtoReferenceToNative(
355355 return ret_val;
356356}
357357
358+ absl::StatusOr<cel::expr::SourceInfo> ConvertSourceInfoToProto (
359+ const ast_internal::SourceInfo& source_info) {
360+ cel::expr::SourceInfo result;
361+ result.set_syntax_version (source_info.syntax_version ());
362+ result.set_location (source_info.location ());
363+
364+ for (int32_t line_offset : source_info.line_offsets ()) {
365+ result.add_line_offsets (line_offset);
366+ }
367+
368+ for (auto pos_iter = source_info.positions ().begin ();
369+ pos_iter != source_info.positions ().end (); ++pos_iter) {
370+ (*result.mutable_positions ())[pos_iter->first ] = pos_iter->second ;
371+ }
372+
373+ for (auto macro_iter = source_info.macro_calls ().begin ();
374+ macro_iter != source_info.macro_calls ().end (); ++macro_iter) {
375+ ExprPb& dest_macro = (*result.mutable_macro_calls ())[macro_iter->first ];
376+ CEL_RETURN_IF_ERROR (
377+ protobuf_internal::ExprToProto (macro_iter->second , &dest_macro));
378+ }
379+
380+ for (const auto & extension : source_info.extensions ()) {
381+ auto * extension_pb = result.add_extensions ();
382+ extension_pb->set_id (extension.id ());
383+ auto * version_pb = extension_pb->mutable_version ();
384+ version_pb->set_major (extension.version ().major ());
385+ version_pb->set_minor (extension.version ().minor ());
386+
387+ for (auto component : extension.affected_components ()) {
388+ switch (component) {
389+ case Extension::Component::kParser :
390+ extension_pb->add_affected_components (ExtensionPb::COMPONENT_PARSER);
391+ break ;
392+ case Extension::Component::kTypeChecker :
393+ extension_pb->add_affected_components (
394+ ExtensionPb::COMPONENT_TYPE_CHECKER);
395+ break ;
396+ case Extension::Component::kRuntime :
397+ extension_pb->add_affected_components (ExtensionPb::COMPONENT_RUNTIME);
398+ break ;
399+ default :
400+ extension_pb->add_affected_components (
401+ ExtensionPb::COMPONENT_UNSPECIFIED);
402+ break ;
403+ }
404+ }
405+ }
406+
407+ return result;
408+ }
409+
358410} // namespace internal
359411
360412namespace {
@@ -370,7 +422,6 @@ using ::cel::ast_internal::CreateStruct;
370422using ::cel::ast_internal::DynamicType;
371423using ::cel::ast_internal::ErrorType;
372424using ::cel::ast_internal::Expr;
373- using ::cel::ast_internal::Extension;
374425using ::cel::ast_internal::FunctionType;
375426using ::cel::ast_internal::Ident;
376427using ::cel::ast_internal::ListType;
@@ -386,6 +437,7 @@ using ::cel::ast_internal::SourceInfo;
386437using ::cel::ast_internal::Type;
387438using ::cel::ast_internal::UnspecifiedType;
388439using ::cel::ast_internal::WellKnownType;
440+ using ::cel::extensions::protobuf_internal::ExprToProto;
389441
390442using ExprPb = cel::expr::Expr;
391443using ParsedExprPb = cel::expr::ParsedExpr;
@@ -446,62 +498,6 @@ absl::Status ConstantToProto(const ast_internal::Constant& source,
446498 source.constant_kind ());
447499}
448500
449- absl::StatusOr<ExprPb> ExprToProto (const Expr& expr) {
450- ExprPb proto_expr;
451- CEL_RETURN_IF_ERROR (protobuf_internal::ExprToProto (expr, &proto_expr));
452- return proto_expr;
453- }
454-
455- absl::StatusOr<SourceInfoPb> SourceInfoToProto (const SourceInfo& source_info) {
456- SourceInfoPb result;
457- result.set_syntax_version (source_info.syntax_version ());
458- result.set_location (source_info.location ());
459-
460- for (int32_t line_offset : source_info.line_offsets ()) {
461- result.add_line_offsets (line_offset);
462- }
463-
464- for (auto pos_iter = source_info.positions ().begin ();
465- pos_iter != source_info.positions ().end (); ++pos_iter) {
466- (*result.mutable_positions ())[pos_iter->first ] = pos_iter->second ;
467- }
468-
469- for (auto macro_iter = source_info.macro_calls ().begin ();
470- macro_iter != source_info.macro_calls ().end (); ++macro_iter) {
471- ExprPb& dest_macro = (*result.mutable_macro_calls ())[macro_iter->first ];
472- CEL_ASSIGN_OR_RETURN (dest_macro, ExprToProto (macro_iter->second ));
473- }
474-
475- for (const auto & extension : source_info.extensions ()) {
476- auto * extension_pb = result.add_extensions ();
477- extension_pb->set_id (extension.id ());
478- auto * version_pb = extension_pb->mutable_version ();
479- version_pb->set_major (extension.version ().major ());
480- version_pb->set_minor (extension.version ().minor ());
481-
482- for (auto component : extension.affected_components ()) {
483- switch (component) {
484- case Extension::Component::kParser :
485- extension_pb->add_affected_components (ExtensionPb::COMPONENT_PARSER);
486- break ;
487- case Extension::Component::kTypeChecker :
488- extension_pb->add_affected_components (
489- ExtensionPb::COMPONENT_TYPE_CHECKER);
490- break ;
491- case Extension::Component::kRuntime :
492- extension_pb->add_affected_components (ExtensionPb::COMPONENT_RUNTIME);
493- break ;
494- default :
495- extension_pb->add_affected_components (
496- ExtensionPb::COMPONENT_UNSPECIFIED);
497- break ;
498- }
499- }
500- }
501-
502- return result;
503- }
504-
505501absl::StatusOr<ReferencePb> ReferenceToProto (const Reference& reference) {
506502 ReferencePb result;
507503
@@ -681,10 +677,11 @@ absl::StatusOr<std::unique_ptr<Ast>> CreateAstFromParsedExpr(
681677absl::StatusOr<ParsedExprPb> CreateParsedExprFromAst (const Ast& ast) {
682678 const auto & ast_impl = ast_internal::AstImpl::CastFromPublicAst (ast);
683679 ParsedExprPb parsed_expr;
684- CEL_ASSIGN_OR_RETURN (*parsed_expr.mutable_expr (),
685- ExprToProto (ast_impl.root_expr ()));
686- CEL_ASSIGN_OR_RETURN (*parsed_expr.mutable_source_info (),
687- SourceInfoToProto (ast_impl.source_info ()));
680+ CEL_RETURN_IF_ERROR (
681+ ExprToProto (ast_impl.root_expr (), parsed_expr.mutable_expr ()));
682+ CEL_ASSIGN_OR_RETURN (
683+ *parsed_expr.mutable_source_info (),
684+ internal::ConvertSourceInfoToProto (ast_impl.source_info ()));
688685
689686 return parsed_expr;
690687}
@@ -728,10 +725,11 @@ absl::StatusOr<cel::expr::CheckedExpr> CreateCheckedExprFromAst(
728725 const auto & ast_impl = ast_internal::AstImpl::CastFromPublicAst (ast);
729726 CheckedExprPb checked_expr;
730727 checked_expr.set_expr_version (ast_impl.expr_version ());
731- CEL_ASSIGN_OR_RETURN (*checked_expr.mutable_expr (),
732- ExprToProto (ast_impl.root_expr ()));
733- CEL_ASSIGN_OR_RETURN (*checked_expr.mutable_source_info (),
734- SourceInfoToProto (ast_impl.source_info ()));
728+ CEL_RETURN_IF_ERROR (
729+ ExprToProto (ast_impl.root_expr (), checked_expr.mutable_expr ()));
730+ CEL_ASSIGN_OR_RETURN (
731+ *checked_expr.mutable_source_info (),
732+ internal::ConvertSourceInfoToProto (ast_impl.source_info ()));
735733 for (auto it = ast_impl.reference_map ().begin ();
736734 it != ast_impl.reference_map ().end (); ++it) {
737735 ReferencePb& dest_reference =
0 commit comments