-
Notifications
You must be signed in to change notification settings - Fork 3
/
file.go
79 lines (71 loc) · 1.83 KB
/
file.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
// Copyright © 2021, SAS Institute Inc., Cary, NC, USA. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package file
import (
"encoding/csv"
"encoding/json"
"os"
"reflect"
"regexp"
"go.uber.org/zap"
)
// File object
type File struct {
Path string
Type string
Schema []string
Content interface{}
}
// Read file
func (f *File) Read() {
zap.S().Debugw("Reading file", "path", f.Path, "type", f.Type)
switch f.Type {
case "csv":
f.readCSV()
case "json":
f.readJSON()
default:
zap.S().Fatalw("Unsupported file type")
}
}
// readJSON opens the JSON file and returns the content
func (f *File) readJSON() {
osf, err := os.OpenFile(f.Path, os.O_RDONLY, 0644)
if err != nil {
zap.S().Fatalw("Error when reading file", "error", err)
}
err = json.NewDecoder(osf).Decode(&f.Content)
if err != nil {
zap.S().Fatalw("Error when unmarshalling JSON file", "error", err)
}
}
// readCSV opens the CSV file and returns the content
func (f *File) readCSV() {
osf, err := os.OpenFile(f.Path, os.O_RDONLY, 0644)
if err != nil {
zap.S().Fatalw("Error when reading file", "error", err)
}
f.Content, err = csv.NewReader(osf).ReadAll()
if err != nil {
zap.S().Fatalw("Error when unmarshalling CSV file", "error", err)
}
if !f.checkHeader() {
zap.S().Fatalw("Header row does not match expected schema", "header", f.Content.([][]string)[0], "schema", f.Schema)
}
}
// checkHeader validates the file header against the provided schema
func (f *File) checkHeader() bool {
var header []string
for _, col := range f.Content.([][]string)[0] {
clean, err := regexp.Compile("[^a-zA-Z0-9]+")
if err != nil {
zap.S().Errorw("Error compiling regular expression", "error", err)
}
header = append(header, clean.ReplaceAllString(col, ""))
}
if reflect.DeepEqual(header, f.Schema) {
return true
} else {
return false
}
}