Skip to content

Commit

Permalink
Replace FileFormat with FileFormatRange
Browse files Browse the repository at this point in the history
  • Loading branch information
MrAlias committed Jan 26, 2024
1 parent db29929 commit 6ef6e67
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
4 changes: 2 additions & 2 deletions schema/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ func ParseFile(path string) (*Schema, error) {

// Parse parses a Schema read from r.
//
// If r contains a Schema with a file format version higher than FileFormat, an
// error will be returned.
// If r contains a Schema with a file format version outside FileFormatRange,
// an error will be returned.
//
// If r contains an invalid schema URL an error will be returned.
func Parse(r io.Reader) (*Schema, error) {
Expand Down
11 changes: 8 additions & 3 deletions schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,14 @@ import (
"github.com/Masterminds/semver/v3"
)

// FileFormat is the highest schema file format version this package is
// FileFormatRange defines the file format version range this package is
// compatible with.
var FileFormat = semver.New(1, 1, 0, "", "")
var FileFormatRange = struct {
Min, Max *semver.Version
}{
Min: semver.New(1, 0, 0, "", ""),
Max: semver.New(1, 1, 0, "", ""),
}

// Schema represents an OpenTelemetry [Schema file].
//
Expand Down Expand Up @@ -66,7 +71,7 @@ func (s *Schema) validate() error {
return fmt.Errorf("invalid file format version: %q: %w", s.FileFormat, err)
}

if FileFormat.LessThan(ffVer) {
if FileFormatRange.Max.LessThan(ffVer) || FileFormatRange.Min.GreaterThan(ffVer) {
return fmt.Errorf("%w: %q", errUnsupportVer, ffVer)
}

Expand Down
18 changes: 9 additions & 9 deletions schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ import (

"github.com/Masterminds/semver/v3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

var fileFormat = "1.1.0"

func TestSchemaInvalidFileFormat(t *testing.T) {
s := &Schema{
FileFormat: "not a semver",
Expand All @@ -32,25 +33,24 @@ func TestSchemaInvalidFileFormat(t *testing.T) {

func TestSchemaUnsupportedFileFormat(t *testing.T) {
versions := []*semver.Version{
semver.New(FileFormat.Major()+1, 0, 0, "", ""),
semver.New(FileFormat.Major(), FileFormat.Minor()+1, 0, "", ""),
semver.New(FileFormat.Major(), FileFormat.Minor(), FileFormat.Patch()+1, "", ""),
semver.New(FileFormatRange.Min.Major()-1, 0, 0, "", ""),
semver.New(FileFormatRange.Max.Major()+1, 0, 0, "", ""),
semver.New(FileFormatRange.Max.Major(), FileFormatRange.Max.Minor()+1, 0, "", ""),
semver.New(FileFormatRange.Max.Major(), FileFormatRange.Max.Minor(), FileFormatRange.Max.Patch()+1, "", ""),
}
for _, v := range versions {
// Sanity check.
require.Truef(t, FileFormat.LessThan(v), "sanity check failed: %s >= %s", FileFormat, v)
s := &Schema{FileFormat: v.String(), SchemaURL: "http://localhost"}
assert.ErrorIsf(t, s.validate(), errUnsupportVer, "unsupported version: %s", v)
assert.Error(t, s.validate(), "unsupported version: %s", v)
}
}

func TestSchemaMissingSchemaURL(t *testing.T) {
s := &Schema{FileFormat: FileFormat.String(), SchemaURL: " "}
s := &Schema{FileFormat: fileFormat, SchemaURL: " "}
assert.ErrorIs(t, s.validate(), errMissingURL)
}

func TestSchemaInvalidSchemaURL(t *testing.T) {
u := "\no\t \a valid URL"
s := &Schema{FileFormat: FileFormat.String(), SchemaURL: u}
s := &Schema{FileFormat: fileFormat, SchemaURL: u}
assert.ErrorContains(t, s.validate(), "invalid schema URL", u)
}

0 comments on commit 6ef6e67

Please sign in to comment.