feat(forge): add safe service stringers
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
bdf669db39
commit
dba9852567
3 changed files with 284 additions and 0 deletions
|
|
@ -173,3 +173,62 @@ func TestOption_Stringers_Empty(t *testing.T) {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestService_Stringers_Good(t *testing.T) {
|
||||
client := NewClient("https://forge.example", "token")
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
got fmt.Stringer
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "RepoService",
|
||||
got: newRepoService(client),
|
||||
want: `forge.RepoService{resource=forge.Resource{path="/api/v1/repos/{owner}/{repo}", collection="/api/v1/repos/{owner}"}}`,
|
||||
},
|
||||
{
|
||||
name: "AdminService",
|
||||
got: newAdminService(client),
|
||||
want: `forge.AdminService{client=forge.Client{baseURL="https://forge.example", token=set, userAgent="go-forge/0.1"}}`,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
if got := tc.got.String(); got != tc.want {
|
||||
t.Fatalf("got String()=%q, want %q", got, tc.want)
|
||||
}
|
||||
if got := fmt.Sprint(tc.got); got != tc.want {
|
||||
t.Fatalf("got fmt.Sprint=%q, want %q", got, tc.want)
|
||||
}
|
||||
if got := fmt.Sprintf("%#v", tc.got); got != tc.want {
|
||||
t.Fatalf("got GoString=%q, want %q", got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestService_Stringers_NilSafe(t *testing.T) {
|
||||
var repo *RepoService
|
||||
if got, want := repo.String(), "forge.RepoService{<nil>}"; got != want {
|
||||
t.Fatalf("got String()=%q, want %q", got, want)
|
||||
}
|
||||
if got, want := fmt.Sprint(repo), "forge.RepoService{<nil>}"; got != want {
|
||||
t.Fatalf("got fmt.Sprint=%q, want %q", got, want)
|
||||
}
|
||||
if got, want := fmt.Sprintf("%#v", repo), "forge.RepoService{<nil>}"; got != want {
|
||||
t.Fatalf("got GoString=%q, want %q", got, want)
|
||||
}
|
||||
|
||||
var admin *AdminService
|
||||
if got, want := admin.String(), "forge.AdminService{<nil>}"; got != want {
|
||||
t.Fatalf("got String()=%q, want %q", got, want)
|
||||
}
|
||||
if got, want := fmt.Sprint(admin), "forge.AdminService{<nil>}"; got != want {
|
||||
t.Fatalf("got fmt.Sprint=%q, want %q", got, want)
|
||||
}
|
||||
if got, want := fmt.Sprintf("%#v", admin), "forge.AdminService{<nil>}"; got != want {
|
||||
t.Fatalf("got GoString=%q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -100,6 +100,10 @@ func formatOptionValue(v any) string {
|
|||
}
|
||||
}
|
||||
|
||||
func serviceString(typeName, fieldName string, value any) string {
|
||||
return typeName + "{" + fieldName + "=" + fmt.Sprint(value) + "}"
|
||||
}
|
||||
|
||||
func lastIndexByte(s string, b byte) int {
|
||||
for i := len(s) - 1; i >= 0; i-- {
|
||||
if s[i] == b {
|
||||
|
|
|
|||
221
service_string.go
Normal file
221
service_string.go
Normal file
|
|
@ -0,0 +1,221 @@
|
|||
package forge
|
||||
|
||||
// String returns a safe summary of the actions service.
|
||||
func (s *ActionsService) String() string {
|
||||
if s == nil {
|
||||
return "forge.ActionsService{<nil>}"
|
||||
}
|
||||
return serviceString("forge.ActionsService", "client", s.client)
|
||||
}
|
||||
|
||||
// GoString returns a safe Go-syntax summary of the actions service.
|
||||
func (s *ActionsService) GoString() string { return s.String() }
|
||||
|
||||
// String returns a safe summary of the ActivityPub service.
|
||||
func (s *ActivityPubService) String() string {
|
||||
if s == nil {
|
||||
return "forge.ActivityPubService{<nil>}"
|
||||
}
|
||||
return serviceString("forge.ActivityPubService", "client", s.client)
|
||||
}
|
||||
|
||||
// GoString returns a safe Go-syntax summary of the ActivityPub service.
|
||||
func (s *ActivityPubService) GoString() string { return s.String() }
|
||||
|
||||
// String returns a safe summary of the admin service.
|
||||
func (s *AdminService) String() string {
|
||||
if s == nil {
|
||||
return "forge.AdminService{<nil>}"
|
||||
}
|
||||
return serviceString("forge.AdminService", "client", s.client)
|
||||
}
|
||||
|
||||
// GoString returns a safe Go-syntax summary of the admin service.
|
||||
func (s *AdminService) GoString() string { return s.String() }
|
||||
|
||||
// String returns a safe summary of the branch service.
|
||||
func (s *BranchService) String() string {
|
||||
if s == nil {
|
||||
return "forge.BranchService{<nil>}"
|
||||
}
|
||||
return serviceString("forge.BranchService", "resource", &s.Resource)
|
||||
}
|
||||
|
||||
// GoString returns a safe Go-syntax summary of the branch service.
|
||||
func (s *BranchService) GoString() string { return s.String() }
|
||||
|
||||
// String returns a safe summary of the commit service.
|
||||
func (s *CommitService) String() string {
|
||||
if s == nil {
|
||||
return "forge.CommitService{<nil>}"
|
||||
}
|
||||
return serviceString("forge.CommitService", "client", s.client)
|
||||
}
|
||||
|
||||
// GoString returns a safe Go-syntax summary of the commit service.
|
||||
func (s *CommitService) GoString() string { return s.String() }
|
||||
|
||||
// String returns a safe summary of the content service.
|
||||
func (s *ContentService) String() string {
|
||||
if s == nil {
|
||||
return "forge.ContentService{<nil>}"
|
||||
}
|
||||
return serviceString("forge.ContentService", "client", s.client)
|
||||
}
|
||||
|
||||
// GoString returns a safe Go-syntax summary of the content service.
|
||||
func (s *ContentService) GoString() string { return s.String() }
|
||||
|
||||
// String returns a safe summary of the issue service.
|
||||
func (s *IssueService) String() string {
|
||||
if s == nil {
|
||||
return "forge.IssueService{<nil>}"
|
||||
}
|
||||
return serviceString("forge.IssueService", "resource", &s.Resource)
|
||||
}
|
||||
|
||||
// GoString returns a safe Go-syntax summary of the issue service.
|
||||
func (s *IssueService) GoString() string { return s.String() }
|
||||
|
||||
// String returns a safe summary of the label service.
|
||||
func (s *LabelService) String() string {
|
||||
if s == nil {
|
||||
return "forge.LabelService{<nil>}"
|
||||
}
|
||||
return serviceString("forge.LabelService", "client", s.client)
|
||||
}
|
||||
|
||||
// GoString returns a safe Go-syntax summary of the label service.
|
||||
func (s *LabelService) GoString() string { return s.String() }
|
||||
|
||||
// String returns a safe summary of the milestone service.
|
||||
func (s *MilestoneService) String() string {
|
||||
if s == nil {
|
||||
return "forge.MilestoneService{<nil>}"
|
||||
}
|
||||
return serviceString("forge.MilestoneService", "client", s.client)
|
||||
}
|
||||
|
||||
// GoString returns a safe Go-syntax summary of the milestone service.
|
||||
func (s *MilestoneService) GoString() string { return s.String() }
|
||||
|
||||
// String returns a safe summary of the misc service.
|
||||
func (s *MiscService) String() string {
|
||||
if s == nil {
|
||||
return "forge.MiscService{<nil>}"
|
||||
}
|
||||
return serviceString("forge.MiscService", "client", s.client)
|
||||
}
|
||||
|
||||
// GoString returns a safe Go-syntax summary of the misc service.
|
||||
func (s *MiscService) GoString() string { return s.String() }
|
||||
|
||||
// String returns a safe summary of the notification service.
|
||||
func (s *NotificationService) String() string {
|
||||
if s == nil {
|
||||
return "forge.NotificationService{<nil>}"
|
||||
}
|
||||
return serviceString("forge.NotificationService", "client", s.client)
|
||||
}
|
||||
|
||||
// GoString returns a safe Go-syntax summary of the notification service.
|
||||
func (s *NotificationService) GoString() string { return s.String() }
|
||||
|
||||
// String returns a safe summary of the organisation service.
|
||||
func (s *OrgService) String() string {
|
||||
if s == nil {
|
||||
return "forge.OrgService{<nil>}"
|
||||
}
|
||||
return serviceString("forge.OrgService", "resource", &s.Resource)
|
||||
}
|
||||
|
||||
// GoString returns a safe Go-syntax summary of the organisation service.
|
||||
func (s *OrgService) GoString() string { return s.String() }
|
||||
|
||||
// String returns a safe summary of the package service.
|
||||
func (s *PackageService) String() string {
|
||||
if s == nil {
|
||||
return "forge.PackageService{<nil>}"
|
||||
}
|
||||
return serviceString("forge.PackageService", "client", s.client)
|
||||
}
|
||||
|
||||
// GoString returns a safe Go-syntax summary of the package service.
|
||||
func (s *PackageService) GoString() string { return s.String() }
|
||||
|
||||
// String returns a safe summary of the pull request service.
|
||||
func (s *PullService) String() string {
|
||||
if s == nil {
|
||||
return "forge.PullService{<nil>}"
|
||||
}
|
||||
return serviceString("forge.PullService", "resource", &s.Resource)
|
||||
}
|
||||
|
||||
// GoString returns a safe Go-syntax summary of the pull request service.
|
||||
func (s *PullService) GoString() string { return s.String() }
|
||||
|
||||
// String returns a safe summary of the release service.
|
||||
func (s *ReleaseService) String() string {
|
||||
if s == nil {
|
||||
return "forge.ReleaseService{<nil>}"
|
||||
}
|
||||
return serviceString("forge.ReleaseService", "resource", &s.Resource)
|
||||
}
|
||||
|
||||
// GoString returns a safe Go-syntax summary of the release service.
|
||||
func (s *ReleaseService) GoString() string { return s.String() }
|
||||
|
||||
// String returns a safe summary of the repository service.
|
||||
func (s *RepoService) String() string {
|
||||
if s == nil {
|
||||
return "forge.RepoService{<nil>}"
|
||||
}
|
||||
return serviceString("forge.RepoService", "resource", &s.Resource)
|
||||
}
|
||||
|
||||
// GoString returns a safe Go-syntax summary of the repository service.
|
||||
func (s *RepoService) GoString() string { return s.String() }
|
||||
|
||||
// String returns a safe summary of the team service.
|
||||
func (s *TeamService) String() string {
|
||||
if s == nil {
|
||||
return "forge.TeamService{<nil>}"
|
||||
}
|
||||
return serviceString("forge.TeamService", "resource", &s.Resource)
|
||||
}
|
||||
|
||||
// GoString returns a safe Go-syntax summary of the team service.
|
||||
func (s *TeamService) GoString() string { return s.String() }
|
||||
|
||||
// String returns a safe summary of the user service.
|
||||
func (s *UserService) String() string {
|
||||
if s == nil {
|
||||
return "forge.UserService{<nil>}"
|
||||
}
|
||||
return serviceString("forge.UserService", "resource", &s.Resource)
|
||||
}
|
||||
|
||||
// GoString returns a safe Go-syntax summary of the user service.
|
||||
func (s *UserService) GoString() string { return s.String() }
|
||||
|
||||
// String returns a safe summary of the webhook service.
|
||||
func (s *WebhookService) String() string {
|
||||
if s == nil {
|
||||
return "forge.WebhookService{<nil>}"
|
||||
}
|
||||
return serviceString("forge.WebhookService", "resource", &s.Resource)
|
||||
}
|
||||
|
||||
// GoString returns a safe Go-syntax summary of the webhook service.
|
||||
func (s *WebhookService) GoString() string { return s.String() }
|
||||
|
||||
// String returns a safe summary of the wiki service.
|
||||
func (s *WikiService) String() string {
|
||||
if s == nil {
|
||||
return "forge.WikiService{<nil>}"
|
||||
}
|
||||
return serviceString("forge.WikiService", "client", s.client)
|
||||
}
|
||||
|
||||
// GoString returns a safe Go-syntax summary of the wiki service.
|
||||
func (s *WikiService) GoString() string { return s.String() }
|
||||
Loading…
Add table
Reference in a new issue