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 or kDouble 39 // values. If we're expecting a kLong or kDouble and we have a kText, try to 40 // parse it as what we expect. 41 if (!matches && data_type == DataType::kLong && 42 arg.data_type() == DataType::kText) { 43 ICING_RETURN_IF_ERROR(arg.ParseInt()); 44 matches = true; 45 } 46 if (!matches && data_type == DataType::kDouble && 47 arg.data_type() == DataType::kText) { 48 ICING_RETURN_IF_ERROR(arg.ParseDouble()); 49 matches = true; 50 } 51 return matches ? libtextclassifier3::Status::OK 52 : absl_ports::InvalidArgumentError( 53 "Provided arg doesn't match required param type."); 54 } 55 56 DataType data_type; 57 Cardinality cardinality; 58 }; 59 60 } // namespace lib 61 } // namespace icing 62 63 #endif // ICING_QUERY_ADVANCED_QUERY_PARSER_PARAM_H_ 64