Skip to content

Commit 109b011

Browse files
committed
Fix float parse with extra chars
1 parent f6f6a7f commit 109b011

File tree

3 files changed

+27
-23
lines changed

3 files changed

+27
-23
lines changed

internal/github/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"net/http"
99
"os"
1010

11+
"github.com/dropseed/commitstat/internal/parse"
1112
commitstatStatus "github.com/dropseed/commitstat/internal/status"
1213
)
1314

@@ -42,7 +43,7 @@ func FetchRefStat(repo, ref, statName, apiToken string) (string, error) {
4243

4344
for _, status := range refStatus.Statuses {
4445
if status.Context == context {
45-
return commitstatStatus.ParseStatFromDescription(status.Description), nil
46+
return parse.ParseStatFromDescription(status.Description), nil
4647
}
4748
}
4849

internal/parse/main.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66
"regexp"
7+
"strconv"
78
)
89

910
func ParseStatFromString(s, regex string) (string, error) {
@@ -18,3 +19,23 @@ func ParseStatFromString(s, regex string) (string, error) {
1819

1920
return matches[1], nil
2021
}
22+
23+
func ParseStatNumber(s string) (float64, error) {
24+
re := regexp.MustCompile(`^[\d\.]+`)
25+
matches := re.FindAllString(s, 1)
26+
if num, err := strconv.ParseFloat(matches[0], 32); err == nil {
27+
return num, nil
28+
} else {
29+
return 0.0, err
30+
}
31+
}
32+
33+
// ParseStatFromDescription expects the stat to be the first part of the string and could have a %, mb, or something directly after it
34+
func ParseStatFromDescription(text string) string {
35+
re := regexp.MustCompile(`^[\d\.]+\S*`)
36+
matches := re.FindAllString(text, 1)
37+
if len(matches) == 1 {
38+
return matches[0]
39+
}
40+
return ""
41+
}

internal/status/main.go

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package status
33
import (
44
"errors"
55
"fmt"
6-
"regexp"
7-
"strconv"
6+
7+
"github.com/dropseed/commitstat/internal/parse"
88
)
99

1010
type state string
@@ -44,12 +44,12 @@ func compareStats(stat, comparisonStat, goal string) (state, string, error) {
4444
return stateSuccess, fmt.Sprintf("%s is the first value seen", stat), nil
4545
}
4646

47-
statNum, err := parseStatNumber(stat)
47+
statNum, err := parse.ParseStatNumber(stat)
4848
if err != nil {
4949
return stateError, "unable to parse stat", err
5050
}
5151

52-
prevStatNum, err := parseStatNumber(comparisonStat)
52+
prevStatNum, err := parse.ParseStatNumber(comparisonStat)
5353
if err != nil {
5454
return stateError, "unable to parse comparison stat", err
5555
}
@@ -74,21 +74,3 @@ func compareStats(stat, comparisonStat, goal string) (state, string, error) {
7474
return stateError, "unknown goal", errors.New("unknown goal")
7575
}
7676
}
77-
78-
func parseStatNumber(s string) (float64, error) {
79-
if num, err := strconv.ParseFloat(s, 32); err == nil {
80-
return num, nil
81-
} else {
82-
return 0.0, err
83-
}
84-
}
85-
86-
// ParseStatFromDescription expects the stat to be the first part of the string and could have a %, mb, or something directly after it
87-
func ParseStatFromDescription(text string) string {
88-
re := regexp.MustCompile("^[\\d\\.]+\\S*")
89-
matches := re.FindAllString(text, 1)
90-
if len(matches) == 1 {
91-
return matches[0]
92-
}
93-
return ""
94-
}

0 commit comments

Comments
 (0)