Skip to content

Commit 85e39fe

Browse files
committed
Use a template to define package index component ID format
The previous inflexible approach to defining the format of the identifiers used for package index components in rule messages will not allow for identifying platform tool dependencies. A template approach will support any ID format.
1 parent 288fe38 commit 85e39fe

File tree

1 file changed

+21
-23
lines changed

1 file changed

+21
-23
lines changed

Diff for: internal/project/projectdata/packageindex.go

+21-23
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
package projectdata
1717

1818
import (
19+
"bytes"
1920
"fmt"
20-
"strings"
21+
"text/template"
2122

2223
clipackageindex "github.com/arduino/arduino-cli/arduino/cores/packageindex"
2324
"github.com/arduino/arduino-lint/internal/project/packageindex"
@@ -46,22 +47,22 @@ func InitializeForPackageIndex() {
4647
packageIndexSystems = nil
4748
packageIndexSchemaValidationResult = nil
4849
if packageIndexLoadError == nil {
49-
packageIndexPackages = getPackageIndexData(PackageIndex(), "", "packages", "", "name", "")
50+
packageIndexPackages = getPackageIndexData(PackageIndex(), "", "packages", "", "{{index . 0}}", []string{"name"})
5051

5152
for _, packageData := range PackageIndexPackages() {
52-
packageIndexPlatforms = append(packageIndexPlatforms, getPackageIndexData(packageData.Object, packageData.JSONPointer, "platforms", packageData.ID+":", "architecture", "version")...)
53+
packageIndexPlatforms = append(packageIndexPlatforms, getPackageIndexData(packageData.Object, packageData.JSONPointer, "platforms", packageData.ID, ":{{index . 0}}@{{index . 1}}", []string{"architecture", "version"})...)
5354
}
5455

5556
for _, platformData := range PackageIndexPlatforms() {
56-
packageIndexBoards = append(packageIndexBoards, getPackageIndexData(platformData.Object, platformData.JSONPointer, "boards", platformData.ID+" - ", "name", "")...)
57+
packageIndexBoards = append(packageIndexBoards, getPackageIndexData(platformData.Object, platformData.JSONPointer, "boards", platformData.ID, " - {{index . 0}}", []string{"name"})...)
5758
}
5859

5960
for _, packageData := range PackageIndexPackages() {
60-
packageIndexTools = append(packageIndexTools, getPackageIndexData(packageData.Object, packageData.JSONPointer, "tools", packageData.ID+":", "name", "version")...)
61+
packageIndexTools = append(packageIndexTools, getPackageIndexData(packageData.Object, packageData.JSONPointer, "tools", packageData.ID, ":{{index . 0}}@{{index . 1}}", []string{"name", "version"})...)
6162
}
6263

6364
for _, toolData := range PackageIndexTools() {
64-
packageIndexSystems = append(packageIndexSystems, getPackageIndexData(toolData.Object, toolData.JSONPointer, "systems", toolData.ID+" - ", "host", "")...)
65+
packageIndexSystems = append(packageIndexSystems, getPackageIndexData(toolData.Object, toolData.JSONPointer, "systems", toolData.ID, " - {{index . 0}}", []string{"host"})...)
6566
}
6667

6768
packageIndexSchemaValidationResult = packageindex.Validate(PackageIndex())
@@ -131,7 +132,7 @@ func PackageIndexSchemaValidationResult() map[compliancelevel.Type]schema.Valida
131132
return packageIndexSchemaValidationResult
132133
}
133134

134-
func getPackageIndexData(interfaceObject map[string]interface{}, pointerPrefix string, dataKey string, iDPrefix string, iDKey string, versionKey string) []PackageIndexData {
135+
func getPackageIndexData(interfaceObject map[string]interface{}, pointerPrefix string, dataKey string, iDPrefix string, iDSuffixTemplateString string, iDSuffixKeys []string) []PackageIndexData {
135136
var data []PackageIndexData
136137

137138
interfaceSlice, ok := interfaceObject[dataKey].([]interface{})
@@ -154,33 +155,30 @@ func getPackageIndexData(interfaceObject map[string]interface{}, pointerPrefix s
154155
// In the event missing data prevents creating a standard reference ID for the data, use the JSON pointer.
155156
fallbackID := interfaceElementData.JSONPointer
156157

157-
if iDPrefix != "" && strings.HasPrefix(iDPrefix, pointerPrefix) {
158+
if iDPrefix != "" && iDPrefix == pointerPrefix {
158159
// Parent object uses fallback ID, so this one must even if it was possible to generate a true suffix.
159160
return fallbackID
160161
}
161-
iD := iDPrefix
162162

163-
iDSuffix, ok := object[iDKey].(string)
164-
if !ok {
165-
return fallbackID
166-
}
167-
if iDSuffix == "" {
168-
return fallbackID
169-
}
170-
iD += iDSuffix
171-
172-
if versionKey != "" {
173-
iDVersion, ok := object[versionKey].(string)
163+
// Gather the ID suffix components.
164+
iDSuffixComponents := []string{}
165+
for _, key := range iDSuffixKeys {
166+
component, ok := object[key].(string)
174167
if !ok {
175168
return fallbackID
176169
}
177-
if iDVersion == "" {
170+
if component == "" {
178171
return fallbackID
179172
}
180-
iD += "@" + iDVersion
173+
iDSuffixComponents = append(iDSuffixComponents, component)
181174
}
182175

183-
return iD
176+
// Fill the ID suffix components into the template.
177+
iDSuffixTemplate := template.Must(template.New("iDSuffixTemplate").Parse(iDSuffixTemplateString))
178+
iDSuffix := new(bytes.Buffer)
179+
iDSuffixTemplate.Execute(iDSuffix, iDSuffixComponents)
180+
181+
return iDPrefix + iDSuffix.String()
184182
}
185183
interfaceElementData.ID = objectID()
186184

0 commit comments

Comments
 (0)