amoghrajesh commented on code in PR #59882:
URL: https://github.com/apache/airflow/pull/59882#discussion_r2654855990


##########
airflow-core/src/airflow/serialization/helpers.py:
##########
@@ -29,6 +29,28 @@
     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 = "..."
+
+    if max_length <= len(suffix):
+        return suffix[:max_length]
+
+    if max_length <= len(prefix) + len(suffix):
+        return (prefix + suffix)[:max_length]
+
+    available = max_length - len(prefix) - len(suffix)
+    return f"{prefix}{rendered[:available]}{suffix}"
+
+
+
+def _safe_truncate_rendered_value(rendered: Any, max_length: int) -> str:
+    return _truncate_rendered_value(str(rendered), max_length)

Review Comment:
   This still doesn't prioritise message first. Take these test cases for 
example:
   
   ```python
   [(1, 'test', 'Minimum value'),
    (3, 'test', 'At ellipsis length'),
    (5, 'test', 'Very small'),
    (10, 'password123', 'Small'),
    (20, 'secret_value', 'Small with content'),
    (50, 'This is a test string', 'Medium'),
    (83, 'Hello World', 'At prefix+suffix boundary v1'),
    (84, 'Hello World', 'Just above boundary v1'),
    (86, 'Hello World', 'At overhead boundary v2'),
    (90, 'short', 'Normal case - short string'),
    (100, 'This is a longer string', 'Normal case'),
    (150,
     
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
     'Large max_length, long string'),
    (100, 'None', "String 'None'"),
    (100, 'True', "String 'True'"),
    (100, "{'key': 'value'}", 'Dict-like string'),
    (100, "test's", 'String with apostrophe'),
    (90, '"quoted"', 'String with quotes')]
   ```
   
   This is what your function returns: 
   ```python
   for max_length, rendered, description in test_cases:
       v1_result = truncate_v1(rendered, max_length)
       print(v1_result)
       
   .
   ...
   Trunc
   Truncated.
   Truncated. You can c
   Truncated. You can change this behaviour in [core]
   Truncated. You can change this behaviour in 
[core]max_templated_field_length. He...
   Truncated. You can change this behaviour in 
[core]max_templated_field_length. Hel...
   Truncated. You can change this behaviour in 
[core]max_templated_field_length. Hello...
   Truncated. You can change this behaviour in 
[core]max_templated_field_length. short...
   Truncated. You can change this behaviour in 
[core]max_templated_field_length. This is a longer st...
   Truncated. You can change this behaviour in 
[core]max_templated_field_length. 
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
   Truncated. You can change this behaviour in 
[core]max_templated_field_length. None...
   Truncated. You can change this behaviour in 
[core]max_templated_field_length. True...
   Truncated. You can change this behaviour in 
[core]max_templated_field_length. {'key': 'value'}...
   Truncated. You can change this behaviour in 
[core]max_templated_field_length. test's...
   Truncated. You can change this behaviour in 
[core]max_templated_field_length. "quoted"...
   ```
   
   This is what I would expect:
   ```python
   Truncated. You can change this behaviour in 
[core]max_templated_field_length. ... 
   Truncated. You can change this behaviour in 
[core]max_templated_field_length. ... 
   Truncated. You can change this behaviour in 
[core]max_templated_field_length. ... 
   Truncated. You can change this behaviour in 
[core]max_templated_field_length. ... 
   Truncated. You can change this behaviour in 
[core]max_templated_field_length. ... 
   Truncated. You can change this behaviour in 
[core]max_templated_field_length. ... 
   Truncated. You can change this behaviour in 
[core]max_templated_field_length. ... 
   Truncated. You can change this behaviour in 
[core]max_templated_field_length. ... 
   Truncated. You can change this behaviour in 
[core]max_templated_field_length. 'He'... 
   Truncated. You can change this behaviour in 
[core]max_templated_field_length. 'short'... 
   Truncated. You can change this behaviour in 
[core]max_templated_field_length. 'This is a longer'... 
   Truncated. You can change this behaviour in 
[core]max_templated_field_length. 
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'... 
   Truncated. You can change this behaviour in 
[core]max_templated_field_length. 'None'... 
   Truncated. You can change this behaviour in 
[core]max_templated_field_length. 'True'... 
   Truncated. You can change this behaviour in 
[core]max_templated_field_length. "{'key': 'value'}"... 
   Truncated. You can change this behaviour in 
[core]max_templated_field_length. "test's"... 
   Truncated. You can change this behaviour in 
[core]max_templated_field_length. '"quote'... 
   ```
   
   



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

Reply via email to