Debian Codegraph @

schema/internal/parser_checks.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 // Package internal provides internal functionality for the schema package.
5 package internal // import "go.opentelemetry.io/otel/schema/internal"
6
7 import (
8 "errors"
9 "fmt"
10 "net/url"
11 "strconv"
12 "strings"
13
14 "github.com/Masterminds/semver/v3"
15 )
16
17 // CheckFileFormatField validates the file format field according to the rules here:
18 // https://github.com/open-telemetry/oteps/blob/main/text/0152-telemetry-schemas.md#schema-file-format-number
19 func CheckFileFormatField(fileFormat string, supportedFormatMajor, supportedFormatMinor int) error {
20 // Verify that the version number in the file is a semver.
21 fileFormatParsed, err := semver.StrictNewVersion(fileFormat)
22 if err != nil {
23 return fmt.Errorf(
24 "invalid schema file format version number %q (expected semver): %w",
25 fileFormat, err,
26 )
27 }
28
29 if supportedFormatMajor < 0 {
30 return errors.New("major version should be positive")
31 }
32 if supportedFormatMinor < 0 {
33 return errors.New("major version should be positive")
34 }
35
36 // Check that the major version number in the file is the same as what we expect.
37 if fileFormatParsed.Major() != uint64(
38 supportedFormatMajor,
39 ) { // nolint:gosec // Version can't be negative (overflow checked).
40 return fmt.Errorf(
41 "this library cannot parse file formats with major version other than %v",
42 supportedFormatMajor,
43 )
44 }
45
46 // Check that the file minor version number is not greater than
47 // what is requested supports.
48 if fileFormatParsed.Minor() > uint64(
49 supportedFormatMinor,
50 ) { // nolint:gosec // Version can't be negative (overflow checked).
51 supportedFormatMajorMinor := strconv.Itoa(supportedFormatMajor) + "." +
52 strconv.Itoa(supportedFormatMinor) // 1.0
53
54 return fmt.Errorf(
55 "unsupported schema file format minor version number, expected no newer than %v, got %v",
56 supportedFormatMajorMinor+".x", fileFormat,
57 )
58 }
59
60 // Patch, prerelease and metadata version number does not matter, so we don't check it.
61
62 return nil
63 }
64
65 // CheckSchemaURL verifies that schemaURL is valid.
66 func CheckSchemaURL(schemaURL string) error {
67 if strings.TrimSpace(schemaURL) == "" {
68 return errors.New("schema_url field is missing")
69 }
70
71 if _, err := url.Parse(schemaURL); err != nil {
72 return fmt.Errorf("invalid URL specified in schema_url field: %w", err)
73 }
74 return nil
75 }