1 // Copyright (C) 2023 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 #ifndef ICING_QUERY_ADVANCED_QUERY_PARSER_PARAM_H_ 15 #define ICING_QUERY_ADVANCED_QUERY_PARSER_PARAM_H_ 16 17 #include "icing/text_classifier/lib3/utils/base/status.h" 18 #include "icing/absl_ports/canonical_errors.h" 19 #include "icing/query/advanced_query_parser/pending-value.h" 20 #include "icing/util/status-macros.h" 21 22 namespace icing { 23 namespace lib { 24 25 enum class Cardinality { 26 kRequired, 27 kOptional, 28 kVariable, 29 }; 30 31 struct Param { 32 explicit Param(DataType data_type, 33 Cardinality cardinality = Cardinality::kRequired) data_typeParam34 : data_type(data_type), cardinality(cardinality) {} 35 MatchesParam36 libtextclassifier3::Status Matches(PendingValue& arg) const { 37 bool matches = arg.data_type() == data_type; 38 // Values of type kText could also potentially be valid kLong values. If 39 // we're expecting a kLong and we have a kText, try to parse it as a kLong. 40 if (!matches && data_type == DataType::kLong && 41 arg.data_type() == DataType::kText) { 42 ICING_RETURN_IF_ERROR(arg.ParseInt()); 43 matches = true; 44 } 45 return matches ? libtextclassifier3::Status::OK 46 : absl_ports::InvalidArgumentError( 47 "Provided arg doesn't match required param type."); 48 } 49 50 DataType data_type; 51 Cardinality cardinality; 52 }; 53 54 } // namespace lib 55 } // namespace icing 56 57 #endif // ICING_QUERY_ADVANCED_QUERY_PARSER_PARAM_H_ 58