Skip to content

Commit 758fd75

Browse files
committed
feat: output formaters
1 parent 5a414eb commit 758fd75

1 file changed

Lines changed: 51 additions & 0 deletions

File tree

internal/formaters.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package internal
2+
3+
import (
4+
"encoding/json"
5+
6+
"go.yaml.in/yaml/v2"
7+
)
8+
9+
type OutputFormater[T any] interface {
10+
Format(input T) (string, error)
11+
}
12+
13+
type YamlOutputFormater struct{}
14+
15+
var _ OutputFormater[any] = (*YamlOutputFormater)(nil)
16+
17+
func (f *YamlOutputFormater) Format(input any) (string, error) {
18+
yamled, err := yaml.Marshal(input)
19+
if err != nil {
20+
return "", err
21+
}
22+
return string(yamled), nil
23+
}
24+
25+
type JsonOutputFormater struct{}
26+
27+
var _ OutputFormater[any] = (*JsonOutputFormater)(nil)
28+
29+
func (f *JsonOutputFormater) Format(input any) (string, error) {
30+
jsoned, err := json.MarshalIndent(input, "", "\t")
31+
if err != nil {
32+
return "", err
33+
}
34+
return string(jsoned), nil
35+
}
36+
37+
type ArrayOutputFormater struct{}
38+
39+
var _ OutputFormater[ArrayInput] = (*ArrayOutputFormater)(nil)
40+
41+
type ArrayInput struct {
42+
Header []string
43+
Content [][]string
44+
}
45+
func (f *ArrayOutputFormater) Format(input ArrayInput) (string, error) {
46+
jsoned, err := json.MarshalIndent(input, "", "\t")
47+
if err != nil {
48+
return "", err
49+
}
50+
return string(jsoned), nil
51+
}

0 commit comments

Comments
 (0)