You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@superset.apache.org by GitBox <gi...@apache.org> on 2021/07/01 09:23:34 UTC

[GitHub] [superset] zhaoyongjie commented on a change in pull request #15447: feat(config): load config with hybrid strategies

zhaoyongjie commented on a change in pull request #15447:
URL: https://github.com/apache/superset/pull/15447#discussion_r662129453



##########
File path: superset/initialization/configurations/meta_configurations.py
##########
@@ -0,0 +1,86 @@
+# 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.
+from __future__ import annotations
+
+from typing import Any, Dict, Type, Union
+
+from .utils import merge_dicts_recursive, take_conf_keys_and_convert_to_dict
+
+ConfigurationValue = Union[Type[Any], Dict[Any, Any]]
+
+# pylint: disable=C0103, W0613
+
+
+def union_values(left_value: Any, right_value: Any, conf_name: str) -> Any:
+    strategy = meta_union_strategy.get(
+        (type(left_value), type(right_value)), take_right_strategy
+    )
+    return strategy(left_value, right_value, conf_name)
+
+
+def override_right_as_class_strategy(
+    left_value: ConfigurationValue, right_value: ConfigurationValue, conf_name: str
+) -> Type[Any]:
+    if isinstance(right_value, dict):
+        return type(conf_name, (), right_value)
+    return right_value
+
+
+def merge_as_class_strategy(
+    left_value: ConfigurationValue, right_value: ConfigurationValue, conf_name: str
+) -> Type[Any]:
+    left_dict = take_conf_keys_and_convert_to_dict(left_value)
+    right_dict = (
+        right_value if isinstance(right_value, dict) else dict(right_value.__dict__)
+    )
+    base = left_value if isinstance(left_value, type) else object
+    merged = {}
+    merged.update(left_dict)
+    merged.update(right_dict)
+    return type(conf_name, (base,), merged)
+
+
+def override_right_as_dict_strategy(  # pylint: disable=C0103, W0613
+    left_value: ConfigurationValue, right_value: ConfigurationValue, conf_name: str
+) -> Dict[Any, Any]:
+    return take_conf_keys_and_convert_to_dict(right_value)
+
+
+def merge_as_dict_strategy(  # pylint: disable=W0613
+    left_value: ConfigurationValue, right_value: ConfigurationValue, conf_name: str
+) -> Dict[Any, Any]:
+    left_dict = take_conf_keys_and_convert_to_dict(left_value)
+    right_dict = take_conf_keys_and_convert_to_dict(right_value)
+    try:
+        return merge_dicts_recursive(left_dict, right_dict)

Review comment:
       how about this?
   
   ```suggestion
           return dict(**left_dict, **right_dict)
   ```




-- 
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.

To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org

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



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org