1616
1717#include < cstddef>
1818#include < cstdint>
19+ #include < iterator>
1920#include < map>
2021#include < ostream>
2122#include < string>
3435#include " absl/strings/numbers.h"
3536#include " absl/strings/str_cat.h"
3637#include " absl/strings/str_format.h"
38+ #include " absl/strings/str_join.h"
3739#include " absl/strings/string_view.h"
3840#include " absl/time/time.h"
41+ #include " absl/types/span.h"
3942#include " common/ast.h"
4043#include " common/constant.h"
4144#include " common/signature.h"
@@ -151,6 +154,73 @@ YAML::Node GetContextNodeForKeyValue(const YAML::Node& node,
151154 return value_node;
152155}
153156
157+ // Fields handled by the parser, for each kind of map node in the config.
158+ constexpr absl::string_view kConfigFields [] = {
159+ " name" , " description" , " container" , " extensions" ,
160+ " stdlib" , " context_variable" , " variables" , " functions" };
161+ // Fields that other CEL implementations support but cel-cpp does not.
162+ constexpr absl::string_view kUnsupportedConfigFields [] = {
163+ " imports" , " limits" , " validators" , " features" };
164+ constexpr absl::string_view kContainerFields [] = {" name" , " abbreviations" ,
165+ " aliases" };
166+ constexpr absl::string_view kAliasFields [] = {" alias" , " qualified_name" };
167+ constexpr absl::string_view kExtensionFields [] = {" name" , " version" };
168+ constexpr absl::string_view kStandardLibraryFields [] = {
169+ " disabled" , " disable" , " disable_macros" , " include_macros" ,
170+ " exclude_macros" , " include_functions" , " exclude_functions" };
171+ constexpr absl::string_view kFunctionSelectorFields [] = {" name" , " overloads" };
172+ constexpr absl::string_view kOverloadSelectorFields [] = {" id" };
173+ constexpr absl::string_view kContextVariableFields [] = {" type_name" , " type" };
174+ constexpr absl::string_view kTypeFields [] = {" type" , " type_name" ,
175+ " is_type_param" , " params" };
176+ // A variable may specify its type inline, so the type fields are included.
177+ constexpr absl::string_view kVariableFields [] = {
178+ " name" , " description" , " value" , " type" ,
179+ " type_name" , " is_type_param" , " params" };
180+ constexpr absl::string_view kFunctionFields [] = {" name" , " description" ,
181+ " overloads" };
182+ constexpr absl::string_view kOverloadFields [] = {
183+ " id" , " examples" , " signature" , " target" , " args" , " return" };
184+
185+ // Collects a warning for each field that the parser does not handle.
186+ class UnhandledFieldReporter {
187+ public:
188+ // Adds a warning for each key of the map `node` that is not in `handled`.
189+ // Keys in `unsupported` are reported as recognized but not supported.
190+ // Does nothing if `node` is not a map. Child nodes are not visited.
191+ void Check (const YAML ::Node& node, absl::string_view context,
192+ absl::Span<const absl::string_view> handled,
193+ absl::Span<const absl::string_view> unsupported = {}) {
194+ if (!node.IsDefined () || !node.IsMap ()) {
195+ return ;
196+ }
197+ for (const auto & kv : node) {
198+ if (!kv.first .IsScalar ()) {
199+ continue ;
200+ }
201+ const std::string& key = kv.first .Scalar ();
202+ if (absl::c_linear_search (handled, key)) {
203+ continue ;
204+ }
205+ const YAML ::Mark mark = kv.first .Mark ();
206+ if (absl::c_linear_search (unsupported, key)) {
207+ warnings_.push_back (absl::StrCat (mark.line + 1 , " :" , mark.column + 1 ,
208+ " : Field '" , key,
209+ " ' is not supported" ));
210+ } else {
211+ warnings_.push_back (absl::StrCat (mark.line + 1 , " :" , mark.column + 1 ,
212+ " : Unknown field '" , key, " ' in " ,
213+ context));
214+ }
215+ }
216+ }
217+
218+ std::vector<std::string>& warnings () { return warnings_; }
219+
220+ private:
221+ std::vector<std::string> warnings_;
222+ };
223+
154224absl::Status ParseName (Config& config, absl::string_view yaml,
155225 const YAML ::Node& root) {
156226 const YAML ::Node name = root[" name" ];
@@ -164,7 +234,8 @@ absl::Status ParseName(Config& config, absl::string_view yaml,
164234}
165235
166236absl::Status ParseContainerConfig (Config& config, absl::string_view yaml,
167- const YAML ::Node& root) {
237+ const YAML ::Node& root,
238+ UnhandledFieldReporter& reporter) {
168239 const YAML ::Node container = root[" container" ];
169240 if (!container.IsDefined ()) {
170241 return absl::OkStatus ();
@@ -180,6 +251,7 @@ absl::Status ParseContainerConfig(Config& config, absl::string_view yaml,
180251 " Node 'container' is neither a string nor a map" );
181252 }
182253
254+ reporter.Check (container, " container" , kContainerFields );
183255 Config::ContainerConfig container_config;
184256
185257 const YAML ::Node name = container[" name" ];
@@ -213,6 +285,7 @@ absl::Status ParseContainerConfig(Config& config, absl::string_view yaml,
213285 if (!alias_node.IsMap ()) {
214286 return YamlError (yaml, alias_node, " Alias entry is not a map" );
215287 }
288+ reporter.Check (alias_node, " alias" , kAliasFields );
216289 const YAML ::Node alias_key = alias_node[" alias" ];
217290 const YAML ::Node qualified_name_key = alias_node[" qualified_name" ];
218291
@@ -237,7 +310,8 @@ absl::Status ParseContainerConfig(Config& config, absl::string_view yaml,
237310
238311absl::Status ParseExtensionConfigs (Config& config, absl::string_view yaml,
239312 const YAML ::Node& root,
240- const EnvConfigFromYamlOptions& options) {
313+ const EnvConfigFromYamlOptions& options,
314+ UnhandledFieldReporter& reporter) {
241315 const YAML ::Node extensions = root[" extensions" ];
242316 if (!extensions.IsDefined ()) {
243317 return absl::OkStatus ();
@@ -250,6 +324,7 @@ absl::Status ParseExtensionConfigs(Config& config, absl::string_view yaml,
250324 if (!extension || !extension.IsMap ()) {
251325 return YamlError (yaml, extension, " Extension is not a map" );
252326 }
327+ reporter.Check (extension, " extension" , kExtensionFields );
253328 const YAML ::Node name = extension[" name" ];
254329 if (!name || !name.IsScalar ()) {
255330 return YamlError (yaml, name, " Extension name is not a string" );
@@ -314,7 +389,7 @@ absl::StatusOr<absl::flat_hash_set<std::string>> ParseMacroList(
314389
315390absl::StatusOr<absl::flat_hash_set<std::pair<std::string, std::string>>>
316391ParseFunctionList (absl::string_view yaml, const YAML ::Node& standard_library,
317- absl::string_view key) {
392+ absl::string_view key, UnhandledFieldReporter& reporter ) {
318393 absl::flat_hash_set<std::pair<std::string, std::string>> function_set;
319394 const YAML ::Node functions = standard_library[std::string (key)];
320395 if (!functions.IsDefined ()) {
@@ -329,6 +404,7 @@ ParseFunctionList(absl::string_view yaml, const YAML::Node& standard_library,
329404 return YamlError (yaml, function,
330405 absl::StrCat (" Entry in '" , key, " ' is not a map" ));
331406 }
407+ reporter.Check (function, " function selector" , kFunctionSelectorFields );
332408 const YAML ::Node name = function[" name" ];
333409 if (!name.IsDefined ()) {
334410 return YamlError (
@@ -356,6 +432,7 @@ ParseFunctionList(absl::string_view yaml, const YAML::Node& standard_library,
356432 yaml, overload,
357433 absl::StrCat (" Overload in '" , key, " ' entry is not a map" ));
358434 }
435+ reporter.Check (overload, " overload selector" , kOverloadSelectorFields );
359436 const YAML ::Node id = overload[" id" ];
360437 if (!id || !id.IsScalar ()) {
361438 return YamlError (
@@ -370,7 +447,8 @@ ParseFunctionList(absl::string_view yaml, const YAML::Node& standard_library,
370447}
371448
372449absl::Status ParseStandardLibraryConfig (Config& config, absl::string_view yaml,
373- const YAML ::Node& root) {
450+ const YAML ::Node& root,
451+ UnhandledFieldReporter& reporter) {
374452 const YAML ::Node standard_library = root[" stdlib" ];
375453 if (!standard_library.IsDefined ()) {
376454 return absl::OkStatus ();
@@ -381,6 +459,7 @@ absl::Status ParseStandardLibraryConfig(Config& config, absl::string_view yaml,
381459 " Standard library config ('stdlib') is not a map" );
382460 }
383461
462+ reporter.Check (standard_library, " stdlib" , kStandardLibraryFields );
384463 Config::StandardLibraryConfig standard_library_config;
385464
386465 // `disabled` is the key used by the other CEL implementations.
@@ -430,17 +509,23 @@ absl::Status ParseStandardLibraryConfig(Config& config, absl::string_view yaml,
430509
431510 CEL_ASSIGN_OR_RETURN (
432511 standard_library_config.included_functions ,
433- ParseFunctionList (yaml, standard_library, " include_functions" ));
512+ ParseFunctionList (yaml, standard_library, " include_functions" , reporter ));
434513
435514 CEL_ASSIGN_OR_RETURN (
436515 standard_library_config.excluded_functions ,
437- ParseFunctionList (yaml, standard_library, " exclude_functions" ));
516+ ParseFunctionList (yaml, standard_library, " exclude_functions" , reporter ));
438517
439518 return config.SetStandardLibraryConfig (standard_library_config);
440519}
441520
442- absl::StatusOr<Config::TypeInfo> ParseTypeInfo (const YAML ::Node& node,
443- absl::string_view yaml) {
521+ absl::StatusOr<Config::TypeInfo> ParseTypeInfo (
522+ const YAML ::Node& node, absl::string_view yaml,
523+ UnhandledFieldReporter& reporter);
524+
525+ // Parses the type fields of `node` without checking for unhandled fields.
526+ absl::StatusOr<Config::TypeInfo> ParseTypeInfoFields (
527+ const YAML ::Node& node, absl::string_view yaml,
528+ UnhandledFieldReporter& reporter) {
444529 Config::TypeInfo type_config;
445530 const YAML ::Node type = node[" type" ];
446531 const YAML ::Node type_name = node[" type_name" ];
@@ -485,13 +570,20 @@ absl::StatusOr<Config::TypeInfo> ParseTypeInfo(const YAML::Node& node,
485570 }
486571 for (const YAML ::Node& param : params) {
487572 CEL_ASSIGN_OR_RETURN (Config::TypeInfo param_config,
488- ParseTypeInfo (param, yaml));
573+ ParseTypeInfo (param, yaml, reporter ));
489574 type_config.params .push_back (param_config);
490575 }
491576
492577 return type_config;
493578}
494579
580+ absl::StatusOr<Config::TypeInfo> ParseTypeInfo (
581+ const YAML ::Node& node, absl::string_view yaml,
582+ UnhandledFieldReporter& reporter) {
583+ reporter.Check (node, " type" , kTypeFields );
584+ return ParseTypeInfoFields (node, yaml, reporter);
585+ }
586+
495587bool CompareTypeInfo (const Config::TypeInfo& a, const Config::TypeInfo& b) {
496588 if (a.name != b.name ) {
497589 return a.name < b.name ;
@@ -612,7 +704,8 @@ absl::StatusOr<Constant> ParseConstantValue(absl::string_view yaml,
612704}
613705
614706absl::Status ParseVariableConfigs (Config& config, absl::string_view yaml,
615- const YAML ::Node& root) {
707+ const YAML ::Node& root,
708+ UnhandledFieldReporter& reporter) {
616709 const YAML ::Node variables = root[" variables" ];
617710 if (!variables.IsDefined ()) {
618711 return absl::OkStatus ();
@@ -626,6 +719,7 @@ absl::Status ParseVariableConfigs(Config& config, absl::string_view yaml,
626719 if (!variable || !variable.IsMap ()) {
627720 return YamlError (yaml, variable, " Variable is not a map" );
628721 }
722+ reporter.Check (variable, " variable" , kVariableFields );
629723 const YAML ::Node name = variable[" name" ];
630724 if (!name || !name.IsScalar ()) {
631725 return YamlError (yaml, name, " Variable name is not a string" );
@@ -643,9 +737,11 @@ absl::Status ParseVariableConfigs(Config& config, absl::string_view yaml,
643737 Config::TypeInfo type_info;
644738 if (type.IsDefined () && !type.IsScalar ()) {
645739 // Old format, type spec is in 'type' instead of directly embedded.
646- CEL_ASSIGN_OR_RETURN (type_info, ParseTypeInfo (variable[" type" ], yaml));
740+ CEL_ASSIGN_OR_RETURN (type_info,
741+ ParseTypeInfo (variable[" type" ], yaml, reporter));
647742 } else {
648- CEL_ASSIGN_OR_RETURN (type_info, ParseTypeInfo (variable, yaml));
743+ CEL_ASSIGN_OR_RETURN (type_info,
744+ ParseTypeInfoFields (variable, yaml, reporter));
649745 }
650746 ConstantKindCase constant_kind_case = GetConstantKindCase (type_info.name );
651747 std::string value_str;
@@ -684,11 +780,12 @@ absl::Status ParseVariableConfigs(Config& config, absl::string_view yaml,
684780
685781absl::StatusOr<Config::FunctionOverloadConfig> ParseFunctionOverloadConfig (
686782 absl::string_view yaml, const YAML ::Node& overload,
687- absl::string_view function_name) {
783+ absl::string_view function_name, UnhandledFieldReporter& reporter ) {
688784 Config::FunctionOverloadConfig overload_config;
689785 if (!overload || !overload.IsMap ()) {
690786 return YamlError (yaml, overload, " Function overload is not a map" );
691787 }
788+ reporter.Check (overload, " overload" , kOverloadFields );
692789 const YAML ::Node id = overload[" id" ];
693790 if (id.IsDefined ()) {
694791 if (!id.IsScalar ()) {
@@ -761,7 +858,7 @@ absl::StatusOr<Config::FunctionOverloadConfig> ParseFunctionOverloadConfig(
761858 return YamlError (yaml, target, " Function overload target is not a map" );
762859 }
763860 CEL_ASSIGN_OR_RETURN (Config::TypeInfo type_info,
764- ParseTypeInfo (target, yaml));
861+ ParseTypeInfo (target, yaml, reporter ));
765862 overload_config.is_member_function = true ;
766863 overload_config.parameters .push_back (type_info);
767864 }
@@ -776,7 +873,7 @@ absl::StatusOr<Config::FunctionOverloadConfig> ParseFunctionOverloadConfig(
776873 return YamlError (yaml, arg, " Function overload arg is not a map" );
777874 }
778875 CEL_ASSIGN_OR_RETURN (Config::TypeInfo type_info,
779- ParseTypeInfo (arg, yaml));
876+ ParseTypeInfo (arg, yaml, reporter ));
780877 overload_config.parameters .push_back (type_info);
781878 }
782879 }
@@ -790,7 +887,7 @@ absl::StatusOr<Config::FunctionOverloadConfig> ParseFunctionOverloadConfig(
790887 TypeSpecToTypeInfo (type_spec));
791888 } else if (return_type.IsMap ()) {
792889 CEL_ASSIGN_OR_RETURN (overload_config.return_type ,
793- ParseTypeInfo (return_type, yaml));
890+ ParseTypeInfo (return_type, yaml, reporter ));
794891 } else {
795892 return YamlError (
796893 yaml, return_type,
@@ -801,7 +898,8 @@ absl::StatusOr<Config::FunctionOverloadConfig> ParseFunctionOverloadConfig(
801898}
802899
803900absl::Status ParseFunctionConfigs (Config& config, absl::string_view yaml,
804- const YAML ::Node& root) {
901+ const YAML ::Node& root,
902+ UnhandledFieldReporter& reporter) {
805903 const YAML ::Node functions = root[" functions" ];
806904 if (!functions.IsDefined ()) {
807905 return absl::OkStatus ();
@@ -815,6 +913,7 @@ absl::Status ParseFunctionConfigs(Config& config, absl::string_view yaml,
815913 if (!function || !function.IsMap ()) {
816914 return YamlError (yaml, function, " Function is not a map" );
817915 }
916+ reporter.Check (function, " function" , kFunctionFields );
818917 const YAML ::Node name = function[" name" ];
819918 if (!name || !name.IsScalar ()) {
820919 return YamlError (yaml, name, " Function name is not a string" );
@@ -838,7 +937,8 @@ absl::Status ParseFunctionConfigs(Config& config, absl::string_view yaml,
838937 for (const YAML ::Node& overload : overloads) {
839938 CEL_ASSIGN_OR_RETURN (
840939 Config::FunctionOverloadConfig overload_config,
841- ParseFunctionOverloadConfig (yaml, overload, function_config.name ));
940+ ParseFunctionOverloadConfig (yaml, overload, function_config.name ,
941+ reporter));
842942 function_config.overload_configs .push_back (std::move (overload_config));
843943 }
844944 }
@@ -1280,7 +1380,8 @@ void EmitFunctionConfigs(const Config& env_config, YAML::Emitter& out,
12801380}
12811381
12821382absl::Status ParseContextVariableConfig (Config& config, absl::string_view yaml,
1283- const YAML ::Node& root) {
1383+ const YAML ::Node& root,
1384+ UnhandledFieldReporter& reporter) {
12841385 const YAML ::Node context_variable = root[" context_variable" ];
12851386 if (!context_variable.IsDefined ()) {
12861387 return absl::OkStatus ();
@@ -1290,6 +1391,7 @@ absl::Status ParseContextVariableConfig(Config& config, absl::string_view yaml,
12901391 " Node 'context_variable' is not a map" );
12911392 }
12921393
1394+ reporter.Check (context_variable, " context_variable" , kContextVariableFields );
12931395 const YAML ::Node type_name = context_variable[" type_name" ];
12941396 const YAML ::Node type = context_variable[" type" ];
12951397 const YAML ::Node* type_node = nullptr ;
@@ -1321,13 +1423,28 @@ absl::StatusOr<Config> EnvConfigFromYaml(
13211423 yaml, " Invalid CEL environment config YAML" , root.Mark ()));
13221424 }
13231425
1426+ UnhandledFieldReporter reporter;
1427+ reporter.Check (root, " config" , kConfigFields , kUnsupportedConfigFields );
13241428 CEL_RETURN_IF_ERROR (ParseName (config, yaml, root));
1325- CEL_RETURN_IF_ERROR (ParseContainerConfig (config, yaml, root));
1326- CEL_RETURN_IF_ERROR (ParseExtensionConfigs (config, yaml, root, options));
1327- CEL_RETURN_IF_ERROR (ParseStandardLibraryConfig (config, yaml, root));
1328- CEL_RETURN_IF_ERROR (ParseContextVariableConfig (config, yaml, root));
1329- CEL_RETURN_IF_ERROR (ParseVariableConfigs (config, yaml, root));
1330- CEL_RETURN_IF_ERROR (ParseFunctionConfigs (config, yaml, root));
1429+ CEL_RETURN_IF_ERROR (ParseContainerConfig (config, yaml, root, reporter));
1430+ CEL_RETURN_IF_ERROR (
1431+ ParseExtensionConfigs (config, yaml, root, options, reporter));
1432+ CEL_RETURN_IF_ERROR (ParseStandardLibraryConfig (config, yaml, root, reporter));
1433+ CEL_RETURN_IF_ERROR (ParseContextVariableConfig (config, yaml, root, reporter));
1434+ CEL_RETURN_IF_ERROR (ParseVariableConfigs (config, yaml, root, reporter));
1435+ CEL_RETURN_IF_ERROR (ParseFunctionConfigs (config, yaml, root, reporter));
1436+
1437+ std::vector<std::string>& warnings = reporter.warnings ();
1438+ if (options.strict && !warnings.empty ()) {
1439+ return absl::InvalidArgumentError (
1440+ absl::StrCat (" Unhandled fields in CEL environment config:\n " ,
1441+ absl::StrJoin (warnings, " \n " )));
1442+ }
1443+ if (options.warnings != nullptr ) {
1444+ options.warnings ->insert (options.warnings ->end (),
1445+ std::make_move_iterator (warnings.begin ()),
1446+ std::make_move_iterator (warnings.end ()));
1447+ }
13311448 return config;
13321449}
13331450
0 commit comments