Skip to content

Commit

Permalink
chore: Change return type of show (#2045)
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-jcieslak authored Sep 27, 2023
1 parent 5af17cf commit 21f069a
Show file tree
Hide file tree
Showing 20 changed files with 123 additions and 254 deletions.
28 changes: 7 additions & 21 deletions pkg/sdk/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Accounts interface {
// Alter modifies an existing account
Alter(ctx context.Context, opts *AlterAccountOptions) error
// Show returns a list of accounts.
Show(ctx context.Context, opts *ShowAccountOptions) ([]*Account, error)
Show(ctx context.Context, opts *ShowAccountOptions) ([]Account, error)
// ShowByID returns an account by id
ShowByID(ctx context.Context, id AccountObjectIdentifier) (*Account, error)
}
Expand Down Expand Up @@ -345,7 +345,7 @@ type accountDBRow struct {
IsOrgAdmin bool `db:"is_org_admin"`
}

func (row accountDBRow) toAccount() *Account {
func (row accountDBRow) convert() *Account {
acc := &Account{
OrganizationName: row.OrganizationName,
AccountName: row.AccountName,
Expand Down Expand Up @@ -376,27 +376,13 @@ func (row accountDBRow) toAccount() *Account {
return acc
}

func (c *accounts) Show(ctx context.Context, opts *ShowAccountOptions) ([]*Account, error) {
if opts == nil {
opts = &ShowAccountOptions{}
}
if err := opts.validate(); err != nil {
return nil, err
}
sql, err := structToSQL(opts)
func (c *accounts) Show(ctx context.Context, opts *ShowAccountOptions) ([]Account, error) {
opts = createIfNil(opts)
dbRows, err := validateAndQuery[accountDBRow](c.client, ctx, opts)
if err != nil {
return nil, err
}
dest := []accountDBRow{}
err = c.client.query(ctx, &dest, sql)
if err != nil {
return nil, err
}
resultList := make([]*Account, len(dest))
for i, row := range dest {
resultList[i] = row.toAccount()
}

resultList := convertRows[accountDBRow, Account](dbRows)
return resultList, nil
}

Expand All @@ -412,7 +398,7 @@ func (c *accounts) ShowByID(ctx context.Context, id AccountObjectIdentifier) (*A

for _, account := range accounts {
if account.AccountName == id.Name() || account.AccountLocator == id.Name() {
return account, nil
return &account, nil
}
}
return nil, errObjectNotExistOrAuthorized
Expand Down
35 changes: 8 additions & 27 deletions pkg/sdk/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Alerts interface {
// Drop removes an alert.
Drop(ctx context.Context, id SchemaObjectIdentifier) error
// Show returns a list of alerts
Show(ctx context.Context, opts *ShowAlertOptions) ([]*Alert, error)
Show(ctx context.Context, opts *ShowAlertOptions) ([]Alert, error)
// ShowByID returns an alert by ID
ShowByID(ctx context.Context, id SchemaObjectIdentifier) (*Alert, error)
// Describe returns the details of an alert.
Expand Down Expand Up @@ -252,7 +252,7 @@ type alertDBRow struct {
Action string `db:"action"`
}

func (row alertDBRow) toAlert() (*Alert, error) {
func (row alertDBRow) convert() *Alert {
return &Alert{
CreatedOn: row.CreatedOn,
Name: row.Name,
Expand All @@ -265,39 +265,20 @@ func (row alertDBRow) toAlert() (*Alert, error) {
State: AlertState(row.State),
Condition: row.Condition,
Action: row.Action,
}, nil
}
}

func (opts *ShowAlertOptions) validate() error {
return nil
}

func (v *alerts) Show(ctx context.Context, opts *ShowAlertOptions) ([]*Alert, error) {
if opts == nil {
opts = &ShowAlertOptions{}
}
if err := opts.validate(); err != nil {
return nil, err
}
sql, err := structToSQL(opts)
func (v *alerts) Show(ctx context.Context, opts *ShowAlertOptions) ([]Alert, error) {
opts = createIfNil(opts)
dbRows, err := validateAndQuery[alertDBRow](v.client, ctx, opts)
if err != nil {
return nil, err
}
dest := []alertDBRow{}

err = v.client.query(ctx, &dest, sql)
if err != nil {
return nil, err
}
resultList := make([]*Alert, len(dest))
for i, row := range dest {
alert, err := row.toAlert()
if err != nil {
return nil, err
}
resultList[i] = alert
}

resultList := convertRows[alertDBRow, Alert](dbRows)
return resultList, nil
}

Expand All @@ -316,7 +297,7 @@ func (v *alerts) ShowByID(ctx context.Context, id SchemaObjectIdentifier) (*Aler

for _, alert := range alerts {
if alert.ID().name == id.Name() {
return alert, nil
return &alert, nil
}
}
return nil, errObjectNotExistOrAuthorized
Expand Down
6 changes: 3 additions & 3 deletions pkg/sdk/alerts_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ func TestInt_AlertsShow(t *testing.T) {
}
alerts, err := client.Alerts.Show(ctx, showOptions)
require.NoError(t, err)
assert.Contains(t, alerts, alertTest)
assert.Contains(t, alerts, alert2Test)
assert.Contains(t, alerts, *alertTest)
assert.Contains(t, alerts, *alert2Test)
assert.Equal(t, 2, len(alerts))
})

Expand All @@ -58,7 +58,7 @@ func TestInt_AlertsShow(t *testing.T) {
}
alerts, err := client.Alerts.Show(ctx, showOptions)
require.NoError(t, err)
assert.Contains(t, alerts, alertTest)
assert.Contains(t, alerts, *alertTest)
assert.Equal(t, 1, len(alerts))
})

Expand Down
30 changes: 10 additions & 20 deletions pkg/sdk/databases.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type Databases interface {
// Undrop restores the most recent version of a dropped database
Undrop(ctx context.Context, id AccountObjectIdentifier) error
// Show returns a list of databases.
Show(ctx context.Context, opts *ShowDatabasesOptions) ([]*Database, error)
Show(ctx context.Context, opts *ShowDatabasesOptions) ([]Database, error)
// ShowByID returns a database by ID
ShowByID(ctx context.Context, id AccountObjectIdentifier) (*Database, error)
// Describe returns the details of a database.
Expand Down Expand Up @@ -94,8 +94,8 @@ type databaseRow struct {
Kind sql.NullString `db:"kind"`
}

func (row *databaseRow) toDatabase() *Database {
database := Database{
func (row databaseRow) convert() *Database {
database := &Database{
CreatedOn: row.CreatedOn,
Name: row.Name,
}
Expand Down Expand Up @@ -141,7 +141,7 @@ func (row *databaseRow) toDatabase() *Database {
if row.Kind.Valid {
database.Kind = row.Kind.String
}
return &database
return database
}

type CreateDatabaseOptions struct {
Expand Down Expand Up @@ -542,24 +542,14 @@ func (opts *ShowDatabasesOptions) validate() error {
return nil
}

func (v *databases) Show(ctx context.Context, opts *ShowDatabasesOptions) ([]*Database, error) {
if opts == nil {
opts = &ShowDatabasesOptions{}
}
if err := opts.validate(); err != nil {
return nil, err
}
sql, err := structToSQL(opts)
func (v *databases) Show(ctx context.Context, opts *ShowDatabasesOptions) ([]Database, error) {
opts = createIfNil(opts)
dbRows, err := validateAndQuery[databaseRow](v.client, ctx, opts)
if err != nil {
return nil, err
}
var rows []databaseRow
err = v.client.query(ctx, &rows, sql)
databases := make([]*Database, len(rows))
for i, row := range rows {
databases[i] = row.toDatabase()
}
return databases, err
resultList := convertRows[databaseRow, Database](dbRows)
return resultList, nil
}

func (v *databases) ShowByID(ctx context.Context, id AccountObjectIdentifier) (*Database, error) {
Expand All @@ -573,7 +563,7 @@ func (v *databases) ShowByID(ctx context.Context, id AccountObjectIdentifier) (*
}
for _, database := range databases {
if database.ID() == id {
return database, nil
return &database, nil
}
}
return nil, errObjectNotExistOrAuthorized
Expand Down
30 changes: 7 additions & 23 deletions pkg/sdk/failover_groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type FailoverGroups interface {
// Drop removes a failover group.
Drop(ctx context.Context, id AccountObjectIdentifier, opts *DropFailoverGroupOptions) error
// Show returns a list of failover groups.
Show(ctx context.Context, opts *ShowFailoverGroupOptions) ([]*FailoverGroup, error)
Show(ctx context.Context, opts *ShowFailoverGroupOptions) ([]FailoverGroup, error)
// ShowByID returns a failover group by ID
ShowByID(ctx context.Context, id AccountObjectIdentifier) (*FailoverGroup, error)
// ShowDatabases returns a list of databases in a failover group.
Expand Down Expand Up @@ -393,7 +393,7 @@ type failoverGroupDBRow struct {
Owner sql.NullString `db:"owner"`
}

func (row failoverGroupDBRow) toFailoverGroup() *FailoverGroup {
func (row failoverGroupDBRow) convert() *FailoverGroup {
ots := strings.Split(row.ObjectTypes, ",")
pluralObjectTypes := make([]PluralObjectType, 0, len(ots))
for _, ot := range ots {
Expand Down Expand Up @@ -458,29 +458,13 @@ func (row failoverGroupDBRow) toFailoverGroup() *FailoverGroup {
}
}

// List all the failover groups by pattern.
func (v *failoverGroups) Show(ctx context.Context, opts *ShowFailoverGroupOptions) ([]*FailoverGroup, error) {
if opts == nil {
opts = &ShowFailoverGroupOptions{}
}
if err := opts.validate(); err != nil {
return nil, err
}
sql, err := structToSQL(opts)
if err != nil {
return nil, err
}
dest := []failoverGroupDBRow{}

err = v.client.query(ctx, &dest, sql)
func (v *failoverGroups) Show(ctx context.Context, opts *ShowFailoverGroupOptions) ([]FailoverGroup, error) {
opts = createIfNil(opts)
dbRows, err := validateAndQuery[failoverGroupDBRow](v.client, ctx, opts)
if err != nil {
return nil, err
}
resultList := make([]*FailoverGroup, len(dest))
for i, row := range dest {
resultList[i] = row.toFailoverGroup()
}

resultList := convertRows[failoverGroupDBRow, FailoverGroup](dbRows)
return resultList, nil
}

Expand All @@ -495,7 +479,7 @@ func (v *failoverGroups) ShowByID(ctx context.Context, id AccountObjectIdentifie
}
for _, failoverGroup := range failoverGroups {
if failoverGroup.ID() == id && failoverGroup.AccountLocator == currentAccount {
return failoverGroup, nil
return &failoverGroup, nil
}
}
return nil, errObjectNotExistOrAuthorized
Expand Down
36 changes: 13 additions & 23 deletions pkg/sdk/file_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type FileFormats interface {
// Drop removes a FileFormat.
Drop(ctx context.Context, id SchemaObjectIdentifier, opts *DropFileFormatOptions) error
// Show returns a list of fileFormats.
Show(ctx context.Context, opts *ShowFileFormatsOptions) ([]*FileFormat, error)
Show(ctx context.Context, opts *ShowFileFormatsOptions) ([]FileFormat, error)
// ShowByID returns a FileFormat by ID
ShowByID(ctx context.Context, id SchemaObjectIdentifier) (*FileFormat, error)
// Describe returns the details of a FileFormat.
Expand Down Expand Up @@ -114,7 +114,7 @@ type showFileFormatsOptionsResult struct {
DisableAutoConvert bool `json:"DISABLE_AUTO_CONVERT"`
}

func (row *FileFormatRow) toFileFormat() *FileFormat {
func (row FileFormatRow) convert() *FileFormat {
inputOptions := showFileFormatsOptionsResult{}
err := json.Unmarshal([]byte(row.FormatOptions), &inputOptions)
if err != nil {
Expand All @@ -132,9 +132,9 @@ func (row *FileFormatRow) toFileFormat() *FileFormat {
Options: FileFormatTypeOptions{},
}

newNullIf := []NullString{}
for _, s := range inputOptions.NullIf {
newNullIf = append(newNullIf, NullString{s})
newNullIf := make([]NullString, len(inputOptions.NullIf))
for i, s := range inputOptions.NullIf {
newNullIf[i] = NullString{s}
}

switch ff.Type {
Expand Down Expand Up @@ -638,24 +638,14 @@ func (opts *ShowFileFormatsOptions) validate() error {
return nil
}

func (v *fileFormats) Show(ctx context.Context, opts *ShowFileFormatsOptions) ([]*FileFormat, error) {
if opts == nil {
opts = &ShowFileFormatsOptions{}
}
if err := opts.validate(); err != nil {
return nil, err
}
sql, err := structToSQL(opts)
func (v *fileFormats) Show(ctx context.Context, opts *ShowFileFormatsOptions) ([]FileFormat, error) {
opts = createIfNil(opts)
dbRows, err := validateAndQuery[FileFormatRow](v.client, ctx, opts)
if err != nil {
return nil, err
}
var rows []FileFormatRow
err = v.client.query(ctx, &rows, sql)
fileFormats := make([]*FileFormat, len(rows))
for i, row := range rows {
fileFormats[i] = row.toFileFormat()
}
return fileFormats, err
resultList := convertRows[FileFormatRow, FileFormat](dbRows)
return resultList, nil
}

func (v *fileFormats) ShowByID(ctx context.Context, id SchemaObjectIdentifier) (*FileFormat, error) {
Expand All @@ -670,9 +660,9 @@ func (v *fileFormats) ShowByID(ctx context.Context, id SchemaObjectIdentifier) (
if err != nil {
return nil, err
}
for _, FileFormat := range fileFormats {
if reflect.DeepEqual(FileFormat.ID(), id) {
return FileFormat, nil
for _, f := range fileFormats {
if reflect.DeepEqual(f.ID(), id) {
return &f, nil
}
}
return nil, errObjectNotExistOrAuthorized
Expand Down
10 changes: 5 additions & 5 deletions pkg/sdk/file_format_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,8 +466,8 @@ func TestInt_FileFormatsShow(t *testing.T) {
fileFormats, err := client.FileFormats.Show(ctx, nil)
require.NoError(t, err)
assert.LessOrEqual(t, 2, len(fileFormats))
assert.Contains(t, fileFormats, fileFormatTest)
assert.Contains(t, fileFormats, fileFormatTest2)
assert.Contains(t, fileFormats, *fileFormatTest)
assert.Contains(t, fileFormats, *fileFormatTest2)
})

t.Run("LIKE", func(t *testing.T) {
Expand All @@ -478,7 +478,7 @@ func TestInt_FileFormatsShow(t *testing.T) {
})
require.NoError(t, err)
assert.LessOrEqual(t, 1, len(fileFormats))
assert.Contains(t, fileFormats, fileFormatTest)
assert.Contains(t, fileFormats, *fileFormatTest)
})

t.Run("IN", func(t *testing.T) {
Expand All @@ -489,8 +489,8 @@ func TestInt_FileFormatsShow(t *testing.T) {
})
require.NoError(t, err)
assert.LessOrEqual(t, 2, len(fileFormats))
assert.Contains(t, fileFormats, fileFormatTest)
assert.Contains(t, fileFormats, fileFormatTest2)
assert.Contains(t, fileFormats, *fileFormatTest)
assert.Contains(t, fileFormats, *fileFormatTest2)
})
}

Expand Down
Loading

0 comments on commit 21f069a

Please sign in to comment.