Skip to content

Add JSON schema for package index #183

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 7, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add JSON schema for package index
This schema defines the data structure of the Arduino package index at three compliance levels:

- permissive: the minimum accepted format. This allows for a gradual deprecation following specification changes.
- specification: the format as defined in the official specification
- strict: best practices

Helper functions for reading package indexes and validating them against the schema are added, at this point for use in
the tests, but also for the schema-based rules to come.
  • Loading branch information
per1234 committed Jun 7, 2021
commit 58619545aa087267eae9f95f3d864fbf1566b092
1,324 changes: 1,324 additions & 0 deletions etc/schemas/arduino-package-index-definitions-schema.json

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions etc/schemas/arduino-package-index-permissive-schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "https://raw.githubusercontent.com/arduino/arduino-lint/main/etc/schemas/arduino-package-index-permissive-schema.json",
"title": "Arduino Package Index JSON permissive schema",
"description": "Package indexes define Arduino hardware packages. See: https://arduino.github.io/arduino-cli/latest/package_index_json-specification/. This schema defines the minimum accepted data format.",
"allOf": [
{
"$ref": "arduino-package-index-definitions-schema.json#/definitions/root/permissive/object"
}
]
}
11 changes: 11 additions & 0 deletions etc/schemas/arduino-package-index-schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "https://raw.githubusercontent.com/arduino/arduino-lint/main/etc/schemas/arduino-package-index-schema.json",
"title": "Arduino Package Index JSON schema",
"description": "Package indexes define Arduino hardware packages. See: https://arduino.github.io/arduino-cli/latest/package_index_json-specification/. This schema defines the data format per the specification.",
"allOf": [
{
"$ref": "arduino-package-index-definitions-schema.json#/definitions/root/specification/object"
}
]
}
11 changes: 11 additions & 0 deletions etc/schemas/arduino-package-index-strict-schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "https://raw.githubusercontent.com/arduino/arduino-lint/main/etc/schemas/arduino-package-index-strict-schema.json",
"title": "Arduino Package Index JSON strict schema",
"description": "Package indexes define Arduino hardware packages. See: https://arduino.github.io/arduino-cli/latest/package_index_json-specification/. This schema defines the best practices for the data format, above and beyond the specification.",
"allOf": [
{
"$ref": "arduino-package-index-definitions-schema.json#/definitions/root/strict/object"
}
]
}
43 changes: 43 additions & 0 deletions internal/project/packageindex/packageindex.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,55 @@ See: https://arduino.github.io/arduino-cli/latest/package_index_json-specificati
package packageindex

import (
"encoding/json"
"fmt"
"regexp"

"github.com/arduino/arduino-lint/internal/rule/schema"
"github.com/arduino/arduino-lint/internal/rule/schema/compliancelevel"
"github.com/arduino/arduino-lint/internal/rule/schema/schemadata"
"github.com/arduino/go-paths-helper"
)

// Properties parses the package index from the given path and returns the data.
func Properties(packageIndexPath *paths.Path) (map[string]interface{}, error) {
rawIndex, err := packageIndexPath.ReadFile()
if err != nil {
panic(err)
}
var indexData map[string]interface{}
err = json.Unmarshal(rawIndex, &indexData)
if err != nil {
return nil, err
}

return indexData, nil
}

var schemaObject = make(map[compliancelevel.Type]schema.Schema)

// Validate validates boards.txt data against the JSON schema and returns a map of the result for each compliance level.
func Validate(packageIndex map[string]interface{}) map[compliancelevel.Type]schema.ValidationResult {
referencedSchemaFilenames := []string{
"general-definitions-schema.json",
"arduino-package-index-definitions-schema.json",
}

var validationResults = make(map[compliancelevel.Type]schema.ValidationResult)

if schemaObject[compliancelevel.Permissive].Compiled == nil { // Only compile the schemas once.
schemaObject[compliancelevel.Permissive] = schema.Compile("arduino-package-index-permissive-schema.json", referencedSchemaFilenames, schemadata.Asset)
schemaObject[compliancelevel.Specification] = schema.Compile("arduino-package-index-schema.json", referencedSchemaFilenames, schemadata.Asset)
schemaObject[compliancelevel.Strict] = schema.Compile("arduino-package-index-strict-schema.json", referencedSchemaFilenames, schemadata.Asset)
}

validationResults[compliancelevel.Permissive] = schema.Validate(packageIndex, schemaObject[compliancelevel.Permissive])
validationResults[compliancelevel.Specification] = schema.Validate(packageIndex, schemaObject[compliancelevel.Specification])
validationResults[compliancelevel.Strict] = schema.Validate(packageIndex, schemaObject[compliancelevel.Strict])

return validationResults
}

var empty struct{}

// Reference: https://arduino.github.io/arduino-cli/latest/package_index_json-specification/#naming-of-the-json-index-file
Expand Down
8 changes: 8 additions & 0 deletions internal/project/packageindex/packageindex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/arduino/go-paths-helper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

var testDataPath *paths.Path
Expand All @@ -33,6 +34,13 @@ func init() {
testDataPath = paths.New(workingDirectory, "testdata")
}

func TestProperties(t *testing.T) {
packageIndex, err := Properties(testDataPath.Join("package_valid_index.json"))
require.Nil(t, err)

assert.NotNil(t, packageIndex)
}

func TestHasValidExtension(t *testing.T) {
assert.True(t, HasValidExtension(paths.New("/foo", "bar.json")))
assert.False(t, HasValidExtension(paths.New("/foo", "bar.baz")))
Expand Down
Loading