Skip to content

Commit

Permalink
feat: sort Service Versions (#355)
Browse files Browse the repository at this point in the history
Fixes #348
  • Loading branch information
mmorel-35 authored Apr 27, 2021
1 parent 7c3660c commit 64a98cc
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
11 changes: 11 additions & 0 deletions file/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,17 @@ type FServicePackage struct {
Versions []FServiceVersion `json:"versions,omitempty" yaml:"versions,omitempty"`
}

// sortKey is used for sorting.
func (s FServiceVersion) sortKey() string {
if s.Version != nil {
return *s.Version
}
if s.ID != nil {
return *s.ID
}
return ""
}

// sortKey is used for sorting.
func (s FServicePackage) sortKey() string {
if s.Name != nil {
Expand Down
17 changes: 17 additions & 0 deletions file/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,23 @@ func Test_sortKey(t *testing.T) {
sortable: FServicePackage{},
expectedKey: "",
},
{
sortable: &FServiceVersion{
Version: kong.String("my-service-version"),
ID: kong.String("my-id"),
},
expectedKey: "my-service-version",
},
{
sortable: &FServiceVersion{
ID: kong.String("my-id"),
},
expectedKey: "my-id",
},
{
sortable: FServiceVersion{},
expectedKey: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
3 changes: 3 additions & 0 deletions file/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ func populateServicePackages(kongState *state.KongState, file *Content,
utils.ZeroOutID(&fVersion, fVersion.Version, config.WithID)
p.Versions = append(p.Versions, fVersion)
}
sort.SliceStable(p.Versions, func(i, j int) bool {
return compareOrder(p.Versions[i], p.Versions[j])
})
utils.ZeroOutID(&p, p.Name, config.WithID)
file.ServicePackages = append(file.ServicePackages, p)
}
Expand Down
11 changes: 11 additions & 0 deletions file/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,17 @@ func Test_compareOrder(t *testing.T) {
},
expected: true,
},
{
sortable1: &FServiceVersion{
Version: kong.String("my-service-version-1"),
ID: kong.String("my-id-1"),
},
sortable2: &FServiceVersion{
Version: kong.String("my-service-version-2"),
ID: kong.String("my-id-2"),
},
expected: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 64a98cc

Please sign in to comment.