Skip to content

Commit

Permalink
flatten projects for datasource
Browse files Browse the repository at this point in the history
  • Loading branch information
pawelsz-rb committed Oct 19, 2023
1 parent dcd33e2 commit 312941b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
12 changes: 12 additions & 0 deletions client/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ type Project struct {
} `json:"settings_data" mapstructure:"settings_data"`
}

// FlattenedProject is needed for data source
type FlattenedProject struct {
ID int `model:"id" mapstructure:"id"`
Name string `model:"name" mapstructure:"name"`
AccountID int `json:"account_id" model:"account_id" mapstructure:"account_id"`
DateCreated int `json:"date_created" model:"date_created" mapstructure:"date_created"`
DateModified int `json:"date_modified" model:"date_modified" mapstructure:"date_modified"`
Status string `model:"status" mapstructure:"status"`
TimeFormat string `json:"time_format" mapstructure:"time_format"`
Timezone string `json:"timezone" mapstructure:"timezone"`
}

// FIXME: finish implementing the entire set of Project fields
/*
SettingsData struct {
Expand Down
20 changes: 19 additions & 1 deletion rollbar/data_source_projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func dataSourceProjectsRead(ctx context.Context, d *schema.ResourceData, m inter
if err != nil {
return diag.FromErr(err)
}
mustSet(d, "projects", projects)
mustSet(d, "projects", flattenProjects(projects))

// Set resource ID to current timestamp (every resource must have an ID or
// it will be destroyed).
Expand All @@ -112,3 +112,21 @@ func dataSourceProjectsRead(ctx context.Context, d *schema.ResourceData, m inter
log.Debug().Msg("Successfully read project list from API.")
return diags
}

func flattenProjects(projects []client.Project) []client.FlattenedProject {
var fps []client.FlattenedProject

Check failure on line 117 in rollbar/data_source_projects.go

View workflow job for this annotation

GitHub Actions / golangci-lint

Consider preallocating `fps` (prealloc)
for _, p := range projects {
fp := client.FlattenedProject{
ID: p.ID,
Name: p.Name,
AccountID: p.AccountID,
DateCreated: p.DateCreated,
DateModified: p.DateModified,
Status: p.Status,
TimeFormat: p.SettingsData.TimeFormat,
Timezone: p.SettingsData.Timezone,
}
fps = append(fps, fp)
}
return fps
}

0 comments on commit 312941b

Please sign in to comment.