diff --git a/CHANGELOG.md b/CHANGELOG.md index be404a660..2fac30319 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,8 +3,6 @@ * 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 ## v3.54.2 diff --git a/driver.go b/driver.go index f62fef9ce..65eb86a81 100644 --- a/driver.go +++ b/driver.go @@ -3,7 +3,6 @@ package ydb import ( "context" "errors" - "fmt" "os" "sync" @@ -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 diff --git a/driver_string.go b/driver_string.go new file mode 100644 index 000000000..6a15e20b4 --- /dev/null +++ b/driver_string.go @@ -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() +} diff --git a/driver_string_test.go b/driver_string_test.go new file mode 100644 index 000000000..fd8162fe4 --- /dev/null +++ b/driver_string_test.go @@ -0,0 +1,34 @@ +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(true), + config.WithCredentials(credentials.NewStaticCredentials("user", "password", "")), + )}, + s: "Driver{Endpoint:'localhost',Database:'local',Secure:true,Credentials:Static{user:'user',password:'XXX',token:'',from:''}}", + }, + } { + t.Run(tt.name, func(t *testing.T) { + require.Equal(t, tt.s, tt.d.String()) + }) + } +}