1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
// Copyright (C) 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "icing/schema/property-util.h"
#include <string>
#include <string_view>
#include <utility>
#include <vector>
#include "icing/text_classifier/lib3/utils/base/statusor.h"
#include "icing/absl_ports/canonical_errors.h"
#include "icing/absl_ports/str_cat.h"
#include "icing/absl_ports/str_join.h"
#include "icing/proto/document.pb.h"
namespace icing {
namespace lib {
namespace property_util {
std::string ConvertToPropertyExprIndexStr(int index) {
if (index == kWildcardPropertyIndex) {
return "";
}
return absl_ports::StrCat(kLBracket, std::to_string(index), kRBracket);
}
std::string ConcatenatePropertyPathExpr(std::string_view property_path_expr1,
std::string_view property_path_expr2) {
if (property_path_expr1.empty()) {
return std::string(property_path_expr2);
}
if (property_path_expr2.empty()) {
return std::string(property_path_expr1);
}
return absl_ports::StrCat(property_path_expr1, kPropertyPathSeparator,
property_path_expr2);
}
std::vector<std::string_view> SplitPropertyPathExpr(
std::string_view property_path_expr) {
return absl_ports::StrSplit(property_path_expr, kPropertyPathSeparator);
}
PropertyInfo ParsePropertyNameExpr(std::string_view property_name_expr) {
size_t l_bracket = property_name_expr.find(kLBracket);
if (l_bracket == std::string_view::npos ||
l_bracket >= property_name_expr.length()) {
return PropertyInfo(std::string(property_name_expr),
kWildcardPropertyIndex);
}
size_t r_bracket = property_name_expr.find(kRBracket, l_bracket);
if (r_bracket == std::string_view::npos || r_bracket - l_bracket < 2) {
return PropertyInfo(std::string(property_name_expr),
kWildcardPropertyIndex);
}
std::string index_string = std::string(
property_name_expr.substr(l_bracket + 1, r_bracket - l_bracket - 1));
return PropertyInfo(std::string(property_name_expr.substr(0, l_bracket)),
std::stoi(index_string));
}
std::vector<PropertyInfo> ParsePropertyPathExpr(
std::string_view property_path_expr) {
std::vector<std::string_view> property_name_exprs =
SplitPropertyPathExpr(property_path_expr);
std::vector<PropertyInfo> property_infos;
property_infos.reserve(property_name_exprs.size());
for (std::string_view property_name_expr : property_name_exprs) {
property_infos.push_back(ParsePropertyNameExpr(property_name_expr));
}
return property_infos;
}
const PropertyProto* GetPropertyProto(const DocumentProto& document,
std::string_view property_name) {
for (const PropertyProto& property : document.properties()) {
if (property.name() == property_name) {
return &property;
}
}
return nullptr;
}
template <>
libtextclassifier3::StatusOr<std::vector<std::string>>
ExtractPropertyValues<std::string>(const PropertyProto& property) {
return std::vector<std::string>(property.string_values().begin(),
property.string_values().end());
}
template <>
libtextclassifier3::StatusOr<std::vector<std::string_view>>
ExtractPropertyValues<std::string_view>(const PropertyProto& property) {
return std::vector<std::string_view>(property.string_values().begin(),
property.string_values().end());
}
template <>
libtextclassifier3::StatusOr<std::vector<int64_t>>
ExtractPropertyValues<int64_t>(const PropertyProto& property) {
return std::vector<int64_t>(property.int64_values().begin(),
property.int64_values().end());
}
} // namespace property_util
} // namespace lib
} // namespace icing
|