move ProjectSorter to package model to avoid package import cycle

This commit is contained in:
Wenkai Yin 2016-06-27 14:46:06 +08:00
parent 3305e0406d
commit 1c2babda0a
3 changed files with 21 additions and 23 deletions

View File

@ -68,7 +68,7 @@ func (s *SearchAPI) Get() {
}
}
projectSorter := &utils.ProjectSorter{Projects: projects}
projectSorter := &models.ProjectSorter{Projects: projects}
sort.Sort(projectSorter)
projectResult := []map[string]interface{}{}
for _, p := range projects {

View File

@ -37,3 +37,23 @@ type Project struct {
Role int `json:"current_user_role_id"`
RepoCount int `json:"repo_count"`
}
// ProjectSorter holds an array of projects
type ProjectSorter struct {
Projects []Project
}
// Len returns the length of array in ProjectSorter
func (ps *ProjectSorter) Len() int {
return len(ps.Projects)
}
// Less defines the comparison rules of project
func (ps *ProjectSorter) Less(i, j int) bool {
return ps.Projects[i].Name < ps.Projects[j].Name
}
// Swap swaps the position of i and j
func (ps *ProjectSorter) Swap(i, j int) {
ps.Projects[i], ps.Projects[j] = ps.Projects[j], ps.Projects[i]
}

View File

@ -18,8 +18,6 @@ package utils
import (
"net/url"
"strings"
"github.com/vmware/harbor/models"
)
// Repository holds information about repository
@ -35,26 +33,6 @@ func (r *Repository) GetProject() string {
return r.Name[0:strings.LastIndex(r.Name, "/")]
}
// ProjectSorter holds an array of projects
type ProjectSorter struct {
Projects []models.Project
}
// Len returns the length of array in ProjectSorter
func (ps *ProjectSorter) Len() int {
return len(ps.Projects)
}
// Less defines the comparison rules of project
func (ps *ProjectSorter) Less(i, j int) bool {
return ps.Projects[i].Name < ps.Projects[j].Name
}
// Swap swaps the position of i and j
func (ps *ProjectSorter) Swap(i, j int) {
ps.Projects[i], ps.Projects[j] = ps.Projects[j], ps.Projects[i]
}
// FormatEndpoint formats endpoint
func FormatEndpoint(endpoint string) string {
endpoint = strings.TrimSpace(endpoint)