You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@unomi.apache.org by GitBox <gi...@apache.org> on 2020/11/17 23:30:31 UTC

[GitHub] [unomi] jkevan commented on a change in pull request #218: UNOMI-400 Refactoring of hardcoded property accessors

jkevan commented on a change in pull request #218:
URL: https://github.com/apache/unomi/pull/218#discussion_r525591625



##########
File path: plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/conditions/accessors/HardcodedPropertyAccessorRegistry.java
##########
@@ -0,0 +1,131 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+package org.apache.unomi.plugins.baseplugin.conditions.accessors;
+
+import org.apache.unomi.api.*;
+import org.apache.unomi.api.campaigns.Campaign;
+import org.apache.unomi.api.goals.Goal;
+import org.apache.unomi.api.rules.Rule;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.*;
+
+/**
+ * This class contains the registry of all the hardcoded property accessors.
+ * For the moment this list of accessors is hardcoded, but in a future update it could be made dynamic.
+ */
+public class HardcodedPropertyAccessorRegistry {
+
+    private static final Logger logger = LoggerFactory.getLogger(HardcodedPropertyAccessorRegistry.class.getName());
+
+    Map<String, HardcodedPropertyAccessor> propertyAccessors = new HashMap<>();
+
+    public HardcodedPropertyAccessorRegistry() {
+        propertyAccessors.put(Item.class.getName(), new ItemHardcodedPropertyAccessor(this));
+        propertyAccessors.put(MetadataItem.class.getName(), new MetadataItemHardcodedPropertyAccessor(this));
+        propertyAccessors.put(Metadata.class.getName(), new MetadataHardcodedPropertyAccessor(this));
+        propertyAccessors.put(TimestampedItem.class.getName(), new TimestampedItemHardcodedPropertyAccessor(this));
+        propertyAccessors.put(Event.class.getName(), new EventHardcodedPropertyAccessor(this));
+        propertyAccessors.put(Profile.class.getName(), new ProfileHardcodedPropertyAccessor(this));
+        propertyAccessors.put(Consent.class.getName(), new ConsentHardcodedPropertyAccessor(this));
+        propertyAccessors.put(Session.class.getName(), new SessionHardcodedPropertyAccessor(this));
+        propertyAccessors.put(Rule.class.getName(), new RuleHardcodedPropertyAccessor(this));
+        propertyAccessors.put(Goal.class.getName(), new GoalHardcodedPropertyAccessor(this));
+        propertyAccessors.put(CustomItem.class.getName(), new CustomItemHardcodedPropertyAccessor(this));
+        propertyAccessors.put(Campaign.class.getName(), new CampaignHardcodedPropertyAccessor(this));
+        propertyAccessors.put(Map.class.getName(), new MapHardcodedPropertyAccessor(this));
+    }
+
+    public static class NextTokens {
+        public String propertyName;
+        public String leftoverExpression;
+    }
+
+    protected NextTokens getNextTokens(String expression) {
+        NextTokens nextTokens = new NextTokens();
+        if (expression.startsWith("[\"")) {
+            int lookupNameBeginPos = "[\"".length();
+            int lookupNameEndPos = expression.indexOf("\"].", lookupNameBeginPos);
+            if (lookupNameEndPos > lookupNameBeginPos) {
+                nextTokens.propertyName = expression.substring(lookupNameBeginPos, lookupNameEndPos);
+                nextTokens.leftoverExpression = expression.substring(lookupNameEndPos+2);
+            } else {
+                nextTokens.propertyName = expression.substring(lookupNameBeginPos);
+                nextTokens.leftoverExpression = null;
+            }
+        } else if (expression.startsWith(".")) {
+            int lookupNameBeginPos = ".".length();
+            int lookupNameEndPos = lookupNameBeginPos;
+            int dotlookupNameEndPos = expression.indexOf(".", lookupNameBeginPos);
+            int squareBracketlookupNameEndPos = expression.indexOf("[", lookupNameBeginPos);
+            if (dotlookupNameEndPos > lookupNameBeginPos && squareBracketlookupNameEndPos > lookupNameBeginPos) {
+                lookupNameEndPos = Math.min(dotlookupNameEndPos, squareBracketlookupNameEndPos);
+            } else if (dotlookupNameEndPos > lookupNameBeginPos) {
+                lookupNameEndPos = dotlookupNameEndPos;
+            } else if (squareBracketlookupNameEndPos > lookupNameBeginPos) {
+                lookupNameEndPos = squareBracketlookupNameEndPos;
+            } else {
+                lookupNameEndPos = -1;
+            }
+            if (lookupNameEndPos > lookupNameBeginPos) {
+                nextTokens.propertyName = expression.substring(lookupNameBeginPos, lookupNameEndPos);
+                nextTokens.leftoverExpression = expression.substring(lookupNameEndPos);
+            } else {
+                nextTokens.propertyName = expression.substring(lookupNameBeginPos);
+                nextTokens.leftoverExpression = null;
+            }
+        } else {
+            int lookupNameBeginPos = 0;
+            int lookupNameEndPos = expression.indexOf(".", lookupNameBeginPos);
+            if (lookupNameEndPos > lookupNameBeginPos) {
+                nextTokens.propertyName = expression.substring(lookupNameBeginPos, lookupNameEndPos);
+                nextTokens.leftoverExpression = expression.substring(lookupNameEndPos);
+            } else {
+                nextTokens.propertyName = expression.substring(lookupNameBeginPos);
+                nextTokens.leftoverExpression = null;
+            }

Review comment:
       this code is duplicated in the 3 cases, we could move it out of the tree cases and just calculated the `lookupNameBeginPos, lookupNameEndPos.`
   Then do the this code at the end of the 3 cases check.

##########
File path: plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/conditions/accessors/HardcodedPropertyAccessorRegistry.java
##########
@@ -0,0 +1,131 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+package org.apache.unomi.plugins.baseplugin.conditions.accessors;
+
+import org.apache.unomi.api.*;
+import org.apache.unomi.api.campaigns.Campaign;
+import org.apache.unomi.api.goals.Goal;
+import org.apache.unomi.api.rules.Rule;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.*;
+
+/**
+ * This class contains the registry of all the hardcoded property accessors.
+ * For the moment this list of accessors is hardcoded, but in a future update it could be made dynamic.
+ */
+public class HardcodedPropertyAccessorRegistry {
+
+    private static final Logger logger = LoggerFactory.getLogger(HardcodedPropertyAccessorRegistry.class.getName());
+
+    Map<String, HardcodedPropertyAccessor> propertyAccessors = new HashMap<>();
+
+    public HardcodedPropertyAccessorRegistry() {
+        propertyAccessors.put(Item.class.getName(), new ItemHardcodedPropertyAccessor(this));
+        propertyAccessors.put(MetadataItem.class.getName(), new MetadataItemHardcodedPropertyAccessor(this));
+        propertyAccessors.put(Metadata.class.getName(), new MetadataHardcodedPropertyAccessor(this));
+        propertyAccessors.put(TimestampedItem.class.getName(), new TimestampedItemHardcodedPropertyAccessor(this));
+        propertyAccessors.put(Event.class.getName(), new EventHardcodedPropertyAccessor(this));
+        propertyAccessors.put(Profile.class.getName(), new ProfileHardcodedPropertyAccessor(this));
+        propertyAccessors.put(Consent.class.getName(), new ConsentHardcodedPropertyAccessor(this));
+        propertyAccessors.put(Session.class.getName(), new SessionHardcodedPropertyAccessor(this));
+        propertyAccessors.put(Rule.class.getName(), new RuleHardcodedPropertyAccessor(this));
+        propertyAccessors.put(Goal.class.getName(), new GoalHardcodedPropertyAccessor(this));
+        propertyAccessors.put(CustomItem.class.getName(), new CustomItemHardcodedPropertyAccessor(this));
+        propertyAccessors.put(Campaign.class.getName(), new CampaignHardcodedPropertyAccessor(this));
+        propertyAccessors.put(Map.class.getName(), new MapHardcodedPropertyAccessor(this));
+    }
+
+    public static class NextTokens {
+        public String propertyName;
+        public String leftoverExpression;
+    }
+
+    protected NextTokens getNextTokens(String expression) {
+        NextTokens nextTokens = new NextTokens();
+        if (expression.startsWith("[\"")) {
+            int lookupNameBeginPos = "[\"".length();
+            int lookupNameEndPos = expression.indexOf("\"].", lookupNameBeginPos);
+            if (lookupNameEndPos > lookupNameBeginPos) {
+                nextTokens.propertyName = expression.substring(lookupNameBeginPos, lookupNameEndPos);
+                nextTokens.leftoverExpression = expression.substring(lookupNameEndPos+2);
+            } else {
+                nextTokens.propertyName = expression.substring(lookupNameBeginPos);
+                nextTokens.leftoverExpression = null;
+            }

Review comment:
       `["foo"].bar` is covered
   `["foo"]` is not covered
   
   Also could be great to cover this function with unit test, since it's covering a lot of cases, and it should not be that complicated todo.

##########
File path: plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/conditions/accessors/HardcodedPropertyAccessorRegistry.java
##########
@@ -0,0 +1,131 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+package org.apache.unomi.plugins.baseplugin.conditions.accessors;
+
+import org.apache.unomi.api.*;
+import org.apache.unomi.api.campaigns.Campaign;
+import org.apache.unomi.api.goals.Goal;
+import org.apache.unomi.api.rules.Rule;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.*;
+
+/**
+ * This class contains the registry of all the hardcoded property accessors.
+ * For the moment this list of accessors is hardcoded, but in a future update it could be made dynamic.
+ */
+public class HardcodedPropertyAccessorRegistry {
+
+    private static final Logger logger = LoggerFactory.getLogger(HardcodedPropertyAccessorRegistry.class.getName());
+
+    Map<String, HardcodedPropertyAccessor> propertyAccessors = new HashMap<>();
+
+    public HardcodedPropertyAccessorRegistry() {
+        propertyAccessors.put(Item.class.getName(), new ItemHardcodedPropertyAccessor(this));
+        propertyAccessors.put(MetadataItem.class.getName(), new MetadataItemHardcodedPropertyAccessor(this));
+        propertyAccessors.put(Metadata.class.getName(), new MetadataHardcodedPropertyAccessor(this));
+        propertyAccessors.put(TimestampedItem.class.getName(), new TimestampedItemHardcodedPropertyAccessor(this));
+        propertyAccessors.put(Event.class.getName(), new EventHardcodedPropertyAccessor(this));
+        propertyAccessors.put(Profile.class.getName(), new ProfileHardcodedPropertyAccessor(this));
+        propertyAccessors.put(Consent.class.getName(), new ConsentHardcodedPropertyAccessor(this));
+        propertyAccessors.put(Session.class.getName(), new SessionHardcodedPropertyAccessor(this));
+        propertyAccessors.put(Rule.class.getName(), new RuleHardcodedPropertyAccessor(this));
+        propertyAccessors.put(Goal.class.getName(), new GoalHardcodedPropertyAccessor(this));
+        propertyAccessors.put(CustomItem.class.getName(), new CustomItemHardcodedPropertyAccessor(this));
+        propertyAccessors.put(Campaign.class.getName(), new CampaignHardcodedPropertyAccessor(this));
+        propertyAccessors.put(Map.class.getName(), new MapHardcodedPropertyAccessor(this));
+    }
+
+    public static class NextTokens {
+        public String propertyName;
+        public String leftoverExpression;
+    }
+
+    protected NextTokens getNextTokens(String expression) {
+        NextTokens nextTokens = new NextTokens();
+        if (expression.startsWith("[\"")) {
+            int lookupNameBeginPos = "[\"".length();
+            int lookupNameEndPos = expression.indexOf("\"].", lookupNameBeginPos);
+            if (lookupNameEndPos > lookupNameBeginPos) {
+                nextTokens.propertyName = expression.substring(lookupNameBeginPos, lookupNameEndPos);
+                nextTokens.leftoverExpression = expression.substring(lookupNameEndPos+2);
+            } else {
+                nextTokens.propertyName = expression.substring(lookupNameBeginPos);
+                nextTokens.leftoverExpression = null;
+            }
+        } else if (expression.startsWith(".")) {
+            int lookupNameBeginPos = ".".length();
+            int lookupNameEndPos = lookupNameBeginPos;
+            int dotlookupNameEndPos = expression.indexOf(".", lookupNameBeginPos);
+            int squareBracketlookupNameEndPos = expression.indexOf("[", lookupNameBeginPos);
+            if (dotlookupNameEndPos > lookupNameBeginPos && squareBracketlookupNameEndPos > lookupNameBeginPos) {
+                lookupNameEndPos = Math.min(dotlookupNameEndPos, squareBracketlookupNameEndPos);
+            } else if (dotlookupNameEndPos > lookupNameBeginPos) {
+                lookupNameEndPos = dotlookupNameEndPos;
+            } else if (squareBracketlookupNameEndPos > lookupNameBeginPos) {
+                lookupNameEndPos = squareBracketlookupNameEndPos;

Review comment:
       the two else if seem's unecessary if we do:
   `dotlookupNameEndPos > lookupNameBeginPos || squareBracketlookupNameEndPos > lookupNameBeginPos`
   in the first condition




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org