feat: support asc/dec sorting

Closes coop-cloud/tagcmp#1.
This commit is contained in:
decentral1se 2021-09-06 12:20:06 +02:00
parent 4ef558bb7a
commit 2a8edd82d7
No known key found for this signature in database
GPG Key ID: 5E2EF5A63E3718CC
4 changed files with 100 additions and 24 deletions

View File

@ -57,7 +57,7 @@ func main() {
} }
} }
sort.Sort(tagcmp.ByTag(compatible)) sort.Sort(tagcmp.ByTagAsc(compatible))
fmt.Println(compatible) fmt.Println(compatible)
} }

View File

@ -12,10 +12,14 @@ Package tagcmp provides image tag comparison operations\.
- [Variables](<#variables>) - [Variables](<#variables>)
- [func IsParsable(tag string) bool](<#func-isparsable>) - [func IsParsable(tag string) bool](<#func-isparsable>)
- [type ByTag](<#type-bytag>) - [type ByTagAsc](<#type-bytagasc>)
- [func (t ByTag) Len() int](<#func-bytag-len>) - [func (t ByTagAsc) Len() int](<#func-bytagasc-len>)
- [func (t ByTag) Less(i, j int) bool](<#func-bytag-less>) - [func (t ByTagAsc) Less(i, j int) bool](<#func-bytagasc-less>)
- [func (t ByTag) Swap(i, j int)](<#func-bytag-swap>) - [func (t ByTagAsc) Swap(i, j int)](<#func-bytagasc-swap>)
- [type ByTagDesc](<#type-bytagdesc>)
- [func (t ByTagDesc) Len() int](<#func-bytagdesc-len>)
- [func (t ByTagDesc) Less(i, j int) bool](<#func-bytagdesc-less>)
- [func (t ByTagDesc) Swap(i, j int)](<#func-bytagdesc-swap>)
- [type Tag](<#type-tag>) - [type Tag](<#type-tag>)
- [func Parse(tag string) (Tag, error)](<#func-parse>) - [func Parse(tag string) (Tag, error)](<#func-parse>)
- [func (t Tag) Equals(tag Tag) bool](<#func-tag-equals>) - [func (t Tag) Equals(tag Tag) bool](<#func-tag-equals>)
@ -65,30 +69,56 @@ func IsParsable(tag string) bool
IsParsable determines if a tag is supported by this library IsParsable determines if a tag is supported by this library
## type ByTag ## type ByTagAsc
ByTag sorts tags in asc/desc order where the last element is the latest tag\. ByTagAsc sorts tags in ascending order where the last element is the latest tag\.
```go ```go
type ByTag []Tag type ByTagAsc []Tag
``` ```
### func \(ByTag\) Len ### func \(ByTagAsc\) Len
```go ```go
func (t ByTag) Len() int func (t ByTagAsc) Len() int
``` ```
### func \(ByTag\) Less ### func \(ByTagAsc\) Less
```go ```go
func (t ByTag) Less(i, j int) bool func (t ByTagAsc) Less(i, j int) bool
``` ```
### func \(ByTag\) Swap ### func \(ByTagAsc\) Swap
```go ```go
func (t ByTag) Swap(i, j int) func (t ByTagAsc) Swap(i, j int)
```
## type ByTagDesc
ByTagDesc sorts tags in descending order where the first element is the latest tag\.
```go
type ByTagDesc []Tag
```
### func \(ByTagDesc\) Len
```go
func (t ByTagDesc) Len() int
```
### func \(ByTagDesc\) Less
```go
func (t ByTagDesc) Less(i, j int) bool
```
### func \(ByTagDesc\) Swap
```go
func (t ByTagDesc) Swap(i, j int)
``` ```
## type Tag ## type Tag

View File

@ -18,15 +18,24 @@ type Tag struct {
UsesV bool // whether or not the tag uses the "v" prefix UsesV bool // whether or not the tag uses the "v" prefix
} }
// ByTag sorts tags in asc/desc order where the last element is the latest tag. // ByTagAsc sorts tags in ascending order where the last element is the latest tag.
type ByTag []Tag type ByTagAsc []Tag
func (t ByTag) Len() int { return len(t) } func (t ByTagAsc) Len() int { return len(t) }
func (t ByTag) Swap(i, j int) { t[i], t[j] = t[j], t[i] } func (t ByTagAsc) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
func (t ByTag) Less(i, j int) bool { func (t ByTagAsc) Less(i, j int) bool {
return t[i].IsLessThan(t[j]) return t[i].IsLessThan(t[j])
} }
// ByTagDesc sorts tags in descending order where the first element is the latest tag.
type ByTagDesc []Tag
func (t ByTagDesc) Len() int { return len(t) }
func (t ByTagDesc) Swap(i, j int) { t[j], t[i] = t[i], t[j] }
func (t ByTagDesc) Less(i, j int) bool {
return t[j].IsLessThan(t[i])
}
// IsGreaterThan tests if a tag is greater than another. There are some // IsGreaterThan tests if a tag is greater than another. There are some
// tag-isms to take into account here, shorter is bigger (i.e. 2.1 > 2.1.1 == // tag-isms to take into account here, shorter is bigger (i.e. 2.1 > 2.1.1 ==
// true, 2 > 2.1 == true). // true, 2 > 2.1 == true).

View File

@ -617,7 +617,7 @@ func TestIsCompatible(t *testing.T) {
} }
} }
func TestSort1(t *testing.T) { func TestSortAsc1(t *testing.T) {
rawTags := []string{ rawTags := []string{
"v1.4.8", "v1.4.8",
"v1.3.9", "v1.3.9",
@ -637,7 +637,7 @@ func TestSort1(t *testing.T) {
tags = append(tags, tag) tags = append(tags, tag)
} }
sort.Sort(tagcmp.ByTag(tags)) sort.Sort(tagcmp.ByTagAsc(tags))
expected := []string{ expected := []string{
"v0.0.1", "v0.0.1",
@ -656,7 +656,7 @@ func TestSort1(t *testing.T) {
} }
} }
func TestSort2(t *testing.T) { func TestSortAsc2(t *testing.T) {
rawTags := []string{ rawTags := []string{
"10.0", "10.0",
"10.6", "10.6",
@ -675,7 +675,7 @@ func TestSort2(t *testing.T) {
tags = append(tags, tag) tags = append(tags, tag)
} }
sort.Sort(tagcmp.ByTag(tags)) sort.Sort(tagcmp.ByTagAsc(tags))
expected := []string{ expected := []string{
"5.5", "5.5",
@ -693,6 +693,43 @@ func TestSort2(t *testing.T) {
} }
} }
func TestSortDesc(t *testing.T) {
rawTags := []string{
"10.0",
"10.6",
"10.2",
"10.1",
"10.5",
"5.5",
}
var tags []tagcmp.Tag
for _, rawTag := range rawTags {
tag, err := tagcmp.Parse(rawTag)
if err != nil {
t.Errorf("'%s' should have parsed but didn't: %s", tag, err)
}
tags = append(tags, tag)
}
sort.Sort(tagcmp.ByTagDesc(tags))
expected := []string{
"10.6",
"10.5",
"10.2",
"10.1",
"10.0",
"5.5",
}
for idx, tag := range tags {
if tag.String() != expected[idx] {
t.Errorf("'%s' sorted out of order, saw '%s', expected '%s'", tag, tags, expected)
}
}
}
func TestString(t *testing.T) { func TestString(t *testing.T) {
for _, tag := range supported { for _, tag := range supported {
p, err := tagcmp.Parse(tag) p, err := tagcmp.Parse(tag)
@ -730,7 +767,7 @@ func TestGiteaFilterCompatible(t *testing.T) {
} }
} }
sort.Sort(tagcmp.ByTag(filtered)) sort.Sort(tagcmp.ByTagAsc(filtered))
for idx, tag := range filtered { for idx, tag := range filtered {
if tag.String() != expected[idx] { if tag.String() != expected[idx] {