15
15
package schema // import "go.opentelemetry.io/otel/schema/v1.0"
16
16
17
17
import (
18
- "fmt"
19
18
"io"
20
- "net/url"
21
19
"os"
22
- "strconv"
23
- "strings"
24
20
25
- "github.com/Masterminds/semver/v3"
26
21
"gopkg.in/yaml.v2"
27
22
23
+ "go.opentelemetry.io/otel/schema/internal"
28
24
"go.opentelemetry.io/otel/schema/v1.0/ast"
29
25
)
30
26
@@ -34,10 +30,6 @@ const supportedFormatMajor = 1
34
30
// Maximum minor version number that this library supports.
35
31
const supportedFormatMinor = 0
36
32
37
- // Maximum major+minor version number that this library supports, as a string.
38
- var supportedFormatMajorMinor = strconv .Itoa (supportedFormatMajor ) + "." +
39
- strconv .Itoa (supportedFormatMinor ) // 1.0
40
-
41
33
// ParseFile a schema file. schemaFilePath is the file path.
42
34
func ParseFile (schemaFilePath string ) (* ast.Schema , error ) {
43
35
file , err := os .Open (schemaFilePath )
@@ -56,51 +48,15 @@ func Parse(schemaFileContent io.Reader) (*ast.Schema, error) {
56
48
return nil , err
57
49
}
58
50
59
- if err := checkFileFormatField (ts .FileFormat ); err != nil {
51
+ err = internal .CheckFileFormatField (ts .FileFormat , supportedFormatMajor , supportedFormatMinor )
52
+ if err != nil {
60
53
return nil , err
61
54
}
62
55
63
- if strings .TrimSpace (ts .SchemaURL ) == "" {
64
- return nil , fmt .Errorf ("schema_url field is missing" )
65
- }
66
-
67
- if _ , err := url .Parse (ts .SchemaURL ); err != nil {
68
- return nil , fmt .Errorf ("invalid URL specified in schema_url field: %w" , err )
69
- }
70
-
71
- return & ts , nil
72
- }
73
-
74
- // checkFileFormatField validates the file format field according to the rules here:
75
- // https://github.com/open-telemetry/oteps/blob/main/text/0152-telemetry-schemas.md#schema-file-format-number
76
- func checkFileFormatField (fileFormat string ) error {
77
- // Verify that the version number in the file is a semver.
78
- fileFormatParsed , err := semver .StrictNewVersion (fileFormat )
56
+ err = internal .CheckSchemaURL (ts .SchemaURL )
79
57
if err != nil {
80
- return fmt .Errorf (
81
- "invalid schema file format version number %q (expected semver): %w" ,
82
- fileFormat , err ,
83
- )
84
- }
85
-
86
- // Check that the major version number in the file is the same as what we expect.
87
- if fileFormatParsed .Major () != supportedFormatMajor {
88
- return fmt .Errorf (
89
- "this library cannot parse file formats with major version other than %v" ,
90
- supportedFormatMajor ,
91
- )
92
- }
93
-
94
- // Check that the file minor version number is not greater than
95
- // what is requested supports.
96
- if fileFormatParsed .Minor () > supportedFormatMinor {
97
- return fmt .Errorf (
98
- "unsupported schema file format minor version number, expected no newer than %v, got %v" ,
99
- supportedFormatMajorMinor + ".x" , fileFormat ,
100
- )
58
+ return nil , err
101
59
}
102
60
103
- // Patch, prerelease and metadata version number does not matter, so we don't check it.
104
-
105
- return nil
61
+ return & ts , nil
106
62
}
0 commit comments