From ddc46d34d861c15139bcc69a4c684f03d761a9ba Mon Sep 17 00:00:00 2001 From: Ariel Mashraki <7413593+a8m@users.noreply.github.com> Date: Sun, 17 Jul 2022 16:49:11 +0300 Subject: [PATCH] schema/field: correctly extract package names from type identifier (#2765) Fixes https://github.com/ent/ent/issues/2761 --- entc/gen/template/import.tmpl | 2 +- .../edgeschema/ent/internal/schema.go | 2 +- entc/integration/ent/entql.go | 8 +- entc/integration/ent/migrate/schema.go | 1 + entc/integration/ent/mutation.go | 80 ++++++++++++++++++- entc/integration/ent/schema/task.go | 2 + entc/integration/ent/task.go | 16 ++++ entc/integration/ent/task/task.go | 3 + entc/integration/ent/task/where.go | 14 ++++ entc/integration/ent/task_create.go | 74 +++++++++++++++++ entc/integration/ent/task_update.go | 50 ++++++++++++ entc/integration/gremlin/ent/mutation.go | 80 ++++++++++++++++++- entc/integration/gremlin/ent/task.go | 21 +++-- entc/integration/gremlin/ent/task/task.go | 2 + entc/integration/gremlin/ent/task/where.go | 14 ++++ entc/integration/gremlin/ent/task_create.go | 9 +++ entc/integration/gremlin/ent/task_update.go | 44 ++++++++++ entc/integration/integration_test.go | 2 +- .../privacy/ent/internal/schema.go | 2 +- schema/field/field.go | 16 ++++ schema/field/field_test.go | 11 +++ 21 files changed, 438 insertions(+), 15 deletions(-) diff --git a/entc/gen/template/import.tmpl b/entc/gen/template/import.tmpl index e549c8fce..ad1e72369 100644 --- a/entc/gen/template/import.tmpl +++ b/entc/gen/template/import.tmpl @@ -42,7 +42,7 @@ import ( {{- range $f := $fields }} {{- $pkg := $f.Type.PkgPath }} {{- if and $pkg (not (hasImport (base $pkg ))) }} - {{- $name := trim (index (split $f.Type.RType.Ident ".") 0) "[]*" }} + {{- $name := $f.Type.PkgName }} {{ if ne $name (base $pkg) }}{{ $name }} {{ end}}"{{ $pkg }}" {{- end }} {{- end }} diff --git a/entc/integration/edgeschema/ent/internal/schema.go b/entc/integration/edgeschema/ent/internal/schema.go index 52e5ec037..ee9b5b5b1 100644 --- a/entc/integration/edgeschema/ent/internal/schema.go +++ b/entc/integration/edgeschema/ent/internal/schema.go @@ -10,4 +10,4 @@ // Package internal holds a loadable version of the latest schema. package internal -const Schema = `{"Schema":"entgo.io/ent/entc/integration/edgeschema/ent/schema","Package":"entgo.io/ent/entc/integration/edgeschema/ent","Schemas":[{"name":"Friendship","config":{"Table":""},"edges":[{"name":"user","type":"User","field":"user_id","unique":true,"required":true},{"name":"friend","type":"User","field":"friend_id","unique":true,"required":true}],"fields":[{"name":"weight","type":{"Type":12,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"default":true,"default_value":1,"default_kind":2,"position":{"Index":0,"MixedIn":false,"MixinIndex":0}},{"name":"created_at","type":{"Type":2,"Ident":"","PkgPath":"time","PkgName":"","Nillable":false,"RType":null},"default":true,"default_kind":19,"position":{"Index":1,"MixedIn":false,"MixinIndex":0}},{"name":"user_id","type":{"Type":12,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"position":{"Index":2,"MixedIn":false,"MixinIndex":0}},{"name":"friend_id","type":{"Type":12,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"position":{"Index":3,"MixedIn":false,"MixinIndex":0}}],"indexes":[{"fields":["created_at"]}]},{"name":"Group","config":{"Table":""},"edges":[{"name":"users","type":"User","ref_name":"groups","through":{"N":"joined_users","T":"UserGroup"},"inverse":true}],"fields":[{"name":"name","type":{"Type":7,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"default":true,"default_value":"Unknown","default_kind":24,"position":{"Index":0,"MixedIn":false,"MixinIndex":0}}]},{"name":"Relationship","config":{"Table":""},"edges":[{"name":"user","type":"User","field":"user_id","unique":true,"required":true},{"name":"relative","type":"User","field":"relative_id","unique":true,"required":true},{"name":"info","type":"RelationshipInfo","field":"info_id","unique":true}],"fields":[{"name":"weight","type":{"Type":12,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"default":true,"default_value":1,"default_kind":2,"position":{"Index":0,"MixedIn":false,"MixinIndex":0}},{"name":"user_id","type":{"Type":12,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"position":{"Index":1,"MixedIn":false,"MixinIndex":0}},{"name":"relative_id","type":{"Type":12,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"position":{"Index":2,"MixedIn":false,"MixinIndex":0}},{"name":"info_id","type":{"Type":12,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"optional":true,"position":{"Index":3,"MixedIn":false,"MixinIndex":0}}],"indexes":[{"fields":["weight"]},{"unique":true,"edges":["info"]}],"annotations":{"Fields":{"ID":["user_id","relative_id"],"StructTag":null}}},{"name":"RelationshipInfo","config":{"Table":""},"fields":[{"name":"text","type":{"Type":7,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"position":{"Index":0,"MixedIn":false,"MixinIndex":0}}]},{"name":"Role","config":{"Table":""},"edges":[{"name":"user","type":"User","ref_name":"roles","through":{"N":"roles_users","T":"RoleUser"},"inverse":true}],"fields":[{"name":"name","type":{"Type":7,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"unique":true,"position":{"Index":0,"MixedIn":false,"MixinIndex":0}},{"name":"created_at","type":{"Type":2,"Ident":"","PkgPath":"time","PkgName":"","Nillable":false,"RType":null},"default":true,"default_kind":19,"position":{"Index":1,"MixedIn":false,"MixinIndex":0}}]},{"name":"RoleUser","config":{"Table":""},"edges":[{"name":"role","type":"Role","field":"role_id","unique":true,"required":true},{"name":"user","type":"User","field":"user_id","unique":true,"required":true}],"fields":[{"name":"created_at","type":{"Type":2,"Ident":"","PkgPath":"time","PkgName":"","Nillable":false,"RType":null},"default":true,"default_kind":19,"position":{"Index":0,"MixedIn":false,"MixinIndex":0}},{"name":"role_id","type":{"Type":12,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"position":{"Index":1,"MixedIn":false,"MixinIndex":0}},{"name":"user_id","type":{"Type":12,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"position":{"Index":2,"MixedIn":false,"MixinIndex":0}}],"annotations":{"Fields":{"ID":["user_id","role_id"],"StructTag":null}}},{"name":"Tag","config":{"Table":""},"edges":[{"name":"tweets","type":"Tweet","through":{"N":"tweet_tags","T":"TweetTag"}}],"fields":[{"name":"value","type":{"Type":7,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"position":{"Index":0,"MixedIn":false,"MixinIndex":0}}]},{"name":"Tweet","config":{"Table":""},"edges":[{"name":"liked_users","type":"User","ref_name":"liked_tweets","through":{"N":"likes","T":"TweetLike"},"inverse":true},{"name":"user","type":"User","ref_name":"tweets","through":{"N":"tweet_user","T":"UserTweet"},"inverse":true,"comment":"The uniqueness is enforced on the edge schema"},{"name":"tags","type":"Tag","ref_name":"tweets","through":{"N":"tweet_tags","T":"TweetTag"},"inverse":true}],"fields":[{"name":"text","type":{"Type":7,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"size":2147483647,"position":{"Index":0,"MixedIn":false,"MixinIndex":0}}]},{"name":"TweetLike","config":{"Table":""},"edges":[{"name":"tweet","type":"Tweet","field":"tweet_id","unique":true,"required":true},{"name":"user","type":"User","field":"user_id","unique":true,"required":true}],"fields":[{"name":"liked_at","type":{"Type":2,"Ident":"","PkgPath":"time","PkgName":"","Nillable":false,"RType":null},"default":true,"default_kind":19,"position":{"Index":0,"MixedIn":false,"MixinIndex":0}},{"name":"user_id","type":{"Type":12,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"position":{"Index":1,"MixedIn":false,"MixinIndex":0}},{"name":"tweet_id","type":{"Type":12,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"position":{"Index":2,"MixedIn":false,"MixinIndex":0}}],"policy":[{"Index":0,"MixedIn":false,"MixinIndex":0}],"annotations":{"Fields":{"ID":["user_id","tweet_id"],"StructTag":null}}},{"name":"TweetTag","config":{"Table":""},"edges":[{"name":"tag","type":"Tag","field":"tag_id","unique":true,"required":true},{"name":"tweet","type":"Tweet","field":"tweet_id","unique":true,"required":true}],"fields":[{"name":"id","type":{"Type":4,"Ident":"uuid.UUID","PkgPath":"github.com/google/uuid","PkgName":"","Nillable":false,"RType":{"Name":"UUID","Ident":"uuid.UUID","Kind":17,"PkgPath":"github.com/google/uuid","Methods":{"ClockSequence":{"In":[],"Out":[{"Name":"int","Ident":"int","Kind":2,"PkgPath":"","Methods":null}]},"Domain":{"In":[],"Out":[{"Name":"Domain","Ident":"uuid.Domain","Kind":8,"PkgPath":"github.com/google/uuid","Methods":null}]},"ID":{"In":[],"Out":[{"Name":"uint32","Ident":"uint32","Kind":10,"PkgPath":"","Methods":null}]},"MarshalBinary":{"In":[],"Out":[{"Name":"","Ident":"[]uint8","Kind":23,"PkgPath":"","Methods":null},{"Name":"error","Ident":"error","Kind":20,"PkgPath":"","Methods":null}]},"MarshalText":{"In":[],"Out":[{"Name":"","Ident":"[]uint8","Kind":23,"PkgPath":"","Methods":null},{"Name":"error","Ident":"error","Kind":20,"PkgPath":"","Methods":null}]},"NodeID":{"In":[],"Out":[{"Name":"","Ident":"[]uint8","Kind":23,"PkgPath":"","Methods":null}]},"Scan":{"In":[{"Name":"","Ident":"interface {}","Kind":20,"PkgPath":"","Methods":null}],"Out":[{"Name":"error","Ident":"error","Kind":20,"PkgPath":"","Methods":null}]},"String":{"In":[],"Out":[{"Name":"string","Ident":"string","Kind":24,"PkgPath":"","Methods":null}]},"Time":{"In":[],"Out":[{"Name":"Time","Ident":"uuid.Time","Kind":6,"PkgPath":"github.com/google/uuid","Methods":null}]},"URN":{"In":[],"Out":[{"Name":"string","Ident":"string","Kind":24,"PkgPath":"","Methods":null}]},"UnmarshalBinary":{"In":[{"Name":"","Ident":"[]uint8","Kind":23,"PkgPath":"","Methods":null}],"Out":[{"Name":"error","Ident":"error","Kind":20,"PkgPath":"","Methods":null}]},"UnmarshalText":{"In":[{"Name":"","Ident":"[]uint8","Kind":23,"PkgPath":"","Methods":null}],"Out":[{"Name":"error","Ident":"error","Kind":20,"PkgPath":"","Methods":null}]},"Value":{"In":[],"Out":[{"Name":"Value","Ident":"driver.Value","Kind":20,"PkgPath":"database/sql/driver","Methods":null},{"Name":"error","Ident":"error","Kind":20,"PkgPath":"","Methods":null}]},"Variant":{"In":[],"Out":[{"Name":"Variant","Ident":"uuid.Variant","Kind":8,"PkgPath":"github.com/google/uuid","Methods":null}]},"Version":{"In":[],"Out":[{"Name":"Version","Ident":"uuid.Version","Kind":8,"PkgPath":"github.com/google/uuid","Methods":null}]}}}},"default":true,"default_kind":19,"position":{"Index":0,"MixedIn":false,"MixinIndex":0}},{"name":"added_at","type":{"Type":2,"Ident":"","PkgPath":"time","PkgName":"","Nillable":false,"RType":null},"default":true,"default_kind":19,"position":{"Index":1,"MixedIn":false,"MixinIndex":0}},{"name":"tag_id","type":{"Type":12,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"position":{"Index":2,"MixedIn":false,"MixinIndex":0}},{"name":"tweet_id","type":{"Type":12,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"position":{"Index":3,"MixedIn":false,"MixinIndex":0}}]},{"name":"User","config":{"Table":""},"edges":[{"name":"groups","type":"Group","through":{"N":"joined_groups","T":"UserGroup"}},{"name":"friends","type":"User","through":{"N":"friendships","T":"Friendship"}},{"name":"relatives","type":"User","through":{"N":"relationship","T":"Relationship"}},{"name":"liked_tweets","type":"Tweet","through":{"N":"likes","T":"TweetLike"}},{"name":"tweets","type":"Tweet","through":{"N":"user_tweets","T":"UserTweet"}},{"name":"roles","type":"Role","through":{"N":"roles_users","T":"RoleUser"}}],"fields":[{"name":"name","type":{"Type":7,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"default":true,"default_value":"Unknown","default_kind":24,"position":{"Index":0,"MixedIn":false,"MixinIndex":0}}],"policy":[{"Index":0,"MixedIn":false,"MixinIndex":0}]},{"name":"UserGroup","config":{"Table":""},"edges":[{"name":"user","type":"User","field":"user_id","unique":true,"required":true},{"name":"group","type":"Group","field":"group_id","unique":true,"required":true}],"fields":[{"name":"joined_at","type":{"Type":2,"Ident":"","PkgPath":"time","PkgName":"","Nillable":false,"RType":null},"default":true,"default_kind":19,"position":{"Index":0,"MixedIn":false,"MixinIndex":0}},{"name":"user_id","type":{"Type":12,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"position":{"Index":1,"MixedIn":false,"MixinIndex":0}},{"name":"group_id","type":{"Type":12,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"position":{"Index":2,"MixedIn":false,"MixinIndex":0}}]},{"name":"UserTweet","config":{"Table":""},"edges":[{"name":"user","type":"User","field":"user_id","unique":true,"required":true},{"name":"tweet","type":"Tweet","field":"tweet_id","unique":true,"required":true}],"fields":[{"name":"created_at","type":{"Type":2,"Ident":"","PkgPath":"time","PkgName":"","Nillable":false,"RType":null},"default":true,"default_kind":19,"position":{"Index":0,"MixedIn":false,"MixinIndex":0}},{"name":"user_id","type":{"Type":12,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"position":{"Index":1,"MixedIn":false,"MixinIndex":0}},{"name":"tweet_id","type":{"Type":12,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"position":{"Index":2,"MixedIn":false,"MixinIndex":0}}],"indexes":[{"unique":true,"fields":["tweet_id"]}]}],"Features":["sql/upsert","privacy","schema/snapshot"]}` +const Schema = `{"Schema":"entgo.io/ent/entc/integration/edgeschema/ent/schema","Package":"entgo.io/ent/entc/integration/edgeschema/ent","Schemas":[{"name":"Friendship","config":{"Table":""},"edges":[{"name":"user","type":"User","field":"user_id","unique":true,"required":true},{"name":"friend","type":"User","field":"friend_id","unique":true,"required":true}],"fields":[{"name":"weight","type":{"Type":12,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"default":true,"default_value":1,"default_kind":2,"position":{"Index":0,"MixedIn":false,"MixinIndex":0}},{"name":"created_at","type":{"Type":2,"Ident":"","PkgPath":"time","PkgName":"","Nillable":false,"RType":null},"default":true,"default_kind":19,"position":{"Index":1,"MixedIn":false,"MixinIndex":0}},{"name":"user_id","type":{"Type":12,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"position":{"Index":2,"MixedIn":false,"MixinIndex":0}},{"name":"friend_id","type":{"Type":12,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"position":{"Index":3,"MixedIn":false,"MixinIndex":0}}],"indexes":[{"fields":["created_at"]}]},{"name":"Group","config":{"Table":""},"edges":[{"name":"users","type":"User","ref_name":"groups","through":{"N":"joined_users","T":"UserGroup"},"inverse":true}],"fields":[{"name":"name","type":{"Type":7,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"default":true,"default_value":"Unknown","default_kind":24,"position":{"Index":0,"MixedIn":false,"MixinIndex":0}}]},{"name":"Relationship","config":{"Table":""},"edges":[{"name":"user","type":"User","field":"user_id","unique":true,"required":true},{"name":"relative","type":"User","field":"relative_id","unique":true,"required":true},{"name":"info","type":"RelationshipInfo","field":"info_id","unique":true}],"fields":[{"name":"weight","type":{"Type":12,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"default":true,"default_value":1,"default_kind":2,"position":{"Index":0,"MixedIn":false,"MixinIndex":0}},{"name":"user_id","type":{"Type":12,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"position":{"Index":1,"MixedIn":false,"MixinIndex":0}},{"name":"relative_id","type":{"Type":12,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"position":{"Index":2,"MixedIn":false,"MixinIndex":0}},{"name":"info_id","type":{"Type":12,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"optional":true,"position":{"Index":3,"MixedIn":false,"MixinIndex":0}}],"indexes":[{"fields":["weight"]},{"unique":true,"edges":["info"]}],"annotations":{"Fields":{"ID":["user_id","relative_id"],"StructTag":null}}},{"name":"RelationshipInfo","config":{"Table":""},"fields":[{"name":"text","type":{"Type":7,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"position":{"Index":0,"MixedIn":false,"MixinIndex":0}}]},{"name":"Role","config":{"Table":""},"edges":[{"name":"user","type":"User","ref_name":"roles","through":{"N":"roles_users","T":"RoleUser"},"inverse":true}],"fields":[{"name":"name","type":{"Type":7,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"unique":true,"position":{"Index":0,"MixedIn":false,"MixinIndex":0}},{"name":"created_at","type":{"Type":2,"Ident":"","PkgPath":"time","PkgName":"","Nillable":false,"RType":null},"default":true,"default_kind":19,"position":{"Index":1,"MixedIn":false,"MixinIndex":0}}]},{"name":"RoleUser","config":{"Table":""},"edges":[{"name":"role","type":"Role","field":"role_id","unique":true,"required":true},{"name":"user","type":"User","field":"user_id","unique":true,"required":true}],"fields":[{"name":"created_at","type":{"Type":2,"Ident":"","PkgPath":"time","PkgName":"","Nillable":false,"RType":null},"default":true,"default_kind":19,"position":{"Index":0,"MixedIn":false,"MixinIndex":0}},{"name":"role_id","type":{"Type":12,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"position":{"Index":1,"MixedIn":false,"MixinIndex":0}},{"name":"user_id","type":{"Type":12,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"position":{"Index":2,"MixedIn":false,"MixinIndex":0}}],"annotations":{"Fields":{"ID":["user_id","role_id"],"StructTag":null}}},{"name":"Tag","config":{"Table":""},"edges":[{"name":"tweets","type":"Tweet","through":{"N":"tweet_tags","T":"TweetTag"}}],"fields":[{"name":"value","type":{"Type":7,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"position":{"Index":0,"MixedIn":false,"MixinIndex":0}}]},{"name":"Tweet","config":{"Table":""},"edges":[{"name":"liked_users","type":"User","ref_name":"liked_tweets","through":{"N":"likes","T":"TweetLike"},"inverse":true},{"name":"user","type":"User","ref_name":"tweets","through":{"N":"tweet_user","T":"UserTweet"},"inverse":true,"comment":"The uniqueness is enforced on the edge schema"},{"name":"tags","type":"Tag","ref_name":"tweets","through":{"N":"tweet_tags","T":"TweetTag"},"inverse":true}],"fields":[{"name":"text","type":{"Type":7,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"size":2147483647,"position":{"Index":0,"MixedIn":false,"MixinIndex":0}}]},{"name":"TweetLike","config":{"Table":""},"edges":[{"name":"tweet","type":"Tweet","field":"tweet_id","unique":true,"required":true},{"name":"user","type":"User","field":"user_id","unique":true,"required":true}],"fields":[{"name":"liked_at","type":{"Type":2,"Ident":"","PkgPath":"time","PkgName":"","Nillable":false,"RType":null},"default":true,"default_kind":19,"position":{"Index":0,"MixedIn":false,"MixinIndex":0}},{"name":"user_id","type":{"Type":12,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"position":{"Index":1,"MixedIn":false,"MixinIndex":0}},{"name":"tweet_id","type":{"Type":12,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"position":{"Index":2,"MixedIn":false,"MixinIndex":0}}],"policy":[{"Index":0,"MixedIn":false,"MixinIndex":0}],"annotations":{"Fields":{"ID":["user_id","tweet_id"],"StructTag":null}}},{"name":"TweetTag","config":{"Table":""},"edges":[{"name":"tag","type":"Tag","field":"tag_id","unique":true,"required":true},{"name":"tweet","type":"Tweet","field":"tweet_id","unique":true,"required":true}],"fields":[{"name":"id","type":{"Type":4,"Ident":"uuid.UUID","PkgPath":"github.com/google/uuid","PkgName":"uuid","Nillable":false,"RType":{"Name":"UUID","Ident":"uuid.UUID","Kind":17,"PkgPath":"github.com/google/uuid","Methods":{"ClockSequence":{"In":[],"Out":[{"Name":"int","Ident":"int","Kind":2,"PkgPath":"","Methods":null}]},"Domain":{"In":[],"Out":[{"Name":"Domain","Ident":"uuid.Domain","Kind":8,"PkgPath":"github.com/google/uuid","Methods":null}]},"ID":{"In":[],"Out":[{"Name":"uint32","Ident":"uint32","Kind":10,"PkgPath":"","Methods":null}]},"MarshalBinary":{"In":[],"Out":[{"Name":"","Ident":"[]uint8","Kind":23,"PkgPath":"","Methods":null},{"Name":"error","Ident":"error","Kind":20,"PkgPath":"","Methods":null}]},"MarshalText":{"In":[],"Out":[{"Name":"","Ident":"[]uint8","Kind":23,"PkgPath":"","Methods":null},{"Name":"error","Ident":"error","Kind":20,"PkgPath":"","Methods":null}]},"NodeID":{"In":[],"Out":[{"Name":"","Ident":"[]uint8","Kind":23,"PkgPath":"","Methods":null}]},"Scan":{"In":[{"Name":"","Ident":"interface {}","Kind":20,"PkgPath":"","Methods":null}],"Out":[{"Name":"error","Ident":"error","Kind":20,"PkgPath":"","Methods":null}]},"String":{"In":[],"Out":[{"Name":"string","Ident":"string","Kind":24,"PkgPath":"","Methods":null}]},"Time":{"In":[],"Out":[{"Name":"Time","Ident":"uuid.Time","Kind":6,"PkgPath":"github.com/google/uuid","Methods":null}]},"URN":{"In":[],"Out":[{"Name":"string","Ident":"string","Kind":24,"PkgPath":"","Methods":null}]},"UnmarshalBinary":{"In":[{"Name":"","Ident":"[]uint8","Kind":23,"PkgPath":"","Methods":null}],"Out":[{"Name":"error","Ident":"error","Kind":20,"PkgPath":"","Methods":null}]},"UnmarshalText":{"In":[{"Name":"","Ident":"[]uint8","Kind":23,"PkgPath":"","Methods":null}],"Out":[{"Name":"error","Ident":"error","Kind":20,"PkgPath":"","Methods":null}]},"Value":{"In":[],"Out":[{"Name":"Value","Ident":"driver.Value","Kind":20,"PkgPath":"database/sql/driver","Methods":null},{"Name":"error","Ident":"error","Kind":20,"PkgPath":"","Methods":null}]},"Variant":{"In":[],"Out":[{"Name":"Variant","Ident":"uuid.Variant","Kind":8,"PkgPath":"github.com/google/uuid","Methods":null}]},"Version":{"In":[],"Out":[{"Name":"Version","Ident":"uuid.Version","Kind":8,"PkgPath":"github.com/google/uuid","Methods":null}]}}}},"default":true,"default_kind":19,"position":{"Index":0,"MixedIn":false,"MixinIndex":0}},{"name":"added_at","type":{"Type":2,"Ident":"","PkgPath":"time","PkgName":"","Nillable":false,"RType":null},"default":true,"default_kind":19,"position":{"Index":1,"MixedIn":false,"MixinIndex":0}},{"name":"tag_id","type":{"Type":12,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"position":{"Index":2,"MixedIn":false,"MixinIndex":0}},{"name":"tweet_id","type":{"Type":12,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"position":{"Index":3,"MixedIn":false,"MixinIndex":0}}]},{"name":"User","config":{"Table":""},"edges":[{"name":"groups","type":"Group","through":{"N":"joined_groups","T":"UserGroup"}},{"name":"friends","type":"User","through":{"N":"friendships","T":"Friendship"}},{"name":"relatives","type":"User","through":{"N":"relationship","T":"Relationship"}},{"name":"liked_tweets","type":"Tweet","through":{"N":"likes","T":"TweetLike"}},{"name":"tweets","type":"Tweet","through":{"N":"user_tweets","T":"UserTweet"}},{"name":"roles","type":"Role","through":{"N":"roles_users","T":"RoleUser"}}],"fields":[{"name":"name","type":{"Type":7,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"default":true,"default_value":"Unknown","default_kind":24,"position":{"Index":0,"MixedIn":false,"MixinIndex":0}}],"policy":[{"Index":0,"MixedIn":false,"MixinIndex":0}]},{"name":"UserGroup","config":{"Table":""},"edges":[{"name":"user","type":"User","field":"user_id","unique":true,"required":true},{"name":"group","type":"Group","field":"group_id","unique":true,"required":true}],"fields":[{"name":"joined_at","type":{"Type":2,"Ident":"","PkgPath":"time","PkgName":"","Nillable":false,"RType":null},"default":true,"default_kind":19,"position":{"Index":0,"MixedIn":false,"MixinIndex":0}},{"name":"user_id","type":{"Type":12,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"position":{"Index":1,"MixedIn":false,"MixinIndex":0}},{"name":"group_id","type":{"Type":12,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"position":{"Index":2,"MixedIn":false,"MixinIndex":0}}]},{"name":"UserTweet","config":{"Table":""},"edges":[{"name":"user","type":"User","field":"user_id","unique":true,"required":true},{"name":"tweet","type":"Tweet","field":"tweet_id","unique":true,"required":true}],"fields":[{"name":"created_at","type":{"Type":2,"Ident":"","PkgPath":"time","PkgName":"","Nillable":false,"RType":null},"default":true,"default_kind":19,"position":{"Index":0,"MixedIn":false,"MixinIndex":0}},{"name":"user_id","type":{"Type":12,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"position":{"Index":1,"MixedIn":false,"MixinIndex":0}},{"name":"tweet_id","type":{"Type":12,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"position":{"Index":2,"MixedIn":false,"MixinIndex":0}}],"indexes":[{"unique":true,"fields":["tweet_id"]}]}],"Features":["sql/upsert","privacy","schema/snapshot"]}` diff --git a/entc/integration/ent/entql.go b/entc/integration/ent/entql.go index d103e2731..9ae3f5846 100644 --- a/entc/integration/ent/entql.go +++ b/entc/integration/ent/entql.go @@ -306,7 +306,8 @@ var schemaGraph = func() *sqlgraph.Schema { }, Type: "Task", Fields: map[string]*sqlgraph.FieldSpec{ - enttask.FieldPriority: {Type: field.TypeInt, Column: enttask.FieldPriority}, + enttask.FieldPriority: {Type: field.TypeInt, Column: enttask.FieldPriority}, + enttask.FieldPriorities: {Type: field.TypeJSON, Column: enttask.FieldPriorities}, }, } graph.Nodes[14] = &sqlgraph.Node{ @@ -1935,6 +1936,11 @@ func (f *TaskFilter) WherePriority(p entql.IntP) { f.Where(p.Field(enttask.FieldPriority)) } +// WherePriorities applies the entql json.RawMessage predicate on the priorities field. +func (f *TaskFilter) WherePriorities(p entql.BytesP) { + f.Where(p.Field(enttask.FieldPriorities)) +} + // addPredicate implements the predicateAdder interface. func (uq *UserQuery) addPredicate(pred func(s *sql.Selector)) { uq.predicates = append(uq.predicates, pred) diff --git a/entc/integration/ent/migrate/schema.go b/entc/integration/ent/migrate/schema.go index 7deefac22..be81a68d0 100644 --- a/entc/integration/ent/migrate/schema.go +++ b/entc/integration/ent/migrate/schema.go @@ -374,6 +374,7 @@ var ( TasksColumns = []*schema.Column{ {Name: "id", Type: field.TypeInt, Increment: true}, {Name: "priority", Type: field.TypeInt, Default: 1}, + {Name: "priorities", Type: field.TypeJSON, Nullable: true}, } // TasksTable holds the schema information for the "tasks" table. TasksTable = &schema.Table{ diff --git a/entc/integration/ent/mutation.go b/entc/integration/ent/mutation.go index 07ae2336a..c8526de71 100644 --- a/entc/integration/ent/mutation.go +++ b/entc/integration/ent/mutation.go @@ -12491,6 +12491,7 @@ type TaskMutation struct { id *int priority *task.Priority addpriority *task.Priority + priorities *map[string]task.Priority clearedFields map[string]struct{} done bool oldValue func(context.Context) (*Task, error) @@ -12651,6 +12652,55 @@ func (m *TaskMutation) ResetPriority() { m.addpriority = nil } +// SetPriorities sets the "priorities" field. +func (m *TaskMutation) SetPriorities(value map[string]task.Priority) { + m.priorities = &value +} + +// Priorities returns the value of the "priorities" field in the mutation. +func (m *TaskMutation) Priorities() (r map[string]task.Priority, exists bool) { + v := m.priorities + if v == nil { + return + } + return *v, true +} + +// OldPriorities returns the old "priorities" field's value of the Task entity. +// If the Task object wasn't provided to the builder, the object is fetched from the database. +// An error is returned if the mutation operation is not UpdateOne, or the database query fails. +func (m *TaskMutation) OldPriorities(ctx context.Context) (v map[string]task.Priority, err error) { + if !m.op.Is(OpUpdateOne) { + return v, errors.New("OldPriorities is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, errors.New("OldPriorities requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldPriorities: %w", err) + } + return oldValue.Priorities, nil +} + +// ClearPriorities clears the value of the "priorities" field. +func (m *TaskMutation) ClearPriorities() { + m.priorities = nil + m.clearedFields[enttask.FieldPriorities] = struct{}{} +} + +// PrioritiesCleared returns if the "priorities" field was cleared in this mutation. +func (m *TaskMutation) PrioritiesCleared() bool { + _, ok := m.clearedFields[enttask.FieldPriorities] + return ok +} + +// ResetPriorities resets all changes to the "priorities" field. +func (m *TaskMutation) ResetPriorities() { + m.priorities = nil + delete(m.clearedFields, enttask.FieldPriorities) +} + // Where appends a list predicates to the TaskMutation builder. func (m *TaskMutation) Where(ps ...predicate.Task) { m.predicates = append(m.predicates, ps...) @@ -12670,10 +12720,13 @@ func (m *TaskMutation) Type() string { // order to get all numeric fields that were incremented/decremented, call // AddedFields(). func (m *TaskMutation) Fields() []string { - fields := make([]string, 0, 1) + fields := make([]string, 0, 2) if m.priority != nil { fields = append(fields, enttask.FieldPriority) } + if m.priorities != nil { + fields = append(fields, enttask.FieldPriorities) + } return fields } @@ -12684,6 +12737,8 @@ func (m *TaskMutation) Field(name string) (ent.Value, bool) { switch name { case enttask.FieldPriority: return m.Priority() + case enttask.FieldPriorities: + return m.Priorities() } return nil, false } @@ -12695,6 +12750,8 @@ func (m *TaskMutation) OldField(ctx context.Context, name string) (ent.Value, er switch name { case enttask.FieldPriority: return m.OldPriority(ctx) + case enttask.FieldPriorities: + return m.OldPriorities(ctx) } return nil, fmt.Errorf("unknown Task field %s", name) } @@ -12711,6 +12768,13 @@ func (m *TaskMutation) SetField(name string, value ent.Value) error { } m.SetPriority(v) return nil + case enttask.FieldPriorities: + v, ok := value.(map[string]task.Priority) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetPriorities(v) + return nil } return fmt.Errorf("unknown Task field %s", name) } @@ -12755,7 +12819,11 @@ func (m *TaskMutation) AddField(name string, value ent.Value) error { // ClearedFields returns all nullable fields that were cleared during this // mutation. func (m *TaskMutation) ClearedFields() []string { - return nil + var fields []string + if m.FieldCleared(enttask.FieldPriorities) { + fields = append(fields, enttask.FieldPriorities) + } + return fields } // FieldCleared returns a boolean indicating if a field with the given name was @@ -12768,6 +12836,11 @@ func (m *TaskMutation) FieldCleared(name string) bool { // ClearField clears the value of the field with the given name. It returns an // error if the field is not defined in the schema. func (m *TaskMutation) ClearField(name string) error { + switch name { + case enttask.FieldPriorities: + m.ClearPriorities() + return nil + } return fmt.Errorf("unknown Task nullable field %s", name) } @@ -12778,6 +12851,9 @@ func (m *TaskMutation) ResetField(name string) error { case enttask.FieldPriority: m.ResetPriority() return nil + case enttask.FieldPriorities: + m.ResetPriorities() + return nil } return fmt.Errorf("unknown Task field %s", name) } diff --git a/entc/integration/ent/schema/task.go b/entc/integration/ent/schema/task.go index c91b94738..b62f7e04e 100644 --- a/entc/integration/ent/schema/task.go +++ b/entc/integration/ent/schema/task.go @@ -25,5 +25,7 @@ func (Task) Fields() []ent.Field { Validate(func(i int) error { return task.Priority(i).Validate() }), + field.JSON("priorities", map[string]task.Priority{}). + Optional(), } } diff --git a/entc/integration/ent/task.go b/entc/integration/ent/task.go index b2f2a9703..e1fae5f8a 100644 --- a/entc/integration/ent/task.go +++ b/entc/integration/ent/task.go @@ -7,6 +7,7 @@ package ent import ( + "encoding/json" "fmt" "strings" @@ -23,6 +24,8 @@ type Task struct { ID int `json:"id,omitempty"` // Priority holds the value of the "priority" field. Priority task.Priority `json:"priority,omitempty"` + // Priorities holds the value of the "priorities" field. + Priorities map[string]task.Priority `json:"priorities,omitempty"` } // scanValues returns the types for scanning values from sql.Rows. @@ -30,6 +33,8 @@ func (*Task) scanValues(columns []string) ([]interface{}, error) { values := make([]interface{}, len(columns)) for i := range columns { switch columns[i] { + case enttask.FieldPriorities: + values[i] = new([]byte) case enttask.FieldID, enttask.FieldPriority: values[i] = new(sql.NullInt64) default: @@ -59,6 +64,14 @@ func (t *Task) assignValues(columns []string, values []interface{}) error { } else if value.Valid { t.Priority = task.Priority(value.Int64) } + case enttask.FieldPriorities: + if value, ok := values[i].(*[]byte); !ok { + return fmt.Errorf("unexpected type %T for field priorities", values[i]) + } else if value != nil && len(*value) > 0 { + if err := json.Unmarshal(*value, &t.Priorities); err != nil { + return fmt.Errorf("unmarshal field priorities: %w", err) + } + } } } return nil @@ -89,6 +102,9 @@ func (t *Task) String() string { builder.WriteString(fmt.Sprintf("id=%v, ", t.ID)) builder.WriteString("priority=") builder.WriteString(fmt.Sprintf("%v", t.Priority)) + builder.WriteString(", ") + builder.WriteString("priorities=") + builder.WriteString(fmt.Sprintf("%v", t.Priorities)) builder.WriteByte(')') return builder.String() } diff --git a/entc/integration/ent/task/task.go b/entc/integration/ent/task/task.go index 7bbbb20e7..c2562b1d3 100644 --- a/entc/integration/ent/task/task.go +++ b/entc/integration/ent/task/task.go @@ -17,6 +17,8 @@ const ( FieldID = "id" // FieldPriority holds the string denoting the priority field in the database. FieldPriority = "priority" + // FieldPriorities holds the string denoting the priorities field in the database. + FieldPriorities = "priorities" // Table holds the table name of the task in the database. Table = "tasks" ) @@ -25,6 +27,7 @@ const ( var Columns = []string{ FieldID, FieldPriority, + FieldPriorities, } // ValidColumn reports if the column name is valid (part of the table columns). diff --git a/entc/integration/ent/task/where.go b/entc/integration/ent/task/where.go index d3a74f8f5..fb1e054ae 100644 --- a/entc/integration/ent/task/where.go +++ b/entc/integration/ent/task/where.go @@ -161,6 +161,20 @@ func PriorityLTE(v task.Priority) predicate.Task { }) } +// PrioritiesIsNil applies the IsNil predicate on the "priorities" field. +func PrioritiesIsNil() predicate.Task { + return predicate.Task(func(s *sql.Selector) { + s.Where(sql.IsNull(s.C(FieldPriorities))) + }) +} + +// PrioritiesNotNil applies the NotNil predicate on the "priorities" field. +func PrioritiesNotNil() predicate.Task { + return predicate.Task(func(s *sql.Selector) { + s.Where(sql.NotNull(s.C(FieldPriorities))) + }) +} + // And groups predicates with the AND operator between them. func And(predicates ...predicate.Task) predicate.Task { return predicate.Task(func(s *sql.Selector) { diff --git a/entc/integration/ent/task_create.go b/entc/integration/ent/task_create.go index 05b4d2187..3694f3372 100644 --- a/entc/integration/ent/task_create.go +++ b/entc/integration/ent/task_create.go @@ -41,6 +41,12 @@ func (tc *TaskCreate) SetNillablePriority(t *task.Priority) *TaskCreate { return tc } +// SetPriorities sets the "priorities" field. +func (tc *TaskCreate) SetPriorities(m map[string]task.Priority) *TaskCreate { + tc.mutation.SetPriorities(m) + return tc +} + // Mutation returns the TaskMutation object of the builder. func (tc *TaskCreate) Mutation() *TaskMutation { return tc.mutation @@ -170,6 +176,14 @@ func (tc *TaskCreate) createSpec() (*Task, *sqlgraph.CreateSpec) { }) _node.Priority = value } + if value, ok := tc.mutation.Priorities(); ok { + _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{ + Type: field.TypeJSON, + Value: value, + Column: enttask.FieldPriorities, + }) + _node.Priorities = value + } return _node, _spec } @@ -242,6 +256,24 @@ func (u *TaskUpsert) AddPriority(v task.Priority) *TaskUpsert { return u } +// SetPriorities sets the "priorities" field. +func (u *TaskUpsert) SetPriorities(v map[string]task.Priority) *TaskUpsert { + u.Set(enttask.FieldPriorities, v) + return u +} + +// UpdatePriorities sets the "priorities" field to the value that was provided on create. +func (u *TaskUpsert) UpdatePriorities() *TaskUpsert { + u.SetExcluded(enttask.FieldPriorities) + return u +} + +// ClearPriorities clears the value of the "priorities" field. +func (u *TaskUpsert) ClearPriorities() *TaskUpsert { + u.SetNull(enttask.FieldPriorities) + return u +} + // UpdateNewValues updates the mutable fields using the new values that were set on create. // Using this option is equivalent to using: // @@ -305,6 +337,27 @@ func (u *TaskUpsertOne) UpdatePriority() *TaskUpsertOne { }) } +// SetPriorities sets the "priorities" field. +func (u *TaskUpsertOne) SetPriorities(v map[string]task.Priority) *TaskUpsertOne { + return u.Update(func(s *TaskUpsert) { + s.SetPriorities(v) + }) +} + +// UpdatePriorities sets the "priorities" field to the value that was provided on create. +func (u *TaskUpsertOne) UpdatePriorities() *TaskUpsertOne { + return u.Update(func(s *TaskUpsert) { + s.UpdatePriorities() + }) +} + +// ClearPriorities clears the value of the "priorities" field. +func (u *TaskUpsertOne) ClearPriorities() *TaskUpsertOne { + return u.Update(func(s *TaskUpsert) { + s.ClearPriorities() + }) +} + // Exec executes the query. func (u *TaskUpsertOne) Exec(ctx context.Context) error { if len(u.create.conflict) == 0 { @@ -530,6 +583,27 @@ func (u *TaskUpsertBulk) UpdatePriority() *TaskUpsertBulk { }) } +// SetPriorities sets the "priorities" field. +func (u *TaskUpsertBulk) SetPriorities(v map[string]task.Priority) *TaskUpsertBulk { + return u.Update(func(s *TaskUpsert) { + s.SetPriorities(v) + }) +} + +// UpdatePriorities sets the "priorities" field to the value that was provided on create. +func (u *TaskUpsertBulk) UpdatePriorities() *TaskUpsertBulk { + return u.Update(func(s *TaskUpsert) { + s.UpdatePriorities() + }) +} + +// ClearPriorities clears the value of the "priorities" field. +func (u *TaskUpsertBulk) ClearPriorities() *TaskUpsertBulk { + return u.Update(func(s *TaskUpsert) { + s.ClearPriorities() + }) +} + // Exec executes the query. func (u *TaskUpsertBulk) Exec(ctx context.Context) error { for i, b := range u.create.builders { diff --git a/entc/integration/ent/task_update.go b/entc/integration/ent/task_update.go index a6bc44a33..b9e2bcb46 100644 --- a/entc/integration/ent/task_update.go +++ b/entc/integration/ent/task_update.go @@ -54,6 +54,18 @@ func (tu *TaskUpdate) AddPriority(t task.Priority) *TaskUpdate { return tu } +// SetPriorities sets the "priorities" field. +func (tu *TaskUpdate) SetPriorities(m map[string]task.Priority) *TaskUpdate { + tu.mutation.SetPriorities(m) + return tu +} + +// ClearPriorities clears the value of the "priorities" field. +func (tu *TaskUpdate) ClearPriorities() *TaskUpdate { + tu.mutation.ClearPriorities() + return tu +} + // Mutation returns the TaskMutation object of the builder. func (tu *TaskUpdate) Mutation() *TaskMutation { return tu.mutation @@ -161,6 +173,19 @@ func (tu *TaskUpdate) sqlSave(ctx context.Context) (n int, err error) { Column: enttask.FieldPriority, }) } + if value, ok := tu.mutation.Priorities(); ok { + _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{ + Type: field.TypeJSON, + Value: value, + Column: enttask.FieldPriorities, + }) + } + if tu.mutation.PrioritiesCleared() { + _spec.Fields.Clear = append(_spec.Fields.Clear, &sqlgraph.FieldSpec{ + Type: field.TypeJSON, + Column: enttask.FieldPriorities, + }) + } if n, err = sqlgraph.UpdateNodes(ctx, tu.driver, _spec); err != nil { if _, ok := err.(*sqlgraph.NotFoundError); ok { err = &NotFoundError{enttask.Label} @@ -201,6 +226,18 @@ func (tuo *TaskUpdateOne) AddPriority(t task.Priority) *TaskUpdateOne { return tuo } +// SetPriorities sets the "priorities" field. +func (tuo *TaskUpdateOne) SetPriorities(m map[string]task.Priority) *TaskUpdateOne { + tuo.mutation.SetPriorities(m) + return tuo +} + +// ClearPriorities clears the value of the "priorities" field. +func (tuo *TaskUpdateOne) ClearPriorities() *TaskUpdateOne { + tuo.mutation.ClearPriorities() + return tuo +} + // Mutation returns the TaskMutation object of the builder. func (tuo *TaskUpdateOne) Mutation() *TaskMutation { return tuo.mutation @@ -338,6 +375,19 @@ func (tuo *TaskUpdateOne) sqlSave(ctx context.Context) (_node *Task, err error) Column: enttask.FieldPriority, }) } + if value, ok := tuo.mutation.Priorities(); ok { + _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{ + Type: field.TypeJSON, + Value: value, + Column: enttask.FieldPriorities, + }) + } + if tuo.mutation.PrioritiesCleared() { + _spec.Fields.Clear = append(_spec.Fields.Clear, &sqlgraph.FieldSpec{ + Type: field.TypeJSON, + Column: enttask.FieldPriorities, + }) + } _node = &Task{config: tuo.config} _spec.Assign = _node.assignValues _spec.ScanValues = _node.scanValues diff --git a/entc/integration/gremlin/ent/mutation.go b/entc/integration/gremlin/ent/mutation.go index 60699cd5f..30491bb94 100644 --- a/entc/integration/gremlin/ent/mutation.go +++ b/entc/integration/gremlin/ent/mutation.go @@ -12491,6 +12491,7 @@ type TaskMutation struct { id *string priority *task.Priority addpriority *task.Priority + priorities *map[string]task.Priority clearedFields map[string]struct{} done bool oldValue func(context.Context) (*Task, error) @@ -12651,6 +12652,55 @@ func (m *TaskMutation) ResetPriority() { m.addpriority = nil } +// SetPriorities sets the "priorities" field. +func (m *TaskMutation) SetPriorities(value map[string]task.Priority) { + m.priorities = &value +} + +// Priorities returns the value of the "priorities" field in the mutation. +func (m *TaskMutation) Priorities() (r map[string]task.Priority, exists bool) { + v := m.priorities + if v == nil { + return + } + return *v, true +} + +// OldPriorities returns the old "priorities" field's value of the Task entity. +// If the Task object wasn't provided to the builder, the object is fetched from the database. +// An error is returned if the mutation operation is not UpdateOne, or the database query fails. +func (m *TaskMutation) OldPriorities(ctx context.Context) (v map[string]task.Priority, err error) { + if !m.op.Is(OpUpdateOne) { + return v, errors.New("OldPriorities is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, errors.New("OldPriorities requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldPriorities: %w", err) + } + return oldValue.Priorities, nil +} + +// ClearPriorities clears the value of the "priorities" field. +func (m *TaskMutation) ClearPriorities() { + m.priorities = nil + m.clearedFields[enttask.FieldPriorities] = struct{}{} +} + +// PrioritiesCleared returns if the "priorities" field was cleared in this mutation. +func (m *TaskMutation) PrioritiesCleared() bool { + _, ok := m.clearedFields[enttask.FieldPriorities] + return ok +} + +// ResetPriorities resets all changes to the "priorities" field. +func (m *TaskMutation) ResetPriorities() { + m.priorities = nil + delete(m.clearedFields, enttask.FieldPriorities) +} + // Where appends a list predicates to the TaskMutation builder. func (m *TaskMutation) Where(ps ...predicate.Task) { m.predicates = append(m.predicates, ps...) @@ -12670,10 +12720,13 @@ func (m *TaskMutation) Type() string { // order to get all numeric fields that were incremented/decremented, call // AddedFields(). func (m *TaskMutation) Fields() []string { - fields := make([]string, 0, 1) + fields := make([]string, 0, 2) if m.priority != nil { fields = append(fields, enttask.FieldPriority) } + if m.priorities != nil { + fields = append(fields, enttask.FieldPriorities) + } return fields } @@ -12684,6 +12737,8 @@ func (m *TaskMutation) Field(name string) (ent.Value, bool) { switch name { case enttask.FieldPriority: return m.Priority() + case enttask.FieldPriorities: + return m.Priorities() } return nil, false } @@ -12695,6 +12750,8 @@ func (m *TaskMutation) OldField(ctx context.Context, name string) (ent.Value, er switch name { case enttask.FieldPriority: return m.OldPriority(ctx) + case enttask.FieldPriorities: + return m.OldPriorities(ctx) } return nil, fmt.Errorf("unknown Task field %s", name) } @@ -12711,6 +12768,13 @@ func (m *TaskMutation) SetField(name string, value ent.Value) error { } m.SetPriority(v) return nil + case enttask.FieldPriorities: + v, ok := value.(map[string]task.Priority) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetPriorities(v) + return nil } return fmt.Errorf("unknown Task field %s", name) } @@ -12755,7 +12819,11 @@ func (m *TaskMutation) AddField(name string, value ent.Value) error { // ClearedFields returns all nullable fields that were cleared during this // mutation. func (m *TaskMutation) ClearedFields() []string { - return nil + var fields []string + if m.FieldCleared(enttask.FieldPriorities) { + fields = append(fields, enttask.FieldPriorities) + } + return fields } // FieldCleared returns a boolean indicating if a field with the given name was @@ -12768,6 +12836,11 @@ func (m *TaskMutation) FieldCleared(name string) bool { // ClearField clears the value of the field with the given name. It returns an // error if the field is not defined in the schema. func (m *TaskMutation) ClearField(name string) error { + switch name { + case enttask.FieldPriorities: + m.ClearPriorities() + return nil + } return fmt.Errorf("unknown Task nullable field %s", name) } @@ -12778,6 +12851,9 @@ func (m *TaskMutation) ResetField(name string) error { case enttask.FieldPriority: m.ResetPriority() return nil + case enttask.FieldPriorities: + m.ResetPriorities() + return nil } return fmt.Errorf("unknown Task field %s", name) } diff --git a/entc/integration/gremlin/ent/task.go b/entc/integration/gremlin/ent/task.go index 247de442f..16e48c698 100644 --- a/entc/integration/gremlin/ent/task.go +++ b/entc/integration/gremlin/ent/task.go @@ -21,6 +21,8 @@ type Task struct { ID string `json:"id,omitempty"` // Priority holds the value of the "priority" field. Priority task.Priority `json:"priority,omitempty"` + // Priorities holds the value of the "priorities" field. + Priorities map[string]task.Priority `json:"priorities,omitempty"` } // FromResponse scans the gremlin response data into Task. @@ -30,14 +32,16 @@ func (t *Task) FromResponse(res *gremlin.Response) error { return err } var scant struct { - ID string `json:"id,omitempty"` - Priority task.Priority `json:"priority,omitempty"` + ID string `json:"id,omitempty"` + Priority task.Priority `json:"priority,omitempty"` + Priorities map[string]task.Priority `json:"priorities,omitempty"` } if err := vmap.Decode(&scant); err != nil { return err } t.ID = scant.ID t.Priority = scant.Priority + t.Priorities = scant.Priorities return nil } @@ -66,6 +70,9 @@ func (t *Task) String() string { builder.WriteString(fmt.Sprintf("id=%v, ", t.ID)) builder.WriteString("priority=") builder.WriteString(fmt.Sprintf("%v", t.Priority)) + builder.WriteString(", ") + builder.WriteString("priorities=") + builder.WriteString(fmt.Sprintf("%v", t.Priorities)) builder.WriteByte(')') return builder.String() } @@ -80,16 +87,18 @@ func (t *Tasks) FromResponse(res *gremlin.Response) error { return err } var scant []struct { - ID string `json:"id,omitempty"` - Priority task.Priority `json:"priority,omitempty"` + ID string `json:"id,omitempty"` + Priority task.Priority `json:"priority,omitempty"` + Priorities map[string]task.Priority `json:"priorities,omitempty"` } if err := vmap.Decode(&scant); err != nil { return err } for _, v := range scant { *t = append(*t, &Task{ - ID: v.ID, - Priority: v.Priority, + ID: v.ID, + Priority: v.Priority, + Priorities: v.Priorities, }) } return nil diff --git a/entc/integration/gremlin/ent/task/task.go b/entc/integration/gremlin/ent/task/task.go index 1912238fa..b455706d6 100644 --- a/entc/integration/gremlin/ent/task/task.go +++ b/entc/integration/gremlin/ent/task/task.go @@ -17,6 +17,8 @@ const ( FieldID = "id" // FieldPriority holds the string denoting the priority field in the database. FieldPriority = "priority" + // FieldPriorities holds the string denoting the priorities field in the database. + FieldPriorities = "priorities" ) var ( diff --git a/entc/integration/gremlin/ent/task/where.go b/entc/integration/gremlin/ent/task/where.go index 94f1558e1..544ea2d06 100644 --- a/entc/integration/gremlin/ent/task/where.go +++ b/entc/integration/gremlin/ent/task/where.go @@ -163,6 +163,20 @@ func PriorityLTE(v task.Priority) predicate.Task { }) } +// PrioritiesIsNil applies the IsNil predicate on the "priorities" field. +func PrioritiesIsNil() predicate.Task { + return predicate.Task(func(t *dsl.Traversal) { + t.HasLabel(Label).HasNot(FieldPriorities) + }) +} + +// PrioritiesNotNil applies the NotNil predicate on the "priorities" field. +func PrioritiesNotNil() predicate.Task { + return predicate.Task(func(t *dsl.Traversal) { + t.HasLabel(Label).Has(FieldPriorities) + }) +} + // And groups predicates with the AND operator between them. func And(predicates ...predicate.Task) predicate.Task { return predicate.Task(func(tr *dsl.Traversal) { diff --git a/entc/integration/gremlin/ent/task_create.go b/entc/integration/gremlin/ent/task_create.go index ac1924135..719860ae2 100644 --- a/entc/integration/gremlin/ent/task_create.go +++ b/entc/integration/gremlin/ent/task_create.go @@ -40,6 +40,12 @@ func (tc *TaskCreate) SetNillablePriority(t *task.Priority) *TaskCreate { return tc } +// SetPriorities sets the "priorities" field. +func (tc *TaskCreate) SetPriorities(m map[string]task.Priority) *TaskCreate { + tc.mutation.SetPriorities(m) + return tc +} + // Mutation returns the TaskMutation object of the builder. func (tc *TaskCreate) Mutation() *TaskMutation { return tc.mutation @@ -157,6 +163,9 @@ func (tc *TaskCreate) gremlin() *dsl.Traversal { if value, ok := tc.mutation.Priority(); ok { v.Property(dsl.Single, enttask.FieldPriority, value) } + if value, ok := tc.mutation.Priorities(); ok { + v.Property(dsl.Single, enttask.FieldPriorities, value) + } return v.ValueMap(true) } diff --git a/entc/integration/gremlin/ent/task_update.go b/entc/integration/gremlin/ent/task_update.go index 44c4cab7d..c8153f4d3 100644 --- a/entc/integration/gremlin/ent/task_update.go +++ b/entc/integration/gremlin/ent/task_update.go @@ -55,6 +55,18 @@ func (tu *TaskUpdate) AddPriority(t task.Priority) *TaskUpdate { return tu } +// SetPriorities sets the "priorities" field. +func (tu *TaskUpdate) SetPriorities(m map[string]task.Priority) *TaskUpdate { + tu.mutation.SetPriorities(m) + return tu +} + +// ClearPriorities clears the value of the "priorities" field. +func (tu *TaskUpdate) ClearPriorities() *TaskUpdate { + tu.mutation.ClearPriorities() + return tu +} + // Mutation returns the TaskMutation object of the builder. func (tu *TaskUpdate) Mutation() *TaskMutation { return tu.mutation @@ -156,6 +168,16 @@ func (tu *TaskUpdate) gremlin() *dsl.Traversal { if value, ok := tu.mutation.AddedPriority(); ok { v.Property(dsl.Single, enttask.FieldPriority, __.Union(__.Values(enttask.FieldPriority), __.Constant(value)).Sum()) } + if value, ok := tu.mutation.Priorities(); ok { + v.Property(dsl.Single, enttask.FieldPriorities, value) + } + var properties []interface{} + if tu.mutation.PrioritiesCleared() { + properties = append(properties, enttask.FieldPriorities) + } + if len(properties) > 0 { + v.SideEffect(__.Properties(properties...).Drop()) + } v.Count() trs = append(trs, v) return dsl.Join(trs...) @@ -190,6 +212,18 @@ func (tuo *TaskUpdateOne) AddPriority(t task.Priority) *TaskUpdateOne { return tuo } +// SetPriorities sets the "priorities" field. +func (tuo *TaskUpdateOne) SetPriorities(m map[string]task.Priority) *TaskUpdateOne { + tuo.mutation.SetPriorities(m) + return tuo +} + +// ClearPriorities clears the value of the "priorities" field. +func (tuo *TaskUpdateOne) ClearPriorities() *TaskUpdateOne { + tuo.mutation.ClearPriorities() + return tuo +} + // Mutation returns the TaskMutation object of the builder. func (tuo *TaskUpdateOne) Mutation() *TaskMutation { return tuo.mutation @@ -309,6 +343,16 @@ func (tuo *TaskUpdateOne) gremlin(id string) *dsl.Traversal { if value, ok := tuo.mutation.AddedPriority(); ok { v.Property(dsl.Single, enttask.FieldPriority, __.Union(__.Values(enttask.FieldPriority), __.Constant(value)).Sum()) } + if value, ok := tuo.mutation.Priorities(); ok { + v.Property(dsl.Single, enttask.FieldPriorities, value) + } + var properties []interface{} + if tuo.mutation.PrioritiesCleared() { + properties = append(properties, enttask.FieldPriorities) + } + if len(properties) > 0 { + v.SideEffect(__.Properties(properties...).Drop()) + } if len(tuo.fields) > 0 { fields := make([]interface{}, 0, len(tuo.fields)+1) fields = append(fields, true) diff --git a/entc/integration/integration_test.go b/entc/integration/integration_test.go index f1c26d201..cf449c4e8 100644 --- a/entc/integration/integration_test.go +++ b/entc/integration/integration_test.go @@ -344,7 +344,7 @@ func Upsert(t *testing.T, client *ent.Client) { SetAge(33). SetPhone("0000"). OnConflictColumns(user.FieldPhone). - // Override some of the fields with custom update. + // Override some fields with custom update. AddAge(-1). IDX(ctx) u = client.User.GetX(ctx, id) diff --git a/entc/integration/privacy/ent/internal/schema.go b/entc/integration/privacy/ent/internal/schema.go index ab91c35b5..7db5b026c 100644 --- a/entc/integration/privacy/ent/internal/schema.go +++ b/entc/integration/privacy/ent/internal/schema.go @@ -10,4 +10,4 @@ // Package internal holds a loadable version of the latest schema. package internal -const Schema = `{"Schema":"entgo.io/ent/entc/integration/privacy/ent/schema","Package":"entgo.io/ent/entc/integration/privacy/ent","Schemas":[{"name":"Task","config":{"Table":""},"edges":[{"name":"teams","type":"Team"},{"name":"owner","type":"User","ref_name":"tasks","unique":true,"inverse":true}],"fields":[{"name":"title","type":{"Type":7,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"validators":1,"position":{"Index":0,"MixedIn":false,"MixinIndex":0}},{"name":"description","type":{"Type":7,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"optional":true,"position":{"Index":1,"MixedIn":false,"MixinIndex":0}},{"name":"status","type":{"Type":6,"Ident":"task.Status","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"enums":[{"N":"planned","V":"planned"},{"N":"in_progress","V":"in_progress"},{"N":"closed","V":"closed"}],"default":true,"default_value":"planned","default_kind":24,"position":{"Index":2,"MixedIn":false,"MixinIndex":0}},{"name":"uuid","type":{"Type":4,"Ident":"uuid.UUID","PkgPath":"github.com/google/uuid","PkgName":"","Nillable":false,"RType":{"Name":"UUID","Ident":"uuid.UUID","Kind":17,"PkgPath":"github.com/google/uuid","Methods":{"ClockSequence":{"In":[],"Out":[{"Name":"int","Ident":"int","Kind":2,"PkgPath":"","Methods":null}]},"Domain":{"In":[],"Out":[{"Name":"Domain","Ident":"uuid.Domain","Kind":8,"PkgPath":"github.com/google/uuid","Methods":null}]},"ID":{"In":[],"Out":[{"Name":"uint32","Ident":"uint32","Kind":10,"PkgPath":"","Methods":null}]},"MarshalBinary":{"In":[],"Out":[{"Name":"","Ident":"[]uint8","Kind":23,"PkgPath":"","Methods":null},{"Name":"error","Ident":"error","Kind":20,"PkgPath":"","Methods":null}]},"MarshalText":{"In":[],"Out":[{"Name":"","Ident":"[]uint8","Kind":23,"PkgPath":"","Methods":null},{"Name":"error","Ident":"error","Kind":20,"PkgPath":"","Methods":null}]},"NodeID":{"In":[],"Out":[{"Name":"","Ident":"[]uint8","Kind":23,"PkgPath":"","Methods":null}]},"Scan":{"In":[{"Name":"","Ident":"interface {}","Kind":20,"PkgPath":"","Methods":null}],"Out":[{"Name":"error","Ident":"error","Kind":20,"PkgPath":"","Methods":null}]},"String":{"In":[],"Out":[{"Name":"string","Ident":"string","Kind":24,"PkgPath":"","Methods":null}]},"Time":{"In":[],"Out":[{"Name":"Time","Ident":"uuid.Time","Kind":6,"PkgPath":"github.com/google/uuid","Methods":null}]},"URN":{"In":[],"Out":[{"Name":"string","Ident":"string","Kind":24,"PkgPath":"","Methods":null}]},"UnmarshalBinary":{"In":[{"Name":"","Ident":"[]uint8","Kind":23,"PkgPath":"","Methods":null}],"Out":[{"Name":"error","Ident":"error","Kind":20,"PkgPath":"","Methods":null}]},"UnmarshalText":{"In":[{"Name":"","Ident":"[]uint8","Kind":23,"PkgPath":"","Methods":null}],"Out":[{"Name":"error","Ident":"error","Kind":20,"PkgPath":"","Methods":null}]},"Value":{"In":[],"Out":[{"Name":"Value","Ident":"driver.Value","Kind":20,"PkgPath":"database/sql/driver","Methods":null},{"Name":"error","Ident":"error","Kind":20,"PkgPath":"","Methods":null}]},"Variant":{"In":[],"Out":[{"Name":"Variant","Ident":"uuid.Variant","Kind":8,"PkgPath":"github.com/google/uuid","Methods":null}]},"Version":{"In":[],"Out":[{"Name":"Version","Ident":"uuid.Version","Kind":8,"PkgPath":"github.com/google/uuid","Methods":null}]}}}},"optional":true,"position":{"Index":3,"MixedIn":false,"MixinIndex":0}}],"hooks":[{"Index":0,"MixedIn":false,"MixinIndex":0}],"policy":[{"Index":0,"MixedIn":true,"MixinIndex":0},{"Index":0,"MixedIn":true,"MixinIndex":1},{"Index":0,"MixedIn":false,"MixinIndex":0}]},{"name":"Team","config":{"Table":""},"edges":[{"name":"tasks","type":"Task","ref_name":"teams","inverse":true},{"name":"users","type":"User","ref_name":"teams","inverse":true}],"fields":[{"name":"name","type":{"Type":7,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"validators":1,"position":{"Index":0,"MixedIn":false,"MixinIndex":0}}],"policy":[{"Index":0,"MixedIn":true,"MixinIndex":0},{"Index":0,"MixedIn":false,"MixinIndex":0}]},{"name":"User","config":{"Table":""},"edges":[{"name":"teams","type":"Team"},{"name":"tasks","type":"Task"}],"fields":[{"name":"name","type":{"Type":7,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"unique":true,"immutable":true,"validators":1,"position":{"Index":0,"MixedIn":false,"MixinIndex":0}},{"name":"age","type":{"Type":17,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"optional":true,"position":{"Index":1,"MixedIn":false,"MixinIndex":0}}],"policy":[{"Index":0,"MixedIn":true,"MixinIndex":0},{"Index":0,"MixedIn":true,"MixinIndex":1},{"Index":0,"MixedIn":false,"MixinIndex":0}]}],"Features":["privacy","entql","schema/snapshot"]}` +const Schema = `{"Schema":"entgo.io/ent/entc/integration/privacy/ent/schema","Package":"entgo.io/ent/entc/integration/privacy/ent","Schemas":[{"name":"Task","config":{"Table":""},"edges":[{"name":"teams","type":"Team"},{"name":"owner","type":"User","ref_name":"tasks","unique":true,"inverse":true}],"fields":[{"name":"title","type":{"Type":7,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"validators":1,"position":{"Index":0,"MixedIn":false,"MixinIndex":0}},{"name":"description","type":{"Type":7,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"optional":true,"position":{"Index":1,"MixedIn":false,"MixinIndex":0}},{"name":"status","type":{"Type":6,"Ident":"task.Status","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"enums":[{"N":"planned","V":"planned"},{"N":"in_progress","V":"in_progress"},{"N":"closed","V":"closed"}],"default":true,"default_value":"planned","default_kind":24,"position":{"Index":2,"MixedIn":false,"MixinIndex":0}},{"name":"uuid","type":{"Type":4,"Ident":"uuid.UUID","PkgPath":"github.com/google/uuid","PkgName":"uuid","Nillable":false,"RType":{"Name":"UUID","Ident":"uuid.UUID","Kind":17,"PkgPath":"github.com/google/uuid","Methods":{"ClockSequence":{"In":[],"Out":[{"Name":"int","Ident":"int","Kind":2,"PkgPath":"","Methods":null}]},"Domain":{"In":[],"Out":[{"Name":"Domain","Ident":"uuid.Domain","Kind":8,"PkgPath":"github.com/google/uuid","Methods":null}]},"ID":{"In":[],"Out":[{"Name":"uint32","Ident":"uint32","Kind":10,"PkgPath":"","Methods":null}]},"MarshalBinary":{"In":[],"Out":[{"Name":"","Ident":"[]uint8","Kind":23,"PkgPath":"","Methods":null},{"Name":"error","Ident":"error","Kind":20,"PkgPath":"","Methods":null}]},"MarshalText":{"In":[],"Out":[{"Name":"","Ident":"[]uint8","Kind":23,"PkgPath":"","Methods":null},{"Name":"error","Ident":"error","Kind":20,"PkgPath":"","Methods":null}]},"NodeID":{"In":[],"Out":[{"Name":"","Ident":"[]uint8","Kind":23,"PkgPath":"","Methods":null}]},"Scan":{"In":[{"Name":"","Ident":"interface {}","Kind":20,"PkgPath":"","Methods":null}],"Out":[{"Name":"error","Ident":"error","Kind":20,"PkgPath":"","Methods":null}]},"String":{"In":[],"Out":[{"Name":"string","Ident":"string","Kind":24,"PkgPath":"","Methods":null}]},"Time":{"In":[],"Out":[{"Name":"Time","Ident":"uuid.Time","Kind":6,"PkgPath":"github.com/google/uuid","Methods":null}]},"URN":{"In":[],"Out":[{"Name":"string","Ident":"string","Kind":24,"PkgPath":"","Methods":null}]},"UnmarshalBinary":{"In":[{"Name":"","Ident":"[]uint8","Kind":23,"PkgPath":"","Methods":null}],"Out":[{"Name":"error","Ident":"error","Kind":20,"PkgPath":"","Methods":null}]},"UnmarshalText":{"In":[{"Name":"","Ident":"[]uint8","Kind":23,"PkgPath":"","Methods":null}],"Out":[{"Name":"error","Ident":"error","Kind":20,"PkgPath":"","Methods":null}]},"Value":{"In":[],"Out":[{"Name":"Value","Ident":"driver.Value","Kind":20,"PkgPath":"database/sql/driver","Methods":null},{"Name":"error","Ident":"error","Kind":20,"PkgPath":"","Methods":null}]},"Variant":{"In":[],"Out":[{"Name":"Variant","Ident":"uuid.Variant","Kind":8,"PkgPath":"github.com/google/uuid","Methods":null}]},"Version":{"In":[],"Out":[{"Name":"Version","Ident":"uuid.Version","Kind":8,"PkgPath":"github.com/google/uuid","Methods":null}]}}}},"optional":true,"position":{"Index":3,"MixedIn":false,"MixinIndex":0}}],"hooks":[{"Index":0,"MixedIn":false,"MixinIndex":0}],"policy":[{"Index":0,"MixedIn":true,"MixinIndex":0},{"Index":0,"MixedIn":true,"MixinIndex":1},{"Index":0,"MixedIn":false,"MixinIndex":0}]},{"name":"Team","config":{"Table":""},"edges":[{"name":"tasks","type":"Task","ref_name":"teams","inverse":true},{"name":"users","type":"User","ref_name":"teams","inverse":true}],"fields":[{"name":"name","type":{"Type":7,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"validators":1,"position":{"Index":0,"MixedIn":false,"MixinIndex":0}}],"policy":[{"Index":0,"MixedIn":true,"MixinIndex":0},{"Index":0,"MixedIn":false,"MixinIndex":0}]},{"name":"User","config":{"Table":""},"edges":[{"name":"teams","type":"Team"},{"name":"tasks","type":"Task"}],"fields":[{"name":"name","type":{"Type":7,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"unique":true,"immutable":true,"validators":1,"position":{"Index":0,"MixedIn":false,"MixinIndex":0}},{"name":"age","type":{"Type":17,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"optional":true,"position":{"Index":1,"MixedIn":false,"MixinIndex":0}}],"policy":[{"Index":0,"MixedIn":true,"MixinIndex":0},{"Index":0,"MixedIn":true,"MixinIndex":1},{"Index":0,"MixedIn":false,"MixinIndex":0}]}],"Features":["privacy","entql","schema/snapshot"]}` diff --git a/schema/field/field.go b/schema/field/field.go index 35b63dd89..fdb322637 100644 --- a/schema/field/field.go +++ b/schema/field/field.go @@ -12,6 +12,7 @@ import ( "math" "reflect" "regexp" + "strings" "time" "entgo.io/ent/schema" @@ -1166,6 +1167,7 @@ func (d *Descriptor) goType(typ interface{}, expectType reflect.Type) { Type: d.Info.Type, Ident: t.String(), PkgPath: tv.PkgPath(), + PkgName: pkgName(tv.String()), RType: &RType{ rtype: t, Kind: t.Kind(), @@ -1189,6 +1191,20 @@ func (d *Descriptor) goType(typ interface{}, expectType reflect.Type) { d.Info = info } +// pkgName returns the package name from a Go +// identifier with a package qualifier. +func pkgName(ident string) string { + i := strings.LastIndexByte(ident, '.') + if i == -1 { + return "" + } + s := ident[:i] + if i := strings.LastIndexAny(s, "]*"); i != -1 { + s = s[i+1:] + } + return s +} + func methods(t reflect.Type, rtype *RType) { // For type T, add methods with // pointer receiver as well (*T). diff --git a/schema/field/field_test.go b/schema/field/field_test.go index 8fec405e8..c799ad0dd 100644 --- a/schema/field/field_test.go +++ b/schema/field/field_test.go @@ -378,6 +378,7 @@ func TestString(t *testing.T) { assert.Equal(t, "*field_test.tURL", fd.Info.String()) assert.True(t, fd.Info.ValueScanner()) assert.True(t, fd.Info.Stringer()) + assert.Equal(t, "field_test", fd.Info.PkgName) fd = field.String("name").GoType(1).Descriptor() assert.Error(t, fd.Err) @@ -426,6 +427,7 @@ func TestTime(t *testing.T) { assert.Equal(t, "*sql.NullTime", fd.Info.String()) assert.True(t, fd.Info.Nillable) assert.True(t, fd.Info.ValueScanner()) + assert.Equal(t, "sql", fd.Info.PkgName) fd = field.Time("deleted_at").GoType(Time{}).Default(time.Now).Descriptor() assert.Error(t, fd.Err) @@ -450,6 +452,7 @@ func TestJSON(t *testing.T) { assert.Equal(t, "comment", fd.Comment) assert.True(t, fd.Info.Nillable) assert.False(t, fd.Info.RType.IsPtr()) + assert.Empty(t, fd.Info.PkgName) type T struct{ S string } fd = field.JSON("name", &T{}). @@ -489,12 +492,15 @@ func TestJSON(t *testing.T) { Default([]http.Dir{"a", "b"}). Descriptor() assert.NoError(t, fd.Err) + assert.Equal(t, "http", fd.Info.PkgName) + fd = field.JSON("dirs", []http.Dir{}). Default(func() []http.Dir { return []http.Dir{"/tmp"} }). Descriptor() assert.NoError(t, fd.Err) + fd = field.JSON("dirs", []http.Dir{}). Default([]string{"a", "b"}). Descriptor() @@ -502,14 +508,19 @@ func TestJSON(t *testing.T) { fd = field.JSON("values", &url.Values{}).Descriptor() assert.Equal(t, "net/url", fd.Info.PkgPath) + assert.Equal(t, "url", fd.Info.PkgName) fd = field.JSON("values", []url.Values{}).Descriptor() assert.Equal(t, "net/url", fd.Info.PkgPath) + assert.Equal(t, "url", fd.Info.PkgName) fd = field.JSON("values", []*url.Values{}).Descriptor() assert.Equal(t, "net/url", fd.Info.PkgPath) + assert.Equal(t, "url", fd.Info.PkgName) fd = field.JSON("values", map[string]url.Values{}).Descriptor() assert.Equal(t, "net/url", fd.Info.PkgPath) + assert.Equal(t, "url", fd.Info.PkgName) fd = field.JSON("values", map[string]*url.Values{}).Descriptor() assert.Equal(t, "net/url", fd.Info.PkgPath) + assert.Equal(t, "url", fd.Info.PkgName) fd = field.JSON("addr", net.Addr(nil)).Descriptor() assert.EqualError(t, fd.Err, "expect a Go value as JSON type, but got nil") }