Skip to content

[Opt]Support passing parameters by overriding default values through environment variables#702

Open
Potterluo wants to merge 2 commits intoModelEngine-Group:developfrom
Potterluo:bugfix_test_config
Open

[Opt]Support passing parameters by overriding default values through environment variables#702
Potterluo wants to merge 2 commits intoModelEngine-Group:developfrom
Potterluo:bugfix_test_config

Conversation

@Potterluo
Copy link
Contributor

Purpose

This Pull Request introduces dynamic environment variable substitution and automatic type inference to the configuration utility. This is specifically designed to facilitate seamless configuration overrides in CI/CD pipelines and containerized environments.

Modifications

Updated ConfigUtils to support the ${VAR_NAME:-default_value} syntax. This allows the application to prioritize system environment variables while falling back to YAML defaults if the variables are unset.

Key Changes

  1. Environment Variable Substitution
    Regex Pattern: Implemented ${(\w+)(?::-([^}]*))?} to capture variable names and optional default values.
    Special Character Support: The parser is designed to handle hyphens and special symbols within default values (e.g., postgresql-primary.svc.cluster.local). It splits only at the first :- sequence.
    Recursive Processing: The utility now performs a deep-traversal of the YAML dictionary/list structure to ensure all nested strings are processed.

  2. Automatic Type Conversion
    To maintain data integrity, the utility now automatically casts strings to their native Python types:
    Boolean: Converts "true" to True and "false"to False.
    Numeric: Automatically detects and converts strings to int or float.

Test

image

@@ -1,86 +1,195 @@
import logging
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import logger from ucm module maybe

logger = logging.getLogger(__name__)


def _parse_string_type(value: Any) -> Any:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try to use ast.literal_eval instead of building another wheel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants