Skip to content

Commit

Permalink
fix Driver.String()
Browse files Browse the repository at this point in the history
  • Loading branch information
asmyasnikov committed Dec 25, 2023
1 parent 17d223e commit a8f6159
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 38 deletions.
4 changes: 1 addition & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
* Extended metrics (fill database.sql callbacks, recognize TLI error)
* Refactored config prefix in metrics
* Removed excess status labels from metrics

## v3.54.3
* Implement `String` interface for `Driver` struct
* Implement `fmt.Stringer` interface for `Driver` struct

## v3.54.2
* Added context to some internal methods for better tracing
Expand Down
3 changes: 2 additions & 1 deletion config/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/ydb-platform/ydb-go-sdk/v3/balancers"
"github.com/ydb-platform/ydb-go-sdk/v3/credentials"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/stack"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xresolver"
"github.com/ydb-platform/ydb-go-sdk/v3/trace"
)
Expand Down Expand Up @@ -84,7 +85,7 @@ func defaultTLSConfig() *tls.Config {
func defaultConfig() (c *Config) {
return &Config{
credentials: credentials.NewAnonymousCredentials(
credentials.WithSourceInfo("default"),
credentials.WithSourceInfo(stack.Record(0)),
),
balancerConfig: balancers.Default(),
tlsConfig: defaultTLSConfig(),
Expand Down
12 changes: 0 additions & 12 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package ydb
import (
"context"
"errors"
"fmt"
"os"
"sync"

Expand Down Expand Up @@ -211,17 +210,6 @@ func (d *Driver) Topic() topic.Client {
return d.topic
}

// String returns string representation of Driver
func (d *Driver) String() string {
return fmt.Sprintf(
"Driver{User: %s, Endpoint: %s, Database: %s, IsSecure %t}",
d.userInfo.User,
d.config.Endpoint(),
d.config.Database(),
d.config.Secure(),
)
}

// Open connects to database by DSN and return driver runtime holder
//
// DSN accept Driver string like
Expand Down
22 changes: 22 additions & 0 deletions driver_string.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package ydb

import (
"fmt"

"github.com/ydb-platform/ydb-go-sdk/v3/internal/xstring"
)

// String returns string representation of Driver
func (d *Driver) String() string {
buffer := xstring.Buffer()
defer buffer.Free()
buffer.WriteString("Driver{")
fmt.Fprintf(buffer, "Endpoint:%q,", d.config.Endpoint())
fmt.Fprintf(buffer, "Database:%q,", d.config.Database())
fmt.Fprintf(buffer, "Secure:%v", d.config.Secure())
if c, has := d.config.Credentials().(fmt.Stringer); has {
fmt.Fprintf(buffer, ",Credentials:%v", c.String())
}
buffer.WriteByte('}')
return buffer.String()
}
72 changes: 72 additions & 0 deletions driver_string_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package ydb

import (
"testing"

"github.com/stretchr/testify/require"

"github.com/ydb-platform/ydb-go-sdk/v3/config"
"github.com/ydb-platform/ydb-go-sdk/v3/credentials"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xtest"
)

func TestDriver_String(t *testing.T) {
for _, tt := range []struct {
name string
d *Driver
s string
}{
{
name: xtest.CurrentFileLine(),
d: &Driver{config: config.New(
config.WithEndpoint("localhost"),
config.WithDatabase("local"),
config.WithSecure(false),
)},
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
},
{
name: xtest.CurrentFileLine(),
d: &Driver{config: config.New(
config.WithEndpoint("localhost"),
config.WithDatabase("local"),
config.WithSecure(true),
)},
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
},
{
name: xtest.CurrentFileLine(),
d: &Driver{config: config.New(
config.WithEndpoint("localhost"),
config.WithDatabase("local"),
config.WithSecure(false),
config.WithCredentials(credentials.NewAnonymousCredentials(credentials.WithSourceInfo(t.Name()))),
)},
s: `Driver{Endpoint:"localhost",Database:"local",Secure:false,Credentials:Anonymous{From:"TestDriver_String"}}`, //nolint:lll
},
{
name: xtest.CurrentFileLine(),
d: &Driver{config: config.New(
config.WithEndpoint("localhost"),
config.WithDatabase("local"),
config.WithSecure(true),
config.WithCredentials(credentials.NewStaticCredentials("user", "password", "")),
)},
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
},
{
name: xtest.CurrentFileLine(),
d: &Driver{config: config.New(
config.WithEndpoint("localhost"),
config.WithDatabase("local"),
config.WithSecure(true),
config.WithCredentials(credentials.NewAccessTokenCredentials("AUTH_TOKEN")),
)},
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)"}}`,
},
} {
t.Run(tt.name, func(t *testing.T) {
require.Equal(t, tt.s, tt.d.String())
})
}
}
14 changes: 7 additions & 7 deletions internal/credentials/access_error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestAccessError(t *testing.T) {
errorString: "something went wrong (" +
"endpoint:\"grps://localhost:2135\"," +
"database:\"/local\"," +
"credentials:\"Anonymous()\"" +
"credentials:\"Anonymous{}\"" +
"): test " +
"at `github.com/ydb-platform/ydb-go-sdk/v3/internal/credentials.TestAccessError(access_error_test.go:33)`", //nolint:lll
},
Expand All @@ -55,7 +55,7 @@ func TestAccessError(t *testing.T) {
errorString: "something went wrong (" +
"endpoint:\"grps://localhost:2135\"," +
"database:\"/local\"," +
"credentials:\"Anonymous(from:\\\"TestAccessError\\\")\"" +
"credentials:\"Anonymous{From:\\\"TestAccessError\\\"}\"" +
"): test " +
"at `github.com/ydb-platform/ydb-go-sdk/v3/internal/credentials.TestAccessError(access_error_test.go:48)`", //nolint:lll
},
Expand All @@ -70,7 +70,7 @@ func TestAccessError(t *testing.T) {
errorString: "something went wrong (" +
"endpoint:\"grps://localhost:2135\"," +
"database:\"/local\"," +
"credentials:\"AccessToken(token:\\\"****(CRC-32c: 9B7801F4)\\\")\"" +
"credentials:\"AccessToken{Token:\\\"****(CRC-32c: 9B7801F4)\\\"}\"" +
"): test " +
"at `github.com/ydb-platform/ydb-go-sdk/v3/internal/credentials.TestAccessError(access_error_test.go:63)`", //nolint:lll
},
Expand All @@ -85,7 +85,7 @@ func TestAccessError(t *testing.T) {
errorString: "something went wrong (" +
"endpoint:\"grps://localhost:2135\"," +
"database:\"/local\"," +
"credentials:\"AccessToken(token:\\\"****(CRC-32c: 9B7801F4)\\\",from:\\\"TestAccessError\\\")\"" +
"credentials:\"AccessToken{Token:\\\"****(CRC-32c: 9B7801F4)\\\",From:\\\"TestAccessError\\\"}\"" +
"): test " +
"at `github.com/ydb-platform/ydb-go-sdk/v3/internal/credentials.TestAccessError(access_error_test.go:78)`", //nolint:lll
},
Expand All @@ -104,7 +104,7 @@ func TestAccessError(t *testing.T) {
errorString: "something went wrong (" +
"endpoint:\"grps://localhost:2135\"," +
"database:\"/local\"," +
"credentials:\"Static(user:\\\"USER\\\",password:\\\"SEC**********RD\\\",token:\\\"****(CRC-32c: 00000000)\\\")\"" + //nolint:lll
"credentials:\"Static{User:\\\"USER\\\",Password:\\\"SEC**********RD\\\",Token:\\\"****(CRC-32c: 00000000)\\\"}\"" + //nolint:lll
"): test " +
"at `github.com/ydb-platform/ydb-go-sdk/v3/internal/credentials.TestAccessError(access_error_test.go:93)`", //nolint:lll
},
Expand All @@ -123,7 +123,7 @@ func TestAccessError(t *testing.T) {
errorString: "something went wrong (" +
"endpoint:\"grps://localhost:2135\"," +
"database:\"/local\"," +
"credentials:\"Static(user:\\\"USER\\\",password:\\\"SEC**********RD\\\",token:\\\"****(CRC-32c: 00000000)\\\",from:\\\"TestAccessError\\\")\"" + //nolint:lll
"credentials:\"Static{User:\\\"USER\\\",Password:\\\"SEC**********RD\\\",Token:\\\"****(CRC-32c: 00000000)\\\",From:\\\"TestAccessError\\\"}\"" + //nolint:lll
"): test " +
"at `github.com/ydb-platform/ydb-go-sdk/v3/internal/credentials.TestAccessError(access_error_test.go:112)`", //nolint:lll
},
Expand Down Expand Up @@ -153,7 +153,7 @@ func TestAccessError(t *testing.T) {
errorString: "something went wrong (" +
"endpoint:\"grps://localhost:2135\"," +
"database:\"/local\"," +
"credentials:\"Anonymous()\"" +
"credentials:\"Anonymous{}\"" +
"): test " +
"at `github.com/ydb-platform/ydb-go-sdk/v3/internal/credentials.TestAccessError(access_error_test.go:146)`", //nolint:lll
},
Expand Down
6 changes: 3 additions & 3 deletions internal/credentials/access_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ func (c AccessToken) Token(_ context.Context) (string, error) {
func (c AccessToken) String() string {
buffer := xstring.Buffer()
defer buffer.Free()
buffer.WriteString("AccessToken(token:")
buffer.WriteString("AccessToken{Token:")
fmt.Fprintf(buffer, "%q", secret.Token(c.token))
if c.sourceInfo != "" {
buffer.WriteString(",from:")
buffer.WriteString(",From:")
fmt.Fprintf(buffer, "%q", c.sourceInfo)
}
buffer.WriteByte(')')
buffer.WriteByte('}')
return buffer.String()
}
6 changes: 3 additions & 3 deletions internal/credentials/anonymous.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ func (c Anonymous) Token(_ context.Context) (string, error) {
func (c Anonymous) String() string {
buffer := xstring.Buffer()
defer buffer.Free()
buffer.WriteString("Anonymous(")
buffer.WriteString("Anonymous{")
if c.sourceInfo != "" {
buffer.WriteString("from:")
buffer.WriteString("From:")
fmt.Fprintf(buffer, "%q", c.sourceInfo)
}
buffer.WriteByte(')')
buffer.WriteByte('}')
return buffer.String()
}
8 changes: 4 additions & 4 deletions internal/credentials/credentials_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ func TestCredentialsString(t *testing.T) {
}{
{
NewAnonymousCredentials(),
"Anonymous(from:\"github.com/ydb-platform/ydb-go-sdk/v3/internal/credentials.TestCredentialsString(credentials_test.go:16)\")", //nolint:lll
"Anonymous{From:\"github.com/ydb-platform/ydb-go-sdk/v3/internal/credentials.TestCredentialsString(credentials_test.go:16)\"}", //nolint:lll
},
{
NewAnonymousCredentials(WithSourceInfo("test")),
"Anonymous(from:\"test\")",
"Anonymous{From:\"test\"}",
},
{
NewAccessTokenCredentials("123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"),
"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
"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
},
{
NewAccessTokenCredentials("123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ", WithSourceInfo("test")),
"AccessToken(token:\"1234****WXYZ(CRC-32c: 81993EA5)\",from:\"test\")",
"AccessToken{Token:\"1234****WXYZ(CRC-32c: 81993EA5)\",From:\"test\"}",
},
} {
t.Run(test.s, func(t *testing.T) {
Expand Down
10 changes: 5 additions & 5 deletions internal/credentials/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,16 @@ func parseExpiresAt(raw string) (expiresAt time.Time, err error) {
func (c *Static) String() string {
buffer := xstring.Buffer()
defer buffer.Free()
buffer.WriteString("Static(user:")
buffer.WriteString("Static{User:")
fmt.Fprintf(buffer, "%q", c.user)
buffer.WriteString(",password:")
buffer.WriteString(",Password:")
fmt.Fprintf(buffer, "%q", secret.Password(c.password))
buffer.WriteString(",token:")
buffer.WriteString(",Token:")
fmt.Fprintf(buffer, "%q", secret.Token(c.token))
if c.sourceInfo != "" {
buffer.WriteString(",from:")
buffer.WriteString(",From:")
fmt.Fprintf(buffer, "%q", c.sourceInfo)
}
buffer.WriteByte(')')
buffer.WriteByte('}')
return buffer.String()
}

0 comments on commit a8f6159

Please sign in to comment.