Skip to content

Commit

Permalink
refactor(ent): extensions (#1448)
Browse files Browse the repository at this point in the history
  • Loading branch information
hekike authored Aug 30, 2024
1 parent 403b6ee commit 2db73c2
Show file tree
Hide file tree
Showing 11 changed files with 151 additions and 4 deletions.
32 changes: 32 additions & 0 deletions openmeter/ent/entc.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
2 changes: 1 addition & 1 deletion openmeter/ent/generate.go
Original file line number Diff line number Diff line change
@@ -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
26 changes: 26 additions & 0 deletions pkg/framework/entutils/entexpose/entexpose.go
Original file line number Diff line number Diff line change
@@ -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{}
}
File renamed without changes.
26 changes: 26 additions & 0 deletions pkg/framework/entutils/entpaginate/paginate.go
Original file line number Diff line number Diff line change
@@ -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{}
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package entutils_test
package entpaginate_test

import (
"context"
Expand Down
32 changes: 32 additions & 0 deletions pkg/framework/entutils/testutils/ent1/entc.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
2 changes: 1 addition & 1 deletion pkg/framework/entutils/testutils/ent1/generate.go
Original file line number Diff line number Diff line change
@@ -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
31 changes: 31 additions & 0 deletions pkg/framework/entutils/testutils/ent2/entc.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
2 changes: 1 addition & 1 deletion pkg/framework/entutils/testutils/ent2/generate.go
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 2db73c2

Please sign in to comment.