From ef5fb6e3201877ec2bd4191b828b4164fccc86ec Mon Sep 17 00:00:00 2001 From: George MacRorie Date: Tue, 12 Sep 2023 16:56:47 +0100 Subject: [PATCH 1/2] feat(controllers/template): indent JSON output --- pkg/controllers/template/template.go | 5 ++++- pkg/encoding/json.go | 9 +++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/pkg/controllers/template/template.go b/pkg/controllers/template/template.go index 68755d8..9218b0e 100644 --- a/pkg/controllers/template/template.go +++ b/pkg/controllers/template/template.go @@ -46,7 +46,10 @@ type Controller struct { // By default it uses a JSON encoding which can be overriden via WithResourceEncoding. func New(opts ...containers.Option[Controller]) *Controller { controller := &Controller{ - encoding: &encoding.JSONEncoding[core.Resource]{}, + encoding: &encoding.JSONEncoding[core.Resource]{ + Prefix: "", + Indent: " ", + }, nsTmpl: template.Must(template.New("ns"). Funcs(funcs). Parse(defaultNamespaceTmpl), diff --git a/pkg/encoding/json.go b/pkg/encoding/json.go index a57930b..9943e87 100644 --- a/pkg/encoding/json.go +++ b/pkg/encoding/json.go @@ -10,14 +10,19 @@ type JSON[T any] struct { *json.Decoder } -type JSONEncoding[T any] struct{} +type JSONEncoding[T any] struct { + Prefix string + Indent string +} func (j JSONEncoding[T]) Extension() string { return "json" } func (j *JSONEncoding[T]) NewEncoder(r io.Writer) TypedEncoder[T] { - return NewJSONEncoder[T](r) + enc := NewJSONEncoder[T](r) + enc.SetIndent(j.Prefix, j.Indent) + return enc } func (j *JSONEncoding[T]) NewDecoder(w io.Reader) TypedDecoder[T] { From 036395a3f4b6221b3b9e5a783ff1134f36816e07 Mon Sep 17 00:00:00 2001 From: George MacRorie Date: Tue, 12 Sep 2023 17:18:45 +0100 Subject: [PATCH 2/2] fix(api/server_test): ensure new indented format --- build/build/base.go | 6 +++++- pkg/api/server_test.go | 29 +++++++++++++---------------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/build/build/base.go b/build/build/base.go index 2e109e8..82588e5 100644 --- a/build/build/base.go +++ b/build/build/base.go @@ -64,7 +64,11 @@ func Base(ctx context.Context, client *dagger.Client, opts ...Option) (*dagger.C WithEnvVariable("GOCACHE", goBuildCachePath). WithEnvVariable("GOMODCACHE", goModCachePath). WithExec([]string{"apk", "add", "gcc", "build-base"}). - WithDirectory("/src", client.Host().Directory("."), dagger.ContainerWithDirectoryOpts{ + WithDirectory("/src", client.Host().Directory(".", dagger.HostDirectoryOpts{ + Exclude: []string{ + "./docs/", + }, + }), dagger.ContainerWithDirectoryOpts{ Include: []string{ "./build/", "./cmd/", diff --git a/pkg/api/server_test.go b/pkg/api/server_test.go index cd4951a..cdb3112 100644 --- a/pkg/api/server_test.go +++ b/pkg/api/server_test.go @@ -242,11 +242,7 @@ func Test_Server_Put(t *testing.T) { data, err := io.ReadAll(fi) require.NoError(t, err) - expected := &bytes.Buffer{} - require.NoError(t, json.Compact(expected, []byte(bazPayload))) - expected.Write([]byte{'\n'}) - - assert.Equal(t, expected.Bytes(), data) + assert.Equal(t, bazPayload, string(data)) } func Test_Server_Delete(t *testing.T) { @@ -297,15 +293,16 @@ func Test_Server_Delete(t *testing.T) { } const bazPayload = `{ - "apiVersion": "test.cup.flipt.io/v1alpha1", - "kind": "Resource", - "metadata": { - "namespace": "default", - "name": "baz", - "labels": { - "foo": "bar" - }, - "annotations": {} + "apiVersion": "test.cup.flipt.io/v1alpha1", + "kind": "Resource", + "metadata": { + "namespace": "default", + "name": "baz", + "labels": { + "foo": "bar" }, - "spec": {} -}` + "annotations": {} + }, + "spec": {} +} +`