From 2917517d3315e0ad1430f753c6d1fdb4ae84ec31 Mon Sep 17 00:00:00 2001 From: "Timothy S. Williams" Date: Fri, 26 Apr 2024 11:31:58 -0400 Subject: [PATCH] Resolve several minor issues with generator script (#38) * Resolve dep issues in templates to the correct repo * Remove ensemble references * Add missing present helpers * Remove reference to un-generated validation method * Add object metadata fields to ObjectRefernce --- openapi/openapi.yaml | 6 ++++++ pkg/api/presenters/time.go | 14 ++++++++++++++ pkg/util/utils.go | 28 +++++++++++++++++++++++++++ scripts/generator.go | 2 +- templates/generate-handlers.txt | 4 +--- templates/generate-presenters.txt | 6 +++--- templates/generate-servicelocator.txt | 4 ++-- templates/generate-test-factories.txt | 4 ++-- 8 files changed, 57 insertions(+), 11 deletions(-) create mode 100644 pkg/api/presenters/time.go diff --git a/openapi/openapi.yaml b/openapi/openapi.yaml index 860317eb..74280f72 100644 --- a/openapi/openapi.yaml +++ b/openapi/openapi.yaml @@ -204,6 +204,12 @@ components: type: string href: type: string + created_at: + type: string + format: date-time + updated_at: + type: string + format: date-time List: type: object properties: diff --git a/pkg/api/presenters/time.go b/pkg/api/presenters/time.go new file mode 100644 index 00000000..09f10529 --- /dev/null +++ b/pkg/api/presenters/time.go @@ -0,0 +1,14 @@ +package presenters + +import ( + "time" + + "github.com/openshift-online/rh-trex/pkg/util" +) + +func PresentTime(t time.Time) *time.Time { + if t.IsZero() { + return util.ToPtr(time.Time{}) + } + return util.ToPtr(t.Round(time.Microsecond)) +} diff --git a/pkg/util/utils.go b/pkg/util/utils.go index 76317b13..1d261bc7 100644 --- a/pkg/util/utils.go +++ b/pkg/util/utils.go @@ -5,6 +5,34 @@ import ( "fmt" ) +// ToPtr returns a pointer copy of value. +func ToPtr[T any](v T) *T { + return &v +} + +// FromPtr returns the pointer value or empty. +func FromPtr[T any](v *T) T { + if v == nil { + return Empty[T]() + } + return *v +} + +// FromEmptyPtr emulates ToPtr(FromPtr(x)) sequence +func FromEmptyPtr[T any](v *T) *T { + if v == nil { + x := Empty[T]() + return &x + } + return v +} + +// Empty returns an empty value of type T. +func Empty[T any]() T { + var zero T + return zero +} + func EmptyStringToNil(a string) *string { if a == "" { return nil diff --git a/scripts/generator.go b/scripts/generator.go index e33b6df7..d8b8b824 100644 --- a/scripts/generator.go +++ b/scripts/generator.go @@ -94,7 +94,7 @@ func main() { "generate-test-factories": fmt.Sprintf("test/factories/%s.go", k.KindLowerPlural), "generate-test": fmt.Sprintf("test/integration/%s_test.go", k.KindLowerPlural), "generate-services": fmt.Sprintf("pkg/%s/%s.go", nm, k.KindLowerSingular), - "generate-servicelocator": fmt.Sprintf("cmd/ensemble/environments/locator_%s.go", k.KindLowerSingular), + "generate-servicelocator": fmt.Sprintf("cmd/trex/environments/locator_%s.go", k.KindLowerSingular), } outputPath, ok := outputPaths["generate-"+nm] diff --git a/templates/generate-handlers.txt b/templates/generate-handlers.txt index 37ef041e..e8aa1b2b 100755 --- a/templates/generate-handlers.txt +++ b/templates/generate-handlers.txt @@ -53,9 +53,7 @@ func (h {{.KindLowerSingular}}Handler) Patch(w http.ResponseWriter, r *http.Requ cfg := &handlerConfig{ &patch, - []validate{ - validate{{.Kind}}Patch(&patch), - }, + []validate{}, func() (interface{}, *errors.ServiceError) { ctx := r.Context() id := mux.Vars(r)["id"] diff --git a/templates/generate-presenters.txt b/templates/generate-presenters.txt index f57edb74..c4c32f6e 100644 --- a/templates/generate-presenters.txt +++ b/templates/generate-presenters.txt @@ -1,9 +1,9 @@ package presenters import ( - "gitlab.cee.redhat.com/ocm/ensemble/pkg/api" - "gitlab.cee.redhat.com/ocm/ensemble/pkg/api/openapi" - "gitlab.cee.redhat.com/ocm/ensemble/pkg/util" + "github.com/openshift-online/rh-trex/pkg/api" + "github.com/openshift-online/rh-trex/pkg/api/openapi" + "github.com/openshift-online/rh-trex/pkg/util" ) func Convert{{.Kind}}({{.KindLowerSingular}} openapi.{{.Kind}}) *api.{{.Kind}} { diff --git a/templates/generate-servicelocator.txt b/templates/generate-servicelocator.txt index 6f354d95..759aa705 100644 --- a/templates/generate-servicelocator.txt +++ b/templates/generate-servicelocator.txt @@ -1,8 +1,8 @@ package environments import ( - "gitlab.cee.redhat.com/ocm/ensemble/pkg/dao" - "gitlab.cee.redhat.com/ocm/ensemble/pkg/services" + "github.com/openshift-online/rh-trex/pkg/dao" + "github.com/openshift-online/rh-trex/pkg/services" ) type {{.Kind}}ServiceLocator func() services.{{.Kind}}Service diff --git a/templates/generate-test-factories.txt b/templates/generate-test-factories.txt index 4bc49f16..d0d67c80 100644 --- a/templates/generate-test-factories.txt +++ b/templates/generate-test-factories.txt @@ -2,8 +2,8 @@ package factories import ( "context" - "gitlab.cee.redhat.com/ocm/ensemble/cmd/ensemble/environments" - "gitlab.cee.redhat.com/ocm/ensemble/pkg/api" + "github.com/openshift-online/rh-trex/cmd/trex/environments" + "github.com/openshift-online/rh-trex/pkg/api" ) func (f *Factories) New{{.Kind}}(id string) (*api.{{.Kind}}, error) {