feat(forge): add safe resource stringers
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
40934c2e43
commit
7e8340a3d4
2 changed files with 44 additions and 0 deletions
23
resource.go
23
resource.go
|
|
@ -3,6 +3,7 @@ package forge
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"iter"
|
"iter"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
core "dappco.re/go/core"
|
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
|
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.
|
// 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 path should be the item path (e.g., /repos/{owner}/{repo}/issues/{index}).
|
||||||
// The collection path is derived by stripping the last /{placeholder} segment.
|
// The collection path is derived by stripping the last /{placeholder} segment.
|
||||||
|
|
|
||||||
21
resource_string_test.go
Normal file
21
resource_string_test.go
Normal file
|
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue