Skip to content

Commit 1a5316a

Browse files
authored
Merge pull request #32 from rizalgowandy/arwego/feat/sortx
Add Sortx Package
2 parents d02941f + 4cabbb7 commit 1a5316a

File tree

7 files changed

+137
-73
lines changed

7 files changed

+137
-73
lines changed

.github/configs/release-drafter.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ version-resolver:
3030
patch:
3131
labels:
3232
- 'patch'
33-
default: patch
33+
default: minor
3434
template: |
3535
## Changes
3636

.github/workflows/go.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
- name: Set up Go 1.x
1717
uses: actions/setup-go@v2
1818
with:
19-
go-version: ^1.13
19+
go-version: 1.15
2020

2121
- name: Check out code into the Go module directory
2222
uses: actions/checkout@v2

.github/workflows/test-coverage.yml

Lines changed: 10 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -7,80 +7,19 @@ on:
77
workflow_dispatch:
88

99
jobs:
10-
1110
test:
12-
name: Test
1311
runs-on: ubuntu-latest
1412
steps:
15-
- name: Set up Go 1.x
16-
uses: actions/setup-go@v2
17-
with:
18-
go-version: ^1.13
19-
20-
- name: Check out code into the Go module directory
21-
uses: actions/checkout@v2
22-
23-
- name: Get dependencies
24-
run: go mod vendor -v
25-
26-
- name: Test
27-
run: |
28-
go test ./... -cover -count=1 -race -coverprofile ../main.out
29-
30-
- name: Set main branch code coverage
31-
id: codecov-main-branch
32-
uses: actions/github-script@v3
33-
with:
34-
github-token: ${{secrets.GITHUB_TOKEN}}
35-
result-encoding: string
36-
script: |
37-
const goTool = require(`${process.env.GITHUB_WORKSPACE}/.github/scripts/gotool.js`)
38-
return await goTool({file: "../main.out"})
39-
40-
- name: Checkout PR branch
41-
uses: actions/checkout@v2
42-
43-
- name: Test PR branch
44-
run: |
45-
go test ./... -cover -count=1 -race -coverprofile ../pr.out
46-
47-
- name: Set PR branch code coverage
48-
id: codecov-pr-branch
49-
uses: actions/github-script@v3
50-
with:
51-
github-token: ${{secrets.GITHUB_TOKEN}}
52-
result-encoding: string
53-
script: |
54-
const goTool = require(`${process.env.GITHUB_WORKSPACE}/.github/scripts/gotool.js`)
55-
return await goTool({file: "../pr.out"})
56-
57-
- name: Create comment
58-
if: github.event_name == 'pull_request'
59-
uses: actions/github-script@v3
60-
env:
61-
main_code_cov: ${{steps.codecov-main-branch.outputs.result}}
62-
pr_code_cov: ${{steps.codecov-pr-branch.outputs.result}}
63-
with:
64-
github-token: ${{secrets.GITHUB_TOKEN}}
65-
script: |
66-
const runID = ${{github.run_id}}
67-
mainCodeCov = process.env.main_code_cov.replace(/[\n\t\r]/g,"")
68-
prCodeCov = process.env.pr_code_cov.replace(/[\n\t\r]/g,"")
69-
70-
const body = `
71-
### Test Coverage Result
72-
Success! :confetti_ball::confetti_ball::confetti_ball:
13+
- name: Checkout
14+
uses: actions/checkout@v2
7315

74-
**Code Coverage**
75-
Main: ${mainCodeCov}
76-
PR : ${prCodeCov}
16+
- name: Set up Go 1.x
17+
uses: actions/setup-go@v2
18+
with:
19+
go-version: 1.15
7720

78-
Check Detail: [Here](https://github.com/${context.issue.owner}/${context.issue.repo}/actions/runs/${runID})
79-
`
21+
- name: Run tests with coverage report output
22+
run: go test ./... -coverprofile=docs/coverage.out
8023

81-
github.issues.createComment({
82-
issue_number: context.issue.number,
83-
owner: context.repo.owner,
84-
repo: context.repo.repo,
85-
body: body
86-
})
24+
- name: Comment
25+
uses: k1LoW/octocov-action@v0

.octocov.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
coverage:
2+
# acceptable: current >= 60% && diff >= 0.5%
3+
badge:
4+
path: docs/coverage.svg
5+
paths:
6+
- docs/coverage.out
7+
codeToTestRatio:
8+
badge:
9+
path: docs/ratio.svg
10+
code:
11+
- '**/*.go'
12+
- '!**/*_test.go'
13+
test:
14+
- '**/*_test.go'
15+
testExecutionTime:
16+
# acceptable: 1min30sec
17+
badge:
18+
path: docs/time.svg
19+
diff:
20+
datastores:
21+
- artifact://rizalgowandy/gdk
22+
push:
23+
if: is_default_branch
24+
comment:
25+
if: is_pull_request
26+
deletePrevious: true
27+
hideFooterLink: true
28+
report:
29+
if: is_default_branch
30+
datastores:
31+
- artifact://rizalgowandy/gdk

loader.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
_ "github.com/rizalgowandy/gdk/pkg/queue/nsqx"
2323
_ "github.com/rizalgowandy/gdk/pkg/queue/nsqx/interceptor"
2424
_ "github.com/rizalgowandy/gdk/pkg/regex"
25+
_ "github.com/rizalgowandy/gdk/pkg/sortx"
2526
_ "github.com/rizalgowandy/gdk/pkg/stack"
2627
_ "github.com/rizalgowandy/gdk/pkg/storage/cache"
2728
_ "github.com/rizalgowandy/gdk/pkg/storage/database"

pkg/sortx/sortx.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package sortx
2+
3+
import "strings"
4+
5+
type (
6+
Key string
7+
Order int64
8+
)
9+
10+
const (
11+
SortOrderAscending Order = iota + 1
12+
SortOrderDescending
13+
)
14+
15+
type Sort struct {
16+
Key Key
17+
Order Order
18+
}
19+
20+
// NewSorts create sorting based on
21+
// Format:
22+
// sort=key1:asc,key2:desc,key3:asc
23+
func NewSorts(qs string) []Sort {
24+
sorts := strings.Split(qs, ",")
25+
26+
var res []Sort
27+
for _, v := range sorts {
28+
kv := strings.Split(v, ":")
29+
30+
s := Sort{
31+
Key: Key(kv[0]),
32+
Order: SortOrderAscending,
33+
}
34+
if len(kv) == 2 {
35+
switch kv[1] {
36+
case "asc":
37+
s.Order = SortOrderAscending
38+
case "desc":
39+
s.Order = SortOrderDescending
40+
default:
41+
s.Order = SortOrderAscending
42+
}
43+
}
44+
res = append(res, s)
45+
}
46+
47+
return res
48+
}

pkg/sortx/sortx_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package sortx
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func TestNewSorts(t *testing.T) {
9+
type args struct {
10+
qs string
11+
}
12+
tests := []struct {
13+
name string
14+
args args
15+
want []Sort
16+
}{
17+
{
18+
name: "Success",
19+
args: args{
20+
qs: "id:desc,status:asc,created_at",
21+
},
22+
want: []Sort{
23+
{
24+
Key: "id",
25+
Order: SortOrderDescending,
26+
},
27+
{
28+
Key: "status",
29+
Order: SortOrderAscending,
30+
},
31+
{
32+
Key: "created_at",
33+
Order: SortOrderAscending,
34+
},
35+
},
36+
},
37+
}
38+
for _, tt := range tests {
39+
t.Run(tt.name, func(t *testing.T) {
40+
if got := NewSorts(tt.args.qs); !reflect.DeepEqual(got, tt.want) {
41+
t.Errorf("NewSorts() = %v, want %v", got, tt.want)
42+
}
43+
})
44+
}
45+
}

0 commit comments

Comments
 (0)