Skip to content

fix: Fix variable assign num not working#2427

Merged
liuruibin merged 1 commit intomainfrom
pr@main@fix_variable_assign
Feb 27, 2025
Merged

fix: Fix variable assign num not working#2427
liuruibin merged 1 commit intomainfrom
pr@main@fix_variable_assign

Conversation

@shaohuzhang1
Copy link
Copy Markdown
Contributor

fix: Fix variable assign num not working --bug=1052497 --user=刘瑞斌 【变量赋值】-将全局变量赋值为num类型,输出时报错 https://www.tapd.cn/57709429/s/1659733

--bug=1052497 --user=刘瑞斌 【变量赋值】-将全局变量赋值为num类型,输出时报错 https://www.tapd.cn/57709429/s/1659733
@f2c-ci-robot
Copy link
Copy Markdown

f2c-ci-robot bot commented Feb 27, 2025

Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our release note process to remove it.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@f2c-ci-robot
Copy link
Copy Markdown

f2c-ci-robot bot commented Feb 27, 2025

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

result['output_value'] = val
else:
reference = self.get_reference_content(variable['reference'])
self.workflow_manage.context[variable['fields'][1]] = reference
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The provided code snippet appears to be a function within a class called execute. It's designed to handle different types of variables during execution. Here are a few comments on the code:

  1. Empty Default Case: The commented-out else block after checking if the type is 'string' suggests that there might have been a default case intended, but it's currently not used. Consider removing this commented-out block or making it functional.

  2. Type Check for 'string' Only: Since the condition checks only for 'str', all other types (like dict or any custom object) will fall into the default if block or remain unchanged by the current logic. This can lead to potential issues when trying to process complex variable values.

  3. Variable Value Assignment: Assigning both result['output_value'] and variable['value'] inside the same condition might not be necessary unless specifically required in the context of some operation you're missing from the comment above.

  4. Optimization Suggestions:

    • Avoid Repeated Context Updates: Ensure that updating the workflow context is done efficiently without redundant operations.
    • Validate Variable Type Before Parsing: Although your parsing based on type seems correct now, consider adding additional validation before attempting to convert strings to JSON, dictionaries, or other objects.

Here’s an improved version with these considerations taken into account:

def execute(self, variable_list, stream, **kwargs):
    result = {'output_value': ''}
    for variable in variable_list:
        if variable['type'].lower() == 'json':
            try:
                val = json.loads(variable['value'])
            except (JSONDecodeError, TypeError):
                print(f"Failed to decode JSON: {variable['value']}")
                continue
        elif variable['type'].upper() == 'STRING':
            # Parse string variables using self.workflow_manage.generate_prompt
            val = self.workflow_manage.generate_prompt(variable['value'])
        # Assuming no conversion needed for scalar primitives (int, float)
        else:
            val = variable['value']

        self.workflow_manage.context[variable['fields'][1]] = val

        # Optionally, update output value
        result['output_value'] = val

    return NodeResult(result=result, status_code=0)  # Add appropriate status codes

This revision includes type-checking explicitly for "json" and "STRING", uses exceptions to manage JSONDecodeError/TypeError cases gracefully, ensures consistent variable handling, and improves code readability by reducing duplicate assignments and improving conditional structure.

@liuruibin liuruibin merged commit c433c03 into main Feb 27, 2025
3 of 4 checks passed
@liuruibin liuruibin deleted the pr@main@fix_variable_assign branch February 27, 2025 04:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants