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 2882334
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 14 deletions.
2 changes: 0 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
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()
}
34 changes: 34 additions & 0 deletions driver_string_test.go
Original file line number Diff line number Diff line change
@@ -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:''}}",

Check failure on line 27 in driver_string_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

line is 130 characters (lll)
},
} {
t.Run(tt.name, func(t *testing.T) {
require.Equal(t, tt.s, tt.d.String())
})
}
}

0 comments on commit 2882334

Please sign in to comment.