Total Lines | 33 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 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 |