func getCategories()

in categories.go [161:218]


func getCategories(ctx context.Context, pkgs packages.Packages, includePolicyTemplates bool) map[string]*packages.Category {
	span, _ := apm.StartSpan(ctx, "FilterCategories", "app")
	defer span.End()

	categories := map[string]*packages.Category{}

	for _, p := range pkgs {
		for _, c := range p.Categories {
			if _, ok := categories[c]; !ok {
				categories[c] = &packages.Category{
					Id:    c,
					Title: c,
					Count: 0,
				}
			}

			categories[c].Count = categories[c].Count + 1
		}

		if includePolicyTemplates {
			// /categories counts policies and packages separately, but packages are counted too
			// if they don't match but any of their policies does (for the AWS case this would mean that
			// the count for "datastore" would be 3: the Package and the RDS and DynamoDB policies).
			var extraPackageCategories []string

			for _, t := range p.PolicyTemplates {
				// Skip when policy template level `categories` is empty and there is only one policy template
				if t.Categories == nil && len(p.PolicyTemplates) == 1 {
					break
				}

				for _, c := range p.Categories {
					categories[c].Count = categories[c].Count + 1
				}

				// Add policy template level categories.
				for _, c := range t.Categories {
					if _, ok := categories[c]; !ok {
						categories[c] = &packages.Category{
							Id:    c,
							Title: c,
							Count: 0,
						}
					}

					if !p.HasCategory(c) && !slices.Contains(extraPackageCategories, c) {
						extraPackageCategories = append(extraPackageCategories, c)
						categories[c].Count = categories[c].Count + 1
					}

					categories[c].Count = categories[c].Count + 1
				}
			}
		}
	}

	return categories
}