Merge pull request #404 from RainbowMango/pr_fix_Stringer

Change the receiver of Stringer interface implementation
This commit is contained in:
karmada-bot 2021-06-05 14:17:45 +08:00 committed by GitHub
commit 552e75a2b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 76 additions and 3 deletions

View File

@ -68,7 +68,7 @@ func (c *Components) FullRepository() string {
}
// String returns the full name of the image, including repository and tag(or digest).
func (c *Components) String() string {
func (c Components) String() string {
if c.tag != "" {
return c.FullRepository() + ":" + c.tag
} else if c.digest != "" {

View File

@ -189,3 +189,25 @@ func ExampleComponents_SetTagOrDigest() {
// gcr.io/kube-apiserver@sha256:50d858e0985ecc7f60418aaf0cc5ab587f42c2570a884095a9e8ccacd0f6545c
// gcr.io/kube-apiserver
}
func ExampleComponents_String() {
key := Components{
hostname: "fictional.registry.example",
repository: "karmada-scheduler",
tag: "v1.0.0",
}
pKey := &key
fmt.Printf("%s\n", key)
fmt.Printf("%v\n", key)
fmt.Printf("%s\n", key.String())
fmt.Printf("%s\n", pKey)
fmt.Printf("%v\n", pKey)
fmt.Printf("%s\n", pKey.String())
// Output:
// fictional.registry.example/karmada-scheduler:v1.0.0
// fictional.registry.example/karmada-scheduler:v1.0.0
// fictional.registry.example/karmada-scheduler:v1.0.0
// fictional.registry.example/karmada-scheduler:v1.0.0
// fictional.registry.example/karmada-scheduler:v1.0.0
// fictional.registry.example/karmada-scheduler:v1.0.0
}

View File

@ -29,7 +29,7 @@ type ClusterWideKey struct {
// String returns the key's printable info with format:
// "<GroupVersion>, kind=<Kind>, <NamespaceKey>"
func (k *ClusterWideKey) String() string {
func (k ClusterWideKey) String() string {
return fmt.Sprintf("%s, kind=%s, %s", k.GroupVersion().String(), k.Kind, k.NamespaceKey())
}
@ -93,7 +93,7 @@ type FederatedKey struct {
// String returns the key's printable info with format:
// "cluster=<Cluster>, <GroupVersion>, kind=<Kind>, <NamespaceKey>"
func (f *FederatedKey) String() string {
func (f FederatedKey) String() string {
return fmt.Sprintf("cluster=%s, %s, kind=%s, %s", f.Cluster, f.GroupVersion().String(), f.Kind, f.NamespaceKey())
}

View File

@ -1,6 +1,7 @@
package keys
import (
"fmt"
"testing"
v1 "k8s.io/api/core/v1"
@ -186,3 +187,53 @@ func TestFederatedKeyFunc(t *testing.T) {
})
}
}
func ExampleClusterWideKey_String() {
key := ClusterWideKey{
Group: "apps",
Version: "v1",
Kind: "Namespace",
Namespace: "default",
Name: "foo",
}
pKey := &key
fmt.Printf("%s\n", key)
fmt.Printf("%v\n", key)
fmt.Printf("%s\n", key.String())
fmt.Printf("%s\n", pKey)
fmt.Printf("%v\n", pKey)
fmt.Printf("%s\n", pKey.String())
// Output:
// apps/v1, kind=Namespace, default/foo
// apps/v1, kind=Namespace, default/foo
// apps/v1, kind=Namespace, default/foo
// apps/v1, kind=Namespace, default/foo
// apps/v1, kind=Namespace, default/foo
// apps/v1, kind=Namespace, default/foo
}
func ExampleFederatedKey_String() {
key := FederatedKey{
Cluster: "karamda",
ClusterWideKey: ClusterWideKey{
Group: "apps",
Version: "v1",
Kind: "Namespace",
Namespace: "default",
Name: "foo"},
}
pKey := &key
fmt.Printf("%s\n", key)
fmt.Printf("%v\n", key)
fmt.Printf("%s\n", key.String())
fmt.Printf("%s\n", pKey)
fmt.Printf("%v\n", pKey)
fmt.Printf("%s\n", pKey.String())
// Output:
// cluster=karamda, apps/v1, kind=Namespace, default/foo
// cluster=karamda, apps/v1, kind=Namespace, default/foo
// cluster=karamda, apps/v1, kind=Namespace, default/foo
// cluster=karamda, apps/v1, kind=Namespace, default/foo
// cluster=karamda, apps/v1, kind=Namespace, default/foo
// cluster=karamda, apps/v1, kind=Namespace, default/foo
}