diff options
| author | mo khan <mo@mokhan.ca> | 2024-05-18 12:20:35 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2024-05-18 12:20:35 -0600 |
| commit | b55760aaa22674b0de3c148eafe13899de2b6041 (patch) | |
| tree | ff689481b121b892498c9e62ae6e9bb24423c02c | |
| parent | 41fe3a65860c6d9572e4dde4682d8233a3bd3ba3 (diff) | |
refactor: extract func to parse issues
| -rw-r--r-- | cmd/stanuki/main.go | 3 | ||||
| -rw-r--r-- | pkg/gitlab/issue.go | 10 | ||||
| -rw-r--r-- | pkg/gitlab/issue_test.go | 11 |
3 files changed, 21 insertions, 3 deletions
diff --git a/cmd/stanuki/main.go b/cmd/stanuki/main.go index 18284d4..3d0da86 100644 --- a/cmd/stanuki/main.go +++ b/cmd/stanuki/main.go @@ -7,7 +7,6 @@ import ( "net/http" "github.com/xlgmokha/x/pkg/env" - "github.com/xlgmokha/x/pkg/serde" "github.com/xlgmokha/x/pkg/x" "gitlab.com/mokhax/stanuki/pkg/gitlab" ) @@ -22,7 +21,7 @@ func main() { if env.Fetch("DUMP", "") != "" { fmt.Println(string(x.Must(io.ReadAll(response.Body)))) } else { - issues := x.Must(serde.From[[]gitlab.Issue](response.Body, serde.JSON)) + issues := x.Must(gitlab.FromIssues(response.Body)) for _, issue := range issues { fmt.Printf("%v: %v\n", issue.ID, issue.Title) } diff --git a/pkg/gitlab/issue.go b/pkg/gitlab/issue.go index 720a78b..959ade6 100644 --- a/pkg/gitlab/issue.go +++ b/pkg/gitlab/issue.go @@ -1,5 +1,11 @@ package gitlab +import ( + "io" + + "github.com/xlgmokha/x/pkg/serde" +) + type Issue struct { ID int `json:"id"` IID int `json:"iid"` @@ -7,3 +13,7 @@ type Issue struct { Title string `json:"title"` Description string `json:"description"` } + +func FromIssues(r io.Reader) ([]Issue, error) { + return serde.From[[]Issue](r, serde.JSON) +} diff --git a/pkg/gitlab/issue_test.go b/pkg/gitlab/issue_test.go index fd1feeb..029ba4a 100644 --- a/pkg/gitlab/issue_test.go +++ b/pkg/gitlab/issue_test.go @@ -1,15 +1,24 @@ package gitlab import ( + "strings" "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestIssue(t *testing.T) { t.Run("ParseIssues", func(t *testing.T) { t.Run("parses the array of issues from IO", func(t *testing.T) { - assert.Equal(t, false, true) + body := strings.NewReader(`[{"ID":1,"title":"Issue Title"}]`) + + results, err := FromIssues(body) + + require.NoError(t, err) + require.Equal(t, 1, len(results)) + assert.Equal(t, 1, results[0].ID) + assert.Equal(t, "Issue Title", results[0].Title) }) }) } |
