pkg/output/decorator.go   A
last analyzed

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
cc 3
eloc 17
dl 0
loc 33
ccs 6
cts 6
cp 1
crap 3
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A output.FormatDecorator.Decorate 0 6 2
A output.ImportDecorator.Decorate 0 4 1
1
package output
2
3
import (
4
	"fmt"
5
	"go/format"
6
	"strings"
7
)
8
9
// Decorator represents an interface to decorate the given content.
10
type Decorator interface {
11
	Decorate(content string) (string, error)
12
}
13
14
// FormatDecorator applies a formatting decoration to the given content.
15
type FormatDecorator struct{}
16
17
// Decorate is the implementation of the Decorator interface.
18
func (FormatDecorator) Decorate(content string) (string, error) {
19 1
	formatted, err := format.Source([]byte(content))
20 1
	if err != nil {
21 1
		return content, fmt.Errorf("could not format content: %v", err)
22
	}
23 1
	return string(formatted), nil
24
}
25
26
// ImportDecorator removes empty import statements from the given content.
27
type ImportDecorator struct{}
28
29
// Decorate is the implementation of the Decorator interface.
30
func (ImportDecorator) Decorate(content string) (string, error) {
31
	// fight the symptom instead of the cause - if we didnt imported anything, remove it
32 1
	decorated := strings.ReplaceAll(content, "\nimport ()\n", "")
33 1
	return decorated, nil
34
}
35