output.ImportDecorator.Decorate   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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