Skip to content

Commit

Permalink
Accept io.Reader in Parse()
Browse files Browse the repository at this point in the history
  • Loading branch information
tigrannajaryan committed Oct 4, 2021
1 parent 5107269 commit f9bd5f0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
19 changes: 15 additions & 4 deletions schema/v1.0/parser.go
Expand Up @@ -16,12 +16,14 @@ package schema // import "go.opentelemetry.io/otel/schema/v1.0"

import (
"fmt"
"io"
"io/ioutil"
"net/url"
"os"
"strconv"
"strings"

semver "github.com/Masterminds/semver/v3"
"github.com/Masterminds/semver/v3"
"gopkg.in/yaml.v2"

"go.opentelemetry.io/otel/schema/v1.0/ast"
Expand All @@ -37,10 +39,19 @@ const supportedFormatMinor = 0
var supportedFormatMajorMinor = strconv.Itoa(supportedFormatMajor) + "." +
strconv.Itoa(supportedFormatMinor) // 1.0

// Parse a schema file. schemaFile is the file path.
func Parse(schemaFile string) (*ast.Schema, error) {
// 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
schemaContent, err := ioutil.ReadFile(schemaFile)
schemaContent, err := ioutil.ReadAll(schemaFileContent)
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions schema/v1.0/parser_test.go
Expand Up @@ -22,17 +22,17 @@ import (
)

func TestParseSchema(t *testing.T) {
ts, err := Parse("testdata/valid-example.yaml")
ts, err := ParseFile("testdata/valid-example.yaml")
require.NoError(t, err)
require.NotNil(t, ts)
}

func TestFailParseSchema(t *testing.T) {
ts, err := Parse("testdata/unsupported-file-format.yaml")
ts, err := ParseFile("testdata/unsupported-file-format.yaml")
require.Error(t, err)
require.Nil(t, ts)

ts, err = Parse("testdata/invalid-schema-url.yaml")
ts, err = ParseFile("testdata/invalid-schema-url.yaml")
require.Error(t, err)
require.Nil(t, ts)
}
Expand Down

0 comments on commit f9bd5f0

Please sign in to comment.