From 2ef48afd3b11e73a288c96797f68c3d25cd718b9 Mon Sep 17 00:00:00 2001 From: Mateusz Hawrus <48822818+nieomylnieja@users.noreply.github.com> Date: Tue, 18 Jun 2024 10:36:22 +0200 Subject: [PATCH] chore: Change objects print to encode (#463) ## Motivation Print was a poor choice of wording and might be confusing. A better alternative would be to change the naming of these functions to "encode" instead. It will also serve as a clear reverse function for `DecodeObjects`. The print functions are marked as deprecated and will be removed in the future. --- sdk/{parser.go => decode.go} | 0 sdk/{parser_test.go => decode_test.go} | 6 +- sdk/{printer.go => encode.go} | 22 ++++- sdk/{printer_test.go => encode_test.go} | 96 +++++++++++++++++-- .../compacted_list_of_objects.json | 0 .../compacted_single_object.json | 0 .../{parser => decode}/list_of_objects.json | 0 .../{parser => decode}/list_of_objects.yaml | 0 .../list_of_objects_with_comments.yaml | 0 .../list_of_objects_with_whitespace.json | 0 .../list_of_objects_with_whitespace.yaml | 0 .../multiline_double_quoted_description.yaml | 0 ...oted_description_square_bracket_array.yaml | 0 .../multiple_documents.yaml | 0 .../{parser => decode}/single_document.yaml | 0 ...gle_document_with_document_separators.yaml | 0 .../{parser => decode}/single_object.json | 0 .../{parser => decode}/single_project.yaml | 0 .../{parser => decode}/two_projects.yaml | 0 .../{printer => encode}/expected_object.json | 0 .../{printer => encode}/expected_object.yaml | 0 .../{printer => encode}/expected_objects.json | 0 .../{printer => encode}/expected_objects.yaml | 0 23 files changed, 108 insertions(+), 16 deletions(-) rename sdk/{parser.go => decode.go} (100%) rename sdk/{parser_test.go => decode_test.go} (97%) rename sdk/{printer.go => encode.go} (54%) rename sdk/{printer_test.go => encode_test.go} (51%) rename sdk/test_data/{parser => decode}/compacted_list_of_objects.json (100%) rename sdk/test_data/{parser => decode}/compacted_single_object.json (100%) rename sdk/test_data/{parser => decode}/list_of_objects.json (100%) rename sdk/test_data/{parser => decode}/list_of_objects.yaml (100%) rename sdk/test_data/{parser => decode}/list_of_objects_with_comments.yaml (100%) rename sdk/test_data/{parser => decode}/list_of_objects_with_whitespace.json (100%) rename sdk/test_data/{parser => decode}/list_of_objects_with_whitespace.yaml (100%) rename sdk/test_data/{parser => decode}/multiline_double_quoted_description.yaml (100%) rename sdk/test_data/{parser => decode}/multiline_double_quoted_description_square_bracket_array.yaml (100%) rename sdk/test_data/{parser => decode}/multiple_documents.yaml (100%) rename sdk/test_data/{parser => decode}/single_document.yaml (100%) rename sdk/test_data/{parser => decode}/single_document_with_document_separators.yaml (100%) rename sdk/test_data/{parser => decode}/single_object.json (100%) rename sdk/test_data/{parser => decode}/single_project.yaml (100%) rename sdk/test_data/{parser => decode}/two_projects.yaml (100%) rename sdk/test_data/{printer => encode}/expected_object.json (100%) rename sdk/test_data/{printer => encode}/expected_object.yaml (100%) rename sdk/test_data/{printer => encode}/expected_objects.json (100%) rename sdk/test_data/{printer => encode}/expected_objects.yaml (100%) diff --git a/sdk/parser.go b/sdk/decode.go similarity index 100% rename from sdk/parser.go rename to sdk/decode.go diff --git a/sdk/parser_test.go b/sdk/decode_test.go similarity index 97% rename from sdk/parser_test.go rename to sdk/decode_test.go index bda0ebe2..025d76a6 100644 --- a/sdk/parser_test.go +++ b/sdk/decode_test.go @@ -17,8 +17,8 @@ import ( v1alphaService "github.com/nobl9/nobl9-go/manifest/v1alpha/service" ) -//go:embed test_data/parser -var parserTestData embed.FS +//go:embed test_data/decode +var decodeTestData embed.FS func TestDecode(t *testing.T) { for _, test := range []struct { @@ -198,7 +198,7 @@ func TestDecodeSingle(t *testing.T) { func readInputFile(t *testing.T, name string) []byte { t.Helper() - data, err := parserTestData.ReadFile(filepath.Join("test_data", "parser", name)) + data, err := decodeTestData.ReadFile(filepath.Join("test_data", "decode", name)) require.NoError(t, err) return data } diff --git a/sdk/printer.go b/sdk/encode.go similarity index 54% rename from sdk/printer.go rename to sdk/encode.go index c1b0b657..01373977 100644 --- a/sdk/printer.go +++ b/sdk/encode.go @@ -10,17 +10,29 @@ import ( "github.com/nobl9/nobl9-go/manifest" ) -// PrintObjects prints objects to the given [io.Writer] in specified [manifest.ObjectFormat]. +// EncodeObjects writes objects to the given [io.Writer] in the specified [manifest.ObjectFormat]. +func EncodeObjects(objects []manifest.Object, out io.Writer, format manifest.ObjectFormat) error { + return encodeObjects(objects, out, format) +} + +// EncodeObject writes a single object to the given [io.Writer] in the specified [manifest.ObjectFormat]. +func EncodeObject(object manifest.Object, out io.Writer, format manifest.ObjectFormat) error { + return encodeObjects(object, out, format) +} + +// PrintObjects prints objects to the given [io.Writer] in the specified [manifest.ObjectFormat]. +// Deprecated: Use EncodeObjects instead. func PrintObjects(objects []manifest.Object, out io.Writer, format manifest.ObjectFormat) error { - return printObjects(objects, out, format) + return encodeObjects(objects, out, format) } -// PrintObject prints a single object to the given [io.Writer] in specified [manifest.ObjectFormat]. +// PrintObject prints a single object to the given [io.Writer] in the specified [manifest.ObjectFormat]. +// Deprecated: Use EncodeObject instead. func PrintObject(object manifest.Object, out io.Writer, format manifest.ObjectFormat) error { - return printObjects(object, out, format) + return encodeObjects(object, out, format) } -func printObjects(objects any, out io.Writer, format manifest.ObjectFormat) error { +func encodeObjects(objects any, out io.Writer, format manifest.ObjectFormat) error { switch format { case manifest.ObjectFormatJSON: enc := json.NewEncoder(out) diff --git a/sdk/printer_test.go b/sdk/encode_test.go similarity index 51% rename from sdk/printer_test.go rename to sdk/encode_test.go index 5da47e4d..d4e8d9e3 100644 --- a/sdk/printer_test.go +++ b/sdk/encode_test.go @@ -12,13 +12,13 @@ import ( v1alphaProject "github.com/nobl9/nobl9-go/manifest/v1alpha/project" ) -//go:embed test_data/printer/expected_objects.json +//go:embed test_data/encode/expected_objects.json var expectedObjectsJSON string -//go:embed test_data/printer/expected_objects.yaml +//go:embed test_data/encode/expected_objects.yaml var expectedObjectsYAML string -func TestPrintObjects(t *testing.T) { +func TestEncodeObjects(t *testing.T) { objects := []manifest.Object{ v1alpha.GenericObject{ "apiVersion": "v1alpha", @@ -46,32 +46,112 @@ func TestPrintObjects(t *testing.T) { t.Run("JSON format", func(t *testing.T) { buf := &bytes.Buffer{} - err := PrintObjects(objects, buf, manifest.ObjectFormatJSON) + err := EncodeObjects(objects, buf, manifest.ObjectFormatJSON) assert.NoError(t, err) assert.Equal(t, expectedObjectsJSON, buf.String()) }) t.Run("YAML format", func(t *testing.T) { buf := &bytes.Buffer{} - err := PrintObjects(objects, buf, manifest.ObjectFormatYAML) + err := EncodeObjects(objects, buf, manifest.ObjectFormatYAML) assert.NoError(t, err) assert.Equal(t, expectedObjectsYAML, buf.String()) }) t.Run("Unsupported format", func(t *testing.T) { buf := &bytes.Buffer{} - err := PrintObjects(objects, buf, manifest.ObjectFormat(-1)) + err := EncodeObjects(objects, buf, manifest.ObjectFormat(-1)) assert.Error(t, err) assert.Equal(t, "unsupported format: ObjectFormat(-1)", err.Error()) }) } -//go:embed test_data/printer/expected_object.json +//go:embed test_data/encode/expected_object.json var expectedObjectJSON string -//go:embed test_data/printer/expected_object.yaml +//go:embed test_data/encode/expected_object.yaml var expectedObjectYAML string +func TestEncodeObject(t *testing.T) { + object := v1alpha.GenericObject{ + "apiVersion": "v1alpha", + "kind": "Project", + "metadata": map[string]interface{}{ + "name": "test-int", + "value": 1, + }, + } + + t.Run("JSON format", func(t *testing.T) { + buf := &bytes.Buffer{} + err := EncodeObject(object, buf, manifest.ObjectFormatJSON) + assert.NoError(t, err) + assert.Equal(t, expectedObjectJSON, buf.String()) + }) + + t.Run("YAML format", func(t *testing.T) { + buf := &bytes.Buffer{} + err := EncodeObject(object, buf, manifest.ObjectFormatYAML) + assert.NoError(t, err) + assert.Equal(t, expectedObjectYAML, buf.String()) + }) + + t.Run("Unsupported format", func(t *testing.T) { + buf := &bytes.Buffer{} + err := EncodeObject(object, buf, manifest.ObjectFormat(-1)) + assert.Error(t, err) + assert.Equal(t, "unsupported format: ObjectFormat(-1)", err.Error()) + }) +} + +func TestPrintObjects(t *testing.T) { + objects := []manifest.Object{ + v1alpha.GenericObject{ + "apiVersion": "v1alpha", + "kind": "Project", + "metadata": map[string]interface{}{ + "name": "test-int", + "value": 1, + }, + }, + v1alpha.GenericObject{ + "apiVersion": "v1alpha", + "kind": "Project", + "metadata": map[string]interface{}{ + "name": "test-float", + "value": 2.89, + }, + }, + v1alphaProject.New( + v1alphaProject.Metadata{ + Name: "test-project", + }, + v1alphaProject.Spec{}, + ), + } + + t.Run("JSON format", func(t *testing.T) { + buf := &bytes.Buffer{} + err := PrintObjects(objects, buf, manifest.ObjectFormatJSON) + assert.NoError(t, err) + assert.Equal(t, expectedObjectsJSON, buf.String()) + }) + + t.Run("YAML format", func(t *testing.T) { + buf := &bytes.Buffer{} + err := PrintObjects(objects, buf, manifest.ObjectFormatYAML) + assert.NoError(t, err) + assert.Equal(t, expectedObjectsYAML, buf.String()) + }) + + t.Run("Unsupported format", func(t *testing.T) { + buf := &bytes.Buffer{} + err := PrintObjects(objects, buf, manifest.ObjectFormat(-1)) + assert.Error(t, err) + assert.Equal(t, "unsupported format: ObjectFormat(-1)", err.Error()) + }) +} + func TestPrintObject(t *testing.T) { object := v1alpha.GenericObject{ "apiVersion": "v1alpha", diff --git a/sdk/test_data/parser/compacted_list_of_objects.json b/sdk/test_data/decode/compacted_list_of_objects.json similarity index 100% rename from sdk/test_data/parser/compacted_list_of_objects.json rename to sdk/test_data/decode/compacted_list_of_objects.json diff --git a/sdk/test_data/parser/compacted_single_object.json b/sdk/test_data/decode/compacted_single_object.json similarity index 100% rename from sdk/test_data/parser/compacted_single_object.json rename to sdk/test_data/decode/compacted_single_object.json diff --git a/sdk/test_data/parser/list_of_objects.json b/sdk/test_data/decode/list_of_objects.json similarity index 100% rename from sdk/test_data/parser/list_of_objects.json rename to sdk/test_data/decode/list_of_objects.json diff --git a/sdk/test_data/parser/list_of_objects.yaml b/sdk/test_data/decode/list_of_objects.yaml similarity index 100% rename from sdk/test_data/parser/list_of_objects.yaml rename to sdk/test_data/decode/list_of_objects.yaml diff --git a/sdk/test_data/parser/list_of_objects_with_comments.yaml b/sdk/test_data/decode/list_of_objects_with_comments.yaml similarity index 100% rename from sdk/test_data/parser/list_of_objects_with_comments.yaml rename to sdk/test_data/decode/list_of_objects_with_comments.yaml diff --git a/sdk/test_data/parser/list_of_objects_with_whitespace.json b/sdk/test_data/decode/list_of_objects_with_whitespace.json similarity index 100% rename from sdk/test_data/parser/list_of_objects_with_whitespace.json rename to sdk/test_data/decode/list_of_objects_with_whitespace.json diff --git a/sdk/test_data/parser/list_of_objects_with_whitespace.yaml b/sdk/test_data/decode/list_of_objects_with_whitespace.yaml similarity index 100% rename from sdk/test_data/parser/list_of_objects_with_whitespace.yaml rename to sdk/test_data/decode/list_of_objects_with_whitespace.yaml diff --git a/sdk/test_data/parser/multiline_double_quoted_description.yaml b/sdk/test_data/decode/multiline_double_quoted_description.yaml similarity index 100% rename from sdk/test_data/parser/multiline_double_quoted_description.yaml rename to sdk/test_data/decode/multiline_double_quoted_description.yaml diff --git a/sdk/test_data/parser/multiline_double_quoted_description_square_bracket_array.yaml b/sdk/test_data/decode/multiline_double_quoted_description_square_bracket_array.yaml similarity index 100% rename from sdk/test_data/parser/multiline_double_quoted_description_square_bracket_array.yaml rename to sdk/test_data/decode/multiline_double_quoted_description_square_bracket_array.yaml diff --git a/sdk/test_data/parser/multiple_documents.yaml b/sdk/test_data/decode/multiple_documents.yaml similarity index 100% rename from sdk/test_data/parser/multiple_documents.yaml rename to sdk/test_data/decode/multiple_documents.yaml diff --git a/sdk/test_data/parser/single_document.yaml b/sdk/test_data/decode/single_document.yaml similarity index 100% rename from sdk/test_data/parser/single_document.yaml rename to sdk/test_data/decode/single_document.yaml diff --git a/sdk/test_data/parser/single_document_with_document_separators.yaml b/sdk/test_data/decode/single_document_with_document_separators.yaml similarity index 100% rename from sdk/test_data/parser/single_document_with_document_separators.yaml rename to sdk/test_data/decode/single_document_with_document_separators.yaml diff --git a/sdk/test_data/parser/single_object.json b/sdk/test_data/decode/single_object.json similarity index 100% rename from sdk/test_data/parser/single_object.json rename to sdk/test_data/decode/single_object.json diff --git a/sdk/test_data/parser/single_project.yaml b/sdk/test_data/decode/single_project.yaml similarity index 100% rename from sdk/test_data/parser/single_project.yaml rename to sdk/test_data/decode/single_project.yaml diff --git a/sdk/test_data/parser/two_projects.yaml b/sdk/test_data/decode/two_projects.yaml similarity index 100% rename from sdk/test_data/parser/two_projects.yaml rename to sdk/test_data/decode/two_projects.yaml diff --git a/sdk/test_data/printer/expected_object.json b/sdk/test_data/encode/expected_object.json similarity index 100% rename from sdk/test_data/printer/expected_object.json rename to sdk/test_data/encode/expected_object.json diff --git a/sdk/test_data/printer/expected_object.yaml b/sdk/test_data/encode/expected_object.yaml similarity index 100% rename from sdk/test_data/printer/expected_object.yaml rename to sdk/test_data/encode/expected_object.yaml diff --git a/sdk/test_data/printer/expected_objects.json b/sdk/test_data/encode/expected_objects.json similarity index 100% rename from sdk/test_data/printer/expected_objects.json rename to sdk/test_data/encode/expected_objects.json diff --git a/sdk/test_data/printer/expected_objects.yaml b/sdk/test_data/encode/expected_objects.yaml similarity index 100% rename from sdk/test_data/printer/expected_objects.yaml rename to sdk/test_data/encode/expected_objects.yaml