diff --git a/adapter_test.go b/adapter_test.go index 31dd3a7..febed18 100644 --- a/adapter_test.go +++ b/adapter_test.go @@ -20,7 +20,7 @@ import ( "github.com/casbin/casbin/v2" "github.com/casbin/casbin/v2/util" - "github.com/casbin/ent-adapter/ent" + "github.com/casbin/ent-adapter/examples/ent" "github.com/stretchr/testify/assert" ) diff --git a/template/casbin.tmpl b/casbin.tmpl similarity index 98% rename from template/casbin.tmpl rename to casbin.tmpl index 0bcad82..e3e148e 100644 --- a/template/casbin.tmpl +++ b/casbin.tmpl @@ -1,9 +1,9 @@ -{{ /* Tell Intellij/GoLand to enable the autocompletion based on the *gen.Graph type. */ }} -{{ /* gotype: entgo.io/ent/entc/gen.Graph */ }} +{{/* Tell Intellij/GoLand to enable the autocompletion based on the *gen.Graph type. */}} +{{/* gotype: entgo.io/ent/entc/gen.Graph */}} {{ define "casbin" }} -{{ /* Add the base header for the generated file */ }} +{{/* Add the base header for the generated file */}} {{ $pkg := base $.Config.Package }} {{ template "header" $ }} diff --git a/ent/casbin.go b/examples/ent/casbin.go similarity index 99% rename from ent/casbin.go rename to examples/ent/casbin.go index 6d2d265..6ec6940 100644 --- a/ent/casbin.go +++ b/examples/ent/casbin.go @@ -12,8 +12,8 @@ import ( "entgo.io/ent/dialect" entsql "entgo.io/ent/dialect/sql" "github.com/casbin/casbin/v2/persist" - "github.com/casbin/ent-adapter/ent/casbinrule" - "github.com/casbin/ent-adapter/ent/predicate" + "github.com/casbin/ent-adapter/examples/ent/casbinrule" + "github.com/casbin/ent-adapter/examples/ent/predicate" "github.com/casbin/casbin/v2/model" _ "github.com/go-sql-driver/mysql" diff --git a/ent/casbinrule.go b/examples/ent/casbinrule.go similarity index 98% rename from ent/casbinrule.go rename to examples/ent/casbinrule.go index d029267..73e6de1 100644 --- a/ent/casbinrule.go +++ b/examples/ent/casbinrule.go @@ -8,7 +8,7 @@ import ( "entgo.io/ent" "entgo.io/ent/dialect/sql" - "github.com/casbin/ent-adapter/ent/casbinrule" + "github.com/casbin/ent-adapter/examples/ent/casbinrule" ) // CasbinRule is the model entity for the CasbinRule schema. diff --git a/ent/casbinrule/casbinrule.go b/examples/ent/casbinrule/casbinrule.go similarity index 100% rename from ent/casbinrule/casbinrule.go rename to examples/ent/casbinrule/casbinrule.go diff --git a/ent/casbinrule/where.go b/examples/ent/casbinrule/where.go similarity index 99% rename from ent/casbinrule/where.go rename to examples/ent/casbinrule/where.go index c779990..ce46588 100644 --- a/ent/casbinrule/where.go +++ b/examples/ent/casbinrule/where.go @@ -4,7 +4,7 @@ package casbinrule import ( "entgo.io/ent/dialect/sql" - "github.com/casbin/ent-adapter/ent/predicate" + "github.com/casbin/ent-adapter/examples/ent/predicate" ) // ID filters vertices based on their ID field. diff --git a/ent/casbinrule_create.go b/examples/ent/casbinrule_create.go similarity index 99% rename from ent/casbinrule_create.go rename to examples/ent/casbinrule_create.go index 64b8d93..4916bf9 100644 --- a/ent/casbinrule_create.go +++ b/examples/ent/casbinrule_create.go @@ -9,7 +9,7 @@ import ( "entgo.io/ent/dialect/sql/sqlgraph" "entgo.io/ent/schema/field" - "github.com/casbin/ent-adapter/ent/casbinrule" + "github.com/casbin/ent-adapter/examples/ent/casbinrule" ) // CasbinRuleCreate is the builder for creating a CasbinRule entity. diff --git a/ent/casbinrule_delete.go b/examples/ent/casbinrule_delete.go similarity index 95% rename from ent/casbinrule_delete.go rename to examples/ent/casbinrule_delete.go index b29b722..c7cd1b4 100644 --- a/ent/casbinrule_delete.go +++ b/examples/ent/casbinrule_delete.go @@ -8,8 +8,8 @@ import ( "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" "entgo.io/ent/schema/field" - "github.com/casbin/ent-adapter/ent/casbinrule" - "github.com/casbin/ent-adapter/ent/predicate" + "github.com/casbin/ent-adapter/examples/ent/casbinrule" + "github.com/casbin/ent-adapter/examples/ent/predicate" ) // CasbinRuleDelete is the builder for deleting a CasbinRule entity. diff --git a/ent/casbinrule_query.go b/examples/ent/casbinrule_query.go similarity index 99% rename from ent/casbinrule_query.go rename to examples/ent/casbinrule_query.go index 21eeac8..e8d537b 100644 --- a/ent/casbinrule_query.go +++ b/examples/ent/casbinrule_query.go @@ -11,8 +11,8 @@ import ( "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" "entgo.io/ent/schema/field" - "github.com/casbin/ent-adapter/ent/casbinrule" - "github.com/casbin/ent-adapter/ent/predicate" + "github.com/casbin/ent-adapter/examples/ent/casbinrule" + "github.com/casbin/ent-adapter/examples/ent/predicate" ) // CasbinRuleQuery is the builder for querying CasbinRule entities. diff --git a/ent/casbinrule_update.go b/examples/ent/casbinrule_update.go similarity index 99% rename from ent/casbinrule_update.go rename to examples/ent/casbinrule_update.go index d462041..5984ad4 100644 --- a/ent/casbinrule_update.go +++ b/examples/ent/casbinrule_update.go @@ -10,8 +10,8 @@ import ( "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" "entgo.io/ent/schema/field" - "github.com/casbin/ent-adapter/ent/casbinrule" - "github.com/casbin/ent-adapter/ent/predicate" + "github.com/casbin/ent-adapter/examples/ent/casbinrule" + "github.com/casbin/ent-adapter/examples/ent/predicate" ) // CasbinRuleUpdate is the builder for updating CasbinRule entities. diff --git a/ent/client.go b/examples/ent/client.go similarity index 98% rename from ent/client.go rename to examples/ent/client.go index ee8d988..a4562de 100644 --- a/ent/client.go +++ b/examples/ent/client.go @@ -9,12 +9,12 @@ import ( "log" "reflect" - "github.com/casbin/ent-adapter/ent/migrate" + "github.com/casbin/ent-adapter/examples/ent/migrate" "entgo.io/ent" "entgo.io/ent/dialect" "entgo.io/ent/dialect/sql" - "github.com/casbin/ent-adapter/ent/casbinrule" + "github.com/casbin/ent-adapter/examples/ent/casbinrule" ) // Client is the client that holds all ent builders. diff --git a/ent/ent.go b/examples/ent/ent.go similarity index 99% rename from ent/ent.go rename to examples/ent/ent.go index c648965..bb2de88 100644 --- a/ent/ent.go +++ b/examples/ent/ent.go @@ -12,7 +12,7 @@ import ( "entgo.io/ent" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" - "github.com/casbin/ent-adapter/ent/casbinrule" + "github.com/casbin/ent-adapter/examples/ent/casbinrule" ) // ent aliases to avoid import conflicts in user's code. diff --git a/ent/enttest/enttest.go b/examples/ent/enttest/enttest.go similarity index 91% rename from ent/enttest/enttest.go rename to examples/ent/enttest/enttest.go index a56c14d..b19f9e8 100644 --- a/ent/enttest/enttest.go +++ b/examples/ent/enttest/enttest.go @@ -5,12 +5,12 @@ package enttest import ( "context" - "github.com/casbin/ent-adapter/ent" + "github.com/casbin/ent-adapter/examples/ent" // required by schema hooks. - _ "github.com/casbin/ent-adapter/ent/runtime" + _ "github.com/casbin/ent-adapter/examples/ent/runtime" "entgo.io/ent/dialect/sql/schema" - "github.com/casbin/ent-adapter/ent/migrate" + "github.com/casbin/ent-adapter/examples/ent/migrate" ) type ( diff --git a/examples/ent/generate.go b/examples/ent/generate.go new file mode 100644 index 0000000..b0695b9 --- /dev/null +++ b/examples/ent/generate.go @@ -0,0 +1,3 @@ +//go:generate go run main.go + +package ent diff --git a/ent/hook/hook.go b/examples/ent/hook/hook.go similarity index 99% rename from ent/hook/hook.go rename to examples/ent/hook/hook.go index 11a5ead..a83ad68 100644 --- a/ent/hook/hook.go +++ b/examples/ent/hook/hook.go @@ -6,7 +6,7 @@ import ( "context" "fmt" - "github.com/casbin/ent-adapter/ent" + "github.com/casbin/ent-adapter/examples/ent" ) // The CasbinRuleFunc type is an adapter to allow the use of ordinary diff --git a/ent/generate.go b/examples/ent/main.go similarity index 73% rename from ent/generate.go rename to examples/ent/main.go index cb0bed9..bf26a4c 100644 --- a/ent/generate.go +++ b/examples/ent/main.go @@ -8,13 +8,13 @@ import ( "entgo.io/ent/entc" "entgo.io/ent/entc/gen" - "github.com/casbin/ent-adapter/template" + "github.com/casbin/ent-adapter" ) func main() { err := entc.Generate("./schema", &gen.Config{}, - entc.Extensions(&template.CasbinExtension{}), + entc.Extensions(&entadapter.CasbinExtension{}), ) if err != nil { log.Fatal("running ent codegen:", err) diff --git a/ent/migrate/migrate.go b/examples/ent/migrate/migrate.go similarity index 100% rename from ent/migrate/migrate.go rename to examples/ent/migrate/migrate.go diff --git a/ent/migrate/schema.go b/examples/ent/migrate/schema.go similarity index 100% rename from ent/migrate/schema.go rename to examples/ent/migrate/schema.go diff --git a/ent/mutation.go b/examples/ent/mutation.go similarity index 99% rename from ent/mutation.go rename to examples/ent/mutation.go index e016001..6e05312 100644 --- a/ent/mutation.go +++ b/examples/ent/mutation.go @@ -10,8 +10,8 @@ import ( "entgo.io/ent" "entgo.io/ent/dialect/sql" - "github.com/casbin/ent-adapter/ent/casbinrule" - "github.com/casbin/ent-adapter/ent/predicate" + "github.com/casbin/ent-adapter/examples/ent/casbinrule" + "github.com/casbin/ent-adapter/examples/ent/predicate" ) const ( diff --git a/ent/predicate/predicate.go b/examples/ent/predicate/predicate.go similarity index 100% rename from ent/predicate/predicate.go rename to examples/ent/predicate/predicate.go diff --git a/ent/runtime.go b/examples/ent/runtime.go similarity index 95% rename from ent/runtime.go rename to examples/ent/runtime.go index a83b349..d07a211 100644 --- a/ent/runtime.go +++ b/examples/ent/runtime.go @@ -3,8 +3,8 @@ package ent import ( - "github.com/casbin/ent-adapter/ent/casbinrule" - "github.com/casbin/ent-adapter/ent/schema" + "github.com/casbin/ent-adapter/examples/ent/casbinrule" + "github.com/casbin/ent-adapter/examples/ent/schema" ) // The init function reads all schema descriptors with runtime code diff --git a/ent/runtime/runtime.go b/examples/ent/runtime/runtime.go similarity index 89% rename from ent/runtime/runtime.go rename to examples/ent/runtime/runtime.go index 8f12166..2ba277f 100644 --- a/ent/runtime/runtime.go +++ b/examples/ent/runtime/runtime.go @@ -2,7 +2,7 @@ package runtime -// The schema-stitching logic is generated in github.com/casbin/ent-adapter/ent/runtime.go +// The schema-stitching logic is generated in github.com/casbin/ent-adapter/examples/ent/runtime.go const ( Version = "v0.14.0" // Version of ent codegen. diff --git a/ent/schema/casbinrule.go b/examples/ent/schema/casbinrule.go similarity index 75% rename from ent/schema/casbinrule.go rename to examples/ent/schema/casbinrule.go index bfb047b..0aa265e 100644 --- a/ent/schema/casbinrule.go +++ b/examples/ent/schema/casbinrule.go @@ -2,7 +2,7 @@ package schema import ( "entgo.io/ent" - "github.com/casbin/ent-adapter/template" + "github.com/casbin/ent-adapter" ) // CasbinRule holds the schema definition for the CasbinRule entity. @@ -12,6 +12,6 @@ type CasbinRule struct { func (CasbinRule) Mixin() []ent.Mixin { return []ent.Mixin{ - template.CasbinRuleMixin{}, + entadapter.CasbinRuleMixin{}, } } diff --git a/ent/tx.go b/examples/ent/tx.go similarity index 100% rename from ent/tx.go rename to examples/ent/tx.go diff --git a/extension.go b/extension.go new file mode 100644 index 0000000..df45b36 --- /dev/null +++ b/extension.go @@ -0,0 +1,29 @@ +package entadapter + +import ( + "fmt" + "path/filepath" + "runtime" + + "entgo.io/ent/entc" + "entgo.io/ent/entc/gen" +) + +type CasbinExtension struct { + entc.DefaultExtension +} + +func (*CasbinExtension) Templates() []*gen.Template { + _, filename, _, ok := runtime.Caller(0) + if !ok { + _ = fmt.Errorf("error retrieving current file path") + } + + dir := filepath.Dir(filename) + + relativePath := filepath.Join(dir, "casbin.tmpl") + + return []*gen.Template{ + gen.MustParse(gen.NewTemplate("casbin").ParseFiles(relativePath)), + } +} diff --git a/template/mixin.go b/mixin.go similarity index 97% rename from template/mixin.go rename to mixin.go index c8e43ca..0d98eaf 100644 --- a/template/mixin.go +++ b/mixin.go @@ -1,4 +1,4 @@ -package template +package entadapter import ( "entgo.io/ent" diff --git a/template/extension.go b/template/extension.go deleted file mode 100644 index 7aa2539..0000000 --- a/template/extension.go +++ /dev/null @@ -1,16 +0,0 @@ -package template - -import ( - "entgo.io/ent/entc" - "entgo.io/ent/entc/gen" -) - -type CasbinExtension struct { - entc.DefaultExtension -} - -func (*CasbinExtension) Templates() []*gen.Template { - return []*gen.Template{ - gen.MustParse(gen.NewTemplate("casbin").ParseFiles("../template/casbin.tmpl")), - } -}