Skip to content

Commit 1297c0e

Browse files
authored
Merge pull request #65 from flipt-io/gm/template-indent
feat(controllers/template): indent JSON output
2 parents 192bb9c + 036395a commit 1297c0e

File tree

4 files changed

+29
-20
lines changed

4 files changed

+29
-20
lines changed

build/build/base.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ func Base(ctx context.Context, client *dagger.Client, opts ...Option) (*dagger.C
6464
WithEnvVariable("GOCACHE", goBuildCachePath).
6565
WithEnvVariable("GOMODCACHE", goModCachePath).
6666
WithExec([]string{"apk", "add", "gcc", "build-base"}).
67-
WithDirectory("/src", client.Host().Directory("."), dagger.ContainerWithDirectoryOpts{
67+
WithDirectory("/src", client.Host().Directory(".", dagger.HostDirectoryOpts{
68+
Exclude: []string{
69+
"./docs/",
70+
},
71+
}), dagger.ContainerWithDirectoryOpts{
6872
Include: []string{
6973
"./build/",
7074
"./cmd/",

pkg/api/server_test.go

+13-16
Original file line numberDiff line numberDiff line change
@@ -242,11 +242,7 @@ func Test_Server_Put(t *testing.T) {
242242
data, err := io.ReadAll(fi)
243243
require.NoError(t, err)
244244

245-
expected := &bytes.Buffer{}
246-
require.NoError(t, json.Compact(expected, []byte(bazPayload)))
247-
expected.Write([]byte{'\n'})
248-
249-
assert.Equal(t, expected.Bytes(), data)
245+
assert.Equal(t, bazPayload, string(data))
250246
}
251247

252248
func Test_Server_Delete(t *testing.T) {
@@ -297,15 +293,16 @@ func Test_Server_Delete(t *testing.T) {
297293
}
298294

299295
const bazPayload = `{
300-
"apiVersion": "test.cup.flipt.io/v1alpha1",
301-
"kind": "Resource",
302-
"metadata": {
303-
"namespace": "default",
304-
"name": "baz",
305-
"labels": {
306-
"foo": "bar"
307-
},
308-
"annotations": {}
296+
"apiVersion": "test.cup.flipt.io/v1alpha1",
297+
"kind": "Resource",
298+
"metadata": {
299+
"namespace": "default",
300+
"name": "baz",
301+
"labels": {
302+
"foo": "bar"
309303
},
310-
"spec": {}
311-
}`
304+
"annotations": {}
305+
},
306+
"spec": {}
307+
}
308+
`

pkg/controllers/template/template.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ type Controller struct {
4646
// By default it uses a JSON encoding which can be overriden via WithResourceEncoding.
4747
func New(opts ...containers.Option[Controller]) *Controller {
4848
controller := &Controller{
49-
encoding: &encoding.JSONEncoding[core.Resource]{},
49+
encoding: &encoding.JSONEncoding[core.Resource]{
50+
Prefix: "",
51+
Indent: " ",
52+
},
5053
nsTmpl: template.Must(template.New("ns").
5154
Funcs(funcs).
5255
Parse(defaultNamespaceTmpl),

pkg/encoding/json.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,19 @@ type JSON[T any] struct {
1010
*json.Decoder
1111
}
1212

13-
type JSONEncoding[T any] struct{}
13+
type JSONEncoding[T any] struct {
14+
Prefix string
15+
Indent string
16+
}
1417

1518
func (j JSONEncoding[T]) Extension() string {
1619
return "json"
1720
}
1821

1922
func (j *JSONEncoding[T]) NewEncoder(r io.Writer) TypedEncoder[T] {
20-
return NewJSONEncoder[T](r)
23+
enc := NewJSONEncoder[T](r)
24+
enc.SetIndent(j.Prefix, j.Indent)
25+
return enc
2126
}
2227

2328
func (j *JSONEncoding[T]) NewDecoder(w io.Reader) TypedDecoder[T] {

0 commit comments

Comments
 (0)