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 f7f724d
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 26 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
3 changes: 2 additions & 1 deletion config/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"crypto/tls"
"crypto/x509"

Check failure on line 5 in config/defaults.go

View workflow job for this annotation

GitHub Actions / golangci-lint

File is not `gofumpt`-ed (gofumpt)
"github.com/ydb-platform/ydb-go-sdk/v3/internal/stack"

Check failure on line 6 in config/defaults.go

View workflow job for this annotation

GitHub Actions / golangci-lint

File is not `goimports`-ed with -local github.com/ydb-platform/ydb-go-sdk/v3 (goimports)
"time"

Check failure on line 7 in config/defaults.go

View workflow job for this annotation

GitHub Actions / golangci-lint

File is not `gofumpt`-ed (gofumpt)

"google.golang.org/grpc"
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)"}}`,

Check failure on line 26 in driver_string_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

line is 172 characters (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)"}}`,

Check failure on line 35 in driver_string_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

line is 171 characters (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"}}`,
},
{
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)"}}`,

Check failure on line 55 in driver_string_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

line is 247 characters (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)"}}`,

Check failure on line 65 in driver_string_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

line is 225 characters (lll)
},
} {
t.Run(tt.name, func(t *testing.T) {
require.Equal(t, tt.s, tt.d.String())
})
}
}
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()
}
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 f7f724d

Please sign in to comment.