Skip to content

Commit c20ed4d

Browse files
committed
SNOW-2306184: config refactory - cleanup source ConfigValue creation
1 parent d4743c9 commit c20ed4d

File tree

3 files changed

+44
-327
lines changed

3 files changed

+44
-327
lines changed

src/snowflake/cli/api/config_ng/core.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
from dataclasses import dataclass, field
2929
from datetime import datetime
3030
from enum import Enum
31-
from typing import Any, Dict, List, Optional
31+
from typing import Any, Callable, Dict, List, Optional
3232

3333

3434
class SourcePriority(Enum):
@@ -62,6 +62,37 @@ def __repr__(self) -> str:
6262
value_display = f"{self.raw_value}{self.value}"
6363
return f"ConfigValue({self.key}={value_display}, from {self.source_name})"
6464

65+
@classmethod
66+
def from_source(
67+
cls,
68+
key: str,
69+
raw_value: str,
70+
source_name: str,
71+
priority: SourcePriority,
72+
value_parser: Optional[Callable[[str], Any]] = None,
73+
) -> ConfigValue:
74+
"""
75+
Factory method to create ConfigValue from a source handler.
76+
77+
Args:
78+
key: Configuration key
79+
raw_value: Raw string value from the source
80+
source_name: Name of the configuration source
81+
priority: Source priority level
82+
value_parser: Optional parser function; if None, raw_value is used as-is
83+
84+
Returns:
85+
ConfigValue instance with parsed value
86+
"""
87+
parsed_value = value_parser(raw_value) if value_parser else raw_value
88+
return cls(
89+
key=key,
90+
value=parsed_value,
91+
source_name=source_name,
92+
priority=priority,
93+
raw_value=raw_value,
94+
)
95+
6596

6697
class ValueSource(ABC):
6798
"""

src/snowflake/cli/api/config_ng/env_handlers.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,25 +68,24 @@ def discover(self, key: Optional[str] = None) -> Dict[str, ConfigValue]:
6868
# Discover specific key
6969
env_key = f"{self.PREFIX}{key.upper()}"
7070
if env_key in os.environ:
71-
raw = os.environ[env_key]
72-
values[key] = ConfigValue(
71+
values[key] = ConfigValue.from_source(
7372
key=key,
74-
value=self._parse_value(raw),
73+
raw_value=os.environ[env_key],
7574
source_name=self.source_name,
7675
priority=self.priority,
77-
raw_value=raw,
76+
value_parser=self._parse_value,
7877
)
7978
else:
8079
# Discover all SNOWFLAKE_* variables
8180
for env_key, env_value in os.environ.items():
8281
if env_key.startswith(self.PREFIX):
8382
config_key = env_key[len(self.PREFIX) :].lower()
84-
values[config_key] = ConfigValue(
83+
values[config_key] = ConfigValue.from_source(
8584
key=config_key,
86-
value=self._parse_value(env_value),
85+
raw_value=env_value,
8786
source_name=self.source_name,
8887
priority=self.priority,
89-
raw_value=env_value,
88+
value_parser=self._parse_value,
9089
)
9190

9291
return values
@@ -162,13 +161,12 @@ def discover(self, key: Optional[str] = None) -> Dict[str, ConfigValue]:
162161
env_key = f"{self.PREFIX}{snowsql_key.upper()}"
163162

164163
if env_key in os.environ:
165-
raw = os.environ[env_key]
166-
values[key] = ConfigValue(
164+
values[key] = ConfigValue.from_source(
167165
key=key, # Normalized SnowCLI key
168-
value=self._parse_value(raw),
166+
raw_value=os.environ[env_key],
169167
source_name=self.source_name,
170168
priority=self.priority,
171-
raw_value=raw,
169+
value_parser=self._parse_value,
172170
)
173171
else:
174172
# Discover all SNOWSQL_* variables
@@ -178,12 +176,12 @@ def discover(self, key: Optional[str] = None) -> Dict[str, ConfigValue]:
178176
# Map to SnowCLI key
179177
config_key = self.KEY_MAPPINGS.get(snowsql_key, snowsql_key)
180178

181-
values[config_key] = ConfigValue(
179+
values[config_key] = ConfigValue.from_source(
182180
key=config_key,
183-
value=self._parse_value(env_value),
181+
raw_value=env_value,
184182
source_name=self.source_name,
185183
priority=self.priority,
186-
raw_value=env_value,
184+
value_parser=self._parse_value,
187185
)
188186

189187
return values

0 commit comments

Comments
 (0)