Skip to content

Commit

Permalink
remove all uses of iota (#4046)
Browse files Browse the repository at this point in the history
I've needed to catch gotchas that arise from contributors adding a value in the middle of an iota list, not to mention have dealt with prior bugs that happened the same way, now too many times to feel safe about its usage.

This PR removes the use of iota from all const declarations. The intent is to not allow the use of iota within the codebase.

---

#### Does this PR need a docs update or release note?

- [x] ⛔ No

#### Type of change

- [x] 🧹 Tech Debt/Cleanup

#### Issue(s)

* #3993

#### Test Plan

- [x] ⚡ Unit test
- [x] 💚 E2E
  • Loading branch information
ryanfkeepers authored Aug 18, 2023
1 parent 2c00ca4 commit 9abd9d4
Show file tree
Hide file tree
Showing 20 changed files with 110 additions and 115 deletions.
8 changes: 4 additions & 4 deletions src/internal/data/implementations.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ var ErrNotFound = clues.New("not found")
type CollectionState int

const (
NewState = CollectionState(iota)
NotMovedState
MovedState
DeletedState
NewState CollectionState = 0
NotMovedState CollectionState = 1
MovedState CollectionState = 2
DeletedState CollectionState = 3
)

type FetchRestoreCollection struct {
Expand Down
6 changes: 3 additions & 3 deletions src/internal/m365/collection/drive/collections.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ type collectionScope int
const (
// CollectionScopeUnknown is used when we don't know and don't need
// to know the kind, like in the case of deletes
CollectionScopeUnknown collectionScope = iota
CollectionScopeUnknown collectionScope = 0

// CollectionScopeFolder is used for regular folder collections
CollectionScopeFolder
CollectionScopeFolder collectionScope = 1

// CollectionScopePackage is used to represent OneNote items
CollectionScopePackage
CollectionScopePackage collectionScope = 2
)

const restrictedDirectory = "Site Pages"
Expand Down
4 changes: 2 additions & 2 deletions src/internal/m365/collection/drive/metadata/permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
type SharingMode int

const (
SharingModeCustom = SharingMode(iota)
SharingModeInherited
SharingModeCustom SharingMode = 0
SharingModeInherited SharingMode = 1
)

type GV2Type string
Expand Down
6 changes: 4 additions & 2 deletions src/internal/m365/collection/site/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func CollectPages(
bpc inject.BackupProducerConfig,
creds account.M365Config,
ac api.Client,
scope selectors.SharePointScope,
su support.StatusUpdater,
errs *fault.Bus,
) ([]data.BackupCollection, error) {
Expand Down Expand Up @@ -105,7 +106,7 @@ func CollectPages(
collection := NewCollection(
dir,
ac,
Pages,
scope,
su,
bpc.Options)
collection.SetBetaService(betaService)
Expand All @@ -122,6 +123,7 @@ func CollectLists(
bpc inject.BackupProducerConfig,
ac api.Client,
tenantID string,
scope selectors.SharePointScope,
su support.StatusUpdater,
errs *fault.Bus,
) ([]data.BackupCollection, error) {
Expand Down Expand Up @@ -156,7 +158,7 @@ func CollectLists(
collection := NewCollection(
dir,
ac,
List,
scope,
su,
bpc.Options)
collection.AddJob(tuple.ID)
Expand Down
4 changes: 4 additions & 0 deletions src/internal/m365/collection/site/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/alcionai/corso/src/internal/version"
"github.com/alcionai/corso/src/pkg/control"
"github.com/alcionai/corso/src/pkg/fault"
"github.com/alcionai/corso/src/pkg/selectors"
"github.com/alcionai/corso/src/pkg/services/m365/api"
)

Expand Down Expand Up @@ -61,11 +62,14 @@ func (suite *SharePointPagesSuite) TestCollectPages() {
ProtectedResource: mock.NewProvider(siteID, siteID),
}

sel := selectors.NewSharePointBackup([]string{siteID})

col, err := CollectPages(
ctx,
bpc,
creds,
ac,
sel.Lists(selectors.Any())[0],
(&MockGraphService{}).UpdateStatus,
fault.New(true))
assert.NoError(t, err, clues.ToCore(err))
Expand Down
26 changes: 15 additions & 11 deletions src/internal/m365/collection/site/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,23 @@ import (
"github.com/alcionai/corso/src/pkg/fault"
"github.com/alcionai/corso/src/pkg/logger"
"github.com/alcionai/corso/src/pkg/path"
"github.com/alcionai/corso/src/pkg/selectors"
"github.com/alcionai/corso/src/pkg/services/m365/api"
)

type DataCategory int

// channel sizes
const (
collectionChannelBufferSize = 50
fetchChannelSize = 5
)

//go:generate stringer -type=DataCategory
const (
collectionChannelBufferSize = 50
fetchChannelSize = 5
Unknown DataCategory = iota
List
Drive
Pages
Unknown DataCategory = 0
List DataCategory = 1
Pages DataCategory = 2
)

var (
Expand All @@ -53,7 +57,7 @@ type Collection struct {
// jobs contain the SharePoint.Site.ListIDs for the associated list(s).
jobs []string
// M365 IDs of the items of this collection
category DataCategory
category path.CategoryType
client api.Sites
ctrl control.Options
betaService *betaAPI.BetaService
Expand All @@ -64,7 +68,7 @@ type Collection struct {
func NewCollection(
folderPath path.Path,
ac api.Client,
category DataCategory,
scope selectors.SharePointScope,
statusUpdater support.StatusUpdater,
ctrlOpts control.Options,
) *Collection {
Expand All @@ -74,7 +78,7 @@ func NewCollection(
data: make(chan data.Item, collectionChannelBufferSize),
client: ac.Sites(),
statusUpdater: statusUpdater,
category: category,
category: scope.Category().PathType(),
ctrl: ctrlOpts,
}

Expand Down Expand Up @@ -198,9 +202,9 @@ func (sc *Collection) runPopulate(

// Switch retrieval function based on category
switch sc.category {
case List:
case path.ListsCategory:
metrics, err = sc.retrieveLists(ctx, writer, colProgress, errs)
case Pages:
case path.PagesCategory:
metrics, err = sc.retrievePages(ctx, sc.client, writer, colProgress, errs)
}

Expand Down
11 changes: 7 additions & 4 deletions src/internal/m365/collection/site/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/alcionai/corso/src/pkg/control/testdata"
"github.com/alcionai/corso/src/pkg/fault"
"github.com/alcionai/corso/src/pkg/path"
"github.com/alcionai/corso/src/pkg/selectors"
"github.com/alcionai/corso/src/pkg/services/m365/api"
)

Expand Down Expand Up @@ -82,16 +83,18 @@ func (suite *SharePointCollectionSuite) TestCollection_Items() {
dirRoot = "directory"
)

sel := selectors.NewSharePointBackup([]string{"site"})

tables := []struct {
name, itemName string
category DataCategory
scope selectors.SharePointScope
getDir func(t *testing.T) path.Path
getItem func(t *testing.T, itemName string) *Item
}{
{
name: "List",
itemName: "MockListing",
category: List,
scope: sel.Lists(selectors.Any())[0],
getDir: func(t *testing.T) path.Path {
dir, err := path.Build(
tenant,
Expand Down Expand Up @@ -127,7 +130,7 @@ func (suite *SharePointCollectionSuite) TestCollection_Items() {
{
name: "Pages",
itemName: "MockPages",
category: Pages,
scope: sel.Pages(selectors.Any())[0],
getDir: func(t *testing.T) path.Path {
dir, err := path.Build(
tenant,
Expand Down Expand Up @@ -166,7 +169,7 @@ func (suite *SharePointCollectionSuite) TestCollection_Items() {
col := NewCollection(
test.getDir(t),
suite.ac,
test.category,
test.scope,
nil,
control.DefaultOptions())
col.data <- test.getItem(t, test.itemName)
Expand Down
27 changes: 0 additions & 27 deletions src/internal/m365/collection/site/datacategory_string.go

This file was deleted.

2 changes: 2 additions & 0 deletions src/internal/m365/service/sharepoint/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func ProduceBackupCollections(
bpc,
ac,
creds.AzureTenantID,
scope,
su,
errs)
if err != nil {
Expand Down Expand Up @@ -95,6 +96,7 @@ func ProduceBackupCollections(
bpc,
creds,
ac,
scope,
su,
errs)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions src/internal/m365/support/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ type Operation int

//go:generate stringer -type=Operation
const (
OpUnknown Operation = iota
Backup
Restore
Export
OpUnknown Operation = 0
Backup Operation = 1
Restore Operation = 2
Export Operation = 3
)

// Constructor for ConnectorOperationStatus. If the counts do not agree, an error is returned.
Expand Down
14 changes: 7 additions & 7 deletions src/internal/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ func (id StableID) String() string {
//
//go:generate go run golang.org/x/tools/cmd/stringer -type=Schema
const (
UnknownSchema = Schema(iota)
BackupOpSchema
RestoreOpSchema
BackupSchema
BackupDetailsSchema
RepositorySchema
UnknownSchema Schema = 0
BackupOpSchema Schema = 1
RestoreOpSchema Schema = 2
BackupSchema Schema = 3
BackupDetailsSchema Schema = 4
RepositorySchema Schema = 5
)

// common tags for filtering
Expand All @@ -38,7 +38,7 @@ const (
MergeBackup = "merge-backup"
)

// Valid returns true if the ModelType value fits within the iota range.
// Valid returns true if the ModelType value fits within the const range.
func (mt Schema) Valid() bool {
return mt > 0 && mt < RepositorySchema+1
}
Expand Down
10 changes: 5 additions & 5 deletions src/internal/operations/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ type OpStatus int

//go:generate stringer -type=OpStatus -linecomment
const (
Unknown OpStatus = iota // Status Unknown
InProgress // In Progress
Completed // Completed
Failed // Failed
NoData // No Data
Unknown OpStatus = 0 // Status Unknown
InProgress OpStatus = 1 // In Progress
Completed OpStatus = 2 // Completed
Failed OpStatus = 3 // Failed
NoData OpStatus = 4 // No Data
)

// --------------------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions src/pkg/account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ type accountProvider int

//go:generate stringer -type=accountProvider -linecomment
const (
ProviderUnknown accountProvider = iota // Unknown Provider
ProviderM365 // M365
ProviderUnknown accountProvider = 0 // Unknown Provider
ProviderM365 accountProvider = 1 // M365
)

// storage parsing errors
Expand Down
15 changes: 8 additions & 7 deletions src/pkg/backup/details/iteminfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,17 @@ type ItemType int
// Additionally, any itemType directly assigned a number should not be altered.
// This applies to OneDriveItem and FolderItem
const (
UnknownType ItemType = iota // 0, global unknown value
UnknownType ItemType = 0

// Exchange (00x)
ExchangeContact
ExchangeEvent
ExchangeMail
ExchangeContact ItemType = 1
ExchangeEvent ItemType = 2
ExchangeMail ItemType = 3

// SharePoint (10x)
SharePointLibrary ItemType = iota + 97 // 100
SharePointList // 101...
SharePointPage
SharePointLibrary ItemType = 101
SharePointList ItemType = 102
SharePointPage ItemType = 103

// OneDrive (20x)
OneDriveItem ItemType = 205
Expand Down
12 changes: 4 additions & 8 deletions src/pkg/control/repository/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,10 @@ type Maintenance struct {

type MaintenanceType int

// Can't be reordered as we rely on iota for numbering.
//
//go:generate stringer -type=MaintenanceType -linecomment
const (
CompleteMaintenance MaintenanceType = iota // complete
MetadataMaintenance // metadata
CompleteMaintenance MaintenanceType = 0 // complete
MetadataMaintenance MaintenanceType = 1 // metadata
)

var StringToMaintenanceType = map[string]MaintenanceType{
Expand All @@ -40,16 +38,14 @@ var StringToMaintenanceType = map[string]MaintenanceType{

type MaintenanceSafety int

// Can't be reordered as we rely on iota for numbering.
//
//go:generate stringer -type=MaintenanceSafety -linecomment
const (
FullMaintenanceSafety MaintenanceSafety = iota
FullMaintenanceSafety MaintenanceSafety = 0
//nolint:lll
// Use only if there's no other kopia instances accessing the repo and the
// storage backend is strongly consistent.
// https://github.com/kopia/kopia/blob/f9de453efc198b6e993af8922f953a7e5322dc5f/repo/maintenance/maintenance_safety.go#L42
NoMaintenanceSafety
NoMaintenanceSafety MaintenanceSafety = 1
)

type RetentionMode int
Expand Down
Loading

0 comments on commit 9abd9d4

Please sign in to comment.