| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
// Package schema provides functionality and types for OpenTelemetry schemas. |
| 5 |
package schema // import "go.opentelemetry.io/otel/schema/v1.1" |
| 6 |
|
| 7 |
import ( |
| 8 |
"io" |
| 9 |
"os" |
| 10 |
|
| 11 |
"gopkg.in/yaml.v3" |
| 12 |
|
| 13 |
"go.opentelemetry.io/otel/schema/internal" |
| 14 |
"go.opentelemetry.io/otel/schema/v1.1/ast" |
| 15 |
) |
| 16 |
|
| 17 |
// Major file version number that this library supports. |
| 18 |
const supportedFormatMajor = 1 |
| 19 |
|
| 20 |
// Maximum minor version number that this library supports. |
| 21 |
const supportedFormatMinor = 1 |
| 22 |
|
| 23 |
// ParseFile a schema file. schemaFilePath is the file path. |
| 24 |
func ParseFile(schemaFilePath string) (*ast.Schema, error) { |
| 25 |
file, err := os.Open(schemaFilePath) |
| 26 |
if err != nil { |
| 27 |
return nil, err |
| 28 |
} |
| 29 |
return Parse(file) |
| 30 |
} |
| 31 |
|
| 32 |
// Parse a schema file. schemaFileContent is the readable content of the schema file. |
| 33 |
func Parse(schemaFileContent io.Reader) (*ast.Schema, error) { |
| 34 |
var ts ast.Schema |
| 35 |
d := yaml.NewDecoder(schemaFileContent) |
| 36 |
d.KnownFields(true) |
| 37 |
err := d.Decode(&ts) |
| 38 |
if err != nil { |
| 39 |
return nil, err |
| 40 |
} |
| 41 |
|
| 42 |
err = internal.CheckFileFormatField(ts.FileFormat, supportedFormatMajor, supportedFormatMinor) |
| 43 |
if err != nil { |
| 44 |
return nil, err |
| 45 |
} |
| 46 |
|
| 47 |
err = internal.CheckSchemaURL(ts.SchemaURL) |
| 48 |
if err != nil { |
| 49 |
return nil, err |
| 50 |
} |
| 51 |
|
| 52 |
return &ts, nil |
| 53 |
} |