-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Conversation
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) |
There was a problem hiding this comment.
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:
-
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. -
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 onapp
. - 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"` |
There was a problem hiding this comment.
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").
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/lgtm
|
/approve |
[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 |
Refs #7886