Skip to content

Commit b493e0b

Browse files
authored
Merge pull request #914 from ydb-platform/driver-string
Driver string
2 parents 10479c6 + 33798c9 commit b493e0b

File tree

9 files changed

+119
-23
lines changed

9 files changed

+119
-23
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Extended metrics (fill database.sql callbacks, recognize TLI error)
44
* Refactored config prefix in metrics
55
* Removed excess status labels from metrics
6+
* Implement `fmt.Stringer` interface for `Driver` struct
67

78
## v3.54.2
89
* Added context to some internal methods for better tracing

config/defaults.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/ydb-platform/ydb-go-sdk/v3/balancers"
1414
"github.com/ydb-platform/ydb-go-sdk/v3/credentials"
15+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/stack"
1516
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xresolver"
1617
"github.com/ydb-platform/ydb-go-sdk/v3/trace"
1718
)
@@ -84,7 +85,7 @@ func defaultTLSConfig() *tls.Config {
8485
func defaultConfig() (c *Config) {
8586
return &Config{
8687
credentials: credentials.NewAnonymousCredentials(
87-
credentials.WithSourceInfo("default"),
88+
credentials.WithSourceInfo(stack.Record(0)),
8889
),
8990
balancerConfig: balancers.Default(),
9091
tlsConfig: defaultTLSConfig(),

driver_string.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package ydb
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xstring"
7+
)
8+
9+
// String returns string representation of Driver
10+
func (d *Driver) String() string {
11+
buffer := xstring.Buffer()
12+
defer buffer.Free()
13+
buffer.WriteString("Driver{")
14+
fmt.Fprintf(buffer, "Endpoint:%q,", d.config.Endpoint())
15+
fmt.Fprintf(buffer, "Database:%q,", d.config.Database())
16+
fmt.Fprintf(buffer, "Secure:%v", d.config.Secure())
17+
if c, has := d.config.Credentials().(fmt.Stringer); has {
18+
fmt.Fprintf(buffer, ",Credentials:%v", c.String())
19+
}
20+
buffer.WriteByte('}')
21+
return buffer.String()
22+
}

driver_string_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package ydb
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
8+
"github.com/ydb-platform/ydb-go-sdk/v3/config"
9+
"github.com/ydb-platform/ydb-go-sdk/v3/credentials"
10+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xtest"
11+
)
12+
13+
func TestDriver_String(t *testing.T) {
14+
for _, tt := range []struct {
15+
name string
16+
d *Driver
17+
s string
18+
}{
19+
{
20+
name: xtest.CurrentFileLine(),
21+
d: &Driver{config: config.New(
22+
config.WithEndpoint("localhost"),
23+
config.WithDatabase("local"),
24+
config.WithSecure(false),
25+
)},
26+
s: `Driver{Endpoint:"localhost",Database:"local",Secure:false,Credentials:Anonymous{From:"github.com/ydb-platform/ydb-go-sdk/v3/config.defaultConfig(defaults.go:88)"}}`, //nolint:lll
27+
},
28+
{
29+
name: xtest.CurrentFileLine(),
30+
d: &Driver{config: config.New(
31+
config.WithEndpoint("localhost"),
32+
config.WithDatabase("local"),
33+
config.WithSecure(true),
34+
)},
35+
s: `Driver{Endpoint:"localhost",Database:"local",Secure:true,Credentials:Anonymous{From:"github.com/ydb-platform/ydb-go-sdk/v3/config.defaultConfig(defaults.go:88)"}}`, //nolint:lll
36+
},
37+
{
38+
name: xtest.CurrentFileLine(),
39+
d: &Driver{config: config.New(
40+
config.WithEndpoint("localhost"),
41+
config.WithDatabase("local"),
42+
config.WithSecure(false),
43+
config.WithCredentials(credentials.NewAnonymousCredentials(credentials.WithSourceInfo(t.Name()))),
44+
)},
45+
s: `Driver{Endpoint:"localhost",Database:"local",Secure:false,Credentials:Anonymous{From:"TestDriver_String"}}`, //nolint:lll
46+
},
47+
{
48+
name: xtest.CurrentFileLine(),
49+
d: &Driver{config: config.New(
50+
config.WithEndpoint("localhost"),
51+
config.WithDatabase("local"),
52+
config.WithSecure(true),
53+
config.WithCredentials(credentials.NewStaticCredentials("user", "password", "")),
54+
)},
55+
s: `Driver{Endpoint:"localhost",Database:"local",Secure:true,Credentials:Static{User:"user",Password:"pas***rd",Token:"****(CRC-32c: 00000000)",From:"github.com/ydb-platform/ydb-go-sdk/v3/credentials.NewStaticCredentials(credentials.go:35)"}}`, //nolint:lll
56+
},
57+
{
58+
name: xtest.CurrentFileLine(),
59+
d: &Driver{config: config.New(
60+
config.WithEndpoint("localhost"),
61+
config.WithDatabase("local"),
62+
config.WithSecure(true),
63+
config.WithCredentials(credentials.NewAccessTokenCredentials("AUTH_TOKEN")),
64+
)},
65+
s: `Driver{Endpoint:"localhost",Database:"local",Secure:true,Credentials:AccessToken{Token:"****(CRC-32c: 9F26E847)",From:"github.com/ydb-platform/ydb-go-sdk/v3/credentials.NewAccessTokenCredentials(credentials.go:20)"}}`, //nolint:lll
66+
},
67+
} {
68+
t.Run(tt.name, func(t *testing.T) {
69+
require.Equal(t, tt.s, tt.d.String())
70+
})
71+
}
72+
}

internal/credentials/access_error_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func TestAccessError(t *testing.T) {
4040
errorString: "something went wrong (" +
4141
"endpoint:\"grps://localhost:2135\"," +
4242
"database:\"/local\"," +
43-
"credentials:\"Anonymous()\"" +
43+
"credentials:\"Anonymous{}\"" +
4444
"): test " +
4545
"at `github.com/ydb-platform/ydb-go-sdk/v3/internal/credentials.TestAccessError(access_error_test.go:33)`", //nolint:lll
4646
},
@@ -55,7 +55,7 @@ func TestAccessError(t *testing.T) {
5555
errorString: "something went wrong (" +
5656
"endpoint:\"grps://localhost:2135\"," +
5757
"database:\"/local\"," +
58-
"credentials:\"Anonymous(from:\\\"TestAccessError\\\")\"" +
58+
"credentials:\"Anonymous{From:\\\"TestAccessError\\\"}\"" +
5959
"): test " +
6060
"at `github.com/ydb-platform/ydb-go-sdk/v3/internal/credentials.TestAccessError(access_error_test.go:48)`", //nolint:lll
6161
},
@@ -70,7 +70,7 @@ func TestAccessError(t *testing.T) {
7070
errorString: "something went wrong (" +
7171
"endpoint:\"grps://localhost:2135\"," +
7272
"database:\"/local\"," +
73-
"credentials:\"AccessToken(token:\\\"****(CRC-32c: 9B7801F4)\\\")\"" +
73+
"credentials:\"AccessToken{Token:\\\"****(CRC-32c: 9B7801F4)\\\"}\"" +
7474
"): test " +
7575
"at `github.com/ydb-platform/ydb-go-sdk/v3/internal/credentials.TestAccessError(access_error_test.go:63)`", //nolint:lll
7676
},
@@ -85,7 +85,7 @@ func TestAccessError(t *testing.T) {
8585
errorString: "something went wrong (" +
8686
"endpoint:\"grps://localhost:2135\"," +
8787
"database:\"/local\"," +
88-
"credentials:\"AccessToken(token:\\\"****(CRC-32c: 9B7801F4)\\\",from:\\\"TestAccessError\\\")\"" +
88+
"credentials:\"AccessToken{Token:\\\"****(CRC-32c: 9B7801F4)\\\",From:\\\"TestAccessError\\\"}\"" +
8989
"): test " +
9090
"at `github.com/ydb-platform/ydb-go-sdk/v3/internal/credentials.TestAccessError(access_error_test.go:78)`", //nolint:lll
9191
},
@@ -104,7 +104,7 @@ func TestAccessError(t *testing.T) {
104104
errorString: "something went wrong (" +
105105
"endpoint:\"grps://localhost:2135\"," +
106106
"database:\"/local\"," +
107-
"credentials:\"Static(user:\\\"USER\\\",password:\\\"SEC**********RD\\\",token:\\\"****(CRC-32c: 00000000)\\\")\"" + //nolint:lll
107+
"credentials:\"Static{User:\\\"USER\\\",Password:\\\"SEC**********RD\\\",Token:\\\"****(CRC-32c: 00000000)\\\"}\"" + //nolint:lll
108108
"): test " +
109109
"at `github.com/ydb-platform/ydb-go-sdk/v3/internal/credentials.TestAccessError(access_error_test.go:93)`", //nolint:lll
110110
},
@@ -123,7 +123,7 @@ func TestAccessError(t *testing.T) {
123123
errorString: "something went wrong (" +
124124
"endpoint:\"grps://localhost:2135\"," +
125125
"database:\"/local\"," +
126-
"credentials:\"Static(user:\\\"USER\\\",password:\\\"SEC**********RD\\\",token:\\\"****(CRC-32c: 00000000)\\\",from:\\\"TestAccessError\\\")\"" + //nolint:lll
126+
"credentials:\"Static{User:\\\"USER\\\",Password:\\\"SEC**********RD\\\",Token:\\\"****(CRC-32c: 00000000)\\\",From:\\\"TestAccessError\\\"}\"" + //nolint:lll
127127
"): test " +
128128
"at `github.com/ydb-platform/ydb-go-sdk/v3/internal/credentials.TestAccessError(access_error_test.go:112)`", //nolint:lll
129129
},
@@ -153,7 +153,7 @@ func TestAccessError(t *testing.T) {
153153
errorString: "something went wrong (" +
154154
"endpoint:\"grps://localhost:2135\"," +
155155
"database:\"/local\"," +
156-
"credentials:\"Anonymous()\"" +
156+
"credentials:\"Anonymous{}\"" +
157157
"): test " +
158158
"at `github.com/ydb-platform/ydb-go-sdk/v3/internal/credentials.TestAccessError(access_error_test.go:146)`", //nolint:lll
159159
},

internal/credentials/access_token.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ func (c AccessToken) Token(_ context.Context) (string, error) {
4646
func (c AccessToken) String() string {
4747
buffer := xstring.Buffer()
4848
defer buffer.Free()
49-
buffer.WriteString("AccessToken(token:")
49+
buffer.WriteString("AccessToken{Token:")
5050
fmt.Fprintf(buffer, "%q", secret.Token(c.token))
5151
if c.sourceInfo != "" {
52-
buffer.WriteString(",from:")
52+
buffer.WriteString(",From:")
5353
fmt.Fprintf(buffer, "%q", c.sourceInfo)
5454
}
55-
buffer.WriteByte(')')
55+
buffer.WriteByte('}')
5656
return buffer.String()
5757
}

internal/credentials/anonymous.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ func (c Anonymous) Token(_ context.Context) (string, error) {
4242
func (c Anonymous) String() string {
4343
buffer := xstring.Buffer()
4444
defer buffer.Free()
45-
buffer.WriteString("Anonymous(")
45+
buffer.WriteString("Anonymous{")
4646
if c.sourceInfo != "" {
47-
buffer.WriteString("from:")
47+
buffer.WriteString("From:")
4848
fmt.Fprintf(buffer, "%q", c.sourceInfo)
4949
}
50-
buffer.WriteByte(')')
50+
buffer.WriteByte('}')
5151
return buffer.String()
5252
}

internal/credentials/credentials_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@ func TestCredentialsString(t *testing.T) {
1414
}{
1515
{
1616
NewAnonymousCredentials(),
17-
"Anonymous(from:\"github.com/ydb-platform/ydb-go-sdk/v3/internal/credentials.TestCredentialsString(credentials_test.go:16)\")", //nolint:lll
17+
"Anonymous{From:\"github.com/ydb-platform/ydb-go-sdk/v3/internal/credentials.TestCredentialsString(credentials_test.go:16)\"}", //nolint:lll
1818
},
1919
{
2020
NewAnonymousCredentials(WithSourceInfo("test")),
21-
"Anonymous(from:\"test\")",
21+
"Anonymous{From:\"test\"}",
2222
},
2323
{
2424
NewAccessTokenCredentials("123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"),
25-
"AccessToken(token:\"1234****WXYZ(CRC-32c: 81993EA5)\",from:\"github.com/ydb-platform/ydb-go-sdk/v3/internal/credentials.TestCredentialsString(credentials_test.go:24)\")", //nolint:lll
25+
"AccessToken{Token:\"1234****WXYZ(CRC-32c: 81993EA5)\",From:\"github.com/ydb-platform/ydb-go-sdk/v3/internal/credentials.TestCredentialsString(credentials_test.go:24)\"}", //nolint:lll
2626
},
2727
{
2828
NewAccessTokenCredentials("123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ", WithSourceInfo("test")),
29-
"AccessToken(token:\"1234****WXYZ(CRC-32c: 81993EA5)\",from:\"test\")",
29+
"AccessToken{Token:\"1234****WXYZ(CRC-32c: 81993EA5)\",From:\"test\"}",
3030
},
3131
} {
3232
t.Run(test.s, func(t *testing.T) {

internal/credentials/static.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,16 +147,16 @@ func parseExpiresAt(raw string) (expiresAt time.Time, err error) {
147147
func (c *Static) String() string {
148148
buffer := xstring.Buffer()
149149
defer buffer.Free()
150-
buffer.WriteString("Static(user:")
150+
buffer.WriteString("Static{User:")
151151
fmt.Fprintf(buffer, "%q", c.user)
152-
buffer.WriteString(",password:")
152+
buffer.WriteString(",Password:")
153153
fmt.Fprintf(buffer, "%q", secret.Password(c.password))
154-
buffer.WriteString(",token:")
154+
buffer.WriteString(",Token:")
155155
fmt.Fprintf(buffer, "%q", secret.Token(c.token))
156156
if c.sourceInfo != "" {
157-
buffer.WriteString(",from:")
157+
buffer.WriteString(",From:")
158158
fmt.Fprintf(buffer, "%q", c.sourceInfo)
159159
}
160-
buffer.WriteByte(')')
160+
buffer.WriteByte('}')
161161
return buffer.String()
162162
}

0 commit comments

Comments
 (0)