Skip to content

Commit

Permalink
Merge pull request #1120 from ydb-platform/fix-nil-opts
Browse files Browse the repository at this point in the history
* Fixed cases when some option is nil
  • Loading branch information
asmyasnikov authored Mar 12, 2024
2 parents 441f994 + 67c9a32 commit 6e38869
Show file tree
Hide file tree
Showing 49 changed files with 187 additions and 144 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
* Fixed cases when some option is nil

## v3.57.1
* Added logs over query service internals
* Changed `trace.Query` events
Expand Down
6 changes: 3 additions & 3 deletions balancers/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ func FromConfig(config string, opts ...fromConfigOption) *balancerConfig.Config
b *balancerConfig.Config
err error
)
for _, o := range opts {
if o != nil {
o(&h)
for _, opt := range opts {
if opt != nil {
opt(&h)
}
}

Expand Down
12 changes: 6 additions & 6 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,9 @@ func ExcludeGRPCCodesForPessimization(codes ...grpcCodes.Code) Option {
func New(opts ...Option) *Config {
c := defaultConfig()

for _, o := range opts {
if o != nil {
o(c)
for _, opt := range opts {
if opt != nil {
opt(c)
}
}

Expand All @@ -281,9 +281,9 @@ func New(opts ...Option) *Config {

// With makes copy of current Config with specified options
func (c *Config) With(opts ...Option) *Config {
for _, o := range opts {
if o != nil {
o(c)
for _, opt := range opts {
if opt != nil {
opt(c)
}
}
c.meta = meta.New(
Expand Down
6 changes: 3 additions & 3 deletions internal/backoff/backoff.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ func New(opts ...option) logBackoff {
b := logBackoff{
r: xrand.New(xrand.WithLock()),
}
for _, o := range opts {
if o != nil {
o(&b)
for _, opt := range opts {
if opt != nil {
opt(&b)
}
}

Expand Down
6 changes: 3 additions & 3 deletions internal/conn/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,9 +500,9 @@ func newConn(e endpoint.Endpoint, config Config, opts ...option) *conn {
done: make(chan struct{}),
}
c.state.Store(uint32(Created))
for _, o := range opts {
if o != nil {
o(c)
for _, opt := range opts {
if opt != nil {
opt(c)
}
}

Expand Down
6 changes: 3 additions & 3 deletions internal/coordination/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ func New(opts ...Option) Config {
c := Config{
trace: &trace.Coordination{},
}
for _, o := range opts {
if o != nil {
o(&c)
for _, opt := range opts {
if opt != nil {
opt(&c)
}
}

Expand Down
8 changes: 5 additions & 3 deletions internal/credentials/access_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,12 @@ func AccessError(msg string, err error, opts ...authErrorOption) error {
buffer.WriteString(msg)
buffer.WriteString(" (")
for i, opt := range opts {
if i != 0 {
buffer.WriteString(",")
if opt != nil {
if i != 0 {
buffer.WriteString(",")
}
opt.applyAuthErrorOption(buffer)
}
opt.applyAuthErrorOption(buffer)
}
buffer.WriteString("): %w")

Expand Down
4 changes: 3 additions & 1 deletion internal/credentials/access_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ func NewAccessTokenCredentials(token string, opts ...AccessTokenCredentialsOptio
sourceInfo: stack.Record(1),
}
for _, opt := range opts {
opt.ApplyAccessTokenCredentialsOption(c)
if opt != nil {
opt.ApplyAccessTokenCredentialsOption(c)
}
}

return c
Expand Down
4 changes: 3 additions & 1 deletion internal/credentials/anonymous.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ func NewAnonymousCredentials(opts ...AnonymousCredentialsOption) *Anonymous {
sourceInfo: stack.Record(1),
}
for _, opt := range opts {
opt.ApplyAnonymousCredentialsOption(c)
if opt != nil {
opt.ApplyAnonymousCredentialsOption(c)
}
}

return c
Expand Down
4 changes: 3 additions & 1 deletion internal/credentials/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ func NewStaticCredentials(user, password, endpoint string, opts ...StaticCredent
sourceInfo: stack.Record(1),
}
for _, opt := range opts {
opt.ApplyStaticCredentialsOption(c)
if opt != nil {
opt.ApplyStaticCredentialsOption(c)
}
}

return c
Expand Down
6 changes: 3 additions & 3 deletions internal/discovery/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ func New(opts ...Option) *Config {
interval: DefaultInterval,
trace: &trace.Discovery{},
}
for _, o := range opts {
if o != nil {
o(c)
for _, opt := range opts {
if opt != nil {
opt(c)
}
}

Expand Down
17 changes: 7 additions & 10 deletions internal/endpoint/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,10 @@ func (e *endpoint) LastUpdated() time.Time {
func (e *endpoint) Touch(opts ...Option) {
e.mu.Lock()
defer e.mu.Unlock()
for _, o := range append(
[]Option{
withLastUpdated(time.Now()),
},
opts...,
) {
o(e)
for _, opt := range append([]Option{withLastUpdated(time.Now())}, opts...) {
if opt != nil {
opt(e)
}
}
}

Expand Down Expand Up @@ -163,9 +160,9 @@ func New(address string, opts ...Option) *endpoint {
address: address,
lastUpdated: time.Now(),
}
for _, o := range opts {
if o != nil {
o(e)
for _, opt := range opts {
if opt != nil {
opt(e)
}
}

Expand Down
6 changes: 3 additions & 3 deletions internal/meta/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ func New(
credentials: credentials,
database: database,
}
for _, o := range opts {
if o != nil {
o(m)
for _, opt := range opts {
if opt != nil {
opt(m)
}
}

Expand Down
4 changes: 3 additions & 1 deletion internal/meta/trace_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ func TraceID(ctx context.Context, opts ...func(opts *newTraceIDOpts)) (context.C
}
options := newTraceIDOpts{newRandom: uuid.NewRandom}
for _, opt := range opts {
opt(&options)
if opt != nil {
opt(&options)
}
}
uuid, err := options.newRandom()
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion internal/pool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ func New[T any](
done: make(chan struct{}),
}
for _, opt := range opts {
opt(p)
if opt != nil {
opt(p)
}
}

return p
Expand Down
6 changes: 3 additions & 3 deletions internal/query/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ type Config struct {

func New(opts ...Option) *Config {
c := defaults()
for _, o := range opts {
if o != nil {
o(c)
for _, opt := range opts {
if opt != nil {
opt(c)
}
}

Expand Down
8 changes: 6 additions & 2 deletions internal/query/options/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ func ExecuteSettings(opts ...ExecuteOption) (settings *Execute) {
settings.commonExecuteSettings = defaultCommonExecuteSettings()
settings.txControl = tx.DefaultTxControl()
for _, opt := range opts {
opt.applyExecuteOption(settings)
if opt != nil {
opt.applyExecuteOption(settings)
}
}

return settings
Expand Down Expand Up @@ -168,7 +170,9 @@ func TxExecuteSettings(id string, opts ...TxExecuteOption) (settings *txExecuteS
ExecuteSettings: ExecuteSettings(WithTxControl(tx.NewControl(tx.WithTxID(id)))),
}
for _, opt := range opts {
opt.applyTxExecuteOption(settings)
if opt != nil {
opt.applyTxExecuteOption(settings)
}
}

return settings
Expand Down
8 changes: 6 additions & 2 deletions internal/query/options/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ func ParseDoOpts(t *trace.Query, opts ...DoOption) (s *doSettings) {
}

for _, opt := range opts {
opt.applyDoOption(s)
if opt != nil {
opt.applyDoOption(s)
}
}

return s
Expand All @@ -127,7 +129,9 @@ func ParseDoTxOpts(t *trace.Query, opts ...DoTxOption) (s *doTxSettings) {
}

for _, opt := range opts {
opt.applyDoTxOption(s)
if opt != nil {
opt.applyDoTxOption(s)
}
}

return s
Expand Down
4 changes: 3 additions & 1 deletion internal/query/scanner/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ func (s StructScanner) ScanStruct(dst interface{}, opts ...ScanStructOption) err
AllowMissingFieldsInStruct: false,
}
for _, opt := range opts {
opt.applyScanStructOption(&settings)
if opt != nil {
opt.applyScanStructOption(&settings)
}
}
ptr := reflect.ValueOf(dst)
if ptr.Kind() != reflect.Pointer {
Expand Down
4 changes: 3 additions & 1 deletion internal/query/tx/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ func (opts beginTxOptions) applyTxSelector(a *allocator.Allocator, txControl *Yd
selector := a.QueryTransactionControlBeginTx()
selector.BeginTx = a.QueryTransactionSettings()
for _, opt := range opts {
opt.ApplyTxSettingsOption(a, selector.BeginTx)
if opt != nil {
opt.ApplyTxSettingsOption(a, selector.BeginTx)
}
}
txControl.TxSelector = selector
}
Expand Down
8 changes: 6 additions & 2 deletions internal/query/tx/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,19 @@ func (opts Settings) applyTxSelector(a *allocator.Allocator, txControl *Ydb_Quer
beginTx := a.QueryTransactionControlBeginTx()
beginTx.BeginTx = a.QueryTransactionSettings()
for _, opt := range opts {
opt.ApplyTxSettingsOption(a, beginTx.BeginTx)
if opt != nil {
opt.ApplyTxSettingsOption(a, beginTx.BeginTx)
}
}
txControl.TxSelector = beginTx
}

func (opts Settings) ToYDB(a *allocator.Allocator) *Ydb_Query.TransactionSettings {
txSettings := a.QueryTransactionSettings()
for _, opt := range opts {
opt.ApplyTxSettingsOption(a, txSettings)
if opt != nil {
opt.ApplyTxSettingsOption(a, txSettings)
}
}

return txSettings
Expand Down
6 changes: 3 additions & 3 deletions internal/ratelimiter/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ func New(opts ...Option) Config {
c := Config{
trace: &trace.Ratelimiter{},
}
for _, o := range opts {
if o != nil {
o(&c)
for _, opt := range opts {
if opt != nil {
opt(&c)
}
}

Expand Down
6 changes: 3 additions & 3 deletions internal/ratelimiter/options/acquire.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ func NewAcquire(opts ...AcquireOption) Acquire {
h := &acquireOptionsHolder{
acquireType: AcquireTypeDefault,
}
for _, o := range opts {
if o != nil {
o(h)
for _, opt := range opts {
if opt != nil {
opt(h)
}
}

Expand Down
6 changes: 3 additions & 3 deletions internal/repeater/repeater.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ func New(
trace: &trace.Driver{},
}

for _, o := range opts {
if o != nil {
o(r)
for _, opt := range opts {
if opt != nil {
opt(r)
}
}

Expand Down
6 changes: 3 additions & 3 deletions internal/scheme/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,9 @@ func (c *Client) ModifyPermissions(
onDone(finalErr)
}()
var desc permissionsDesc
for _, o := range opts {
if o != nil {
o(&desc)
for _, opt := range opts {
if opt != nil {
opt(&desc)
}
}
call := func(ctx context.Context) error {
Expand Down
6 changes: 3 additions & 3 deletions internal/scheme/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ func New(opts ...Option) Config {
c := Config{
trace: &trace.Scheme{},
}
for _, o := range opts {
if o != nil {
o(&c)
for _, opt := range opts {
if opt != nil {
opt(&c)
}
}

Expand Down
6 changes: 3 additions & 3 deletions internal/scheme/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ func TestSchemeOptions(t *testing.T) {
}

var desc permissionsDesc
for _, o := range opts {
if o != nil {
o(&desc)
for _, opt := range opts {
if opt != nil {
opt(&desc)
}
}

Expand Down
Loading

0 comments on commit 6e38869

Please sign in to comment.