diff --git a/resource.go b/resource.go index d56fd47..d2f34f1 100644 --- a/resource.go +++ b/resource.go @@ -3,6 +3,7 @@ package forge import ( "context" "iter" + "strconv" core "dappco.re/go/core" ) @@ -20,6 +21,28 @@ type Resource[T any, C any, U any] struct { collection string // collection path: /api/v1/repos/{owner}/{repo}/issues } +// String returns a safe summary of the resource configuration. +// +// Usage: +// +// s := res.String() +func (r *Resource[T, C, U]) String() string { + return core.Concat( + "forge.Resource{path=", + strconv.Quote(r.path), + ", collection=", + strconv.Quote(r.collection), + "}", + ) +} + +// GoString returns a safe Go-syntax summary of the resource configuration. +// +// Usage: +// +// s := fmt.Sprintf("%#v", res) +func (r *Resource[T, C, U]) GoString() string { return r.String() } + // NewResource creates a new Resource for the given path pattern. // The path should be the item path (e.g., /repos/{owner}/{repo}/issues/{index}). // The collection path is derived by stripping the last /{placeholder} segment. diff --git a/resource_string_test.go b/resource_string_test.go new file mode 100644 index 0000000..15e1da2 --- /dev/null +++ b/resource_string_test.go @@ -0,0 +1,21 @@ +package forge + +import ( + "fmt" + "testing" +) + +func TestResource_String_Good(t *testing.T) { + res := NewResource[int, struct{}, struct{}](NewClient("https://forge.lthn.ai", "tok"), "/api/v1/repos/{owner}/{repo}") + got := fmt.Sprint(res) + want := `forge.Resource{path="/api/v1/repos/{owner}/{repo}", collection="/api/v1/repos/{owner}"}` + if got != want { + t.Fatalf("got %q, want %q", got, want) + } + if got := res.String(); got != want { + t.Fatalf("got String()=%q, want %q", got, want) + } + if got := fmt.Sprintf("%#v", res); got != want { + t.Fatalf("got GoString=%q, want %q", got, want) + } +}