Skip to content

Commit

Permalink
tweaks generators to complete CRUD (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
markturansky authored Apr 25, 2024
1 parent abacec5 commit 8c9dfb8
Show file tree
Hide file tree
Showing 13 changed files with 150 additions and 21 deletions.
41 changes: 25 additions & 16 deletions scripts/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,16 @@ func main() {

templates := []string{
"api",
"presenters",
"dao",
"services",
"mock",
"migration",
"test",
"test-factories",
"handlers",
"openapi-kind",
"servicelocator",
}

for _, nm := range templates {
Expand All @@ -72,28 +75,34 @@ func main() {

k := myWriter{
Kind: kind,
KindPlural: fmt.Sprintf("%ss", kind),
KindLowerPlural: strings.ToLower(fmt.Sprintf("%ss", kind)),
KindLowerSingular: strings.ToLower(kind),
}

now := time.Now()
k.ID = fmt.Sprintf("%d%s%s%s%s", now.Year(), datePad(int(now.Month())), datePad(now.Day()), datePad(now.Hour()), datePad(now.Minute()))

var outPath string

if strings.Contains(nm, "mock") {
outPath = fmt.Sprintf("pkg/dao/mocks/%s.go", k.KindLowerSingular)
} else if strings.Contains(nm, "migration") {
outPath = fmt.Sprintf("pkg/db/migrations/%s_add_%s.go", k.ID, k.KindLowerPlural)
} else if strings.Contains(nm, "test") {
outPath = fmt.Sprintf("test/integration/%s_test.go", k.KindLowerPlural)
} else if strings.Contains(nm, "openapi") {
outPath = fmt.Sprintf("openapi/openapi.%s.yaml", k.KindLowerPlural)
} else {
outPath = fmt.Sprintf("pkg/%s/%s.go", nm, k.KindLowerSingular)
outputPaths := map[string]string{
"generate-api": fmt.Sprintf("pkg/%s/%s.go", nm, k.KindLowerSingular),
"generate-presenters": fmt.Sprintf("pkg/api/presenters/%s.go", k.KindLowerSingular),
"generate-dao": fmt.Sprintf("pkg/%s/%s.go", nm, k.KindLowerSingular),
"generate-handlers": fmt.Sprintf("pkg/%s/%s.go", nm, k.KindLowerSingular),
"generate-migration": fmt.Sprintf("pkg/db/migrations/%s_add_%s.go", k.ID, k.KindLowerPlural),
"generate-mock": fmt.Sprintf("pkg/dao/mocks/%s.go", k.KindLowerSingular),
"generate-openapi-kind": fmt.Sprintf("openapi/openapi.%s.yaml", k.KindLowerPlural),
"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),
}

f, err := os.Create(outPath)
outputPath, ok := outputPaths["generate-"+nm]
if !ok {
panic("expected to find outputPath for " + nm)
}

f, err := os.Create(outputPath)
if err != nil {
panic(err)
}
Expand All @@ -107,7 +116,7 @@ func main() {
w.Flush()
f.Sync()

if strings.Contains(nm, "openapi") {
if strings.EqualFold("generate-"+nm, "generate-openapi-kind") {
modifyOpenapi("openapi/openapi.yaml", fmt.Sprintf("openapi/openapi.%s.yaml", k.KindLowerPlural))
}
}
Expand All @@ -121,8 +130,8 @@ func datePad(d int) string {
}

type myWriter struct {
Kind string
//KindLower string
Kind string
KindPlural string
KindLowerPlural string
KindLowerSingular string
ID string
Expand Down
Empty file modified templates/generate-api.txt
100644 → 100755
Empty file.
Empty file modified templates/generate-dao.txt
100644 → 100755
Empty file.
Empty file modified templates/generate-handlers.txt
100644 → 100755
Empty file.
Empty file modified templates/generate-migration.txt
100644 → 100755
Empty file.
Empty file modified templates/generate-mock.txt
100644 → 100755
Empty file.
Empty file modified templates/generate-openapi-kind.txt
100644 → 100755
Empty file.
Empty file modified templates/generate-openapi.txt
100644 → 100755
Empty file.
37 changes: 37 additions & 0 deletions templates/generate-presenters.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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"
)

func Convert{{.Kind}}({{.KindLowerSingular}} openapi.{{.Kind}}) *api.{{.Kind}} {
c := &api.{{.Kind}}{
Meta: api.Meta{
ID: util.NilToEmptyString({{.KindLowerSingular}}.Id),
},
}

if {{.KindLowerSingular}}.CreatedAt != nil {
c.CreatedAt = *{{.KindLowerSingular}}.CreatedAt
c.UpdatedAt = *{{.KindLowerSingular}}.UpdatedAt
}

return c
}

func Present{{.Kind}}({{.KindLowerSingular}} *api.{{.Kind}}) openapi.{{.Kind}} {
reference := PresentReference({{.KindLowerSingular}}.ID, {{.KindLowerSingular}})

reference.CreatedAt = PresentTime({{.KindLowerSingular}}.CreatedAt)
reference.UpdatedAt = PresentTime({{.KindLowerSingular}}.UpdatedAt)

return openapi.{{.Kind}}{
Id: reference.Id,
CreatedAt: PresentTime(*reference.CreatedAt),
UpdatedAt: PresentTime(*reference.UpdatedAt),
Kind: reference.Kind,
Href: reference.Href,
}
}
17 changes: 17 additions & 0 deletions templates/generate-servicelocator.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package environments

import (
"gitlab.cee.redhat.com/ocm/ensemble/pkg/dao"
"gitlab.cee.redhat.com/ocm/ensemble/pkg/services"
)

type {{.Kind}}ServiceLocator func() services.{{.Kind}}Service

func New{{.Kind}}ServiceLocator(env *Env) {{.Kind}}ServiceLocator {
return func() services.{{.Kind}}Service {
return services.New{{.Kind}}Service(
dao.New{{.Kind}}Dao(&env.Database.SessionFactory),
env.Services.Events(),
)
}
}
34 changes: 33 additions & 1 deletion templates/generate-services.txt
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,18 @@ type {{.Kind}}Service interface {
FindByIDs(ctx context.Context, ids []string) (api.{{.Kind}}List, *errors.ServiceError)
}

func New{{.Kind}}Service({{.KindLowerSingular}}Dao dao.{{.Kind}}Dao) {{.Kind}}Service {
func New{{.Kind}}Service({{.KindLowerSingular}}Dao dao.{{.Kind}}Dao, events EventService) {{.Kind}}Service {
return &sql{{.Kind}}Service{
{{.KindLowerSingular}}Dao: {{.KindLowerSingular}}Dao,
events: events,
}
}

var _ {{.Kind}}Service = &sql{{.Kind}}Service{}

type sql{{.Kind}}Service struct {
{{.KindLowerSingular}}Dao dao.{{.Kind}}Dao
events EventService
}

func (s *sql{{.Kind}}Service) Get(ctx context.Context, id string) (*api.{{.Kind}}, *errors.ServiceError) {
Expand All @@ -43,6 +45,16 @@ func (s *sql{{.Kind}}Service) Create(ctx context.Context, {{.KindLowerSingular}}
if err != nil {
return nil, handleCreateError("{{.Kind}}", err)
}

_, evErr := s.events.Create(ctx, &api.Event{
Source: "{{.KindPlural}}",
SourceID: {{.KindLowerSingular}}.ID,
EventType: api.CreateEventType,
})
if evErr != nil {
return nil, handleCreateError("{{.Kind}}", evErr)
}

return {{.KindLowerSingular}}, nil
}

Expand All @@ -51,13 +63,33 @@ func (s *sql{{.Kind}}Service) Replace(ctx context.Context, {{.KindLowerSingular}
if err != nil {
return nil, handleUpdateError("{{.Kind}}", err)
}

_, evErr := s.events.Create(ctx, &api.Event{
Source: "{{.KindPlural}}",
SourceID: {{.KindLowerSingular}}.ID,
EventType: api.UpdateEventType,
})
if evErr != nil {
return nil, handleUpdateError("{{.Kind}}", evErr)
}

return {{.KindLowerSingular}}, nil
}

func (s *sql{{.Kind}}Service) Delete(ctx context.Context, id string) *errors.ServiceError {
if err := s.{{.KindLowerSingular}}Dao.Delete(ctx, id); err != nil {
return handleDeleteError("{{.Kind}}", errors.GeneralError("Unable to delete {{.KindLowerSingular}}: %s", err))
}

_, evErr := s.events.Create(ctx, &api.Event{
Source: "{{.KindPlural}}",
SourceID: id,
EventType: api.DeleteEventType,
})
if evErr != nil {
return handleDeleteError("{{.Kind}}", evErr)
}

return nil
}

Expand Down
31 changes: 31 additions & 0 deletions templates/generate-test-factories.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package factories

import (
"context"
"gitlab.cee.redhat.com/ocm/ensemble/cmd/ensemble/environments"
"gitlab.cee.redhat.com/ocm/ensemble/pkg/api"
)

func (f *Factories) New{{.Kind}}(id string) (*api.{{.Kind}}, error) {
{{.Kind}}Service := environments.Environment().Services.{{.KindPlural}}()

{{.Kind}} := &api.{{.Kind}}{
Meta: api.Meta{ID: id},
}

sub, err := {{.Kind}}Service.Create(context.Background(), {{.Kind}})
if err != nil {
return nil, err
}

return sub, nil
}

func (f *Factories) New{{.Kind}}List(name string, count int) ([]*api.{{.Kind}}, error) {
{{.KindPlural}} := []*api.{{.Kind}}{}
for i := 1; i <= count; i++ {
c, _ := f.New{{.Kind}}(f.NewID())
{{.KindPlural}} = append({{.KindPlural}}, c)
}
return {{.KindPlural}}, nil
}
11 changes: 7 additions & 4 deletions templates/generate-test.txt
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ func Test{{.Kind}}Get(t *testing.T) {
Expect(err).To(HaveOccurred(), "Expected 404")
Expect(resp.StatusCode).To(Equal(http.StatusNotFound))

dino := h.New{{.Kind}}(h.NewID())
dino, err := h.Factories.New{{.Kind}}(h.NewID())
Expect(err).NotTo(HaveOccurred())

{{.KindLowerSingular}}, resp, err := client.DefaultApi.ApiRhTrexV1{{.Kind}}sIdGet(ctx, dino.ID).Execute()
Expect(err).NotTo(HaveOccurred())
Expand Down Expand Up @@ -80,7 +81,8 @@ func Test{{.Kind}}Patch(t *testing.T) {

// POST responses per openapi spec: 201, 409, 500

dino := h.New{{.Kind}}("Brontosaurus")
dino, err := h.Factories.New{{.Kind}}(h.NewID())
Expect(err).NotTo(HaveOccurred())

// 200 OK
{{.KindLowerSingular}}, resp, err := client.DefaultApi.ApiRhTrexV1{{.Kind}}sIdPatch(ctx, dino.ID).{{.Kind}}PatchRequest(openapi.{{.Kind}}PatchRequest{}).Execute()
Expand Down Expand Up @@ -109,7 +111,8 @@ func Test{{.Kind}}Paging(t *testing.T) {
ctx := h.NewAuthenticatedContext(account)

// Paging
_ = h.New{{.Kind}}List("Bronto", 20)
_, err := h.Factories.New{{.Kind}}List("Bronto", 20)
Expect(err).NotTo(HaveOccurred())

list, _, err := client.DefaultApi.ApiRhTrexV1{{.Kind}}sGet(ctx).Execute()
Expect(err).NotTo(HaveOccurred(), "Error getting {{.KindLowerSingular}} list: %v", err)
Expand All @@ -132,7 +135,7 @@ func Test{{.Kind}}ListSearch(t *testing.T) {
account := h.NewRandAccount()
ctx := h.NewAuthenticatedContext(account)

{{.KindLowerPlural}} := h.New{{.Kind}}List("bronto", 20)
{{.KindLowerPlural}}, _ := h.Factories.New{{.Kind}}List("bronto", 20)

search := fmt.Sprintf("id in ('%s')", {{.KindLowerPlural}}[0].ID)
list, _, err := client.DefaultApi.ApiRhTrexV1{{.Kind}}sGet(ctx).Search(search).Execute()
Expand Down

0 comments on commit 8c9dfb8

Please sign in to comment.