File: parser_checks.go

package info (click to toggle)
golang-opentelemetry-otel 1.31.0-5
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid
  • size: 11,844 kB
  • sloc: makefile: 237; sh: 51
file content (70 lines) | stat: -rw-r--r-- 2,282 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package internal // import "go.opentelemetry.io/otel/schema/internal"

import (
	"errors"
	"fmt"
	"net/url"
	"strconv"
	"strings"

	"github.com/Masterminds/semver/v3"
)

// 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, supportedFormatMajor, supportedFormatMinor int) error {
	// Verify that the version number in the file is a semver.
	fileFormatParsed, err := semver.StrictNewVersion(fileFormat)
	if err != nil {
		return fmt.Errorf(
			"invalid schema file format version number %q (expected semver): %w",
			fileFormat, err,
		)
	}

	if supportedFormatMajor < 0 {
		return errors.New("major version should be positive")
	}
	if supportedFormatMinor < 0 {
		return errors.New("major version should be positive")
	}

	// Check that the major version number in the file is the same as what we expect.
	if fileFormatParsed.Major() != uint64(supportedFormatMajor) { // nolint:gosec // Version can't be negative (overflow checked).
		return fmt.Errorf(
			"this library cannot parse file formats with major version other than %v",
			supportedFormatMajor,
		)
	}

	// Check that the file minor version number is not greater than
	// what is requested supports.
	if fileFormatParsed.Minor() > uint64(supportedFormatMinor) { // nolint:gosec // Version can't be negative (overflow checked).
		supportedFormatMajorMinor := strconv.Itoa(supportedFormatMajor) + "." +
			strconv.Itoa(supportedFormatMinor) // 1.0

		return fmt.Errorf(
			"unsupported schema file format minor version number, expected no newer than %v, got %v",
			supportedFormatMajorMinor+".x", fileFormat,
		)
	}

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

	return nil
}

// CheckSchemaURL verifies that schemaURL is valid.
func CheckSchemaURL(schemaURL string) error {
	if strings.TrimSpace(schemaURL) == "" {
		return errors.New("schema_url field is missing")
	}

	if _, err := url.Parse(schemaURL); err != nil {
		return fmt.Errorf("invalid URL specified in schema_url field: %w", err)
	}
	return nil
}