diff --git a/scripts/generator.go b/scripts/generator.go index faceef3c..e33b6df7 100644 --- a/scripts/generator.go +++ b/scripts/generator.go @@ -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 { @@ -72,6 +75,7 @@ func main() { k := myWriter{ Kind: kind, + KindPlural: fmt.Sprintf("%ss", kind), KindLowerPlural: strings.ToLower(fmt.Sprintf("%ss", kind)), KindLowerSingular: strings.ToLower(kind), } @@ -79,21 +83,26 @@ func main() { 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) } @@ -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)) } } @@ -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 diff --git a/templates/generate-api.txt b/templates/generate-api.txt old mode 100644 new mode 100755 diff --git a/templates/generate-dao.txt b/templates/generate-dao.txt old mode 100644 new mode 100755 diff --git a/templates/generate-handlers.txt b/templates/generate-handlers.txt old mode 100644 new mode 100755 diff --git a/templates/generate-migration.txt b/templates/generate-migration.txt old mode 100644 new mode 100755 diff --git a/templates/generate-mock.txt b/templates/generate-mock.txt old mode 100644 new mode 100755 diff --git a/templates/generate-openapi-kind.txt b/templates/generate-openapi-kind.txt old mode 100644 new mode 100755 diff --git a/templates/generate-openapi.txt b/templates/generate-openapi.txt old mode 100644 new mode 100755 diff --git a/templates/generate-presenters.txt b/templates/generate-presenters.txt new file mode 100644 index 00000000..f57edb74 --- /dev/null +++ b/templates/generate-presenters.txt @@ -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, + } +} diff --git a/templates/generate-servicelocator.txt b/templates/generate-servicelocator.txt new file mode 100644 index 00000000..6f354d95 --- /dev/null +++ b/templates/generate-servicelocator.txt @@ -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(), + ) + } +} diff --git a/templates/generate-services.txt b/templates/generate-services.txt old mode 100644 new mode 100755 index e3062db3..3a362212 --- a/templates/generate-services.txt +++ b/templates/generate-services.txt @@ -18,9 +18,10 @@ 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, } } @@ -28,6 +29,7 @@ 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) { @@ -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 } @@ -51,6 +63,16 @@ 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 } @@ -58,6 +80,16 @@ func (s *sql{{.Kind}}Service) Delete(ctx context.Context, id string) *errors.Ser 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 } diff --git a/templates/generate-test-factories.txt b/templates/generate-test-factories.txt new file mode 100644 index 00000000..4bc49f16 --- /dev/null +++ b/templates/generate-test-factories.txt @@ -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 +} diff --git a/templates/generate-test.txt b/templates/generate-test.txt old mode 100644 new mode 100755 index 780f0918..f72badc8 --- a/templates/generate-test.txt +++ b/templates/generate-test.txt @@ -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()) @@ -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() @@ -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) @@ -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()