Skip to content

Commit

Permalink
optimize: bscp服务变量排序优化,变量名按ascii码排序,变量值为空的放在最前面 (#2676)
Browse files Browse the repository at this point in the history
  • Loading branch information
fireyun authored Oct 11, 2023
1 parent dfb38a2 commit d30829b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package service
import (
"bytes"
"context"
"sort"
"time"

"bscp.io/pkg/dal/table"
Expand Down Expand Up @@ -245,6 +246,9 @@ func (s *Service) getVariableReferences(kt *kit.Kit, tmplRevisions []*table.Temp
}

allVariables = tools.RemoveDuplicateStrings(allVariables)
// Sort in ascending order
sort.Strings(allVariables)

refs := make([]*pbatv.AppTemplateVariableReference, len(allVariables))
for idx, v := range allVariables {
ref := &pbatv.AppTemplateVariableReference{
Expand Down Expand Up @@ -334,12 +338,32 @@ func (s *Service) ListAppTmplVariables(ctx context.Context, req *pbds.ListAppTmp
// for unset variable, just return its name, other fields keep empty
finalVar = append(finalVar, &pbtv.TemplateVariableSpec{Name: name})
}
finalVar = sortVariables(finalVar)

return &pbds.ListAppTmplVariablesResp{
Details: finalVar,
}, nil
}

func sortVariables(vars []*pbtv.TemplateVariableSpec) []*pbtv.TemplateVariableSpec {
// Define a custom sorting function that sorts by the name field in ascending order.
sortByName := func(i, j int) bool {
return vars[i].Name < vars[j].Name
}
sort.Slice(vars, sortByName)

// put the variables whose value is empty in front
var varsNoVal, varWithVal []*pbtv.TemplateVariableSpec
for _, v := range vars {
if v.DefaultVal == "" {
varsNoVal = append(varsNoVal, v)
} else {
varWithVal = append(varWithVal, v)
}
}
return append(varsNoVal, varWithVal...)
}

// ListReleasedAppTmplVariables get app template variable references.
func (s *Service) ListReleasedAppTmplVariables(ctx context.Context, req *pbds.ListReleasedAppTmplVariablesReq) (
*pbds.ListReleasedAppTmplVariablesResp, error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
package dao

import (
"sort"

"bscp.io/pkg/dal/gen"
"bscp.io/pkg/dal/table"
"bscp.io/pkg/kit"
Expand Down Expand Up @@ -74,5 +76,13 @@ func (dao *releasedAppTemplateVariableDao) ListVariables(kit *kit.Kit, bizID, ap
if len(appVars) == 0 {
return []*table.TemplateVariableSpec{}, nil
}
return appVars[0].Spec.Variables, nil
vars := appVars[0].Spec.Variables

// Define a custom sorting function that sorts by the name field in ascending order.
sortByName := func(i, j int) bool {
return vars[i].Name < vars[j].Name
}
sort.Slice(vars, sortByName)

return vars, nil
}
4 changes: 4 additions & 0 deletions bcs-services/bcs-bscp/pkg/tmplprocess/tmplprocessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package tmplprocess
import (
"fmt"
"regexp"
"sort"
"strings"
)

Expand Down Expand Up @@ -53,6 +54,9 @@ func (p *processor) ExtractVariables(template []byte) []string {
varNames = append(varNames, name)
}

// Sort in ascending order
sort.Strings(varNames)

return varNames
}

Expand Down

0 comments on commit d30829b

Please sign in to comment.