imrichardwu commented on code in PR #61878:
URL: https://github.com/apache/airflow/pull/61878#discussion_r2810311588
##########
airflow-core/src/airflow/serialization/helpers.py:
##########
@@ -31,6 +31,94 @@
from airflow.timetables.base import Timetable as CoreTimetable
+def _truncate_rendered_value(rendered: str, max_length: int) -> str:
+ if max_length <= 0:
+ return ""
+
+ prefix = "Truncated. You can change this behaviour in
[core]max_templated_field_length. "
+ suffix = "..."
+ value = str(rendered)
+
+ # Always prioritize showing the truncation message first
+ trunc_only = f"{prefix}{suffix}"
+ trunc_only_len = len(trunc_only)
+
+ # If max_length is too small to even show the message, return it anyway
+ # (message takes priority over the constraint)
+ if max_length < trunc_only_len:
+ return trunc_only
+
+ # Check if value already has outer quotes - if so, preserve them and don't
add extra quotes
+ has_outer_quotes = (value.startswith('"') and value.endswith('"')) or (
+ value.startswith("'") and value.endswith("'")
+ )
+
+ if has_outer_quotes:
+ # Value already has quotes - preserve the opening quote, truncate
inner content
+ quote_char = value[0]
+ inner_value = value[1:-1] # Strip outer quotes
+ # Calculate overhead: prefix + opening quote + suffix (no closing
quote)
+ overhead = len(prefix) + 1 + len(suffix)
+ available = max_length - overhead
+
+ MIN_CONTENT_LENGTH = 7
+ if available < MIN_CONTENT_LENGTH:
+ return trunc_only
+
+ # Get content and trim trailing spaces
+ content = inner_value[:available].rstrip()
+
+ # Build result with opening quote, content, and suffix (no closing
quote)
+ result = f"{prefix}{quote_char}{content}{suffix}"
+
+ # Ensure result < max_length, with a buffer when possible
+ # For values with outer quotes, use a larger buffer (3) since we don't
add closing quotes
+ target_length = max_length - 3
+ while len(result) > target_length and len(content) > 0:
+ content = content[:-1].rstrip()
+ result = f"{prefix}{quote_char}{content}{suffix}"
Review Comment:
For sure
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]