@@ -94,7 +94,7 @@ struct ExprtkGFunction : public exprtk::igeneric_function<numerical_type> {
9494 }
9595};
9696
97- struct ExprtkOtherFunction : public exprtk ::igeneric_function<numerical_type> {
97+ struct ExprtkMatchFunction : public exprtk ::igeneric_function<numerical_type> {
9898 typedef typename exprtk::igeneric_function<numerical_type>::generic_type generic_type;
9999
100100 typedef typename generic_type::scalar_view scalar_t ;
@@ -107,9 +107,9 @@ struct ExprtkOtherFunction : public exprtk::igeneric_function<numerical_type> {
107107 casbin::MatchingFunc func_;
108108
109109public:
110- ExprtkOtherFunction (const std::string& idenfier, casbin::MatchingFunc func) : exprtk::igeneric_function<numerical_type>(idenfier), func_(func) {}
110+ ExprtkMatchFunction (const std::string& idenfier, casbin::MatchingFunc func) : exprtk::igeneric_function<numerical_type>(idenfier), func_(func) {}
111111
112- ExprtkOtherFunction () : exprtk::igeneric_function<numerical_type>(" ss" ) {}
112+ ExprtkMatchFunction () : exprtk::igeneric_function<numerical_type>(" ss" ) {}
113113
114114 inline numerical_type operator ()(parameter_list_t parameters) {
115115 bool res = false ;
@@ -143,6 +143,90 @@ struct ExprtkOtherFunction : public exprtk::igeneric_function<numerical_type> {
143143 }
144144};
145145
146+ // KeyGet
147+ struct ExprtkGetFunction : public exprtk ::igeneric_function<numerical_type> {
148+ typedef exprtk::igeneric_function<numerical_type> igenfunct_t ;
149+ typedef typename igenfunct_t ::generic_type generic_t ;
150+ typedef typename igenfunct_t ::parameter_list_t parameter_list_t ;
151+ typedef typename generic_t ::string_view string_t ;
152+
153+ private:
154+ using MatchingFunc = std::function<std::string(const std::string&, const std::string&)>;
155+ MatchingFunc func_;
156+
157+ public:
158+ ExprtkGetFunction (const std::string& idenfier, MatchingFunc func) : igenfunct_t(idenfier, igenfunct_t ::e_rtrn_string), func_(func) {}
159+
160+ ExprtkGetFunction () : igenfunct_t(" SS" , igenfunct_t ::e_rtrn_string) {}
161+
162+ inline numerical_type operator ()(std::string& result, parameter_list_t parameters) {
163+ result.clear ();
164+
165+ // check value cnt
166+ if (parameters.size () != 2 ) {
167+ return numerical_type (0 );
168+ }
169+
170+ // check value type
171+ for (std::size_t i = 0 ; i < parameters.size (); ++i) {
172+ generic_type& gt = parameters[i];
173+ if (generic_type::e_string != gt.type ) {
174+ return numerical_type (0 );
175+ }
176+ }
177+ std::string key1 = exprtk::to_str (string_t (parameters[0 ]));
178+ std::string key2 = exprtk::to_str (string_t (parameters[1 ]));
179+
180+ if (this ->func_ != nullptr ) {
181+ result = this ->func_ (key1, key2);
182+ }
183+
184+ return numerical_type (0 );
185+ }
186+ };
187+
188+ struct ExprtkGetWithPathFunction : public exprtk ::igeneric_function<numerical_type> {
189+ typedef exprtk::igeneric_function<numerical_type> igenfunct_t ;
190+ typedef typename igenfunct_t ::generic_type generic_t ;
191+ typedef typename igenfunct_t ::parameter_list_t parameter_list_t ;
192+ typedef typename generic_t ::string_view string_t ;
193+
194+ private:
195+ using MatchingFunc = std::function<std::string(const std::string&, const std::string&, const std::string&)>;
196+ MatchingFunc func_;
197+
198+ public:
199+ ExprtkGetWithPathFunction (const std::string& idenfier, MatchingFunc func) : igenfunct_t(idenfier, igenfunct_t ::e_rtrn_string), func_(func) {}
200+
201+ ExprtkGetWithPathFunction () : igenfunct_t(" SSS" , igenfunct_t ::e_rtrn_string) {}
202+
203+ inline numerical_type operator ()(std::string& result, parameter_list_t parameters) {
204+ result.clear ();
205+
206+ // check value cnt
207+ if (parameters.size () != 3 ) {
208+ return numerical_type (0 );
209+ }
210+
211+ // check value type
212+ for (std::size_t i = 0 ; i < parameters.size (); ++i) {
213+ generic_type& gt = parameters[i];
214+ if (generic_type::e_string != gt.type ) {
215+ return numerical_type (0 );
216+ }
217+ }
218+ std::string key1 = exprtk::to_str (string_t (parameters[0 ]));
219+ std::string key2 = exprtk::to_str (string_t (parameters[1 ]));
220+ std::string path_var = exprtk::to_str (string_t (parameters[2 ]));
221+
222+ if (this ->func_ != nullptr ) {
223+ result = this ->func_ (key1, key2, path_var);
224+ }
225+
226+ return numerical_type (0 );
227+ }
228+ };
229+
146230enum class ExprtkFunctionType {
147231 Unknown,
148232 Gfunction,
@@ -152,6 +236,9 @@ enum class ExprtkFunctionType {
152236 KeyMatch4,
153237 RegexMatch,
154238 IpMatch,
239+ KeyGet,
240+ KeyGet2,
241+ KeyGet3,
155242};
156243
157244class ExprtkFunctionFactory {
@@ -164,22 +251,31 @@ class ExprtkFunctionFactory {
164251 func = std::make_shared<ExprtkGFunction>(idenfier, rm);
165252 break ;
166253 case ExprtkFunctionType::KeyMatch:
167- func.reset (new ExprtkOtherFunction (idenfier, KeyMatch));
254+ func.reset (new ExprtkMatchFunction (idenfier, KeyMatch));
168255 break ;
169256 case ExprtkFunctionType::KeyMatch2:
170- func.reset (new ExprtkOtherFunction (idenfier, KeyMatch2));
257+ func.reset (new ExprtkMatchFunction (idenfier, KeyMatch2));
171258 break ;
172259 case ExprtkFunctionType::KeyMatch3:
173- func.reset (new ExprtkOtherFunction (idenfier, KeyMatch3));
260+ func.reset (new ExprtkMatchFunction (idenfier, KeyMatch3));
174261 break ;
175262 case ExprtkFunctionType::KeyMatch4:
176- func.reset (new ExprtkOtherFunction (idenfier, KeyMatch4));
263+ func.reset (new ExprtkMatchFunction (idenfier, KeyMatch4));
177264 break ;
178265 case ExprtkFunctionType::IpMatch:
179- func.reset (new ExprtkOtherFunction (idenfier, IPMatch));
266+ func.reset (new ExprtkMatchFunction (idenfier, IPMatch));
180267 break ;
181268 case ExprtkFunctionType::RegexMatch:
182- func.reset (new ExprtkOtherFunction (idenfier, RegexMatch));
269+ func.reset (new ExprtkMatchFunction (idenfier, RegexMatch));
270+ break ;
271+ case ExprtkFunctionType::KeyGet:
272+ func.reset (new ExprtkGetFunction (idenfier, KeyGet));
273+ break ;
274+ case ExprtkFunctionType::KeyGet2:
275+ func.reset (new ExprtkGetWithPathFunction (idenfier, KeyGet2));
276+ break ;
277+ case ExprtkFunctionType::KeyGet3:
278+ func.reset (new ExprtkGetWithPathFunction (idenfier, KeyGet3));
183279 break ;
184280 default :
185281 func = nullptr ;
0 commit comments