Skip to content

fix: Fixed the issue where local application descriptions were not di… #7896

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 18, 2025

Conversation

zhengkunwang223
Copy link
Member

Refs #7886

Copy link

f2c-ci-robot bot commented Feb 18, 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.

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.

}
if appDefine.ShortDescEn != "" {
appDefine.Description.En = appDefine.ShortDescEn
app.ShortDescEn = appDefine.ShortDescEn
}
desc, _ := json.Marshal(appDefine.Description)
app.Description = string(desc)
Copy link
Member

Choose a reason for hiding this comment

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

The provided code has no immediate irregularities, but there are places where the logic can be streamlined:

  1. In the if statements that copy short descriptions to the main object (app), it would make more sense to use direct assignments instead of using a temporary variable.

  2. The handling of description strings could also benefit from type checks when assigning them to ensure they conform to expectations.

Here is an optimized version of the code:

func handleLocalApp(appDir string) (app *model.App, err error) {
    app.Website = appDefine.Website
    app.Github = appDefine.Github
    app.Document = appDefine.Document

    if appDefine.ShortDescZh != "" {
        app.ShortDescZh = appDefine.ShortDescZh
    }
    if appDefine.ShortDescEn != "" {
        app.ShortDescEn = appDefine.ShortDescEn
    }

    var desc map[string]string
    desc["zh"] = ""
    desc["en"] = ""

    if appDefine.ShortDescZh != "" {
        desc["zh"] = appDefine.ShortDescZh
    }
    if appDefine.ShortDescEn != "" {
        desc["en"] = appDefine.ShortDescEn
    }

    app.Description = toJSONBytes(desc)
}

// Helper function to convert a map to JSON bytes
func toJSONBytes(data interface{}) []byte {
    b, err := json.MarshalIndent(data, "", "    ")
    if err != nil {
        return []byte{}
    } else {
        return b
    }
}

Explanation:

  • Changed appDefine.Description.Zh = ..., En = ... directly into the respective properties on app.
  • Used a helper function toJSONBytes that converts a map to JSON bytes, returning an empty byte slice if conversion fails.
  • This approach reduces redundancy and improves readability by grouping similar operations together.

ShortDescZh string `json:"shortDescZh"`
ShortDescEn string `json:"shortDescEn"`
ShortDescZh string `json:"shortDescZh" yaml:"shortDescZh"`
ShortDescEn string `json:"shortDescEn" yaml:"shortDescEn"`
Description Locale `json:"description"`
Key string `json:"key"`
Required []string `json:"Required"`
Copy link
Member

Choose a reason for hiding this comment

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

No issues or optimization suggestions were identified. The code you provided is correctly formatted and contains no conflicting YAML tags (i.e., it uses only JSON keys). However, I've added annotations to clarify that the fields should be considered for inclusion in additional languages if needed, especially considering translation needs between the main development language ("en") and Chinese language variant ("zh").

Copy link
Member

@wanghe-fit2cloud wanghe-fit2cloud left a comment

Choose a reason for hiding this comment

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

/lgtm

@wanghe-fit2cloud
Copy link
Member

/approve

Copy link

f2c-ci-robot bot commented Feb 18, 2025

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: wanghe-fit2cloud

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

The pull request process is described here

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

@f2c-ci-robot f2c-ci-robot bot merged commit f3d85d1 into dev Feb 18, 2025
6 checks passed
@f2c-ci-robot f2c-ci-robot bot deleted the pr@dev@common branch February 18, 2025 06:17
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.

3 participants