pkg/output/writer.go   A
last analyzed

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
cc 6
eloc 28
dl 0
loc 57
ccs 11
cts 11
cp 1
crap 6
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A output.NewFileWriter 0 6 1
A output.FileWriter.Write 0 9 2
A output.FileWriter.decorate 0 9 3
1
package output
2
3
import (
4
	"io/ioutil"
5
	"path"
6
)
7
8
const (
9
	// FileWriterExtension is the extension to write files of.
10
	FileWriterExtension = ".go"
11
)
12
13
// Writer represents an interface to write the produced struct content.
14
type Writer interface {
15
	Write(tableName string, content string) error
16
}
17
18
// FileWriter is a writer that writes to a file given by the path and the table name.
19
type FileWriter struct {
20
	path       string
21
	decorators []Decorator
22
}
23
24
// NewFileWriter constructs a new FileWriter.
25
func NewFileWriter(path string) *FileWriter {
26 1
	return &FileWriter{
27
		path: path,
28
		decorators: []Decorator{
29
			FormatDecorator{},
30
			ImportDecorator{},
31
		},
32
	}
33
}
34
35
// Write is the implementation of the Writer interface. The FilerWriter writes
36
// decorated content to the file specified by the given path and table name.
37
func (w FileWriter) Write(tableName string, content string) error {
38 1
	fileName := path.Join(w.path, tableName+FileWriterExtension)
39
40 1
	decorated, err := w.decorate(content)
41 1
	if err != nil {
42 1
		return err
43
	}
44
45 1
	return ioutil.WriteFile(fileName, []byte(decorated), 0666)
46
}
47
48
// decorate applies some decorations like formatting and empty import removal.
49
func (w FileWriter) decorate(content string) (decorated string, err error) {
50 1
	for _, decorator := range w.decorators {
51 1
		content, err = decorator.Decorate(content)
52 1
		if err != nil {
53 1
			return content, err
54
		}
55
	}
56
57 1
	return content, nil
58
}
59