Skip to content

Commit

Permalink
Resolve several minor issues with generator script (#38)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
tiwillia authored Apr 26, 2024
1 parent 8c9dfb8 commit 2917517
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 11 deletions.
6 changes: 6 additions & 0 deletions openapi/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
14 changes: 14 additions & 0 deletions pkg/api/presenters/time.go
Original file line number Diff line number Diff line change
@@ -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))
}
28 changes: 28 additions & 0 deletions pkg/util/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion scripts/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 1 addition & 3 deletions templates/generate-handlers.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
6 changes: 3 additions & 3 deletions templates/generate-presenters.txt
Original file line number Diff line number Diff line change
@@ -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}} {
Expand Down
4 changes: 2 additions & 2 deletions templates/generate-servicelocator.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions templates/generate-test-factories.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 2917517

Please sign in to comment.