Skip to content

Commit 2be9012

Browse files
author
Rizal Widyarta Gowandy
authored
Merge pull request #26 from rizalgowandy/arwego/feat/fn
Add fn package
2 parents 1a6f62e + fa1a0f3 commit 2be9012

File tree

8 files changed

+152
-22
lines changed

8 files changed

+152
-22
lines changed

.github/scripts/gotool.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const util = require("util");
2+
const exec = util.promisify(require("child_process").exec);
3+
4+
async function goTool(file) {
5+
const { stdout } = await exec(
6+
`go tool cover -func ${file} | grep total | awk '{printf("%s",$3);}'`
7+
);
8+
9+
console.log(`${file} Code Coverage:`, stdout);
10+
return stdout;
11+
}
12+
13+
module.exports = async ({ file }) => {
14+
return await goTool(file);
15+
};

.github/workflows/go.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ on:
55
branches: [ main ]
66
pull_request:
77
branches: [ main ]
8+
workflow_dispatch:
89

910
jobs:
1011

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
name: Coverage
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
workflow_dispatch:
9+
10+
jobs:
11+
12+
test:
13+
name: Test
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: Set up Go 1.x
17+
uses: actions/setup-go@v2
18+
with:
19+
go-version: ^1.13
20+
21+
- name: Check out code into the Go module directory
22+
uses: actions/checkout@v2
23+
24+
- name: Get dependencies
25+
run: go mod vendor -v
26+
27+
- name: Test
28+
run: |
29+
go test ./... -cover -count=1 -race -coverprofile ../main.out
30+
31+
- name: Set main branch code coverage
32+
id: codecov-main-branch
33+
uses: actions/github-script@v3
34+
with:
35+
github-token: ${{secrets.GITHUB_TOKEN}}
36+
result-encoding: string
37+
script: |
38+
const goTool = require(`${process.env.GITHUB_WORKSPACE}/.github/scripts/gotool.js`)
39+
return await goTool({file: "../main.out"})
40+
41+
- name: Checkout PR branch
42+
uses: actions/checkout@v2
43+
44+
- name: Test PR branch
45+
run: |
46+
go test ./... -cover -count=1 -race -coverprofile ../pr.out
47+
48+
- name: Set PR branch code coverage
49+
id: codecov-pr-branch
50+
uses: actions/github-script@v3
51+
with:
52+
github-token: ${{secrets.GITHUB_TOKEN}}
53+
result-encoding: string
54+
script: |
55+
const goTool = require(`${process.env.GITHUB_WORKSPACE}/.github/scripts/gotool.js`)
56+
return await goTool({file: "../pr.out"})
57+
58+
- name: Create comment
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:
73+
74+
**Code Coverage**
75+
Main: ${mainCodeCov}
76+
PR : ${prCodeCov}
77+
78+
Check Detail: [Here](https://github.com/${context.issue.owner}/${context.issue.repo}/actions/runs/${runID})
79+
`
80+
81+
github.issues.createComment({
82+
issue_number: context.issue.number,
83+
owner: context.repo.owner,
84+
repo: context.repo.repo,
85+
body: body
86+
})

.golangci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ linters:
7272
- bodyclose
7373
- deadcode
7474
- depguard
75-
- dogsled
7675
- dupl
7776
- errcheck
7877
- exhaustive
@@ -109,6 +108,7 @@ linters:
109108
- whitespace
110109

111110
# don't enable:
111+
# - dogsled
112112
# - asciicheck
113113
# - gochecknoglobals
114114
# - gocognit

pkg/errorx/v2/errorx.go

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,18 @@ package errorx
33
import (
44
"errors"
55
"fmt"
6-
"runtime"
7-
"strings"
86

97
"github.com/imdario/mergo"
8+
"github.com/rizalgowandy/gdk/pkg/fn"
109
)
1110

12-
var ServiceName = "rizalgowandy"
11+
const callerSkip = 2
1312

1413
// E for creating new error.
1514
// error should always be the first param.
1615
func E(args ...interface{}) error {
1716
if len(args) == 0 {
18-
_, file, line, _ := runtime.Caller(1)
19-
file = file[strings.Index(file, ServiceName)+len(ServiceName):]
20-
return Errorf("errorx.E: bad call without args from file=%s:%d", file, line)
17+
return Errorf("errorx.E: bad call without args from file=%s", fn.Line(callerSkip))
2118
}
2219

2320
e := &Error{}
@@ -27,18 +24,17 @@ func E(args ...interface{}) error {
2724
// Copy and put the errors back.
2825
errCopy := *arg
2926
e = &errCopy
27+
e.OpTraces = append([]Op{Op(fn.Name(callerSkip))}, e.OpTraces...)
3028

3129
case error:
3230
e.Err = arg
33-
_, file, line, _ := runtime.Caller(1)
34-
file = file[strings.Index(file, ServiceName)+len(ServiceName):]
35-
e.Line = Line(fmt.Sprintf("%s:%d", file, line))
31+
e.Line = Line(fn.Line(callerSkip))
32+
e.OpTraces = append([]Op{Op(fn.Name(callerSkip))}, e.OpTraces...)
3633

3734
case string:
3835
e.Err = Errorf(arg)
39-
_, file, line, _ := runtime.Caller(1)
40-
file = file[strings.Index(file, ServiceName)+len(ServiceName):]
41-
e.Line = Line(fmt.Sprintf("%s:%d", file, line))
36+
e.Line = Line(fn.Line(callerSkip))
37+
e.OpTraces = append([]Op{Op(fn.Name(callerSkip))}, e.OpTraces...)
4238

4339
case Code:
4440
// New code will always replace the old code.
@@ -60,7 +56,8 @@ func E(args ...interface{}) error {
6056
}
6157

6258
case Op:
63-
e.OpTraces = append([]Op{arg}, e.OpTraces...)
59+
// For backward compatibility.
60+
// Client is no longer to pass Op manually as an argument but will be filled automatically.
6461

6562
case Message:
6663
e.Message = arg
@@ -70,9 +67,7 @@ func E(args ...interface{}) error {
7067

7168
default:
7269
// The default error is unknown.
73-
_, file, line, _ := runtime.Caller(1)
74-
file = file[strings.Index(file, ServiceName)+len(ServiceName):]
75-
msg := fmt.Sprintf("errorx.E: bad call from file=%s:%d args=%v", file, line, args)
70+
msg := fmt.Sprintf("errorx.E: bad call from file=%s args=%v", fn.Line(callerSkip), args)
7671
return Errorf(msg+"; unknown_type=%T value=%v", arg, arg)
7772
}
7873
}
@@ -118,7 +113,7 @@ func Match(errs1, errs2 error) bool {
118113

119114
// Is reports whether err is an *Error of the given Code.
120115
// If err is nil then Is returns false.
121-
func Is(code Code, err error) bool {
116+
func Is(err error, code Code) bool {
122117
if err == nil {
123118
return false
124119
}
@@ -133,7 +128,7 @@ func Is(code Code, err error) bool {
133128
}
134129

135130
if e.Err != nil {
136-
return Is(code, e.Err)
131+
return Is(e.Err, code)
137132
}
138133

139134
return false

pkg/errorx/v2/errorx_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ func TestIs(t *testing.T) {
228228

229229
for _, tt := range tests {
230230
t.Run(tt.name, func(t *testing.T) {
231-
got := Is(tt.args.code, tt.args.err)
231+
got := Is(tt.args.err, tt.args.code)
232232
if !reflect.DeepEqual(tt.want, got) {
233233
msg := "\nwant = %#v" + "\ngot = %#v"
234234
t.Errorf(msg, tt.want, got)

pkg/fn/fn.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package fn
2+
3+
import (
4+
"fmt"
5+
"path/filepath"
6+
"runtime"
7+
"strings"
8+
)
9+
10+
const Company = "rizalgowandy"
11+
12+
// Name return the caller function name automatically.
13+
// Format: package.struct.method
14+
func Name(skips ...int) string {
15+
skip := 1
16+
if len(skips) > 0 {
17+
skip = skips[0]
18+
}
19+
pc, _, _, _ := runtime.Caller(skip)
20+
return filepath.Base(runtime.FuncForPC(pc).Name())
21+
}
22+
23+
// Line return the caller code line automatically.
24+
// Format: /path/filename:line
25+
func Line(skips ...int) string {
26+
skip := 1
27+
if len(skips) > 0 {
28+
skip = skips[0]
29+
}
30+
_, file, line, _ := runtime.Caller(skip)
31+
file = file[strings.Index(file, Company)+len(Company):]
32+
return fmt.Sprintf("%s:%d", file, line)
33+
}

pkg/logx/metadata_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func TestErrMetadata(t *testing.T) {
4747
tags.Message: errorx.Message("qwerty"),
4848
tags.MetricStatus: errorx.MetricStatusExpectedErr,
4949
tags.Ops: []errorx.Op{
50-
errorx.Op("abc"),
50+
errorx.Op("logx.TestErrMetadata"),
5151
},
5252
tags.ErrorLine: "",
5353
},
@@ -69,7 +69,7 @@ func TestErrMetadata(t *testing.T) {
6969
}
7070
}
7171

72-
assert.Equal(t, got, tt.want)
72+
assert.Equal(t, tt.want, got)
7373
})
7474
}
7575
}

0 commit comments

Comments
 (0)