Skip to content

Commit

Permalink
enabled nlreturn (#1027)
Browse files Browse the repository at this point in the history
* enabled nlreturn + partial fix

* nlreturn refactoring pt.2

* fix nlreturn pt. 3

* nlreturn fixed

* fixed tests

* codegen fix

* fixed gtrace codegen
  • Loading branch information
size12 authored Feb 2, 2024
1 parent 54823f0 commit c6e45cd
Show file tree
Hide file tree
Showing 264 changed files with 1,731 additions and 11 deletions.
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,6 @@ linters:
- ireturn
- maintidx
- maligned
- nlreturn
- nonamedreturns
- paralleltest
- protogetter
Expand Down
7 changes: 7 additions & 0 deletions balancers/balancers.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func (filterLocalDC) String() string {
func PreferLocalDC(balancer *balancerConfig.Config) *balancerConfig.Config {
balancer.Filter = filterLocalDC{}
balancer.DetectLocalDC = true

return balancer
}

Expand All @@ -49,6 +50,7 @@ func PreferLocalDC(balancer *balancerConfig.Config) *balancerConfig.Config {
func PreferLocalDCWithFallBack(balancer *balancerConfig.Config) *balancerConfig.Config {
balancer = PreferLocalDC(balancer)
balancer.AllowFallback = true

return balancer
}

Expand All @@ -61,6 +63,7 @@ func (locations filterLocations) Allow(_ balancerConfig.Info, c conn.Conn) bool
return true
}
}

return false
}

Expand Down Expand Up @@ -91,6 +94,7 @@ func PreferLocations(balancer *balancerConfig.Config, locations ...string) *bala
}
sort.Strings(locations)
balancer.Filter = filterLocations(locations)

return balancer
}

Expand All @@ -100,6 +104,7 @@ func PreferLocations(balancer *balancerConfig.Config, locations ...string) *bala
func PreferLocationsWithFallback(balancer *balancerConfig.Config, locations ...string) *balancerConfig.Config {
balancer = PreferLocations(balancer, locations...)
balancer.AllowFallback = true

return balancer
}

Expand Down Expand Up @@ -129,6 +134,7 @@ func Prefer(balancer *balancerConfig.Config, filter func(endpoint Endpoint) bool
balancer.Filter = filterFunc(func(_ balancerConfig.Info, c conn.Conn) bool {
return filter(c.Endpoint())
})

return balancer
}

Expand All @@ -138,6 +144,7 @@ func Prefer(balancer *balancerConfig.Config, filter func(endpoint Endpoint) bool
func PreferWithFallback(balancer *balancerConfig.Config, filter func(endpoint Endpoint) bool) *balancerConfig.Config {
balancer = Prefer(balancer, filter)
balancer.AllowFallback = true

return balancer
}

Expand Down
1 change: 1 addition & 0 deletions balancers/balancers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,6 @@ func applyPreferFilter(info balancerConfig.Info, b *balancerConfig.Config, conns
res = append(res, c)
}
}

return res
}
3 changes: 3 additions & 0 deletions balancers/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func CreateFromConfig(s string) (*balancerConfig.Config, error) {
if c.Fallback {
return PreferLocalDCWithFallBack(b), nil
}

return PreferLocalDC(b), nil
case preferTypeLocations:
if len(c.Locations) == 0 {
Expand All @@ -100,6 +101,7 @@ func CreateFromConfig(s string) (*balancerConfig.Config, error) {
if c.Fallback {
return PreferLocationsWithFallback(b, c.Locations...), nil
}

return PreferLocations(b, c.Locations...), nil
default:
return b, nil
Expand All @@ -125,6 +127,7 @@ func FromConfig(config string, opts ...fromConfigOption) *balancerConfig.Config
if h.errorHandler != nil {
h.errorHandler(err)
}

return h.fallbackBalancer
}

Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,5 +292,6 @@ func (c *Config) With(opts ...Option) *Config {
c.trace,
c.metaOptions...,
)

return c
}
2 changes: 2 additions & 0 deletions config/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func defaultGrpcOptions(t *trace.Driver, secure bool, tlsConfig *tls.Config) (op
insecure.NewCredentials(),
))
}

return opts
}

Expand All @@ -72,6 +73,7 @@ func certPool() *x509.CertPool {
if err == nil {
return certPool
}

return x509.NewCertPool()
}

Expand Down
3 changes: 3 additions & 0 deletions coordination/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func Example() {
db, err := ydb.Open(ctx, "grpc://localhost:2136/local")
if err != nil {
fmt.Printf("failed to connect: %v", err)

return
}
defer db.Close(ctx) // cleanup resources
Expand All @@ -28,12 +29,14 @@ func Example() {
})
if err != nil {
fmt.Printf("failed to create node: %v", err)

return
}
defer db.Coordination().DropNode(ctx, "/local/test")
e, c, err := db.Coordination().DescribeNode(ctx, "/local/test")
if err != nil {
fmt.Printf("failed to describe node: %v", err)

return
}
fmt.Printf("node description: %+v\nnode config: %+v\n", e, c)
Expand Down
4 changes: 4 additions & 0 deletions discovery/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ func Example_discoverCluster() {
db, err := ydb.Open(ctx, "grpc://localhost:2136/local")
if err != nil {
fmt.Printf("failed to connect: %v", err)

return
}
defer db.Close(ctx) // cleanup resources
endpoints, err := db.Discovery().Discover(ctx)
if err != nil {
fmt.Printf("discover failed: %v", err)

return
}
fmt.Printf("%s endpoints:\n", db.Name())
Expand All @@ -31,12 +33,14 @@ func Example_whoAmI() {
db, err := ydb.Open(ctx, "grpc://localhost:2136/local")
if err != nil {
fmt.Printf("failed to connect: %v", err)

return
}
defer db.Close(ctx) // cleanup resources
whoAmI, err := db.Discovery().WhoAmI(ctx)
if err != nil {
fmt.Printf("discover failed: %v", err)

return
}
fmt.Printf("%s whoAmI: %s\n", db.Name(), whoAmI.String())
Expand Down
1 change: 1 addition & 0 deletions driver_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ func (d *Driver) String() string {
fmt.Fprintf(buffer, ",Credentials:%v", c.String())
}
buffer.WriteByte('}')

return buffer.String()
}
4 changes: 2 additions & 2 deletions driver_string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestDriver_String(t *testing.T) {
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
s: `Driver{Endpoint:"localhost",Database:"local",Secure:false,Credentials:Anonymous{From:"github.com/ydb-platform/ydb-go-sdk/v3/config.defaultConfig(defaults.go:90)"}}`, //nolint:lll
},
{
name: xtest.CurrentFileLine(),
Expand All @@ -32,7 +32,7 @@ func TestDriver_String(t *testing.T) {
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
s: `Driver{Endpoint:"localhost",Database:"local",Secure:true,Credentials:Anonymous{From:"github.com/ydb-platform/ydb-go-sdk/v3/config.defaultConfig(defaults.go:90)"}}`, //nolint:lll
},
{
name: xtest.CurrentFileLine(),
Expand Down
5 changes: 5 additions & 0 deletions examples/basic/database_sql/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func seriesData(id string, released time.Time, title, info, comment string) type
} else {
commentv = types.OptionalValue(types.TextValue(comment))
}

return types.StructValue(
types.StructFieldValue("series_id", types.BytesValueFromString(id)),
types.StructFieldValue("release_date", types.DateValueFromTime(released)),
Expand Down Expand Up @@ -59,6 +60,7 @@ func getData() (series, seasons, episodes []types.Value) {
seasons = append(seasons, seasonsData...)
episodes = append(episodes, episodesData...)
}

return
}

Expand Down Expand Up @@ -114,6 +116,7 @@ func getDataForITCrowd(seriesID string) (series types.Value, seasons, episodes [
episodes = append(episodes, episodeData(seriesID, seasonID, uuid.New().String(), title, date))
}
}

return series, seasons, episodes
}

Expand Down Expand Up @@ -193,6 +196,7 @@ func getDataForSiliconValley(seriesID string) (series types.Value, seasons, epis
episodes = append(episodes, episodeData(seriesID, seasonID, uuid.New().String(), title, date))
}
}

return series, seasons, episodes
}

Expand All @@ -203,5 +207,6 @@ func date(date string) time.Time {
if err != nil {
panic(err)
}

return t
}
15 changes: 15 additions & 0 deletions examples/basic/database_sql/series.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func selectDefault(ctx context.Context, db *sql.DB) (err error) {
return err
}
log.Printf("AST = %s\n\nPlan = %s", ast, plan)

return nil
}, retry.WithIdempotent(true))
if err != nil {
Expand Down Expand Up @@ -61,11 +62,13 @@ func selectDefault(ctx context.Context, db *sql.DB) (err error) {
*id, *title, releaseDate.Format("2006-01-02"),
)
}

return rows.Err()
}, retry.WithIdempotent(true))
if err != nil {
return fmt.Errorf("execute data query failed: %w", err)
}

return nil
}

Expand Down Expand Up @@ -154,11 +157,13 @@ func selectScan(ctx context.Context, db *sql.DB) (err error) {
episodeID, title, firstAired.Format("2006-01-02"),
)
}

return rows.Err()
}, retry.WithIdempotent(true))
if err != nil {
return fmt.Errorf("scan query failed: %w", err)
}

return nil
}

Expand Down Expand Up @@ -201,11 +206,13 @@ func fillTablesWithData(ctx context.Context, db *sql.DB) (err error) {
); err != nil {
return err
}

return nil
}, retry.WithIdempotent(true))
if err != nil {
return fmt.Errorf("upsert query failed: %w", err)
}

return nil
}

Expand All @@ -232,8 +239,10 @@ func prepareSchema(ctx context.Context, db *sql.DB) (err error) {
)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "create series table failed: %v", err)

return err
}

return nil
}, retry.WithIdempotent(true))
if err != nil {
Expand Down Expand Up @@ -263,8 +272,10 @@ func prepareSchema(ctx context.Context, db *sql.DB) (err error) {
)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "create seasons table failed: %v\n", err)

return err
}

return nil
}, retry.WithIdempotent(true))
if err != nil {
Expand Down Expand Up @@ -296,13 +307,16 @@ func prepareSchema(ctx context.Context, db *sql.DB) (err error) {
)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "create episodes table failed: %v\n", err)

return err
}

return nil
}, retry.WithIdempotent(true))
if err != nil {
return fmt.Errorf("create table failed: %w", err)
}

return nil
}

Expand All @@ -326,5 +340,6 @@ func dropTableIfExists(ctx context.Context, cc *sql.Conn, tableName string) erro
if err != nil {
return fmt.Errorf("drop table failed: %w", err)
}

return nil
}
1 change: 1 addition & 0 deletions examples/basic/gorm/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,5 +373,6 @@ func date(date string) time.Time {
if err != nil {
panic(err)
}

return t
}
3 changes: 3 additions & 0 deletions examples/basic/gorm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func prepareScheme(db *gorm.DB) error {
); err != nil {
return err
}

return db.AutoMigrate(
&Series{},
&Season{},
Expand Down Expand Up @@ -123,6 +124,7 @@ func readAll(db *gorm.DB) error {
}
}
}

return nil
}

Expand Down Expand Up @@ -161,5 +163,6 @@ func findEpisodesByTitle(db *gorm.DB, fragment string) error {
episodes[i].ID, episodes[i].AirDate.Format(dateISO8601), episodes[i].Title,
)
}

return nil
}
3 changes: 3 additions & 0 deletions examples/basic/gorm/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func (s *Series) BeforeCreate(_ *gorm.DB) (err error) {
for i := range s.Seasons {
s.Seasons[i].SeriesID = s.ID
}

return
}

Expand All @@ -48,6 +49,7 @@ func (s *Season) BeforeCreate(_ *gorm.DB) (err error) {
for i := range s.Episodes {
s.Episodes[i].SeasonID = s.ID
}

return
}

Expand All @@ -64,5 +66,6 @@ func (e *Episode) BeforeCreate(_ *gorm.DB) (err error) {
return err
}
e.ID = id.String()

return
}
2 changes: 2 additions & 0 deletions examples/basic/native/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func seriesData(id uint64, released time.Time, title, info, comment string) type
} else {
commentv = types.OptionalValue(types.TextValue(comment))
}

return types.StructValue(
types.StructFieldValue("series_id", types.Uint64Value(id)),
types.StructFieldValue("release_date", types.DateValueFromTime(released)),
Expand Down Expand Up @@ -155,5 +156,6 @@ func days(date string) time.Time {
if err != nil {
panic(err)
}

return t
}
Loading

0 comments on commit c6e45cd

Please sign in to comment.