From 95ac9f6a2178bf69cfb59e2bd20c27f055e2ae9d Mon Sep 17 00:00:00 2001 From: Evsyukov Denis Date: Thu, 22 Sep 2022 10:09:13 +0300 Subject: [PATCH] feat: memory optimization by using custom struct --- github.go | 25 +++++++++++++++++++------ main.go | 5 ++--- templates/template.tmpl | 4 +--- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/github.go b/github.go index 137fe4c..769d96c 100644 --- a/github.go +++ b/github.go @@ -23,6 +23,14 @@ type Github struct { client *github.Client } +// Repository struct for storing parameters from Repository +type Repository struct { + FullName string + HTMLURL string + Language string + Description string +} + // NewGithub creates new github client func NewGithub(ctx context.Context, token string) (client *Github) { var tc *http.Client @@ -36,7 +44,7 @@ func NewGithub(ctx context.Context, token string) (client *Github) { } // GetRepositories getting repositories from Github -func (g *Github) GetRepositories(ctx context.Context) (langRepoMap map[string][]github.Repository, repositories []github.Repository) { +func (g *Github) GetRepositories(ctx context.Context) (langRepoMap map[string][]Repository, repositories []Repository) { opt := &github.ActivityListStarredOptions{} opt.ListOptions.PerPage = perPage @@ -49,7 +57,12 @@ func (g *Github) GetRepositories(ctx context.Context) (langRepoMap map[string][] log.Fatalln("Error: cannot fetch starred:", err) } for _, r := range reps { - repositories = append(repositories, *r.Repository) + repositories = append(repositories, Repository{ + FullName: r.Repository.GetFullName(), + HTMLURL: r.Repository.GetHTMLURL(), + Language: r.Repository.GetLanguage(), + Description: r.Repository.GetDescription(), + }) } if len(reps) != perPage { @@ -63,16 +76,16 @@ func (g *Github) GetRepositories(ctx context.Context) (langRepoMap map[string][] return nil, repositories } - langRepoMap = make(map[string][]github.Repository) + langRepoMap = make(map[string][]Repository) for _, r := range repositories { lang := "Others" - if r.Language != nil { - lang = capitalize(*r.Language) + if r.Language != "" { + lang = capitalize(r.Language) } langList, ok := langRepoMap[lang] if !ok { - langList = []github.Repository{} + langList = []Repository{} } langList = append(langList, r) langRepoMap[lang] = langList diff --git a/main.go b/main.go index 6b2af59..0716937 100644 --- a/main.go +++ b/main.go @@ -10,7 +10,6 @@ import ( _ "embed" - "github.com/google/go-github/github" flag "github.com/spf13/pflag" ) @@ -80,9 +79,9 @@ func main() { r := struct { SortCmd bool - LangRepoMap map[string][]github.Repository + LangRepoMap map[string][]Repository UserName string - Repositories []github.Repository + Repositories []Repository }{ SortCmd: sortCmd, LangRepoMap: langRepoMap, diff --git a/templates/template.tmpl b/templates/template.tmpl index f861418..bdd3a0a 100644 --- a/templates/template.tmpl +++ b/templates/template.tmpl @@ -14,9 +14,7 @@ ## {{ $lang }} {{ range $langMap -}} - {{ if ne .Description nil -}} -- [{{ .FullName }}]({{ .HTMLURL }}) – {{ .Description }}{{ else -}} -- [{{ .FullName }}]({{ .HTMLURL }}){{- end }} +- [{{ .FullName }}]({{ .HTMLURL }}){{ if ne .Description "" }} – {{ .Description }}{{- end }} {{ end }} {{- end }} {{- else }}