It seems that the error you encountered is related to the way the PermissionsGroups map is being populated in the toPermissionsLabels method. The issue arises from attempting to use item.GroupTitle directly to index a map incorrectly.
Here's the corrected version of the toPermissionsLabels method to ensure that it properly builds the PermissionsLabels structure without any indexing issues:
package main
import (
"fmt"
"strings"
)
type PermissionsItem struct {
Id string `json:"id"`
Label string `json:"label"`
Dependence *string `json:"dependence,omitempty"`
}
type PermissionsGroups map[string][]*PermissionsItem
type PermissionsLabels map[string]PermissionsGroups
type permissionsLabels struct {
ResourceCode string `gorm:"column:resource_code"`
GroupTitle string `gorm:"column:group_title"`
PermissionsIDs string `gorm:"column:permissons_ids"`
Label string `gorm:"column:label"`
Dependence *string `gorm:"column:dependence"`
}
type uiStorageImpl struct{}
// toPermissionsLabels method implementation
func (u *uiStorageImpl) toPermissionsLabels(dto []*permissionsLabels) *PermissionsLabels {
permissionsMap := make(PermissionsLabels)
for _, item := range dto {
if item == nil {
continue // skip nil items
}
// Split PermissionsIDs into a slice of strings
permissionsIDs := item.PermissionsIDs
ids := []string{}
if permissionsIDs != "" {
ids = strings.Split(permissionsIDs, ",")
}
// Create PermissionItem for each ID
permsItems := make([]*PermissionsItem, len(ids))
for i, id := range ids {
permsItems[i] = &PermissionsItem{
Id: id,
Label: item.Label,
Dependence: item.Dependence,
}
}
// Populate the map with ResourceCode and GroupTitle
// Ensure the map is initialized for the ResourceCode if not yet done
if _, ok := permissionsMap[item.ResourceCode]; !ok {
permissionsMap[item.ResourceCode] = make(PermissionsGroups)
}
// Assign the permissions items to the group
permissionsMap[item.ResourceCode][item.GroupTitle] = permsItems
}
return &permissionsMap
}
func main() {
// Example usage
dto := []*permissionsLabels{
{ResourceCode: "res1", GroupTitle: "group1", PermissionsIDs: "perm1,perm2", Label: "Group 1 Label", Dependence: nil},
{ResourceCode: "res1", GroupTitle: "group2", PermissionsIDs: "perm3", Label: "Group 2 Label", Dependence: nil},
{ResourceCode: "res2", GroupTitle: "group1", PermissionsIDs: "perm4,perm5", Label: "Group 1 Label", Dependence: nil},
}
uiStorage := &uiStorageImpl{}
permissionsLabels := uiStorage.toPermissionsLabels(dto)
// Print results to verify the output
for resource, groups := range *permissionsLabels {
fmt.Printf("Resource: %s\n", resource)
for group, items := range groups {
fmt.Printf(" Group: %s\n", group)
for _, item := range items {
fmt.Printf(" Item ID: %s, Label: %s, Dependence: %v\n", item.Id, item.Label, item.Dependence)
}
}
}
}
Key Changes:
-
Check for Map Initialization:
- Before trying to assign values to
permissionsMap[item.ResourceCode][item.GroupTitle], check if the permissionsMap[item.ResourceCode] is initialized. If it's not, create a new PermissionsGroups map for that ResourceCode.
-
Error Prevention:
- Ensure that you're working correctly with the map types, especially populating the second level of the map with
item.GroupTitle being a string key.
Functionality:
- Each item from the
dto slice is processed to create appropriate PermissionsItem structs.
- The outer map uses
ResourceCode, and the inner structure uses GroupTitle, correctly allowing you to store and retrieve necessary permissions items.
This implementation should work without producing the index error you previously encountered.