fix(forge): make stringers nil-safe
Some checks failed
Security Scan / security (push) Successful in 15s
Test / test (push) Has been cancelled

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-02 07:54:28 +00:00
parent 7e8340a3d4
commit edbf3f7088
4 changed files with 60 additions and 0 deletions

View file

@ -165,6 +165,9 @@ func (c *Client) HTTPClient() *http.Client {
//
// s := client.String()
func (c *Client) String() string {
if c == nil {
return "forge.Client{<nil>}"
}
tokenState := "unset"
if c.HasToken() {
tokenState = "set"

View file

@ -117,6 +117,12 @@ func (f *Forge) HasToken() bool { return f.client.HasToken() }
//
// s := f.String()
func (f *Forge) String() string {
if f == nil {
return "forge.Forge{<nil>}"
}
if f.client == nil {
return "forge.Forge{client=<nil>}"
}
return "forge.Forge{" + f.client.String() + "}"
}

View file

@ -27,6 +27,9 @@ type Resource[T any, C any, U any] struct {
//
// s := res.String()
func (r *Resource[T, C, U]) String() string {
if r == nil {
return "forge.Resource{<nil>}"
}
return core.Concat(
"forge.Resource{path=",
strconv.Quote(r.path),

48
stringer_nil_test.go Normal file
View file

@ -0,0 +1,48 @@
package forge
import (
"fmt"
"testing"
)
func TestClient_String_NilSafe(t *testing.T) {
var c *Client
want := "forge.Client{<nil>}"
if got := c.String(); got != want {
t.Fatalf("got String()=%q, want %q", got, want)
}
if got := fmt.Sprint(c); got != want {
t.Fatalf("got fmt.Sprint=%q, want %q", got, want)
}
if got := fmt.Sprintf("%#v", c); got != want {
t.Fatalf("got GoString=%q, want %q", got, want)
}
}
func TestForge_String_NilSafe(t *testing.T) {
var f *Forge
want := "forge.Forge{<nil>}"
if got := f.String(); got != want {
t.Fatalf("got String()=%q, want %q", got, want)
}
if got := fmt.Sprint(f); got != want {
t.Fatalf("got fmt.Sprint=%q, want %q", got, want)
}
if got := fmt.Sprintf("%#v", f); got != want {
t.Fatalf("got GoString=%q, want %q", got, want)
}
}
func TestResource_String_NilSafe(t *testing.T) {
var r *Resource[int, struct{}, struct{}]
want := "forge.Resource{<nil>}"
if got := r.String(); got != want {
t.Fatalf("got String()=%q, want %q", got, want)
}
if got := fmt.Sprint(r); got != want {
t.Fatalf("got fmt.Sprint=%q, want %q", got, want)
}
if got := fmt.Sprintf("%#v", r); got != want {
t.Fatalf("got GoString=%q, want %q", got, want)
}
}