Skip to content
This repository was archived by the owner on May 11, 2025. It is now read-only.

Commit ff798ab

Browse files
committed
add analysis data struct
1 parent 8f9ee34 commit ff798ab

3 files changed

Lines changed: 51 additions & 41 deletions

File tree

api.go

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -94,58 +94,68 @@ func CollectLanguages(username string, repos []interface{}) (data map[string]flo
9494
return data, nil
9595
}
9696

97-
func AnalysisUser(username string) (data iris.Map, err error, code int) {
97+
type AnalysisData struct {
98+
Data iris.Map
99+
Err error
100+
Code int
101+
}
102+
103+
func AnalysisUser(username string) AnalysisData {
98104
if GetUserExist(username) {
99105
res, err := GetUser(username)
100106
if err != nil {
101-
return nil, err, iris.StatusInternalServerError
107+
return AnalysisData{nil, err, iris.StatusInternalServerError}
102108
}
103109
repos, err := iterRepos(username)
104110
if err != nil {
105-
return nil, err, iris.StatusInternalServerError
111+
return AnalysisData{nil, err, iris.StatusInternalServerError}
106112
}
107113
languages, err := CollectLanguages(username, repos)
108114
if err != nil {
109-
return nil, err, iris.StatusInternalServerError
115+
return AnalysisData{nil, err, iris.StatusInternalServerError}
116+
}
117+
return AnalysisData{
118+
iris.Map{
119+
"username": username,
120+
"location": res["location"],
121+
"org": res["type"] != "User",
122+
"repos": res["public_repos"],
123+
"follower": ScaleConvert(res["followers"].(float64), true),
124+
"stars": ScaleConvert(Sum(repos, "stargazers_count"), true),
125+
"forks": ScaleConvert(Sum(repos, "forks_count"), true),
126+
"issues": ScaleConvert(Sum(repos, "open_issues_count"), true),
127+
"watchers": ScaleConvert(Sum(repos, "watchers_count"), true),
128+
"languages": CountLanguages(languages),
129+
}, nil, iris.StatusOK,
110130
}
111-
return iris.Map{
112-
"username": username,
113-
"location": res["location"],
114-
"org": res["type"] != "User",
115-
"repos": res["public_repos"],
116-
"follower": ScaleConvert(res["followers"].(float64), true),
117-
"stars": ScaleConvert(Sum(repos, "stargazers_count"), true),
118-
"forks": ScaleConvert(Sum(repos, "forks_count"), true),
119-
"issues": ScaleConvert(Sum(repos, "open_issues_count"), true),
120-
"watchers": ScaleConvert(Sum(repos, "watchers_count"), true),
121-
"languages": CountLanguages(languages),
122-
}, nil, iris.StatusOK
123131
}
124-
return nil, errors.New("user not found"), iris.StatusNotFound
132+
return AnalysisData{nil, errors.New("user not found"), iris.StatusNotFound}
125133
}
126134

127-
func AnalysisRepo(username string, repo string) (data iris.Map, err error, code int) {
135+
func AnalysisRepo(username string, repo string) AnalysisData {
128136
if GetRepoExist(username, repo) {
129137
res, err := GetRepo(username, repo)
130138
if err != nil {
131-
return nil, err, iris.StatusInternalServerError
139+
return AnalysisData{nil, err, iris.StatusInternalServerError}
132140
}
133141
languages, err := GetLanguages(username, repo)
134142
if err != nil {
135-
return nil, err, iris.StatusInternalServerError
143+
return AnalysisData{nil, err, iris.StatusInternalServerError}
144+
}
145+
return AnalysisData{
146+
iris.Map{
147+
"username": username,
148+
"repo": repo,
149+
"size": SizeConvert(res["size"].(float64), 1),
150+
"stars": ScaleConvert(res["stargazers_count"].(float64), true),
151+
"forks": ScaleConvert(res["forks_count"].(float64), true),
152+
"watchers": ScaleConvert(res["watchers_count"].(float64), true),
153+
"issues": ScaleConvert(res["open_issues_count"].(float64), false),
154+
"color": GetColor(res["language"]),
155+
"license": getLicense(res["license"]),
156+
"languages": CountLanguages(languages),
157+
}, nil, iris.StatusOK,
136158
}
137-
return iris.Map{
138-
"username": username,
139-
"repo": repo,
140-
"size": SizeConvert(res["size"].(float64), 1),
141-
"stars": ScaleConvert(res["stargazers_count"].(float64), true),
142-
"forks": ScaleConvert(res["forks_count"].(float64), true),
143-
"watchers": ScaleConvert(res["watchers_count"].(float64), true),
144-
"issues": ScaleConvert(res["open_issues_count"].(float64), false),
145-
"color": GetColor(res["language"]),
146-
"license": getLicense(res["license"]),
147-
"languages": CountLanguages(languages),
148-
}, nil, iris.StatusOK
149159
}
150-
return nil, errors.New("repo not found"), iris.StatusNotFound
160+
return AnalysisData{nil, errors.New("repo not found"), iris.StatusNotFound}
151161
}

app/repo.php

Whitespace-only changes.

server.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,20 @@ func RunServer() {
1616

1717
func UserAPI(ctx iris.Context) {
1818
username := ctx.Params().Get("username")
19-
data, err, code := AnalysisUser(username)
20-
if err != nil {
21-
ThrowError(ctx, err.Error(), code)
19+
data := AnalysisUser(username)
20+
if data.Err != nil {
21+
ThrowError(ctx, data.Err.Error(), data.Code)
2222
} else {
23-
EndBody(ctx, data, "username")
23+
EndBody(ctx, data.Data, "username")
2424
}
2525
}
2626

2727
func RepoAPI(ctx iris.Context) {
2828
username, repo := ctx.Params().Get("username"), ctx.Params().Get("repo")
29-
data, err, code := AnalysisRepo(username, repo)
30-
if err != nil {
31-
ThrowError(ctx, err.Error(), code)
29+
data := AnalysisRepo(username, repo)
30+
if data.Err != nil {
31+
ThrowError(ctx, data.Err.Error(), data.Code)
3232
} else {
33-
EndBody(ctx, data, "username", "repo")
33+
EndBody(ctx, data.Data, "username", "repo")
3434
}
3535
}

0 commit comments

Comments
 (0)