Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions core/app/service/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,11 +296,11 @@ func (u *UpgradeService) loadVersionByMode(developer, currentVersion string) (st

versionPart := strings.Split(current, ".")
if len(versionPart) < 3 {
return betaVersionLatest, current, latest
return betaVersionLatest, "", latest
}
num, _ := strconv.Atoi(versionPart[1])
if num == 0 {
return betaVersionLatest, current, latest
return betaVersionLatest, "", latest
}
if num >= 10 {
if current[:6] == currentVersion[:6] {
Expand All @@ -309,7 +309,7 @@ func (u *UpgradeService) loadVersionByMode(developer, currentVersion string) (st
return betaVersionLatest, "", latest
}
if current[:5] == currentVersion[:5] {
return betaVersionLatest, current, ""
return betaVersionLatest, "", ""
}
return betaVersionLatest, "", latest
}
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 several potential issues and improvements that might affect its functionality:

  1. Redundant Return Values: The function returns three values but only uses two of them (betaVersionLatest, current). It should return one or all three values based on how they are used in the calling context.

  2. Error Handling: There is no explicit error handling for parsing integers or dealing with non-string inputs, which could handle unexpected data gracefully.

  3. Variable Scope: Unused variables like num and developer can be removed to improve readability and maintainability.

  4. Comments: Add more comments to describe what each part of the code does, especially where conditions depend on specific substring comparisons.

  5. Efficiency: The use of repeated substring comparisons using strings.HasPrefix() can be optimized if these substrings do not change after initialization.

Here's an improved version of the code considering some of these points:

func (u *UpgradeService) loadVersionByMode(currentVersion string) (string, bool, bool) {
    versionPart := strings.Split(currentVersion, ".")

    // Determine if it matches beta patterns
    matchBetaPattern := func(prefix, suffix string) bool {
        return strings.HasPrefix(currentVersion, prefix+suffix)
    }

    switch {
    case len(versionPart) < 3:
        return betaVersionLatest, false, true
    case num > 10 && matchBetaPattern("v", "pre-release"):
        return betaVersionLatest, true, true
    case num == 10 && matchBetaPattern("v", ""):
        return betaVersionLatest, false, true
    default:
        return betaVersionLatest, matchBetaPattern("", ""), false
    }
}

Key Changes:

  • Function Signature: Changed the function signature to return (version string, matchedPreRelease bool, matchedStable bool) based on the logic.
  • Removed Redundant Returns: Only the version pattern that fits best for the given condition is returned.
  • Improved Comments: Added explanation for why certain blocks are executed.
  • Logical Simplification: Combined related checks into one line using a helper function matchBetaPattern for clearer logic.

This approach reduces redundancy and improves clarity while maintaining the necessary functionality.

Expand Down
Loading