2021-10-15 18:44:13 +02:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2024-02-29 08:05:28 +02:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2021-10-15 18:44:13 +02:00
|
|
|
|
|
|
|
package schema // import "go.opentelemetry.io/otel/schema/v1.0"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
|
2023-09-21 12:52:17 +02:00
|
|
|
"gopkg.in/yaml.v3"
|
2021-10-15 18:44:13 +02:00
|
|
|
|
2022-07-25 17:30:23 +02:00
|
|
|
"go.opentelemetry.io/otel/schema/internal"
|
2021-10-15 18:44:13 +02:00
|
|
|
"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
|
|
|
|
|
|
|
|
// ParseFile a schema file. schemaFilePath is the file path.
|
|
|
|
func ParseFile(schemaFilePath string) (*ast.Schema, error) {
|
|
|
|
file, err := os.Open(schemaFilePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return Parse(file)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse a schema file. schemaFileContent is the readable content of the schema file.
|
|
|
|
func Parse(schemaFileContent io.Reader) (*ast.Schema, error) {
|
|
|
|
var ts ast.Schema
|
|
|
|
d := yaml.NewDecoder(schemaFileContent)
|
2023-09-21 12:52:17 +02:00
|
|
|
d.KnownFields(true)
|
2021-10-15 18:44:13 +02:00
|
|
|
err := d.Decode(&ts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-07-25 17:30:23 +02:00
|
|
|
err = internal.CheckFileFormatField(ts.FileFormat, supportedFormatMajor, supportedFormatMinor)
|
|
|
|
if err != nil {
|
2021-10-15 18:44:13 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-07-25 17:30:23 +02:00
|
|
|
err = internal.CheckSchemaURL(ts.SchemaURL)
|
2021-10-15 18:44:13 +02:00
|
|
|
if err != nil {
|
2022-07-25 17:30:23 +02:00
|
|
|
return nil, err
|
2021-10-15 18:44:13 +02:00
|
|
|
}
|
|
|
|
|
2022-07-25 17:30:23 +02:00
|
|
|
return &ts, nil
|
2021-10-15 18:44:13 +02:00
|
|
|
}
|