GreatEugenius commented on code in PR #122: URL: https://github.com/apache/flink-agents/pull/122#discussion_r2300262617
########## python/flink_agents/plan/configuration.py: ########## @@ -0,0 +1,224 @@ +################################################################################ +# 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 pathlib import Path +from typing import Any, Dict, Optional + +import yaml +from pydantic import BaseModel +from pyflink.common import Configuration +from typing_extensions import override + +from flink_agents.api.configuration import ( + ConfigOption, + ReadableConfiguration, + WritableConfiguration, +) + + +def flatten_dict(d: Dict, parent_key: str = '', sep: str = '.') -> Dict[str, Any]: + """Flatten a nested dictionary into a single-level dictionary. + + This function recursively traverses the dictionary, converting multi-level + nested key-value pairs into a single-level structure, where nested levels + are represented by joining key names with the specified separator. + + Args: + d (Dict): The nested dictionary to be flattened + parent_key (str): The parent key name, used in recursion to track the + upper-level key path. Defaults to an empty string. + sep (str): The separator used to join parent and child keys. + Defaults to dot ('.'). + + Returns: + Dict[str, Any]: A flattened single-level dictionary where keys from + the original nested structure are joined with the separator + """ + items = {} + for k, v in d.items(): + new_key = f"{parent_key}{sep}{k}" if parent_key else k + if isinstance(v, dict): + items.update(flatten_dict(v, new_key, sep=sep)) + else: + items[new_key] = v + return items + +class AgentConfiguration(BaseModel, WritableConfiguration, ReadableConfiguration): + """Base class for config objects in the system. + Provides a flat dict interface to access nested config values. + """ + + conf_data: Dict[str, Any] + + def __init__(self, conf_data: Optional[Dict[str, Any]] = None) -> None: + """Initialize with optional configuration data.""" + if conf_data is None: + super().__init__(conf_data = {}) + else: + super().__init__(conf_data = conf_data) + + @override + def get_int(self, key: str, default: Optional[int]=None) -> int: + value = self.conf_data.get(key) + if value is None: + if default is None: Review Comment: You are right. I also discovered while writing tests that if the default value is set to None, an error occurs. This can be quite harsh for the user. -- 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: issues-unsubscr...@flink.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org