Skip to content

Commit e4ce243

Browse files
committed
cue/parser: factor out source reading into internal package
Change-Id: If592e5d2d8e5191cf8ad8d4a4bfdc5d7e91acebf Reviewed-on: https://cue-review.googlesource.com/c/cue/+/2340 Reviewed-by: Marcel van Lohuizen <[email protected]>
1 parent 5e3a5d4 commit e4ce243

File tree

4 files changed

+60
-37
lines changed

4 files changed

+60
-37
lines changed

cue/parser/error_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import (
4242
"cuelang.org/go/cue/errors"
4343
"cuelang.org/go/cue/scanner"
4444
"cuelang.org/go/cue/token"
45+
"cuelang.org/go/internal/source"
4546
)
4647

4748
const testdata = "testdata"
@@ -148,7 +149,7 @@ func compareErrors(t *testing.T, file *token.File, expected map[token.Pos]string
148149

149150
func checkErrors(t *testing.T, filename string, input interface{}) {
150151
t.Helper()
151-
src, err := readSource(filename, input)
152+
src, err := source.Read(filename, input)
152153
if err != nil {
153154
t.Error(err)
154155
return

cue/parser/interface.go

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,43 +17,11 @@
1717
package parser
1818

1919
import (
20-
"bytes"
21-
"fmt"
22-
"io"
23-
"io/ioutil"
24-
2520
"cuelang.org/go/cue/ast"
2621
"cuelang.org/go/cue/token"
22+
"cuelang.org/go/internal/source"
2723
)
2824

29-
// If src != nil, readSource converts src to a []byte if possible;
30-
// otherwise it returns an error. If src == nil, readSource returns
31-
// the result of reading the file specified by filename.
32-
//
33-
func readSource(filename string, src interface{}) ([]byte, error) {
34-
if src != nil {
35-
switch s := src.(type) {
36-
case string:
37-
return []byte(s), nil
38-
case []byte:
39-
return s, nil
40-
case *bytes.Buffer:
41-
// is io.Reader, but src is already available in []byte form
42-
if s != nil {
43-
return s.Bytes(), nil
44-
}
45-
case io.Reader:
46-
var buf bytes.Buffer
47-
if _, err := io.Copy(&buf, s); err != nil {
48-
return nil, err
49-
}
50-
return buf.Bytes(), nil
51-
}
52-
return nil, fmt.Errorf("invalid source type %T", src)
53-
}
54-
return ioutil.ReadFile(filename)
55-
}
56-
5725
// Option specifies a parse option.
5826
type Option func(p *parser)
5927

@@ -137,7 +105,7 @@ const (
137105
func ParseFile(filename string, src interface{}, mode ...Option) (f *ast.File, err error) {
138106

139107
// get source
140-
text, err := readSource(filename, src)
108+
text, err := source.Read(filename, src)
141109
if err != nil {
142110
return nil, err
143111
}
@@ -181,7 +149,7 @@ func ParseFile(filename string, src interface{}, mode ...Option) (f *ast.File, e
181149
// be nil.
182150
func ParseExpr(filename string, src interface{}, mode ...Option) (ast.Expr, error) {
183151
// get source
184-
text, err := readSource(filename, src)
152+
text, err := source.Read(filename, src)
185153
if err != nil {
186154
return nil, err
187155
}

cue/parser/interface_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"testing"
2020

2121
"cuelang.org/go/cue/ast"
22+
"cuelang.org/go/internal/source"
2223
)
2324

2425
func Test_readSource(t *testing.T) {
@@ -35,7 +36,7 @@ func Test_readSource(t *testing.T) {
3536
// TODO: Add test cases.
3637
}
3738
for _, tt := range tests {
38-
got, err := readSource(tt.args.filename, tt.args.src)
39+
got, err := source.Read(tt.args.filename, tt.args.src)
3940
if (err != nil) != tt.wantErr {
4041
t.Errorf("%q. readSource() error = %v, wantErr %v", tt.name, err, tt.wantErr)
4142
continue

internal/source/source.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2019 CUE Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// Package source contains utility functions that standardize reading source
16+
// bytes across cue packages.
17+
package source
18+
19+
import (
20+
"bytes"
21+
"fmt"
22+
"io"
23+
"io/ioutil"
24+
)
25+
26+
// Read loads the source bytes for the given arguments. If src != nil,
27+
// Read converts src to a []byte if possible; otherwise it returns an
28+
// error. If src == nil, readSource returns the result of reading the file
29+
// specified by filename.
30+
//
31+
func Read(filename string, src interface{}) ([]byte, error) {
32+
if src != nil {
33+
switch s := src.(type) {
34+
case string:
35+
return []byte(s), nil
36+
case []byte:
37+
return s, nil
38+
case *bytes.Buffer:
39+
// is io.Reader, but src is already available in []byte form
40+
if s != nil {
41+
return s.Bytes(), nil
42+
}
43+
case io.Reader:
44+
var buf bytes.Buffer
45+
if _, err := io.Copy(&buf, s); err != nil {
46+
return nil, err
47+
}
48+
return buf.Bytes(), nil
49+
}
50+
return nil, fmt.Errorf("invalid source type %T", src)
51+
}
52+
return ioutil.ReadFile(filename)
53+
}

0 commit comments

Comments
 (0)