Skip to content

Commit

Permalink
Use separate packages for each file format version
Browse files Browse the repository at this point in the history
  • Loading branch information
tigrannajaryan committed Oct 2, 2021
1 parent 2901a57 commit 2413b13
Show file tree
Hide file tree
Showing 12 changed files with 47 additions and 50 deletions.
2 changes: 1 addition & 1 deletion schema/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module go.opentelemetry.io/otel/schema
go 1.15

require (
github.com/Masterminds/semver/v3 v3.1.1
github.com/stretchr/testify v1.7.0
golang.org/x/mod v0.5.1
gopkg.in/yaml.v2 v2.4.0
)

Expand Down
15 changes: 2 additions & 13 deletions schema/go.sum
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
github.com/Masterminds/semver/v3 v3.1.1 h1:hLg3sBzpNErnxhQtUy/mmLR2I9foDujNK030IGemrRc=
github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/mod v0.5.1 h1:OJxoQ/rynoF0dcCdI7cLPktw/hR2cueqYfjm43oqK38=
golang.org/x/mod v0.5.1/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
Expand Down
6 changes: 2 additions & 4 deletions schema/ast/ast_schema.go → schema/v1.0/ast/ast_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package ast // import "go.opentelemetry.io/otel/schema/ast"
package ast // import "go.opentelemetry.io/otel/schema/v1.0/ast"

import "go.opentelemetry.io/otel/schema/types"

const FileFormat = "1.0.0"
import "go.opentelemetry.io/otel/schema/v1.0/types"

// Schema represents a Schema file in FileFormat 1.0.0 as defined in
// https://github.com/open-telemetry/oteps/blob/main/text/0152-telemetry-schemas.md
Expand Down
2 changes: 1 addition & 1 deletion schema/ast/logs.go → schema/v1.0/ast/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package ast // import "go.opentelemetry.io/otel/schema/ast"
package ast // import "go.opentelemetry.io/otel/schema/v1.0/ast"

// VersionOfLogs corresponds to a section representing a list of changes that happened
// to logs schema in a particular version.
Expand Down
4 changes: 2 additions & 2 deletions schema/ast/metrics.go → schema/v1.0/ast/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package ast // import "go.opentelemetry.io/otel/schema/ast"
package ast // import "go.opentelemetry.io/otel/schema/v1.0/ast"

import "go.opentelemetry.io/otel/schema/types"
import "go.opentelemetry.io/otel/schema/v1.0/types"

// VersionOfMetrics corresponds to a section representing a list of changes that happened
// to metrics schema in a particular version.
Expand Down
4 changes: 2 additions & 2 deletions schema/ast/spans.go → schema/v1.0/ast/spans.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package ast // import "go.opentelemetry.io/otel/schema/ast"
package ast // import "go.opentelemetry.io/otel/schema/v1.0/ast"

import "go.opentelemetry.io/otel/schema/types"
import "go.opentelemetry.io/otel/schema/v1.0/types"

// VersionOfSpans corresponds to a section representing a list of changes that happened
// to spans schema in a particular version.
Expand Down
54 changes: 31 additions & 23 deletions schema/parser.go → schema/v1.0/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,34 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package schema // import "go.opentelemetry.io/otel/schema"
package schema // import "go.opentelemetry.io/otel/schema/v1.0"

import (
"fmt"
"io/ioutil"
"net/url"
"strconv"
"strings"

"golang.org/x/mod/semver"
semver "github.com/Masterminds/semver/v3"
"gopkg.in/yaml.v2"

"go.opentelemetry.io/otel/schema/ast"
"go.opentelemetry.io/otel/schema/v1.0/ast"
)

// Major file version number that this library supports.
const supportedFormatMajor = 1

// Maximum minor version number that this library supports.
const supportedFormatMinor = 0

// Maximum major+minor version number that this library supports, as a string.
var supportedFormatMajorMinor = strconv.Itoa(supportedFormatMajor) + "." +
strconv.Itoa(supportedFormatMinor) // 1.0.0

// Parse a schema file.
// schemaFile is the file path.
// maxFileFormatMajorMinorVer specified the maximum major and minor version.
func Parse(schemaFile string) (*ast.Schema, error) {
var ts ast.Schema
schemaContent, err := ioutil.ReadFile(schemaFile)
Expand Down Expand Up @@ -56,39 +70,33 @@ func Parse(schemaFile string) (*ast.Schema, error) {
// checkFileFormatField validates the file format field according to the rules here:
// https://github.com/open-telemetry/oteps/blob/main/text/0152-telemetry-schemas.md#schema-file-format-number
func checkFileFormatField(fileFormat string) error {
if !semver.IsValid("v" + fileFormat) {
// Verify that the version number in the file is a semver.
fileFormatParsed, err := semver.StrictNewVersion(fileFormat)
if err != nil {
return fmt.Errorf(
"invalud schema file format version number %q (expected semver)",
fileFormat,
"invalid schema file format version number %q (expected semver): %w",
fileFormat, err,
)
}

// Check that the major version number is the same as what we expect.
expected := semver.Major("v" + ast.FileFormat)
got := semver.Major("v" + fileFormat)
if expected != got {
expected = strings.TrimLeft(expected, "v")
got = strings.TrimLeft(got, "v")
// Check that the major version number in the file is the same as what we expect.
if fileFormatParsed.Major() != supportedFormatMajor {
return fmt.Errorf(
"unsupported schema file format major version number, expected %v, got %v",
expected, got,
"this library cannot parse file formats with major version other than %v",
supportedFormatMajor,
)
}

// Check that the minor version number is not newer than what we expect.
expected = semver.MajorMinor("v" + ast.FileFormat)

got = semver.MajorMinor("v" + fileFormat)
if expected != got && semver.Compare(expected, got) < 0 {
expected = strings.TrimLeft(expected, "v") + ".x"
got = fileFormat
// Check that the file minor version number is not greater than
// what is requested supports.
if fileFormatParsed.Minor() > supportedFormatMinor {
return fmt.Errorf(
"unsupported schema file format minor version number, expected no newer than %v, got %v",
expected, got,
supportedFormatMajorMinor+".x", fileFormat,
)
}

// Patch version number does not matter, so we don't check it.
// Patch, prerelease and metadata version number does not matter, so we don't check it.

return nil
}
8 changes: 5 additions & 3 deletions schema/parser_test.go → schema/v1.0/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"go.opentelemetry.io/otel/schema/ast"
)

func TestParseSchema(t *testing.T) {
Expand All @@ -40,9 +38,13 @@ func TestFailParseSchema(t *testing.T) {
}

func TestCheckFileFormatField(t *testing.T) {
// Invalid file format version numbers.
assert.Error(t, checkFileFormatField("not a semver"))
assert.Error(t, checkFileFormatField("2.0.0"))
assert.Error(t, checkFileFormatField("1.1.0"))

// Valid cases.
assert.NoError(t, checkFileFormatField("1.0.0"))
assert.NoError(t, checkFileFormatField("1.0.1"))
assert.NoError(t, checkFileFormatField(ast.FileFormat))
assert.NoError(t, checkFileFormatField("1.0.10000-alpha+4857"))
}
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion schema/types/types.go → schema/v1.0/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package types // import "go.opentelemetry.io/otel/schema/types"
package types // import "go.opentelemetry.io/otel/schema/v1.0/types"

type TelemetryVersion string

Expand Down

0 comments on commit 2413b13

Please sign in to comment.