-
Notifications
You must be signed in to change notification settings - Fork 30
/
read.go
167 lines (149 loc) · 3.51 KB
/
read.go
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package std
import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"os"
"path"
"golang.org/x/text/encoding/unicode"
"github.com/jkcfg/jk/pkg/__std"
"github.com/jkcfg/jk/pkg/record"
"github.com/ghodss/yaml"
yamlclassic "gopkg.in/yaml.v2"
)
func readYAML(in io.Reader) ([]byte, error) {
bytes, err := ioutil.ReadAll(in)
if err != nil {
return nil, err
}
return yaml.YAMLToJSON(bytes)
}
func readYAMLStream(in io.Reader) ([]byte, error) {
// This specific one is tricky: since YAML can have more than just
// strings as map keys, the decoder may return a
// `map[interface{}]interface{}`, which can't be directly encoded
// in JSON.
//
// ghodss/yaml has a conversion function, from YAML bytes to JSON
// butes -- so we have to _un_parse the value we just got, to put
// it through the conversion.
decoder := yamlclassic.NewDecoder(in)
var yamlvalues []interface{}
for {
var v interface{}
err := decoder.Decode(&v)
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
yamlvalues = append(yamlvalues, v)
}
yamlbytes, err := yamlclassic.Marshal(yamlvalues)
if err != nil {
return nil, err
}
return yaml.YAMLToJSON(yamlbytes)
}
func readJSON(in io.Reader) ([]byte, error) {
var buf bytes.Buffer
tee := io.TeeReader(in, &buf)
var obj interface{}
if err := json.NewDecoder(tee).Decode(&obj); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func readJSONStream(in io.Reader) ([]byte, error) {
decoder := json.NewDecoder(in)
var items []interface{}
for {
var v interface{}
err := decoder.Decode(&v)
if err == nil {
items = append(items, v)
continue
}
if err == io.EOF {
break
}
return nil, err
}
return json.Marshal(items)
}
func readRaw(in io.Reader) ([]byte, error) {
return ioutil.ReadAll(in)
}
type readFunc func(io.Reader) ([]byte, error)
func readerByPath(p string) readFunc {
switch path.Ext(p) {
case ".yml", ".yaml":
return readYAML
case ".json":
return readJSON
}
return readJSON
}
// Read returns the contents of the file at `path`, relative to the
// root path or if given, the module directory identified by `module`.
func (r Sandbox) Read(relPath string, format __std.Format, encoding __std.Encoding, module string) ([]byte, error) {
// Special case for reading from stdin
if relPath == "" {
return read(nil, "", format, encoding)
}
loc, err := r.getReadPath(relPath, module)
if err != nil {
return nil, err
}
if r.Recorder != nil {
r.Recorder.Record(record.ReadFile, record.Params{
"path": loc.Vfs.QualifyPath(loc.Path),
})
}
return read(loc.Vfs, loc.Path, format, encoding)
}
func read(vfs http.FileSystem, p string, format __std.Format, encoding __std.Encoding) ([]byte, error) {
var reader readFunc = readRaw
if encoding == __std.EncodingJSON {
switch format {
case __std.FormatFromExtension:
reader = readerByPath(p)
case __std.FormatYAML:
reader = readYAML
case __std.FormatYAMLStream:
reader = readYAMLStream
case __std.FormatJSON:
reader = readJSON
case __std.FormatJSONStream:
reader = readJSONStream
default:
reader = readJSON
}
}
var in io.Reader
if p == "" {
in = os.Stdin
} else {
f, err := vfs.Open(p)
if err != nil {
return nil, err
}
defer f.Close()
in = f
}
bytes, err := reader(in)
if err != nil {
return nil, err
}
switch encoding {
case __std.EncodingBytes:
break
case __std.EncodingString, __std.EncodingJSON:
encoder := unicode.UTF16(NativeEndian, unicode.IgnoreBOM).NewEncoder()
bytes, err = encoder.Bytes(bytes)
}
return bytes, err
}