Skip to content

Commit

Permalink
chore: Change objects print to encode (#463)
Browse files Browse the repository at this point in the history
## 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.
  • Loading branch information
nieomylnieja authored Jun 18, 2024
1 parent 7143c20 commit 2ef48af
Show file tree
Hide file tree
Showing 23 changed files with 108 additions and 16 deletions.
File renamed without changes.
6 changes: 3 additions & 3 deletions sdk/parser_test.go → sdk/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
22 changes: 17 additions & 5 deletions sdk/printer.go → sdk/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
96 changes: 88 additions & 8 deletions sdk/printer_test.go → sdk/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 2ef48af

Please sign in to comment.