From 2db73c24b58c6db27627c4c6bc98c69adc7b71d5 Mon Sep 17 00:00:00 2001 From: Peter Marton Date: Fri, 30 Aug 2024 11:31:19 +0200 Subject: [PATCH] refactor(ent): extensions (#1448) --- openmeter/ent/entc.go | 32 +++++++++++++++++++ openmeter/ent/generate.go | 2 +- pkg/framework/entutils/entexpose/entexpose.go | 26 +++++++++++++++ .../entutils/{ => entexpose}/expose.tpl | 0 .../entutils/entpaginate/paginate.go | 26 +++++++++++++++ .../entutils/{ => entpaginate}/paginate.tpl | 0 .../{ => entpaginate}/paginate_test.go | 2 +- pkg/framework/entutils/testutils/ent1/entc.go | 32 +++++++++++++++++++ .../entutils/testutils/ent1/generate.go | 2 +- pkg/framework/entutils/testutils/ent2/entc.go | 31 ++++++++++++++++++ .../entutils/testutils/ent2/generate.go | 2 +- 11 files changed, 151 insertions(+), 4 deletions(-) create mode 100644 openmeter/ent/entc.go create mode 100644 pkg/framework/entutils/entexpose/entexpose.go rename pkg/framework/entutils/{ => entexpose}/expose.tpl (100%) create mode 100644 pkg/framework/entutils/entpaginate/paginate.go rename pkg/framework/entutils/{ => entpaginate}/paginate.tpl (100%) rename pkg/framework/entutils/{ => entpaginate}/paginate_test.go (99%) create mode 100644 pkg/framework/entutils/testutils/ent1/entc.go create mode 100644 pkg/framework/entutils/testutils/ent2/entc.go diff --git a/openmeter/ent/entc.go b/openmeter/ent/entc.go new file mode 100644 index 000000000..1b55551af --- /dev/null +++ b/openmeter/ent/entc.go @@ -0,0 +1,32 @@ +//go:build ignore +// +build ignore + +package main + +import ( + "log" + + "entgo.io/ent/entc" + "entgo.io/ent/entc/gen" + "github.com/openmeterio/openmeter/pkg/framework/entutils/entexpose" + "github.com/openmeterio/openmeter/pkg/framework/entutils/entpaginate" +) + +func main() { + err := entc.Generate("./schema", + &gen.Config{ + Features: []gen.Feature{ + gen.FeatureVersionedMigration, + gen.FeatureLock, + gen.FeatureUpsert, + }, + Target: "./db", + Schema: "./schema", + Package: "github.com/openmeterio/openmeter/openmeter/ent/db", + }, + entc.Extensions(entexpose.New(), entpaginate.New()), + ) + if err != nil { + log.Fatal("running ent codegen:", err) + } +} diff --git a/openmeter/ent/generate.go b/openmeter/ent/generate.go index 79480da1a..904d39c38 100644 --- a/openmeter/ent/generate.go +++ b/openmeter/ent/generate.go @@ -1,3 +1,3 @@ package entdb -//go:generate go run -mod=mod entgo.io/ent/cmd/ent generate --target ./db --feature sql/upsert --feature sql/lock --feature sql/versioned-migration --template ../../pkg/framework/entutils/expose.tpl --template ../../pkg/framework/entutils/paginate.tpl ./schema +//go:generate go run -mod=readonly entc.go diff --git a/pkg/framework/entutils/entexpose/entexpose.go b/pkg/framework/entutils/entexpose/entexpose.go new file mode 100644 index 000000000..9f344d56b --- /dev/null +++ b/pkg/framework/entutils/entexpose/entexpose.go @@ -0,0 +1,26 @@ +package entexpose + +import ( + _ "embed" + + "entgo.io/ent/entc" + "entgo.io/ent/entc/gen" +) + +//go:embed expose.tpl +var tmplfile string + +// Extension implements entc.Extension. +type Extension struct { + entc.DefaultExtension +} + +func (Extension) Templates() []*gen.Template { + return []*gen.Template{ + gen.MustParse(gen.NewTemplate("entexpose").Parse(tmplfile)), + } +} + +func New() *Extension { + return &Extension{} +} diff --git a/pkg/framework/entutils/expose.tpl b/pkg/framework/entutils/entexpose/expose.tpl similarity index 100% rename from pkg/framework/entutils/expose.tpl rename to pkg/framework/entutils/entexpose/expose.tpl diff --git a/pkg/framework/entutils/entpaginate/paginate.go b/pkg/framework/entutils/entpaginate/paginate.go new file mode 100644 index 000000000..cb857649a --- /dev/null +++ b/pkg/framework/entutils/entpaginate/paginate.go @@ -0,0 +1,26 @@ +package entpaginate + +import ( + _ "embed" + + "entgo.io/ent/entc" + "entgo.io/ent/entc/gen" +) + +//go:embed paginate.tpl +var tmplfile string + +// Extension implements entc.Extension. +type Extension struct { + entc.DefaultExtension +} + +func (Extension) Templates() []*gen.Template { + return []*gen.Template{ + gen.MustParse(gen.NewTemplate("entpaginate").Parse(tmplfile)), + } +} + +func New() *Extension { + return &Extension{} +} diff --git a/pkg/framework/entutils/paginate.tpl b/pkg/framework/entutils/entpaginate/paginate.tpl similarity index 100% rename from pkg/framework/entutils/paginate.tpl rename to pkg/framework/entutils/entpaginate/paginate.tpl diff --git a/pkg/framework/entutils/paginate_test.go b/pkg/framework/entutils/entpaginate/paginate_test.go similarity index 99% rename from pkg/framework/entutils/paginate_test.go rename to pkg/framework/entutils/entpaginate/paginate_test.go index 88f74690a..5bb9cb471 100644 --- a/pkg/framework/entutils/paginate_test.go +++ b/pkg/framework/entutils/entpaginate/paginate_test.go @@ -1,4 +1,4 @@ -package entutils_test +package entpaginate_test import ( "context" diff --git a/pkg/framework/entutils/testutils/ent1/entc.go b/pkg/framework/entutils/testutils/ent1/entc.go new file mode 100644 index 000000000..88be9b535 --- /dev/null +++ b/pkg/framework/entutils/testutils/ent1/entc.go @@ -0,0 +1,32 @@ +//go:build ignore +// +build ignore + +package main + +import ( + "log" + + "entgo.io/ent/entc" + "entgo.io/ent/entc/gen" + "github.com/openmeterio/openmeter/pkg/framework/entutils/entexpose" + "github.com/openmeterio/openmeter/pkg/framework/entutils/entpaginate" +) + +func main() { + err := entc.Generate("./schema", + &gen.Config{ + Features: []gen.Feature{ + gen.FeatureVersionedMigration, + gen.FeatureLock, + gen.FeatureUpsert, + }, + Target: "./db", + Schema: "./schema", + Package: "github.com/openmeterio/openmeter/pkg/framework/entutils/testutils/ent1/db", + }, + entc.Extensions(entexpose.New(), entpaginate.New()), + ) + if err != nil { + log.Fatal("running ent codegen:", err) + } +} diff --git a/pkg/framework/entutils/testutils/ent1/generate.go b/pkg/framework/entutils/testutils/ent1/generate.go index 47726fadc..dfc69c4f4 100644 --- a/pkg/framework/entutils/testutils/ent1/generate.go +++ b/pkg/framework/entutils/testutils/ent1/generate.go @@ -1,3 +1,3 @@ package ent1 -//go:generate go run -mod=mod entgo.io/ent/cmd/ent generate --target ./db --feature sql/upsert --feature sql/lock --feature sql/versioned-migration ./schema --template ../../expose.tpl --template ../../paginate.tpl +//go:generate go run -mod=mod entc.go diff --git a/pkg/framework/entutils/testutils/ent2/entc.go b/pkg/framework/entutils/testutils/ent2/entc.go new file mode 100644 index 000000000..e176c1fcf --- /dev/null +++ b/pkg/framework/entutils/testutils/ent2/entc.go @@ -0,0 +1,31 @@ +//go:build ignore +// +build ignore + +package main + +import ( + "log" + + "entgo.io/ent/entc" + "entgo.io/ent/entc/gen" + "github.com/openmeterio/openmeter/pkg/framework/entutils/entexpose" +) + +func main() { + err := entc.Generate("./schema", + &gen.Config{ + Features: []gen.Feature{ + gen.FeatureVersionedMigration, + gen.FeatureLock, + gen.FeatureUpsert, + }, + Target: "./db", + Schema: "./schema", + Package: "github.com/openmeterio/openmeter/pkg/framework/entutils/testutils/ent2/db", + }, + entc.Extensions(entexpose.New()), + ) + if err != nil { + log.Fatal("running ent codegen:", err) + } +} diff --git a/pkg/framework/entutils/testutils/ent2/generate.go b/pkg/framework/entutils/testutils/ent2/generate.go index 5ac7215a7..ab96df2b1 100644 --- a/pkg/framework/entutils/testutils/ent2/generate.go +++ b/pkg/framework/entutils/testutils/ent2/generate.go @@ -1,3 +1,3 @@ package ent2 -//go:generate go run -mod=mod entgo.io/ent/cmd/ent generate --target ./db --feature sql/upsert --feature sql/lock --feature sql/versioned-migration ./schema --template ../../expose.tpl +//go:generate go run -mod=mod entc.go