diff --git a/.github/workflows/e2e-tests.yml b/.github/workflows/e2e-tests.yml index 9e561b23..ab4c6446 100644 --- a/.github/workflows/e2e-tests.yml +++ b/.github/workflows/e2e-tests.yml @@ -43,4 +43,5 @@ jobs: NOBL9_SDK_CLIENT_SECRET: ${{ secrets.clientSecret }} NOBL9_SDK_OKTA_ORG_URL: ${{ inputs.oktaOrgUrl }} NOBL9_SDK_OKTA_AUTH_SERVER: ${{ inputs.oktaAuthServer }} + NOBL9_SDK_NO_CONFIG_FILE: true run: make test/e2e diff --git a/cspell.yaml b/cspell.yaml index 821bc5d8..19094f73 100644 --- a/cspell.yaml +++ b/cspell.yaml @@ -99,6 +99,7 @@ words: - noprefix - nriq - nrql + - objectimpl - opentsdb - opsgenie - pagerduty @@ -133,6 +134,7 @@ words: - timeseries - timeslice - timeslices + - tmpl - tpng - tsdb - twindow diff --git a/go.mod b/go.mod index bf4e2a72..dba05d9c 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( github.com/BurntSushi/toml v1.4.0 github.com/MicahParks/jwkset v0.5.18 github.com/MicahParks/keyfunc/v3 v3.3.3 - github.com/aws/aws-sdk-go v1.53.20 + github.com/aws/aws-sdk-go v1.54.3 github.com/bmatcuk/doublestar/v4 v4.6.1 github.com/goccy/go-yaml v1.11.3 github.com/golang-jwt/jwt/v5 v5.2.1 diff --git a/go.sum b/go.sum index 8431261b..8f3ccc98 100644 --- a/go.sum +++ b/go.sum @@ -4,8 +4,8 @@ github.com/MicahParks/jwkset v0.5.18 h1:WLdyMngF7rCrnstQxA7mpRoxeaWqGzPM/0z40PJU github.com/MicahParks/jwkset v0.5.18/go.mod h1:q8ptTGn/Z9c4MwbcfeCDssADeVQb3Pk7PnVxrvi+2QY= github.com/MicahParks/keyfunc/v3 v3.3.3 h1:c6j9oSu1YUo0k//KwF1miIQlEMtqNlj7XBFLB8jtEmY= github.com/MicahParks/keyfunc/v3 v3.3.3/go.mod h1:f/UMyXdKfkZzmBeBFUeYk+zu066J1Fcl48f7Wnl5Z48= -github.com/aws/aws-sdk-go v1.53.20 h1:cYWPvZLP1gPj5CfUdnfjaaA7WFK3FGoJ/R9+Ks1inU4= -github.com/aws/aws-sdk-go v1.53.20/go.mod h1:LF8svs817+Nz+DmiMQKTO3ubZ/6IaTpq3TjupRn3Eqk= +github.com/aws/aws-sdk-go v1.54.3 h1:Bk+EXoq6v5I1xmHR9GQGpsMWZZFXs+FD+5uPyEmfgX0= +github.com/aws/aws-sdk-go v1.54.3/go.mod h1:eRwEWoyTWFMVYVQzKMNHWP5/RV4xIUGMQfXQHfHkpNU= github.com/bmatcuk/doublestar/v4 v4.6.1 h1:FH9SifrbvJhnlQpztAx++wlkk70QBf0iBWDwNy7PA4I= github.com/bmatcuk/doublestar/v4 v4.6.1/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= diff --git a/internal/cmd/objectimpl/README.md b/internal/cmd/objectimpl/README.md new file mode 100644 index 00000000..0cc521a5 --- /dev/null +++ b/internal/cmd/objectimpl/README.md @@ -0,0 +1,16 @@ +# docgen + +`objectimpl` is a tool that generates boilerplate functions for manifest objects +in order to implement `manifest.Object` interface. + +It utilizes `text/template` to generate the code. + +## Usage + +Add the following `generate` directive to the file that contains +the `manifest.Object` object definition. +Replace the `` with the struct name of your object, e.g. `Project`. + +```go +//go:generate go run ../../../internal/cmd/objectimpl +``` diff --git a/scripts/generate-object-impl.go b/internal/cmd/objectimpl/main.go similarity index 93% rename from scripts/generate-object-impl.go rename to internal/cmd/objectimpl/main.go index 5b9da453..cf1c43e1 100644 --- a/scripts/generate-object-impl.go +++ b/internal/cmd/objectimpl/main.go @@ -15,7 +15,7 @@ import ( "unicode" ) -//go:embed generate-object-impl.tpl +//go:embed object.tmpl var templateStr string type generator struct { @@ -80,7 +80,7 @@ func main() { errFatal(err.Error()) } outputName := filepath.Join(cwd, fmt.Sprintf("%s_object.go", strings.TrimSuffix(filename, ".go"))) - if err = os.WriteFile(outputName, formatted, 0600); err != nil { + if err = os.WriteFile(outputName, formatted, 0o600); err != nil { errFatal(err.Error()) } } @@ -168,11 +168,7 @@ func (g *generator) hasOrganizationAndManifestSource(fields *ast.FieldList) bool return hasOrganization && hasManifestSource } -func errFatal(f string, a ...interface{}) { - if len(a) == 0 { - fmt.Fprintln(os.Stderr, f) - } else { - fmt.Fprintf(os.Stderr, f+"\n", a...) - } +func errFatal(f string) { + fmt.Fprintln(os.Stderr, f) os.Exit(1) } diff --git a/scripts/generate-object-impl.tpl b/internal/cmd/objectimpl/object.tmpl similarity index 100% rename from scripts/generate-object-impl.tpl rename to internal/cmd/objectimpl/object.tmpl diff --git a/manifest/v1alpha/agent/agent.go b/manifest/v1alpha/agent/agent.go index 5926b631..ecc04568 100644 --- a/manifest/v1alpha/agent/agent.go +++ b/manifest/v1alpha/agent/agent.go @@ -7,7 +7,7 @@ import ( "github.com/nobl9/nobl9-go/manifest/v1alpha" ) -//go:generate go run ../../../scripts/generate-object-impl.go Agent +//go:generate go run ../../../internal/cmd/objectimpl Agent // New creates new Agent instance. func New(metadata Metadata, spec Spec) Agent { diff --git a/manifest/v1alpha/agent/agent_object.go b/manifest/v1alpha/agent/agent_object.go index ed510262..06a7375d 100644 --- a/manifest/v1alpha/agent/agent_object.go +++ b/manifest/v1alpha/agent/agent_object.go @@ -1,4 +1,4 @@ -// Code generated by "generate-object-impl Agent"; DO NOT EDIT. +// Code generated by "objectimpl Agent"; DO NOT EDIT. package agent diff --git a/manifest/v1alpha/alert/alert.go b/manifest/v1alpha/alert/alert.go index db91efd8..715d66f4 100644 --- a/manifest/v1alpha/alert/alert.go +++ b/manifest/v1alpha/alert/alert.go @@ -5,7 +5,7 @@ import ( "github.com/nobl9/nobl9-go/manifest/v1alpha" ) -//go:generate go run ../../../scripts/generate-object-impl.go Alert +//go:generate go run ../../../internal/cmd/objectimpl Alert // New creates a new Alert based on provided Metadata nad Spec. func New(metadata Metadata, spec Spec) Alert { diff --git a/manifest/v1alpha/alert/alert_object.go b/manifest/v1alpha/alert/alert_object.go index 4d71f77a..18a5ff62 100644 --- a/manifest/v1alpha/alert/alert_object.go +++ b/manifest/v1alpha/alert/alert_object.go @@ -1,4 +1,4 @@ -// Code generated by "generate-object-impl Alert"; DO NOT EDIT. +// Code generated by "objectimpl Alert"; DO NOT EDIT. package alert diff --git a/manifest/v1alpha/alertmethod/alert_method.go b/manifest/v1alpha/alertmethod/alert_method.go index 7f67fc31..494979ff 100644 --- a/manifest/v1alpha/alertmethod/alert_method.go +++ b/manifest/v1alpha/alertmethod/alert_method.go @@ -4,7 +4,7 @@ import ( "github.com/nobl9/nobl9-go/manifest" ) -//go:generate go run ../../../scripts/generate-object-impl.go AlertMethod +//go:generate go run ../../../internal/cmd/objectimpl AlertMethod // New creates a new AlertMethod based on provided Metadata nad Spec. func New(metadata Metadata, spec Spec) AlertMethod { diff --git a/manifest/v1alpha/alertmethod/alert_method_object.go b/manifest/v1alpha/alertmethod/alert_method_object.go index 3bc4bb02..a99a0ee7 100644 --- a/manifest/v1alpha/alertmethod/alert_method_object.go +++ b/manifest/v1alpha/alertmethod/alert_method_object.go @@ -1,4 +1,4 @@ -// Code generated by "generate-object-impl AlertMethod"; DO NOT EDIT. +// Code generated by "objectimpl AlertMethod"; DO NOT EDIT. package alertmethod diff --git a/manifest/v1alpha/alertpolicy/alert_policy.go b/manifest/v1alpha/alertpolicy/alert_policy.go index 40240a6c..d2da7649 100644 --- a/manifest/v1alpha/alertpolicy/alert_policy.go +++ b/manifest/v1alpha/alertpolicy/alert_policy.go @@ -7,7 +7,7 @@ import ( "github.com/nobl9/nobl9-go/manifest/v1alpha" ) -//go:generate go run ../../../scripts/generate-object-impl.go AlertPolicy +//go:generate go run ../../../internal/cmd/objectimpl AlertPolicy func New(metadata Metadata, spec Spec) AlertPolicy { return AlertPolicy{ diff --git a/manifest/v1alpha/alertpolicy/alert_policy_object.go b/manifest/v1alpha/alertpolicy/alert_policy_object.go index 18bd826d..ac8178f7 100644 --- a/manifest/v1alpha/alertpolicy/alert_policy_object.go +++ b/manifest/v1alpha/alertpolicy/alert_policy_object.go @@ -1,4 +1,4 @@ -// Code generated by "generate-object-impl AlertPolicy"; DO NOT EDIT. +// Code generated by "objectimpl AlertPolicy"; DO NOT EDIT. package alertpolicy diff --git a/manifest/v1alpha/alertsilence/alert_silence.go b/manifest/v1alpha/alertsilence/alert_silence.go index 7e192703..d3f670ed 100644 --- a/manifest/v1alpha/alertsilence/alert_silence.go +++ b/manifest/v1alpha/alertsilence/alert_silence.go @@ -6,7 +6,7 @@ import ( "github.com/nobl9/nobl9-go/manifest" ) -//go:generate go run ../../../scripts/generate-object-impl.go AlertSilence +//go:generate go run ../../../internal/cmd/objectimpl AlertSilence // New creates a new AlertSilence based on provided Metadata nad Spec. func New(metadata Metadata, spec Spec) AlertSilence { diff --git a/manifest/v1alpha/alertsilence/alert_silence_object.go b/manifest/v1alpha/alertsilence/alert_silence_object.go index 6dcd36cb..f4545c89 100644 --- a/manifest/v1alpha/alertsilence/alert_silence_object.go +++ b/manifest/v1alpha/alertsilence/alert_silence_object.go @@ -1,4 +1,4 @@ -// Code generated by "generate-object-impl AlertSilence"; DO NOT EDIT. +// Code generated by "objectimpl AlertSilence"; DO NOT EDIT. package alertsilence diff --git a/manifest/v1alpha/annotation/annotation.go b/manifest/v1alpha/annotation/annotation.go index 0de5e48b..be9a745f 100644 --- a/manifest/v1alpha/annotation/annotation.go +++ b/manifest/v1alpha/annotation/annotation.go @@ -6,7 +6,7 @@ import ( "github.com/nobl9/nobl9-go/manifest" ) -//go:generate go run ../../../scripts/generate-object-impl.go Annotation +//go:generate go run ../../../internal/cmd/objectimpl Annotation // New creates a new Annotation based on provided Metadata nad Spec. func New(metadata Metadata, spec Spec) Annotation { diff --git a/manifest/v1alpha/annotation/annotation_object.go b/manifest/v1alpha/annotation/annotation_object.go index 19396dc1..5680fa62 100644 --- a/manifest/v1alpha/annotation/annotation_object.go +++ b/manifest/v1alpha/annotation/annotation_object.go @@ -1,4 +1,4 @@ -// Code generated by "generate-object-impl Annotation"; DO NOT EDIT. +// Code generated by "objectimpl Annotation"; DO NOT EDIT. package annotation diff --git a/manifest/v1alpha/budgetadjustment/budget_adjustment.go b/manifest/v1alpha/budgetadjustment/budget_adjustment.go index 5c437153..4fc47b8a 100644 --- a/manifest/v1alpha/budgetadjustment/budget_adjustment.go +++ b/manifest/v1alpha/budgetadjustment/budget_adjustment.go @@ -6,7 +6,7 @@ import ( "github.com/nobl9/nobl9-go/manifest" ) -//go:generate go run ../../../scripts/generate-object-impl.go BudgetAdjustment +//go:generate go run ../../../internal/cmd/objectimpl BudgetAdjustment func New(metadata Metadata, spec Spec) BudgetAdjustment { return BudgetAdjustment{ diff --git a/manifest/v1alpha/budgetadjustment/budget_adjustment_object.go b/manifest/v1alpha/budgetadjustment/budget_adjustment_object.go index b12fe18c..36cd3b5f 100644 --- a/manifest/v1alpha/budgetadjustment/budget_adjustment_object.go +++ b/manifest/v1alpha/budgetadjustment/budget_adjustment_object.go @@ -1,4 +1,4 @@ -// Code generated by "generate-object-impl BudgetAdjustment"; DO NOT EDIT. +// Code generated by "objectimpl BudgetAdjustment"; DO NOT EDIT. package budgetadjustment diff --git a/manifest/v1alpha/dataexport/data_export.go b/manifest/v1alpha/dataexport/data_export.go index e1ff0bed..0a86397b 100644 --- a/manifest/v1alpha/dataexport/data_export.go +++ b/manifest/v1alpha/dataexport/data_export.go @@ -9,7 +9,7 @@ import ( "github.com/nobl9/nobl9-go/manifest" ) -//go:generate go run ../../../scripts/generate-object-impl.go DataExport +//go:generate go run ../../../internal/cmd/objectimpl DataExport // New creates a new DataExport based on provided Metadata nad Spec. func New(metadata Metadata, spec Spec) DataExport { diff --git a/manifest/v1alpha/dataexport/data_export_object.go b/manifest/v1alpha/dataexport/data_export_object.go index 08e06191..b436659f 100644 --- a/manifest/v1alpha/dataexport/data_export_object.go +++ b/manifest/v1alpha/dataexport/data_export_object.go @@ -1,4 +1,4 @@ -// Code generated by "generate-object-impl DataExport"; DO NOT EDIT. +// Code generated by "objectimpl DataExport"; DO NOT EDIT. package dataexport diff --git a/manifest/v1alpha/direct/direct.go b/manifest/v1alpha/direct/direct.go index d4357264..f69a1698 100644 --- a/manifest/v1alpha/direct/direct.go +++ b/manifest/v1alpha/direct/direct.go @@ -7,7 +7,7 @@ import ( "github.com/nobl9/nobl9-go/manifest/v1alpha" ) -//go:generate go run ../../../scripts/generate-object-impl.go Direct +//go:generate go run ../../../internal/cmd/objectimpl Direct func New(metadata Metadata, spec Spec) Direct { return Direct{ diff --git a/manifest/v1alpha/direct/direct_object.go b/manifest/v1alpha/direct/direct_object.go index 5a8588ee..e0934e0f 100644 --- a/manifest/v1alpha/direct/direct_object.go +++ b/manifest/v1alpha/direct/direct_object.go @@ -1,4 +1,4 @@ -// Code generated by "generate-object-impl Direct"; DO NOT EDIT. +// Code generated by "objectimpl Direct"; DO NOT EDIT. package direct diff --git a/manifest/v1alpha/project/project.go b/manifest/v1alpha/project/project.go index 22144a51..acfa06af 100644 --- a/manifest/v1alpha/project/project.go +++ b/manifest/v1alpha/project/project.go @@ -5,7 +5,7 @@ import ( "github.com/nobl9/nobl9-go/manifest/v1alpha" ) -//go:generate go run ../../../scripts/generate-object-impl.go Project +//go:generate go run ../../../internal/cmd/objectimpl Project // New creates a new Project based on provided Metadata nad Spec. func New(metadata Metadata, spec Spec) Project { diff --git a/manifest/v1alpha/project/project_object.go b/manifest/v1alpha/project/project_object.go index 6916973a..cc21e2b0 100644 --- a/manifest/v1alpha/project/project_object.go +++ b/manifest/v1alpha/project/project_object.go @@ -1,4 +1,4 @@ -// Code generated by "generate-object-impl Project"; DO NOT EDIT. +// Code generated by "objectimpl Project"; DO NOT EDIT. package project diff --git a/manifest/v1alpha/rolebinding/role_binding.go b/manifest/v1alpha/rolebinding/role_binding.go index aa577711..04edd002 100644 --- a/manifest/v1alpha/rolebinding/role_binding.go +++ b/manifest/v1alpha/rolebinding/role_binding.go @@ -4,7 +4,7 @@ import ( "github.com/nobl9/nobl9-go/manifest" ) -//go:generate go run ../../../scripts/generate-object-impl.go RoleBinding +//go:generate go run ../../../internal/cmd/objectimpl RoleBinding func New(metadata Metadata, spec Spec) RoleBinding { return RoleBinding{ diff --git a/manifest/v1alpha/rolebinding/role_binding_object.go b/manifest/v1alpha/rolebinding/role_binding_object.go index ae808fd0..021e4602 100644 --- a/manifest/v1alpha/rolebinding/role_binding_object.go +++ b/manifest/v1alpha/rolebinding/role_binding_object.go @@ -1,4 +1,4 @@ -// Code generated by "generate-object-impl RoleBinding"; DO NOT EDIT. +// Code generated by "objectimpl RoleBinding"; DO NOT EDIT. package rolebinding diff --git a/manifest/v1alpha/service/service.go b/manifest/v1alpha/service/service.go index 52b14721..704a92e6 100644 --- a/manifest/v1alpha/service/service.go +++ b/manifest/v1alpha/service/service.go @@ -5,7 +5,7 @@ import ( "github.com/nobl9/nobl9-go/manifest/v1alpha" ) -//go:generate go run ../../../scripts/generate-object-impl.go Service +//go:generate go run ../../../internal/cmd/objectimpl Service // New creates a new Service based on provided Metadata nad Spec. func New(metadata Metadata, spec Spec) Service { diff --git a/manifest/v1alpha/service/service_object.go b/manifest/v1alpha/service/service_object.go index af23bbc1..10702f06 100644 --- a/manifest/v1alpha/service/service_object.go +++ b/manifest/v1alpha/service/service_object.go @@ -1,4 +1,4 @@ -// Code generated by "generate-object-impl Service"; DO NOT EDIT. +// Code generated by "objectimpl Service"; DO NOT EDIT. package service diff --git a/manifest/v1alpha/slo/slo.go b/manifest/v1alpha/slo/slo.go index 86b1add4..0e47569d 100644 --- a/manifest/v1alpha/slo/slo.go +++ b/manifest/v1alpha/slo/slo.go @@ -5,7 +5,7 @@ import ( "github.com/nobl9/nobl9-go/manifest/v1alpha" ) -//go:generate go run ../../../scripts/generate-object-impl.go SLO +//go:generate go run ../../../internal/cmd/objectimpl SLO // New creates a new SLO based on provided Metadata nad Spec. func New(metadata Metadata, spec Spec) SLO { diff --git a/manifest/v1alpha/slo/slo_object.go b/manifest/v1alpha/slo/slo_object.go index 81891e1d..64cfbcd9 100644 --- a/manifest/v1alpha/slo/slo_object.go +++ b/manifest/v1alpha/slo/slo_object.go @@ -1,4 +1,4 @@ -// Code generated by "generate-object-impl SLO"; DO NOT EDIT. +// Code generated by "objectimpl SLO"; DO NOT EDIT. package slo diff --git a/manifest/v1alpha/usergroup/user_group.go b/manifest/v1alpha/usergroup/user_group.go index 63e2cca6..8c3b5e8b 100644 --- a/manifest/v1alpha/usergroup/user_group.go +++ b/manifest/v1alpha/usergroup/user_group.go @@ -4,7 +4,7 @@ import ( "github.com/nobl9/nobl9-go/manifest" ) -//go:generate go run ../../../scripts/generate-object-impl.go UserGroup +//go:generate go run ../../../internal/cmd/objectimpl UserGroup func New(metadata Metadata, spec Spec) UserGroup { return UserGroup{ diff --git a/manifest/v1alpha/usergroup/user_group_object.go b/manifest/v1alpha/usergroup/user_group_object.go index 91d5ace6..374b7ecb 100644 --- a/manifest/v1alpha/usergroup/user_group_object.go +++ b/manifest/v1alpha/usergroup/user_group_object.go @@ -1,4 +1,4 @@ -// Code generated by "generate-object-impl UserGroup"; DO NOT EDIT. +// Code generated by "objectimpl UserGroup"; DO NOT EDIT. package usergroup diff --git a/package.json b/package.json index 0c63ea1e..6cd9f749 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "private": true, "type": "module", "devDependencies": { - "cspell": "8.8.4", + "cspell": "8.9.0", "markdownlint-cli": "0.41.0", "yaml": "2.4.5" }, 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