From 34bd0b7b6f72a93022998be2cc1ca6e9bd97113b Mon Sep 17 00:00:00 2001 From: Ariel Mashraki <7413593+a8m@users.noreply.github.com> Date: Mon, 19 Dec 2022 17:36:29 +0200 Subject: [PATCH] entc/gen: move mutation hooks logic to a shared generic function (#3180) --- entc/gen/template/base.tmpl | 35 ++++++++ entc/gen/template/builder/create.tmpl | 46 +--------- entc/gen/template/builder/delete.tmpl | 37 +------- entc/gen/template/builder/update.tmpl | 84 +------------------ entc/gen/template/dialect/gremlin/create.tmpl | 7 ++ entc/gen/template/dialect/gremlin/delete.tmpl | 1 + entc/gen/template/dialect/gremlin/update.tmpl | 6 ++ entc/gen/template/dialect/sql/create.tmpl | 7 ++ entc/gen/template/dialect/sql/delete.tmpl | 1 + entc/gen/template/dialect/sql/update.tmpl | 6 ++ .../cascadelete/ent/comment_create.go | 49 ++--------- .../cascadelete/ent/comment_delete.go | 37 +------- .../cascadelete/ent/comment_update.go | 84 +++---------------- entc/integration/cascadelete/ent/ent.go | 34 ++++++++ .../cascadelete/ent/post_create.go | 49 ++--------- .../cascadelete/ent/post_delete.go | 37 +------- .../cascadelete/ent/post_update.go | 66 +-------------- .../cascadelete/ent/user_create.go | 49 ++--------- .../cascadelete/ent/user_delete.go | 37 +------- .../cascadelete/ent/user_update.go | 66 +-------------- entc/integration/config/ent/ent.go | 34 ++++++++ entc/integration/config/ent/user_create.go | 49 ++--------- entc/integration/config/ent/user_delete.go | 37 +------- entc/integration/config/ent/user_update.go | 66 +-------------- .../customid/ent/account_create.go | 49 ++--------- .../customid/ent/account_delete.go | 37 +------- .../customid/ent/account_update.go | 84 +++---------------- entc/integration/customid/ent/blob_create.go | 49 ++--------- entc/integration/customid/ent/blob_delete.go | 37 +------- entc/integration/customid/ent/blob_update.go | 66 +-------------- .../customid/ent/bloblink_create.go | 45 +--------- .../customid/ent/bloblink_delete.go | 37 +------- .../customid/ent/bloblink_update.go | 84 +++---------------- entc/integration/customid/ent/car_create.go | 49 ++--------- entc/integration/customid/ent/car_delete.go | 37 +------- entc/integration/customid/ent/car_update.go | 84 +++---------------- .../integration/customid/ent/device_create.go | 49 ++--------- .../integration/customid/ent/device_delete.go | 37 +------- .../integration/customid/ent/device_update.go | 66 +-------------- entc/integration/customid/ent/doc_create.go | 49 ++--------- entc/integration/customid/ent/doc_delete.go | 37 +------- entc/integration/customid/ent/doc_update.go | 66 +-------------- entc/integration/customid/ent/ent.go | 34 ++++++++ entc/integration/customid/ent/group_create.go | 49 ++--------- entc/integration/customid/ent/group_delete.go | 37 +------- entc/integration/customid/ent/group_update.go | 66 +-------------- .../integration/customid/ent/intsid_create.go | 49 ++--------- .../integration/customid/ent/intsid_delete.go | 37 +------- .../integration/customid/ent/intsid_update.go | 66 +-------------- entc/integration/customid/ent/link_create.go | 49 ++--------- entc/integration/customid/ent/link_delete.go | 37 +------- entc/integration/customid/ent/link_update.go | 66 +-------------- .../customid/ent/mixinid_create.go | 49 ++--------- .../customid/ent/mixinid_delete.go | 37 +------- .../customid/ent/mixinid_update.go | 66 +-------------- entc/integration/customid/ent/note_create.go | 49 ++--------- entc/integration/customid/ent/note_delete.go | 37 +------- entc/integration/customid/ent/note_update.go | 66 +-------------- entc/integration/customid/ent/other_create.go | 49 ++--------- entc/integration/customid/ent/other_delete.go | 37 +------- entc/integration/customid/ent/other_update.go | 66 +-------------- entc/integration/customid/ent/pet_create.go | 49 ++--------- entc/integration/customid/ent/pet_delete.go | 37 +------- entc/integration/customid/ent/pet_update.go | 66 +-------------- .../customid/ent/revision_create.go | 49 ++--------- .../customid/ent/revision_delete.go | 37 +------- .../customid/ent/revision_update.go | 66 +-------------- .../customid/ent/session_create.go | 49 ++--------- .../customid/ent/session_delete.go | 37 +------- .../customid/ent/session_update.go | 66 +-------------- entc/integration/customid/ent/token_create.go | 49 ++--------- entc/integration/customid/ent/token_delete.go | 37 +------- entc/integration/customid/ent/token_update.go | 84 +++---------------- entc/integration/customid/ent/user_create.go | 49 ++--------- entc/integration/customid/ent/user_delete.go | 37 +------- entc/integration/customid/ent/user_update.go | 66 +-------------- entc/integration/edgefield/ent/car_create.go | 49 ++--------- entc/integration/edgefield/ent/car_delete.go | 37 +------- entc/integration/edgefield/ent/car_update.go | 66 +-------------- entc/integration/edgefield/ent/card_create.go | 49 ++--------- entc/integration/edgefield/ent/card_delete.go | 37 +------- entc/integration/edgefield/ent/card_update.go | 66 +-------------- entc/integration/edgefield/ent/ent.go | 34 ++++++++ entc/integration/edgefield/ent/info_create.go | 49 ++--------- entc/integration/edgefield/ent/info_delete.go | 37 +------- entc/integration/edgefield/ent/info_update.go | 66 +-------------- .../edgefield/ent/metadata_create.go | 49 ++--------- .../edgefield/ent/metadata_delete.go | 37 +------- .../edgefield/ent/metadata_update.go | 66 +-------------- entc/integration/edgefield/ent/node_create.go | 49 ++--------- entc/integration/edgefield/ent/node_delete.go | 37 +------- entc/integration/edgefield/ent/node_update.go | 66 +-------------- entc/integration/edgefield/ent/pet_create.go | 49 ++--------- entc/integration/edgefield/ent/pet_delete.go | 37 +------- entc/integration/edgefield/ent/pet_update.go | 66 +-------------- entc/integration/edgefield/ent/post_create.go | 49 ++--------- entc/integration/edgefield/ent/post_delete.go | 37 +------- entc/integration/edgefield/ent/post_update.go | 66 +-------------- .../edgefield/ent/rental_create.go | 49 ++--------- .../edgefield/ent/rental_delete.go | 37 +------- .../edgefield/ent/rental_update.go | 84 +++---------------- entc/integration/edgefield/ent/user_create.go | 49 ++--------- entc/integration/edgefield/ent/user_delete.go | 37 +------- entc/integration/edgefield/ent/user_update.go | 66 +-------------- entc/integration/edgeschema/ent/ent.go | 34 ++++++++ .../edgeschema/ent/friendship_create.go | 49 ++--------- .../edgeschema/ent/friendship_delete.go | 37 +------- .../edgeschema/ent/friendship_update.go | 84 +++---------------- .../edgeschema/ent/group_create.go | 49 ++--------- .../edgeschema/ent/group_delete.go | 37 +------- .../edgeschema/ent/group_update.go | 66 +-------------- .../edgeschema/ent/grouptag_create.go | 49 ++--------- .../edgeschema/ent/grouptag_delete.go | 37 +------- .../edgeschema/ent/grouptag_update.go | 84 +++---------------- .../edgeschema/ent/relationship_create.go | 45 +--------- .../edgeschema/ent/relationship_delete.go | 37 +------- .../edgeschema/ent/relationship_update.go | 84 +++---------------- .../edgeschema/ent/relationshipinfo_create.go | 49 ++--------- .../edgeschema/ent/relationshipinfo_delete.go | 37 +------- .../edgeschema/ent/relationshipinfo_update.go | 66 +-------------- .../integration/edgeschema/ent/role_create.go | 49 ++--------- .../integration/edgeschema/ent/role_delete.go | 37 +------- .../integration/edgeschema/ent/role_update.go | 66 +-------------- .../edgeschema/ent/roleuser_create.go | 45 +--------- .../edgeschema/ent/roleuser_delete.go | 37 +------- .../edgeschema/ent/roleuser_update.go | 84 +++---------------- entc/integration/edgeschema/ent/tag_create.go | 49 ++--------- entc/integration/edgeschema/ent/tag_delete.go | 37 +------- entc/integration/edgeschema/ent/tag_update.go | 66 +-------------- .../edgeschema/ent/tweet_create.go | 49 ++--------- .../edgeschema/ent/tweet_delete.go | 37 +------- .../edgeschema/ent/tweet_update.go | 66 +-------------- .../edgeschema/ent/tweetlike_create.go | 45 +--------- .../edgeschema/ent/tweetlike_delete.go | 37 +------- .../edgeschema/ent/tweetlike_update.go | 84 +++---------------- .../edgeschema/ent/tweettag_create.go | 49 ++--------- .../edgeschema/ent/tweettag_delete.go | 37 +------- .../edgeschema/ent/tweettag_update.go | 84 +++---------------- .../integration/edgeschema/ent/user_create.go | 49 ++--------- .../integration/edgeschema/ent/user_delete.go | 37 +------- .../integration/edgeschema/ent/user_update.go | 66 +-------------- .../edgeschema/ent/usergroup_create.go | 49 ++--------- .../edgeschema/ent/usergroup_delete.go | 37 +------- .../edgeschema/ent/usergroup_update.go | 84 +++---------------- .../edgeschema/ent/usertweet_create.go | 49 ++--------- .../edgeschema/ent/usertweet_delete.go | 37 +------- .../edgeschema/ent/usertweet_update.go | 84 +++---------------- entc/integration/ent/card_create.go | 49 ++--------- entc/integration/ent/card_delete.go | 37 +------- entc/integration/ent/card_update.go | 84 +++---------------- entc/integration/ent/comment_create.go | 49 ++--------- entc/integration/ent/comment_delete.go | 37 +------- entc/integration/ent/comment_update.go | 66 +-------------- entc/integration/ent/ent.go | 34 ++++++++ entc/integration/ent/fieldtype_create.go | 49 ++--------- entc/integration/ent/fieldtype_delete.go | 37 +------- entc/integration/ent/fieldtype_update.go | 84 +++---------------- entc/integration/ent/file_create.go | 49 ++--------- entc/integration/ent/file_delete.go | 37 +------- entc/integration/ent/file_update.go | 84 +++---------------- entc/integration/ent/filetype_create.go | 49 ++--------- entc/integration/ent/filetype_delete.go | 37 +------- entc/integration/ent/filetype_update.go | 84 +++---------------- entc/integration/ent/goods_create.go | 49 ++--------- entc/integration/ent/goods_delete.go | 37 +------- entc/integration/ent/goods_update.go | 66 +-------------- entc/integration/ent/group_create.go | 49 ++--------- entc/integration/ent/group_delete.go | 37 +------- entc/integration/ent/group_update.go | 84 +++---------------- entc/integration/ent/groupinfo_create.go | 49 ++--------- entc/integration/ent/groupinfo_delete.go | 37 +------- entc/integration/ent/groupinfo_update.go | 66 +-------------- entc/integration/ent/item_create.go | 49 ++--------- entc/integration/ent/item_delete.go | 37 +------- entc/integration/ent/item_update.go | 84 +++---------------- entc/integration/ent/license_create.go | 49 ++--------- entc/integration/ent/license_delete.go | 37 +------- entc/integration/ent/license_update.go | 66 +-------------- entc/integration/ent/node_create.go | 49 ++--------- entc/integration/ent/node_delete.go | 37 +------- entc/integration/ent/node_update.go | 66 +-------------- entc/integration/ent/pet_create.go | 49 ++--------- entc/integration/ent/pet_delete.go | 37 +------- entc/integration/ent/pet_update.go | 66 +-------------- entc/integration/ent/spec_create.go | 49 ++--------- entc/integration/ent/spec_delete.go | 37 +------- entc/integration/ent/spec_update.go | 66 +-------------- entc/integration/ent/task_create.go | 49 ++--------- entc/integration/ent/task_delete.go | 37 +------- entc/integration/ent/task_update.go | 84 +++---------------- entc/integration/ent/user_create.go | 49 ++--------- entc/integration/ent/user_delete.go | 37 +------- entc/integration/ent/user_update.go | 84 +++---------------- entc/integration/gremlin/ent/card_create.go | 49 ++--------- entc/integration/gremlin/ent/card_delete.go | 37 +------- entc/integration/gremlin/ent/card_update.go | 84 +++---------------- .../integration/gremlin/ent/comment_create.go | 50 ++--------- .../integration/gremlin/ent/comment_delete.go | 37 +------- .../integration/gremlin/ent/comment_update.go | 66 +-------------- entc/integration/gremlin/ent/ent.go | 34 ++++++++ .../gremlin/ent/fieldtype_create.go | 49 ++--------- .../gremlin/ent/fieldtype_delete.go | 37 +------- .../gremlin/ent/fieldtype_update.go | 84 +++---------------- entc/integration/gremlin/ent/file_create.go | 49 ++--------- entc/integration/gremlin/ent/file_delete.go | 37 +------- entc/integration/gremlin/ent/file_update.go | 84 +++---------------- .../gremlin/ent/filetype_create.go | 49 ++--------- .../gremlin/ent/filetype_delete.go | 37 +------- .../gremlin/ent/filetype_update.go | 84 +++---------------- entc/integration/gremlin/ent/goods_create.go | 50 ++--------- entc/integration/gremlin/ent/goods_delete.go | 37 +------- entc/integration/gremlin/ent/goods_update.go | 67 +-------------- entc/integration/gremlin/ent/group_create.go | 49 ++--------- entc/integration/gremlin/ent/group_delete.go | 37 +------- entc/integration/gremlin/ent/group_update.go | 84 +++---------------- .../gremlin/ent/groupinfo_create.go | 50 ++--------- .../gremlin/ent/groupinfo_delete.go | 37 +------- .../gremlin/ent/groupinfo_update.go | 67 +-------------- entc/integration/gremlin/ent/item_create.go | 49 ++--------- entc/integration/gremlin/ent/item_delete.go | 37 +------- entc/integration/gremlin/ent/item_update.go | 84 +++---------------- .../integration/gremlin/ent/license_create.go | 50 ++--------- .../integration/gremlin/ent/license_delete.go | 37 +------- .../integration/gremlin/ent/license_update.go | 67 +-------------- entc/integration/gremlin/ent/node_create.go | 50 ++--------- entc/integration/gremlin/ent/node_delete.go | 37 +------- entc/integration/gremlin/ent/node_update.go | 67 +-------------- entc/integration/gremlin/ent/pet_create.go | 50 ++--------- entc/integration/gremlin/ent/pet_delete.go | 37 +------- entc/integration/gremlin/ent/pet_update.go | 67 +-------------- entc/integration/gremlin/ent/spec_create.go | 50 ++--------- entc/integration/gremlin/ent/spec_delete.go | 37 +------- entc/integration/gremlin/ent/spec_update.go | 67 +-------------- entc/integration/gremlin/ent/task_create.go | 49 ++--------- entc/integration/gremlin/ent/task_delete.go | 37 +------- entc/integration/gremlin/ent/task_update.go | 84 +++---------------- entc/integration/gremlin/ent/user_create.go | 49 ++--------- entc/integration/gremlin/ent/user_delete.go | 37 +------- entc/integration/gremlin/ent/user_update.go | 84 +++---------------- entc/integration/hooks/ent/card_create.go | 49 ++--------- entc/integration/hooks/ent/card_delete.go | 37 +------- entc/integration/hooks/ent/card_update.go | 66 +-------------- entc/integration/hooks/ent/ent.go | 34 ++++++++ entc/integration/hooks/ent/pet_create.go | 49 ++--------- entc/integration/hooks/ent/pet_delete.go | 37 +------- entc/integration/hooks/ent/pet_update.go | 66 +-------------- entc/integration/hooks/ent/user_create.go | 49 ++--------- entc/integration/hooks/ent/user_delete.go | 37 +------- entc/integration/hooks/ent/user_update.go | 66 +-------------- entc/integration/idtype/ent/ent.go | 34 ++++++++ entc/integration/idtype/ent/user_create.go | 49 ++--------- entc/integration/idtype/ent/user_delete.go | 37 +------- entc/integration/idtype/ent/user_update.go | 66 +-------------- entc/integration/json/ent/ent.go | 34 ++++++++ entc/integration/json/ent/user_create.go | 49 ++--------- entc/integration/json/ent/user_delete.go | 37 +------- entc/integration/json/ent/user_update.go | 66 +-------------- entc/integration/migrate/entv1/car_create.go | 49 ++--------- entc/integration/migrate/entv1/car_delete.go | 37 +------- entc/integration/migrate/entv1/car_update.go | 66 +-------------- .../migrate/entv1/conversion_create.go | 49 ++--------- .../migrate/entv1/conversion_delete.go | 37 +------- .../migrate/entv1/conversion_update.go | 66 +-------------- .../migrate/entv1/customtype_create.go | 49 ++--------- .../migrate/entv1/customtype_delete.go | 37 +------- .../migrate/entv1/customtype_update.go | 66 +-------------- entc/integration/migrate/entv1/ent.go | 34 ++++++++ entc/integration/migrate/entv1/user_create.go | 49 ++--------- entc/integration/migrate/entv1/user_delete.go | 37 +------- entc/integration/migrate/entv1/user_update.go | 84 +++---------------- entc/integration/migrate/entv2/blog_create.go | 49 ++--------- entc/integration/migrate/entv2/blog_delete.go | 37 +------- entc/integration/migrate/entv2/blog_update.go | 66 +-------------- entc/integration/migrate/entv2/car_create.go | 49 ++--------- entc/integration/migrate/entv2/car_delete.go | 37 +------- entc/integration/migrate/entv2/car_update.go | 84 +++---------------- .../migrate/entv2/conversion_create.go | 49 ++--------- .../migrate/entv2/conversion_delete.go | 37 +------- .../migrate/entv2/conversion_update.go | 66 +-------------- .../migrate/entv2/customtype_create.go | 49 ++--------- .../migrate/entv2/customtype_delete.go | 37 +------- .../migrate/entv2/customtype_update.go | 66 +-------------- entc/integration/migrate/entv2/ent.go | 34 ++++++++ .../integration/migrate/entv2/group_create.go | 49 ++--------- .../integration/migrate/entv2/group_delete.go | 37 +------- .../integration/migrate/entv2/group_update.go | 66 +-------------- .../integration/migrate/entv2/media_create.go | 49 ++--------- .../integration/migrate/entv2/media_delete.go | 37 +------- .../integration/migrate/entv2/media_update.go | 66 +-------------- entc/integration/migrate/entv2/pet_create.go | 49 ++--------- entc/integration/migrate/entv2/pet_delete.go | 37 +------- entc/integration/migrate/entv2/pet_update.go | 66 +-------------- entc/integration/migrate/entv2/user_create.go | 49 ++--------- entc/integration/migrate/entv2/user_delete.go | 37 +------- entc/integration/migrate/entv2/user_update.go | 84 +++---------------- entc/integration/migrate/entv2/zoo_create.go | 49 ++--------- entc/integration/migrate/entv2/zoo_delete.go | 37 +------- entc/integration/migrate/entv2/zoo_update.go | 66 +-------------- entc/integration/migrate/versioned/ent.go | 34 ++++++++ .../migrate/versioned/group_create.go | 49 ++--------- .../migrate/versioned/group_delete.go | 37 +------- .../migrate/versioned/group_update.go | 66 +-------------- .../migrate/versioned/user_create.go | 49 ++--------- .../migrate/versioned/user_delete.go | 37 +------- .../migrate/versioned/user_update.go | 84 +++---------------- entc/integration/multischema/ent/ent.go | 34 ++++++++ .../multischema/ent/friendship_create.go | 49 ++--------- .../multischema/ent/friendship_delete.go | 37 +------- .../multischema/ent/friendship_update.go | 84 +++---------------- .../multischema/ent/group_create.go | 49 ++--------- .../multischema/ent/group_delete.go | 37 +------- .../multischema/ent/group_update.go | 66 +-------------- .../integration/multischema/ent/pet_create.go | 49 ++--------- .../integration/multischema/ent/pet_delete.go | 37 +------- .../integration/multischema/ent/pet_update.go | 66 +-------------- .../multischema/ent/user_create.go | 49 ++--------- .../multischema/ent/user_delete.go | 37 +------- .../multischema/ent/user_update.go | 66 +-------------- entc/integration/privacy/ent/ent.go | 34 ++++++++ entc/integration/privacy/ent/task_create.go | 49 ++--------- entc/integration/privacy/ent/task_delete.go | 37 +------- entc/integration/privacy/ent/task_update.go | 84 +++---------------- entc/integration/privacy/ent/team_create.go | 49 ++--------- entc/integration/privacy/ent/team_delete.go | 37 +------- entc/integration/privacy/ent/team_update.go | 84 +++---------------- entc/integration/privacy/ent/user_create.go | 49 ++--------- entc/integration/privacy/ent/user_delete.go | 37 +------- entc/integration/privacy/ent/user_update.go | 66 +-------------- entc/integration/template/ent/ent.go | 34 ++++++++ entc/integration/template/ent/group_create.go | 49 ++--------- entc/integration/template/ent/group_delete.go | 37 +------- entc/integration/template/ent/group_update.go | 66 +-------------- entc/integration/template/ent/pet_create.go | 49 ++--------- entc/integration/template/ent/pet_delete.go | 37 +------- entc/integration/template/ent/pet_update.go | 66 +-------------- entc/integration/template/ent/user_create.go | 49 ++--------- entc/integration/template/ent/user_delete.go | 37 +------- entc/integration/template/ent/user_update.go | 66 +-------------- examples/edgeindex/ent/city_create.go | 49 ++--------- examples/edgeindex/ent/city_delete.go | 37 +------- examples/edgeindex/ent/city_update.go | 66 +-------------- examples/edgeindex/ent/ent.go | 34 ++++++++ examples/edgeindex/ent/street_create.go | 49 ++--------- examples/edgeindex/ent/street_delete.go | 37 +------- examples/edgeindex/ent/street_update.go | 66 +-------------- examples/entcpkg/ent/ent.go | 34 ++++++++ examples/entcpkg/ent/user_create.go | 49 ++--------- examples/entcpkg/ent/user_delete.go | 37 +------- examples/entcpkg/ent/user_update.go | 66 +-------------- examples/fs/ent/ent.go | 34 ++++++++ examples/fs/ent/file_create.go | 49 ++--------- examples/fs/ent/file_delete.go | 37 +------- examples/fs/ent/file_update.go | 66 +-------------- examples/jsonencode/ent/ent.go | 34 ++++++++ examples/jsonencode/ent/pet_create.go | 49 ++--------- examples/jsonencode/ent/pet_delete.go | 37 +------- examples/jsonencode/ent/pet_update.go | 84 +++---------------- examples/jsonencode/ent/user_create.go | 49 ++--------- examples/jsonencode/ent/user_delete.go | 37 +------- examples/jsonencode/ent/user_update.go | 66 +-------------- examples/m2m2types/ent/ent.go | 34 ++++++++ examples/m2m2types/ent/group_create.go | 49 ++--------- examples/m2m2types/ent/group_delete.go | 37 +------- examples/m2m2types/ent/group_update.go | 66 +-------------- examples/m2m2types/ent/user_create.go | 49 ++--------- examples/m2m2types/ent/user_delete.go | 37 +------- examples/m2m2types/ent/user_update.go | 66 +-------------- examples/m2mbidi/ent/ent.go | 34 ++++++++ examples/m2mbidi/ent/user_create.go | 49 ++--------- examples/m2mbidi/ent/user_delete.go | 37 +------- examples/m2mbidi/ent/user_update.go | 66 +-------------- examples/m2mrecur/ent/ent.go | 34 ++++++++ examples/m2mrecur/ent/user_create.go | 49 ++--------- examples/m2mrecur/ent/user_delete.go | 37 +------- examples/m2mrecur/ent/user_update.go | 66 +-------------- examples/migration/ent/ent.go | 34 ++++++++ examples/migration/ent/user_create.go | 49 ++--------- examples/migration/ent/user_delete.go | 37 +------- examples/migration/ent/user_update.go | 66 +-------------- examples/o2m2types/ent/ent.go | 34 ++++++++ examples/o2m2types/ent/pet_create.go | 49 ++--------- examples/o2m2types/ent/pet_delete.go | 37 +------- examples/o2m2types/ent/pet_update.go | 66 +-------------- examples/o2m2types/ent/user_create.go | 49 ++--------- examples/o2m2types/ent/user_delete.go | 37 +------- examples/o2m2types/ent/user_update.go | 66 +-------------- examples/o2mrecur/ent/ent.go | 34 ++++++++ examples/o2mrecur/ent/node_create.go | 49 ++--------- examples/o2mrecur/ent/node_delete.go | 37 +------- examples/o2mrecur/ent/node_update.go | 66 +-------------- examples/o2o2types/ent/card_create.go | 49 ++--------- examples/o2o2types/ent/card_delete.go | 37 +------- examples/o2o2types/ent/card_update.go | 84 +++---------------- examples/o2o2types/ent/ent.go | 34 ++++++++ examples/o2o2types/ent/user_create.go | 49 ++--------- examples/o2o2types/ent/user_delete.go | 37 +------- examples/o2o2types/ent/user_update.go | 66 +-------------- examples/o2obidi/ent/ent.go | 34 ++++++++ examples/o2obidi/ent/user_create.go | 49 ++--------- examples/o2obidi/ent/user_delete.go | 37 +------- examples/o2obidi/ent/user_update.go | 66 +-------------- examples/o2orecur/ent/ent.go | 34 ++++++++ examples/o2orecur/ent/node_create.go | 49 ++--------- examples/o2orecur/ent/node_delete.go | 37 +------- examples/o2orecur/ent/node_update.go | 66 +-------------- examples/privacyadmin/ent/ent.go | 34 ++++++++ examples/privacyadmin/ent/user_create.go | 49 ++--------- examples/privacyadmin/ent/user_delete.go | 37 +------- examples/privacyadmin/ent/user_update.go | 66 +-------------- examples/privacytenant/ent/ent.go | 34 ++++++++ examples/privacytenant/ent/group_create.go | 49 ++--------- examples/privacytenant/ent/group_delete.go | 37 +------- examples/privacytenant/ent/group_update.go | 84 +++---------------- examples/privacytenant/ent/tenant_create.go | 49 ++--------- examples/privacytenant/ent/tenant_delete.go | 37 +------- examples/privacytenant/ent/tenant_update.go | 84 +++---------------- examples/privacytenant/ent/user_create.go | 49 ++--------- examples/privacytenant/ent/user_delete.go | 37 +------- examples/privacytenant/ent/user_update.go | 84 +++---------------- examples/start/ent/car_create.go | 49 ++--------- examples/start/ent/car_delete.go | 37 +------- examples/start/ent/car_update.go | 66 +-------------- examples/start/ent/ent.go | 34 ++++++++ examples/start/ent/group_create.go | 49 ++--------- examples/start/ent/group_delete.go | 37 +------- examples/start/ent/group_update.go | 84 +++---------------- examples/start/ent/user_create.go | 49 ++--------- examples/start/ent/user_delete.go | 37 +------- examples/start/ent/user_update.go | 84 +++---------------- examples/traversal/ent/ent.go | 34 ++++++++ examples/traversal/ent/group_create.go | 49 ++--------- examples/traversal/ent/group_delete.go | 37 +------- examples/traversal/ent/group_update.go | 66 +-------------- examples/traversal/ent/pet_create.go | 49 ++--------- examples/traversal/ent/pet_delete.go | 37 +------- examples/traversal/ent/pet_update.go | 66 +-------------- examples/traversal/ent/user_create.go | 49 ++--------- examples/traversal/ent/user_delete.go | 37 +------- examples/traversal/ent/user_update.go | 66 +-------------- examples/version/ent/ent.go | 34 ++++++++ examples/version/ent/user_create.go | 49 ++--------- examples/version/ent/user_delete.go | 37 +------- examples/version/ent/user_update.go | 84 +++---------------- 443 files changed, 3082 insertions(+), 19327 deletions(-) diff --git a/entc/gen/template/base.tmpl b/entc/gen/template/base.tmpl index 1b6b34a95..8876c3c15 100644 --- a/entc/gen/template/base.tmpl +++ b/entc/gen/template/base.tmpl @@ -265,6 +265,41 @@ func (s *selector) ScanX(ctx context.Context, v any) { } {{ end }} + +// withHooks invokes the builder operation with the given hooks, if any. +func withHooks[V Value, M any, PM interface { + *M + Mutation +}](ctx context.Context, exec func(context.Context) (V, error), mutation PM, hooks []Hook) (value V, err error) { + if len(hooks) == 0 { + return exec(ctx) + } + var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { + mutationT, ok := m.(PM) + if !ok { + return nil, fmt.Errorf("unexpected mutation type %T", m) + } + // Set the mutation to the builder. + *mutation = *mutationT + return exec(ctx) + }) + for i := len(hooks) - 1; i >= 0; i-- { + if hooks[i] == nil { + return value, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") + } + mut = hooks[i](mut) + } + v, err := mut.Mutate(ctx, mutation) + if err != nil { + return value, err + } + nv, ok := v.(V) + if !ok { + return value, fmt.Errorf("unexpected node type %T returned from %T", v, mutation) + } + return nv, nil +} + // newQueryContext returns a new context with the given QueryContext attached in case it does not exist. func newQueryContext(ctx context.Context, typ, op string) context.Context { if ent.QueryFromContext(ctx) == nil { diff --git a/entc/gen/template/builder/create.tmpl b/entc/gen/template/builder/create.tmpl index 455262ecd..83f6fe988 100644 --- a/entc/gen/template/builder/create.tmpl +++ b/entc/gen/template/builder/create.tmpl @@ -42,10 +42,6 @@ type {{ $builder }} struct { // Save creates the {{ $.Name }} in the database. func ({{ $receiver }} *{{ $builder }}) Save(ctx context.Context) (*{{ $.Name }}, error) { - var ( - err error - node *{{ $.Name }} - ) {{- if $.HasDefault }} {{- if $runtimeRequired }} if err := {{ $receiver }}.defaults(); err != nil { @@ -55,47 +51,7 @@ func ({{ $receiver }} *{{ $builder }}) Save(ctx context.Context) (*{{ $.Name }}, {{ $receiver }}.defaults() {{- end }} {{- end }} - if len({{ $receiver }}.hooks) == 0 { - if err = {{ $receiver }}.check(); err != nil { - return nil, err - } - node, err = {{ $receiver }}.{{ $.Storage }}Save(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*{{ $.MutationName }}) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = {{ $receiver }}.check(); err != nil { - return nil, err - } - {{ $mutation }} = mutation - if node, err = {{ $receiver }}.{{ $.Storage }}Save(ctx) ; err != nil { - return nil, err - } - {{- if $.HasOneFieldID }} - mutation.{{ $.ID.BuilderField }} = &node.{{ $.ID.StructField }} - mutation.done = true - {{- end }} - return node, err - }) - for i := len({{ $receiver }}.hooks) - 1; i >= 0; i-- { - if {{ $receiver }}.hooks[i] == nil { - return nil, fmt.Errorf("{{ $pkg }}: uninitialized hook (forgotten import {{ $pkg }}/runtime?)") - } - mut = {{ $receiver }}.hooks[i](mut) - } - v, err := mut.Mutate(ctx, {{ $mutation }}) - if err != nil { - return nil, err - } - nv, ok := v.(*{{ $.Name }}) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from {{ $.MutationName }}", v) - } - node = nv - } - return node, err + return withHooks[*{{ $.Name }}, {{ $.MutationName }}](ctx, {{ $receiver }}.{{ $.Storage }}Save, {{ $mutation }}, {{ $receiver }}.hooks) } // SaveX calls Save and panics if Save returns an error. diff --git a/entc/gen/template/builder/delete.tmpl b/entc/gen/template/builder/delete.tmpl index 9bbc01fff..9dbb627a0 100644 --- a/entc/gen/template/builder/delete.tmpl +++ b/entc/gen/template/builder/delete.tmpl @@ -35,41 +35,8 @@ func ({{ $receiver }} *{{ $builder }}) Where(ps ...predicate.{{ $.Name }}) *{{ $ } // Exec executes the deletion query and returns how many vertices were deleted. -func ({{ $receiver}} *{{ $builder }}) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len({{ $receiver }}.hooks) == 0 { - affected, err = {{ $receiver }}.{{ $.Storage }}Exec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*{{ $.MutationName }}) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - {{ $mutation }} = mutation - affected, err = {{ $receiver }}.{{ $.Storage }}Exec(ctx) - mutation.done = true - return affected, err - }) - for i := len({{ $receiver }}.hooks) - 1; i >= 0; i-- { - if {{ $receiver }}.hooks[i] == nil { - return 0, fmt.Errorf("{{ $pkg }}: uninitialized hook (forgotten import {{ $pkg }}/runtime?)") - } - mut = {{ $receiver }}.hooks[i](mut) - } - n, err := mut.Mutate(ctx, {{ $mutation }}) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err +func ({{ $receiver }} *{{ $builder }}) Exec(ctx context.Context) (int, error) { + return withHooks[int, {{ $.MutationName }}](ctx, {{ $receiver }}.{{ $.Storage }}Exec, {{ $mutation }}, {{ $receiver }}.hooks) } // ExecX is like Exec, but panics if an error occurs. diff --git a/entc/gen/template/builder/update.tmpl b/entc/gen/template/builder/update.tmpl index 9a112ee92..3e9b2c665 100644 --- a/entc/gen/template/builder/update.tmpl +++ b/entc/gen/template/builder/update.tmpl @@ -46,10 +46,6 @@ func ({{ $receiver}} *{{ $builder }}) Where(ps ...predicate.{{ $.Name }}) *{{ $b // Save executes the query and returns the number of nodes affected by the update operation. func ({{ $receiver }} *{{ $builder }}) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) {{- if $.HasUpdateDefault }} {{- if $runtimeRequired }} if err := {{ $receiver }}.defaults(); err != nil { @@ -59,40 +55,7 @@ func ({{ $receiver }} *{{ $builder }}) Save(ctx context.Context) (int, error) { {{ $receiver }}.defaults() {{- end }} {{- end }} - if len({{ $receiver }}.hooks) == 0 { - {{- if $.HasUpdateCheckers }} - if err = {{ $receiver }}.check(); err != nil { - return 0, err - } - {{- end }} - affected, err = {{ $receiver }}.{{ $.Storage }}Save(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*{{ $.MutationName }}) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - {{- if $.HasUpdateCheckers }} - if err = {{ $receiver }}.check(); err != nil { - return 0, err - } - {{- end }} - {{ $mutation }} = mutation - affected, err = {{ $receiver }}.{{ $.Storage }}Save(ctx) - mutation.done = true - return affected, err - }) - for i := len({{ $receiver }}.hooks) - 1; i >= 0; i-- { - if {{ $receiver }}.hooks[i] == nil { - return 0, fmt.Errorf("{{ $pkg }}: uninitialized hook (forgotten import {{ $pkg }}/runtime?)") - } - mut = {{ $receiver }}.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, {{ $mutation }}); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, {{ $.MutationName }}](ctx, {{ $receiver }}.{{ $.Storage }}Save, {{ $mutation }}, {{ $receiver }}.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -155,10 +118,6 @@ func ({{ $receiver }} *{{ $onebuilder }}) Select(field string, fields ...string) // Save executes the query and returns the updated {{ $.Name }} entity. func ({{ $receiver }} *{{ $onebuilder }} ) Save(ctx context.Context) (*{{ $.Name }}, error) { - var ( - err error - node *{{ $.Name }} - ) {{- if $.HasUpdateDefault }} {{- if $runtimeRequired }} if err := {{ $receiver }}.defaults(); err != nil { @@ -168,46 +127,7 @@ func ({{ $receiver }} *{{ $onebuilder }} ) Save(ctx context.Context) (*{{ $.Name {{ $receiver }}.defaults() {{- end }} {{- end }} - if len({{ $receiver }}.hooks) == 0 { - {{- if $.HasUpdateCheckers }} - if err = {{ $receiver }}.check(); err != nil { - return nil, err - } - {{- end }} - node, err = {{ $receiver }}.{{ $.Storage }}Save(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*{{ $.MutationName }}) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - {{- if $.HasUpdateCheckers }} - if err = {{ $receiver }}.check(); err != nil { - return nil, err - } - {{- end }} - {{ $mutation }} = mutation - node, err = {{ $receiver }}.{{ $.Storage }}Save(ctx) - mutation.done = true - return node, err - }) - for i := len({{ $receiver }}.hooks) - 1; i >= 0; i-- { - if {{ $receiver }}.hooks[i] == nil { - return nil, fmt.Errorf("{{ $pkg }}: uninitialized hook (forgotten import {{ $pkg }}/runtime?)") - } - mut = {{ $receiver }}.hooks[i](mut) - } - v, err := mut.Mutate(ctx, {{ $mutation }}) - if err != nil { - return nil, err - } - nv, ok := v.(*{{ $.Name }}) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from {{ $.MutationName }}", v) - } - node = nv - } - return node, err + return withHooks[*{{ $.Name }}, {{ $.MutationName }}](ctx, {{ $receiver }}.{{ $.Storage }}Save, {{ $mutation }}, {{ $receiver }}.hooks) } // SaveX is like Save, but panics if an error occurs. diff --git a/entc/gen/template/dialect/gremlin/create.tmpl b/entc/gen/template/dialect/gremlin/create.tmpl index 83bb3fe54..14b1529f2 100644 --- a/entc/gen/template/dialect/gremlin/create.tmpl +++ b/entc/gen/template/dialect/gremlin/create.tmpl @@ -12,6 +12,9 @@ in the LICENSE file in the root directory of this source tree. {{ $mutation := print $receiver ".mutation" }} func ({{ $receiver }} *{{ $builder }}) gremlinSave(ctx context.Context) (*{{ $.Name }}, error) { + if err := {{ $receiver }}.check(); err != nil { + return nil, err + } res := &gremlin.Response{} query, bindings := {{ $receiver }}.gremlin().Query() if err := {{ $receiver }}.driver.Exec(ctx, query, bindings, res); err != nil { @@ -24,6 +27,10 @@ func ({{ $receiver }} *{{ $builder }}) gremlinSave(ctx context.Context) (*{{ $.N if err := {{ $.Receiver }}.FromResponse(res); err != nil { return nil, err } + {{- if $.HasOneFieldID }} + {{ $mutation }}.{{ $.ID.BuilderField }} = &{{ $.Receiver }} .{{ $.ID.StructField }} + {{ $mutation }}.done = true + {{- end }} return {{ $.Receiver }}, nil } diff --git a/entc/gen/template/dialect/gremlin/delete.tmpl b/entc/gen/template/dialect/gremlin/delete.tmpl index e8ea802fc..0ab1bcefc 100644 --- a/entc/gen/template/dialect/gremlin/delete.tmpl +++ b/entc/gen/template/dialect/gremlin/delete.tmpl @@ -17,6 +17,7 @@ func ({{ $receiver}} *{{ $builder }}) gremlinExec(ctx context.Context) (int, err if err := {{ $receiver }}.driver.Exec(ctx, query, bindings, res); err != nil { return 0, err } + {{ $mutation }}.done = true return res.ReadInt() } diff --git a/entc/gen/template/dialect/gremlin/update.tmpl b/entc/gen/template/dialect/gremlin/update.tmpl index 170b03ca0..3d1a9f982 100644 --- a/entc/gen/template/dialect/gremlin/update.tmpl +++ b/entc/gen/template/dialect/gremlin/update.tmpl @@ -15,6 +15,11 @@ in the LICENSE file in the root directory of this source tree. {{ $zero := 0 }}{{ if $one }}{{ $zero = "nil" }}{{ end }} func ({{ $receiver }} *{{ $builder }}) gremlinSave(ctx context.Context) ({{- if $one }}*{{ $.Name }}{{ else }}int{{ end }}, error) { + {{- if $.HasUpdateCheckers }} + if err := {{ $receiver }}.check(); err != nil { + return {{ $zero }}, err + } + {{- end }} res := &gremlin.Response{} {{- if $one }} id, ok := {{ $mutation }}.{{ $.ID.MutationGet }}() @@ -31,6 +36,7 @@ func ({{ $receiver }} *{{ $builder }}) gremlinSave(ctx context.Context) ({{- if if err, ok := isConstantError(res); ok { return {{ $zero }}, err } + {{ $mutation }}.done = true {{- if $one }} {{- $r := $.Receiver }} {{ $r }} := &{{ $.Name }}{config: {{ $receiver }}.config} diff --git a/entc/gen/template/dialect/sql/create.tmpl b/entc/gen/template/dialect/sql/create.tmpl index bad217f20..74a3c11be 100644 --- a/entc/gen/template/dialect/sql/create.tmpl +++ b/entc/gen/template/dialect/sql/create.tmpl @@ -12,6 +12,9 @@ in the LICENSE file in the root directory of this source tree. {{ $mutation := print $receiver ".mutation" }} func ({{ $receiver }} *{{ $builder }}) sqlSave(ctx context.Context) (*{{ $.Name }}, error) { + if err := {{ $receiver }}.check(); err != nil { + return nil, err + } _node, _spec := {{ $receiver }}.createSpec() if err := sqlgraph.CreateNode(ctx, {{ $receiver }}.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -49,6 +52,10 @@ func ({{ $receiver }} *{{ $builder }}) sqlSave(ctx context.Context) (*{{ $.Name } {{- end }} {{- end }} + {{- if $.HasOneFieldID }} + {{ $mutation }}.{{ $.ID.BuilderField }} = &_node.{{ $.ID.StructField }} + {{ $mutation }}.done = true + {{- end }} return _node, nil } diff --git a/entc/gen/template/dialect/sql/delete.tmpl b/entc/gen/template/dialect/sql/delete.tmpl index a70a51d4b..c0fe07294 100644 --- a/entc/gen/template/dialect/sql/delete.tmpl +++ b/entc/gen/template/dialect/sql/delete.tmpl @@ -40,6 +40,7 @@ func ({{ $receiver}} *{{ $builder }}) sqlExec(ctx context.Context) (int, error) if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + {{ $mutation }}.done = true return affected, err } diff --git a/entc/gen/template/dialect/sql/update.tmpl b/entc/gen/template/dialect/sql/update.tmpl index 7cd05c411..6524b49c3 100644 --- a/entc/gen/template/dialect/sql/update.tmpl +++ b/entc/gen/template/dialect/sql/update.tmpl @@ -32,6 +32,11 @@ in the LICENSE file in the root directory of this source tree. {{- end }} func ({{ $receiver }} *{{ $builder }}) sqlSave(ctx context.Context) ({{ $ret }} {{ if $one }}*{{ $.Name }}{{ else }}int{{ end }}, err error) { + {{- if $.HasUpdateCheckers }} + if err := {{ $receiver }}.check(); err != nil { + return {{ $ret }}, err + } + {{- end }} _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: {{ $.Package }}.Table, @@ -171,6 +176,7 @@ func ({{ $receiver }} *{{ $builder }}) sqlSave(ctx context.Context) ({{ $ret }} } return {{ $zero }}, err } + {{ $mutation }}.done = true return {{ $ret }}, nil } {{ end }} diff --git a/entc/integration/cascadelete/ent/comment_create.go b/entc/integration/cascadelete/ent/comment_create.go index 1fa8717bc..5e4ba3cd8 100644 --- a/entc/integration/cascadelete/ent/comment_create.go +++ b/entc/integration/cascadelete/ent/comment_create.go @@ -48,49 +48,7 @@ func (cc *CommentCreate) Mutation() *CommentMutation { // Save creates the Comment in the database. func (cc *CommentCreate) Save(ctx context.Context) (*Comment, error) { - var ( - err error - node *Comment - ) - if len(cc.hooks) == 0 { - if err = cc.check(); err != nil { - return nil, err - } - node, err = cc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CommentMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = cc.check(); err != nil { - return nil, err - } - cc.mutation = mutation - if node, err = cc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(cc.hooks) - 1; i >= 0; i-- { - if cc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = cc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, cc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Comment) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from CommentMutation", v) - } - node = nv - } - return node, err + return withHooks[*Comment, CommentMutation](ctx, cc.sqlSave, cc.mutation, cc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -130,6 +88,9 @@ func (cc *CommentCreate) check() error { } func (cc *CommentCreate) sqlSave(ctx context.Context) (*Comment, error) { + if err := cc.check(); err != nil { + return nil, err + } _node, _spec := cc.createSpec() if err := sqlgraph.CreateNode(ctx, cc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -139,6 +100,8 @@ func (cc *CommentCreate) sqlSave(ctx context.Context) (*Comment, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + cc.mutation.id = &_node.ID + cc.mutation.done = true return _node, nil } diff --git a/entc/integration/cascadelete/ent/comment_delete.go b/entc/integration/cascadelete/ent/comment_delete.go index 9ee9601e0..7445f7c3e 100644 --- a/entc/integration/cascadelete/ent/comment_delete.go +++ b/entc/integration/cascadelete/ent/comment_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (cd *CommentDelete) Where(ps ...predicate.Comment) *CommentDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (cd *CommentDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(cd.hooks) == 0 { - affected, err = cd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CommentMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - cd.mutation = mutation - affected, err = cd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(cd.hooks) - 1; i >= 0; i-- { - if cd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = cd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, cd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, CommentMutation](ctx, cd.sqlExec, cd.mutation, cd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (cd *CommentDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + cd.mutation.done = true return affected, err } diff --git a/entc/integration/cascadelete/ent/comment_update.go b/entc/integration/cascadelete/ent/comment_update.go index 87f5b062a..672721f34 100644 --- a/entc/integration/cascadelete/ent/comment_update.go +++ b/entc/integration/cascadelete/ent/comment_update.go @@ -62,40 +62,7 @@ func (cu *CommentUpdate) ClearPost() *CommentUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (cu *CommentUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(cu.hooks) == 0 { - if err = cu.check(); err != nil { - return 0, err - } - affected, err = cu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CommentMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = cu.check(); err != nil { - return 0, err - } - cu.mutation = mutation - affected, err = cu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(cu.hooks) - 1; i >= 0; i-- { - if cu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = cu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, cu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, CommentMutation](ctx, cu.sqlSave, cu.mutation, cu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -129,6 +96,9 @@ func (cu *CommentUpdate) check() error { } func (cu *CommentUpdate) sqlSave(ctx context.Context) (n int, err error) { + if err := cu.check(); err != nil { + return n, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: comment.Table, @@ -192,6 +162,7 @@ func (cu *CommentUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + cu.mutation.done = true return n, nil } @@ -240,46 +211,7 @@ func (cuo *CommentUpdateOne) Select(field string, fields ...string) *CommentUpda // Save executes the query and returns the updated Comment entity. func (cuo *CommentUpdateOne) Save(ctx context.Context) (*Comment, error) { - var ( - err error - node *Comment - ) - if len(cuo.hooks) == 0 { - if err = cuo.check(); err != nil { - return nil, err - } - node, err = cuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CommentMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = cuo.check(); err != nil { - return nil, err - } - cuo.mutation = mutation - node, err = cuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(cuo.hooks) - 1; i >= 0; i-- { - if cuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = cuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, cuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Comment) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from CommentMutation", v) - } - node = nv - } - return node, err + return withHooks[*Comment, CommentMutation](ctx, cuo.sqlSave, cuo.mutation, cuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -313,6 +245,9 @@ func (cuo *CommentUpdateOne) check() error { } func (cuo *CommentUpdateOne) sqlSave(ctx context.Context) (_node *Comment, err error) { + if err := cuo.check(); err != nil { + return _node, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: comment.Table, @@ -396,5 +331,6 @@ func (cuo *CommentUpdateOne) sqlSave(ctx context.Context) (_node *Comment, err e } return nil, err } + cuo.mutation.done = true return _node, nil } diff --git a/entc/integration/cascadelete/ent/ent.go b/entc/integration/cascadelete/ent/ent.go index 898a80c2c..27aa4671f 100644 --- a/entc/integration/cascadelete/ent/ent.go +++ b/entc/integration/cascadelete/ent/ent.go @@ -477,6 +477,40 @@ func (s *selector) BoolX(ctx context.Context) bool { return v } +// withHooks invokes the builder operation with the given hooks, if any. +func withHooks[V Value, M any, PM interface { + *M + Mutation +}](ctx context.Context, exec func(context.Context) (V, error), mutation PM, hooks []Hook) (value V, err error) { + if len(hooks) == 0 { + return exec(ctx) + } + var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { + mutationT, ok := m.(PM) + if !ok { + return nil, fmt.Errorf("unexpected mutation type %T", m) + } + // Set the mutation to the builder. + *mutation = *mutationT + return exec(ctx) + }) + for i := len(hooks) - 1; i >= 0; i-- { + if hooks[i] == nil { + return value, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") + } + mut = hooks[i](mut) + } + v, err := mut.Mutate(ctx, mutation) + if err != nil { + return value, err + } + nv, ok := v.(V) + if !ok { + return value, fmt.Errorf("unexpected node type %T returned from %T", v, mutation) + } + return nv, nil +} + // newQueryContext returns a new context with the given QueryContext attached in case it does not exist. func newQueryContext(ctx context.Context, typ, op string) context.Context { if ent.QueryFromContext(ctx) == nil { diff --git a/entc/integration/cascadelete/ent/post_create.go b/entc/integration/cascadelete/ent/post_create.go index 930aa466d..57b4b09b9 100644 --- a/entc/integration/cascadelete/ent/post_create.go +++ b/entc/integration/cascadelete/ent/post_create.go @@ -80,50 +80,8 @@ func (pc *PostCreate) Mutation() *PostMutation { // Save creates the Post in the database. func (pc *PostCreate) Save(ctx context.Context) (*Post, error) { - var ( - err error - node *Post - ) pc.defaults() - if len(pc.hooks) == 0 { - if err = pc.check(); err != nil { - return nil, err - } - node, err = pc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*PostMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = pc.check(); err != nil { - return nil, err - } - pc.mutation = mutation - if node, err = pc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(pc.hooks) - 1; i >= 0; i-- { - if pc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = pc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, pc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Post) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from PostMutation", v) - } - node = nv - } - return node, err + return withHooks[*Post, PostMutation](ctx, pc.sqlSave, pc.mutation, pc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -165,6 +123,9 @@ func (pc *PostCreate) check() error { } func (pc *PostCreate) sqlSave(ctx context.Context) (*Post, error) { + if err := pc.check(); err != nil { + return nil, err + } _node, _spec := pc.createSpec() if err := sqlgraph.CreateNode(ctx, pc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -174,6 +135,8 @@ func (pc *PostCreate) sqlSave(ctx context.Context) (*Post, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + pc.mutation.id = &_node.ID + pc.mutation.done = true return _node, nil } diff --git a/entc/integration/cascadelete/ent/post_delete.go b/entc/integration/cascadelete/ent/post_delete.go index 00432b118..55ca13fed 100644 --- a/entc/integration/cascadelete/ent/post_delete.go +++ b/entc/integration/cascadelete/ent/post_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (pd *PostDelete) Where(ps ...predicate.Post) *PostDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (pd *PostDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(pd.hooks) == 0 { - affected, err = pd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*PostMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - pd.mutation = mutation - affected, err = pd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(pd.hooks) - 1; i >= 0; i-- { - if pd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = pd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, pd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, PostMutation](ctx, pd.sqlExec, pd.mutation, pd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (pd *PostDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + pd.mutation.done = true return affected, err } diff --git a/entc/integration/cascadelete/ent/post_update.go b/entc/integration/cascadelete/ent/post_update.go index 23aee5ea2..f2319743b 100644 --- a/entc/integration/cascadelete/ent/post_update.go +++ b/entc/integration/cascadelete/ent/post_update.go @@ -121,34 +121,7 @@ func (pu *PostUpdate) RemoveComments(c ...*Comment) *PostUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (pu *PostUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(pu.hooks) == 0 { - affected, err = pu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*PostMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - pu.mutation = mutation - affected, err = pu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(pu.hooks) - 1; i >= 0; i-- { - if pu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = pu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, pu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, PostMutation](ctx, pu.sqlSave, pu.mutation, pu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -291,6 +264,7 @@ func (pu *PostUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + pu.mutation.done = true return n, nil } @@ -397,40 +371,7 @@ func (puo *PostUpdateOne) Select(field string, fields ...string) *PostUpdateOne // Save executes the query and returns the updated Post entity. func (puo *PostUpdateOne) Save(ctx context.Context) (*Post, error) { - var ( - err error - node *Post - ) - if len(puo.hooks) == 0 { - node, err = puo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*PostMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - puo.mutation = mutation - node, err = puo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(puo.hooks) - 1; i >= 0; i-- { - if puo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = puo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, puo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Post) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from PostMutation", v) - } - node = nv - } - return node, err + return withHooks[*Post, PostMutation](ctx, puo.sqlSave, puo.mutation, puo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -593,5 +534,6 @@ func (puo *PostUpdateOne) sqlSave(ctx context.Context) (_node *Post, err error) } return nil, err } + puo.mutation.done = true return _node, nil } diff --git a/entc/integration/cascadelete/ent/user_create.go b/entc/integration/cascadelete/ent/user_create.go index 69bc61996..5e5c88702 100644 --- a/entc/integration/cascadelete/ent/user_create.go +++ b/entc/integration/cascadelete/ent/user_create.go @@ -60,50 +60,8 @@ func (uc *UserCreate) Mutation() *UserMutation { // Save creates the User in the database. func (uc *UserCreate) Save(ctx context.Context) (*User, error) { - var ( - err error - node *User - ) uc.defaults() - if len(uc.hooks) == 0 { - if err = uc.check(); err != nil { - return nil, err - } - node, err = uc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = uc.check(); err != nil { - return nil, err - } - uc.mutation = mutation - if node, err = uc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(uc.hooks) - 1; i >= 0; i-- { - if uc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*User) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) - } - node = nv - } - return node, err + return withHooks[*User, UserMutation](ctx, uc.sqlSave, uc.mutation, uc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -145,6 +103,9 @@ func (uc *UserCreate) check() error { } func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { + if err := uc.check(); err != nil { + return nil, err + } _node, _spec := uc.createSpec() if err := sqlgraph.CreateNode(ctx, uc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -154,6 +115,8 @@ func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + uc.mutation.id = &_node.ID + uc.mutation.done = true return _node, nil } diff --git a/entc/integration/cascadelete/ent/user_delete.go b/entc/integration/cascadelete/ent/user_delete.go index 9ab4e5db9..bb84605cb 100644 --- a/entc/integration/cascadelete/ent/user_delete.go +++ b/entc/integration/cascadelete/ent/user_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (ud *UserDelete) Where(ps ...predicate.User) *UserDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (ud *UserDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(ud.hooks) == 0 { - affected, err = ud.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - ud.mutation = mutation - affected, err = ud.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(ud.hooks) - 1; i >= 0; i-- { - if ud.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ud.hooks[i](mut) - } - n, err := mut.Mutate(ctx, ud.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, UserMutation](ctx, ud.sqlExec, ud.mutation, ud.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (ud *UserDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + ud.mutation.done = true return affected, err } diff --git a/entc/integration/cascadelete/ent/user_update.go b/entc/integration/cascadelete/ent/user_update.go index 773b3ca6c..71e76e4aa 100644 --- a/entc/integration/cascadelete/ent/user_update.go +++ b/entc/integration/cascadelete/ent/user_update.go @@ -89,34 +89,7 @@ func (uu *UserUpdate) RemovePosts(p ...*Post) *UserUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (uu *UserUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(uu.hooks) == 0 { - affected, err = uu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - uu.mutation = mutation - affected, err = uu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(uu.hooks) - 1; i >= 0; i-- { - if uu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, uu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, UserMutation](ctx, uu.sqlSave, uu.mutation, uu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -224,6 +197,7 @@ func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + uu.mutation.done = true return n, nil } @@ -299,40 +273,7 @@ func (uuo *UserUpdateOne) Select(field string, fields ...string) *UserUpdateOne // Save executes the query and returns the updated User entity. func (uuo *UserUpdateOne) Save(ctx context.Context) (*User, error) { - var ( - err error - node *User - ) - if len(uuo.hooks) == 0 { - node, err = uuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - uuo.mutation = mutation - node, err = uuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(uuo.hooks) - 1; i >= 0; i-- { - if uuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*User) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) - } - node = nv - } - return node, err + return withHooks[*User, UserMutation](ctx, uuo.sqlSave, uuo.mutation, uuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -460,5 +401,6 @@ func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) } return nil, err } + uuo.mutation.done = true return _node, nil } diff --git a/entc/integration/config/ent/ent.go b/entc/integration/config/ent/ent.go index e86be07f0..0b1ff16f8 100644 --- a/entc/integration/config/ent/ent.go +++ b/entc/integration/config/ent/ent.go @@ -473,6 +473,40 @@ func (s *selector) BoolX(ctx context.Context) bool { return v } +// withHooks invokes the builder operation with the given hooks, if any. +func withHooks[V Value, M any, PM interface { + *M + Mutation +}](ctx context.Context, exec func(context.Context) (V, error), mutation PM, hooks []Hook) (value V, err error) { + if len(hooks) == 0 { + return exec(ctx) + } + var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { + mutationT, ok := m.(PM) + if !ok { + return nil, fmt.Errorf("unexpected mutation type %T", m) + } + // Set the mutation to the builder. + *mutation = *mutationT + return exec(ctx) + }) + for i := len(hooks) - 1; i >= 0; i-- { + if hooks[i] == nil { + return value, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") + } + mut = hooks[i](mut) + } + v, err := mut.Mutate(ctx, mutation) + if err != nil { + return value, err + } + nv, ok := v.(V) + if !ok { + return value, fmt.Errorf("unexpected node type %T returned from %T", v, mutation) + } + return nv, nil +} + // newQueryContext returns a new context with the given QueryContext attached in case it does not exist. func newQueryContext(ctx context.Context, typ, op string) context.Context { if ent.QueryFromContext(ctx) == nil { diff --git a/entc/integration/config/ent/user_create.go b/entc/integration/config/ent/user_create.go index f8160a8c7..877e9cb12 100644 --- a/entc/integration/config/ent/user_create.go +++ b/entc/integration/config/ent/user_create.go @@ -63,49 +63,7 @@ func (uc *UserCreate) Mutation() *UserMutation { // Save creates the User in the database. func (uc *UserCreate) Save(ctx context.Context) (*User, error) { - var ( - err error - node *User - ) - if len(uc.hooks) == 0 { - if err = uc.check(); err != nil { - return nil, err - } - node, err = uc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = uc.check(); err != nil { - return nil, err - } - uc.mutation = mutation - if node, err = uc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(uc.hooks) - 1; i >= 0; i-- { - if uc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*User) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) - } - node = nv - } - return node, err + return withHooks[*User, UserMutation](ctx, uc.sqlSave, uc.mutation, uc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -136,6 +94,9 @@ func (uc *UserCreate) check() error { } func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { + if err := uc.check(); err != nil { + return nil, err + } _node, _spec := uc.createSpec() if err := sqlgraph.CreateNode(ctx, uc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -147,6 +108,8 @@ func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { id := _spec.ID.Value.(int64) _node.ID = int(id) } + uc.mutation.id = &_node.ID + uc.mutation.done = true return _node, nil } diff --git a/entc/integration/config/ent/user_delete.go b/entc/integration/config/ent/user_delete.go index 293d53d2f..15838680f 100644 --- a/entc/integration/config/ent/user_delete.go +++ b/entc/integration/config/ent/user_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (ud *UserDelete) Where(ps ...predicate.User) *UserDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (ud *UserDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(ud.hooks) == 0 { - affected, err = ud.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - ud.mutation = mutation - affected, err = ud.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(ud.hooks) - 1; i >= 0; i-- { - if ud.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ud.hooks[i](mut) - } - n, err := mut.Mutate(ctx, ud.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, UserMutation](ctx, ud.sqlExec, ud.mutation, ud.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (ud *UserDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + ud.mutation.done = true return affected, err } diff --git a/entc/integration/config/ent/user_update.go b/entc/integration/config/ent/user_update.go index 45aba0b98..edbc0ab3c 100644 --- a/entc/integration/config/ent/user_update.go +++ b/entc/integration/config/ent/user_update.go @@ -78,34 +78,7 @@ func (uu *UserUpdate) Mutation() *UserMutation { // Save executes the query and returns the number of nodes affected by the update operation. func (uu *UserUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(uu.hooks) == 0 { - affected, err = uu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - uu.mutation = mutation - affected, err = uu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(uu.hooks) - 1; i >= 0; i-- { - if uu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, uu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, UserMutation](ctx, uu.sqlSave, uu.mutation, uu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -168,6 +141,7 @@ func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + uu.mutation.done = true return n, nil } @@ -233,40 +207,7 @@ func (uuo *UserUpdateOne) Select(field string, fields ...string) *UserUpdateOne // Save executes the query and returns the updated User entity. func (uuo *UserUpdateOne) Save(ctx context.Context) (*User, error) { - var ( - err error - node *User - ) - if len(uuo.hooks) == 0 { - node, err = uuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - uuo.mutation = mutation - node, err = uuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(uuo.hooks) - 1; i >= 0; i-- { - if uuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*User) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) - } - node = nv - } - return node, err + return withHooks[*User, UserMutation](ctx, uuo.sqlSave, uuo.mutation, uuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -349,5 +290,6 @@ func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) } return nil, err } + uuo.mutation.done = true return _node, nil } diff --git a/entc/integration/customid/ent/account_create.go b/entc/integration/customid/ent/account_create.go index 858950e37..a8659d093 100644 --- a/entc/integration/customid/ent/account_create.go +++ b/entc/integration/customid/ent/account_create.go @@ -70,50 +70,8 @@ func (ac *AccountCreate) Mutation() *AccountMutation { // Save creates the Account in the database. func (ac *AccountCreate) Save(ctx context.Context) (*Account, error) { - var ( - err error - node *Account - ) ac.defaults() - if len(ac.hooks) == 0 { - if err = ac.check(); err != nil { - return nil, err - } - node, err = ac.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*AccountMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = ac.check(); err != nil { - return nil, err - } - ac.mutation = mutation - if node, err = ac.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(ac.hooks) - 1; i >= 0; i-- { - if ac.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ac.hooks[i](mut) - } - v, err := mut.Mutate(ctx, ac.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Account) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from AccountMutation", v) - } - node = nv - } - return node, err + return withHooks[*Account, AccountMutation](ctx, ac.sqlSave, ac.mutation, ac.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -160,6 +118,9 @@ func (ac *AccountCreate) check() error { } func (ac *AccountCreate) sqlSave(ctx context.Context) (*Account, error) { + if err := ac.check(); err != nil { + return nil, err + } _node, _spec := ac.createSpec() if err := sqlgraph.CreateNode(ctx, ac.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -174,6 +135,8 @@ func (ac *AccountCreate) sqlSave(ctx context.Context) (*Account, error) { return nil, err } } + ac.mutation.id = &_node.ID + ac.mutation.done = true return _node, nil } diff --git a/entc/integration/customid/ent/account_delete.go b/entc/integration/customid/ent/account_delete.go index 7fee2121f..85c77c193 100644 --- a/entc/integration/customid/ent/account_delete.go +++ b/entc/integration/customid/ent/account_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (ad *AccountDelete) Where(ps ...predicate.Account) *AccountDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (ad *AccountDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(ad.hooks) == 0 { - affected, err = ad.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*AccountMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - ad.mutation = mutation - affected, err = ad.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(ad.hooks) - 1; i >= 0; i-- { - if ad.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ad.hooks[i](mut) - } - n, err := mut.Mutate(ctx, ad.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, AccountMutation](ctx, ad.sqlExec, ad.mutation, ad.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (ad *AccountDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + ad.mutation.done = true return affected, err } diff --git a/entc/integration/customid/ent/account_update.go b/entc/integration/customid/ent/account_update.go index ba6b56769..3873d2e57 100644 --- a/entc/integration/customid/ent/account_update.go +++ b/entc/integration/customid/ent/account_update.go @@ -82,40 +82,7 @@ func (au *AccountUpdate) RemoveToken(t ...*Token) *AccountUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (au *AccountUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(au.hooks) == 0 { - if err = au.check(); err != nil { - return 0, err - } - affected, err = au.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*AccountMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = au.check(); err != nil { - return 0, err - } - au.mutation = mutation - affected, err = au.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(au.hooks) - 1; i >= 0; i-- { - if au.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = au.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, au.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, AccountMutation](ctx, au.sqlSave, au.mutation, au.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -151,6 +118,9 @@ func (au *AccountUpdate) check() error { } func (au *AccountUpdate) sqlSave(ctx context.Context) (n int, err error) { + if err := au.check(); err != nil { + return n, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: account.Table, @@ -233,6 +203,7 @@ func (au *AccountUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + au.mutation.done = true return n, nil } @@ -300,46 +271,7 @@ func (auo *AccountUpdateOne) Select(field string, fields ...string) *AccountUpda // Save executes the query and returns the updated Account entity. func (auo *AccountUpdateOne) Save(ctx context.Context) (*Account, error) { - var ( - err error - node *Account - ) - if len(auo.hooks) == 0 { - if err = auo.check(); err != nil { - return nil, err - } - node, err = auo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*AccountMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = auo.check(); err != nil { - return nil, err - } - auo.mutation = mutation - node, err = auo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(auo.hooks) - 1; i >= 0; i-- { - if auo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = auo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, auo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Account) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from AccountMutation", v) - } - node = nv - } - return node, err + return withHooks[*Account, AccountMutation](ctx, auo.sqlSave, auo.mutation, auo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -375,6 +307,9 @@ func (auo *AccountUpdateOne) check() error { } func (auo *AccountUpdateOne) sqlSave(ctx context.Context) (_node *Account, err error) { + if err := auo.check(); err != nil { + return _node, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: account.Table, @@ -477,5 +412,6 @@ func (auo *AccountUpdateOne) sqlSave(ctx context.Context) (_node *Account, err e } return nil, err } + auo.mutation.done = true return _node, nil } diff --git a/entc/integration/customid/ent/blob_create.go b/entc/integration/customid/ent/blob_create.go index b2777ee90..7bf95c52d 100644 --- a/entc/integration/customid/ent/blob_create.go +++ b/entc/integration/customid/ent/blob_create.go @@ -110,50 +110,8 @@ func (bc *BlobCreate) Mutation() *BlobMutation { // Save creates the Blob in the database. func (bc *BlobCreate) Save(ctx context.Context) (*Blob, error) { - var ( - err error - node *Blob - ) bc.defaults() - if len(bc.hooks) == 0 { - if err = bc.check(); err != nil { - return nil, err - } - node, err = bc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*BlobMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = bc.check(); err != nil { - return nil, err - } - bc.mutation = mutation - if node, err = bc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(bc.hooks) - 1; i >= 0; i-- { - if bc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = bc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, bc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Blob) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from BlobMutation", v) - } - node = nv - } - return node, err + return withHooks[*Blob, BlobMutation](ctx, bc.sqlSave, bc.mutation, bc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -206,6 +164,9 @@ func (bc *BlobCreate) check() error { } func (bc *BlobCreate) sqlSave(ctx context.Context) (*Blob, error) { + if err := bc.check(); err != nil { + return nil, err + } _node, _spec := bc.createSpec() if err := sqlgraph.CreateNode(ctx, bc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -220,6 +181,8 @@ func (bc *BlobCreate) sqlSave(ctx context.Context) (*Blob, error) { return nil, err } } + bc.mutation.id = &_node.ID + bc.mutation.done = true return _node, nil } diff --git a/entc/integration/customid/ent/blob_delete.go b/entc/integration/customid/ent/blob_delete.go index 8e387f636..19fb58ce1 100644 --- a/entc/integration/customid/ent/blob_delete.go +++ b/entc/integration/customid/ent/blob_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (bd *BlobDelete) Where(ps ...predicate.Blob) *BlobDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (bd *BlobDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(bd.hooks) == 0 { - affected, err = bd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*BlobMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - bd.mutation = mutation - affected, err = bd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(bd.hooks) - 1; i >= 0; i-- { - if bd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = bd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, bd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, BlobMutation](ctx, bd.sqlExec, bd.mutation, bd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (bd *BlobDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + bd.mutation.done = true return affected, err } diff --git a/entc/integration/customid/ent/blob_update.go b/entc/integration/customid/ent/blob_update.go index 5e7ae81d7..85011276d 100644 --- a/entc/integration/customid/ent/blob_update.go +++ b/entc/integration/customid/ent/blob_update.go @@ -135,34 +135,7 @@ func (bu *BlobUpdate) RemoveLinks(b ...*Blob) *BlobUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (bu *BlobUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(bu.hooks) == 0 { - affected, err = bu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*BlobMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - bu.mutation = mutation - affected, err = bu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(bu.hooks) - 1; i >= 0; i-- { - if bu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = bu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, bu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, BlobMutation](ctx, bu.sqlSave, bu.mutation, bu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -323,6 +296,7 @@ func (bu *BlobUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + bu.mutation.done = true return n, nil } @@ -444,40 +418,7 @@ func (buo *BlobUpdateOne) Select(field string, fields ...string) *BlobUpdateOne // Save executes the query and returns the updated Blob entity. func (buo *BlobUpdateOne) Save(ctx context.Context) (*Blob, error) { - var ( - err error - node *Blob - ) - if len(buo.hooks) == 0 { - node, err = buo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*BlobMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - buo.mutation = mutation - node, err = buo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(buo.hooks) - 1; i >= 0; i-- { - if buo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = buo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, buo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Blob) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from BlobMutation", v) - } - node = nv - } - return node, err + return withHooks[*Blob, BlobMutation](ctx, buo.sqlSave, buo.mutation, buo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -658,5 +599,6 @@ func (buo *BlobUpdateOne) sqlSave(ctx context.Context) (_node *Blob, err error) } return nil, err } + buo.mutation.done = true return _node, nil } diff --git a/entc/integration/customid/ent/bloblink_create.go b/entc/integration/customid/ent/bloblink_create.go index 6cc8098b8..b0380f5d1 100644 --- a/entc/integration/customid/ent/bloblink_create.go +++ b/entc/integration/customid/ent/bloblink_create.go @@ -71,48 +71,8 @@ func (blc *BlobLinkCreate) Mutation() *BlobLinkMutation { // Save creates the BlobLink in the database. func (blc *BlobLinkCreate) Save(ctx context.Context) (*BlobLink, error) { - var ( - err error - node *BlobLink - ) blc.defaults() - if len(blc.hooks) == 0 { - if err = blc.check(); err != nil { - return nil, err - } - node, err = blc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*BlobLinkMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = blc.check(); err != nil { - return nil, err - } - blc.mutation = mutation - if node, err = blc.sqlSave(ctx); err != nil { - return nil, err - } - return node, err - }) - for i := len(blc.hooks) - 1; i >= 0; i-- { - if blc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = blc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, blc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*BlobLink) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from BlobLinkMutation", v) - } - node = nv - } - return node, err + return withHooks[*BlobLink, BlobLinkMutation](ctx, blc.sqlSave, blc.mutation, blc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -166,6 +126,9 @@ func (blc *BlobLinkCreate) check() error { } func (blc *BlobLinkCreate) sqlSave(ctx context.Context) (*BlobLink, error) { + if err := blc.check(); err != nil { + return nil, err + } _node, _spec := blc.createSpec() if err := sqlgraph.CreateNode(ctx, blc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { diff --git a/entc/integration/customid/ent/bloblink_delete.go b/entc/integration/customid/ent/bloblink_delete.go index ec20a0406..d3071d14e 100644 --- a/entc/integration/customid/ent/bloblink_delete.go +++ b/entc/integration/customid/ent/bloblink_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -31,40 +30,7 @@ func (bld *BlobLinkDelete) Where(ps ...predicate.BlobLink) *BlobLinkDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (bld *BlobLinkDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(bld.hooks) == 0 { - affected, err = bld.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*BlobLinkMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - bld.mutation = mutation - affected, err = bld.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(bld.hooks) - 1; i >= 0; i-- { - if bld.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = bld.hooks[i](mut) - } - n, err := mut.Mutate(ctx, bld.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, BlobLinkMutation](ctx, bld.sqlExec, bld.mutation, bld.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -93,6 +59,7 @@ func (bld *BlobLinkDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + bld.mutation.done = true return affected, err } diff --git a/entc/integration/customid/ent/bloblink_update.go b/entc/integration/customid/ent/bloblink_update.go index 0948a5b7a..d1933a264 100644 --- a/entc/integration/customid/ent/bloblink_update.go +++ b/entc/integration/customid/ent/bloblink_update.go @@ -89,40 +89,7 @@ func (blu *BlobLinkUpdate) ClearLink() *BlobLinkUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (blu *BlobLinkUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(blu.hooks) == 0 { - if err = blu.check(); err != nil { - return 0, err - } - affected, err = blu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*BlobLinkMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = blu.check(); err != nil { - return 0, err - } - blu.mutation = mutation - affected, err = blu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(blu.hooks) - 1; i >= 0; i-- { - if blu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = blu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, blu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, BlobLinkMutation](ctx, blu.sqlSave, blu.mutation, blu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -159,6 +126,9 @@ func (blu *BlobLinkUpdate) check() error { } func (blu *BlobLinkUpdate) sqlSave(ctx context.Context) (n int, err error) { + if err := blu.check(); err != nil { + return n, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: bloblink.Table, @@ -263,6 +233,7 @@ func (blu *BlobLinkUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + blu.mutation.done = true return n, nil } @@ -336,46 +307,7 @@ func (bluo *BlobLinkUpdateOne) Select(field string, fields ...string) *BlobLinkU // Save executes the query and returns the updated BlobLink entity. func (bluo *BlobLinkUpdateOne) Save(ctx context.Context) (*BlobLink, error) { - var ( - err error - node *BlobLink - ) - if len(bluo.hooks) == 0 { - if err = bluo.check(); err != nil { - return nil, err - } - node, err = bluo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*BlobLinkMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = bluo.check(); err != nil { - return nil, err - } - bluo.mutation = mutation - node, err = bluo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(bluo.hooks) - 1; i >= 0; i-- { - if bluo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = bluo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, bluo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*BlobLink) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from BlobLinkMutation", v) - } - node = nv - } - return node, err + return withHooks[*BlobLink, BlobLinkMutation](ctx, bluo.sqlSave, bluo.mutation, bluo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -412,6 +344,9 @@ func (bluo *BlobLinkUpdateOne) check() error { } func (bluo *BlobLinkUpdateOne) sqlSave(ctx context.Context) (_node *BlobLink, err error) { + if err := bluo.check(); err != nil { + return _node, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: bloblink.Table, @@ -538,5 +473,6 @@ func (bluo *BlobLinkUpdateOne) sqlSave(ctx context.Context) (_node *BlobLink, er } return nil, err } + bluo.mutation.done = true return _node, nil } diff --git a/entc/integration/customid/ent/car_create.go b/entc/integration/customid/ent/car_create.go index 9b1735880..411388947 100644 --- a/entc/integration/customid/ent/car_create.go +++ b/entc/integration/customid/ent/car_create.go @@ -92,49 +92,7 @@ func (cc *CarCreate) Mutation() *CarMutation { // Save creates the Car in the database. func (cc *CarCreate) Save(ctx context.Context) (*Car, error) { - var ( - err error - node *Car - ) - if len(cc.hooks) == 0 { - if err = cc.check(); err != nil { - return nil, err - } - node, err = cc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CarMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = cc.check(); err != nil { - return nil, err - } - cc.mutation = mutation - if node, err = cc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(cc.hooks) - 1; i >= 0; i-- { - if cc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = cc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, cc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Car) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from CarMutation", v) - } - node = nv - } - return node, err + return withHooks[*Car, CarMutation](ctx, cc.sqlSave, cc.mutation, cc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -183,6 +141,9 @@ func (cc *CarCreate) check() error { } func (cc *CarCreate) sqlSave(ctx context.Context) (*Car, error) { + if err := cc.check(); err != nil { + return nil, err + } _node, _spec := cc.createSpec() if err := sqlgraph.CreateNode(ctx, cc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -194,6 +155,8 @@ func (cc *CarCreate) sqlSave(ctx context.Context) (*Car, error) { id := _spec.ID.Value.(int64) _node.ID = int(id) } + cc.mutation.id = &_node.ID + cc.mutation.done = true return _node, nil } diff --git a/entc/integration/customid/ent/car_delete.go b/entc/integration/customid/ent/car_delete.go index 042247e32..3f636f5c8 100644 --- a/entc/integration/customid/ent/car_delete.go +++ b/entc/integration/customid/ent/car_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (cd *CarDelete) Where(ps ...predicate.Car) *CarDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (cd *CarDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(cd.hooks) == 0 { - affected, err = cd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CarMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - cd.mutation = mutation - affected, err = cd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(cd.hooks) - 1; i >= 0; i-- { - if cd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = cd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, cd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, CarMutation](ctx, cd.sqlExec, cd.mutation, cd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (cd *CarDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + cd.mutation.done = true return affected, err } diff --git a/entc/integration/customid/ent/car_update.go b/entc/integration/customid/ent/car_update.go index 26690ecd8..e2dd36b77 100644 --- a/entc/integration/customid/ent/car_update.go +++ b/entc/integration/customid/ent/car_update.go @@ -124,40 +124,7 @@ func (cu *CarUpdate) ClearOwner() *CarUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (cu *CarUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(cu.hooks) == 0 { - if err = cu.check(); err != nil { - return 0, err - } - affected, err = cu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CarMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = cu.check(); err != nil { - return 0, err - } - cu.mutation = mutation - affected, err = cu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(cu.hooks) - 1; i >= 0; i-- { - if cu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = cu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, cu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, CarMutation](ctx, cu.sqlSave, cu.mutation, cu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -198,6 +165,9 @@ func (cu *CarUpdate) check() error { } func (cu *CarUpdate) sqlSave(ctx context.Context) (n int, err error) { + if err := cu.check(); err != nil { + return n, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: car.Table, @@ -279,6 +249,7 @@ func (cu *CarUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + cu.mutation.done = true return n, nil } @@ -389,46 +360,7 @@ func (cuo *CarUpdateOne) Select(field string, fields ...string) *CarUpdateOne { // Save executes the query and returns the updated Car entity. func (cuo *CarUpdateOne) Save(ctx context.Context) (*Car, error) { - var ( - err error - node *Car - ) - if len(cuo.hooks) == 0 { - if err = cuo.check(); err != nil { - return nil, err - } - node, err = cuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CarMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = cuo.check(); err != nil { - return nil, err - } - cuo.mutation = mutation - node, err = cuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(cuo.hooks) - 1; i >= 0; i-- { - if cuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = cuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, cuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Car) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from CarMutation", v) - } - node = nv - } - return node, err + return withHooks[*Car, CarMutation](ctx, cuo.sqlSave, cuo.mutation, cuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -469,6 +401,9 @@ func (cuo *CarUpdateOne) check() error { } func (cuo *CarUpdateOne) sqlSave(ctx context.Context) (_node *Car, err error) { + if err := cuo.check(); err != nil { + return _node, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: car.Table, @@ -570,5 +505,6 @@ func (cuo *CarUpdateOne) sqlSave(ctx context.Context) (_node *Car, err error) { } return nil, err } + cuo.mutation.done = true return _node, nil } diff --git a/entc/integration/customid/ent/device_create.go b/entc/integration/customid/ent/device_create.go index 787ea1e5e..c0d346ec3 100644 --- a/entc/integration/customid/ent/device_create.go +++ b/entc/integration/customid/ent/device_create.go @@ -83,50 +83,8 @@ func (dc *DeviceCreate) Mutation() *DeviceMutation { // Save creates the Device in the database. func (dc *DeviceCreate) Save(ctx context.Context) (*Device, error) { - var ( - err error - node *Device - ) dc.defaults() - if len(dc.hooks) == 0 { - if err = dc.check(); err != nil { - return nil, err - } - node, err = dc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*DeviceMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = dc.check(); err != nil { - return nil, err - } - dc.mutation = mutation - if node, err = dc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(dc.hooks) - 1; i >= 0; i-- { - if dc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = dc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, dc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Device) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from DeviceMutation", v) - } - node = nv - } - return node, err + return withHooks[*Device, DeviceMutation](ctx, dc.sqlSave, dc.mutation, dc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -170,6 +128,9 @@ func (dc *DeviceCreate) check() error { } func (dc *DeviceCreate) sqlSave(ctx context.Context) (*Device, error) { + if err := dc.check(); err != nil { + return nil, err + } _node, _spec := dc.createSpec() if err := sqlgraph.CreateNode(ctx, dc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -184,6 +145,8 @@ func (dc *DeviceCreate) sqlSave(ctx context.Context) (*Device, error) { return nil, err } } + dc.mutation.id = &_node.ID + dc.mutation.done = true return _node, nil } diff --git a/entc/integration/customid/ent/device_delete.go b/entc/integration/customid/ent/device_delete.go index e2b974a7a..ca5507960 100644 --- a/entc/integration/customid/ent/device_delete.go +++ b/entc/integration/customid/ent/device_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (dd *DeviceDelete) Where(ps ...predicate.Device) *DeviceDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (dd *DeviceDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(dd.hooks) == 0 { - affected, err = dd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*DeviceMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - dd.mutation = mutation - affected, err = dd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(dd.hooks) - 1; i >= 0; i-- { - if dd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = dd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, dd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, DeviceMutation](ctx, dd.sqlExec, dd.mutation, dd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (dd *DeviceDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + dd.mutation.done = true return affected, err } diff --git a/entc/integration/customid/ent/device_update.go b/entc/integration/customid/ent/device_update.go index 2619c760c..5605f6ec2 100644 --- a/entc/integration/customid/ent/device_update.go +++ b/entc/integration/customid/ent/device_update.go @@ -101,34 +101,7 @@ func (du *DeviceUpdate) RemoveSessions(s ...*Session) *DeviceUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (du *DeviceUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(du.hooks) == 0 { - affected, err = du.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*DeviceMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - du.mutation = mutation - affected, err = du.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(du.hooks) - 1; i >= 0; i-- { - if du.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = du.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, du.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, DeviceMutation](ctx, du.sqlSave, du.mutation, du.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -268,6 +241,7 @@ func (du *DeviceUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + du.mutation.done = true return n, nil } @@ -354,40 +328,7 @@ func (duo *DeviceUpdateOne) Select(field string, fields ...string) *DeviceUpdate // Save executes the query and returns the updated Device entity. func (duo *DeviceUpdateOne) Save(ctx context.Context) (*Device, error) { - var ( - err error - node *Device - ) - if len(duo.hooks) == 0 { - node, err = duo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*DeviceMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - duo.mutation = mutation - node, err = duo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(duo.hooks) - 1; i >= 0; i-- { - if duo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = duo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, duo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Device) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from DeviceMutation", v) - } - node = nv - } - return node, err + return withHooks[*Device, DeviceMutation](ctx, duo.sqlSave, duo.mutation, duo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -547,5 +488,6 @@ func (duo *DeviceUpdateOne) sqlSave(ctx context.Context) (_node *Device, err err } return nil, err } + duo.mutation.done = true return _node, nil } diff --git a/entc/integration/customid/ent/doc_create.go b/entc/integration/customid/ent/doc_create.go index 950c80d0f..2c7ae0cb7 100644 --- a/entc/integration/customid/ent/doc_create.go +++ b/entc/integration/customid/ent/doc_create.go @@ -111,50 +111,8 @@ func (dc *DocCreate) Mutation() *DocMutation { // Save creates the Doc in the database. func (dc *DocCreate) Save(ctx context.Context) (*Doc, error) { - var ( - err error - node *Doc - ) dc.defaults() - if len(dc.hooks) == 0 { - if err = dc.check(); err != nil { - return nil, err - } - node, err = dc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*DocMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = dc.check(); err != nil { - return nil, err - } - dc.mutation = mutation - if node, err = dc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(dc.hooks) - 1; i >= 0; i-- { - if dc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = dc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, dc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Doc) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from DocMutation", v) - } - node = nv - } - return node, err + return withHooks[*Doc, DocMutation](ctx, dc.sqlSave, dc.mutation, dc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -198,6 +156,9 @@ func (dc *DocCreate) check() error { } func (dc *DocCreate) sqlSave(ctx context.Context) (*Doc, error) { + if err := dc.check(); err != nil { + return nil, err + } _node, _spec := dc.createSpec() if err := sqlgraph.CreateNode(ctx, dc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -212,6 +173,8 @@ func (dc *DocCreate) sqlSave(ctx context.Context) (*Doc, error) { return nil, err } } + dc.mutation.id = &_node.ID + dc.mutation.done = true return _node, nil } diff --git a/entc/integration/customid/ent/doc_delete.go b/entc/integration/customid/ent/doc_delete.go index f532babf7..347195ecc 100644 --- a/entc/integration/customid/ent/doc_delete.go +++ b/entc/integration/customid/ent/doc_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (dd *DocDelete) Where(ps ...predicate.Doc) *DocDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (dd *DocDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(dd.hooks) == 0 { - affected, err = dd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*DocMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - dd.mutation = mutation - affected, err = dd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(dd.hooks) - 1; i >= 0; i-- { - if dd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = dd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, dd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, DocMutation](ctx, dd.sqlExec, dd.mutation, dd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (dd *DocDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + dd.mutation.done = true return affected, err } diff --git a/entc/integration/customid/ent/doc_update.go b/entc/integration/customid/ent/doc_update.go index 31ea0990b..603ec874a 100644 --- a/entc/integration/customid/ent/doc_update.go +++ b/entc/integration/customid/ent/doc_update.go @@ -156,34 +156,7 @@ func (du *DocUpdate) RemoveRelated(d ...*Doc) *DocUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (du *DocUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(du.hooks) == 0 { - affected, err = du.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*DocMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - du.mutation = mutation - affected, err = du.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(du.hooks) - 1; i >= 0; i-- { - if du.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = du.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, du.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, DocMutation](ctx, du.sqlSave, du.mutation, du.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -383,6 +356,7 @@ func (du *DocUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + du.mutation.done = true return n, nil } @@ -525,40 +499,7 @@ func (duo *DocUpdateOne) Select(field string, fields ...string) *DocUpdateOne { // Save executes the query and returns the updated Doc entity. func (duo *DocUpdateOne) Save(ctx context.Context) (*Doc, error) { - var ( - err error - node *Doc - ) - if len(duo.hooks) == 0 { - node, err = duo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*DocMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - duo.mutation = mutation - node, err = duo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(duo.hooks) - 1; i >= 0; i-- { - if duo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = duo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, duo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Doc) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from DocMutation", v) - } - node = nv - } - return node, err + return withHooks[*Doc, DocMutation](ctx, duo.sqlSave, duo.mutation, duo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -778,5 +719,6 @@ func (duo *DocUpdateOne) sqlSave(ctx context.Context) (_node *Doc, err error) { } return nil, err } + duo.mutation.done = true return _node, nil } diff --git a/entc/integration/customid/ent/ent.go b/entc/integration/customid/ent/ent.go index 384a4c603..a5c81e197 100644 --- a/entc/integration/customid/ent/ent.go +++ b/entc/integration/customid/ent/ent.go @@ -505,6 +505,40 @@ func (s *selector) BoolX(ctx context.Context) bool { return v } +// withHooks invokes the builder operation with the given hooks, if any. +func withHooks[V Value, M any, PM interface { + *M + Mutation +}](ctx context.Context, exec func(context.Context) (V, error), mutation PM, hooks []Hook) (value V, err error) { + if len(hooks) == 0 { + return exec(ctx) + } + var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { + mutationT, ok := m.(PM) + if !ok { + return nil, fmt.Errorf("unexpected mutation type %T", m) + } + // Set the mutation to the builder. + *mutation = *mutationT + return exec(ctx) + }) + for i := len(hooks) - 1; i >= 0; i-- { + if hooks[i] == nil { + return value, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") + } + mut = hooks[i](mut) + } + v, err := mut.Mutate(ctx, mutation) + if err != nil { + return value, err + } + nv, ok := v.(V) + if !ok { + return value, fmt.Errorf("unexpected node type %T returned from %T", v, mutation) + } + return nv, nil +} + // newQueryContext returns a new context with the given QueryContext attached in case it does not exist. func newQueryContext(ctx context.Context, typ, op string) context.Context { if ent.QueryFromContext(ctx) == nil { diff --git a/entc/integration/customid/ent/group_create.go b/entc/integration/customid/ent/group_create.go index a5b258dab..a688b67be 100644 --- a/entc/integration/customid/ent/group_create.go +++ b/entc/integration/customid/ent/group_create.go @@ -54,49 +54,7 @@ func (gc *GroupCreate) Mutation() *GroupMutation { // Save creates the Group in the database. func (gc *GroupCreate) Save(ctx context.Context) (*Group, error) { - var ( - err error - node *Group - ) - if len(gc.hooks) == 0 { - if err = gc.check(); err != nil { - return nil, err - } - node, err = gc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GroupMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = gc.check(); err != nil { - return nil, err - } - gc.mutation = mutation - if node, err = gc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(gc.hooks) - 1; i >= 0; i-- { - if gc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = gc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, gc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Group) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from GroupMutation", v) - } - node = nv - } - return node, err + return withHooks[*Group, GroupMutation](ctx, gc.sqlSave, gc.mutation, gc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -127,6 +85,9 @@ func (gc *GroupCreate) check() error { } func (gc *GroupCreate) sqlSave(ctx context.Context) (*Group, error) { + if err := gc.check(); err != nil { + return nil, err + } _node, _spec := gc.createSpec() if err := sqlgraph.CreateNode(ctx, gc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -138,6 +99,8 @@ func (gc *GroupCreate) sqlSave(ctx context.Context) (*Group, error) { id := _spec.ID.Value.(int64) _node.ID = int(id) } + gc.mutation.id = &_node.ID + gc.mutation.done = true return _node, nil } diff --git a/entc/integration/customid/ent/group_delete.go b/entc/integration/customid/ent/group_delete.go index 2688cb631..be5018c5f 100644 --- a/entc/integration/customid/ent/group_delete.go +++ b/entc/integration/customid/ent/group_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (gd *GroupDelete) Where(ps ...predicate.Group) *GroupDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (gd *GroupDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(gd.hooks) == 0 { - affected, err = gd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GroupMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - gd.mutation = mutation - affected, err = gd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(gd.hooks) - 1; i >= 0; i-- { - if gd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = gd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, gd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, GroupMutation](ctx, gd.sqlExec, gd.mutation, gd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (gd *GroupDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + gd.mutation.done = true return affected, err } diff --git a/entc/integration/customid/ent/group_update.go b/entc/integration/customid/ent/group_update.go index 23d37bed4..66704b28d 100644 --- a/entc/integration/customid/ent/group_update.go +++ b/entc/integration/customid/ent/group_update.go @@ -75,34 +75,7 @@ func (gu *GroupUpdate) RemoveUsers(u ...*User) *GroupUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (gu *GroupUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(gu.hooks) == 0 { - affected, err = gu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GroupMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - gu.mutation = mutation - affected, err = gu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(gu.hooks) - 1; i >= 0; i-- { - if gu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = gu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, gu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, GroupMutation](ctx, gu.sqlSave, gu.mutation, gu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -207,6 +180,7 @@ func (gu *GroupUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + gu.mutation.done = true return n, nil } @@ -268,40 +242,7 @@ func (guo *GroupUpdateOne) Select(field string, fields ...string) *GroupUpdateOn // Save executes the query and returns the updated Group entity. func (guo *GroupUpdateOne) Save(ctx context.Context) (*Group, error) { - var ( - err error - node *Group - ) - if len(guo.hooks) == 0 { - node, err = guo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GroupMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - guo.mutation = mutation - node, err = guo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(guo.hooks) - 1; i >= 0; i-- { - if guo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = guo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, guo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Group) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from GroupMutation", v) - } - node = nv - } - return node, err + return withHooks[*Group, GroupMutation](ctx, guo.sqlSave, guo.mutation, guo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -426,5 +367,6 @@ func (guo *GroupUpdateOne) sqlSave(ctx context.Context) (_node *Group, err error } return nil, err } + guo.mutation.done = true return _node, nil } diff --git a/entc/integration/customid/ent/intsid_create.go b/entc/integration/customid/ent/intsid_create.go index 58a18514d..52eda2c99 100644 --- a/entc/integration/customid/ent/intsid_create.go +++ b/entc/integration/customid/ent/intsid_create.go @@ -73,49 +73,7 @@ func (isc *IntSIDCreate) Mutation() *IntSIDMutation { // Save creates the IntSID in the database. func (isc *IntSIDCreate) Save(ctx context.Context) (*IntSID, error) { - var ( - err error - node *IntSID - ) - if len(isc.hooks) == 0 { - if err = isc.check(); err != nil { - return nil, err - } - node, err = isc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*IntSIDMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = isc.check(); err != nil { - return nil, err - } - isc.mutation = mutation - if node, err = isc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(isc.hooks) - 1; i >= 0; i-- { - if isc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = isc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, isc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*IntSID) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from IntSIDMutation", v) - } - node = nv - } - return node, err + return withHooks[*IntSID, IntSIDMutation](ctx, isc.sqlSave, isc.mutation, isc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -146,6 +104,9 @@ func (isc *IntSIDCreate) check() error { } func (isc *IntSIDCreate) sqlSave(ctx context.Context) (*IntSID, error) { + if err := isc.check(); err != nil { + return nil, err + } _node, _spec := isc.createSpec() if err := sqlgraph.CreateNode(ctx, isc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -160,6 +121,8 @@ func (isc *IntSIDCreate) sqlSave(ctx context.Context) (*IntSID, error) { return nil, err } } + isc.mutation.id = &_node.ID + isc.mutation.done = true return _node, nil } diff --git a/entc/integration/customid/ent/intsid_delete.go b/entc/integration/customid/ent/intsid_delete.go index 422b24763..9e94f8676 100644 --- a/entc/integration/customid/ent/intsid_delete.go +++ b/entc/integration/customid/ent/intsid_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (isd *IntSIDDelete) Where(ps ...predicate.IntSID) *IntSIDDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (isd *IntSIDDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(isd.hooks) == 0 { - affected, err = isd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*IntSIDMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - isd.mutation = mutation - affected, err = isd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(isd.hooks) - 1; i >= 0; i-- { - if isd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = isd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, isd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, IntSIDMutation](ctx, isd.sqlExec, isd.mutation, isd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (isd *IntSIDDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + isd.mutation.done = true return affected, err } diff --git a/entc/integration/customid/ent/intsid_update.go b/entc/integration/customid/ent/intsid_update.go index 869f404fd..d0514438c 100644 --- a/entc/integration/customid/ent/intsid_update.go +++ b/entc/integration/customid/ent/intsid_update.go @@ -100,34 +100,7 @@ func (isu *IntSIDUpdate) RemoveChildren(i ...*IntSID) *IntSIDUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (isu *IntSIDUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(isu.hooks) == 0 { - affected, err = isu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*IntSIDMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - isu.mutation = mutation - affected, err = isu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(isu.hooks) - 1; i >= 0; i-- { - if isu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = isu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, isu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, IntSIDMutation](ctx, isu.sqlSave, isu.mutation, isu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -267,6 +240,7 @@ func (isu *IntSIDUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + isu.mutation.done = true return n, nil } @@ -353,40 +327,7 @@ func (isuo *IntSIDUpdateOne) Select(field string, fields ...string) *IntSIDUpdat // Save executes the query and returns the updated IntSID entity. func (isuo *IntSIDUpdateOne) Save(ctx context.Context) (*IntSID, error) { - var ( - err error - node *IntSID - ) - if len(isuo.hooks) == 0 { - node, err = isuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*IntSIDMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - isuo.mutation = mutation - node, err = isuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(isuo.hooks) - 1; i >= 0; i-- { - if isuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = isuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, isuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*IntSID) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from IntSIDMutation", v) - } - node = nv - } - return node, err + return withHooks[*IntSID, IntSIDMutation](ctx, isuo.sqlSave, isuo.mutation, isuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -546,5 +487,6 @@ func (isuo *IntSIDUpdateOne) sqlSave(ctx context.Context) (_node *IntSID, err er } return nil, err } + isuo.mutation.done = true return _node, nil } diff --git a/entc/integration/customid/ent/link_create.go b/entc/integration/customid/ent/link_create.go index 92a7860fc..dd30829f8 100644 --- a/entc/integration/customid/ent/link_create.go +++ b/entc/integration/customid/ent/link_create.go @@ -55,50 +55,8 @@ func (lc *LinkCreate) Mutation() *LinkMutation { // Save creates the Link in the database. func (lc *LinkCreate) Save(ctx context.Context) (*Link, error) { - var ( - err error - node *Link - ) lc.defaults() - if len(lc.hooks) == 0 { - if err = lc.check(); err != nil { - return nil, err - } - node, err = lc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*LinkMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = lc.check(); err != nil { - return nil, err - } - lc.mutation = mutation - if node, err = lc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(lc.hooks) - 1; i >= 0; i-- { - if lc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = lc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, lc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Link) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from LinkMutation", v) - } - node = nv - } - return node, err + return withHooks[*Link, LinkMutation](ctx, lc.sqlSave, lc.mutation, lc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -144,6 +102,9 @@ func (lc *LinkCreate) check() error { } func (lc *LinkCreate) sqlSave(ctx context.Context) (*Link, error) { + if err := lc.check(); err != nil { + return nil, err + } _node, _spec := lc.createSpec() if err := sqlgraph.CreateNode(ctx, lc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -158,6 +119,8 @@ func (lc *LinkCreate) sqlSave(ctx context.Context) (*Link, error) { return nil, err } } + lc.mutation.id = &_node.ID + lc.mutation.done = true return _node, nil } diff --git a/entc/integration/customid/ent/link_delete.go b/entc/integration/customid/ent/link_delete.go index abc78a319..0b31ab2db 100644 --- a/entc/integration/customid/ent/link_delete.go +++ b/entc/integration/customid/ent/link_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (ld *LinkDelete) Where(ps ...predicate.Link) *LinkDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (ld *LinkDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(ld.hooks) == 0 { - affected, err = ld.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*LinkMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - ld.mutation = mutation - affected, err = ld.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(ld.hooks) - 1; i >= 0; i-- { - if ld.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ld.hooks[i](mut) - } - n, err := mut.Mutate(ctx, ld.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, LinkMutation](ctx, ld.sqlExec, ld.mutation, ld.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (ld *LinkDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + ld.mutation.done = true return affected, err } diff --git a/entc/integration/customid/ent/link_update.go b/entc/integration/customid/ent/link_update.go index 847690903..fc2bacd03 100644 --- a/entc/integration/customid/ent/link_update.go +++ b/entc/integration/customid/ent/link_update.go @@ -45,34 +45,7 @@ func (lu *LinkUpdate) Mutation() *LinkMutation { // Save executes the query and returns the number of nodes affected by the update operation. func (lu *LinkUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(lu.hooks) == 0 { - affected, err = lu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*LinkMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - lu.mutation = mutation - affected, err = lu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(lu.hooks) - 1; i >= 0; i-- { - if lu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = lu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, lu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, LinkMutation](ctx, lu.sqlSave, lu.mutation, lu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -126,6 +99,7 @@ func (lu *LinkUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + lu.mutation.done = true return n, nil } @@ -157,40 +131,7 @@ func (luo *LinkUpdateOne) Select(field string, fields ...string) *LinkUpdateOne // Save executes the query and returns the updated Link entity. func (luo *LinkUpdateOne) Save(ctx context.Context) (*Link, error) { - var ( - err error - node *Link - ) - if len(luo.hooks) == 0 { - node, err = luo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*LinkMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - luo.mutation = mutation - node, err = luo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(luo.hooks) - 1; i >= 0; i-- { - if luo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = luo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, luo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Link) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from LinkMutation", v) - } - node = nv - } - return node, err + return withHooks[*Link, LinkMutation](ctx, luo.sqlSave, luo.mutation, luo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -264,5 +205,6 @@ func (luo *LinkUpdateOne) sqlSave(ctx context.Context) (_node *Link, err error) } return nil, err } + luo.mutation.done = true return _node, nil } diff --git a/entc/integration/customid/ent/mixinid_create.go b/entc/integration/customid/ent/mixinid_create.go index 7f702f563..b80542748 100644 --- a/entc/integration/customid/ent/mixinid_create.go +++ b/entc/integration/customid/ent/mixinid_create.go @@ -60,50 +60,8 @@ func (mic *MixinIDCreate) Mutation() *MixinIDMutation { // Save creates the MixinID in the database. func (mic *MixinIDCreate) Save(ctx context.Context) (*MixinID, error) { - var ( - err error - node *MixinID - ) mic.defaults() - if len(mic.hooks) == 0 { - if err = mic.check(); err != nil { - return nil, err - } - node, err = mic.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*MixinIDMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = mic.check(); err != nil { - return nil, err - } - mic.mutation = mutation - if node, err = mic.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(mic.hooks) - 1; i >= 0; i-- { - if mic.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = mic.hooks[i](mut) - } - v, err := mut.Mutate(ctx, mic.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*MixinID) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from MixinIDMutation", v) - } - node = nv - } - return node, err + return withHooks[*MixinID, MixinIDMutation](ctx, mic.sqlSave, mic.mutation, mic.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -148,6 +106,9 @@ func (mic *MixinIDCreate) check() error { } func (mic *MixinIDCreate) sqlSave(ctx context.Context) (*MixinID, error) { + if err := mic.check(); err != nil { + return nil, err + } _node, _spec := mic.createSpec() if err := sqlgraph.CreateNode(ctx, mic.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -162,6 +123,8 @@ func (mic *MixinIDCreate) sqlSave(ctx context.Context) (*MixinID, error) { return nil, err } } + mic.mutation.id = &_node.ID + mic.mutation.done = true return _node, nil } diff --git a/entc/integration/customid/ent/mixinid_delete.go b/entc/integration/customid/ent/mixinid_delete.go index 179e745c4..7bfd5b4c2 100644 --- a/entc/integration/customid/ent/mixinid_delete.go +++ b/entc/integration/customid/ent/mixinid_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (mid *MixinIDDelete) Where(ps ...predicate.MixinID) *MixinIDDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (mid *MixinIDDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(mid.hooks) == 0 { - affected, err = mid.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*MixinIDMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - mid.mutation = mutation - affected, err = mid.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(mid.hooks) - 1; i >= 0; i-- { - if mid.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = mid.hooks[i](mut) - } - n, err := mut.Mutate(ctx, mid.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, MixinIDMutation](ctx, mid.sqlExec, mid.mutation, mid.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (mid *MixinIDDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + mid.mutation.done = true return affected, err } diff --git a/entc/integration/customid/ent/mixinid_update.go b/entc/integration/customid/ent/mixinid_update.go index 7e04b3f34..c8cca0235 100644 --- a/entc/integration/customid/ent/mixinid_update.go +++ b/entc/integration/customid/ent/mixinid_update.go @@ -50,34 +50,7 @@ func (miu *MixinIDUpdate) Mutation() *MixinIDMutation { // Save executes the query and returns the number of nodes affected by the update operation. func (miu *MixinIDUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(miu.hooks) == 0 { - affected, err = miu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*MixinIDMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - miu.mutation = mutation - affected, err = miu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(miu.hooks) - 1; i >= 0; i-- { - if miu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = miu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, miu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, MixinIDMutation](ctx, miu.sqlSave, miu.mutation, miu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -134,6 +107,7 @@ func (miu *MixinIDUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + miu.mutation.done = true return n, nil } @@ -171,40 +145,7 @@ func (miuo *MixinIDUpdateOne) Select(field string, fields ...string) *MixinIDUpd // Save executes the query and returns the updated MixinID entity. func (miuo *MixinIDUpdateOne) Save(ctx context.Context) (*MixinID, error) { - var ( - err error - node *MixinID - ) - if len(miuo.hooks) == 0 { - node, err = miuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*MixinIDMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - miuo.mutation = mutation - node, err = miuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(miuo.hooks) - 1; i >= 0; i-- { - if miuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = miuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, miuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*MixinID) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from MixinIDMutation", v) - } - node = nv - } - return node, err + return withHooks[*MixinID, MixinIDMutation](ctx, miuo.sqlSave, miuo.mutation, miuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -281,5 +222,6 @@ func (miuo *MixinIDUpdateOne) sqlSave(ctx context.Context) (_node *MixinID, err } return nil, err } + miuo.mutation.done = true return _node, nil } diff --git a/entc/integration/customid/ent/note_create.go b/entc/integration/customid/ent/note_create.go index b54a5e407..967dfb2a5 100644 --- a/entc/integration/customid/ent/note_create.go +++ b/entc/integration/customid/ent/note_create.go @@ -96,50 +96,8 @@ func (nc *NoteCreate) Mutation() *NoteMutation { // Save creates the Note in the database. func (nc *NoteCreate) Save(ctx context.Context) (*Note, error) { - var ( - err error - node *Note - ) nc.defaults() - if len(nc.hooks) == 0 { - if err = nc.check(); err != nil { - return nil, err - } - node, err = nc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*NoteMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = nc.check(); err != nil { - return nil, err - } - nc.mutation = mutation - if node, err = nc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(nc.hooks) - 1; i >= 0; i-- { - if nc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = nc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, nc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Note) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from NoteMutation", v) - } - node = nv - } - return node, err + return withHooks[*Note, NoteMutation](ctx, nc.sqlSave, nc.mutation, nc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -183,6 +141,9 @@ func (nc *NoteCreate) check() error { } func (nc *NoteCreate) sqlSave(ctx context.Context) (*Note, error) { + if err := nc.check(); err != nil { + return nil, err + } _node, _spec := nc.createSpec() if err := sqlgraph.CreateNode(ctx, nc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -197,6 +158,8 @@ func (nc *NoteCreate) sqlSave(ctx context.Context) (*Note, error) { return nil, fmt.Errorf("unexpected Note.ID type: %T", _spec.ID.Value) } } + nc.mutation.id = &_node.ID + nc.mutation.done = true return _node, nil } diff --git a/entc/integration/customid/ent/note_delete.go b/entc/integration/customid/ent/note_delete.go index c18b73b98..6822440d7 100644 --- a/entc/integration/customid/ent/note_delete.go +++ b/entc/integration/customid/ent/note_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (nd *NoteDelete) Where(ps ...predicate.Note) *NoteDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (nd *NoteDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(nd.hooks) == 0 { - affected, err = nd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*NoteMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - nd.mutation = mutation - affected, err = nd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(nd.hooks) - 1; i >= 0; i-- { - if nd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = nd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, nd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, NoteMutation](ctx, nd.sqlExec, nd.mutation, nd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (nd *NoteDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + nd.mutation.done = true return affected, err } diff --git a/entc/integration/customid/ent/note_update.go b/entc/integration/customid/ent/note_update.go index b6bff6ef0..8b0b1b453 100644 --- a/entc/integration/customid/ent/note_update.go +++ b/entc/integration/customid/ent/note_update.go @@ -120,34 +120,7 @@ func (nu *NoteUpdate) RemoveChildren(n ...*Note) *NoteUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (nu *NoteUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(nu.hooks) == 0 { - affected, err = nu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*NoteMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - nu.mutation = mutation - affected, err = nu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(nu.hooks) - 1; i >= 0; i-- { - if nu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = nu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, nu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, NoteMutation](ctx, nu.sqlSave, nu.mutation, nu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -293,6 +266,7 @@ func (nu *NoteUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + nu.mutation.done = true return n, nil } @@ -399,40 +373,7 @@ func (nuo *NoteUpdateOne) Select(field string, fields ...string) *NoteUpdateOne // Save executes the query and returns the updated Note entity. func (nuo *NoteUpdateOne) Save(ctx context.Context) (*Note, error) { - var ( - err error - node *Note - ) - if len(nuo.hooks) == 0 { - node, err = nuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*NoteMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - nuo.mutation = mutation - node, err = nuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(nuo.hooks) - 1; i >= 0; i-- { - if nuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = nuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, nuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Note) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from NoteMutation", v) - } - node = nv - } - return node, err + return withHooks[*Note, NoteMutation](ctx, nuo.sqlSave, nuo.mutation, nuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -598,5 +539,6 @@ func (nuo *NoteUpdateOne) sqlSave(ctx context.Context) (_node *Note, err error) } return nil, err } + nuo.mutation.done = true return _node, nil } diff --git a/entc/integration/customid/ent/other_create.go b/entc/integration/customid/ent/other_create.go index 55f69544e..08c87db0c 100644 --- a/entc/integration/customid/ent/other_create.go +++ b/entc/integration/customid/ent/other_create.go @@ -48,50 +48,8 @@ func (oc *OtherCreate) Mutation() *OtherMutation { // Save creates the Other in the database. func (oc *OtherCreate) Save(ctx context.Context) (*Other, error) { - var ( - err error - node *Other - ) oc.defaults() - if len(oc.hooks) == 0 { - if err = oc.check(); err != nil { - return nil, err - } - node, err = oc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*OtherMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = oc.check(); err != nil { - return nil, err - } - oc.mutation = mutation - if node, err = oc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(oc.hooks) - 1; i >= 0; i-- { - if oc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = oc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, oc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Other) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from OtherMutation", v) - } - node = nv - } - return node, err + return withHooks[*Other, OtherMutation](ctx, oc.sqlSave, oc.mutation, oc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -130,6 +88,9 @@ func (oc *OtherCreate) check() error { } func (oc *OtherCreate) sqlSave(ctx context.Context) (*Other, error) { + if err := oc.check(); err != nil { + return nil, err + } _node, _spec := oc.createSpec() if err := sqlgraph.CreateNode(ctx, oc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -144,6 +105,8 @@ func (oc *OtherCreate) sqlSave(ctx context.Context) (*Other, error) { return nil, err } } + oc.mutation.id = &_node.ID + oc.mutation.done = true return _node, nil } diff --git a/entc/integration/customid/ent/other_delete.go b/entc/integration/customid/ent/other_delete.go index f256fe6c0..da19a9f92 100644 --- a/entc/integration/customid/ent/other_delete.go +++ b/entc/integration/customid/ent/other_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (od *OtherDelete) Where(ps ...predicate.Other) *OtherDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (od *OtherDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(od.hooks) == 0 { - affected, err = od.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*OtherMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - od.mutation = mutation - affected, err = od.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(od.hooks) - 1; i >= 0; i-- { - if od.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = od.hooks[i](mut) - } - n, err := mut.Mutate(ctx, od.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, OtherMutation](ctx, od.sqlExec, od.mutation, od.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (od *OtherDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + od.mutation.done = true return affected, err } diff --git a/entc/integration/customid/ent/other_update.go b/entc/integration/customid/ent/other_update.go index fdc0ad0b0..d2d1c8c16 100644 --- a/entc/integration/customid/ent/other_update.go +++ b/entc/integration/customid/ent/other_update.go @@ -38,34 +38,7 @@ func (ou *OtherUpdate) Mutation() *OtherMutation { // Save executes the query and returns the number of nodes affected by the update operation. func (ou *OtherUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(ou.hooks) == 0 { - affected, err = ou.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*OtherMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - ou.mutation = mutation - affected, err = ou.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(ou.hooks) - 1; i >= 0; i-- { - if ou.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ou.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, ou.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, OtherMutation](ctx, ou.sqlSave, ou.mutation, ou.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -116,6 +89,7 @@ func (ou *OtherUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + ou.mutation.done = true return n, nil } @@ -141,40 +115,7 @@ func (ouo *OtherUpdateOne) Select(field string, fields ...string) *OtherUpdateOn // Save executes the query and returns the updated Other entity. func (ouo *OtherUpdateOne) Save(ctx context.Context) (*Other, error) { - var ( - err error - node *Other - ) - if len(ouo.hooks) == 0 { - node, err = ouo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*OtherMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - ouo.mutation = mutation - node, err = ouo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(ouo.hooks) - 1; i >= 0; i-- { - if ouo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ouo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, ouo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Other) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from OtherMutation", v) - } - node = nv - } - return node, err + return withHooks[*Other, OtherMutation](ctx, ouo.sqlSave, ouo.mutation, ouo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -245,5 +186,6 @@ func (ouo *OtherUpdateOne) sqlSave(ctx context.Context) (_node *Other, err error } return nil, err } + ouo.mutation.done = true return _node, nil } diff --git a/entc/integration/customid/ent/pet_create.go b/entc/integration/customid/ent/pet_create.go index 2741c5dd6..d951f9ee4 100644 --- a/entc/integration/customid/ent/pet_create.go +++ b/entc/integration/customid/ent/pet_create.go @@ -117,50 +117,8 @@ func (pc *PetCreate) Mutation() *PetMutation { // Save creates the Pet in the database. func (pc *PetCreate) Save(ctx context.Context) (*Pet, error) { - var ( - err error - node *Pet - ) pc.defaults() - if len(pc.hooks) == 0 { - if err = pc.check(); err != nil { - return nil, err - } - node, err = pc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*PetMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = pc.check(); err != nil { - return nil, err - } - pc.mutation = mutation - if node, err = pc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(pc.hooks) - 1; i >= 0; i-- { - if pc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = pc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, pc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Pet) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from PetMutation", v) - } - node = nv - } - return node, err + return withHooks[*Pet, PetMutation](ctx, pc.sqlSave, pc.mutation, pc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -204,6 +162,9 @@ func (pc *PetCreate) check() error { } func (pc *PetCreate) sqlSave(ctx context.Context) (*Pet, error) { + if err := pc.check(); err != nil { + return nil, err + } _node, _spec := pc.createSpec() if err := sqlgraph.CreateNode(ctx, pc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -218,6 +179,8 @@ func (pc *PetCreate) sqlSave(ctx context.Context) (*Pet, error) { return nil, fmt.Errorf("unexpected Pet.ID type: %T", _spec.ID.Value) } } + pc.mutation.id = &_node.ID + pc.mutation.done = true return _node, nil } diff --git a/entc/integration/customid/ent/pet_delete.go b/entc/integration/customid/ent/pet_delete.go index 963df3adc..9b724bfa2 100644 --- a/entc/integration/customid/ent/pet_delete.go +++ b/entc/integration/customid/ent/pet_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (pd *PetDelete) Where(ps ...predicate.Pet) *PetDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (pd *PetDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(pd.hooks) == 0 { - affected, err = pd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*PetMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - pd.mutation = mutation - affected, err = pd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(pd.hooks) - 1; i >= 0; i-- { - if pd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = pd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, pd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, PetMutation](ctx, pd.sqlExec, pd.mutation, pd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (pd *PetDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + pd.mutation.done = true return affected, err } diff --git a/entc/integration/customid/ent/pet_update.go b/entc/integration/customid/ent/pet_update.go index 4bb250e06..5e53064f7 100644 --- a/entc/integration/customid/ent/pet_update.go +++ b/entc/integration/customid/ent/pet_update.go @@ -162,34 +162,7 @@ func (pu *PetUpdate) ClearBestFriend() *PetUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (pu *PetUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(pu.hooks) == 0 { - affected, err = pu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*PetMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - pu.mutation = mutation - affected, err = pu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(pu.hooks) - 1; i >= 0; i-- { - if pu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = pu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, pu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, PetMutation](ctx, pu.sqlSave, pu.mutation, pu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -418,6 +391,7 @@ func (pu *PetUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + pu.mutation.done = true return n, nil } @@ -565,40 +539,7 @@ func (puo *PetUpdateOne) Select(field string, fields ...string) *PetUpdateOne { // Save executes the query and returns the updated Pet entity. func (puo *PetUpdateOne) Save(ctx context.Context) (*Pet, error) { - var ( - err error - node *Pet - ) - if len(puo.hooks) == 0 { - node, err = puo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*PetMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - puo.mutation = mutation - node, err = puo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(puo.hooks) - 1; i >= 0; i-- { - if puo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = puo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, puo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Pet) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from PetMutation", v) - } - node = nv - } - return node, err + return withHooks[*Pet, PetMutation](ctx, puo.sqlSave, puo.mutation, puo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -847,5 +788,6 @@ func (puo *PetUpdateOne) sqlSave(ctx context.Context) (_node *Pet, err error) { } return nil, err } + puo.mutation.done = true return _node, nil } diff --git a/entc/integration/customid/ent/revision_create.go b/entc/integration/customid/ent/revision_create.go index 36b2f7272..b204a9607 100644 --- a/entc/integration/customid/ent/revision_create.go +++ b/entc/integration/customid/ent/revision_create.go @@ -39,49 +39,7 @@ func (rc *RevisionCreate) Mutation() *RevisionMutation { // Save creates the Revision in the database. func (rc *RevisionCreate) Save(ctx context.Context) (*Revision, error) { - var ( - err error - node *Revision - ) - if len(rc.hooks) == 0 { - if err = rc.check(); err != nil { - return nil, err - } - node, err = rc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*RevisionMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = rc.check(); err != nil { - return nil, err - } - rc.mutation = mutation - if node, err = rc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(rc.hooks) - 1; i >= 0; i-- { - if rc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = rc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, rc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Revision) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from RevisionMutation", v) - } - node = nv - } - return node, err + return withHooks[*Revision, RevisionMutation](ctx, rc.sqlSave, rc.mutation, rc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -112,6 +70,9 @@ func (rc *RevisionCreate) check() error { } func (rc *RevisionCreate) sqlSave(ctx context.Context) (*Revision, error) { + if err := rc.check(); err != nil { + return nil, err + } _node, _spec := rc.createSpec() if err := sqlgraph.CreateNode(ctx, rc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -126,6 +87,8 @@ func (rc *RevisionCreate) sqlSave(ctx context.Context) (*Revision, error) { return nil, fmt.Errorf("unexpected Revision.ID type: %T", _spec.ID.Value) } } + rc.mutation.id = &_node.ID + rc.mutation.done = true return _node, nil } diff --git a/entc/integration/customid/ent/revision_delete.go b/entc/integration/customid/ent/revision_delete.go index 8253cba6b..f193ebdeb 100644 --- a/entc/integration/customid/ent/revision_delete.go +++ b/entc/integration/customid/ent/revision_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (rd *RevisionDelete) Where(ps ...predicate.Revision) *RevisionDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (rd *RevisionDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(rd.hooks) == 0 { - affected, err = rd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*RevisionMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - rd.mutation = mutation - affected, err = rd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(rd.hooks) - 1; i >= 0; i-- { - if rd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = rd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, rd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, RevisionMutation](ctx, rd.sqlExec, rd.mutation, rd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (rd *RevisionDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + rd.mutation.done = true return affected, err } diff --git a/entc/integration/customid/ent/revision_update.go b/entc/integration/customid/ent/revision_update.go index 1968393a0..926ad552d 100644 --- a/entc/integration/customid/ent/revision_update.go +++ b/entc/integration/customid/ent/revision_update.go @@ -38,34 +38,7 @@ func (ru *RevisionUpdate) Mutation() *RevisionMutation { // Save executes the query and returns the number of nodes affected by the update operation. func (ru *RevisionUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(ru.hooks) == 0 { - affected, err = ru.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*RevisionMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - ru.mutation = mutation - affected, err = ru.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(ru.hooks) - 1; i >= 0; i-- { - if ru.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ru.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, ru.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, RevisionMutation](ctx, ru.sqlSave, ru.mutation, ru.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -116,6 +89,7 @@ func (ru *RevisionUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + ru.mutation.done = true return n, nil } @@ -141,40 +115,7 @@ func (ruo *RevisionUpdateOne) Select(field string, fields ...string) *RevisionUp // Save executes the query and returns the updated Revision entity. func (ruo *RevisionUpdateOne) Save(ctx context.Context) (*Revision, error) { - var ( - err error - node *Revision - ) - if len(ruo.hooks) == 0 { - node, err = ruo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*RevisionMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - ruo.mutation = mutation - node, err = ruo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(ruo.hooks) - 1; i >= 0; i-- { - if ruo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ruo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, ruo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Revision) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from RevisionMutation", v) - } - node = nv - } - return node, err + return withHooks[*Revision, RevisionMutation](ctx, ruo.sqlSave, ruo.mutation, ruo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -245,5 +186,6 @@ func (ruo *RevisionUpdateOne) sqlSave(ctx context.Context) (_node *Revision, err } return nil, err } + ruo.mutation.done = true return _node, nil } diff --git a/entc/integration/customid/ent/session_create.go b/entc/integration/customid/ent/session_create.go index f7e3d3940..a90310f12 100644 --- a/entc/integration/customid/ent/session_create.go +++ b/entc/integration/customid/ent/session_create.go @@ -68,50 +68,8 @@ func (sc *SessionCreate) Mutation() *SessionMutation { // Save creates the Session in the database. func (sc *SessionCreate) Save(ctx context.Context) (*Session, error) { - var ( - err error - node *Session - ) sc.defaults() - if len(sc.hooks) == 0 { - if err = sc.check(); err != nil { - return nil, err - } - node, err = sc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*SessionMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = sc.check(); err != nil { - return nil, err - } - sc.mutation = mutation - if node, err = sc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(sc.hooks) - 1; i >= 0; i-- { - if sc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = sc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, sc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Session) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from SessionMutation", v) - } - node = nv - } - return node, err + return withHooks[*Session, SessionMutation](ctx, sc.sqlSave, sc.mutation, sc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -155,6 +113,9 @@ func (sc *SessionCreate) check() error { } func (sc *SessionCreate) sqlSave(ctx context.Context) (*Session, error) { + if err := sc.check(); err != nil { + return nil, err + } _node, _spec := sc.createSpec() if err := sqlgraph.CreateNode(ctx, sc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -169,6 +130,8 @@ func (sc *SessionCreate) sqlSave(ctx context.Context) (*Session, error) { return nil, err } } + sc.mutation.id = &_node.ID + sc.mutation.done = true return _node, nil } diff --git a/entc/integration/customid/ent/session_delete.go b/entc/integration/customid/ent/session_delete.go index 08da9ae68..9e7167507 100644 --- a/entc/integration/customid/ent/session_delete.go +++ b/entc/integration/customid/ent/session_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (sd *SessionDelete) Where(ps ...predicate.Session) *SessionDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (sd *SessionDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(sd.hooks) == 0 { - affected, err = sd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*SessionMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - sd.mutation = mutation - affected, err = sd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(sd.hooks) - 1; i >= 0; i-- { - if sd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = sd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, sd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, SessionMutation](ctx, sd.sqlExec, sd.mutation, sd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (sd *SessionDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + sd.mutation.done = true return affected, err } diff --git a/entc/integration/customid/ent/session_update.go b/entc/integration/customid/ent/session_update.go index 741c77125..da34890df 100644 --- a/entc/integration/customid/ent/session_update.go +++ b/entc/integration/customid/ent/session_update.go @@ -65,34 +65,7 @@ func (su *SessionUpdate) ClearDevice() *SessionUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (su *SessionUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(su.hooks) == 0 { - affected, err = su.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*SessionMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - su.mutation = mutation - affected, err = su.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(su.hooks) - 1; i >= 0; i-- { - if su.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = su.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, su.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, SessionMutation](ctx, su.sqlSave, su.mutation, su.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -178,6 +151,7 @@ func (su *SessionUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + su.mutation.done = true return n, nil } @@ -228,40 +202,7 @@ func (suo *SessionUpdateOne) Select(field string, fields ...string) *SessionUpda // Save executes the query and returns the updated Session entity. func (suo *SessionUpdateOne) Save(ctx context.Context) (*Session, error) { - var ( - err error - node *Session - ) - if len(suo.hooks) == 0 { - node, err = suo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*SessionMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - suo.mutation = mutation - node, err = suo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(suo.hooks) - 1; i >= 0; i-- { - if suo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = suo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, suo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Session) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from SessionMutation", v) - } - node = nv - } - return node, err + return withHooks[*Session, SessionMutation](ctx, suo.sqlSave, suo.mutation, suo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -367,5 +308,6 @@ func (suo *SessionUpdateOne) sqlSave(ctx context.Context) (_node *Session, err e } return nil, err } + suo.mutation.done = true return _node, nil } diff --git a/entc/integration/customid/ent/token_create.go b/entc/integration/customid/ent/token_create.go index ad0293ac4..a2c7af27d 100644 --- a/entc/integration/customid/ent/token_create.go +++ b/entc/integration/customid/ent/token_create.go @@ -66,50 +66,8 @@ func (tc *TokenCreate) Mutation() *TokenMutation { // Save creates the Token in the database. func (tc *TokenCreate) Save(ctx context.Context) (*Token, error) { - var ( - err error - node *Token - ) tc.defaults() - if len(tc.hooks) == 0 { - if err = tc.check(); err != nil { - return nil, err - } - node, err = tc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*TokenMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = tc.check(); err != nil { - return nil, err - } - tc.mutation = mutation - if node, err = tc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(tc.hooks) - 1; i >= 0; i-- { - if tc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = tc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, tc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Token) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from TokenMutation", v) - } - node = nv - } - return node, err + return withHooks[*Token, TokenMutation](ctx, tc.sqlSave, tc.mutation, tc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -159,6 +117,9 @@ func (tc *TokenCreate) check() error { } func (tc *TokenCreate) sqlSave(ctx context.Context) (*Token, error) { + if err := tc.check(); err != nil { + return nil, err + } _node, _spec := tc.createSpec() if err := sqlgraph.CreateNode(ctx, tc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -173,6 +134,8 @@ func (tc *TokenCreate) sqlSave(ctx context.Context) (*Token, error) { return nil, err } } + tc.mutation.id = &_node.ID + tc.mutation.done = true return _node, nil } diff --git a/entc/integration/customid/ent/token_delete.go b/entc/integration/customid/ent/token_delete.go index 41de402a2..5a7b2aca0 100644 --- a/entc/integration/customid/ent/token_delete.go +++ b/entc/integration/customid/ent/token_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (td *TokenDelete) Where(ps ...predicate.Token) *TokenDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (td *TokenDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(td.hooks) == 0 { - affected, err = td.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*TokenMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - td.mutation = mutation - affected, err = td.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(td.hooks) - 1; i >= 0; i-- { - if td.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = td.hooks[i](mut) - } - n, err := mut.Mutate(ctx, td.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, TokenMutation](ctx, td.sqlExec, td.mutation, td.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (td *TokenDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + td.mutation.done = true return affected, err } diff --git a/entc/integration/customid/ent/token_update.go b/entc/integration/customid/ent/token_update.go index eb5699ca1..ff0153cbf 100644 --- a/entc/integration/customid/ent/token_update.go +++ b/entc/integration/customid/ent/token_update.go @@ -63,40 +63,7 @@ func (tu *TokenUpdate) ClearAccount() *TokenUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (tu *TokenUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(tu.hooks) == 0 { - if err = tu.check(); err != nil { - return 0, err - } - affected, err = tu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*TokenMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = tu.check(); err != nil { - return 0, err - } - tu.mutation = mutation - affected, err = tu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(tu.hooks) - 1; i >= 0; i-- { - if tu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = tu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, tu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, TokenMutation](ctx, tu.sqlSave, tu.mutation, tu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -135,6 +102,9 @@ func (tu *TokenUpdate) check() error { } func (tu *TokenUpdate) sqlSave(ctx context.Context) (n int, err error) { + if err := tu.check(); err != nil { + return n, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: token.Table, @@ -198,6 +168,7 @@ func (tu *TokenUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + tu.mutation.done = true return n, nil } @@ -246,46 +217,7 @@ func (tuo *TokenUpdateOne) Select(field string, fields ...string) *TokenUpdateOn // Save executes the query and returns the updated Token entity. func (tuo *TokenUpdateOne) Save(ctx context.Context) (*Token, error) { - var ( - err error - node *Token - ) - if len(tuo.hooks) == 0 { - if err = tuo.check(); err != nil { - return nil, err - } - node, err = tuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*TokenMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = tuo.check(); err != nil { - return nil, err - } - tuo.mutation = mutation - node, err = tuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(tuo.hooks) - 1; i >= 0; i-- { - if tuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = tuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, tuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Token) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from TokenMutation", v) - } - node = nv - } - return node, err + return withHooks[*Token, TokenMutation](ctx, tuo.sqlSave, tuo.mutation, tuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -324,6 +256,9 @@ func (tuo *TokenUpdateOne) check() error { } func (tuo *TokenUpdateOne) sqlSave(ctx context.Context) (_node *Token, err error) { + if err := tuo.check(); err != nil { + return _node, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: token.Table, @@ -407,5 +342,6 @@ func (tuo *TokenUpdateOne) sqlSave(ctx context.Context) (_node *Token, err error } return nil, err } + tuo.mutation.done = true return _node, nil } diff --git a/entc/integration/customid/ent/user_create.go b/entc/integration/customid/ent/user_create.go index d69e2a8b1..cae45c4a4 100644 --- a/entc/integration/customid/ent/user_create.go +++ b/entc/integration/customid/ent/user_create.go @@ -104,49 +104,7 @@ func (uc *UserCreate) Mutation() *UserMutation { // Save creates the User in the database. func (uc *UserCreate) Save(ctx context.Context) (*User, error) { - var ( - err error - node *User - ) - if len(uc.hooks) == 0 { - if err = uc.check(); err != nil { - return nil, err - } - node, err = uc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = uc.check(); err != nil { - return nil, err - } - uc.mutation = mutation - if node, err = uc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(uc.hooks) - 1; i >= 0; i-- { - if uc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*User) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) - } - node = nv - } - return node, err + return withHooks[*User, UserMutation](ctx, uc.sqlSave, uc.mutation, uc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -177,6 +135,9 @@ func (uc *UserCreate) check() error { } func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { + if err := uc.check(); err != nil { + return nil, err + } _node, _spec := uc.createSpec() if err := sqlgraph.CreateNode(ctx, uc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -188,6 +149,8 @@ func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { id := _spec.ID.Value.(int64) _node.ID = int(id) } + uc.mutation.id = &_node.ID + uc.mutation.done = true return _node, nil } diff --git a/entc/integration/customid/ent/user_delete.go b/entc/integration/customid/ent/user_delete.go index 30a2d50ac..4c37a6832 100644 --- a/entc/integration/customid/ent/user_delete.go +++ b/entc/integration/customid/ent/user_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (ud *UserDelete) Where(ps ...predicate.User) *UserDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (ud *UserDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(ud.hooks) == 0 { - affected, err = ud.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - ud.mutation = mutation - affected, err = ud.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(ud.hooks) - 1; i >= 0; i-- { - if ud.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ud.hooks[i](mut) - } - n, err := mut.Mutate(ctx, ud.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, UserMutation](ctx, ud.sqlExec, ud.mutation, ud.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (ud *UserDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + ud.mutation.done = true return affected, err } diff --git a/entc/integration/customid/ent/user_update.go b/entc/integration/customid/ent/user_update.go index 2ec1687c7..70f0a1baf 100644 --- a/entc/integration/customid/ent/user_update.go +++ b/entc/integration/customid/ent/user_update.go @@ -173,34 +173,7 @@ func (uu *UserUpdate) RemovePets(p ...*Pet) *UserUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (uu *UserUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(uu.hooks) == 0 { - affected, err = uu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - uu.mutation = mutation - affected, err = uu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(uu.hooks) - 1; i >= 0; i-- { - if uu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, uu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, UserMutation](ctx, uu.sqlSave, uu.mutation, uu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -448,6 +421,7 @@ func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + uu.mutation.done = true return n, nil } @@ -606,40 +580,7 @@ func (uuo *UserUpdateOne) Select(field string, fields ...string) *UserUpdateOne // Save executes the query and returns the updated User entity. func (uuo *UserUpdateOne) Save(ctx context.Context) (*User, error) { - var ( - err error - node *User - ) - if len(uuo.hooks) == 0 { - node, err = uuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - uuo.mutation = mutation - node, err = uuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(uuo.hooks) - 1; i >= 0; i-- { - if uuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*User) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) - } - node = nv - } - return node, err + return withHooks[*User, UserMutation](ctx, uuo.sqlSave, uuo.mutation, uuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -907,5 +848,6 @@ func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) } return nil, err } + uuo.mutation.done = true return _node, nil } diff --git a/entc/integration/edgefield/ent/car_create.go b/entc/integration/edgefield/ent/car_create.go index dd46d4678..839378e3e 100644 --- a/entc/integration/edgefield/ent/car_create.go +++ b/entc/integration/edgefield/ent/car_create.go @@ -74,50 +74,8 @@ func (cc *CarCreate) Mutation() *CarMutation { // Save creates the Car in the database. func (cc *CarCreate) Save(ctx context.Context) (*Car, error) { - var ( - err error - node *Car - ) cc.defaults() - if len(cc.hooks) == 0 { - if err = cc.check(); err != nil { - return nil, err - } - node, err = cc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CarMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = cc.check(); err != nil { - return nil, err - } - cc.mutation = mutation - if node, err = cc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(cc.hooks) - 1; i >= 0; i-- { - if cc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = cc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, cc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Car) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from CarMutation", v) - } - node = nv - } - return node, err + return withHooks[*Car, CarMutation](ctx, cc.sqlSave, cc.mutation, cc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -156,6 +114,9 @@ func (cc *CarCreate) check() error { } func (cc *CarCreate) sqlSave(ctx context.Context) (*Car, error) { + if err := cc.check(); err != nil { + return nil, err + } _node, _spec := cc.createSpec() if err := sqlgraph.CreateNode(ctx, cc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -170,6 +131,8 @@ func (cc *CarCreate) sqlSave(ctx context.Context) (*Car, error) { return nil, err } } + cc.mutation.id = &_node.ID + cc.mutation.done = true return _node, nil } diff --git a/entc/integration/edgefield/ent/car_delete.go b/entc/integration/edgefield/ent/car_delete.go index aba0ab534..a2f53d004 100644 --- a/entc/integration/edgefield/ent/car_delete.go +++ b/entc/integration/edgefield/ent/car_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (cd *CarDelete) Where(ps ...predicate.Car) *CarDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (cd *CarDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(cd.hooks) == 0 { - affected, err = cd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CarMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - cd.mutation = mutation - affected, err = cd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(cd.hooks) - 1; i >= 0; i-- { - if cd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = cd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, cd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, CarMutation](ctx, cd.sqlExec, cd.mutation, cd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (cd *CarDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + cd.mutation.done = true return affected, err } diff --git a/entc/integration/edgefield/ent/car_update.go b/entc/integration/edgefield/ent/car_update.go index 33e1c37ea..302d71542 100644 --- a/entc/integration/edgefield/ent/car_update.go +++ b/entc/integration/edgefield/ent/car_update.go @@ -95,34 +95,7 @@ func (cu *CarUpdate) RemoveRentals(r ...*Rental) *CarUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (cu *CarUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(cu.hooks) == 0 { - affected, err = cu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CarMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - cu.mutation = mutation - affected, err = cu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(cu.hooks) - 1; i >= 0; i-- { - if cu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = cu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, cu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, CarMutation](ctx, cu.sqlSave, cu.mutation, cu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -233,6 +206,7 @@ func (cu *CarUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + cu.mutation.done = true return n, nil } @@ -314,40 +288,7 @@ func (cuo *CarUpdateOne) Select(field string, fields ...string) *CarUpdateOne { // Save executes the query and returns the updated Car entity. func (cuo *CarUpdateOne) Save(ctx context.Context) (*Car, error) { - var ( - err error - node *Car - ) - if len(cuo.hooks) == 0 { - node, err = cuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CarMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - cuo.mutation = mutation - node, err = cuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(cuo.hooks) - 1; i >= 0; i-- { - if cuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = cuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, cuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Car) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from CarMutation", v) - } - node = nv - } - return node, err + return withHooks[*Car, CarMutation](ctx, cuo.sqlSave, cuo.mutation, cuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -478,5 +419,6 @@ func (cuo *CarUpdateOne) sqlSave(ctx context.Context) (_node *Car, err error) { } return nil, err } + cuo.mutation.done = true return _node, nil } diff --git a/entc/integration/edgefield/ent/card_create.go b/entc/integration/edgefield/ent/card_create.go index 9b45a4801..1b2648dce 100644 --- a/entc/integration/edgefield/ent/card_create.go +++ b/entc/integration/edgefield/ent/card_create.go @@ -63,49 +63,7 @@ func (cc *CardCreate) Mutation() *CardMutation { // Save creates the Card in the database. func (cc *CardCreate) Save(ctx context.Context) (*Card, error) { - var ( - err error - node *Card - ) - if len(cc.hooks) == 0 { - if err = cc.check(); err != nil { - return nil, err - } - node, err = cc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CardMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = cc.check(); err != nil { - return nil, err - } - cc.mutation = mutation - if node, err = cc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(cc.hooks) - 1; i >= 0; i-- { - if cc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = cc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, cc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Card) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from CardMutation", v) - } - node = nv - } - return node, err + return withHooks[*Card, CardMutation](ctx, cc.sqlSave, cc.mutation, cc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -136,6 +94,9 @@ func (cc *CardCreate) check() error { } func (cc *CardCreate) sqlSave(ctx context.Context) (*Card, error) { + if err := cc.check(); err != nil { + return nil, err + } _node, _spec := cc.createSpec() if err := sqlgraph.CreateNode(ctx, cc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -145,6 +106,8 @@ func (cc *CardCreate) sqlSave(ctx context.Context) (*Card, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + cc.mutation.id = &_node.ID + cc.mutation.done = true return _node, nil } diff --git a/entc/integration/edgefield/ent/card_delete.go b/entc/integration/edgefield/ent/card_delete.go index f251ba375..d17989b73 100644 --- a/entc/integration/edgefield/ent/card_delete.go +++ b/entc/integration/edgefield/ent/card_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (cd *CardDelete) Where(ps ...predicate.Card) *CardDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (cd *CardDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(cd.hooks) == 0 { - affected, err = cd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CardMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - cd.mutation = mutation - affected, err = cd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(cd.hooks) - 1; i >= 0; i-- { - if cd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = cd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, cd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, CardMutation](ctx, cd.sqlExec, cd.mutation, cd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (cd *CardDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + cd.mutation.done = true return affected, err } diff --git a/entc/integration/edgefield/ent/card_update.go b/entc/integration/edgefield/ent/card_update.go index 6b0c27ff2..e79e0d2bf 100644 --- a/entc/integration/edgefield/ent/card_update.go +++ b/entc/integration/edgefield/ent/card_update.go @@ -90,34 +90,7 @@ func (cu *CardUpdate) ClearOwner() *CardUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (cu *CardUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(cu.hooks) == 0 { - affected, err = cu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CardMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - cu.mutation = mutation - affected, err = cu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(cu.hooks) - 1; i >= 0; i-- { - if cu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = cu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, cu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, CardMutation](ctx, cu.sqlSave, cu.mutation, cu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -209,6 +182,7 @@ func (cu *CardUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + cu.mutation.done = true return n, nil } @@ -285,40 +259,7 @@ func (cuo *CardUpdateOne) Select(field string, fields ...string) *CardUpdateOne // Save executes the query and returns the updated Card entity. func (cuo *CardUpdateOne) Save(ctx context.Context) (*Card, error) { - var ( - err error - node *Card - ) - if len(cuo.hooks) == 0 { - node, err = cuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CardMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - cuo.mutation = mutation - node, err = cuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(cuo.hooks) - 1; i >= 0; i-- { - if cuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = cuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, cuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Card) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from CardMutation", v) - } - node = nv - } - return node, err + return withHooks[*Card, CardMutation](ctx, cuo.sqlSave, cuo.mutation, cuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -430,5 +371,6 @@ func (cuo *CardUpdateOne) sqlSave(ctx context.Context) (_node *Card, err error) } return nil, err } + cuo.mutation.done = true return _node, nil } diff --git a/entc/integration/edgefield/ent/ent.go b/entc/integration/edgefield/ent/ent.go index b1d3cf154..2dd9faee9 100644 --- a/entc/integration/edgefield/ent/ent.go +++ b/entc/integration/edgefield/ent/ent.go @@ -489,6 +489,40 @@ func (s *selector) BoolX(ctx context.Context) bool { return v } +// withHooks invokes the builder operation with the given hooks, if any. +func withHooks[V Value, M any, PM interface { + *M + Mutation +}](ctx context.Context, exec func(context.Context) (V, error), mutation PM, hooks []Hook) (value V, err error) { + if len(hooks) == 0 { + return exec(ctx) + } + var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { + mutationT, ok := m.(PM) + if !ok { + return nil, fmt.Errorf("unexpected mutation type %T", m) + } + // Set the mutation to the builder. + *mutation = *mutationT + return exec(ctx) + }) + for i := len(hooks) - 1; i >= 0; i-- { + if hooks[i] == nil { + return value, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") + } + mut = hooks[i](mut) + } + v, err := mut.Mutate(ctx, mutation) + if err != nil { + return value, err + } + nv, ok := v.(V) + if !ok { + return value, fmt.Errorf("unexpected node type %T returned from %T", v, mutation) + } + return nv, nil +} + // newQueryContext returns a new context with the given QueryContext attached in case it does not exist. func newQueryContext(ctx context.Context, typ, op string) context.Context { if ent.QueryFromContext(ctx) == nil { diff --git a/entc/integration/edgefield/ent/info_create.go b/entc/integration/edgefield/ent/info_create.go index 39cfd905b..422666eb1 100644 --- a/entc/integration/edgefield/ent/info_create.go +++ b/entc/integration/edgefield/ent/info_create.go @@ -63,49 +63,7 @@ func (ic *InfoCreate) Mutation() *InfoMutation { // Save creates the Info in the database. func (ic *InfoCreate) Save(ctx context.Context) (*Info, error) { - var ( - err error - node *Info - ) - if len(ic.hooks) == 0 { - if err = ic.check(); err != nil { - return nil, err - } - node, err = ic.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*InfoMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = ic.check(); err != nil { - return nil, err - } - ic.mutation = mutation - if node, err = ic.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(ic.hooks) - 1; i >= 0; i-- { - if ic.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ic.hooks[i](mut) - } - v, err := mut.Mutate(ctx, ic.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Info) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from InfoMutation", v) - } - node = nv - } - return node, err + return withHooks[*Info, InfoMutation](ctx, ic.sqlSave, ic.mutation, ic.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -139,6 +97,9 @@ func (ic *InfoCreate) check() error { } func (ic *InfoCreate) sqlSave(ctx context.Context) (*Info, error) { + if err := ic.check(); err != nil { + return nil, err + } _node, _spec := ic.createSpec() if err := sqlgraph.CreateNode(ctx, ic.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -150,6 +111,8 @@ func (ic *InfoCreate) sqlSave(ctx context.Context) (*Info, error) { id := _spec.ID.Value.(int64) _node.ID = int(id) } + ic.mutation.id = &_node.ID + ic.mutation.done = true return _node, nil } diff --git a/entc/integration/edgefield/ent/info_delete.go b/entc/integration/edgefield/ent/info_delete.go index 53750d9b1..36a9d148b 100644 --- a/entc/integration/edgefield/ent/info_delete.go +++ b/entc/integration/edgefield/ent/info_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (id *InfoDelete) Where(ps ...predicate.Info) *InfoDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (id *InfoDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(id.hooks) == 0 { - affected, err = id.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*InfoMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - id.mutation = mutation - affected, err = id.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(id.hooks) - 1; i >= 0; i-- { - if id.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = id.hooks[i](mut) - } - n, err := mut.Mutate(ctx, id.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, InfoMutation](ctx, id.sqlExec, id.mutation, id.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (id *InfoDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + id.mutation.done = true return affected, err } diff --git a/entc/integration/edgefield/ent/info_update.go b/entc/integration/edgefield/ent/info_update.go index 4cb455231..fdf4816e1 100644 --- a/entc/integration/edgefield/ent/info_update.go +++ b/entc/integration/edgefield/ent/info_update.go @@ -78,34 +78,7 @@ func (iu *InfoUpdate) ClearUser() *InfoUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (iu *InfoUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(iu.hooks) == 0 { - affected, err = iu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*InfoMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - iu.mutation = mutation - affected, err = iu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(iu.hooks) - 1; i >= 0; i-- { - if iu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = iu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, iu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, InfoMutation](ctx, iu.sqlSave, iu.mutation, iu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -199,6 +172,7 @@ func (iu *InfoUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + iu.mutation.done = true return n, nil } @@ -261,40 +235,7 @@ func (iuo *InfoUpdateOne) Select(field string, fields ...string) *InfoUpdateOne // Save executes the query and returns the updated Info entity. func (iuo *InfoUpdateOne) Save(ctx context.Context) (*Info, error) { - var ( - err error - node *Info - ) - if len(iuo.hooks) == 0 { - node, err = iuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*InfoMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - iuo.mutation = mutation - node, err = iuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(iuo.hooks) - 1; i >= 0; i-- { - if iuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = iuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, iuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Info) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from InfoMutation", v) - } - node = nv - } - return node, err + return withHooks[*Info, InfoMutation](ctx, iuo.sqlSave, iuo.mutation, iuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -408,5 +349,6 @@ func (iuo *InfoUpdateOne) sqlSave(ctx context.Context) (_node *Info, err error) } return nil, err } + iuo.mutation.done = true return _node, nil } diff --git a/entc/integration/edgefield/ent/metadata_create.go b/entc/integration/edgefield/ent/metadata_create.go index dbbebc861..ffd0e655e 100644 --- a/entc/integration/edgefield/ent/metadata_create.go +++ b/entc/integration/edgefield/ent/metadata_create.go @@ -104,50 +104,8 @@ func (mc *MetadataCreate) Mutation() *MetadataMutation { // Save creates the Metadata in the database. func (mc *MetadataCreate) Save(ctx context.Context) (*Metadata, error) { - var ( - err error - node *Metadata - ) mc.defaults() - if len(mc.hooks) == 0 { - if err = mc.check(); err != nil { - return nil, err - } - node, err = mc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*MetadataMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = mc.check(); err != nil { - return nil, err - } - mc.mutation = mutation - if node, err = mc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(mc.hooks) - 1; i >= 0; i-- { - if mc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = mc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, mc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Metadata) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from MetadataMutation", v) - } - node = nv - } - return node, err + return withHooks[*Metadata, MetadataMutation](ctx, mc.sqlSave, mc.mutation, mc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -189,6 +147,9 @@ func (mc *MetadataCreate) check() error { } func (mc *MetadataCreate) sqlSave(ctx context.Context) (*Metadata, error) { + if err := mc.check(); err != nil { + return nil, err + } _node, _spec := mc.createSpec() if err := sqlgraph.CreateNode(ctx, mc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -200,6 +161,8 @@ func (mc *MetadataCreate) sqlSave(ctx context.Context) (*Metadata, error) { id := _spec.ID.Value.(int64) _node.ID = int(id) } + mc.mutation.id = &_node.ID + mc.mutation.done = true return _node, nil } diff --git a/entc/integration/edgefield/ent/metadata_delete.go b/entc/integration/edgefield/ent/metadata_delete.go index 703fe5ac8..f240db03d 100644 --- a/entc/integration/edgefield/ent/metadata_delete.go +++ b/entc/integration/edgefield/ent/metadata_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (md *MetadataDelete) Where(ps ...predicate.Metadata) *MetadataDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (md *MetadataDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(md.hooks) == 0 { - affected, err = md.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*MetadataMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - md.mutation = mutation - affected, err = md.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(md.hooks) - 1; i >= 0; i-- { - if md.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = md.hooks[i](mut) - } - n, err := mut.Mutate(ctx, md.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, MetadataMutation](ctx, md.sqlExec, md.mutation, md.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (md *MetadataDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + md.mutation.done = true return affected, err } diff --git a/entc/integration/edgefield/ent/metadata_update.go b/entc/integration/edgefield/ent/metadata_update.go index 913a3628c..9f1440b3a 100644 --- a/entc/integration/edgefield/ent/metadata_update.go +++ b/entc/integration/edgefield/ent/metadata_update.go @@ -152,34 +152,7 @@ func (mu *MetadataUpdate) ClearParent() *MetadataUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (mu *MetadataUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(mu.hooks) == 0 { - affected, err = mu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*MetadataMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - mu.mutation = mutation - affected, err = mu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(mu.hooks) - 1; i >= 0; i-- { - if mu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = mu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, mu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, MetadataMutation](ctx, mu.sqlSave, mu.mutation, mu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -360,6 +333,7 @@ func (mu *MetadataUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + mu.mutation.done = true return n, nil } @@ -498,40 +472,7 @@ func (muo *MetadataUpdateOne) Select(field string, fields ...string) *MetadataUp // Save executes the query and returns the updated Metadata entity. func (muo *MetadataUpdateOne) Save(ctx context.Context) (*Metadata, error) { - var ( - err error - node *Metadata - ) - if len(muo.hooks) == 0 { - node, err = muo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*MetadataMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - muo.mutation = mutation - node, err = muo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(muo.hooks) - 1; i >= 0; i-- { - if muo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = muo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, muo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Metadata) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from MetadataMutation", v) - } - node = nv - } - return node, err + return withHooks[*Metadata, MetadataMutation](ctx, muo.sqlSave, muo.mutation, muo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -732,5 +673,6 @@ func (muo *MetadataUpdateOne) sqlSave(ctx context.Context) (_node *Metadata, err } return nil, err } + muo.mutation.done = true return _node, nil } diff --git a/entc/integration/edgefield/ent/node_create.go b/entc/integration/edgefield/ent/node_create.go index b93ddca8a..51592329a 100644 --- a/entc/integration/edgefield/ent/node_create.go +++ b/entc/integration/edgefield/ent/node_create.go @@ -82,50 +82,8 @@ func (nc *NodeCreate) Mutation() *NodeMutation { // Save creates the Node in the database. func (nc *NodeCreate) Save(ctx context.Context) (*Node, error) { - var ( - err error - node *Node - ) nc.defaults() - if len(nc.hooks) == 0 { - if err = nc.check(); err != nil { - return nil, err - } - node, err = nc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*NodeMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = nc.check(); err != nil { - return nil, err - } - nc.mutation = mutation - if node, err = nc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(nc.hooks) - 1; i >= 0; i-- { - if nc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = nc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, nc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Node) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from NodeMutation", v) - } - node = nv - } - return node, err + return withHooks[*Node, NodeMutation](ctx, nc.sqlSave, nc.mutation, nc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -167,6 +125,9 @@ func (nc *NodeCreate) check() error { } func (nc *NodeCreate) sqlSave(ctx context.Context) (*Node, error) { + if err := nc.check(); err != nil { + return nil, err + } _node, _spec := nc.createSpec() if err := sqlgraph.CreateNode(ctx, nc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -176,6 +137,8 @@ func (nc *NodeCreate) sqlSave(ctx context.Context) (*Node, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + nc.mutation.id = &_node.ID + nc.mutation.done = true return _node, nil } diff --git a/entc/integration/edgefield/ent/node_delete.go b/entc/integration/edgefield/ent/node_delete.go index 1b2c86865..154fdfb4f 100644 --- a/entc/integration/edgefield/ent/node_delete.go +++ b/entc/integration/edgefield/ent/node_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (nd *NodeDelete) Where(ps ...predicate.Node) *NodeDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (nd *NodeDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(nd.hooks) == 0 { - affected, err = nd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*NodeMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - nd.mutation = mutation - affected, err = nd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(nd.hooks) - 1; i >= 0; i-- { - if nd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = nd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, nd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, NodeMutation](ctx, nd.sqlExec, nd.mutation, nd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (nd *NodeDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + nd.mutation.done = true return affected, err } diff --git a/entc/integration/edgefield/ent/node_update.go b/entc/integration/edgefield/ent/node_update.go index 66942ca14..a0562a0b8 100644 --- a/entc/integration/edgefield/ent/node_update.go +++ b/entc/integration/edgefield/ent/node_update.go @@ -115,34 +115,7 @@ func (nu *NodeUpdate) ClearNext() *NodeUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (nu *NodeUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(nu.hooks) == 0 { - affected, err = nu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*NodeMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - nu.mutation = mutation - affected, err = nu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(nu.hooks) - 1; i >= 0; i-- { - if nu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = nu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, nu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, NodeMutation](ctx, nu.sqlSave, nu.mutation, nu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -269,6 +242,7 @@ func (nu *NodeUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + nu.mutation.done = true return n, nil } @@ -371,40 +345,7 @@ func (nuo *NodeUpdateOne) Select(field string, fields ...string) *NodeUpdateOne // Save executes the query and returns the updated Node entity. func (nuo *NodeUpdateOne) Save(ctx context.Context) (*Node, error) { - var ( - err error - node *Node - ) - if len(nuo.hooks) == 0 { - node, err = nuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*NodeMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - nuo.mutation = mutation - node, err = nuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(nuo.hooks) - 1; i >= 0; i-- { - if nuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = nuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, nuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Node) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from NodeMutation", v) - } - node = nv - } - return node, err + return withHooks[*Node, NodeMutation](ctx, nuo.sqlSave, nuo.mutation, nuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -551,5 +492,6 @@ func (nuo *NodeUpdateOne) sqlSave(ctx context.Context) (_node *Node, err error) } return nil, err } + nuo.mutation.done = true return _node, nil } diff --git a/entc/integration/edgefield/ent/pet_create.go b/entc/integration/edgefield/ent/pet_create.go index 246bf5aa1..4849312a4 100644 --- a/entc/integration/edgefield/ent/pet_create.go +++ b/entc/integration/edgefield/ent/pet_create.go @@ -49,49 +49,7 @@ func (pc *PetCreate) Mutation() *PetMutation { // Save creates the Pet in the database. func (pc *PetCreate) Save(ctx context.Context) (*Pet, error) { - var ( - err error - node *Pet - ) - if len(pc.hooks) == 0 { - if err = pc.check(); err != nil { - return nil, err - } - node, err = pc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*PetMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = pc.check(); err != nil { - return nil, err - } - pc.mutation = mutation - if node, err = pc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(pc.hooks) - 1; i >= 0; i-- { - if pc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = pc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, pc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Pet) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from PetMutation", v) - } - node = nv - } - return node, err + return withHooks[*Pet, PetMutation](ctx, pc.sqlSave, pc.mutation, pc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -122,6 +80,9 @@ func (pc *PetCreate) check() error { } func (pc *PetCreate) sqlSave(ctx context.Context) (*Pet, error) { + if err := pc.check(); err != nil { + return nil, err + } _node, _spec := pc.createSpec() if err := sqlgraph.CreateNode(ctx, pc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -131,6 +92,8 @@ func (pc *PetCreate) sqlSave(ctx context.Context) (*Pet, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + pc.mutation.id = &_node.ID + pc.mutation.done = true return _node, nil } diff --git a/entc/integration/edgefield/ent/pet_delete.go b/entc/integration/edgefield/ent/pet_delete.go index 705c120fb..d244112c7 100644 --- a/entc/integration/edgefield/ent/pet_delete.go +++ b/entc/integration/edgefield/ent/pet_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (pd *PetDelete) Where(ps ...predicate.Pet) *PetDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (pd *PetDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(pd.hooks) == 0 { - affected, err = pd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*PetMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - pd.mutation = mutation - affected, err = pd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(pd.hooks) - 1; i >= 0; i-- { - if pd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = pd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, pd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, PetMutation](ctx, pd.sqlExec, pd.mutation, pd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (pd *PetDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + pd.mutation.done = true return affected, err } diff --git a/entc/integration/edgefield/ent/pet_update.go b/entc/integration/edgefield/ent/pet_update.go index d04ad1161..332e96c27 100644 --- a/entc/integration/edgefield/ent/pet_update.go +++ b/entc/integration/edgefield/ent/pet_update.go @@ -70,34 +70,7 @@ func (pu *PetUpdate) ClearOwner() *PetUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (pu *PetUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(pu.hooks) == 0 { - affected, err = pu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*PetMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - pu.mutation = mutation - affected, err = pu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(pu.hooks) - 1; i >= 0; i-- { - if pu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = pu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, pu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, PetMutation](ctx, pu.sqlSave, pu.mutation, pu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -183,6 +156,7 @@ func (pu *PetUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + pu.mutation.done = true return n, nil } @@ -239,40 +213,7 @@ func (puo *PetUpdateOne) Select(field string, fields ...string) *PetUpdateOne { // Save executes the query and returns the updated Pet entity. func (puo *PetUpdateOne) Save(ctx context.Context) (*Pet, error) { - var ( - err error - node *Pet - ) - if len(puo.hooks) == 0 { - node, err = puo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*PetMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - puo.mutation = mutation - node, err = puo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(puo.hooks) - 1; i >= 0; i-- { - if puo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = puo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, puo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Pet) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from PetMutation", v) - } - node = nv - } - return node, err + return withHooks[*Pet, PetMutation](ctx, puo.sqlSave, puo.mutation, puo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -378,5 +319,6 @@ func (puo *PetUpdateOne) sqlSave(ctx context.Context) (_node *Pet, err error) { } return nil, err } + puo.mutation.done = true return _node, nil } diff --git a/entc/integration/edgefield/ent/post_create.go b/entc/integration/edgefield/ent/post_create.go index 713fb5529..d857a495d 100644 --- a/entc/integration/edgefield/ent/post_create.go +++ b/entc/integration/edgefield/ent/post_create.go @@ -56,49 +56,7 @@ func (pc *PostCreate) Mutation() *PostMutation { // Save creates the Post in the database. func (pc *PostCreate) Save(ctx context.Context) (*Post, error) { - var ( - err error - node *Post - ) - if len(pc.hooks) == 0 { - if err = pc.check(); err != nil { - return nil, err - } - node, err = pc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*PostMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = pc.check(); err != nil { - return nil, err - } - pc.mutation = mutation - if node, err = pc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(pc.hooks) - 1; i >= 0; i-- { - if pc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = pc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, pc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Post) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from PostMutation", v) - } - node = nv - } - return node, err + return withHooks[*Post, PostMutation](ctx, pc.sqlSave, pc.mutation, pc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -132,6 +90,9 @@ func (pc *PostCreate) check() error { } func (pc *PostCreate) sqlSave(ctx context.Context) (*Post, error) { + if err := pc.check(); err != nil { + return nil, err + } _node, _spec := pc.createSpec() if err := sqlgraph.CreateNode(ctx, pc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -141,6 +102,8 @@ func (pc *PostCreate) sqlSave(ctx context.Context) (*Post, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + pc.mutation.id = &_node.ID + pc.mutation.done = true return _node, nil } diff --git a/entc/integration/edgefield/ent/post_delete.go b/entc/integration/edgefield/ent/post_delete.go index 30e3bcba6..a72616a36 100644 --- a/entc/integration/edgefield/ent/post_delete.go +++ b/entc/integration/edgefield/ent/post_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (pd *PostDelete) Where(ps ...predicate.Post) *PostDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (pd *PostDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(pd.hooks) == 0 { - affected, err = pd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*PostMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - pd.mutation = mutation - affected, err = pd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(pd.hooks) - 1; i >= 0; i-- { - if pd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = pd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, pd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, PostMutation](ctx, pd.sqlExec, pd.mutation, pd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (pd *PostDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + pd.mutation.done = true return affected, err } diff --git a/entc/integration/edgefield/ent/post_update.go b/entc/integration/edgefield/ent/post_update.go index 5793cda9b..73bb97b85 100644 --- a/entc/integration/edgefield/ent/post_update.go +++ b/entc/integration/edgefield/ent/post_update.go @@ -76,34 +76,7 @@ func (pu *PostUpdate) ClearAuthor() *PostUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (pu *PostUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(pu.hooks) == 0 { - affected, err = pu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*PostMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - pu.mutation = mutation - affected, err = pu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(pu.hooks) - 1; i >= 0; i-- { - if pu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = pu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, pu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, PostMutation](ctx, pu.sqlSave, pu.mutation, pu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -192,6 +165,7 @@ func (pu *PostUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + pu.mutation.done = true return n, nil } @@ -254,40 +228,7 @@ func (puo *PostUpdateOne) Select(field string, fields ...string) *PostUpdateOne // Save executes the query and returns the updated Post entity. func (puo *PostUpdateOne) Save(ctx context.Context) (*Post, error) { - var ( - err error - node *Post - ) - if len(puo.hooks) == 0 { - node, err = puo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*PostMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - puo.mutation = mutation - node, err = puo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(puo.hooks) - 1; i >= 0; i-- { - if puo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = puo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, puo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Post) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from PostMutation", v) - } - node = nv - } - return node, err + return withHooks[*Post, PostMutation](ctx, puo.sqlSave, puo.mutation, puo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -396,5 +337,6 @@ func (puo *PostUpdateOne) sqlSave(ctx context.Context) (_node *Post, err error) } return nil, err } + puo.mutation.done = true return _node, nil } diff --git a/entc/integration/edgefield/ent/rental_create.go b/entc/integration/edgefield/ent/rental_create.go index 39abe62da..2fe612d31 100644 --- a/entc/integration/edgefield/ent/rental_create.go +++ b/entc/integration/edgefield/ent/rental_create.go @@ -70,50 +70,8 @@ func (rc *RentalCreate) Mutation() *RentalMutation { // Save creates the Rental in the database. func (rc *RentalCreate) Save(ctx context.Context) (*Rental, error) { - var ( - err error - node *Rental - ) rc.defaults() - if len(rc.hooks) == 0 { - if err = rc.check(); err != nil { - return nil, err - } - node, err = rc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*RentalMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = rc.check(); err != nil { - return nil, err - } - rc.mutation = mutation - if node, err = rc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(rc.hooks) - 1; i >= 0; i-- { - if rc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = rc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, rc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Rental) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from RentalMutation", v) - } - node = nv - } - return node, err + return withHooks[*Rental, RentalMutation](ctx, rc.sqlSave, rc.mutation, rc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -167,6 +125,9 @@ func (rc *RentalCreate) check() error { } func (rc *RentalCreate) sqlSave(ctx context.Context) (*Rental, error) { + if err := rc.check(); err != nil { + return nil, err + } _node, _spec := rc.createSpec() if err := sqlgraph.CreateNode(ctx, rc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -176,6 +137,8 @@ func (rc *RentalCreate) sqlSave(ctx context.Context) (*Rental, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + rc.mutation.id = &_node.ID + rc.mutation.done = true return _node, nil } diff --git a/entc/integration/edgefield/ent/rental_delete.go b/entc/integration/edgefield/ent/rental_delete.go index ed03039c3..6d5e0ea6f 100644 --- a/entc/integration/edgefield/ent/rental_delete.go +++ b/entc/integration/edgefield/ent/rental_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (rd *RentalDelete) Where(ps ...predicate.Rental) *RentalDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (rd *RentalDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(rd.hooks) == 0 { - affected, err = rd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*RentalMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - rd.mutation = mutation - affected, err = rd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(rd.hooks) - 1; i >= 0; i-- { - if rd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = rd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, rd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, RentalMutation](ctx, rd.sqlExec, rd.mutation, rd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (rd *RentalDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + rd.mutation.done = true return affected, err } diff --git a/entc/integration/edgefield/ent/rental_update.go b/entc/integration/edgefield/ent/rental_update.go index 8220b91ac..0d95cd526 100644 --- a/entc/integration/edgefield/ent/rental_update.go +++ b/entc/integration/edgefield/ent/rental_update.go @@ -53,40 +53,7 @@ func (ru *RentalUpdate) Mutation() *RentalMutation { // Save executes the query and returns the number of nodes affected by the update operation. func (ru *RentalUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(ru.hooks) == 0 { - if err = ru.check(); err != nil { - return 0, err - } - affected, err = ru.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*RentalMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = ru.check(); err != nil { - return 0, err - } - ru.mutation = mutation - affected, err = ru.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(ru.hooks) - 1; i >= 0; i-- { - if ru.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ru.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, ru.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, RentalMutation](ctx, ru.sqlSave, ru.mutation, ru.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -123,6 +90,9 @@ func (ru *RentalUpdate) check() error { } func (ru *RentalUpdate) sqlSave(ctx context.Context) (n int, err error) { + if err := ru.check(); err != nil { + return n, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: rental.Table, @@ -151,6 +121,7 @@ func (ru *RentalUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + ru.mutation.done = true return n, nil } @@ -190,46 +161,7 @@ func (ruo *RentalUpdateOne) Select(field string, fields ...string) *RentalUpdate // Save executes the query and returns the updated Rental entity. func (ruo *RentalUpdateOne) Save(ctx context.Context) (*Rental, error) { - var ( - err error - node *Rental - ) - if len(ruo.hooks) == 0 { - if err = ruo.check(); err != nil { - return nil, err - } - node, err = ruo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*RentalMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = ruo.check(); err != nil { - return nil, err - } - ruo.mutation = mutation - node, err = ruo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(ruo.hooks) - 1; i >= 0; i-- { - if ruo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ruo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, ruo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Rental) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from RentalMutation", v) - } - node = nv - } - return node, err + return withHooks[*Rental, RentalMutation](ctx, ruo.sqlSave, ruo.mutation, ruo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -266,6 +198,9 @@ func (ruo *RentalUpdateOne) check() error { } func (ruo *RentalUpdateOne) sqlSave(ctx context.Context) (_node *Rental, err error) { + if err := ruo.check(); err != nil { + return _node, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: rental.Table, @@ -314,5 +249,6 @@ func (ruo *RentalUpdateOne) sqlSave(ctx context.Context) (_node *Rental, err err } return nil, err } + ruo.mutation.done = true return _node, nil } diff --git a/entc/integration/edgefield/ent/user_create.go b/entc/integration/edgefield/ent/user_create.go index a0db19dbd..725c9b16f 100644 --- a/entc/integration/edgefield/ent/user_create.go +++ b/entc/integration/edgefield/ent/user_create.go @@ -176,49 +176,7 @@ func (uc *UserCreate) Mutation() *UserMutation { // Save creates the User in the database. func (uc *UserCreate) Save(ctx context.Context) (*User, error) { - var ( - err error - node *User - ) - if len(uc.hooks) == 0 { - if err = uc.check(); err != nil { - return nil, err - } - node, err = uc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = uc.check(); err != nil { - return nil, err - } - uc.mutation = mutation - if node, err = uc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(uc.hooks) - 1; i >= 0; i-- { - if uc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*User) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) - } - node = nv - } - return node, err + return withHooks[*User, UserMutation](ctx, uc.sqlSave, uc.mutation, uc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -249,6 +207,9 @@ func (uc *UserCreate) check() error { } func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { + if err := uc.check(); err != nil { + return nil, err + } _node, _spec := uc.createSpec() if err := sqlgraph.CreateNode(ctx, uc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -260,6 +221,8 @@ func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { id := _spec.ID.Value.(int64) _node.ID = int(id) } + uc.mutation.id = &_node.ID + uc.mutation.done = true return _node, nil } diff --git a/entc/integration/edgefield/ent/user_delete.go b/entc/integration/edgefield/ent/user_delete.go index eaadd4a0c..3a5ed5ae7 100644 --- a/entc/integration/edgefield/ent/user_delete.go +++ b/entc/integration/edgefield/ent/user_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (ud *UserDelete) Where(ps ...predicate.User) *UserDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (ud *UserDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(ud.hooks) == 0 { - affected, err = ud.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - ud.mutation = mutation - affected, err = ud.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(ud.hooks) - 1; i >= 0; i-- { - if ud.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ud.hooks[i](mut) - } - n, err := mut.Mutate(ctx, ud.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, UserMutation](ctx, ud.sqlExec, ud.mutation, ud.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (ud *UserDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + ud.mutation.done = true return affected, err } diff --git a/entc/integration/edgefield/ent/user_update.go b/entc/integration/edgefield/ent/user_update.go index 0fa4f8de5..47a02e7ee 100644 --- a/entc/integration/edgefield/ent/user_update.go +++ b/entc/integration/edgefield/ent/user_update.go @@ -268,34 +268,7 @@ func (uu *UserUpdate) RemoveRentals(r ...*Rental) *UserUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (uu *UserUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(uu.hooks) == 0 { - affected, err = uu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - uu.mutation = mutation - affected, err = uu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(uu.hooks) - 1; i >= 0; i-- { - if uu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, uu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, UserMutation](ctx, uu.sqlSave, uu.mutation, uu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -667,6 +640,7 @@ func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + uu.mutation.done = true return n, nil } @@ -917,40 +891,7 @@ func (uuo *UserUpdateOne) Select(field string, fields ...string) *UserUpdateOne // Save executes the query and returns the updated User entity. func (uuo *UserUpdateOne) Save(ctx context.Context) (*User, error) { - var ( - err error - node *User - ) - if len(uuo.hooks) == 0 { - node, err = uuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - uuo.mutation = mutation - node, err = uuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(uuo.hooks) - 1; i >= 0; i-- { - if uuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*User) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) - } - node = nv - } - return node, err + return withHooks[*User, UserMutation](ctx, uuo.sqlSave, uuo.mutation, uuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -1342,5 +1283,6 @@ func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) } return nil, err } + uuo.mutation.done = true return _node, nil } diff --git a/entc/integration/edgeschema/ent/ent.go b/entc/integration/edgeschema/ent/ent.go index 1d4d99874..73d49bcdc 100644 --- a/entc/integration/edgeschema/ent/ent.go +++ b/entc/integration/edgeschema/ent/ent.go @@ -499,6 +499,40 @@ func (s *selector) BoolX(ctx context.Context) bool { return v } +// withHooks invokes the builder operation with the given hooks, if any. +func withHooks[V Value, M any, PM interface { + *M + Mutation +}](ctx context.Context, exec func(context.Context) (V, error), mutation PM, hooks []Hook) (value V, err error) { + if len(hooks) == 0 { + return exec(ctx) + } + var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { + mutationT, ok := m.(PM) + if !ok { + return nil, fmt.Errorf("unexpected mutation type %T", m) + } + // Set the mutation to the builder. + *mutation = *mutationT + return exec(ctx) + }) + for i := len(hooks) - 1; i >= 0; i-- { + if hooks[i] == nil { + return value, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") + } + mut = hooks[i](mut) + } + v, err := mut.Mutate(ctx, mutation) + if err != nil { + return value, err + } + nv, ok := v.(V) + if !ok { + return value, fmt.Errorf("unexpected node type %T returned from %T", v, mutation) + } + return nv, nil +} + // newQueryContext returns a new context with the given QueryContext attached in case it does not exist. func newQueryContext(ctx context.Context, typ, op string) context.Context { if ent.QueryFromContext(ctx) == nil { diff --git a/entc/integration/edgeschema/ent/friendship_create.go b/entc/integration/edgeschema/ent/friendship_create.go index b6d8b6ad8..82fe8d5b1 100644 --- a/entc/integration/edgeschema/ent/friendship_create.go +++ b/entc/integration/edgeschema/ent/friendship_create.go @@ -84,50 +84,8 @@ func (fc *FriendshipCreate) Mutation() *FriendshipMutation { // Save creates the Friendship in the database. func (fc *FriendshipCreate) Save(ctx context.Context) (*Friendship, error) { - var ( - err error - node *Friendship - ) fc.defaults() - if len(fc.hooks) == 0 { - if err = fc.check(); err != nil { - return nil, err - } - node, err = fc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*FriendshipMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = fc.check(); err != nil { - return nil, err - } - fc.mutation = mutation - if node, err = fc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(fc.hooks) - 1; i >= 0; i-- { - if fc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = fc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, fc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Friendship) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from FriendshipMutation", v) - } - node = nv - } - return node, err + return withHooks[*Friendship, FriendshipMutation](ctx, fc.sqlSave, fc.mutation, fc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -188,6 +146,9 @@ func (fc *FriendshipCreate) check() error { } func (fc *FriendshipCreate) sqlSave(ctx context.Context) (*Friendship, error) { + if err := fc.check(); err != nil { + return nil, err + } _node, _spec := fc.createSpec() if err := sqlgraph.CreateNode(ctx, fc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -197,6 +158,8 @@ func (fc *FriendshipCreate) sqlSave(ctx context.Context) (*Friendship, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + fc.mutation.id = &_node.ID + fc.mutation.done = true return _node, nil } diff --git a/entc/integration/edgeschema/ent/friendship_delete.go b/entc/integration/edgeschema/ent/friendship_delete.go index 139117ac8..7f62d4e63 100644 --- a/entc/integration/edgeschema/ent/friendship_delete.go +++ b/entc/integration/edgeschema/ent/friendship_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (fd *FriendshipDelete) Where(ps ...predicate.Friendship) *FriendshipDelete // Exec executes the deletion query and returns how many vertices were deleted. func (fd *FriendshipDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(fd.hooks) == 0 { - affected, err = fd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*FriendshipMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - fd.mutation = mutation - affected, err = fd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(fd.hooks) - 1; i >= 0; i-- { - if fd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = fd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, fd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, FriendshipMutation](ctx, fd.sqlExec, fd.mutation, fd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (fd *FriendshipDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + fd.mutation.done = true return affected, err } diff --git a/entc/integration/edgeschema/ent/friendship_update.go b/entc/integration/edgeschema/ent/friendship_update.go index 330f34335..9da83304e 100644 --- a/entc/integration/edgeschema/ent/friendship_update.go +++ b/entc/integration/edgeschema/ent/friendship_update.go @@ -74,40 +74,7 @@ func (fu *FriendshipUpdate) Mutation() *FriendshipMutation { // Save executes the query and returns the number of nodes affected by the update operation. func (fu *FriendshipUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(fu.hooks) == 0 { - if err = fu.check(); err != nil { - return 0, err - } - affected, err = fu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*FriendshipMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = fu.check(); err != nil { - return 0, err - } - fu.mutation = mutation - affected, err = fu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(fu.hooks) - 1; i >= 0; i-- { - if fu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = fu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, fu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, FriendshipMutation](ctx, fu.sqlSave, fu.mutation, fu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -144,6 +111,9 @@ func (fu *FriendshipUpdate) check() error { } func (fu *FriendshipUpdate) sqlSave(ctx context.Context) (n int, err error) { + if err := fu.check(); err != nil { + return n, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: friendship.Table, @@ -178,6 +148,7 @@ func (fu *FriendshipUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + fu.mutation.done = true return n, nil } @@ -238,46 +209,7 @@ func (fuo *FriendshipUpdateOne) Select(field string, fields ...string) *Friendsh // Save executes the query and returns the updated Friendship entity. func (fuo *FriendshipUpdateOne) Save(ctx context.Context) (*Friendship, error) { - var ( - err error - node *Friendship - ) - if len(fuo.hooks) == 0 { - if err = fuo.check(); err != nil { - return nil, err - } - node, err = fuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*FriendshipMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = fuo.check(); err != nil { - return nil, err - } - fuo.mutation = mutation - node, err = fuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(fuo.hooks) - 1; i >= 0; i-- { - if fuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = fuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, fuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Friendship) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from FriendshipMutation", v) - } - node = nv - } - return node, err + return withHooks[*Friendship, FriendshipMutation](ctx, fuo.sqlSave, fuo.mutation, fuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -314,6 +246,9 @@ func (fuo *FriendshipUpdateOne) check() error { } func (fuo *FriendshipUpdateOne) sqlSave(ctx context.Context) (_node *Friendship, err error) { + if err := fuo.check(); err != nil { + return _node, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: friendship.Table, @@ -368,5 +303,6 @@ func (fuo *FriendshipUpdateOne) sqlSave(ctx context.Context) (_node *Friendship, } return nil, err } + fuo.mutation.done = true return _node, nil } diff --git a/entc/integration/edgeschema/ent/group_create.go b/entc/integration/edgeschema/ent/group_create.go index a0e753ed7..08c048717 100644 --- a/entc/integration/edgeschema/ent/group_create.go +++ b/entc/integration/edgeschema/ent/group_create.go @@ -110,50 +110,8 @@ func (gc *GroupCreate) Mutation() *GroupMutation { // Save creates the Group in the database. func (gc *GroupCreate) Save(ctx context.Context) (*Group, error) { - var ( - err error - node *Group - ) gc.defaults() - if len(gc.hooks) == 0 { - if err = gc.check(); err != nil { - return nil, err - } - node, err = gc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GroupMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = gc.check(); err != nil { - return nil, err - } - gc.mutation = mutation - if node, err = gc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(gc.hooks) - 1; i >= 0; i-- { - if gc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = gc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, gc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Group) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from GroupMutation", v) - } - node = nv - } - return node, err + return withHooks[*Group, GroupMutation](ctx, gc.sqlSave, gc.mutation, gc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -195,6 +153,9 @@ func (gc *GroupCreate) check() error { } func (gc *GroupCreate) sqlSave(ctx context.Context) (*Group, error) { + if err := gc.check(); err != nil { + return nil, err + } _node, _spec := gc.createSpec() if err := sqlgraph.CreateNode(ctx, gc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -204,6 +165,8 @@ func (gc *GroupCreate) sqlSave(ctx context.Context) (*Group, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + gc.mutation.id = &_node.ID + gc.mutation.done = true return _node, nil } diff --git a/entc/integration/edgeschema/ent/group_delete.go b/entc/integration/edgeschema/ent/group_delete.go index 67d70b50d..4382d70a4 100644 --- a/entc/integration/edgeschema/ent/group_delete.go +++ b/entc/integration/edgeschema/ent/group_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (gd *GroupDelete) Where(ps ...predicate.Group) *GroupDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (gd *GroupDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(gd.hooks) == 0 { - affected, err = gd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GroupMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - gd.mutation = mutation - affected, err = gd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(gd.hooks) - 1; i >= 0; i-- { - if gd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = gd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, gd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, GroupMutation](ctx, gd.sqlExec, gd.mutation, gd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (gd *GroupDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + gd.mutation.done = true return affected, err } diff --git a/entc/integration/edgeschema/ent/group_update.go b/entc/integration/edgeschema/ent/group_update.go index 2defcfdf1..39f72940f 100644 --- a/entc/integration/edgeschema/ent/group_update.go +++ b/entc/integration/edgeschema/ent/group_update.go @@ -200,34 +200,7 @@ func (gu *GroupUpdate) RemoveGroupTags(g ...*GroupTag) *GroupUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (gu *GroupUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(gu.hooks) == 0 { - affected, err = gu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GroupMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - gu.mutation = mutation - affected, err = gu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(gu.hooks) - 1; i >= 0; i-- { - if gu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = gu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, gu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, GroupMutation](ctx, gu.sqlSave, gu.mutation, gu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -509,6 +482,7 @@ func (gu *GroupUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + gu.mutation.done = true return n, nil } @@ -692,40 +666,7 @@ func (guo *GroupUpdateOne) Select(field string, fields ...string) *GroupUpdateOn // Save executes the query and returns the updated Group entity. func (guo *GroupUpdateOne) Save(ctx context.Context) (*Group, error) { - var ( - err error - node *Group - ) - if len(guo.hooks) == 0 { - node, err = guo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GroupMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - guo.mutation = mutation - node, err = guo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(guo.hooks) - 1; i >= 0; i-- { - if guo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = guo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, guo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Group) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from GroupMutation", v) - } - node = nv - } - return node, err + return withHooks[*Group, GroupMutation](ctx, guo.sqlSave, guo.mutation, guo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -1027,5 +968,6 @@ func (guo *GroupUpdateOne) sqlSave(ctx context.Context) (_node *Group, err error } return nil, err } + guo.mutation.done = true return _node, nil } diff --git a/entc/integration/edgeschema/ent/grouptag_create.go b/entc/integration/edgeschema/ent/grouptag_create.go index 122171158..a6657cfb2 100644 --- a/entc/integration/edgeschema/ent/grouptag_create.go +++ b/entc/integration/edgeschema/ent/grouptag_create.go @@ -56,49 +56,7 @@ func (gtc *GroupTagCreate) Mutation() *GroupTagMutation { // Save creates the GroupTag in the database. func (gtc *GroupTagCreate) Save(ctx context.Context) (*GroupTag, error) { - var ( - err error - node *GroupTag - ) - if len(gtc.hooks) == 0 { - if err = gtc.check(); err != nil { - return nil, err - } - node, err = gtc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GroupTagMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = gtc.check(); err != nil { - return nil, err - } - gtc.mutation = mutation - if node, err = gtc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(gtc.hooks) - 1; i >= 0; i-- { - if gtc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = gtc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, gtc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*GroupTag) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from GroupTagMutation", v) - } - node = nv - } - return node, err + return withHooks[*GroupTag, GroupTagMutation](ctx, gtc.sqlSave, gtc.mutation, gtc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -141,6 +99,9 @@ func (gtc *GroupTagCreate) check() error { } func (gtc *GroupTagCreate) sqlSave(ctx context.Context) (*GroupTag, error) { + if err := gtc.check(); err != nil { + return nil, err + } _node, _spec := gtc.createSpec() if err := sqlgraph.CreateNode(ctx, gtc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -150,6 +111,8 @@ func (gtc *GroupTagCreate) sqlSave(ctx context.Context) (*GroupTag, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + gtc.mutation.id = &_node.ID + gtc.mutation.done = true return _node, nil } diff --git a/entc/integration/edgeschema/ent/grouptag_delete.go b/entc/integration/edgeschema/ent/grouptag_delete.go index 34e6c8861..01bdb7330 100644 --- a/entc/integration/edgeschema/ent/grouptag_delete.go +++ b/entc/integration/edgeschema/ent/grouptag_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (gtd *GroupTagDelete) Where(ps ...predicate.GroupTag) *GroupTagDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (gtd *GroupTagDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(gtd.hooks) == 0 { - affected, err = gtd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GroupTagMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - gtd.mutation = mutation - affected, err = gtd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(gtd.hooks) - 1; i >= 0; i-- { - if gtd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = gtd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, gtd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, GroupTagMutation](ctx, gtd.sqlExec, gtd.mutation, gtd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (gtd *GroupTagDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + gtd.mutation.done = true return affected, err } diff --git a/entc/integration/edgeschema/ent/grouptag_update.go b/entc/integration/edgeschema/ent/grouptag_update.go index c2e6e35e4..a17adedf0 100644 --- a/entc/integration/edgeschema/ent/grouptag_update.go +++ b/entc/integration/edgeschema/ent/grouptag_update.go @@ -74,40 +74,7 @@ func (gtu *GroupTagUpdate) ClearGroup() *GroupTagUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (gtu *GroupTagUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(gtu.hooks) == 0 { - if err = gtu.check(); err != nil { - return 0, err - } - affected, err = gtu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GroupTagMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = gtu.check(); err != nil { - return 0, err - } - gtu.mutation = mutation - affected, err = gtu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(gtu.hooks) - 1; i >= 0; i-- { - if gtu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = gtu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, gtu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, GroupTagMutation](ctx, gtu.sqlSave, gtu.mutation, gtu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -144,6 +111,9 @@ func (gtu *GroupTagUpdate) check() error { } func (gtu *GroupTagUpdate) sqlSave(ctx context.Context) (n int, err error) { + if err := gtu.check(); err != nil { + return n, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: grouptag.Table, @@ -239,6 +209,7 @@ func (gtu *GroupTagUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + gtu.mutation.done = true return n, nil } @@ -298,46 +269,7 @@ func (gtuo *GroupTagUpdateOne) Select(field string, fields ...string) *GroupTagU // Save executes the query and returns the updated GroupTag entity. func (gtuo *GroupTagUpdateOne) Save(ctx context.Context) (*GroupTag, error) { - var ( - err error - node *GroupTag - ) - if len(gtuo.hooks) == 0 { - if err = gtuo.check(); err != nil { - return nil, err - } - node, err = gtuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GroupTagMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = gtuo.check(); err != nil { - return nil, err - } - gtuo.mutation = mutation - node, err = gtuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(gtuo.hooks) - 1; i >= 0; i-- { - if gtuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = gtuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, gtuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*GroupTag) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from GroupTagMutation", v) - } - node = nv - } - return node, err + return withHooks[*GroupTag, GroupTagMutation](ctx, gtuo.sqlSave, gtuo.mutation, gtuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -374,6 +306,9 @@ func (gtuo *GroupTagUpdateOne) check() error { } func (gtuo *GroupTagUpdateOne) sqlSave(ctx context.Context) (_node *GroupTag, err error) { + if err := gtuo.check(); err != nil { + return _node, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: grouptag.Table, @@ -489,5 +424,6 @@ func (gtuo *GroupTagUpdateOne) sqlSave(ctx context.Context) (_node *GroupTag, er } return nil, err } + gtuo.mutation.done = true return _node, nil } diff --git a/entc/integration/edgeschema/ent/relationship_create.go b/entc/integration/edgeschema/ent/relationship_create.go index 8615403dc..c0c7ca9a7 100644 --- a/entc/integration/edgeschema/ent/relationship_create.go +++ b/entc/integration/edgeschema/ent/relationship_create.go @@ -89,50 +89,10 @@ func (rc *RelationshipCreate) Mutation() *RelationshipMutation { // Save creates the Relationship in the database. func (rc *RelationshipCreate) Save(ctx context.Context) (*Relationship, error) { - var ( - err error - node *Relationship - ) if err := rc.defaults(); err != nil { return nil, err } - if len(rc.hooks) == 0 { - if err = rc.check(); err != nil { - return nil, err - } - node, err = rc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*RelationshipMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = rc.check(); err != nil { - return nil, err - } - rc.mutation = mutation - if node, err = rc.sqlSave(ctx); err != nil { - return nil, err - } - return node, err - }) - for i := len(rc.hooks) - 1; i >= 0; i-- { - if rc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = rc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, rc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Relationship) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from RelationshipMutation", v) - } - node = nv - } - return node, err + return withHooks[*Relationship, RelationshipMutation](ctx, rc.sqlSave, rc.mutation, rc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -187,6 +147,9 @@ func (rc *RelationshipCreate) check() error { } func (rc *RelationshipCreate) sqlSave(ctx context.Context) (*Relationship, error) { + if err := rc.check(); err != nil { + return nil, err + } _node, _spec := rc.createSpec() if err := sqlgraph.CreateNode(ctx, rc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { diff --git a/entc/integration/edgeschema/ent/relationship_delete.go b/entc/integration/edgeschema/ent/relationship_delete.go index 5cbfe1dd6..0252e036c 100644 --- a/entc/integration/edgeschema/ent/relationship_delete.go +++ b/entc/integration/edgeschema/ent/relationship_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -31,40 +30,7 @@ func (rd *RelationshipDelete) Where(ps ...predicate.Relationship) *RelationshipD // Exec executes the deletion query and returns how many vertices were deleted. func (rd *RelationshipDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(rd.hooks) == 0 { - affected, err = rd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*RelationshipMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - rd.mutation = mutation - affected, err = rd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(rd.hooks) - 1; i >= 0; i-- { - if rd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = rd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, rd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, RelationshipMutation](ctx, rd.sqlExec, rd.mutation, rd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -93,6 +59,7 @@ func (rd *RelationshipDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + rd.mutation.done = true return affected, err } diff --git a/entc/integration/edgeschema/ent/relationship_update.go b/entc/integration/edgeschema/ent/relationship_update.go index 06f77fafe..acded7983 100644 --- a/entc/integration/edgeschema/ent/relationship_update.go +++ b/entc/integration/edgeschema/ent/relationship_update.go @@ -126,40 +126,7 @@ func (ru *RelationshipUpdate) ClearInfo() *RelationshipUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (ru *RelationshipUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(ru.hooks) == 0 { - if err = ru.check(); err != nil { - return 0, err - } - affected, err = ru.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*RelationshipMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = ru.check(); err != nil { - return 0, err - } - ru.mutation = mutation - affected, err = ru.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(ru.hooks) - 1; i >= 0; i-- { - if ru.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ru.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, ru.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, RelationshipMutation](ctx, ru.sqlSave, ru.mutation, ru.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -196,6 +163,9 @@ func (ru *RelationshipUpdate) check() error { } func (ru *RelationshipUpdate) sqlSave(ctx context.Context) (n int, err error) { + if err := ru.check(); err != nil { + return n, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: relationship.Table, @@ -338,6 +308,7 @@ func (ru *RelationshipUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + ru.mutation.done = true return n, nil } @@ -449,46 +420,7 @@ func (ruo *RelationshipUpdateOne) Select(field string, fields ...string) *Relati // Save executes the query and returns the updated Relationship entity. func (ruo *RelationshipUpdateOne) Save(ctx context.Context) (*Relationship, error) { - var ( - err error - node *Relationship - ) - if len(ruo.hooks) == 0 { - if err = ruo.check(); err != nil { - return nil, err - } - node, err = ruo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*RelationshipMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = ruo.check(); err != nil { - return nil, err - } - ruo.mutation = mutation - node, err = ruo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(ruo.hooks) - 1; i >= 0; i-- { - if ruo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ruo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, ruo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Relationship) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from RelationshipMutation", v) - } - node = nv - } - return node, err + return withHooks[*Relationship, RelationshipMutation](ctx, ruo.sqlSave, ruo.mutation, ruo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -525,6 +457,9 @@ func (ruo *RelationshipUpdateOne) check() error { } func (ruo *RelationshipUpdateOne) sqlSave(ctx context.Context) (_node *Relationship, err error) { + if err := ruo.check(); err != nil { + return _node, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: relationship.Table, @@ -689,5 +624,6 @@ func (ruo *RelationshipUpdateOne) sqlSave(ctx context.Context) (_node *Relations } return nil, err } + ruo.mutation.done = true return _node, nil } diff --git a/entc/integration/edgeschema/ent/relationshipinfo_create.go b/entc/integration/edgeschema/ent/relationshipinfo_create.go index 1f6c11a20..da3bc4add 100644 --- a/entc/integration/edgeschema/ent/relationshipinfo_create.go +++ b/entc/integration/edgeschema/ent/relationshipinfo_create.go @@ -38,49 +38,7 @@ func (ric *RelationshipInfoCreate) Mutation() *RelationshipInfoMutation { // Save creates the RelationshipInfo in the database. func (ric *RelationshipInfoCreate) Save(ctx context.Context) (*RelationshipInfo, error) { - var ( - err error - node *RelationshipInfo - ) - if len(ric.hooks) == 0 { - if err = ric.check(); err != nil { - return nil, err - } - node, err = ric.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*RelationshipInfoMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = ric.check(); err != nil { - return nil, err - } - ric.mutation = mutation - if node, err = ric.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(ric.hooks) - 1; i >= 0; i-- { - if ric.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ric.hooks[i](mut) - } - v, err := mut.Mutate(ctx, ric.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*RelationshipInfo) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from RelationshipInfoMutation", v) - } - node = nv - } - return node, err + return withHooks[*RelationshipInfo, RelationshipInfoMutation](ctx, ric.sqlSave, ric.mutation, ric.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -114,6 +72,9 @@ func (ric *RelationshipInfoCreate) check() error { } func (ric *RelationshipInfoCreate) sqlSave(ctx context.Context) (*RelationshipInfo, error) { + if err := ric.check(); err != nil { + return nil, err + } _node, _spec := ric.createSpec() if err := sqlgraph.CreateNode(ctx, ric.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -123,6 +84,8 @@ func (ric *RelationshipInfoCreate) sqlSave(ctx context.Context) (*RelationshipIn } id := _spec.ID.Value.(int64) _node.ID = int(id) + ric.mutation.id = &_node.ID + ric.mutation.done = true return _node, nil } diff --git a/entc/integration/edgeschema/ent/relationshipinfo_delete.go b/entc/integration/edgeschema/ent/relationshipinfo_delete.go index f4f277ae1..120f60a23 100644 --- a/entc/integration/edgeschema/ent/relationshipinfo_delete.go +++ b/entc/integration/edgeschema/ent/relationshipinfo_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (rid *RelationshipInfoDelete) Where(ps ...predicate.RelationshipInfo) *Rela // Exec executes the deletion query and returns how many vertices were deleted. func (rid *RelationshipInfoDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(rid.hooks) == 0 { - affected, err = rid.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*RelationshipInfoMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - rid.mutation = mutation - affected, err = rid.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(rid.hooks) - 1; i >= 0; i-- { - if rid.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = rid.hooks[i](mut) - } - n, err := mut.Mutate(ctx, rid.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, RelationshipInfoMutation](ctx, rid.sqlExec, rid.mutation, rid.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (rid *RelationshipInfoDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + rid.mutation.done = true return affected, err } diff --git a/entc/integration/edgeschema/ent/relationshipinfo_update.go b/entc/integration/edgeschema/ent/relationshipinfo_update.go index f2ff70d70..f2a5cfc4a 100644 --- a/entc/integration/edgeschema/ent/relationshipinfo_update.go +++ b/entc/integration/edgeschema/ent/relationshipinfo_update.go @@ -44,34 +44,7 @@ func (riu *RelationshipInfoUpdate) Mutation() *RelationshipInfoMutation { // Save executes the query and returns the number of nodes affected by the update operation. func (riu *RelationshipInfoUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(riu.hooks) == 0 { - affected, err = riu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*RelationshipInfoMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - riu.mutation = mutation - affected, err = riu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(riu.hooks) - 1; i >= 0; i-- { - if riu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = riu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, riu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, RelationshipInfoMutation](ctx, riu.sqlSave, riu.mutation, riu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -125,6 +98,7 @@ func (riu *RelationshipInfoUpdate) sqlSave(ctx context.Context) (n int, err erro } return 0, err } + riu.mutation.done = true return n, nil } @@ -156,40 +130,7 @@ func (riuo *RelationshipInfoUpdateOne) Select(field string, fields ...string) *R // Save executes the query and returns the updated RelationshipInfo entity. func (riuo *RelationshipInfoUpdateOne) Save(ctx context.Context) (*RelationshipInfo, error) { - var ( - err error - node *RelationshipInfo - ) - if len(riuo.hooks) == 0 { - node, err = riuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*RelationshipInfoMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - riuo.mutation = mutation - node, err = riuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(riuo.hooks) - 1; i >= 0; i-- { - if riuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = riuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, riuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*RelationshipInfo) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from RelationshipInfoMutation", v) - } - node = nv - } - return node, err + return withHooks[*RelationshipInfo, RelationshipInfoMutation](ctx, riuo.sqlSave, riuo.mutation, riuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -263,5 +204,6 @@ func (riuo *RelationshipInfoUpdateOne) sqlSave(ctx context.Context) (_node *Rela } return nil, err } + riuo.mutation.done = true return _node, nil } diff --git a/entc/integration/edgeschema/ent/role_create.go b/entc/integration/edgeschema/ent/role_create.go index 4e66a8a2e..c5971d0ec 100644 --- a/entc/integration/edgeschema/ent/role_create.go +++ b/entc/integration/edgeschema/ent/role_create.go @@ -69,50 +69,8 @@ func (rc *RoleCreate) Mutation() *RoleMutation { // Save creates the Role in the database. func (rc *RoleCreate) Save(ctx context.Context) (*Role, error) { - var ( - err error - node *Role - ) rc.defaults() - if len(rc.hooks) == 0 { - if err = rc.check(); err != nil { - return nil, err - } - node, err = rc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*RoleMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = rc.check(); err != nil { - return nil, err - } - rc.mutation = mutation - if node, err = rc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(rc.hooks) - 1; i >= 0; i-- { - if rc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = rc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, rc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Role) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from RoleMutation", v) - } - node = nv - } - return node, err + return withHooks[*Role, RoleMutation](ctx, rc.sqlSave, rc.mutation, rc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -157,6 +115,9 @@ func (rc *RoleCreate) check() error { } func (rc *RoleCreate) sqlSave(ctx context.Context) (*Role, error) { + if err := rc.check(); err != nil { + return nil, err + } _node, _spec := rc.createSpec() if err := sqlgraph.CreateNode(ctx, rc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -166,6 +127,8 @@ func (rc *RoleCreate) sqlSave(ctx context.Context) (*Role, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + rc.mutation.id = &_node.ID + rc.mutation.done = true return _node, nil } diff --git a/entc/integration/edgeschema/ent/role_delete.go b/entc/integration/edgeschema/ent/role_delete.go index 93036cfca..4aac07ef0 100644 --- a/entc/integration/edgeschema/ent/role_delete.go +++ b/entc/integration/edgeschema/ent/role_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (rd *RoleDelete) Where(ps ...predicate.Role) *RoleDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (rd *RoleDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(rd.hooks) == 0 { - affected, err = rd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*RoleMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - rd.mutation = mutation - affected, err = rd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(rd.hooks) - 1; i >= 0; i-- { - if rd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = rd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, rd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, RoleMutation](ctx, rd.sqlExec, rd.mutation, rd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (rd *RoleDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + rd.mutation.done = true return affected, err } diff --git a/entc/integration/edgeschema/ent/role_update.go b/entc/integration/edgeschema/ent/role_update.go index 512816d14..cc71f907b 100644 --- a/entc/integration/edgeschema/ent/role_update.go +++ b/entc/integration/edgeschema/ent/role_update.go @@ -96,34 +96,7 @@ func (ru *RoleUpdate) RemoveUser(u ...*User) *RoleUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (ru *RoleUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(ru.hooks) == 0 { - affected, err = ru.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*RoleMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - ru.mutation = mutation - affected, err = ru.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(ru.hooks) - 1; i >= 0; i-- { - if ru.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ru.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, ru.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, RoleMutation](ctx, ru.sqlSave, ru.mutation, ru.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -246,6 +219,7 @@ func (ru *RoleUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + ru.mutation.done = true return n, nil } @@ -327,40 +301,7 @@ func (ruo *RoleUpdateOne) Select(field string, fields ...string) *RoleUpdateOne // Save executes the query and returns the updated Role entity. func (ruo *RoleUpdateOne) Save(ctx context.Context) (*Role, error) { - var ( - err error - node *Role - ) - if len(ruo.hooks) == 0 { - node, err = ruo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*RoleMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - ruo.mutation = mutation - node, err = ruo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(ruo.hooks) - 1; i >= 0; i-- { - if ruo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ruo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, ruo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Role) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from RoleMutation", v) - } - node = nv - } - return node, err + return withHooks[*Role, RoleMutation](ctx, ruo.sqlSave, ruo.mutation, ruo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -503,5 +444,6 @@ func (ruo *RoleUpdateOne) sqlSave(ctx context.Context) (_node *Role, err error) } return nil, err } + ruo.mutation.done = true return _node, nil } diff --git a/entc/integration/edgeschema/ent/roleuser_create.go b/entc/integration/edgeschema/ent/roleuser_create.go index 433a82151..a8bfcdcea 100644 --- a/entc/integration/edgeschema/ent/roleuser_create.go +++ b/entc/integration/edgeschema/ent/roleuser_create.go @@ -71,48 +71,8 @@ func (ruc *RoleUserCreate) Mutation() *RoleUserMutation { // Save creates the RoleUser in the database. func (ruc *RoleUserCreate) Save(ctx context.Context) (*RoleUser, error) { - var ( - err error - node *RoleUser - ) ruc.defaults() - if len(ruc.hooks) == 0 { - if err = ruc.check(); err != nil { - return nil, err - } - node, err = ruc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*RoleUserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = ruc.check(); err != nil { - return nil, err - } - ruc.mutation = mutation - if node, err = ruc.sqlSave(ctx); err != nil { - return nil, err - } - return node, err - }) - for i := len(ruc.hooks) - 1; i >= 0; i-- { - if ruc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ruc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, ruc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*RoleUser) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from RoleUserMutation", v) - } - node = nv - } - return node, err + return withHooks[*RoleUser, RoleUserMutation](ctx, ruc.sqlSave, ruc.mutation, ruc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -166,6 +126,9 @@ func (ruc *RoleUserCreate) check() error { } func (ruc *RoleUserCreate) sqlSave(ctx context.Context) (*RoleUser, error) { + if err := ruc.check(); err != nil { + return nil, err + } _node, _spec := ruc.createSpec() if err := sqlgraph.CreateNode(ctx, ruc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { diff --git a/entc/integration/edgeschema/ent/roleuser_delete.go b/entc/integration/edgeschema/ent/roleuser_delete.go index bd3f8ca58..9b70b28d4 100644 --- a/entc/integration/edgeschema/ent/roleuser_delete.go +++ b/entc/integration/edgeschema/ent/roleuser_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -31,40 +30,7 @@ func (rud *RoleUserDelete) Where(ps ...predicate.RoleUser) *RoleUserDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (rud *RoleUserDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(rud.hooks) == 0 { - affected, err = rud.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*RoleUserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - rud.mutation = mutation - affected, err = rud.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(rud.hooks) - 1; i >= 0; i-- { - if rud.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = rud.hooks[i](mut) - } - n, err := mut.Mutate(ctx, rud.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, RoleUserMutation](ctx, rud.sqlExec, rud.mutation, rud.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -93,6 +59,7 @@ func (rud *RoleUserDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + rud.mutation.done = true return affected, err } diff --git a/entc/integration/edgeschema/ent/roleuser_update.go b/entc/integration/edgeschema/ent/roleuser_update.go index 9b880a4a2..545aea239 100644 --- a/entc/integration/edgeschema/ent/roleuser_update.go +++ b/entc/integration/edgeschema/ent/roleuser_update.go @@ -89,40 +89,7 @@ func (ruu *RoleUserUpdate) ClearUser() *RoleUserUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (ruu *RoleUserUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(ruu.hooks) == 0 { - if err = ruu.check(); err != nil { - return 0, err - } - affected, err = ruu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*RoleUserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = ruu.check(); err != nil { - return 0, err - } - ruu.mutation = mutation - affected, err = ruu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(ruu.hooks) - 1; i >= 0; i-- { - if ruu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ruu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, ruu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, RoleUserMutation](ctx, ruu.sqlSave, ruu.mutation, ruu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -159,6 +126,9 @@ func (ruu *RoleUserUpdate) check() error { } func (ruu *RoleUserUpdate) sqlSave(ctx context.Context) (n int, err error) { + if err := ruu.check(); err != nil { + return n, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: roleuser.Table, @@ -263,6 +233,7 @@ func (ruu *RoleUserUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + ruu.mutation.done = true return n, nil } @@ -336,46 +307,7 @@ func (ruuo *RoleUserUpdateOne) Select(field string, fields ...string) *RoleUserU // Save executes the query and returns the updated RoleUser entity. func (ruuo *RoleUserUpdateOne) Save(ctx context.Context) (*RoleUser, error) { - var ( - err error - node *RoleUser - ) - if len(ruuo.hooks) == 0 { - if err = ruuo.check(); err != nil { - return nil, err - } - node, err = ruuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*RoleUserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = ruuo.check(); err != nil { - return nil, err - } - ruuo.mutation = mutation - node, err = ruuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(ruuo.hooks) - 1; i >= 0; i-- { - if ruuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ruuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, ruuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*RoleUser) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from RoleUserMutation", v) - } - node = nv - } - return node, err + return withHooks[*RoleUser, RoleUserMutation](ctx, ruuo.sqlSave, ruuo.mutation, ruuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -412,6 +344,9 @@ func (ruuo *RoleUserUpdateOne) check() error { } func (ruuo *RoleUserUpdateOne) sqlSave(ctx context.Context) (_node *RoleUser, err error) { + if err := ruuo.check(); err != nil { + return _node, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: roleuser.Table, @@ -538,5 +473,6 @@ func (ruuo *RoleUserUpdateOne) sqlSave(ctx context.Context) (_node *RoleUser, er } return nil, err } + ruuo.mutation.done = true return _node, nil } diff --git a/entc/integration/edgeschema/ent/tag_create.go b/entc/integration/edgeschema/ent/tag_create.go index 3c5d42a0b..6fd840734 100644 --- a/entc/integration/edgeschema/ent/tag_create.go +++ b/entc/integration/edgeschema/ent/tag_create.go @@ -103,49 +103,7 @@ func (tc *TagCreate) Mutation() *TagMutation { // Save creates the Tag in the database. func (tc *TagCreate) Save(ctx context.Context) (*Tag, error) { - var ( - err error - node *Tag - ) - if len(tc.hooks) == 0 { - if err = tc.check(); err != nil { - return nil, err - } - node, err = tc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*TagMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = tc.check(); err != nil { - return nil, err - } - tc.mutation = mutation - if node, err = tc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(tc.hooks) - 1; i >= 0; i-- { - if tc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = tc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, tc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Tag) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from TagMutation", v) - } - node = nv - } - return node, err + return withHooks[*Tag, TagMutation](ctx, tc.sqlSave, tc.mutation, tc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -179,6 +137,9 @@ func (tc *TagCreate) check() error { } func (tc *TagCreate) sqlSave(ctx context.Context) (*Tag, error) { + if err := tc.check(); err != nil { + return nil, err + } _node, _spec := tc.createSpec() if err := sqlgraph.CreateNode(ctx, tc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -188,6 +149,8 @@ func (tc *TagCreate) sqlSave(ctx context.Context) (*Tag, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + tc.mutation.id = &_node.ID + tc.mutation.done = true return _node, nil } diff --git a/entc/integration/edgeschema/ent/tag_delete.go b/entc/integration/edgeschema/ent/tag_delete.go index eb9ef008d..ddc1b94d4 100644 --- a/entc/integration/edgeschema/ent/tag_delete.go +++ b/entc/integration/edgeschema/ent/tag_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (td *TagDelete) Where(ps ...predicate.Tag) *TagDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (td *TagDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(td.hooks) == 0 { - affected, err = td.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*TagMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - td.mutation = mutation - affected, err = td.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(td.hooks) - 1; i >= 0; i-- { - if td.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = td.hooks[i](mut) - } - n, err := mut.Mutate(ctx, td.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, TagMutation](ctx, td.sqlExec, td.mutation, td.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (td *TagDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + td.mutation.done = true return affected, err } diff --git a/entc/integration/edgeschema/ent/tag_update.go b/entc/integration/edgeschema/ent/tag_update.go index 0a44305b3..9c0cbffc2 100644 --- a/entc/integration/edgeschema/ent/tag_update.go +++ b/entc/integration/edgeschema/ent/tag_update.go @@ -193,34 +193,7 @@ func (tu *TagUpdate) RemoveGroupTags(g ...*GroupTag) *TagUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (tu *TagUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(tu.hooks) == 0 { - affected, err = tu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*TagMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - tu.mutation = mutation - affected, err = tu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(tu.hooks) - 1; i >= 0; i-- { - if tu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = tu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, tu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, TagMutation](ctx, tu.sqlSave, tu.mutation, tu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -511,6 +484,7 @@ func (tu *TagUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + tu.mutation.done = true return n, nil } @@ -686,40 +660,7 @@ func (tuo *TagUpdateOne) Select(field string, fields ...string) *TagUpdateOne { // Save executes the query and returns the updated Tag entity. func (tuo *TagUpdateOne) Save(ctx context.Context) (*Tag, error) { - var ( - err error - node *Tag - ) - if len(tuo.hooks) == 0 { - node, err = tuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*TagMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - tuo.mutation = mutation - node, err = tuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(tuo.hooks) - 1; i >= 0; i-- { - if tuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = tuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, tuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Tag) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from TagMutation", v) - } - node = nv - } - return node, err + return withHooks[*Tag, TagMutation](ctx, tuo.sqlSave, tuo.mutation, tuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -1030,5 +971,6 @@ func (tuo *TagUpdateOne) sqlSave(ctx context.Context) (_node *Tag, err error) { } return nil, err } + tuo.mutation.done = true return _node, nil } diff --git a/entc/integration/edgeschema/ent/tweet_create.go b/entc/integration/edgeschema/ent/tweet_create.go index 43f38b922..a981ef80c 100644 --- a/entc/integration/edgeschema/ent/tweet_create.go +++ b/entc/integration/edgeschema/ent/tweet_create.go @@ -118,49 +118,7 @@ func (tc *TweetCreate) Mutation() *TweetMutation { // Save creates the Tweet in the database. func (tc *TweetCreate) Save(ctx context.Context) (*Tweet, error) { - var ( - err error - node *Tweet - ) - if len(tc.hooks) == 0 { - if err = tc.check(); err != nil { - return nil, err - } - node, err = tc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*TweetMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = tc.check(); err != nil { - return nil, err - } - tc.mutation = mutation - if node, err = tc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(tc.hooks) - 1; i >= 0; i-- { - if tc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = tc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, tc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Tweet) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from TweetMutation", v) - } - node = nv - } - return node, err + return withHooks[*Tweet, TweetMutation](ctx, tc.sqlSave, tc.mutation, tc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -194,6 +152,9 @@ func (tc *TweetCreate) check() error { } func (tc *TweetCreate) sqlSave(ctx context.Context) (*Tweet, error) { + if err := tc.check(); err != nil { + return nil, err + } _node, _spec := tc.createSpec() if err := sqlgraph.CreateNode(ctx, tc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -203,6 +164,8 @@ func (tc *TweetCreate) sqlSave(ctx context.Context) (*Tweet, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + tc.mutation.id = &_node.ID + tc.mutation.done = true return _node, nil } diff --git a/entc/integration/edgeschema/ent/tweet_delete.go b/entc/integration/edgeschema/ent/tweet_delete.go index 05458cfc7..59f7a1ae1 100644 --- a/entc/integration/edgeschema/ent/tweet_delete.go +++ b/entc/integration/edgeschema/ent/tweet_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (td *TweetDelete) Where(ps ...predicate.Tweet) *TweetDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (td *TweetDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(td.hooks) == 0 { - affected, err = td.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*TweetMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - td.mutation = mutation - affected, err = td.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(td.hooks) - 1; i >= 0; i-- { - if td.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = td.hooks[i](mut) - } - n, err := mut.Mutate(ctx, td.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, TweetMutation](ctx, td.sqlExec, td.mutation, td.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (td *TweetDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + td.mutation.done = true return affected, err } diff --git a/entc/integration/edgeschema/ent/tweet_update.go b/entc/integration/edgeschema/ent/tweet_update.go index 0187ddd17..812416dc8 100644 --- a/entc/integration/edgeschema/ent/tweet_update.go +++ b/entc/integration/edgeschema/ent/tweet_update.go @@ -229,34 +229,7 @@ func (tu *TweetUpdate) RemoveTweetTags(t ...*TweetTag) *TweetUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (tu *TweetUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(tu.hooks) == 0 { - affected, err = tu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*TweetMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - tu.mutation = mutation - affected, err = tu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(tu.hooks) - 1; i >= 0; i-- { - if tu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = tu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, tu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, TweetMutation](ctx, tu.sqlSave, tu.mutation, tu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -625,6 +598,7 @@ func (tu *TweetUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + tu.mutation.done = true return n, nil } @@ -836,40 +810,7 @@ func (tuo *TweetUpdateOne) Select(field string, fields ...string) *TweetUpdateOn // Save executes the query and returns the updated Tweet entity. func (tuo *TweetUpdateOne) Save(ctx context.Context) (*Tweet, error) { - var ( - err error - node *Tweet - ) - if len(tuo.hooks) == 0 { - node, err = tuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*TweetMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - tuo.mutation = mutation - node, err = tuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(tuo.hooks) - 1; i >= 0; i-- { - if tuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = tuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, tuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Tweet) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from TweetMutation", v) - } - node = nv - } - return node, err + return withHooks[*Tweet, TweetMutation](ctx, tuo.sqlSave, tuo.mutation, tuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -1258,5 +1199,6 @@ func (tuo *TweetUpdateOne) sqlSave(ctx context.Context) (_node *Tweet, err error } return nil, err } + tuo.mutation.done = true return _node, nil } diff --git a/entc/integration/edgeschema/ent/tweetlike_create.go b/entc/integration/edgeschema/ent/tweetlike_create.go index 7dc47150c..0b812754d 100644 --- a/entc/integration/edgeschema/ent/tweetlike_create.go +++ b/entc/integration/edgeschema/ent/tweetlike_create.go @@ -71,50 +71,10 @@ func (tlc *TweetLikeCreate) Mutation() *TweetLikeMutation { // Save creates the TweetLike in the database. func (tlc *TweetLikeCreate) Save(ctx context.Context) (*TweetLike, error) { - var ( - err error - node *TweetLike - ) if err := tlc.defaults(); err != nil { return nil, err } - if len(tlc.hooks) == 0 { - if err = tlc.check(); err != nil { - return nil, err - } - node, err = tlc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*TweetLikeMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = tlc.check(); err != nil { - return nil, err - } - tlc.mutation = mutation - if node, err = tlc.sqlSave(ctx); err != nil { - return nil, err - } - return node, err - }) - for i := len(tlc.hooks) - 1; i >= 0; i-- { - if tlc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = tlc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, tlc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*TweetLike) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from TweetLikeMutation", v) - } - node = nv - } - return node, err + return withHooks[*TweetLike, TweetLikeMutation](ctx, tlc.sqlSave, tlc.mutation, tlc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -172,6 +132,9 @@ func (tlc *TweetLikeCreate) check() error { } func (tlc *TweetLikeCreate) sqlSave(ctx context.Context) (*TweetLike, error) { + if err := tlc.check(); err != nil { + return nil, err + } _node, _spec := tlc.createSpec() if err := sqlgraph.CreateNode(ctx, tlc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { diff --git a/entc/integration/edgeschema/ent/tweetlike_delete.go b/entc/integration/edgeschema/ent/tweetlike_delete.go index 59f69aa1b..a499d4936 100644 --- a/entc/integration/edgeschema/ent/tweetlike_delete.go +++ b/entc/integration/edgeschema/ent/tweetlike_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -31,40 +30,7 @@ func (tld *TweetLikeDelete) Where(ps ...predicate.TweetLike) *TweetLikeDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (tld *TweetLikeDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(tld.hooks) == 0 { - affected, err = tld.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*TweetLikeMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - tld.mutation = mutation - affected, err = tld.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(tld.hooks) - 1; i >= 0; i-- { - if tld.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = tld.hooks[i](mut) - } - n, err := mut.Mutate(ctx, tld.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, TweetLikeMutation](ctx, tld.sqlExec, tld.mutation, tld.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -93,6 +59,7 @@ func (tld *TweetLikeDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + tld.mutation.done = true return affected, err } diff --git a/entc/integration/edgeschema/ent/tweetlike_update.go b/entc/integration/edgeschema/ent/tweetlike_update.go index f668a6191..a2cc087bb 100644 --- a/entc/integration/edgeschema/ent/tweetlike_update.go +++ b/entc/integration/edgeschema/ent/tweetlike_update.go @@ -89,40 +89,7 @@ func (tlu *TweetLikeUpdate) ClearUser() *TweetLikeUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (tlu *TweetLikeUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(tlu.hooks) == 0 { - if err = tlu.check(); err != nil { - return 0, err - } - affected, err = tlu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*TweetLikeMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = tlu.check(); err != nil { - return 0, err - } - tlu.mutation = mutation - affected, err = tlu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(tlu.hooks) - 1; i >= 0; i-- { - if tlu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = tlu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, tlu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, TweetLikeMutation](ctx, tlu.sqlSave, tlu.mutation, tlu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -159,6 +126,9 @@ func (tlu *TweetLikeUpdate) check() error { } func (tlu *TweetLikeUpdate) sqlSave(ctx context.Context) (n int, err error) { + if err := tlu.check(); err != nil { + return n, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: tweetlike.Table, @@ -263,6 +233,7 @@ func (tlu *TweetLikeUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + tlu.mutation.done = true return n, nil } @@ -336,46 +307,7 @@ func (tluo *TweetLikeUpdateOne) Select(field string, fields ...string) *TweetLik // Save executes the query and returns the updated TweetLike entity. func (tluo *TweetLikeUpdateOne) Save(ctx context.Context) (*TweetLike, error) { - var ( - err error - node *TweetLike - ) - if len(tluo.hooks) == 0 { - if err = tluo.check(); err != nil { - return nil, err - } - node, err = tluo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*TweetLikeMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = tluo.check(); err != nil { - return nil, err - } - tluo.mutation = mutation - node, err = tluo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(tluo.hooks) - 1; i >= 0; i-- { - if tluo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = tluo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, tluo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*TweetLike) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from TweetLikeMutation", v) - } - node = nv - } - return node, err + return withHooks[*TweetLike, TweetLikeMutation](ctx, tluo.sqlSave, tluo.mutation, tluo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -412,6 +344,9 @@ func (tluo *TweetLikeUpdateOne) check() error { } func (tluo *TweetLikeUpdateOne) sqlSave(ctx context.Context) (_node *TweetLike, err error) { + if err := tluo.check(); err != nil { + return _node, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: tweetlike.Table, @@ -538,5 +473,6 @@ func (tluo *TweetLikeUpdateOne) sqlSave(ctx context.Context) (_node *TweetLike, } return nil, err } + tluo.mutation.done = true return _node, nil } diff --git a/entc/integration/edgeschema/ent/tweettag_create.go b/entc/integration/edgeschema/ent/tweettag_create.go index 356a57cb3..a255d29fd 100644 --- a/entc/integration/edgeschema/ent/tweettag_create.go +++ b/entc/integration/edgeschema/ent/tweettag_create.go @@ -87,50 +87,8 @@ func (ttc *TweetTagCreate) Mutation() *TweetTagMutation { // Save creates the TweetTag in the database. func (ttc *TweetTagCreate) Save(ctx context.Context) (*TweetTag, error) { - var ( - err error - node *TweetTag - ) ttc.defaults() - if len(ttc.hooks) == 0 { - if err = ttc.check(); err != nil { - return nil, err - } - node, err = ttc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*TweetTagMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = ttc.check(); err != nil { - return nil, err - } - ttc.mutation = mutation - if node, err = ttc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(ttc.hooks) - 1; i >= 0; i-- { - if ttc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ttc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, ttc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*TweetTag) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from TweetTagMutation", v) - } - node = nv - } - return node, err + return withHooks[*TweetTag, TweetTagMutation](ctx, ttc.sqlSave, ttc.mutation, ttc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -188,6 +146,9 @@ func (ttc *TweetTagCreate) check() error { } func (ttc *TweetTagCreate) sqlSave(ctx context.Context) (*TweetTag, error) { + if err := ttc.check(); err != nil { + return nil, err + } _node, _spec := ttc.createSpec() if err := sqlgraph.CreateNode(ctx, ttc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -202,6 +163,8 @@ func (ttc *TweetTagCreate) sqlSave(ctx context.Context) (*TweetTag, error) { return nil, err } } + ttc.mutation.id = &_node.ID + ttc.mutation.done = true return _node, nil } diff --git a/entc/integration/edgeschema/ent/tweettag_delete.go b/entc/integration/edgeschema/ent/tweettag_delete.go index 66f6a540b..e829be199 100644 --- a/entc/integration/edgeschema/ent/tweettag_delete.go +++ b/entc/integration/edgeschema/ent/tweettag_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (ttd *TweetTagDelete) Where(ps ...predicate.TweetTag) *TweetTagDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (ttd *TweetTagDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(ttd.hooks) == 0 { - affected, err = ttd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*TweetTagMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - ttd.mutation = mutation - affected, err = ttd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(ttd.hooks) - 1; i >= 0; i-- { - if ttd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ttd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, ttd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, TweetTagMutation](ctx, ttd.sqlExec, ttd.mutation, ttd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (ttd *TweetTagDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + ttd.mutation.done = true return affected, err } diff --git a/entc/integration/edgeschema/ent/tweettag_update.go b/entc/integration/edgeschema/ent/tweettag_update.go index 805a5f8ec..d83853a01 100644 --- a/entc/integration/edgeschema/ent/tweettag_update.go +++ b/entc/integration/edgeschema/ent/tweettag_update.go @@ -89,40 +89,7 @@ func (ttu *TweetTagUpdate) ClearTweet() *TweetTagUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (ttu *TweetTagUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(ttu.hooks) == 0 { - if err = ttu.check(); err != nil { - return 0, err - } - affected, err = ttu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*TweetTagMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = ttu.check(); err != nil { - return 0, err - } - ttu.mutation = mutation - affected, err = ttu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(ttu.hooks) - 1; i >= 0; i-- { - if ttu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ttu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, ttu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, TweetTagMutation](ctx, ttu.sqlSave, ttu.mutation, ttu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -159,6 +126,9 @@ func (ttu *TweetTagUpdate) check() error { } func (ttu *TweetTagUpdate) sqlSave(ctx context.Context) (n int, err error) { + if err := ttu.check(); err != nil { + return n, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: tweettag.Table, @@ -257,6 +227,7 @@ func (ttu *TweetTagUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + ttu.mutation.done = true return n, nil } @@ -330,46 +301,7 @@ func (ttuo *TweetTagUpdateOne) Select(field string, fields ...string) *TweetTagU // Save executes the query and returns the updated TweetTag entity. func (ttuo *TweetTagUpdateOne) Save(ctx context.Context) (*TweetTag, error) { - var ( - err error - node *TweetTag - ) - if len(ttuo.hooks) == 0 { - if err = ttuo.check(); err != nil { - return nil, err - } - node, err = ttuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*TweetTagMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = ttuo.check(); err != nil { - return nil, err - } - ttuo.mutation = mutation - node, err = ttuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(ttuo.hooks) - 1; i >= 0; i-- { - if ttuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ttuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, ttuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*TweetTag) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from TweetTagMutation", v) - } - node = nv - } - return node, err + return withHooks[*TweetTag, TweetTagMutation](ctx, ttuo.sqlSave, ttuo.mutation, ttuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -406,6 +338,9 @@ func (ttuo *TweetTagUpdateOne) check() error { } func (ttuo *TweetTagUpdateOne) sqlSave(ctx context.Context) (_node *TweetTag, err error) { + if err := ttuo.check(); err != nil { + return _node, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: tweettag.Table, @@ -524,5 +459,6 @@ func (ttuo *TweetTagUpdateOne) sqlSave(ctx context.Context) (_node *TweetTag, er } return nil, err } + ttuo.mutation.done = true return _node, nil } diff --git a/entc/integration/edgeschema/ent/user_create.go b/entc/integration/edgeschema/ent/user_create.go index 48cf33c73..a0a2a1816 100644 --- a/entc/integration/edgeschema/ent/user_create.go +++ b/entc/integration/edgeschema/ent/user_create.go @@ -187,52 +187,10 @@ func (uc *UserCreate) Mutation() *UserMutation { // Save creates the User in the database. func (uc *UserCreate) Save(ctx context.Context) (*User, error) { - var ( - err error - node *User - ) if err := uc.defaults(); err != nil { return nil, err } - if len(uc.hooks) == 0 { - if err = uc.check(); err != nil { - return nil, err - } - node, err = uc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = uc.check(); err != nil { - return nil, err - } - uc.mutation = mutation - if node, err = uc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(uc.hooks) - 1; i >= 0; i-- { - if uc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*User) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) - } - node = nv - } - return node, err + return withHooks[*User, UserMutation](ctx, uc.sqlSave, uc.mutation, uc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -275,6 +233,9 @@ func (uc *UserCreate) check() error { } func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { + if err := uc.check(); err != nil { + return nil, err + } _node, _spec := uc.createSpec() if err := sqlgraph.CreateNode(ctx, uc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -284,6 +245,8 @@ func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + uc.mutation.id = &_node.ID + uc.mutation.done = true return _node, nil } diff --git a/entc/integration/edgeschema/ent/user_delete.go b/entc/integration/edgeschema/ent/user_delete.go index 56fa623f5..de287819c 100644 --- a/entc/integration/edgeschema/ent/user_delete.go +++ b/entc/integration/edgeschema/ent/user_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (ud *UserDelete) Where(ps ...predicate.User) *UserDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (ud *UserDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(ud.hooks) == 0 { - affected, err = ud.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - ud.mutation = mutation - affected, err = ud.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(ud.hooks) - 1; i >= 0; i-- { - if ud.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ud.hooks[i](mut) - } - n, err := mut.Mutate(ctx, ud.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, UserMutation](ctx, ud.sqlExec, ud.mutation, ud.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (ud *UserDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + ud.mutation.done = true return affected, err } diff --git a/entc/integration/edgeschema/ent/user_update.go b/entc/integration/edgeschema/ent/user_update.go index 1a4821bb4..f9b6d1906 100644 --- a/entc/integration/edgeschema/ent/user_update.go +++ b/entc/integration/edgeschema/ent/user_update.go @@ -382,34 +382,7 @@ func (uu *UserUpdate) RemoveUserTweets(u ...*UserTweet) *UserUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (uu *UserUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(uu.hooks) == 0 { - affected, err = uu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - uu.mutation = mutation - affected, err = uu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(uu.hooks) - 1; i >= 0; i-- { - if uu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, uu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, UserMutation](ctx, uu.sqlSave, uu.mutation, uu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -1021,6 +994,7 @@ func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + uu.mutation.done = true return n, nil } @@ -1384,40 +1358,7 @@ func (uuo *UserUpdateOne) Select(field string, fields ...string) *UserUpdateOne // Save executes the query and returns the updated User entity. func (uuo *UserUpdateOne) Save(ctx context.Context) (*User, error) { - var ( - err error - node *User - ) - if len(uuo.hooks) == 0 { - node, err = uuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - uuo.mutation = mutation - node, err = uuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(uuo.hooks) - 1; i >= 0; i-- { - if uuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*User) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) - } - node = nv - } - return node, err + return withHooks[*User, UserMutation](ctx, uuo.sqlSave, uuo.mutation, uuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -2049,5 +1990,6 @@ func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) } return nil, err } + uuo.mutation.done = true return _node, nil } diff --git a/entc/integration/edgeschema/ent/usergroup_create.go b/entc/integration/edgeschema/ent/usergroup_create.go index cff5beb47..5118696fb 100644 --- a/entc/integration/edgeschema/ent/usergroup_create.go +++ b/entc/integration/edgeschema/ent/usergroup_create.go @@ -71,50 +71,8 @@ func (ugc *UserGroupCreate) Mutation() *UserGroupMutation { // Save creates the UserGroup in the database. func (ugc *UserGroupCreate) Save(ctx context.Context) (*UserGroup, error) { - var ( - err error - node *UserGroup - ) ugc.defaults() - if len(ugc.hooks) == 0 { - if err = ugc.check(); err != nil { - return nil, err - } - node, err = ugc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserGroupMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = ugc.check(); err != nil { - return nil, err - } - ugc.mutation = mutation - if node, err = ugc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(ugc.hooks) - 1; i >= 0; i-- { - if ugc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ugc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, ugc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*UserGroup) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserGroupMutation", v) - } - node = nv - } - return node, err + return withHooks[*UserGroup, UserGroupMutation](ctx, ugc.sqlSave, ugc.mutation, ugc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -168,6 +126,9 @@ func (ugc *UserGroupCreate) check() error { } func (ugc *UserGroupCreate) sqlSave(ctx context.Context) (*UserGroup, error) { + if err := ugc.check(); err != nil { + return nil, err + } _node, _spec := ugc.createSpec() if err := sqlgraph.CreateNode(ctx, ugc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -177,6 +138,8 @@ func (ugc *UserGroupCreate) sqlSave(ctx context.Context) (*UserGroup, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + ugc.mutation.id = &_node.ID + ugc.mutation.done = true return _node, nil } diff --git a/entc/integration/edgeschema/ent/usergroup_delete.go b/entc/integration/edgeschema/ent/usergroup_delete.go index b50defe1c..6476ae0ef 100644 --- a/entc/integration/edgeschema/ent/usergroup_delete.go +++ b/entc/integration/edgeschema/ent/usergroup_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (ugd *UserGroupDelete) Where(ps ...predicate.UserGroup) *UserGroupDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (ugd *UserGroupDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(ugd.hooks) == 0 { - affected, err = ugd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserGroupMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - ugd.mutation = mutation - affected, err = ugd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(ugd.hooks) - 1; i >= 0; i-- { - if ugd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ugd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, ugd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, UserGroupMutation](ctx, ugd.sqlExec, ugd.mutation, ugd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (ugd *UserGroupDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + ugd.mutation.done = true return affected, err } diff --git a/entc/integration/edgeschema/ent/usergroup_update.go b/entc/integration/edgeschema/ent/usergroup_update.go index 49d9ec70a..466d29690 100644 --- a/entc/integration/edgeschema/ent/usergroup_update.go +++ b/entc/integration/edgeschema/ent/usergroup_update.go @@ -89,40 +89,7 @@ func (ugu *UserGroupUpdate) ClearGroup() *UserGroupUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (ugu *UserGroupUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(ugu.hooks) == 0 { - if err = ugu.check(); err != nil { - return 0, err - } - affected, err = ugu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserGroupMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = ugu.check(); err != nil { - return 0, err - } - ugu.mutation = mutation - affected, err = ugu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(ugu.hooks) - 1; i >= 0; i-- { - if ugu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ugu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, ugu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, UserGroupMutation](ctx, ugu.sqlSave, ugu.mutation, ugu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -159,6 +126,9 @@ func (ugu *UserGroupUpdate) check() error { } func (ugu *UserGroupUpdate) sqlSave(ctx context.Context) (n int, err error) { + if err := ugu.check(); err != nil { + return n, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: usergroup.Table, @@ -257,6 +227,7 @@ func (ugu *UserGroupUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + ugu.mutation.done = true return n, nil } @@ -330,46 +301,7 @@ func (uguo *UserGroupUpdateOne) Select(field string, fields ...string) *UserGrou // Save executes the query and returns the updated UserGroup entity. func (uguo *UserGroupUpdateOne) Save(ctx context.Context) (*UserGroup, error) { - var ( - err error - node *UserGroup - ) - if len(uguo.hooks) == 0 { - if err = uguo.check(); err != nil { - return nil, err - } - node, err = uguo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserGroupMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = uguo.check(); err != nil { - return nil, err - } - uguo.mutation = mutation - node, err = uguo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(uguo.hooks) - 1; i >= 0; i-- { - if uguo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uguo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uguo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*UserGroup) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserGroupMutation", v) - } - node = nv - } - return node, err + return withHooks[*UserGroup, UserGroupMutation](ctx, uguo.sqlSave, uguo.mutation, uguo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -406,6 +338,9 @@ func (uguo *UserGroupUpdateOne) check() error { } func (uguo *UserGroupUpdateOne) sqlSave(ctx context.Context) (_node *UserGroup, err error) { + if err := uguo.check(); err != nil { + return _node, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: usergroup.Table, @@ -524,5 +459,6 @@ func (uguo *UserGroupUpdateOne) sqlSave(ctx context.Context) (_node *UserGroup, } return nil, err } + uguo.mutation.done = true return _node, nil } diff --git a/entc/integration/edgeschema/ent/usertweet_create.go b/entc/integration/edgeschema/ent/usertweet_create.go index 0e6388fc9..c9a4e6184 100644 --- a/entc/integration/edgeschema/ent/usertweet_create.go +++ b/entc/integration/edgeschema/ent/usertweet_create.go @@ -71,50 +71,8 @@ func (utc *UserTweetCreate) Mutation() *UserTweetMutation { // Save creates the UserTweet in the database. func (utc *UserTweetCreate) Save(ctx context.Context) (*UserTweet, error) { - var ( - err error - node *UserTweet - ) utc.defaults() - if len(utc.hooks) == 0 { - if err = utc.check(); err != nil { - return nil, err - } - node, err = utc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserTweetMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = utc.check(); err != nil { - return nil, err - } - utc.mutation = mutation - if node, err = utc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(utc.hooks) - 1; i >= 0; i-- { - if utc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = utc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, utc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*UserTweet) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserTweetMutation", v) - } - node = nv - } - return node, err + return withHooks[*UserTweet, UserTweetMutation](ctx, utc.sqlSave, utc.mutation, utc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -168,6 +126,9 @@ func (utc *UserTweetCreate) check() error { } func (utc *UserTweetCreate) sqlSave(ctx context.Context) (*UserTweet, error) { + if err := utc.check(); err != nil { + return nil, err + } _node, _spec := utc.createSpec() if err := sqlgraph.CreateNode(ctx, utc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -177,6 +138,8 @@ func (utc *UserTweetCreate) sqlSave(ctx context.Context) (*UserTweet, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + utc.mutation.id = &_node.ID + utc.mutation.done = true return _node, nil } diff --git a/entc/integration/edgeschema/ent/usertweet_delete.go b/entc/integration/edgeschema/ent/usertweet_delete.go index 1436c4cbe..e78e49a7c 100644 --- a/entc/integration/edgeschema/ent/usertweet_delete.go +++ b/entc/integration/edgeschema/ent/usertweet_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (utd *UserTweetDelete) Where(ps ...predicate.UserTweet) *UserTweetDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (utd *UserTweetDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(utd.hooks) == 0 { - affected, err = utd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserTweetMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - utd.mutation = mutation - affected, err = utd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(utd.hooks) - 1; i >= 0; i-- { - if utd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = utd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, utd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, UserTweetMutation](ctx, utd.sqlExec, utd.mutation, utd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (utd *UserTweetDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + utd.mutation.done = true return affected, err } diff --git a/entc/integration/edgeschema/ent/usertweet_update.go b/entc/integration/edgeschema/ent/usertweet_update.go index 21ac9db68..6622bfa24 100644 --- a/entc/integration/edgeschema/ent/usertweet_update.go +++ b/entc/integration/edgeschema/ent/usertweet_update.go @@ -89,40 +89,7 @@ func (utu *UserTweetUpdate) ClearTweet() *UserTweetUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (utu *UserTweetUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(utu.hooks) == 0 { - if err = utu.check(); err != nil { - return 0, err - } - affected, err = utu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserTweetMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = utu.check(); err != nil { - return 0, err - } - utu.mutation = mutation - affected, err = utu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(utu.hooks) - 1; i >= 0; i-- { - if utu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = utu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, utu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, UserTweetMutation](ctx, utu.sqlSave, utu.mutation, utu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -159,6 +126,9 @@ func (utu *UserTweetUpdate) check() error { } func (utu *UserTweetUpdate) sqlSave(ctx context.Context) (n int, err error) { + if err := utu.check(); err != nil { + return n, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: usertweet.Table, @@ -257,6 +227,7 @@ func (utu *UserTweetUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + utu.mutation.done = true return n, nil } @@ -330,46 +301,7 @@ func (utuo *UserTweetUpdateOne) Select(field string, fields ...string) *UserTwee // Save executes the query and returns the updated UserTweet entity. func (utuo *UserTweetUpdateOne) Save(ctx context.Context) (*UserTweet, error) { - var ( - err error - node *UserTweet - ) - if len(utuo.hooks) == 0 { - if err = utuo.check(); err != nil { - return nil, err - } - node, err = utuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserTweetMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = utuo.check(); err != nil { - return nil, err - } - utuo.mutation = mutation - node, err = utuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(utuo.hooks) - 1; i >= 0; i-- { - if utuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = utuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, utuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*UserTweet) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserTweetMutation", v) - } - node = nv - } - return node, err + return withHooks[*UserTweet, UserTweetMutation](ctx, utuo.sqlSave, utuo.mutation, utuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -406,6 +338,9 @@ func (utuo *UserTweetUpdateOne) check() error { } func (utuo *UserTweetUpdateOne) sqlSave(ctx context.Context) (_node *UserTweet, err error) { + if err := utuo.check(); err != nil { + return _node, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: usertweet.Table, @@ -524,5 +459,6 @@ func (utuo *UserTweetUpdateOne) sqlSave(ctx context.Context) (_node *UserTweet, } return nil, err } + utuo.mutation.done = true return _node, nil } diff --git a/entc/integration/ent/card_create.go b/entc/integration/ent/card_create.go index f48af9811..bef7243a6 100644 --- a/entc/integration/ent/card_create.go +++ b/entc/integration/ent/card_create.go @@ -131,50 +131,8 @@ func (cc *CardCreate) Mutation() *CardMutation { // Save creates the Card in the database. func (cc *CardCreate) Save(ctx context.Context) (*Card, error) { - var ( - err error - node *Card - ) cc.defaults() - if len(cc.hooks) == 0 { - if err = cc.check(); err != nil { - return nil, err - } - node, err = cc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CardMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = cc.check(); err != nil { - return nil, err - } - cc.mutation = mutation - if node, err = cc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(cc.hooks) - 1; i >= 0; i-- { - if cc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = cc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, cc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Card) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from CardMutation", v) - } - node = nv - } - return node, err + return withHooks[*Card, CardMutation](ctx, cc.sqlSave, cc.mutation, cc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -243,6 +201,9 @@ func (cc *CardCreate) check() error { } func (cc *CardCreate) sqlSave(ctx context.Context) (*Card, error) { + if err := cc.check(); err != nil { + return nil, err + } _node, _spec := cc.createSpec() if err := sqlgraph.CreateNode(ctx, cc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -252,6 +213,8 @@ func (cc *CardCreate) sqlSave(ctx context.Context) (*Card, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + cc.mutation.id = &_node.ID + cc.mutation.done = true return _node, nil } diff --git a/entc/integration/ent/card_delete.go b/entc/integration/ent/card_delete.go index 21c7a125f..92fc3611f 100644 --- a/entc/integration/ent/card_delete.go +++ b/entc/integration/ent/card_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (cd *CardDelete) Where(ps ...predicate.Card) *CardDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (cd *CardDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(cd.hooks) == 0 { - affected, err = cd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CardMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - cd.mutation = mutation - affected, err = cd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(cd.hooks) - 1; i >= 0; i-- { - if cd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = cd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, cd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, CardMutation](ctx, cd.sqlExec, cd.mutation, cd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (cd *CardDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + cd.mutation.done = true return affected, err } diff --git a/entc/integration/ent/card_update.go b/entc/integration/ent/card_update.go index 879eb6fb5..d8db1fb9e 100644 --- a/entc/integration/ent/card_update.go +++ b/entc/integration/ent/card_update.go @@ -150,41 +150,8 @@ func (cu *CardUpdate) RemoveSpec(s ...*Spec) *CardUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (cu *CardUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) cu.defaults() - if len(cu.hooks) == 0 { - if err = cu.check(); err != nil { - return 0, err - } - affected, err = cu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CardMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = cu.check(); err != nil { - return 0, err - } - cu.mutation = mutation - affected, err = cu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(cu.hooks) - 1; i >= 0; i-- { - if cu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = cu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, cu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, CardMutation](ctx, cu.sqlSave, cu.mutation, cu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -234,6 +201,9 @@ func (cu *CardUpdate) Modify(modifiers ...func(u *sql.UpdateBuilder)) *CardUpdat } func (cu *CardUpdate) sqlSave(ctx context.Context) (n int, err error) { + if err := cu.check(); err != nil { + return n, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: card.Table, @@ -364,6 +334,7 @@ func (cu *CardUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + cu.mutation.done = true return n, nil } @@ -498,47 +469,8 @@ func (cuo *CardUpdateOne) Select(field string, fields ...string) *CardUpdateOne // Save executes the query and returns the updated Card entity. func (cuo *CardUpdateOne) Save(ctx context.Context) (*Card, error) { - var ( - err error - node *Card - ) cuo.defaults() - if len(cuo.hooks) == 0 { - if err = cuo.check(); err != nil { - return nil, err - } - node, err = cuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CardMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = cuo.check(); err != nil { - return nil, err - } - cuo.mutation = mutation - node, err = cuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(cuo.hooks) - 1; i >= 0; i-- { - if cuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = cuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, cuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Card) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from CardMutation", v) - } - node = nv - } - return node, err + return withHooks[*Card, CardMutation](ctx, cuo.sqlSave, cuo.mutation, cuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -588,6 +520,9 @@ func (cuo *CardUpdateOne) Modify(modifiers ...func(u *sql.UpdateBuilder)) *CardU } func (cuo *CardUpdateOne) sqlSave(ctx context.Context) (_node *Card, err error) { + if err := cuo.check(); err != nil { + return _node, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: card.Table, @@ -738,5 +673,6 @@ func (cuo *CardUpdateOne) sqlSave(ctx context.Context) (_node *Card, err error) } return nil, err } + cuo.mutation.done = true return _node, nil } diff --git a/entc/integration/ent/comment_create.go b/entc/integration/ent/comment_create.go index 61dad9990..f389c19e0 100644 --- a/entc/integration/ent/comment_create.go +++ b/entc/integration/ent/comment_create.go @@ -101,49 +101,7 @@ func (cc *CommentCreate) Mutation() *CommentMutation { // Save creates the Comment in the database. func (cc *CommentCreate) Save(ctx context.Context) (*Comment, error) { - var ( - err error - node *Comment - ) - if len(cc.hooks) == 0 { - if err = cc.check(); err != nil { - return nil, err - } - node, err = cc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CommentMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = cc.check(); err != nil { - return nil, err - } - cc.mutation = mutation - if node, err = cc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(cc.hooks) - 1; i >= 0; i-- { - if cc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = cc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, cc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Comment) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from CommentMutation", v) - } - node = nv - } - return node, err + return withHooks[*Comment, CommentMutation](ctx, cc.sqlSave, cc.mutation, cc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -180,6 +138,9 @@ func (cc *CommentCreate) check() error { } func (cc *CommentCreate) sqlSave(ctx context.Context) (*Comment, error) { + if err := cc.check(); err != nil { + return nil, err + } _node, _spec := cc.createSpec() if err := sqlgraph.CreateNode(ctx, cc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -189,6 +150,8 @@ func (cc *CommentCreate) sqlSave(ctx context.Context) (*Comment, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + cc.mutation.id = &_node.ID + cc.mutation.done = true return _node, nil } diff --git a/entc/integration/ent/comment_delete.go b/entc/integration/ent/comment_delete.go index 9618e5b2e..451680d34 100644 --- a/entc/integration/ent/comment_delete.go +++ b/entc/integration/ent/comment_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (cd *CommentDelete) Where(ps ...predicate.Comment) *CommentDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (cd *CommentDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(cd.hooks) == 0 { - affected, err = cd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CommentMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - cd.mutation = mutation - affected, err = cd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(cd.hooks) - 1; i >= 0; i-- { - if cd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = cd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, cd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, CommentMutation](ctx, cd.sqlExec, cd.mutation, cd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (cd *CommentDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + cd.mutation.done = true return affected, err } diff --git a/entc/integration/ent/comment_update.go b/entc/integration/ent/comment_update.go index 117ae631d..e1e23f7eb 100644 --- a/entc/integration/ent/comment_update.go +++ b/entc/integration/ent/comment_update.go @@ -153,34 +153,7 @@ func (cu *CommentUpdate) Mutation() *CommentMutation { // Save executes the query and returns the number of nodes affected by the update operation. func (cu *CommentUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(cu.hooks) == 0 { - affected, err = cu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CommentMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - cu.mutation = mutation - affected, err = cu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(cu.hooks) - 1; i >= 0; i-- { - if cu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = cu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, cu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, CommentMutation](ctx, cu.sqlSave, cu.mutation, cu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -277,6 +250,7 @@ func (cu *CommentUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + cu.mutation.done = true return n, nil } @@ -416,40 +390,7 @@ func (cuo *CommentUpdateOne) Select(field string, fields ...string) *CommentUpda // Save executes the query and returns the updated Comment entity. func (cuo *CommentUpdateOne) Save(ctx context.Context) (*Comment, error) { - var ( - err error - node *Comment - ) - if len(cuo.hooks) == 0 { - node, err = cuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CommentMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - cuo.mutation = mutation - node, err = cuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(cuo.hooks) - 1; i >= 0; i-- { - if cuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = cuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, cuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Comment) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from CommentMutation", v) - } - node = nv - } - return node, err + return withHooks[*Comment, CommentMutation](ctx, cuo.sqlSave, cuo.mutation, cuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -566,5 +507,6 @@ func (cuo *CommentUpdateOne) sqlSave(ctx context.Context) (_node *Comment, err e } return nil, err } + cuo.mutation.done = true return _node, nil } diff --git a/entc/integration/ent/ent.go b/entc/integration/ent/ent.go index 7651658f4..b9ed69b80 100644 --- a/entc/integration/ent/ent.go +++ b/entc/integration/ent/ent.go @@ -501,6 +501,40 @@ func (s *selector) BoolX(ctx context.Context) bool { return v } +// withHooks invokes the builder operation with the given hooks, if any. +func withHooks[V Value, M any, PM interface { + *M + Mutation +}](ctx context.Context, exec func(context.Context) (V, error), mutation PM, hooks []Hook) (value V, err error) { + if len(hooks) == 0 { + return exec(ctx) + } + var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { + mutationT, ok := m.(PM) + if !ok { + return nil, fmt.Errorf("unexpected mutation type %T", m) + } + // Set the mutation to the builder. + *mutation = *mutationT + return exec(ctx) + }) + for i := len(hooks) - 1; i >= 0; i-- { + if hooks[i] == nil { + return value, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") + } + mut = hooks[i](mut) + } + v, err := mut.Mutate(ctx, mutation) + if err != nil { + return value, err + } + nv, ok := v.(V) + if !ok { + return value, fmt.Errorf("unexpected node type %T returned from %T", v, mutation) + } + return nv, nil +} + // newQueryContext returns a new context with the given QueryContext attached in case it does not exist. func newQueryContext(ctx context.Context, typ, op string) context.Context { if ent.QueryFromContext(ctx) == nil { diff --git a/entc/integration/ent/fieldtype_create.go b/entc/integration/ent/fieldtype_create.go index d9cc36b5d..92ada286f 100644 --- a/entc/integration/ent/fieldtype_create.go +++ b/entc/integration/ent/fieldtype_create.go @@ -796,50 +796,8 @@ func (ftc *FieldTypeCreate) Mutation() *FieldTypeMutation { // Save creates the FieldType in the database. func (ftc *FieldTypeCreate) Save(ctx context.Context) (*FieldType, error) { - var ( - err error - node *FieldType - ) ftc.defaults() - if len(ftc.hooks) == 0 { - if err = ftc.check(); err != nil { - return nil, err - } - node, err = ftc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*FieldTypeMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = ftc.check(); err != nil { - return nil, err - } - ftc.mutation = mutation - if node, err = ftc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(ftc.hooks) - 1; i >= 0; i-- { - if ftc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ftc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, ftc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*FieldType) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from FieldTypeMutation", v) - } - node = nv - } - return node, err + return withHooks[*FieldType, FieldTypeMutation](ctx, ftc.sqlSave, ftc.mutation, ftc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -993,6 +951,9 @@ func (ftc *FieldTypeCreate) check() error { } func (ftc *FieldTypeCreate) sqlSave(ctx context.Context) (*FieldType, error) { + if err := ftc.check(); err != nil { + return nil, err + } _node, _spec := ftc.createSpec() if err := sqlgraph.CreateNode(ctx, ftc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -1002,6 +963,8 @@ func (ftc *FieldTypeCreate) sqlSave(ctx context.Context) (*FieldType, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + ftc.mutation.id = &_node.ID + ftc.mutation.done = true return _node, nil } diff --git a/entc/integration/ent/fieldtype_delete.go b/entc/integration/ent/fieldtype_delete.go index e49590424..8a03c46da 100644 --- a/entc/integration/ent/fieldtype_delete.go +++ b/entc/integration/ent/fieldtype_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (ftd *FieldTypeDelete) Where(ps ...predicate.FieldType) *FieldTypeDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (ftd *FieldTypeDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(ftd.hooks) == 0 { - affected, err = ftd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*FieldTypeMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - ftd.mutation = mutation - affected, err = ftd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(ftd.hooks) - 1; i >= 0; i-- { - if ftd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ftd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, ftd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, FieldTypeMutation](ctx, ftd.sqlExec, ftd.mutation, ftd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (ftd *FieldTypeDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + ftd.mutation.done = true return affected, err } diff --git a/entc/integration/ent/fieldtype_update.go b/entc/integration/ent/fieldtype_update.go index b9454ad73..f40c6908e 100644 --- a/entc/integration/ent/fieldtype_update.go +++ b/entc/integration/ent/fieldtype_update.go @@ -1349,41 +1349,8 @@ func (ftu *FieldTypeUpdate) Mutation() *FieldTypeMutation { // Save executes the query and returns the number of nodes affected by the update operation. func (ftu *FieldTypeUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) ftu.defaults() - if len(ftu.hooks) == 0 { - if err = ftu.check(); err != nil { - return 0, err - } - affected, err = ftu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*FieldTypeMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = ftu.check(); err != nil { - return 0, err - } - ftu.mutation = mutation - affected, err = ftu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(ftu.hooks) - 1; i >= 0; i-- { - if ftu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ftu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, ftu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, FieldTypeMutation](ctx, ftu.sqlSave, ftu.mutation, ftu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -1481,6 +1448,9 @@ func (ftu *FieldTypeUpdate) Modify(modifiers ...func(u *sql.UpdateBuilder)) *Fie } func (ftu *FieldTypeUpdate) sqlSave(ctx context.Context) (n int, err error) { + if err := ftu.check(); err != nil { + return n, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: fieldtype.Table, @@ -1965,6 +1935,7 @@ func (ftu *FieldTypeUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + ftu.mutation.done = true return n, nil } @@ -3294,47 +3265,8 @@ func (ftuo *FieldTypeUpdateOne) Select(field string, fields ...string) *FieldTyp // Save executes the query and returns the updated FieldType entity. func (ftuo *FieldTypeUpdateOne) Save(ctx context.Context) (*FieldType, error) { - var ( - err error - node *FieldType - ) ftuo.defaults() - if len(ftuo.hooks) == 0 { - if err = ftuo.check(); err != nil { - return nil, err - } - node, err = ftuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*FieldTypeMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = ftuo.check(); err != nil { - return nil, err - } - ftuo.mutation = mutation - node, err = ftuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(ftuo.hooks) - 1; i >= 0; i-- { - if ftuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ftuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, ftuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*FieldType) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from FieldTypeMutation", v) - } - node = nv - } - return node, err + return withHooks[*FieldType, FieldTypeMutation](ctx, ftuo.sqlSave, ftuo.mutation, ftuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -3432,6 +3364,9 @@ func (ftuo *FieldTypeUpdateOne) Modify(modifiers ...func(u *sql.UpdateBuilder)) } func (ftuo *FieldTypeUpdateOne) sqlSave(ctx context.Context) (_node *FieldType, err error) { + if err := ftuo.check(); err != nil { + return _node, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: fieldtype.Table, @@ -3936,5 +3871,6 @@ func (ftuo *FieldTypeUpdateOne) sqlSave(ctx context.Context) (_node *FieldType, } return nil, err } + ftuo.mutation.done = true return _node, nil } diff --git a/entc/integration/ent/file_create.go b/entc/integration/ent/file_create.go index f6b020538..6e9ba5cd7 100644 --- a/entc/integration/ent/file_create.go +++ b/entc/integration/ent/file_create.go @@ -164,50 +164,8 @@ func (fc *FileCreate) Mutation() *FileMutation { // Save creates the File in the database. func (fc *FileCreate) Save(ctx context.Context) (*File, error) { - var ( - err error - node *File - ) fc.defaults() - if len(fc.hooks) == 0 { - if err = fc.check(); err != nil { - return nil, err - } - node, err = fc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*FileMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = fc.check(); err != nil { - return nil, err - } - fc.mutation = mutation - if node, err = fc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(fc.hooks) - 1; i >= 0; i-- { - if fc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = fc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, fc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*File) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from FileMutation", v) - } - node = nv - } - return node, err + return withHooks[*File, FileMutation](ctx, fc.sqlSave, fc.mutation, fc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -257,6 +215,9 @@ func (fc *FileCreate) check() error { } func (fc *FileCreate) sqlSave(ctx context.Context) (*File, error) { + if err := fc.check(); err != nil { + return nil, err + } _node, _spec := fc.createSpec() if err := sqlgraph.CreateNode(ctx, fc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -266,6 +227,8 @@ func (fc *FileCreate) sqlSave(ctx context.Context) (*File, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + fc.mutation.id = &_node.ID + fc.mutation.done = true return _node, nil } diff --git a/entc/integration/ent/file_delete.go b/entc/integration/ent/file_delete.go index 242e2cbad..71f8343ee 100644 --- a/entc/integration/ent/file_delete.go +++ b/entc/integration/ent/file_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (fd *FileDelete) Where(ps ...predicate.File) *FileDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (fd *FileDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(fd.hooks) == 0 { - affected, err = fd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*FileMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - fd.mutation = mutation - affected, err = fd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(fd.hooks) - 1; i >= 0; i-- { - if fd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = fd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, fd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, FileMutation](ctx, fd.sqlExec, fd.mutation, fd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (fd *FileDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + fd.mutation.done = true return affected, err } diff --git a/entc/integration/ent/file_update.go b/entc/integration/ent/file_update.go index 0323da4ed..3823176f6 100644 --- a/entc/integration/ent/file_update.go +++ b/entc/integration/ent/file_update.go @@ -242,40 +242,7 @@ func (fu *FileUpdate) RemoveField(f ...*FieldType) *FileUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (fu *FileUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(fu.hooks) == 0 { - if err = fu.check(); err != nil { - return 0, err - } - affected, err = fu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*FileMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = fu.check(); err != nil { - return 0, err - } - fu.mutation = mutation - affected, err = fu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(fu.hooks) - 1; i >= 0; i-- { - if fu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = fu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, fu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, FileMutation](ctx, fu.sqlSave, fu.mutation, fu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -317,6 +284,9 @@ func (fu *FileUpdate) Modify(modifiers ...func(u *sql.UpdateBuilder)) *FileUpdat } func (fu *FileUpdate) sqlSave(ctx context.Context) (n int, err error) { + if err := fu.check(); err != nil { + return n, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: file.Table, @@ -503,6 +473,7 @@ func (fu *FileUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + fu.mutation.done = true return n, nil } @@ -729,46 +700,7 @@ func (fuo *FileUpdateOne) Select(field string, fields ...string) *FileUpdateOne // Save executes the query and returns the updated File entity. func (fuo *FileUpdateOne) Save(ctx context.Context) (*File, error) { - var ( - err error - node *File - ) - if len(fuo.hooks) == 0 { - if err = fuo.check(); err != nil { - return nil, err - } - node, err = fuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*FileMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = fuo.check(); err != nil { - return nil, err - } - fuo.mutation = mutation - node, err = fuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(fuo.hooks) - 1; i >= 0; i-- { - if fuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = fuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, fuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*File) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from FileMutation", v) - } - node = nv - } - return node, err + return withHooks[*File, FileMutation](ctx, fuo.sqlSave, fuo.mutation, fuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -810,6 +742,9 @@ func (fuo *FileUpdateOne) Modify(modifiers ...func(u *sql.UpdateBuilder)) *FileU } func (fuo *FileUpdateOne) sqlSave(ctx context.Context) (_node *File, err error) { + if err := fuo.check(); err != nil { + return _node, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: file.Table, @@ -1016,5 +951,6 @@ func (fuo *FileUpdateOne) sqlSave(ctx context.Context) (_node *File, err error) } return nil, err } + fuo.mutation.done = true return _node, nil } diff --git a/entc/integration/ent/filetype_create.go b/entc/integration/ent/filetype_create.go index a484cff0b..3061fcfcd 100644 --- a/entc/integration/ent/filetype_create.go +++ b/entc/integration/ent/filetype_create.go @@ -82,50 +82,8 @@ func (ftc *FileTypeCreate) Mutation() *FileTypeMutation { // Save creates the FileType in the database. func (ftc *FileTypeCreate) Save(ctx context.Context) (*FileType, error) { - var ( - err error - node *FileType - ) ftc.defaults() - if len(ftc.hooks) == 0 { - if err = ftc.check(); err != nil { - return nil, err - } - node, err = ftc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*FileTypeMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = ftc.check(); err != nil { - return nil, err - } - ftc.mutation = mutation - if node, err = ftc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(ftc.hooks) - 1; i >= 0; i-- { - if ftc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ftc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, ftc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*FileType) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from FileTypeMutation", v) - } - node = nv - } - return node, err + return withHooks[*FileType, FileTypeMutation](ctx, ftc.sqlSave, ftc.mutation, ftc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -187,6 +145,9 @@ func (ftc *FileTypeCreate) check() error { } func (ftc *FileTypeCreate) sqlSave(ctx context.Context) (*FileType, error) { + if err := ftc.check(); err != nil { + return nil, err + } _node, _spec := ftc.createSpec() if err := sqlgraph.CreateNode(ctx, ftc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -196,6 +157,8 @@ func (ftc *FileTypeCreate) sqlSave(ctx context.Context) (*FileType, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + ftc.mutation.id = &_node.ID + ftc.mutation.done = true return _node, nil } diff --git a/entc/integration/ent/filetype_delete.go b/entc/integration/ent/filetype_delete.go index fffee9807..432e89218 100644 --- a/entc/integration/ent/filetype_delete.go +++ b/entc/integration/ent/filetype_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (ftd *FileTypeDelete) Where(ps ...predicate.FileType) *FileTypeDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (ftd *FileTypeDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(ftd.hooks) == 0 { - affected, err = ftd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*FileTypeMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - ftd.mutation = mutation - affected, err = ftd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(ftd.hooks) - 1; i >= 0; i-- { - if ftd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ftd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, ftd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, FileTypeMutation](ctx, ftd.sqlExec, ftd.mutation, ftd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (ftd *FileTypeDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + ftd.mutation.done = true return affected, err } diff --git a/entc/integration/ent/filetype_update.go b/entc/integration/ent/filetype_update.go index 8a024cf09..b4b216222 100644 --- a/entc/integration/ent/filetype_update.go +++ b/entc/integration/ent/filetype_update.go @@ -110,40 +110,7 @@ func (ftu *FileTypeUpdate) RemoveFiles(f ...*File) *FileTypeUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (ftu *FileTypeUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(ftu.hooks) == 0 { - if err = ftu.check(); err != nil { - return 0, err - } - affected, err = ftu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*FileTypeMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = ftu.check(); err != nil { - return 0, err - } - ftu.mutation = mutation - affected, err = ftu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(ftu.hooks) - 1; i >= 0; i-- { - if ftu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ftu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, ftu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, FileTypeMutation](ctx, ftu.sqlSave, ftu.mutation, ftu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -190,6 +157,9 @@ func (ftu *FileTypeUpdate) Modify(modifiers ...func(u *sql.UpdateBuilder)) *File } func (ftu *FileTypeUpdate) sqlSave(ctx context.Context) (n int, err error) { + if err := ftu.check(); err != nil { + return n, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: filetype.Table, @@ -279,6 +249,7 @@ func (ftu *FileTypeUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + ftu.mutation.done = true return n, nil } @@ -375,46 +346,7 @@ func (ftuo *FileTypeUpdateOne) Select(field string, fields ...string) *FileTypeU // Save executes the query and returns the updated FileType entity. func (ftuo *FileTypeUpdateOne) Save(ctx context.Context) (*FileType, error) { - var ( - err error - node *FileType - ) - if len(ftuo.hooks) == 0 { - if err = ftuo.check(); err != nil { - return nil, err - } - node, err = ftuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*FileTypeMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = ftuo.check(); err != nil { - return nil, err - } - ftuo.mutation = mutation - node, err = ftuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(ftuo.hooks) - 1; i >= 0; i-- { - if ftuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ftuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, ftuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*FileType) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from FileTypeMutation", v) - } - node = nv - } - return node, err + return withHooks[*FileType, FileTypeMutation](ctx, ftuo.sqlSave, ftuo.mutation, ftuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -461,6 +393,9 @@ func (ftuo *FileTypeUpdateOne) Modify(modifiers ...func(u *sql.UpdateBuilder)) * } func (ftuo *FileTypeUpdateOne) sqlSave(ctx context.Context) (_node *FileType, err error) { + if err := ftuo.check(); err != nil { + return _node, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: filetype.Table, @@ -570,5 +505,6 @@ func (ftuo *FileTypeUpdateOne) sqlSave(ctx context.Context) (_node *FileType, er } return nil, err } + ftuo.mutation.done = true return _node, nil } diff --git a/entc/integration/ent/goods_create.go b/entc/integration/ent/goods_create.go index aa0a4acdc..705ff40b3 100644 --- a/entc/integration/ent/goods_create.go +++ b/entc/integration/ent/goods_create.go @@ -32,49 +32,7 @@ func (gc *GoodsCreate) Mutation() *GoodsMutation { // Save creates the Goods in the database. func (gc *GoodsCreate) Save(ctx context.Context) (*Goods, error) { - var ( - err error - node *Goods - ) - if len(gc.hooks) == 0 { - if err = gc.check(); err != nil { - return nil, err - } - node, err = gc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GoodsMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = gc.check(); err != nil { - return nil, err - } - gc.mutation = mutation - if node, err = gc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(gc.hooks) - 1; i >= 0; i-- { - if gc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = gc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, gc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Goods) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from GoodsMutation", v) - } - node = nv - } - return node, err + return withHooks[*Goods, GoodsMutation](ctx, gc.sqlSave, gc.mutation, gc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -105,6 +63,9 @@ func (gc *GoodsCreate) check() error { } func (gc *GoodsCreate) sqlSave(ctx context.Context) (*Goods, error) { + if err := gc.check(); err != nil { + return nil, err + } _node, _spec := gc.createSpec() if err := sqlgraph.CreateNode(ctx, gc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -114,6 +75,8 @@ func (gc *GoodsCreate) sqlSave(ctx context.Context) (*Goods, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + gc.mutation.id = &_node.ID + gc.mutation.done = true return _node, nil } diff --git a/entc/integration/ent/goods_delete.go b/entc/integration/ent/goods_delete.go index 8a478072b..24a1daca3 100644 --- a/entc/integration/ent/goods_delete.go +++ b/entc/integration/ent/goods_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (gd *GoodsDelete) Where(ps ...predicate.Goods) *GoodsDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (gd *GoodsDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(gd.hooks) == 0 { - affected, err = gd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GoodsMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - gd.mutation = mutation - affected, err = gd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(gd.hooks) - 1; i >= 0; i-- { - if gd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = gd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, gd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, GoodsMutation](ctx, gd.sqlExec, gd.mutation, gd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (gd *GoodsDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + gd.mutation.done = true return affected, err } diff --git a/entc/integration/ent/goods_update.go b/entc/integration/ent/goods_update.go index 9f97eeb34..042cfbb39 100644 --- a/entc/integration/ent/goods_update.go +++ b/entc/integration/ent/goods_update.go @@ -39,34 +39,7 @@ func (gu *GoodsUpdate) Mutation() *GoodsMutation { // Save executes the query and returns the number of nodes affected by the update operation. func (gu *GoodsUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(gu.hooks) == 0 { - affected, err = gu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GoodsMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - gu.mutation = mutation - affected, err = gu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(gu.hooks) - 1; i >= 0; i-- { - if gu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = gu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, gu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, GoodsMutation](ctx, gu.sqlSave, gu.mutation, gu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -124,6 +97,7 @@ func (gu *GoodsUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + gu.mutation.done = true return n, nil } @@ -150,40 +124,7 @@ func (guo *GoodsUpdateOne) Select(field string, fields ...string) *GoodsUpdateOn // Save executes the query and returns the updated Goods entity. func (guo *GoodsUpdateOne) Save(ctx context.Context) (*Goods, error) { - var ( - err error - node *Goods - ) - if len(guo.hooks) == 0 { - node, err = guo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GoodsMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - guo.mutation = mutation - node, err = guo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(guo.hooks) - 1; i >= 0; i-- { - if guo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = guo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, guo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Goods) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from GoodsMutation", v) - } - node = nv - } - return node, err + return withHooks[*Goods, GoodsMutation](ctx, guo.sqlSave, guo.mutation, guo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -261,5 +202,6 @@ func (guo *GoodsUpdateOne) sqlSave(ctx context.Context) (_node *Goods, err error } return nil, err } + guo.mutation.done = true return _node, nil } diff --git a/entc/integration/ent/group_create.go b/entc/integration/ent/group_create.go index aa01bda49..6648a7e8b 100644 --- a/entc/integration/ent/group_create.go +++ b/entc/integration/ent/group_create.go @@ -146,50 +146,8 @@ func (gc *GroupCreate) Mutation() *GroupMutation { // Save creates the Group in the database. func (gc *GroupCreate) Save(ctx context.Context) (*Group, error) { - var ( - err error - node *Group - ) gc.defaults() - if len(gc.hooks) == 0 { - if err = gc.check(); err != nil { - return nil, err - } - node, err = gc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GroupMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = gc.check(); err != nil { - return nil, err - } - gc.mutation = mutation - if node, err = gc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(gc.hooks) - 1; i >= 0; i-- { - if gc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = gc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, gc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Group) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from GroupMutation", v) - } - node = nv - } - return node, err + return withHooks[*Group, GroupMutation](ctx, gc.sqlSave, gc.mutation, gc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -259,6 +217,9 @@ func (gc *GroupCreate) check() error { } func (gc *GroupCreate) sqlSave(ctx context.Context) (*Group, error) { + if err := gc.check(); err != nil { + return nil, err + } _node, _spec := gc.createSpec() if err := sqlgraph.CreateNode(ctx, gc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -268,6 +229,8 @@ func (gc *GroupCreate) sqlSave(ctx context.Context) (*Group, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + gc.mutation.id = &_node.ID + gc.mutation.done = true return _node, nil } diff --git a/entc/integration/ent/group_delete.go b/entc/integration/ent/group_delete.go index 06846ed5b..4512a1495 100644 --- a/entc/integration/ent/group_delete.go +++ b/entc/integration/ent/group_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (gd *GroupDelete) Where(ps ...predicate.Group) *GroupDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (gd *GroupDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(gd.hooks) == 0 { - affected, err = gd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GroupMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - gd.mutation = mutation - affected, err = gd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(gd.hooks) - 1; i >= 0; i-- { - if gd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = gd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, gd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, GroupMutation](ctx, gd.sqlExec, gd.mutation, gd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (gd *GroupDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + gd.mutation.done = true return affected, err } diff --git a/entc/integration/ent/group_update.go b/entc/integration/ent/group_update.go index 13ad25c1f..96cd440a1 100644 --- a/entc/integration/ent/group_update.go +++ b/entc/integration/ent/group_update.go @@ -241,40 +241,7 @@ func (gu *GroupUpdate) ClearInfo() *GroupUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (gu *GroupUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(gu.hooks) == 0 { - if err = gu.check(); err != nil { - return 0, err - } - affected, err = gu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GroupMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = gu.check(); err != nil { - return 0, err - } - gu.mutation = mutation - affected, err = gu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(gu.hooks) - 1; i >= 0; i-- { - if gu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = gu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, gu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, GroupMutation](ctx, gu.sqlSave, gu.mutation, gu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -329,6 +296,9 @@ func (gu *GroupUpdate) Modify(modifiers ...func(u *sql.UpdateBuilder)) *GroupUpd } func (gu *GroupUpdate) sqlSave(ctx context.Context) (n int, err error) { + if err := gu.check(); err != nil { + return n, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: group.Table, @@ -576,6 +546,7 @@ func (gu *GroupUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + gu.mutation.done = true return n, nil } @@ -800,46 +771,7 @@ func (guo *GroupUpdateOne) Select(field string, fields ...string) *GroupUpdateOn // Save executes the query and returns the updated Group entity. func (guo *GroupUpdateOne) Save(ctx context.Context) (*Group, error) { - var ( - err error - node *Group - ) - if len(guo.hooks) == 0 { - if err = guo.check(); err != nil { - return nil, err - } - node, err = guo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GroupMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = guo.check(); err != nil { - return nil, err - } - guo.mutation = mutation - node, err = guo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(guo.hooks) - 1; i >= 0; i-- { - if guo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = guo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, guo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Group) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from GroupMutation", v) - } - node = nv - } - return node, err + return withHooks[*Group, GroupMutation](ctx, guo.sqlSave, guo.mutation, guo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -894,6 +826,9 @@ func (guo *GroupUpdateOne) Modify(modifiers ...func(u *sql.UpdateBuilder)) *Grou } func (guo *GroupUpdateOne) sqlSave(ctx context.Context) (_node *Group, err error) { + if err := guo.check(); err != nil { + return _node, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: group.Table, @@ -1161,5 +1096,6 @@ func (guo *GroupUpdateOne) sqlSave(ctx context.Context) (_node *Group, err error } return nil, err } + guo.mutation.done = true return _node, nil } diff --git a/entc/integration/ent/groupinfo_create.go b/entc/integration/ent/groupinfo_create.go index 39aebeb36..fe4f63a47 100644 --- a/entc/integration/ent/groupinfo_create.go +++ b/entc/integration/ent/groupinfo_create.go @@ -68,50 +68,8 @@ func (gic *GroupInfoCreate) Mutation() *GroupInfoMutation { // Save creates the GroupInfo in the database. func (gic *GroupInfoCreate) Save(ctx context.Context) (*GroupInfo, error) { - var ( - err error - node *GroupInfo - ) gic.defaults() - if len(gic.hooks) == 0 { - if err = gic.check(); err != nil { - return nil, err - } - node, err = gic.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GroupInfoMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = gic.check(); err != nil { - return nil, err - } - gic.mutation = mutation - if node, err = gic.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(gic.hooks) - 1; i >= 0; i-- { - if gic.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = gic.hooks[i](mut) - } - v, err := mut.Mutate(ctx, gic.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*GroupInfo) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from GroupInfoMutation", v) - } - node = nv - } - return node, err + return withHooks[*GroupInfo, GroupInfoMutation](ctx, gic.sqlSave, gic.mutation, gic.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -156,6 +114,9 @@ func (gic *GroupInfoCreate) check() error { } func (gic *GroupInfoCreate) sqlSave(ctx context.Context) (*GroupInfo, error) { + if err := gic.check(); err != nil { + return nil, err + } _node, _spec := gic.createSpec() if err := sqlgraph.CreateNode(ctx, gic.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -165,6 +126,8 @@ func (gic *GroupInfoCreate) sqlSave(ctx context.Context) (*GroupInfo, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + gic.mutation.id = &_node.ID + gic.mutation.done = true return _node, nil } diff --git a/entc/integration/ent/groupinfo_delete.go b/entc/integration/ent/groupinfo_delete.go index 39971d8ec..b8185ccca 100644 --- a/entc/integration/ent/groupinfo_delete.go +++ b/entc/integration/ent/groupinfo_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (gid *GroupInfoDelete) Where(ps ...predicate.GroupInfo) *GroupInfoDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (gid *GroupInfoDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(gid.hooks) == 0 { - affected, err = gid.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GroupInfoMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - gid.mutation = mutation - affected, err = gid.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(gid.hooks) - 1; i >= 0; i-- { - if gid.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = gid.hooks[i](mut) - } - n, err := mut.Mutate(ctx, gid.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, GroupInfoMutation](ctx, gid.sqlExec, gid.mutation, gid.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (gid *GroupInfoDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + gid.mutation.done = true return affected, err } diff --git a/entc/integration/ent/groupinfo_update.go b/entc/integration/ent/groupinfo_update.go index ce7646ac8..8b1306f06 100644 --- a/entc/integration/ent/groupinfo_update.go +++ b/entc/integration/ent/groupinfo_update.go @@ -103,34 +103,7 @@ func (giu *GroupInfoUpdate) RemoveGroups(g ...*Group) *GroupInfoUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (giu *GroupInfoUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(giu.hooks) == 0 { - affected, err = giu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GroupInfoMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - giu.mutation = mutation - affected, err = giu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(giu.hooks) - 1; i >= 0; i-- { - if giu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = giu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, giu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, GroupInfoMutation](ctx, giu.sqlSave, giu.mutation, giu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -251,6 +224,7 @@ func (giu *GroupInfoUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + giu.mutation.done = true return n, nil } @@ -340,40 +314,7 @@ func (giuo *GroupInfoUpdateOne) Select(field string, fields ...string) *GroupInf // Save executes the query and returns the updated GroupInfo entity. func (giuo *GroupInfoUpdateOne) Save(ctx context.Context) (*GroupInfo, error) { - var ( - err error - node *GroupInfo - ) - if len(giuo.hooks) == 0 { - node, err = giuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GroupInfoMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - giuo.mutation = mutation - node, err = giuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(giuo.hooks) - 1; i >= 0; i-- { - if giuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = giuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, giuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*GroupInfo) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from GroupInfoMutation", v) - } - node = nv - } - return node, err + return withHooks[*GroupInfo, GroupInfoMutation](ctx, giuo.sqlSave, giuo.mutation, giuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -514,5 +455,6 @@ func (giuo *GroupInfoUpdateOne) sqlSave(ctx context.Context) (_node *GroupInfo, } return nil, err } + giuo.mutation.done = true return _node, nil } diff --git a/entc/integration/ent/item_create.go b/entc/integration/ent/item_create.go index fb27fa7a0..bf8c41a5c 100644 --- a/entc/integration/ent/item_create.go +++ b/entc/integration/ent/item_create.go @@ -61,50 +61,8 @@ func (ic *ItemCreate) Mutation() *ItemMutation { // Save creates the Item in the database. func (ic *ItemCreate) Save(ctx context.Context) (*Item, error) { - var ( - err error - node *Item - ) ic.defaults() - if len(ic.hooks) == 0 { - if err = ic.check(); err != nil { - return nil, err - } - node, err = ic.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*ItemMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = ic.check(); err != nil { - return nil, err - } - ic.mutation = mutation - if node, err = ic.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(ic.hooks) - 1; i >= 0; i-- { - if ic.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ic.hooks[i](mut) - } - v, err := mut.Mutate(ctx, ic.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Item) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from ItemMutation", v) - } - node = nv - } - return node, err + return withHooks[*Item, ItemMutation](ctx, ic.sqlSave, ic.mutation, ic.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -153,6 +111,9 @@ func (ic *ItemCreate) check() error { } func (ic *ItemCreate) sqlSave(ctx context.Context) (*Item, error) { + if err := ic.check(); err != nil { + return nil, err + } _node, _spec := ic.createSpec() if err := sqlgraph.CreateNode(ctx, ic.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -167,6 +128,8 @@ func (ic *ItemCreate) sqlSave(ctx context.Context) (*Item, error) { return nil, fmt.Errorf("unexpected Item.ID type: %T", _spec.ID.Value) } } + ic.mutation.id = &_node.ID + ic.mutation.done = true return _node, nil } diff --git a/entc/integration/ent/item_delete.go b/entc/integration/ent/item_delete.go index ef97ef6d1..1f811c94b 100644 --- a/entc/integration/ent/item_delete.go +++ b/entc/integration/ent/item_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (id *ItemDelete) Where(ps ...predicate.Item) *ItemDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (id *ItemDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(id.hooks) == 0 { - affected, err = id.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*ItemMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - id.mutation = mutation - affected, err = id.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(id.hooks) - 1; i >= 0; i-- { - if id.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = id.hooks[i](mut) - } - n, err := mut.Mutate(ctx, id.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, ItemMutation](ctx, id.sqlExec, id.mutation, id.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (id *ItemDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + id.mutation.done = true return affected, err } diff --git a/entc/integration/ent/item_update.go b/entc/integration/ent/item_update.go index 9cb00aa94..838f6288f 100644 --- a/entc/integration/ent/item_update.go +++ b/entc/integration/ent/item_update.go @@ -59,40 +59,7 @@ func (iu *ItemUpdate) Mutation() *ItemMutation { // Save executes the query and returns the number of nodes affected by the update operation. func (iu *ItemUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(iu.hooks) == 0 { - if err = iu.check(); err != nil { - return 0, err - } - affected, err = iu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*ItemMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = iu.check(); err != nil { - return 0, err - } - iu.mutation = mutation - affected, err = iu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(iu.hooks) - 1; i >= 0; i-- { - if iu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = iu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, iu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, ItemMutation](ctx, iu.sqlSave, iu.mutation, iu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -134,6 +101,9 @@ func (iu *ItemUpdate) Modify(modifiers ...func(u *sql.UpdateBuilder)) *ItemUpdat } func (iu *ItemUpdate) sqlSave(ctx context.Context) (n int, err error) { + if err := iu.check(); err != nil { + return n, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: item.Table, @@ -166,6 +136,7 @@ func (iu *ItemUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + iu.mutation.done = true return n, nil } @@ -212,46 +183,7 @@ func (iuo *ItemUpdateOne) Select(field string, fields ...string) *ItemUpdateOne // Save executes the query and returns the updated Item entity. func (iuo *ItemUpdateOne) Save(ctx context.Context) (*Item, error) { - var ( - err error - node *Item - ) - if len(iuo.hooks) == 0 { - if err = iuo.check(); err != nil { - return nil, err - } - node, err = iuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*ItemMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = iuo.check(); err != nil { - return nil, err - } - iuo.mutation = mutation - node, err = iuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(iuo.hooks) - 1; i >= 0; i-- { - if iuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = iuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, iuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Item) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from ItemMutation", v) - } - node = nv - } - return node, err + return withHooks[*Item, ItemMutation](ctx, iuo.sqlSave, iuo.mutation, iuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -293,6 +225,9 @@ func (iuo *ItemUpdateOne) Modify(modifiers ...func(u *sql.UpdateBuilder)) *ItemU } func (iuo *ItemUpdateOne) sqlSave(ctx context.Context) (_node *Item, err error) { + if err := iuo.check(); err != nil { + return _node, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: item.Table, @@ -345,5 +280,6 @@ func (iuo *ItemUpdateOne) sqlSave(ctx context.Context) (_node *Item, err error) } return nil, err } + iuo.mutation.done = true return _node, nil } diff --git a/entc/integration/ent/license_create.go b/entc/integration/ent/license_create.go index b523de733..88a50726b 100644 --- a/entc/integration/ent/license_create.go +++ b/entc/integration/ent/license_create.go @@ -67,50 +67,8 @@ func (lc *LicenseCreate) Mutation() *LicenseMutation { // Save creates the License in the database. func (lc *LicenseCreate) Save(ctx context.Context) (*License, error) { - var ( - err error - node *License - ) lc.defaults() - if len(lc.hooks) == 0 { - if err = lc.check(); err != nil { - return nil, err - } - node, err = lc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*LicenseMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = lc.check(); err != nil { - return nil, err - } - lc.mutation = mutation - if node, err = lc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(lc.hooks) - 1; i >= 0; i-- { - if lc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = lc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, lc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*License) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from LicenseMutation", v) - } - node = nv - } - return node, err + return withHooks[*License, LicenseMutation](ctx, lc.sqlSave, lc.mutation, lc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -159,6 +117,9 @@ func (lc *LicenseCreate) check() error { } func (lc *LicenseCreate) sqlSave(ctx context.Context) (*License, error) { + if err := lc.check(); err != nil { + return nil, err + } _node, _spec := lc.createSpec() if err := sqlgraph.CreateNode(ctx, lc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -170,6 +131,8 @@ func (lc *LicenseCreate) sqlSave(ctx context.Context) (*License, error) { id := _spec.ID.Value.(int64) _node.ID = int(id) } + lc.mutation.id = &_node.ID + lc.mutation.done = true return _node, nil } diff --git a/entc/integration/ent/license_delete.go b/entc/integration/ent/license_delete.go index 0b2d64f4e..ecd013e32 100644 --- a/entc/integration/ent/license_delete.go +++ b/entc/integration/ent/license_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (ld *LicenseDelete) Where(ps ...predicate.License) *LicenseDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (ld *LicenseDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(ld.hooks) == 0 { - affected, err = ld.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*LicenseMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - ld.mutation = mutation - affected, err = ld.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(ld.hooks) - 1; i >= 0; i-- { - if ld.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ld.hooks[i](mut) - } - n, err := mut.Mutate(ctx, ld.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, LicenseMutation](ctx, ld.sqlExec, ld.mutation, ld.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (ld *LicenseDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + ld.mutation.done = true return affected, err } diff --git a/entc/integration/ent/license_update.go b/entc/integration/ent/license_update.go index 8e48f975e..2920758c9 100644 --- a/entc/integration/ent/license_update.go +++ b/entc/integration/ent/license_update.go @@ -46,35 +46,8 @@ func (lu *LicenseUpdate) Mutation() *LicenseMutation { // Save executes the query and returns the number of nodes affected by the update operation. func (lu *LicenseUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) lu.defaults() - if len(lu.hooks) == 0 { - affected, err = lu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*LicenseMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - lu.mutation = mutation - affected, err = lu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(lu.hooks) - 1; i >= 0; i-- { - if lu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = lu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, lu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, LicenseMutation](ctx, lu.sqlSave, lu.mutation, lu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -143,6 +116,7 @@ func (lu *LicenseUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + lu.mutation.done = true return n, nil } @@ -175,41 +149,8 @@ func (luo *LicenseUpdateOne) Select(field string, fields ...string) *LicenseUpda // Save executes the query and returns the updated License entity. func (luo *LicenseUpdateOne) Save(ctx context.Context) (*License, error) { - var ( - err error - node *License - ) luo.defaults() - if len(luo.hooks) == 0 { - node, err = luo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*LicenseMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - luo.mutation = mutation - node, err = luo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(luo.hooks) - 1; i >= 0; i-- { - if luo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = luo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, luo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*License) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from LicenseMutation", v) - } - node = nv - } - return node, err + return withHooks[*License, LicenseMutation](ctx, luo.sqlSave, luo.mutation, luo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -298,5 +239,6 @@ func (luo *LicenseUpdateOne) sqlSave(ctx context.Context) (_node *License, err e } return nil, err } + luo.mutation.done = true return _node, nil } diff --git a/entc/integration/ent/node_create.go b/entc/integration/ent/node_create.go index 8a30b7d13..20409cbd8 100644 --- a/entc/integration/ent/node_create.go +++ b/entc/integration/ent/node_create.go @@ -84,49 +84,7 @@ func (nc *NodeCreate) Mutation() *NodeMutation { // Save creates the Node in the database. func (nc *NodeCreate) Save(ctx context.Context) (*Node, error) { - var ( - err error - node *Node - ) - if len(nc.hooks) == 0 { - if err = nc.check(); err != nil { - return nil, err - } - node, err = nc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*NodeMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = nc.check(); err != nil { - return nil, err - } - nc.mutation = mutation - if node, err = nc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(nc.hooks) - 1; i >= 0; i-- { - if nc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = nc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, nc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Node) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from NodeMutation", v) - } - node = nv - } - return node, err + return withHooks[*Node, NodeMutation](ctx, nc.sqlSave, nc.mutation, nc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -157,6 +115,9 @@ func (nc *NodeCreate) check() error { } func (nc *NodeCreate) sqlSave(ctx context.Context) (*Node, error) { + if err := nc.check(); err != nil { + return nil, err + } _node, _spec := nc.createSpec() if err := sqlgraph.CreateNode(ctx, nc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -166,6 +127,8 @@ func (nc *NodeCreate) sqlSave(ctx context.Context) (*Node, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + nc.mutation.id = &_node.ID + nc.mutation.done = true return _node, nil } diff --git a/entc/integration/ent/node_delete.go b/entc/integration/ent/node_delete.go index 32588e6b7..bc51f6997 100644 --- a/entc/integration/ent/node_delete.go +++ b/entc/integration/ent/node_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (nd *NodeDelete) Where(ps ...predicate.Node) *NodeDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (nd *NodeDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(nd.hooks) == 0 { - affected, err = nd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*NodeMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - nd.mutation = mutation - affected, err = nd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(nd.hooks) - 1; i >= 0; i-- { - if nd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = nd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, nd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, NodeMutation](ctx, nd.sqlExec, nd.mutation, nd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (nd *NodeDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + nd.mutation.done = true return affected, err } diff --git a/entc/integration/ent/node_update.go b/entc/integration/ent/node_update.go index 06f90a8d7..28f787b1e 100644 --- a/entc/integration/ent/node_update.go +++ b/entc/integration/ent/node_update.go @@ -116,34 +116,7 @@ func (nu *NodeUpdate) ClearNext() *NodeUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (nu *NodeUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(nu.hooks) == 0 { - affected, err = nu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*NodeMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - nu.mutation = mutation - affected, err = nu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(nu.hooks) - 1; i >= 0; i-- { - if nu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = nu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, nu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, NodeMutation](ctx, nu.sqlSave, nu.mutation, nu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -280,6 +253,7 @@ func (nu *NodeUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + nu.mutation.done = true return n, nil } @@ -383,40 +357,7 @@ func (nuo *NodeUpdateOne) Select(field string, fields ...string) *NodeUpdateOne // Save executes the query and returns the updated Node entity. func (nuo *NodeUpdateOne) Save(ctx context.Context) (*Node, error) { - var ( - err error - node *Node - ) - if len(nuo.hooks) == 0 { - node, err = nuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*NodeMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - nuo.mutation = mutation - node, err = nuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(nuo.hooks) - 1; i >= 0; i-- { - if nuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = nuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, nuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Node) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from NodeMutation", v) - } - node = nv - } - return node, err + return withHooks[*Node, NodeMutation](ctx, nuo.sqlSave, nuo.mutation, nuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -573,5 +514,6 @@ func (nuo *NodeUpdateOne) sqlSave(ctx context.Context) (_node *Node, err error) } return nil, err } + nuo.mutation.done = true return _node, nil } diff --git a/entc/integration/ent/pet_create.go b/entc/integration/ent/pet_create.go index 10cba9052..ef75fa568 100644 --- a/entc/integration/ent/pet_create.go +++ b/entc/integration/ent/pet_create.go @@ -134,50 +134,8 @@ func (pc *PetCreate) Mutation() *PetMutation { // Save creates the Pet in the database. func (pc *PetCreate) Save(ctx context.Context) (*Pet, error) { - var ( - err error - node *Pet - ) pc.defaults() - if len(pc.hooks) == 0 { - if err = pc.check(); err != nil { - return nil, err - } - node, err = pc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*PetMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = pc.check(); err != nil { - return nil, err - } - pc.mutation = mutation - if node, err = pc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(pc.hooks) - 1; i >= 0; i-- { - if pc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = pc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, pc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Pet) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from PetMutation", v) - } - node = nv - } - return node, err + return withHooks[*Pet, PetMutation](ctx, pc.sqlSave, pc.mutation, pc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -229,6 +187,9 @@ func (pc *PetCreate) check() error { } func (pc *PetCreate) sqlSave(ctx context.Context) (*Pet, error) { + if err := pc.check(); err != nil { + return nil, err + } _node, _spec := pc.createSpec() if err := sqlgraph.CreateNode(ctx, pc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -238,6 +199,8 @@ func (pc *PetCreate) sqlSave(ctx context.Context) (*Pet, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + pc.mutation.id = &_node.ID + pc.mutation.done = true return _node, nil } diff --git a/entc/integration/ent/pet_delete.go b/entc/integration/ent/pet_delete.go index b494fb8ff..46fa97c77 100644 --- a/entc/integration/ent/pet_delete.go +++ b/entc/integration/ent/pet_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (pd *PetDelete) Where(ps ...predicate.Pet) *PetDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (pd *PetDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(pd.hooks) == 0 { - affected, err = pd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*PetMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - pd.mutation = mutation - affected, err = pd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(pd.hooks) - 1; i >= 0; i-- { - if pd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = pd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, pd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, PetMutation](ctx, pd.sqlExec, pd.mutation, pd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (pd *PetDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + pd.mutation.done = true return affected, err } diff --git a/entc/integration/ent/pet_update.go b/entc/integration/ent/pet_update.go index db18097db..cca857759 100644 --- a/entc/integration/ent/pet_update.go +++ b/entc/integration/ent/pet_update.go @@ -172,34 +172,7 @@ func (pu *PetUpdate) ClearOwner() *PetUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (pu *PetUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(pu.hooks) == 0 { - affected, err = pu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*PetMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - pu.mutation = mutation - affected, err = pu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(pu.hooks) - 1; i >= 0; i-- { - if pu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = pu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, pu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, PetMutation](ctx, pu.sqlSave, pu.mutation, pu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -351,6 +324,7 @@ func (pu *PetUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + pu.mutation.done = true return n, nil } @@ -508,40 +482,7 @@ func (puo *PetUpdateOne) Select(field string, fields ...string) *PetUpdateOne { // Save executes the query and returns the updated Pet entity. func (puo *PetUpdateOne) Save(ctx context.Context) (*Pet, error) { - var ( - err error - node *Pet - ) - if len(puo.hooks) == 0 { - node, err = puo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*PetMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - puo.mutation = mutation - node, err = puo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(puo.hooks) - 1; i >= 0; i-- { - if puo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = puo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, puo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Pet) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from PetMutation", v) - } - node = nv - } - return node, err + return withHooks[*Pet, PetMutation](ctx, puo.sqlSave, puo.mutation, puo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -713,5 +654,6 @@ func (puo *PetUpdateOne) sqlSave(ctx context.Context) (_node *Pet, err error) { } return nil, err } + puo.mutation.done = true return _node, nil } diff --git a/entc/integration/ent/spec_create.go b/entc/integration/ent/spec_create.go index 4414e2455..025af9707 100644 --- a/entc/integration/ent/spec_create.go +++ b/entc/integration/ent/spec_create.go @@ -48,49 +48,7 @@ func (sc *SpecCreate) Mutation() *SpecMutation { // Save creates the Spec in the database. func (sc *SpecCreate) Save(ctx context.Context) (*Spec, error) { - var ( - err error - node *Spec - ) - if len(sc.hooks) == 0 { - if err = sc.check(); err != nil { - return nil, err - } - node, err = sc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*SpecMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = sc.check(); err != nil { - return nil, err - } - sc.mutation = mutation - if node, err = sc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(sc.hooks) - 1; i >= 0; i-- { - if sc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = sc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, sc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Spec) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from SpecMutation", v) - } - node = nv - } - return node, err + return withHooks[*Spec, SpecMutation](ctx, sc.sqlSave, sc.mutation, sc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -121,6 +79,9 @@ func (sc *SpecCreate) check() error { } func (sc *SpecCreate) sqlSave(ctx context.Context) (*Spec, error) { + if err := sc.check(); err != nil { + return nil, err + } _node, _spec := sc.createSpec() if err := sqlgraph.CreateNode(ctx, sc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -130,6 +91,8 @@ func (sc *SpecCreate) sqlSave(ctx context.Context) (*Spec, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + sc.mutation.id = &_node.ID + sc.mutation.done = true return _node, nil } diff --git a/entc/integration/ent/spec_delete.go b/entc/integration/ent/spec_delete.go index 213a5bba1..5a2b4c7b4 100644 --- a/entc/integration/ent/spec_delete.go +++ b/entc/integration/ent/spec_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (sd *SpecDelete) Where(ps ...predicate.Spec) *SpecDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (sd *SpecDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(sd.hooks) == 0 { - affected, err = sd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*SpecMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - sd.mutation = mutation - affected, err = sd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(sd.hooks) - 1; i >= 0; i-- { - if sd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = sd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, sd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, SpecMutation](ctx, sd.sqlExec, sd.mutation, sd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (sd *SpecDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + sd.mutation.done = true return affected, err } diff --git a/entc/integration/ent/spec_update.go b/entc/integration/ent/spec_update.go index 7ee23d9f3..04ac0aaae 100644 --- a/entc/integration/ent/spec_update.go +++ b/entc/integration/ent/spec_update.go @@ -76,34 +76,7 @@ func (su *SpecUpdate) RemoveCard(c ...*Card) *SpecUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (su *SpecUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(su.hooks) == 0 { - affected, err = su.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*SpecMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - su.mutation = mutation - affected, err = su.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(su.hooks) - 1; i >= 0; i-- { - if su.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = su.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, su.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, SpecMutation](ctx, su.sqlSave, su.mutation, su.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -215,6 +188,7 @@ func (su *SpecUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + su.mutation.done = true return n, nil } @@ -277,40 +251,7 @@ func (suo *SpecUpdateOne) Select(field string, fields ...string) *SpecUpdateOne // Save executes the query and returns the updated Spec entity. func (suo *SpecUpdateOne) Save(ctx context.Context) (*Spec, error) { - var ( - err error - node *Spec - ) - if len(suo.hooks) == 0 { - node, err = suo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*SpecMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - suo.mutation = mutation - node, err = suo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(suo.hooks) - 1; i >= 0; i-- { - if suo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = suo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, suo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Spec) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from SpecMutation", v) - } - node = nv - } - return node, err + return withHooks[*Spec, SpecMutation](ctx, suo.sqlSave, suo.mutation, suo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -442,5 +383,6 @@ func (suo *SpecUpdateOne) sqlSave(ctx context.Context) (_node *Spec, err error) } return nil, err } + suo.mutation.done = true return _node, nil } diff --git a/entc/integration/ent/task_create.go b/entc/integration/ent/task_create.go index cbb6a8ae3..4d6067a12 100644 --- a/entc/integration/ent/task_create.go +++ b/entc/integration/ent/task_create.go @@ -69,50 +69,8 @@ func (tc *TaskCreate) Mutation() *TaskMutation { // Save creates the Task in the database. func (tc *TaskCreate) Save(ctx context.Context) (*Task, error) { - var ( - err error - node *Task - ) tc.defaults() - if len(tc.hooks) == 0 { - if err = tc.check(); err != nil { - return nil, err - } - node, err = tc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*TaskMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = tc.check(); err != nil { - return nil, err - } - tc.mutation = mutation - if node, err = tc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(tc.hooks) - 1; i >= 0; i-- { - if tc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = tc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, tc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Task) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from TaskMutation", v) - } - node = nv - } - return node, err + return withHooks[*Task, TaskMutation](ctx, tc.sqlSave, tc.mutation, tc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -166,6 +124,9 @@ func (tc *TaskCreate) check() error { } func (tc *TaskCreate) sqlSave(ctx context.Context) (*Task, error) { + if err := tc.check(); err != nil { + return nil, err + } _node, _spec := tc.createSpec() if err := sqlgraph.CreateNode(ctx, tc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -175,6 +136,8 @@ func (tc *TaskCreate) sqlSave(ctx context.Context) (*Task, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + tc.mutation.id = &_node.ID + tc.mutation.done = true return _node, nil } diff --git a/entc/integration/ent/task_delete.go b/entc/integration/ent/task_delete.go index a2327fbc1..00e7b38cd 100644 --- a/entc/integration/ent/task_delete.go +++ b/entc/integration/ent/task_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -33,40 +32,7 @@ func (td *TaskDelete) Where(ps ...predicate.Task) *TaskDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (td *TaskDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(td.hooks) == 0 { - affected, err = td.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*TaskMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - td.mutation = mutation - affected, err = td.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(td.hooks) - 1; i >= 0; i-- { - if td.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = td.hooks[i](mut) - } - n, err := mut.Mutate(ctx, td.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, TaskMutation](ctx, td.sqlExec, td.mutation, td.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -99,6 +65,7 @@ func (td *TaskDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + td.mutation.done = true return affected, err } diff --git a/entc/integration/ent/task_update.go b/entc/integration/ent/task_update.go index f0ffabc4f..5bd589f76 100644 --- a/entc/integration/ent/task_update.go +++ b/entc/integration/ent/task_update.go @@ -74,40 +74,7 @@ func (tu *TaskUpdate) Mutation() *TaskMutation { // Save executes the query and returns the number of nodes affected by the update operation. func (tu *TaskUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(tu.hooks) == 0 { - if err = tu.check(); err != nil { - return 0, err - } - affected, err = tu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*TaskMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = tu.check(); err != nil { - return 0, err - } - tu.mutation = mutation - affected, err = tu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(tu.hooks) - 1; i >= 0; i-- { - if tu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = tu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, tu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, TaskMutation](ctx, tu.sqlSave, tu.mutation, tu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -149,6 +116,9 @@ func (tu *TaskUpdate) Modify(modifiers ...func(u *sql.UpdateBuilder)) *TaskUpdat } func (tu *TaskUpdate) sqlSave(ctx context.Context) (n int, err error) { + if err := tu.check(); err != nil { + return n, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: enttask.Table, @@ -187,6 +157,7 @@ func (tu *TaskUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + tu.mutation.done = true return n, nil } @@ -246,46 +217,7 @@ func (tuo *TaskUpdateOne) Select(field string, fields ...string) *TaskUpdateOne // Save executes the query and returns the updated Task entity. func (tuo *TaskUpdateOne) Save(ctx context.Context) (*Task, error) { - var ( - err error - node *Task - ) - if len(tuo.hooks) == 0 { - if err = tuo.check(); err != nil { - return nil, err - } - node, err = tuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*TaskMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = tuo.check(); err != nil { - return nil, err - } - tuo.mutation = mutation - node, err = tuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(tuo.hooks) - 1; i >= 0; i-- { - if tuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = tuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, tuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Task) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from TaskMutation", v) - } - node = nv - } - return node, err + return withHooks[*Task, TaskMutation](ctx, tuo.sqlSave, tuo.mutation, tuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -327,6 +259,9 @@ func (tuo *TaskUpdateOne) Modify(modifiers ...func(u *sql.UpdateBuilder)) *TaskU } func (tuo *TaskUpdateOne) sqlSave(ctx context.Context) (_node *Task, err error) { + if err := tuo.check(); err != nil { + return _node, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: enttask.Table, @@ -385,5 +320,6 @@ func (tuo *TaskUpdateOne) sqlSave(ctx context.Context) (_node *Task, err error) } return nil, err } + tuo.mutation.done = true return _node, nil } diff --git a/entc/integration/ent/user_create.go b/entc/integration/ent/user_create.go index c4825b0a0..4046080c5 100644 --- a/entc/integration/ent/user_create.go +++ b/entc/integration/ent/user_create.go @@ -355,50 +355,8 @@ func (uc *UserCreate) Mutation() *UserMutation { // Save creates the User in the database. func (uc *UserCreate) Save(ctx context.Context) (*User, error) { - var ( - err error - node *User - ) uc.defaults() - if len(uc.hooks) == 0 { - if err = uc.check(); err != nil { - return nil, err - } - node, err = uc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = uc.check(); err != nil { - return nil, err - } - uc.mutation = mutation - if node, err = uc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(uc.hooks) - 1; i >= 0; i-- { - if uc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*User) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) - } - node = nv - } - return node, err + return withHooks[*User, UserMutation](ctx, uc.sqlSave, uc.mutation, uc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -479,6 +437,9 @@ func (uc *UserCreate) check() error { } func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { + if err := uc.check(); err != nil { + return nil, err + } _node, _spec := uc.createSpec() if err := sqlgraph.CreateNode(ctx, uc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -488,6 +449,8 @@ func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + uc.mutation.id = &_node.ID + uc.mutation.done = true return _node, nil } diff --git a/entc/integration/ent/user_delete.go b/entc/integration/ent/user_delete.go index b6f2eeb61..c30edda17 100644 --- a/entc/integration/ent/user_delete.go +++ b/entc/integration/ent/user_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (ud *UserDelete) Where(ps ...predicate.User) *UserDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (ud *UserDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(ud.hooks) == 0 { - affected, err = ud.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - ud.mutation = mutation - affected, err = ud.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(ud.hooks) - 1; i >= 0; i-- { - if ud.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ud.hooks[i](mut) - } - n, err := mut.Mutate(ctx, ud.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, UserMutation](ctx, ud.sqlExec, ud.mutation, ud.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (ud *UserDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + ud.mutation.done = true return affected, err } diff --git a/entc/integration/ent/user_update.go b/entc/integration/ent/user_update.go index f14e5888b..e045ab964 100644 --- a/entc/integration/ent/user_update.go +++ b/entc/integration/ent/user_update.go @@ -583,40 +583,7 @@ func (uu *UserUpdate) ClearParent() *UserUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (uu *UserUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(uu.hooks) == 0 { - if err = uu.check(); err != nil { - return 0, err - } - affected, err = uu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = uu.check(); err != nil { - return 0, err - } - uu.mutation = mutation - affected, err = uu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(uu.hooks) - 1; i >= 0; i-- { - if uu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, uu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, UserMutation](ctx, uu.sqlSave, uu.mutation, uu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -668,6 +635,9 @@ func (uu *UserUpdate) Modify(modifiers ...func(u *sql.UpdateBuilder)) *UserUpdat } func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) { + if err := uu.check(); err != nil { + return n, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: user.Table, @@ -1269,6 +1239,7 @@ func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + uu.mutation.done = true return n, nil } @@ -1835,46 +1806,7 @@ func (uuo *UserUpdateOne) Select(field string, fields ...string) *UserUpdateOne // Save executes the query and returns the updated User entity. func (uuo *UserUpdateOne) Save(ctx context.Context) (*User, error) { - var ( - err error - node *User - ) - if len(uuo.hooks) == 0 { - if err = uuo.check(); err != nil { - return nil, err - } - node, err = uuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = uuo.check(); err != nil { - return nil, err - } - uuo.mutation = mutation - node, err = uuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(uuo.hooks) - 1; i >= 0; i-- { - if uuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*User) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) - } - node = nv - } - return node, err + return withHooks[*User, UserMutation](ctx, uuo.sqlSave, uuo.mutation, uuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -1926,6 +1858,9 @@ func (uuo *UserUpdateOne) Modify(modifiers ...func(u *sql.UpdateBuilder)) *UserU } func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) { + if err := uuo.check(); err != nil { + return _node, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: user.Table, @@ -2547,5 +2482,6 @@ func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) } return nil, err } + uuo.mutation.done = true return _node, nil } diff --git a/entc/integration/gremlin/ent/card_create.go b/entc/integration/gremlin/ent/card_create.go index f9fc0d284..31b7098ec 100644 --- a/entc/integration/gremlin/ent/card_create.go +++ b/entc/integration/gremlin/ent/card_create.go @@ -132,50 +132,8 @@ func (cc *CardCreate) Mutation() *CardMutation { // Save creates the Card in the database. func (cc *CardCreate) Save(ctx context.Context) (*Card, error) { - var ( - err error - node *Card - ) cc.defaults() - if len(cc.hooks) == 0 { - if err = cc.check(); err != nil { - return nil, err - } - node, err = cc.gremlinSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CardMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = cc.check(); err != nil { - return nil, err - } - cc.mutation = mutation - if node, err = cc.gremlinSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(cc.hooks) - 1; i >= 0; i-- { - if cc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = cc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, cc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Card) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from CardMutation", v) - } - node = nv - } - return node, err + return withHooks[*Card, CardMutation](ctx, cc.gremlinSave, cc.mutation, cc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -244,6 +202,9 @@ func (cc *CardCreate) check() error { } func (cc *CardCreate) gremlinSave(ctx context.Context) (*Card, error) { + if err := cc.check(); err != nil { + return nil, err + } res := &gremlin.Response{} query, bindings := cc.gremlin().Query() if err := cc.driver.Exec(ctx, query, bindings, res); err != nil { @@ -256,6 +217,8 @@ func (cc *CardCreate) gremlinSave(ctx context.Context) (*Card, error) { if err := c.FromResponse(res); err != nil { return nil, err } + cc.mutation.id = &c.ID + cc.mutation.done = true return c, nil } diff --git a/entc/integration/gremlin/ent/card_delete.go b/entc/integration/gremlin/ent/card_delete.go index ec3f149f9..b5c6d5902 100644 --- a/entc/integration/gremlin/ent/card_delete.go +++ b/entc/integration/gremlin/ent/card_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/gremlin" "entgo.io/ent/dialect/gremlin/graph/dsl" @@ -33,40 +32,7 @@ func (cd *CardDelete) Where(ps ...predicate.Card) *CardDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (cd *CardDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(cd.hooks) == 0 { - affected, err = cd.gremlinExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CardMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - cd.mutation = mutation - affected, err = cd.gremlinExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(cd.hooks) - 1; i >= 0; i-- { - if cd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = cd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, cd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, CardMutation](ctx, cd.gremlinExec, cd.mutation, cd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -84,6 +50,7 @@ func (cd *CardDelete) gremlinExec(ctx context.Context) (int, error) { if err := cd.driver.Exec(ctx, query, bindings, res); err != nil { return 0, err } + cd.mutation.done = true return res.ReadInt() } diff --git a/entc/integration/gremlin/ent/card_update.go b/entc/integration/gremlin/ent/card_update.go index caa80f8da..997334bab 100644 --- a/entc/integration/gremlin/ent/card_update.go +++ b/entc/integration/gremlin/ent/card_update.go @@ -151,41 +151,8 @@ func (cu *CardUpdate) RemoveSpec(s ...*Spec) *CardUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (cu *CardUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) cu.defaults() - if len(cu.hooks) == 0 { - if err = cu.check(); err != nil { - return 0, err - } - affected, err = cu.gremlinSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CardMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = cu.check(); err != nil { - return 0, err - } - cu.mutation = mutation - affected, err = cu.gremlinSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(cu.hooks) - 1; i >= 0; i-- { - if cu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = cu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, cu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, CardMutation](ctx, cu.gremlinSave, cu.mutation, cu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -229,6 +196,9 @@ func (cu *CardUpdate) check() error { } func (cu *CardUpdate) gremlinSave(ctx context.Context) (int, error) { + if err := cu.check(); err != nil { + return 0, err + } res := &gremlin.Response{} query, bindings := cu.gremlin().Query() if err := cu.driver.Exec(ctx, query, bindings, res); err != nil { @@ -237,6 +207,7 @@ func (cu *CardUpdate) gremlinSave(ctx context.Context) (int, error) { if err, ok := isConstantError(res); ok { return 0, err } + cu.mutation.done = true return res.ReadInt() } @@ -438,47 +409,8 @@ func (cuo *CardUpdateOne) Select(field string, fields ...string) *CardUpdateOne // Save executes the query and returns the updated Card entity. func (cuo *CardUpdateOne) Save(ctx context.Context) (*Card, error) { - var ( - err error - node *Card - ) cuo.defaults() - if len(cuo.hooks) == 0 { - if err = cuo.check(); err != nil { - return nil, err - } - node, err = cuo.gremlinSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CardMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = cuo.check(); err != nil { - return nil, err - } - cuo.mutation = mutation - node, err = cuo.gremlinSave(ctx) - mutation.done = true - return node, err - }) - for i := len(cuo.hooks) - 1; i >= 0; i-- { - if cuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = cuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, cuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Card) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from CardMutation", v) - } - node = nv - } - return node, err + return withHooks[*Card, CardMutation](ctx, cuo.gremlinSave, cuo.mutation, cuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -522,6 +454,9 @@ func (cuo *CardUpdateOne) check() error { } func (cuo *CardUpdateOne) gremlinSave(ctx context.Context) (*Card, error) { + if err := cuo.check(); err != nil { + return nil, err + } res := &gremlin.Response{} id, ok := cuo.mutation.ID() if !ok { @@ -534,6 +469,7 @@ func (cuo *CardUpdateOne) gremlinSave(ctx context.Context) (*Card, error) { if err, ok := isConstantError(res); ok { return nil, err } + cuo.mutation.done = true c := &Card{config: cuo.config} if err := c.FromResponse(res); err != nil { return nil, err diff --git a/entc/integration/gremlin/ent/comment_create.go b/entc/integration/gremlin/ent/comment_create.go index f3b2278c6..812036346 100644 --- a/entc/integration/gremlin/ent/comment_create.go +++ b/entc/integration/gremlin/ent/comment_create.go @@ -9,7 +9,6 @@ package ent import ( "context" "errors" - "fmt" "entgo.io/ent/dialect/gremlin" "entgo.io/ent/dialect/gremlin/graph/dsl" @@ -102,49 +101,7 @@ func (cc *CommentCreate) Mutation() *CommentMutation { // Save creates the Comment in the database. func (cc *CommentCreate) Save(ctx context.Context) (*Comment, error) { - var ( - err error - node *Comment - ) - if len(cc.hooks) == 0 { - if err = cc.check(); err != nil { - return nil, err - } - node, err = cc.gremlinSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CommentMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = cc.check(); err != nil { - return nil, err - } - cc.mutation = mutation - if node, err = cc.gremlinSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(cc.hooks) - 1; i >= 0; i-- { - if cc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = cc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, cc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Comment) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from CommentMutation", v) - } - node = nv - } - return node, err + return withHooks[*Comment, CommentMutation](ctx, cc.gremlinSave, cc.mutation, cc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -181,6 +138,9 @@ func (cc *CommentCreate) check() error { } func (cc *CommentCreate) gremlinSave(ctx context.Context) (*Comment, error) { + if err := cc.check(); err != nil { + return nil, err + } res := &gremlin.Response{} query, bindings := cc.gremlin().Query() if err := cc.driver.Exec(ctx, query, bindings, res); err != nil { @@ -193,6 +153,8 @@ func (cc *CommentCreate) gremlinSave(ctx context.Context) (*Comment, error) { if err := c.FromResponse(res); err != nil { return nil, err } + cc.mutation.id = &c.ID + cc.mutation.done = true return c, nil } diff --git a/entc/integration/gremlin/ent/comment_delete.go b/entc/integration/gremlin/ent/comment_delete.go index e37e8f1fe..81cb8c500 100644 --- a/entc/integration/gremlin/ent/comment_delete.go +++ b/entc/integration/gremlin/ent/comment_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/gremlin" "entgo.io/ent/dialect/gremlin/graph/dsl" @@ -33,40 +32,7 @@ func (cd *CommentDelete) Where(ps ...predicate.Comment) *CommentDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (cd *CommentDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(cd.hooks) == 0 { - affected, err = cd.gremlinExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CommentMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - cd.mutation = mutation - affected, err = cd.gremlinExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(cd.hooks) - 1; i >= 0; i-- { - if cd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = cd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, cd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, CommentMutation](ctx, cd.gremlinExec, cd.mutation, cd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -84,6 +50,7 @@ func (cd *CommentDelete) gremlinExec(ctx context.Context) (int, error) { if err := cd.driver.Exec(ctx, query, bindings, res); err != nil { return 0, err } + cd.mutation.done = true return res.ReadInt() } diff --git a/entc/integration/gremlin/ent/comment_update.go b/entc/integration/gremlin/ent/comment_update.go index 08694954e..118aa11d7 100644 --- a/entc/integration/gremlin/ent/comment_update.go +++ b/entc/integration/gremlin/ent/comment_update.go @@ -154,34 +154,7 @@ func (cu *CommentUpdate) Mutation() *CommentMutation { // Save executes the query and returns the number of nodes affected by the update operation. func (cu *CommentUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(cu.hooks) == 0 { - affected, err = cu.gremlinSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CommentMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - cu.mutation = mutation - affected, err = cu.gremlinSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(cu.hooks) - 1; i >= 0; i-- { - if cu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = cu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, cu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, CommentMutation](ctx, cu.gremlinSave, cu.mutation, cu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -215,6 +188,7 @@ func (cu *CommentUpdate) gremlinSave(ctx context.Context) (int, error) { if err, ok := isConstantError(res); ok { return 0, err } + cu.mutation.done = true return res.ReadInt() } @@ -445,40 +419,7 @@ func (cuo *CommentUpdateOne) Select(field string, fields ...string) *CommentUpda // Save executes the query and returns the updated Comment entity. func (cuo *CommentUpdateOne) Save(ctx context.Context) (*Comment, error) { - var ( - err error - node *Comment - ) - if len(cuo.hooks) == 0 { - node, err = cuo.gremlinSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CommentMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - cuo.mutation = mutation - node, err = cuo.gremlinSave(ctx) - mutation.done = true - return node, err - }) - for i := len(cuo.hooks) - 1; i >= 0; i-- { - if cuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = cuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, cuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Comment) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from CommentMutation", v) - } - node = nv - } - return node, err + return withHooks[*Comment, CommentMutation](ctx, cuo.gremlinSave, cuo.mutation, cuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -516,6 +457,7 @@ func (cuo *CommentUpdateOne) gremlinSave(ctx context.Context) (*Comment, error) if err, ok := isConstantError(res); ok { return nil, err } + cuo.mutation.done = true c := &Comment{config: cuo.config} if err := c.FromResponse(res); err != nil { return nil, err diff --git a/entc/integration/gremlin/ent/ent.go b/entc/integration/gremlin/ent/ent.go index 256569822..609dcdba6 100644 --- a/entc/integration/gremlin/ent/ent.go +++ b/entc/integration/gremlin/ent/ent.go @@ -476,6 +476,40 @@ func (s *selector) BoolX(ctx context.Context) bool { return v } +// withHooks invokes the builder operation with the given hooks, if any. +func withHooks[V Value, M any, PM interface { + *M + Mutation +}](ctx context.Context, exec func(context.Context) (V, error), mutation PM, hooks []Hook) (value V, err error) { + if len(hooks) == 0 { + return exec(ctx) + } + var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { + mutationT, ok := m.(PM) + if !ok { + return nil, fmt.Errorf("unexpected mutation type %T", m) + } + // Set the mutation to the builder. + *mutation = *mutationT + return exec(ctx) + }) + for i := len(hooks) - 1; i >= 0; i-- { + if hooks[i] == nil { + return value, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") + } + mut = hooks[i](mut) + } + v, err := mut.Mutate(ctx, mutation) + if err != nil { + return value, err + } + nv, ok := v.(V) + if !ok { + return value, fmt.Errorf("unexpected node type %T returned from %T", v, mutation) + } + return nv, nil +} + // newQueryContext returns a new context with the given QueryContext attached in case it does not exist. func newQueryContext(ctx context.Context, typ, op string) context.Context { if ent.QueryFromContext(ctx) == nil { diff --git a/entc/integration/gremlin/ent/fieldtype_create.go b/entc/integration/gremlin/ent/fieldtype_create.go index c92047edd..da5989246 100644 --- a/entc/integration/gremlin/ent/fieldtype_create.go +++ b/entc/integration/gremlin/ent/fieldtype_create.go @@ -796,50 +796,8 @@ func (ftc *FieldTypeCreate) Mutation() *FieldTypeMutation { // Save creates the FieldType in the database. func (ftc *FieldTypeCreate) Save(ctx context.Context) (*FieldType, error) { - var ( - err error - node *FieldType - ) ftc.defaults() - if len(ftc.hooks) == 0 { - if err = ftc.check(); err != nil { - return nil, err - } - node, err = ftc.gremlinSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*FieldTypeMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = ftc.check(); err != nil { - return nil, err - } - ftc.mutation = mutation - if node, err = ftc.gremlinSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(ftc.hooks) - 1; i >= 0; i-- { - if ftc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ftc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, ftc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*FieldType) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from FieldTypeMutation", v) - } - node = nv - } - return node, err + return withHooks[*FieldType, FieldTypeMutation](ctx, ftc.gremlinSave, ftc.mutation, ftc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -993,6 +951,9 @@ func (ftc *FieldTypeCreate) check() error { } func (ftc *FieldTypeCreate) gremlinSave(ctx context.Context) (*FieldType, error) { + if err := ftc.check(); err != nil { + return nil, err + } res := &gremlin.Response{} query, bindings := ftc.gremlin().Query() if err := ftc.driver.Exec(ctx, query, bindings, res); err != nil { @@ -1005,6 +966,8 @@ func (ftc *FieldTypeCreate) gremlinSave(ctx context.Context) (*FieldType, error) if err := ft.FromResponse(res); err != nil { return nil, err } + ftc.mutation.id = &ft.ID + ftc.mutation.done = true return ft, nil } diff --git a/entc/integration/gremlin/ent/fieldtype_delete.go b/entc/integration/gremlin/ent/fieldtype_delete.go index 8cdb96d36..3b6d698bc 100644 --- a/entc/integration/gremlin/ent/fieldtype_delete.go +++ b/entc/integration/gremlin/ent/fieldtype_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/gremlin" "entgo.io/ent/dialect/gremlin/graph/dsl" @@ -33,40 +32,7 @@ func (ftd *FieldTypeDelete) Where(ps ...predicate.FieldType) *FieldTypeDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (ftd *FieldTypeDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(ftd.hooks) == 0 { - affected, err = ftd.gremlinExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*FieldTypeMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - ftd.mutation = mutation - affected, err = ftd.gremlinExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(ftd.hooks) - 1; i >= 0; i-- { - if ftd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ftd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, ftd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, FieldTypeMutation](ctx, ftd.gremlinExec, ftd.mutation, ftd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -84,6 +50,7 @@ func (ftd *FieldTypeDelete) gremlinExec(ctx context.Context) (int, error) { if err := ftd.driver.Exec(ctx, query, bindings, res); err != nil { return 0, err } + ftd.mutation.done = true return res.ReadInt() } diff --git a/entc/integration/gremlin/ent/fieldtype_update.go b/entc/integration/gremlin/ent/fieldtype_update.go index 64bfe176b..964d1d84d 100644 --- a/entc/integration/gremlin/ent/fieldtype_update.go +++ b/entc/integration/gremlin/ent/fieldtype_update.go @@ -1349,41 +1349,8 @@ func (ftu *FieldTypeUpdate) Mutation() *FieldTypeMutation { // Save executes the query and returns the number of nodes affected by the update operation. func (ftu *FieldTypeUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) ftu.defaults() - if len(ftu.hooks) == 0 { - if err = ftu.check(); err != nil { - return 0, err - } - affected, err = ftu.gremlinSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*FieldTypeMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = ftu.check(); err != nil { - return 0, err - } - ftu.mutation = mutation - affected, err = ftu.gremlinSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(ftu.hooks) - 1; i >= 0; i-- { - if ftu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ftu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, ftu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, FieldTypeMutation](ctx, ftu.gremlinSave, ftu.mutation, ftu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -1475,6 +1442,9 @@ func (ftu *FieldTypeUpdate) check() error { } func (ftu *FieldTypeUpdate) gremlinSave(ctx context.Context) (int, error) { + if err := ftu.check(); err != nil { + return 0, err + } res := &gremlin.Response{} query, bindings := ftu.gremlin().Query() if err := ftu.driver.Exec(ctx, query, bindings, res); err != nil { @@ -1483,6 +1453,7 @@ func (ftu *FieldTypeUpdate) gremlinSave(ctx context.Context) (int, error) { if err, ok := isConstantError(res); ok { return 0, err } + ftu.mutation.done = true return res.ReadInt() } @@ -3281,47 +3252,8 @@ func (ftuo *FieldTypeUpdateOne) Select(field string, fields ...string) *FieldTyp // Save executes the query and returns the updated FieldType entity. func (ftuo *FieldTypeUpdateOne) Save(ctx context.Context) (*FieldType, error) { - var ( - err error - node *FieldType - ) ftuo.defaults() - if len(ftuo.hooks) == 0 { - if err = ftuo.check(); err != nil { - return nil, err - } - node, err = ftuo.gremlinSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*FieldTypeMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = ftuo.check(); err != nil { - return nil, err - } - ftuo.mutation = mutation - node, err = ftuo.gremlinSave(ctx) - mutation.done = true - return node, err - }) - for i := len(ftuo.hooks) - 1; i >= 0; i-- { - if ftuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ftuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, ftuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*FieldType) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from FieldTypeMutation", v) - } - node = nv - } - return node, err + return withHooks[*FieldType, FieldTypeMutation](ctx, ftuo.gremlinSave, ftuo.mutation, ftuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -3413,6 +3345,9 @@ func (ftuo *FieldTypeUpdateOne) check() error { } func (ftuo *FieldTypeUpdateOne) gremlinSave(ctx context.Context) (*FieldType, error) { + if err := ftuo.check(); err != nil { + return nil, err + } res := &gremlin.Response{} id, ok := ftuo.mutation.ID() if !ok { @@ -3425,6 +3360,7 @@ func (ftuo *FieldTypeUpdateOne) gremlinSave(ctx context.Context) (*FieldType, er if err, ok := isConstantError(res); ok { return nil, err } + ftuo.mutation.done = true ft := &FieldType{config: ftuo.config} if err := ft.FromResponse(res); err != nil { return nil, err diff --git a/entc/integration/gremlin/ent/file_create.go b/entc/integration/gremlin/ent/file_create.go index ffba3cf6e..ba5190b88 100644 --- a/entc/integration/gremlin/ent/file_create.go +++ b/entc/integration/gremlin/ent/file_create.go @@ -164,50 +164,8 @@ func (fc *FileCreate) Mutation() *FileMutation { // Save creates the File in the database. func (fc *FileCreate) Save(ctx context.Context) (*File, error) { - var ( - err error - node *File - ) fc.defaults() - if len(fc.hooks) == 0 { - if err = fc.check(); err != nil { - return nil, err - } - node, err = fc.gremlinSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*FileMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = fc.check(); err != nil { - return nil, err - } - fc.mutation = mutation - if node, err = fc.gremlinSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(fc.hooks) - 1; i >= 0; i-- { - if fc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = fc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, fc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*File) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from FileMutation", v) - } - node = nv - } - return node, err + return withHooks[*File, FileMutation](ctx, fc.gremlinSave, fc.mutation, fc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -257,6 +215,9 @@ func (fc *FileCreate) check() error { } func (fc *FileCreate) gremlinSave(ctx context.Context) (*File, error) { + if err := fc.check(); err != nil { + return nil, err + } res := &gremlin.Response{} query, bindings := fc.gremlin().Query() if err := fc.driver.Exec(ctx, query, bindings, res); err != nil { @@ -269,6 +230,8 @@ func (fc *FileCreate) gremlinSave(ctx context.Context) (*File, error) { if err := f.FromResponse(res); err != nil { return nil, err } + fc.mutation.id = &f.ID + fc.mutation.done = true return f, nil } diff --git a/entc/integration/gremlin/ent/file_delete.go b/entc/integration/gremlin/ent/file_delete.go index 79df6f7b5..ffb283d74 100644 --- a/entc/integration/gremlin/ent/file_delete.go +++ b/entc/integration/gremlin/ent/file_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/gremlin" "entgo.io/ent/dialect/gremlin/graph/dsl" @@ -33,40 +32,7 @@ func (fd *FileDelete) Where(ps ...predicate.File) *FileDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (fd *FileDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(fd.hooks) == 0 { - affected, err = fd.gremlinExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*FileMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - fd.mutation = mutation - affected, err = fd.gremlinExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(fd.hooks) - 1; i >= 0; i-- { - if fd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = fd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, fd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, FileMutation](ctx, fd.gremlinExec, fd.mutation, fd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -84,6 +50,7 @@ func (fd *FileDelete) gremlinExec(ctx context.Context) (int, error) { if err := fd.driver.Exec(ctx, query, bindings, res); err != nil { return 0, err } + fd.mutation.done = true return res.ReadInt() } diff --git a/entc/integration/gremlin/ent/file_update.go b/entc/integration/gremlin/ent/file_update.go index 7e441cd99..ca09e9eff 100644 --- a/entc/integration/gremlin/ent/file_update.go +++ b/entc/integration/gremlin/ent/file_update.go @@ -242,40 +242,7 @@ func (fu *FileUpdate) RemoveField(f ...*FieldType) *FileUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (fu *FileUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(fu.hooks) == 0 { - if err = fu.check(); err != nil { - return 0, err - } - affected, err = fu.gremlinSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*FileMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = fu.check(); err != nil { - return 0, err - } - fu.mutation = mutation - affected, err = fu.gremlinSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(fu.hooks) - 1; i >= 0; i-- { - if fu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = fu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, fu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, FileMutation](ctx, fu.gremlinSave, fu.mutation, fu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -311,6 +278,9 @@ func (fu *FileUpdate) check() error { } func (fu *FileUpdate) gremlinSave(ctx context.Context) (int, error) { + if err := fu.check(); err != nil { + return 0, err + } res := &gremlin.Response{} query, bindings := fu.gremlin().Query() if err := fu.driver.Exec(ctx, query, bindings, res); err != nil { @@ -319,6 +289,7 @@ func (fu *FileUpdate) gremlinSave(ctx context.Context) (int, error) { if err, ok := isConstantError(res); ok { return 0, err } + fu.mutation.done = true return res.ReadInt() } @@ -640,46 +611,7 @@ func (fuo *FileUpdateOne) Select(field string, fields ...string) *FileUpdateOne // Save executes the query and returns the updated File entity. func (fuo *FileUpdateOne) Save(ctx context.Context) (*File, error) { - var ( - err error - node *File - ) - if len(fuo.hooks) == 0 { - if err = fuo.check(); err != nil { - return nil, err - } - node, err = fuo.gremlinSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*FileMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = fuo.check(); err != nil { - return nil, err - } - fuo.mutation = mutation - node, err = fuo.gremlinSave(ctx) - mutation.done = true - return node, err - }) - for i := len(fuo.hooks) - 1; i >= 0; i-- { - if fuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = fuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, fuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*File) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from FileMutation", v) - } - node = nv - } - return node, err + return withHooks[*File, FileMutation](ctx, fuo.gremlinSave, fuo.mutation, fuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -715,6 +647,9 @@ func (fuo *FileUpdateOne) check() error { } func (fuo *FileUpdateOne) gremlinSave(ctx context.Context) (*File, error) { + if err := fuo.check(); err != nil { + return nil, err + } res := &gremlin.Response{} id, ok := fuo.mutation.ID() if !ok { @@ -727,6 +662,7 @@ func (fuo *FileUpdateOne) gremlinSave(ctx context.Context) (*File, error) { if err, ok := isConstantError(res); ok { return nil, err } + fuo.mutation.done = true f := &File{config: fuo.config} if err := f.FromResponse(res); err != nil { return nil, err diff --git a/entc/integration/gremlin/ent/filetype_create.go b/entc/integration/gremlin/ent/filetype_create.go index b3e6d7e46..57720803c 100644 --- a/entc/integration/gremlin/ent/filetype_create.go +++ b/entc/integration/gremlin/ent/filetype_create.go @@ -82,50 +82,8 @@ func (ftc *FileTypeCreate) Mutation() *FileTypeMutation { // Save creates the FileType in the database. func (ftc *FileTypeCreate) Save(ctx context.Context) (*FileType, error) { - var ( - err error - node *FileType - ) ftc.defaults() - if len(ftc.hooks) == 0 { - if err = ftc.check(); err != nil { - return nil, err - } - node, err = ftc.gremlinSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*FileTypeMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = ftc.check(); err != nil { - return nil, err - } - ftc.mutation = mutation - if node, err = ftc.gremlinSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(ftc.hooks) - 1; i >= 0; i-- { - if ftc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ftc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, ftc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*FileType) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from FileTypeMutation", v) - } - node = nv - } - return node, err + return withHooks[*FileType, FileTypeMutation](ctx, ftc.gremlinSave, ftc.mutation, ftc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -187,6 +145,9 @@ func (ftc *FileTypeCreate) check() error { } func (ftc *FileTypeCreate) gremlinSave(ctx context.Context) (*FileType, error) { + if err := ftc.check(); err != nil { + return nil, err + } res := &gremlin.Response{} query, bindings := ftc.gremlin().Query() if err := ftc.driver.Exec(ctx, query, bindings, res); err != nil { @@ -199,6 +160,8 @@ func (ftc *FileTypeCreate) gremlinSave(ctx context.Context) (*FileType, error) { if err := ft.FromResponse(res); err != nil { return nil, err } + ftc.mutation.id = &ft.ID + ftc.mutation.done = true return ft, nil } diff --git a/entc/integration/gremlin/ent/filetype_delete.go b/entc/integration/gremlin/ent/filetype_delete.go index 69d523845..fe8083963 100644 --- a/entc/integration/gremlin/ent/filetype_delete.go +++ b/entc/integration/gremlin/ent/filetype_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/gremlin" "entgo.io/ent/dialect/gremlin/graph/dsl" @@ -33,40 +32,7 @@ func (ftd *FileTypeDelete) Where(ps ...predicate.FileType) *FileTypeDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (ftd *FileTypeDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(ftd.hooks) == 0 { - affected, err = ftd.gremlinExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*FileTypeMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - ftd.mutation = mutation - affected, err = ftd.gremlinExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(ftd.hooks) - 1; i >= 0; i-- { - if ftd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ftd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, ftd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, FileTypeMutation](ctx, ftd.gremlinExec, ftd.mutation, ftd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -84,6 +50,7 @@ func (ftd *FileTypeDelete) gremlinExec(ctx context.Context) (int, error) { if err := ftd.driver.Exec(ctx, query, bindings, res); err != nil { return 0, err } + ftd.mutation.done = true return res.ReadInt() } diff --git a/entc/integration/gremlin/ent/filetype_update.go b/entc/integration/gremlin/ent/filetype_update.go index f1605543e..41a5d36d6 100644 --- a/entc/integration/gremlin/ent/filetype_update.go +++ b/entc/integration/gremlin/ent/filetype_update.go @@ -110,40 +110,7 @@ func (ftu *FileTypeUpdate) RemoveFiles(f ...*File) *FileTypeUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (ftu *FileTypeUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(ftu.hooks) == 0 { - if err = ftu.check(); err != nil { - return 0, err - } - affected, err = ftu.gremlinSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*FileTypeMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = ftu.check(); err != nil { - return 0, err - } - ftu.mutation = mutation - affected, err = ftu.gremlinSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(ftu.hooks) - 1; i >= 0; i-- { - if ftu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ftu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, ftu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, FileTypeMutation](ctx, ftu.gremlinSave, ftu.mutation, ftu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -184,6 +151,9 @@ func (ftu *FileTypeUpdate) check() error { } func (ftu *FileTypeUpdate) gremlinSave(ctx context.Context) (int, error) { + if err := ftu.check(); err != nil { + return 0, err + } res := &gremlin.Response{} query, bindings := ftu.gremlin().Query() if err := ftu.driver.Exec(ctx, query, bindings, res); err != nil { @@ -192,6 +162,7 @@ func (ftu *FileTypeUpdate) gremlinSave(ctx context.Context) (int, error) { if err, ok := isConstantError(res); ok { return 0, err } + ftu.mutation.done = true return res.ReadInt() } @@ -342,46 +313,7 @@ func (ftuo *FileTypeUpdateOne) Select(field string, fields ...string) *FileTypeU // Save executes the query and returns the updated FileType entity. func (ftuo *FileTypeUpdateOne) Save(ctx context.Context) (*FileType, error) { - var ( - err error - node *FileType - ) - if len(ftuo.hooks) == 0 { - if err = ftuo.check(); err != nil { - return nil, err - } - node, err = ftuo.gremlinSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*FileTypeMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = ftuo.check(); err != nil { - return nil, err - } - ftuo.mutation = mutation - node, err = ftuo.gremlinSave(ctx) - mutation.done = true - return node, err - }) - for i := len(ftuo.hooks) - 1; i >= 0; i-- { - if ftuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ftuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, ftuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*FileType) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from FileTypeMutation", v) - } - node = nv - } - return node, err + return withHooks[*FileType, FileTypeMutation](ctx, ftuo.gremlinSave, ftuo.mutation, ftuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -422,6 +354,9 @@ func (ftuo *FileTypeUpdateOne) check() error { } func (ftuo *FileTypeUpdateOne) gremlinSave(ctx context.Context) (*FileType, error) { + if err := ftuo.check(); err != nil { + return nil, err + } res := &gremlin.Response{} id, ok := ftuo.mutation.ID() if !ok { @@ -434,6 +369,7 @@ func (ftuo *FileTypeUpdateOne) gremlinSave(ctx context.Context) (*FileType, erro if err, ok := isConstantError(res); ok { return nil, err } + ftuo.mutation.done = true ft := &FileType{config: ftuo.config} if err := ft.FromResponse(res); err != nil { return nil, err diff --git a/entc/integration/gremlin/ent/goods_create.go b/entc/integration/gremlin/ent/goods_create.go index 35b7b3f49..a75753cac 100644 --- a/entc/integration/gremlin/ent/goods_create.go +++ b/entc/integration/gremlin/ent/goods_create.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/gremlin" "entgo.io/ent/dialect/gremlin/graph/dsl" @@ -30,49 +29,7 @@ func (gc *GoodsCreate) Mutation() *GoodsMutation { // Save creates the Goods in the database. func (gc *GoodsCreate) Save(ctx context.Context) (*Goods, error) { - var ( - err error - node *Goods - ) - if len(gc.hooks) == 0 { - if err = gc.check(); err != nil { - return nil, err - } - node, err = gc.gremlinSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GoodsMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = gc.check(); err != nil { - return nil, err - } - gc.mutation = mutation - if node, err = gc.gremlinSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(gc.hooks) - 1; i >= 0; i-- { - if gc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = gc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, gc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Goods) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from GoodsMutation", v) - } - node = nv - } - return node, err + return withHooks[*Goods, GoodsMutation](ctx, gc.gremlinSave, gc.mutation, gc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -103,6 +60,9 @@ func (gc *GoodsCreate) check() error { } func (gc *GoodsCreate) gremlinSave(ctx context.Context) (*Goods, error) { + if err := gc.check(); err != nil { + return nil, err + } res := &gremlin.Response{} query, bindings := gc.gremlin().Query() if err := gc.driver.Exec(ctx, query, bindings, res); err != nil { @@ -115,6 +75,8 @@ func (gc *GoodsCreate) gremlinSave(ctx context.Context) (*Goods, error) { if err := _go.FromResponse(res); err != nil { return nil, err } + gc.mutation.id = &_go.ID + gc.mutation.done = true return _go, nil } diff --git a/entc/integration/gremlin/ent/goods_delete.go b/entc/integration/gremlin/ent/goods_delete.go index 792847eb6..c5f91da92 100644 --- a/entc/integration/gremlin/ent/goods_delete.go +++ b/entc/integration/gremlin/ent/goods_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/gremlin" "entgo.io/ent/dialect/gremlin/graph/dsl" @@ -33,40 +32,7 @@ func (gd *GoodsDelete) Where(ps ...predicate.Goods) *GoodsDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (gd *GoodsDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(gd.hooks) == 0 { - affected, err = gd.gremlinExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GoodsMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - gd.mutation = mutation - affected, err = gd.gremlinExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(gd.hooks) - 1; i >= 0; i-- { - if gd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = gd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, gd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, GoodsMutation](ctx, gd.gremlinExec, gd.mutation, gd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -84,6 +50,7 @@ func (gd *GoodsDelete) gremlinExec(ctx context.Context) (int, error) { if err := gd.driver.Exec(ctx, query, bindings, res); err != nil { return 0, err } + gd.mutation.done = true return res.ReadInt() } diff --git a/entc/integration/gremlin/ent/goods_update.go b/entc/integration/gremlin/ent/goods_update.go index 98a5a3758..50b4f382b 100644 --- a/entc/integration/gremlin/ent/goods_update.go +++ b/entc/integration/gremlin/ent/goods_update.go @@ -9,7 +9,6 @@ package ent import ( "context" "errors" - "fmt" "entgo.io/ent/dialect/gremlin" "entgo.io/ent/dialect/gremlin/graph/dsl" @@ -38,34 +37,7 @@ func (gu *GoodsUpdate) Mutation() *GoodsMutation { // Save executes the query and returns the number of nodes affected by the update operation. func (gu *GoodsUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(gu.hooks) == 0 { - affected, err = gu.gremlinSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GoodsMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - gu.mutation = mutation - affected, err = gu.gremlinSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(gu.hooks) - 1; i >= 0; i-- { - if gu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = gu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, gu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, GoodsMutation](ctx, gu.gremlinSave, gu.mutation, gu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -99,6 +71,7 @@ func (gu *GoodsUpdate) gremlinSave(ctx context.Context) (int, error) { if err, ok := isConstantError(res); ok { return 0, err } + gu.mutation.done = true return res.ReadInt() } @@ -137,40 +110,7 @@ func (guo *GoodsUpdateOne) Select(field string, fields ...string) *GoodsUpdateOn // Save executes the query and returns the updated Goods entity. func (guo *GoodsUpdateOne) Save(ctx context.Context) (*Goods, error) { - var ( - err error - node *Goods - ) - if len(guo.hooks) == 0 { - node, err = guo.gremlinSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GoodsMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - guo.mutation = mutation - node, err = guo.gremlinSave(ctx) - mutation.done = true - return node, err - }) - for i := len(guo.hooks) - 1; i >= 0; i-- { - if guo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = guo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, guo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Goods) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from GoodsMutation", v) - } - node = nv - } - return node, err + return withHooks[*Goods, GoodsMutation](ctx, guo.gremlinSave, guo.mutation, guo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -208,6 +148,7 @@ func (guo *GoodsUpdateOne) gremlinSave(ctx context.Context) (*Goods, error) { if err, ok := isConstantError(res); ok { return nil, err } + guo.mutation.done = true _go := &Goods{config: guo.config} if err := _go.FromResponse(res); err != nil { return nil, err diff --git a/entc/integration/gremlin/ent/group_create.go b/entc/integration/gremlin/ent/group_create.go index 074d8d673..b7583866b 100644 --- a/entc/integration/gremlin/ent/group_create.go +++ b/entc/integration/gremlin/ent/group_create.go @@ -145,50 +145,8 @@ func (gc *GroupCreate) Mutation() *GroupMutation { // Save creates the Group in the database. func (gc *GroupCreate) Save(ctx context.Context) (*Group, error) { - var ( - err error - node *Group - ) gc.defaults() - if len(gc.hooks) == 0 { - if err = gc.check(); err != nil { - return nil, err - } - node, err = gc.gremlinSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GroupMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = gc.check(); err != nil { - return nil, err - } - gc.mutation = mutation - if node, err = gc.gremlinSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(gc.hooks) - 1; i >= 0; i-- { - if gc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = gc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, gc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Group) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from GroupMutation", v) - } - node = nv - } - return node, err + return withHooks[*Group, GroupMutation](ctx, gc.gremlinSave, gc.mutation, gc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -258,6 +216,9 @@ func (gc *GroupCreate) check() error { } func (gc *GroupCreate) gremlinSave(ctx context.Context) (*Group, error) { + if err := gc.check(); err != nil { + return nil, err + } res := &gremlin.Response{} query, bindings := gc.gremlin().Query() if err := gc.driver.Exec(ctx, query, bindings, res); err != nil { @@ -270,6 +231,8 @@ func (gc *GroupCreate) gremlinSave(ctx context.Context) (*Group, error) { if err := gr.FromResponse(res); err != nil { return nil, err } + gc.mutation.id = &gr.ID + gc.mutation.done = true return gr, nil } diff --git a/entc/integration/gremlin/ent/group_delete.go b/entc/integration/gremlin/ent/group_delete.go index 2634b5c68..fc24f190f 100644 --- a/entc/integration/gremlin/ent/group_delete.go +++ b/entc/integration/gremlin/ent/group_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/gremlin" "entgo.io/ent/dialect/gremlin/graph/dsl" @@ -33,40 +32,7 @@ func (gd *GroupDelete) Where(ps ...predicate.Group) *GroupDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (gd *GroupDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(gd.hooks) == 0 { - affected, err = gd.gremlinExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GroupMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - gd.mutation = mutation - affected, err = gd.gremlinExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(gd.hooks) - 1; i >= 0; i-- { - if gd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = gd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, gd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, GroupMutation](ctx, gd.gremlinExec, gd.mutation, gd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -84,6 +50,7 @@ func (gd *GroupDelete) gremlinExec(ctx context.Context) (int, error) { if err := gd.driver.Exec(ctx, query, bindings, res); err != nil { return 0, err } + gd.mutation.done = true return res.ReadInt() } diff --git a/entc/integration/gremlin/ent/group_update.go b/entc/integration/gremlin/ent/group_update.go index 94a1d0923..439d89941 100644 --- a/entc/integration/gremlin/ent/group_update.go +++ b/entc/integration/gremlin/ent/group_update.go @@ -240,40 +240,7 @@ func (gu *GroupUpdate) ClearInfo() *GroupUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (gu *GroupUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(gu.hooks) == 0 { - if err = gu.check(); err != nil { - return 0, err - } - affected, err = gu.gremlinSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GroupMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = gu.check(); err != nil { - return 0, err - } - gu.mutation = mutation - affected, err = gu.gremlinSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(gu.hooks) - 1; i >= 0; i-- { - if gu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = gu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, gu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, GroupMutation](ctx, gu.gremlinSave, gu.mutation, gu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -322,6 +289,9 @@ func (gu *GroupUpdate) check() error { } func (gu *GroupUpdate) gremlinSave(ctx context.Context) (int, error) { + if err := gu.check(); err != nil { + return 0, err + } res := &gremlin.Response{} query, bindings := gu.gremlin().Query() if err := gu.driver.Exec(ctx, query, bindings, res); err != nil { @@ -330,6 +300,7 @@ func (gu *GroupUpdate) gremlinSave(ctx context.Context) (int, error) { if err, ok := isConstantError(res); ok { return 0, err } + gu.mutation.done = true return res.ReadInt() } @@ -648,46 +619,7 @@ func (guo *GroupUpdateOne) Select(field string, fields ...string) *GroupUpdateOn // Save executes the query and returns the updated Group entity. func (guo *GroupUpdateOne) Save(ctx context.Context) (*Group, error) { - var ( - err error - node *Group - ) - if len(guo.hooks) == 0 { - if err = guo.check(); err != nil { - return nil, err - } - node, err = guo.gremlinSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GroupMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = guo.check(); err != nil { - return nil, err - } - guo.mutation = mutation - node, err = guo.gremlinSave(ctx) - mutation.done = true - return node, err - }) - for i := len(guo.hooks) - 1; i >= 0; i-- { - if guo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = guo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, guo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Group) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from GroupMutation", v) - } - node = nv - } - return node, err + return withHooks[*Group, GroupMutation](ctx, guo.gremlinSave, guo.mutation, guo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -736,6 +668,9 @@ func (guo *GroupUpdateOne) check() error { } func (guo *GroupUpdateOne) gremlinSave(ctx context.Context) (*Group, error) { + if err := guo.check(); err != nil { + return nil, err + } res := &gremlin.Response{} id, ok := guo.mutation.ID() if !ok { @@ -748,6 +683,7 @@ func (guo *GroupUpdateOne) gremlinSave(ctx context.Context) (*Group, error) { if err, ok := isConstantError(res); ok { return nil, err } + guo.mutation.done = true gr := &Group{config: guo.config} if err := gr.FromResponse(res); err != nil { return nil, err diff --git a/entc/integration/gremlin/ent/groupinfo_create.go b/entc/integration/gremlin/ent/groupinfo_create.go index d324ed503..68ba40316 100644 --- a/entc/integration/gremlin/ent/groupinfo_create.go +++ b/entc/integration/gremlin/ent/groupinfo_create.go @@ -9,7 +9,6 @@ package ent import ( "context" "errors" - "fmt" "entgo.io/ent/dialect/gremlin" "entgo.io/ent/dialect/gremlin/graph/dsl" @@ -69,50 +68,8 @@ func (gic *GroupInfoCreate) Mutation() *GroupInfoMutation { // Save creates the GroupInfo in the database. func (gic *GroupInfoCreate) Save(ctx context.Context) (*GroupInfo, error) { - var ( - err error - node *GroupInfo - ) gic.defaults() - if len(gic.hooks) == 0 { - if err = gic.check(); err != nil { - return nil, err - } - node, err = gic.gremlinSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GroupInfoMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = gic.check(); err != nil { - return nil, err - } - gic.mutation = mutation - if node, err = gic.gremlinSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(gic.hooks) - 1; i >= 0; i-- { - if gic.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = gic.hooks[i](mut) - } - v, err := mut.Mutate(ctx, gic.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*GroupInfo) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from GroupInfoMutation", v) - } - node = nv - } - return node, err + return withHooks[*GroupInfo, GroupInfoMutation](ctx, gic.gremlinSave, gic.mutation, gic.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -157,6 +114,9 @@ func (gic *GroupInfoCreate) check() error { } func (gic *GroupInfoCreate) gremlinSave(ctx context.Context) (*GroupInfo, error) { + if err := gic.check(); err != nil { + return nil, err + } res := &gremlin.Response{} query, bindings := gic.gremlin().Query() if err := gic.driver.Exec(ctx, query, bindings, res); err != nil { @@ -169,6 +129,8 @@ func (gic *GroupInfoCreate) gremlinSave(ctx context.Context) (*GroupInfo, error) if err := gi.FromResponse(res); err != nil { return nil, err } + gic.mutation.id = &gi.ID + gic.mutation.done = true return gi, nil } diff --git a/entc/integration/gremlin/ent/groupinfo_delete.go b/entc/integration/gremlin/ent/groupinfo_delete.go index 955666798..60ef5cb4c 100644 --- a/entc/integration/gremlin/ent/groupinfo_delete.go +++ b/entc/integration/gremlin/ent/groupinfo_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/gremlin" "entgo.io/ent/dialect/gremlin/graph/dsl" @@ -33,40 +32,7 @@ func (gid *GroupInfoDelete) Where(ps ...predicate.GroupInfo) *GroupInfoDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (gid *GroupInfoDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(gid.hooks) == 0 { - affected, err = gid.gremlinExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GroupInfoMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - gid.mutation = mutation - affected, err = gid.gremlinExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(gid.hooks) - 1; i >= 0; i-- { - if gid.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = gid.hooks[i](mut) - } - n, err := mut.Mutate(ctx, gid.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, GroupInfoMutation](ctx, gid.gremlinExec, gid.mutation, gid.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -84,6 +50,7 @@ func (gid *GroupInfoDelete) gremlinExec(ctx context.Context) (int, error) { if err := gid.driver.Exec(ctx, query, bindings, res); err != nil { return 0, err } + gid.mutation.done = true return res.ReadInt() } diff --git a/entc/integration/gremlin/ent/groupinfo_update.go b/entc/integration/gremlin/ent/groupinfo_update.go index 0de6f40db..df21f94bb 100644 --- a/entc/integration/gremlin/ent/groupinfo_update.go +++ b/entc/integration/gremlin/ent/groupinfo_update.go @@ -9,7 +9,6 @@ package ent import ( "context" "errors" - "fmt" "entgo.io/ent/dialect/gremlin" "entgo.io/ent/dialect/gremlin/graph/dsl" @@ -104,34 +103,7 @@ func (giu *GroupInfoUpdate) RemoveGroups(g ...*Group) *GroupInfoUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (giu *GroupInfoUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(giu.hooks) == 0 { - affected, err = giu.gremlinSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GroupInfoMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - giu.mutation = mutation - affected, err = giu.gremlinSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(giu.hooks) - 1; i >= 0; i-- { - if giu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = giu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, giu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, GroupInfoMutation](ctx, giu.gremlinSave, giu.mutation, giu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -165,6 +137,7 @@ func (giu *GroupInfoUpdate) gremlinSave(ctx context.Context) (int, error) { if err, ok := isConstantError(res); ok { return 0, err } + giu.mutation.done = true return res.ReadInt() } @@ -304,40 +277,7 @@ func (giuo *GroupInfoUpdateOne) Select(field string, fields ...string) *GroupInf // Save executes the query and returns the updated GroupInfo entity. func (giuo *GroupInfoUpdateOne) Save(ctx context.Context) (*GroupInfo, error) { - var ( - err error - node *GroupInfo - ) - if len(giuo.hooks) == 0 { - node, err = giuo.gremlinSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GroupInfoMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - giuo.mutation = mutation - node, err = giuo.gremlinSave(ctx) - mutation.done = true - return node, err - }) - for i := len(giuo.hooks) - 1; i >= 0; i-- { - if giuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = giuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, giuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*GroupInfo) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from GroupInfoMutation", v) - } - node = nv - } - return node, err + return withHooks[*GroupInfo, GroupInfoMutation](ctx, giuo.gremlinSave, giuo.mutation, giuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -375,6 +315,7 @@ func (giuo *GroupInfoUpdateOne) gremlinSave(ctx context.Context) (*GroupInfo, er if err, ok := isConstantError(res); ok { return nil, err } + giuo.mutation.done = true gi := &GroupInfo{config: giuo.config} if err := gi.FromResponse(res); err != nil { return nil, err diff --git a/entc/integration/gremlin/ent/item_create.go b/entc/integration/gremlin/ent/item_create.go index 174ae9829..5bf762983 100644 --- a/entc/integration/gremlin/ent/item_create.go +++ b/entc/integration/gremlin/ent/item_create.go @@ -60,50 +60,8 @@ func (ic *ItemCreate) Mutation() *ItemMutation { // Save creates the Item in the database. func (ic *ItemCreate) Save(ctx context.Context) (*Item, error) { - var ( - err error - node *Item - ) ic.defaults() - if len(ic.hooks) == 0 { - if err = ic.check(); err != nil { - return nil, err - } - node, err = ic.gremlinSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*ItemMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = ic.check(); err != nil { - return nil, err - } - ic.mutation = mutation - if node, err = ic.gremlinSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(ic.hooks) - 1; i >= 0; i-- { - if ic.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ic.hooks[i](mut) - } - v, err := mut.Mutate(ctx, ic.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Item) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from ItemMutation", v) - } - node = nv - } - return node, err + return withHooks[*Item, ItemMutation](ctx, ic.gremlinSave, ic.mutation, ic.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -152,6 +110,9 @@ func (ic *ItemCreate) check() error { } func (ic *ItemCreate) gremlinSave(ctx context.Context) (*Item, error) { + if err := ic.check(); err != nil { + return nil, err + } res := &gremlin.Response{} query, bindings := ic.gremlin().Query() if err := ic.driver.Exec(ctx, query, bindings, res); err != nil { @@ -164,6 +125,8 @@ func (ic *ItemCreate) gremlinSave(ctx context.Context) (*Item, error) { if err := i.FromResponse(res); err != nil { return nil, err } + ic.mutation.id = &i.ID + ic.mutation.done = true return i, nil } diff --git a/entc/integration/gremlin/ent/item_delete.go b/entc/integration/gremlin/ent/item_delete.go index dc6e75533..6d4209940 100644 --- a/entc/integration/gremlin/ent/item_delete.go +++ b/entc/integration/gremlin/ent/item_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/gremlin" "entgo.io/ent/dialect/gremlin/graph/dsl" @@ -33,40 +32,7 @@ func (id *ItemDelete) Where(ps ...predicate.Item) *ItemDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (id *ItemDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(id.hooks) == 0 { - affected, err = id.gremlinExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*ItemMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - id.mutation = mutation - affected, err = id.gremlinExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(id.hooks) - 1; i >= 0; i-- { - if id.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = id.hooks[i](mut) - } - n, err := mut.Mutate(ctx, id.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, ItemMutation](ctx, id.gremlinExec, id.mutation, id.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -84,6 +50,7 @@ func (id *ItemDelete) gremlinExec(ctx context.Context) (int, error) { if err := id.driver.Exec(ctx, query, bindings, res); err != nil { return 0, err } + id.mutation.done = true return res.ReadInt() } diff --git a/entc/integration/gremlin/ent/item_update.go b/entc/integration/gremlin/ent/item_update.go index cb800251e..3ca7a96c1 100644 --- a/entc/integration/gremlin/ent/item_update.go +++ b/entc/integration/gremlin/ent/item_update.go @@ -60,40 +60,7 @@ func (iu *ItemUpdate) Mutation() *ItemMutation { // Save executes the query and returns the number of nodes affected by the update operation. func (iu *ItemUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(iu.hooks) == 0 { - if err = iu.check(); err != nil { - return 0, err - } - affected, err = iu.gremlinSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*ItemMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = iu.check(); err != nil { - return 0, err - } - iu.mutation = mutation - affected, err = iu.gremlinSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(iu.hooks) - 1; i >= 0; i-- { - if iu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = iu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, iu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, ItemMutation](ctx, iu.gremlinSave, iu.mutation, iu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -129,6 +96,9 @@ func (iu *ItemUpdate) check() error { } func (iu *ItemUpdate) gremlinSave(ctx context.Context) (int, error) { + if err := iu.check(); err != nil { + return 0, err + } res := &gremlin.Response{} query, bindings := iu.gremlin().Query() if err := iu.driver.Exec(ctx, query, bindings, res); err != nil { @@ -137,6 +107,7 @@ func (iu *ItemUpdate) gremlinSave(ctx context.Context) (int, error) { if err, ok := isConstantError(res); ok { return 0, err } + iu.mutation.done = true return res.ReadInt() } @@ -227,46 +198,7 @@ func (iuo *ItemUpdateOne) Select(field string, fields ...string) *ItemUpdateOne // Save executes the query and returns the updated Item entity. func (iuo *ItemUpdateOne) Save(ctx context.Context) (*Item, error) { - var ( - err error - node *Item - ) - if len(iuo.hooks) == 0 { - if err = iuo.check(); err != nil { - return nil, err - } - node, err = iuo.gremlinSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*ItemMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = iuo.check(); err != nil { - return nil, err - } - iuo.mutation = mutation - node, err = iuo.gremlinSave(ctx) - mutation.done = true - return node, err - }) - for i := len(iuo.hooks) - 1; i >= 0; i-- { - if iuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = iuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, iuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Item) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from ItemMutation", v) - } - node = nv - } - return node, err + return withHooks[*Item, ItemMutation](ctx, iuo.gremlinSave, iuo.mutation, iuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -302,6 +234,9 @@ func (iuo *ItemUpdateOne) check() error { } func (iuo *ItemUpdateOne) gremlinSave(ctx context.Context) (*Item, error) { + if err := iuo.check(); err != nil { + return nil, err + } res := &gremlin.Response{} id, ok := iuo.mutation.ID() if !ok { @@ -314,6 +249,7 @@ func (iuo *ItemUpdateOne) gremlinSave(ctx context.Context) (*Item, error) { if err, ok := isConstantError(res); ok { return nil, err } + iuo.mutation.done = true i := &Item{config: iuo.config} if err := i.FromResponse(res); err != nil { return nil, err diff --git a/entc/integration/gremlin/ent/license_create.go b/entc/integration/gremlin/ent/license_create.go index d0c825577..0795155b9 100644 --- a/entc/integration/gremlin/ent/license_create.go +++ b/entc/integration/gremlin/ent/license_create.go @@ -9,7 +9,6 @@ package ent import ( "context" "errors" - "fmt" "time" "entgo.io/ent/dialect/gremlin" @@ -66,50 +65,8 @@ func (lc *LicenseCreate) Mutation() *LicenseMutation { // Save creates the License in the database. func (lc *LicenseCreate) Save(ctx context.Context) (*License, error) { - var ( - err error - node *License - ) lc.defaults() - if len(lc.hooks) == 0 { - if err = lc.check(); err != nil { - return nil, err - } - node, err = lc.gremlinSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*LicenseMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = lc.check(); err != nil { - return nil, err - } - lc.mutation = mutation - if node, err = lc.gremlinSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(lc.hooks) - 1; i >= 0; i-- { - if lc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = lc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, lc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*License) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from LicenseMutation", v) - } - node = nv - } - return node, err + return withHooks[*License, LicenseMutation](ctx, lc.gremlinSave, lc.mutation, lc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -158,6 +115,9 @@ func (lc *LicenseCreate) check() error { } func (lc *LicenseCreate) gremlinSave(ctx context.Context) (*License, error) { + if err := lc.check(); err != nil { + return nil, err + } res := &gremlin.Response{} query, bindings := lc.gremlin().Query() if err := lc.driver.Exec(ctx, query, bindings, res); err != nil { @@ -170,6 +130,8 @@ func (lc *LicenseCreate) gremlinSave(ctx context.Context) (*License, error) { if err := l.FromResponse(res); err != nil { return nil, err } + lc.mutation.id = &l.ID + lc.mutation.done = true return l, nil } diff --git a/entc/integration/gremlin/ent/license_delete.go b/entc/integration/gremlin/ent/license_delete.go index 93ccc5f62..ed2ace796 100644 --- a/entc/integration/gremlin/ent/license_delete.go +++ b/entc/integration/gremlin/ent/license_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/gremlin" "entgo.io/ent/dialect/gremlin/graph/dsl" @@ -33,40 +32,7 @@ func (ld *LicenseDelete) Where(ps ...predicate.License) *LicenseDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (ld *LicenseDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(ld.hooks) == 0 { - affected, err = ld.gremlinExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*LicenseMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - ld.mutation = mutation - affected, err = ld.gremlinExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(ld.hooks) - 1; i >= 0; i-- { - if ld.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ld.hooks[i](mut) - } - n, err := mut.Mutate(ctx, ld.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, LicenseMutation](ctx, ld.gremlinExec, ld.mutation, ld.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -84,6 +50,7 @@ func (ld *LicenseDelete) gremlinExec(ctx context.Context) (int, error) { if err := ld.driver.Exec(ctx, query, bindings, res); err != nil { return 0, err } + ld.mutation.done = true return res.ReadInt() } diff --git a/entc/integration/gremlin/ent/license_update.go b/entc/integration/gremlin/ent/license_update.go index b45459906..dbc21de7a 100644 --- a/entc/integration/gremlin/ent/license_update.go +++ b/entc/integration/gremlin/ent/license_update.go @@ -9,7 +9,6 @@ package ent import ( "context" "errors" - "fmt" "time" "entgo.io/ent/dialect/gremlin" @@ -45,35 +44,8 @@ func (lu *LicenseUpdate) Mutation() *LicenseMutation { // Save executes the query and returns the number of nodes affected by the update operation. func (lu *LicenseUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) lu.defaults() - if len(lu.hooks) == 0 { - affected, err = lu.gremlinSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*LicenseMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - lu.mutation = mutation - affected, err = lu.gremlinSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(lu.hooks) - 1; i >= 0; i-- { - if lu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = lu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, lu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, LicenseMutation](ctx, lu.gremlinSave, lu.mutation, lu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -115,6 +87,7 @@ func (lu *LicenseUpdate) gremlinSave(ctx context.Context) (int, error) { if err, ok := isConstantError(res); ok { return 0, err } + lu.mutation.done = true return res.ReadInt() } @@ -162,41 +135,8 @@ func (luo *LicenseUpdateOne) Select(field string, fields ...string) *LicenseUpda // Save executes the query and returns the updated License entity. func (luo *LicenseUpdateOne) Save(ctx context.Context) (*License, error) { - var ( - err error - node *License - ) luo.defaults() - if len(luo.hooks) == 0 { - node, err = luo.gremlinSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*LicenseMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - luo.mutation = mutation - node, err = luo.gremlinSave(ctx) - mutation.done = true - return node, err - }) - for i := len(luo.hooks) - 1; i >= 0; i-- { - if luo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = luo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, luo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*License) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from LicenseMutation", v) - } - node = nv - } - return node, err + return withHooks[*License, LicenseMutation](ctx, luo.gremlinSave, luo.mutation, luo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -242,6 +182,7 @@ func (luo *LicenseUpdateOne) gremlinSave(ctx context.Context) (*License, error) if err, ok := isConstantError(res); ok { return nil, err } + luo.mutation.done = true l := &License{config: luo.config} if err := l.FromResponse(res); err != nil { return nil, err diff --git a/entc/integration/gremlin/ent/node_create.go b/entc/integration/gremlin/ent/node_create.go index aa8a567fb..b1735c37c 100644 --- a/entc/integration/gremlin/ent/node_create.go +++ b/entc/integration/gremlin/ent/node_create.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/gremlin" "entgo.io/ent/dialect/gremlin/graph/dsl" @@ -84,49 +83,7 @@ func (nc *NodeCreate) Mutation() *NodeMutation { // Save creates the Node in the database. func (nc *NodeCreate) Save(ctx context.Context) (*Node, error) { - var ( - err error - node *Node - ) - if len(nc.hooks) == 0 { - if err = nc.check(); err != nil { - return nil, err - } - node, err = nc.gremlinSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*NodeMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = nc.check(); err != nil { - return nil, err - } - nc.mutation = mutation - if node, err = nc.gremlinSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(nc.hooks) - 1; i >= 0; i-- { - if nc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = nc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, nc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Node) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from NodeMutation", v) - } - node = nv - } - return node, err + return withHooks[*Node, NodeMutation](ctx, nc.gremlinSave, nc.mutation, nc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -157,6 +114,9 @@ func (nc *NodeCreate) check() error { } func (nc *NodeCreate) gremlinSave(ctx context.Context) (*Node, error) { + if err := nc.check(); err != nil { + return nil, err + } res := &gremlin.Response{} query, bindings := nc.gremlin().Query() if err := nc.driver.Exec(ctx, query, bindings, res); err != nil { @@ -169,6 +129,8 @@ func (nc *NodeCreate) gremlinSave(ctx context.Context) (*Node, error) { if err := n.FromResponse(res); err != nil { return nil, err } + nc.mutation.id = &n.ID + nc.mutation.done = true return n, nil } diff --git a/entc/integration/gremlin/ent/node_delete.go b/entc/integration/gremlin/ent/node_delete.go index cea4387cd..21b257e72 100644 --- a/entc/integration/gremlin/ent/node_delete.go +++ b/entc/integration/gremlin/ent/node_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/gremlin" "entgo.io/ent/dialect/gremlin/graph/dsl" @@ -33,40 +32,7 @@ func (nd *NodeDelete) Where(ps ...predicate.Node) *NodeDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (nd *NodeDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(nd.hooks) == 0 { - affected, err = nd.gremlinExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*NodeMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - nd.mutation = mutation - affected, err = nd.gremlinExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(nd.hooks) - 1; i >= 0; i-- { - if nd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = nd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, nd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, NodeMutation](ctx, nd.gremlinExec, nd.mutation, nd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -84,6 +50,7 @@ func (nd *NodeDelete) gremlinExec(ctx context.Context) (int, error) { if err := nd.driver.Exec(ctx, query, bindings, res); err != nil { return 0, err } + nd.mutation.done = true return res.ReadInt() } diff --git a/entc/integration/gremlin/ent/node_update.go b/entc/integration/gremlin/ent/node_update.go index b9c9749ad..465ed0bbd 100644 --- a/entc/integration/gremlin/ent/node_update.go +++ b/entc/integration/gremlin/ent/node_update.go @@ -9,7 +9,6 @@ package ent import ( "context" "errors" - "fmt" "entgo.io/ent/dialect/gremlin" "entgo.io/ent/dialect/gremlin/graph/dsl" @@ -117,34 +116,7 @@ func (nu *NodeUpdate) ClearNext() *NodeUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (nu *NodeUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(nu.hooks) == 0 { - affected, err = nu.gremlinSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*NodeMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - nu.mutation = mutation - affected, err = nu.gremlinSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(nu.hooks) - 1; i >= 0; i-- { - if nu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = nu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, nu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, NodeMutation](ctx, nu.gremlinSave, nu.mutation, nu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -178,6 +150,7 @@ func (nu *NodeUpdate) gremlinSave(ctx context.Context) (int, error) { if err, ok := isConstantError(res); ok { return 0, err } + nu.mutation.done = true return res.ReadInt() } @@ -346,40 +319,7 @@ func (nuo *NodeUpdateOne) Select(field string, fields ...string) *NodeUpdateOne // Save executes the query and returns the updated Node entity. func (nuo *NodeUpdateOne) Save(ctx context.Context) (*Node, error) { - var ( - err error - node *Node - ) - if len(nuo.hooks) == 0 { - node, err = nuo.gremlinSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*NodeMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - nuo.mutation = mutation - node, err = nuo.gremlinSave(ctx) - mutation.done = true - return node, err - }) - for i := len(nuo.hooks) - 1; i >= 0; i-- { - if nuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = nuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, nuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Node) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from NodeMutation", v) - } - node = nv - } - return node, err + return withHooks[*Node, NodeMutation](ctx, nuo.gremlinSave, nuo.mutation, nuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -417,6 +357,7 @@ func (nuo *NodeUpdateOne) gremlinSave(ctx context.Context) (*Node, error) { if err, ok := isConstantError(res); ok { return nil, err } + nuo.mutation.done = true n := &Node{config: nuo.config} if err := n.FromResponse(res); err != nil { return nil, err diff --git a/entc/integration/gremlin/ent/pet_create.go b/entc/integration/gremlin/ent/pet_create.go index c3d8b42f4..768a0fd02 100644 --- a/entc/integration/gremlin/ent/pet_create.go +++ b/entc/integration/gremlin/ent/pet_create.go @@ -9,7 +9,6 @@ package ent import ( "context" "errors" - "fmt" "entgo.io/ent/dialect/gremlin" "entgo.io/ent/dialect/gremlin/graph/dsl" @@ -135,50 +134,8 @@ func (pc *PetCreate) Mutation() *PetMutation { // Save creates the Pet in the database. func (pc *PetCreate) Save(ctx context.Context) (*Pet, error) { - var ( - err error - node *Pet - ) pc.defaults() - if len(pc.hooks) == 0 { - if err = pc.check(); err != nil { - return nil, err - } - node, err = pc.gremlinSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*PetMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = pc.check(); err != nil { - return nil, err - } - pc.mutation = mutation - if node, err = pc.gremlinSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(pc.hooks) - 1; i >= 0; i-- { - if pc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = pc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, pc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Pet) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from PetMutation", v) - } - node = nv - } - return node, err + return withHooks[*Pet, PetMutation](ctx, pc.gremlinSave, pc.mutation, pc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -230,6 +187,9 @@ func (pc *PetCreate) check() error { } func (pc *PetCreate) gremlinSave(ctx context.Context) (*Pet, error) { + if err := pc.check(); err != nil { + return nil, err + } res := &gremlin.Response{} query, bindings := pc.gremlin().Query() if err := pc.driver.Exec(ctx, query, bindings, res); err != nil { @@ -242,6 +202,8 @@ func (pc *PetCreate) gremlinSave(ctx context.Context) (*Pet, error) { if err := pe.FromResponse(res); err != nil { return nil, err } + pc.mutation.id = &pe.ID + pc.mutation.done = true return pe, nil } diff --git a/entc/integration/gremlin/ent/pet_delete.go b/entc/integration/gremlin/ent/pet_delete.go index cdb26d799..f65b516ac 100644 --- a/entc/integration/gremlin/ent/pet_delete.go +++ b/entc/integration/gremlin/ent/pet_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/gremlin" "entgo.io/ent/dialect/gremlin/graph/dsl" @@ -33,40 +32,7 @@ func (pd *PetDelete) Where(ps ...predicate.Pet) *PetDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (pd *PetDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(pd.hooks) == 0 { - affected, err = pd.gremlinExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*PetMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - pd.mutation = mutation - affected, err = pd.gremlinExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(pd.hooks) - 1; i >= 0; i-- { - if pd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = pd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, pd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, PetMutation](ctx, pd.gremlinExec, pd.mutation, pd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -84,6 +50,7 @@ func (pd *PetDelete) gremlinExec(ctx context.Context) (int, error) { if err := pd.driver.Exec(ctx, query, bindings, res); err != nil { return 0, err } + pd.mutation.done = true return res.ReadInt() } diff --git a/entc/integration/gremlin/ent/pet_update.go b/entc/integration/gremlin/ent/pet_update.go index 7eb605f7b..208034f3d 100644 --- a/entc/integration/gremlin/ent/pet_update.go +++ b/entc/integration/gremlin/ent/pet_update.go @@ -9,7 +9,6 @@ package ent import ( "context" "errors" - "fmt" "entgo.io/ent/dialect/gremlin" "entgo.io/ent/dialect/gremlin/graph/dsl" @@ -173,34 +172,7 @@ func (pu *PetUpdate) ClearOwner() *PetUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (pu *PetUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(pu.hooks) == 0 { - affected, err = pu.gremlinSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*PetMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - pu.mutation = mutation - affected, err = pu.gremlinSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(pu.hooks) - 1; i >= 0; i-- { - if pu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = pu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, pu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, PetMutation](ctx, pu.gremlinSave, pu.mutation, pu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -234,6 +206,7 @@ func (pu *PetUpdate) gremlinSave(ctx context.Context) (int, error) { if err, ok := isConstantError(res); ok { return 0, err } + pu.mutation.done = true return res.ReadInt() } @@ -467,40 +440,7 @@ func (puo *PetUpdateOne) Select(field string, fields ...string) *PetUpdateOne { // Save executes the query and returns the updated Pet entity. func (puo *PetUpdateOne) Save(ctx context.Context) (*Pet, error) { - var ( - err error - node *Pet - ) - if len(puo.hooks) == 0 { - node, err = puo.gremlinSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*PetMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - puo.mutation = mutation - node, err = puo.gremlinSave(ctx) - mutation.done = true - return node, err - }) - for i := len(puo.hooks) - 1; i >= 0; i-- { - if puo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = puo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, puo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Pet) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from PetMutation", v) - } - node = nv - } - return node, err + return withHooks[*Pet, PetMutation](ctx, puo.gremlinSave, puo.mutation, puo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -538,6 +478,7 @@ func (puo *PetUpdateOne) gremlinSave(ctx context.Context) (*Pet, error) { if err, ok := isConstantError(res); ok { return nil, err } + puo.mutation.done = true pe := &Pet{config: puo.config} if err := pe.FromResponse(res); err != nil { return nil, err diff --git a/entc/integration/gremlin/ent/spec_create.go b/entc/integration/gremlin/ent/spec_create.go index 16625dd5c..9e1670f25 100644 --- a/entc/integration/gremlin/ent/spec_create.go +++ b/entc/integration/gremlin/ent/spec_create.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/gremlin" "entgo.io/ent/dialect/gremlin/graph/dsl" @@ -45,49 +44,7 @@ func (sc *SpecCreate) Mutation() *SpecMutation { // Save creates the Spec in the database. func (sc *SpecCreate) Save(ctx context.Context) (*Spec, error) { - var ( - err error - node *Spec - ) - if len(sc.hooks) == 0 { - if err = sc.check(); err != nil { - return nil, err - } - node, err = sc.gremlinSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*SpecMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = sc.check(); err != nil { - return nil, err - } - sc.mutation = mutation - if node, err = sc.gremlinSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(sc.hooks) - 1; i >= 0; i-- { - if sc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = sc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, sc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Spec) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from SpecMutation", v) - } - node = nv - } - return node, err + return withHooks[*Spec, SpecMutation](ctx, sc.gremlinSave, sc.mutation, sc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -118,6 +75,9 @@ func (sc *SpecCreate) check() error { } func (sc *SpecCreate) gremlinSave(ctx context.Context) (*Spec, error) { + if err := sc.check(); err != nil { + return nil, err + } res := &gremlin.Response{} query, bindings := sc.gremlin().Query() if err := sc.driver.Exec(ctx, query, bindings, res); err != nil { @@ -130,6 +90,8 @@ func (sc *SpecCreate) gremlinSave(ctx context.Context) (*Spec, error) { if err := s.FromResponse(res); err != nil { return nil, err } + sc.mutation.id = &s.ID + sc.mutation.done = true return s, nil } diff --git a/entc/integration/gremlin/ent/spec_delete.go b/entc/integration/gremlin/ent/spec_delete.go index 208c1ca6a..15789857f 100644 --- a/entc/integration/gremlin/ent/spec_delete.go +++ b/entc/integration/gremlin/ent/spec_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/gremlin" "entgo.io/ent/dialect/gremlin/graph/dsl" @@ -33,40 +32,7 @@ func (sd *SpecDelete) Where(ps ...predicate.Spec) *SpecDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (sd *SpecDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(sd.hooks) == 0 { - affected, err = sd.gremlinExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*SpecMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - sd.mutation = mutation - affected, err = sd.gremlinExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(sd.hooks) - 1; i >= 0; i-- { - if sd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = sd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, sd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, SpecMutation](ctx, sd.gremlinExec, sd.mutation, sd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -84,6 +50,7 @@ func (sd *SpecDelete) gremlinExec(ctx context.Context) (int, error) { if err := sd.driver.Exec(ctx, query, bindings, res); err != nil { return 0, err } + sd.mutation.done = true return res.ReadInt() } diff --git a/entc/integration/gremlin/ent/spec_update.go b/entc/integration/gremlin/ent/spec_update.go index 2ab387747..eee50605d 100644 --- a/entc/integration/gremlin/ent/spec_update.go +++ b/entc/integration/gremlin/ent/spec_update.go @@ -9,7 +9,6 @@ package ent import ( "context" "errors" - "fmt" "entgo.io/ent/dialect/gremlin" "entgo.io/ent/dialect/gremlin/graph/dsl" @@ -75,34 +74,7 @@ func (su *SpecUpdate) RemoveCard(c ...*Card) *SpecUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (su *SpecUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(su.hooks) == 0 { - affected, err = su.gremlinSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*SpecMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - su.mutation = mutation - affected, err = su.gremlinSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(su.hooks) - 1; i >= 0; i-- { - if su.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = su.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, su.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, SpecMutation](ctx, su.gremlinSave, su.mutation, su.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -136,6 +108,7 @@ func (su *SpecUpdate) gremlinSave(ctx context.Context) (int, error) { if err, ok := isConstantError(res); ok { return 0, err } + su.mutation.done = true return res.ReadInt() } @@ -220,40 +193,7 @@ func (suo *SpecUpdateOne) Select(field string, fields ...string) *SpecUpdateOne // Save executes the query and returns the updated Spec entity. func (suo *SpecUpdateOne) Save(ctx context.Context) (*Spec, error) { - var ( - err error - node *Spec - ) - if len(suo.hooks) == 0 { - node, err = suo.gremlinSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*SpecMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - suo.mutation = mutation - node, err = suo.gremlinSave(ctx) - mutation.done = true - return node, err - }) - for i := len(suo.hooks) - 1; i >= 0; i-- { - if suo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = suo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, suo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Spec) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from SpecMutation", v) - } - node = nv - } - return node, err + return withHooks[*Spec, SpecMutation](ctx, suo.gremlinSave, suo.mutation, suo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -291,6 +231,7 @@ func (suo *SpecUpdateOne) gremlinSave(ctx context.Context) (*Spec, error) { if err, ok := isConstantError(res); ok { return nil, err } + suo.mutation.done = true s := &Spec{config: suo.config} if err := s.FromResponse(res); err != nil { return nil, err diff --git a/entc/integration/gremlin/ent/task_create.go b/entc/integration/gremlin/ent/task_create.go index 05824a341..3b7069699 100644 --- a/entc/integration/gremlin/ent/task_create.go +++ b/entc/integration/gremlin/ent/task_create.go @@ -68,50 +68,8 @@ func (tc *TaskCreate) Mutation() *TaskMutation { // Save creates the Task in the database. func (tc *TaskCreate) Save(ctx context.Context) (*Task, error) { - var ( - err error - node *Task - ) tc.defaults() - if len(tc.hooks) == 0 { - if err = tc.check(); err != nil { - return nil, err - } - node, err = tc.gremlinSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*TaskMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = tc.check(); err != nil { - return nil, err - } - tc.mutation = mutation - if node, err = tc.gremlinSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(tc.hooks) - 1; i >= 0; i-- { - if tc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = tc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, tc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Task) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from TaskMutation", v) - } - node = nv - } - return node, err + return withHooks[*Task, TaskMutation](ctx, tc.gremlinSave, tc.mutation, tc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -165,6 +123,9 @@ func (tc *TaskCreate) check() error { } func (tc *TaskCreate) gremlinSave(ctx context.Context) (*Task, error) { + if err := tc.check(); err != nil { + return nil, err + } res := &gremlin.Response{} query, bindings := tc.gremlin().Query() if err := tc.driver.Exec(ctx, query, bindings, res); err != nil { @@ -177,6 +138,8 @@ func (tc *TaskCreate) gremlinSave(ctx context.Context) (*Task, error) { if err := t.FromResponse(res); err != nil { return nil, err } + tc.mutation.id = &t.ID + tc.mutation.done = true return t, nil } diff --git a/entc/integration/gremlin/ent/task_delete.go b/entc/integration/gremlin/ent/task_delete.go index 40fee9242..3ba2a6165 100644 --- a/entc/integration/gremlin/ent/task_delete.go +++ b/entc/integration/gremlin/ent/task_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/gremlin" "entgo.io/ent/dialect/gremlin/graph/dsl" @@ -34,40 +33,7 @@ func (td *TaskDelete) Where(ps ...predicate.Task) *TaskDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (td *TaskDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(td.hooks) == 0 { - affected, err = td.gremlinExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*TaskMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - td.mutation = mutation - affected, err = td.gremlinExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(td.hooks) - 1; i >= 0; i-- { - if td.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = td.hooks[i](mut) - } - n, err := mut.Mutate(ctx, td.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, TaskMutation](ctx, td.gremlinExec, td.mutation, td.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -85,6 +51,7 @@ func (td *TaskDelete) gremlinExec(ctx context.Context) (int, error) { if err := td.driver.Exec(ctx, query, bindings, res); err != nil { return 0, err } + td.mutation.done = true return res.ReadInt() } diff --git a/entc/integration/gremlin/ent/task_update.go b/entc/integration/gremlin/ent/task_update.go index baa511f64..fdd1aed01 100644 --- a/entc/integration/gremlin/ent/task_update.go +++ b/entc/integration/gremlin/ent/task_update.go @@ -74,40 +74,7 @@ func (tu *TaskUpdate) Mutation() *TaskMutation { // Save executes the query and returns the number of nodes affected by the update operation. func (tu *TaskUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(tu.hooks) == 0 { - if err = tu.check(); err != nil { - return 0, err - } - affected, err = tu.gremlinSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*TaskMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = tu.check(); err != nil { - return 0, err - } - tu.mutation = mutation - affected, err = tu.gremlinSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(tu.hooks) - 1; i >= 0; i-- { - if tu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = tu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, tu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, TaskMutation](ctx, tu.gremlinSave, tu.mutation, tu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -143,6 +110,9 @@ func (tu *TaskUpdate) check() error { } func (tu *TaskUpdate) gremlinSave(ctx context.Context) (int, error) { + if err := tu.check(); err != nil { + return 0, err + } res := &gremlin.Response{} query, bindings := tu.gremlin().Query() if err := tu.driver.Exec(ctx, query, bindings, res); err != nil { @@ -151,6 +121,7 @@ func (tu *TaskUpdate) gremlinSave(ctx context.Context) (int, error) { if err, ok := isConstantError(res); ok { return 0, err } + tu.mutation.done = true return res.ReadInt() } @@ -238,46 +209,7 @@ func (tuo *TaskUpdateOne) Select(field string, fields ...string) *TaskUpdateOne // Save executes the query and returns the updated Task entity. func (tuo *TaskUpdateOne) Save(ctx context.Context) (*Task, error) { - var ( - err error - node *Task - ) - if len(tuo.hooks) == 0 { - if err = tuo.check(); err != nil { - return nil, err - } - node, err = tuo.gremlinSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*TaskMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = tuo.check(); err != nil { - return nil, err - } - tuo.mutation = mutation - node, err = tuo.gremlinSave(ctx) - mutation.done = true - return node, err - }) - for i := len(tuo.hooks) - 1; i >= 0; i-- { - if tuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = tuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, tuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Task) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from TaskMutation", v) - } - node = nv - } - return node, err + return withHooks[*Task, TaskMutation](ctx, tuo.gremlinSave, tuo.mutation, tuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -313,6 +245,9 @@ func (tuo *TaskUpdateOne) check() error { } func (tuo *TaskUpdateOne) gremlinSave(ctx context.Context) (*Task, error) { + if err := tuo.check(); err != nil { + return nil, err + } res := &gremlin.Response{} id, ok := tuo.mutation.ID() if !ok { @@ -325,6 +260,7 @@ func (tuo *TaskUpdateOne) gremlinSave(ctx context.Context) (*Task, error) { if err, ok := isConstantError(res); ok { return nil, err } + tuo.mutation.done = true t := &Task{config: tuo.config} if err := t.FromResponse(res); err != nil { return nil, err diff --git a/entc/integration/gremlin/ent/user_create.go b/entc/integration/gremlin/ent/user_create.go index 8b252cdd8..797d2a78f 100644 --- a/entc/integration/gremlin/ent/user_create.go +++ b/entc/integration/gremlin/ent/user_create.go @@ -352,50 +352,8 @@ func (uc *UserCreate) Mutation() *UserMutation { // Save creates the User in the database. func (uc *UserCreate) Save(ctx context.Context) (*User, error) { - var ( - err error - node *User - ) uc.defaults() - if len(uc.hooks) == 0 { - if err = uc.check(); err != nil { - return nil, err - } - node, err = uc.gremlinSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = uc.check(); err != nil { - return nil, err - } - uc.mutation = mutation - if node, err = uc.gremlinSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(uc.hooks) - 1; i >= 0; i-- { - if uc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*User) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) - } - node = nv - } - return node, err + return withHooks[*User, UserMutation](ctx, uc.gremlinSave, uc.mutation, uc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -476,6 +434,9 @@ func (uc *UserCreate) check() error { } func (uc *UserCreate) gremlinSave(ctx context.Context) (*User, error) { + if err := uc.check(); err != nil { + return nil, err + } res := &gremlin.Response{} query, bindings := uc.gremlin().Query() if err := uc.driver.Exec(ctx, query, bindings, res); err != nil { @@ -488,6 +449,8 @@ func (uc *UserCreate) gremlinSave(ctx context.Context) (*User, error) { if err := u.FromResponse(res); err != nil { return nil, err } + uc.mutation.id = &u.ID + uc.mutation.done = true return u, nil } diff --git a/entc/integration/gremlin/ent/user_delete.go b/entc/integration/gremlin/ent/user_delete.go index 36f89e093..efe77cc6d 100644 --- a/entc/integration/gremlin/ent/user_delete.go +++ b/entc/integration/gremlin/ent/user_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/gremlin" "entgo.io/ent/dialect/gremlin/graph/dsl" @@ -33,40 +32,7 @@ func (ud *UserDelete) Where(ps ...predicate.User) *UserDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (ud *UserDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(ud.hooks) == 0 { - affected, err = ud.gremlinExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - ud.mutation = mutation - affected, err = ud.gremlinExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(ud.hooks) - 1; i >= 0; i-- { - if ud.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ud.hooks[i](mut) - } - n, err := mut.Mutate(ctx, ud.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, UserMutation](ctx, ud.gremlinExec, ud.mutation, ud.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -84,6 +50,7 @@ func (ud *UserDelete) gremlinExec(ctx context.Context) (int, error) { if err := ud.driver.Exec(ctx, query, bindings, res); err != nil { return 0, err } + ud.mutation.done = true return res.ReadInt() } diff --git a/entc/integration/gremlin/ent/user_update.go b/entc/integration/gremlin/ent/user_update.go index 3ab45837e..5c5f50464 100644 --- a/entc/integration/gremlin/ent/user_update.go +++ b/entc/integration/gremlin/ent/user_update.go @@ -580,40 +580,7 @@ func (uu *UserUpdate) ClearParent() *UserUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (uu *UserUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(uu.hooks) == 0 { - if err = uu.check(); err != nil { - return 0, err - } - affected, err = uu.gremlinSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = uu.check(); err != nil { - return 0, err - } - uu.mutation = mutation - affected, err = uu.gremlinSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(uu.hooks) - 1; i >= 0; i-- { - if uu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, uu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, UserMutation](ctx, uu.gremlinSave, uu.mutation, uu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -659,6 +626,9 @@ func (uu *UserUpdate) check() error { } func (uu *UserUpdate) gremlinSave(ctx context.Context) (int, error) { + if err := uu.check(); err != nil { + return 0, err + } res := &gremlin.Response{} query, bindings := uu.gremlin().Query() if err := uu.driver.Exec(ctx, query, bindings, res); err != nil { @@ -667,6 +637,7 @@ func (uu *UserUpdate) gremlinSave(ctx context.Context) (int, error) { if err, ok := isConstantError(res); ok { return 0, err } + uu.mutation.done = true return res.ReadInt() } @@ -1437,46 +1408,7 @@ func (uuo *UserUpdateOne) Select(field string, fields ...string) *UserUpdateOne // Save executes the query and returns the updated User entity. func (uuo *UserUpdateOne) Save(ctx context.Context) (*User, error) { - var ( - err error - node *User - ) - if len(uuo.hooks) == 0 { - if err = uuo.check(); err != nil { - return nil, err - } - node, err = uuo.gremlinSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = uuo.check(); err != nil { - return nil, err - } - uuo.mutation = mutation - node, err = uuo.gremlinSave(ctx) - mutation.done = true - return node, err - }) - for i := len(uuo.hooks) - 1; i >= 0; i-- { - if uuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*User) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) - } - node = nv - } - return node, err + return withHooks[*User, UserMutation](ctx, uuo.gremlinSave, uuo.mutation, uuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -1522,6 +1454,9 @@ func (uuo *UserUpdateOne) check() error { } func (uuo *UserUpdateOne) gremlinSave(ctx context.Context) (*User, error) { + if err := uuo.check(); err != nil { + return nil, err + } res := &gremlin.Response{} id, ok := uuo.mutation.ID() if !ok { @@ -1534,6 +1469,7 @@ func (uuo *UserUpdateOne) gremlinSave(ctx context.Context) (*User, error) { if err, ok := isConstantError(res); ok { return nil, err } + uuo.mutation.done = true u := &User{config: uuo.config} if err := u.FromResponse(res); err != nil { return nil, err diff --git a/entc/integration/hooks/ent/card_create.go b/entc/integration/hooks/ent/card_create.go index ef6af5b30..275b12968 100644 --- a/entc/integration/hooks/ent/card_create.go +++ b/entc/integration/hooks/ent/card_create.go @@ -113,52 +113,10 @@ func (cc *CardCreate) Mutation() *CardMutation { // Save creates the Card in the database. func (cc *CardCreate) Save(ctx context.Context) (*Card, error) { - var ( - err error - node *Card - ) if err := cc.defaults(); err != nil { return nil, err } - if len(cc.hooks) == 0 { - if err = cc.check(); err != nil { - return nil, err - } - node, err = cc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CardMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = cc.check(); err != nil { - return nil, err - } - cc.mutation = mutation - if node, err = cc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(cc.hooks) - 1; i >= 0; i-- { - if cc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = cc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, cc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Card) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from CardMutation", v) - } - node = nv - } - return node, err + return withHooks[*Card, CardMutation](ctx, cc.sqlSave, cc.mutation, cc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -219,6 +177,9 @@ func (cc *CardCreate) check() error { } func (cc *CardCreate) sqlSave(ctx context.Context) (*Card, error) { + if err := cc.check(); err != nil { + return nil, err + } _node, _spec := cc.createSpec() if err := sqlgraph.CreateNode(ctx, cc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -228,6 +189,8 @@ func (cc *CardCreate) sqlSave(ctx context.Context) (*Card, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + cc.mutation.id = &_node.ID + cc.mutation.done = true return _node, nil } diff --git a/entc/integration/hooks/ent/card_delete.go b/entc/integration/hooks/ent/card_delete.go index bba060aa2..a506cbfcb 100644 --- a/entc/integration/hooks/ent/card_delete.go +++ b/entc/integration/hooks/ent/card_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (cd *CardDelete) Where(ps ...predicate.Card) *CardDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (cd *CardDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(cd.hooks) == 0 { - affected, err = cd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CardMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - cd.mutation = mutation - affected, err = cd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(cd.hooks) - 1; i >= 0; i-- { - if cd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = cd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, cd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, CardMutation](ctx, cd.sqlExec, cd.mutation, cd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (cd *CardDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + cd.mutation.done = true return affected, err } diff --git a/entc/integration/hooks/ent/card_update.go b/entc/integration/hooks/ent/card_update.go index 1ad9c7f10..d7c781db5 100644 --- a/entc/integration/hooks/ent/card_update.go +++ b/entc/integration/hooks/ent/card_update.go @@ -125,34 +125,7 @@ func (cu *CardUpdate) ClearOwner() *CardUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (cu *CardUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(cu.hooks) == 0 { - affected, err = cu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CardMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - cu.mutation = mutation - affected, err = cu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(cu.hooks) - 1; i >= 0; i-- { - if cu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = cu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, cu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, CardMutation](ctx, cu.sqlSave, cu.mutation, cu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -256,6 +229,7 @@ func (cu *CardUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + cu.mutation.done = true return n, nil } @@ -366,40 +340,7 @@ func (cuo *CardUpdateOne) Select(field string, fields ...string) *CardUpdateOne // Save executes the query and returns the updated Card entity. func (cuo *CardUpdateOne) Save(ctx context.Context) (*Card, error) { - var ( - err error - node *Card - ) - if len(cuo.hooks) == 0 { - node, err = cuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CardMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - cuo.mutation = mutation - node, err = cuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(cuo.hooks) - 1; i >= 0; i-- { - if cuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = cuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, cuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Card) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from CardMutation", v) - } - node = nv - } - return node, err + return withHooks[*Card, CardMutation](ctx, cuo.sqlSave, cuo.mutation, cuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -523,5 +464,6 @@ func (cuo *CardUpdateOne) sqlSave(ctx context.Context) (_node *Card, err error) } return nil, err } + cuo.mutation.done = true return _node, nil } diff --git a/entc/integration/hooks/ent/ent.go b/entc/integration/hooks/ent/ent.go index eae58df32..6b23f9fdf 100644 --- a/entc/integration/hooks/ent/ent.go +++ b/entc/integration/hooks/ent/ent.go @@ -477,6 +477,40 @@ func (s *selector) BoolX(ctx context.Context) bool { return v } +// withHooks invokes the builder operation with the given hooks, if any. +func withHooks[V Value, M any, PM interface { + *M + Mutation +}](ctx context.Context, exec func(context.Context) (V, error), mutation PM, hooks []Hook) (value V, err error) { + if len(hooks) == 0 { + return exec(ctx) + } + var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { + mutationT, ok := m.(PM) + if !ok { + return nil, fmt.Errorf("unexpected mutation type %T", m) + } + // Set the mutation to the builder. + *mutation = *mutationT + return exec(ctx) + }) + for i := len(hooks) - 1; i >= 0; i-- { + if hooks[i] == nil { + return value, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") + } + mut = hooks[i](mut) + } + v, err := mut.Mutate(ctx, mutation) + if err != nil { + return value, err + } + nv, ok := v.(V) + if !ok { + return value, fmt.Errorf("unexpected node type %T returned from %T", v, mutation) + } + return nv, nil +} + // newQueryContext returns a new context with the given QueryContext attached in case it does not exist. func newQueryContext(ctx context.Context, typ, op string) context.Context { if ent.QueryFromContext(ctx) == nil { diff --git a/entc/integration/hooks/ent/pet_create.go b/entc/integration/hooks/ent/pet_create.go index 3abf17f59..1e9e96c15 100644 --- a/entc/integration/hooks/ent/pet_create.go +++ b/entc/integration/hooks/ent/pet_create.go @@ -78,49 +78,7 @@ func (pc *PetCreate) Mutation() *PetMutation { // Save creates the Pet in the database. func (pc *PetCreate) Save(ctx context.Context) (*Pet, error) { - var ( - err error - node *Pet - ) - if len(pc.hooks) == 0 { - if err = pc.check(); err != nil { - return nil, err - } - node, err = pc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*PetMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = pc.check(); err != nil { - return nil, err - } - pc.mutation = mutation - if node, err = pc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(pc.hooks) - 1; i >= 0; i-- { - if pc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = pc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, pc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Pet) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from PetMutation", v) - } - node = nv - } - return node, err + return withHooks[*Pet, PetMutation](ctx, pc.sqlSave, pc.mutation, pc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -151,6 +109,9 @@ func (pc *PetCreate) check() error { } func (pc *PetCreate) sqlSave(ctx context.Context) (*Pet, error) { + if err := pc.check(); err != nil { + return nil, err + } _node, _spec := pc.createSpec() if err := sqlgraph.CreateNode(ctx, pc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -160,6 +121,8 @@ func (pc *PetCreate) sqlSave(ctx context.Context) (*Pet, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + pc.mutation.id = &_node.ID + pc.mutation.done = true return _node, nil } diff --git a/entc/integration/hooks/ent/pet_delete.go b/entc/integration/hooks/ent/pet_delete.go index 837386e02..f2ebb619d 100644 --- a/entc/integration/hooks/ent/pet_delete.go +++ b/entc/integration/hooks/ent/pet_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (pd *PetDelete) Where(ps ...predicate.Pet) *PetDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (pd *PetDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(pd.hooks) == 0 { - affected, err = pd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*PetMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - pd.mutation = mutation - affected, err = pd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(pd.hooks) - 1; i >= 0; i-- { - if pd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = pd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, pd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, PetMutation](ctx, pd.sqlExec, pd.mutation, pd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (pd *PetDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + pd.mutation.done = true return affected, err } diff --git a/entc/integration/hooks/ent/pet_update.go b/entc/integration/hooks/ent/pet_update.go index 24861ad4c..636f2b027 100644 --- a/entc/integration/hooks/ent/pet_update.go +++ b/entc/integration/hooks/ent/pet_update.go @@ -105,34 +105,7 @@ func (pu *PetUpdate) ClearOwner() *PetUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (pu *PetUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(pu.hooks) == 0 { - affected, err = pu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*PetMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - pu.mutation = mutation - affected, err = pu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(pu.hooks) - 1; i >= 0; i-- { - if pu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = pu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, pu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, PetMutation](ctx, pu.sqlSave, pu.mutation, pu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -230,6 +203,7 @@ func (pu *PetUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + pu.mutation.done = true return n, nil } @@ -320,40 +294,7 @@ func (puo *PetUpdateOne) Select(field string, fields ...string) *PetUpdateOne { // Save executes the query and returns the updated Pet entity. func (puo *PetUpdateOne) Save(ctx context.Context) (*Pet, error) { - var ( - err error - node *Pet - ) - if len(puo.hooks) == 0 { - node, err = puo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*PetMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - puo.mutation = mutation - node, err = puo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(puo.hooks) - 1; i >= 0; i-- { - if puo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = puo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, puo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Pet) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from PetMutation", v) - } - node = nv - } - return node, err + return withHooks[*Pet, PetMutation](ctx, puo.sqlSave, puo.mutation, puo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -471,5 +412,6 @@ func (puo *PetUpdateOne) sqlSave(ctx context.Context) (_node *Pet, err error) { } return nil, err } + puo.mutation.done = true return _node, nil } diff --git a/entc/integration/hooks/ent/user_create.go b/entc/integration/hooks/ent/user_create.go index 080fe9ea4..4499198f1 100644 --- a/entc/integration/hooks/ent/user_create.go +++ b/entc/integration/hooks/ent/user_create.go @@ -158,52 +158,10 @@ func (uc *UserCreate) Mutation() *UserMutation { // Save creates the User in the database. func (uc *UserCreate) Save(ctx context.Context) (*User, error) { - var ( - err error - node *User - ) if err := uc.defaults(); err != nil { return nil, err } - if len(uc.hooks) == 0 { - if err = uc.check(); err != nil { - return nil, err - } - node, err = uc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = uc.check(); err != nil { - return nil, err - } - uc.mutation = mutation - if node, err = uc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(uc.hooks) - 1; i >= 0; i-- { - if uc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*User) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) - } - node = nv - } - return node, err + return withHooks[*User, UserMutation](ctx, uc.sqlSave, uc.mutation, uc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -256,6 +214,9 @@ func (uc *UserCreate) check() error { } func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { + if err := uc.check(); err != nil { + return nil, err + } _node, _spec := uc.createSpec() if err := sqlgraph.CreateNode(ctx, uc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -265,6 +226,8 @@ func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + uc.mutation.id = &_node.ID + uc.mutation.done = true return _node, nil } diff --git a/entc/integration/hooks/ent/user_delete.go b/entc/integration/hooks/ent/user_delete.go index 1a8008824..c1a39de39 100644 --- a/entc/integration/hooks/ent/user_delete.go +++ b/entc/integration/hooks/ent/user_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (ud *UserDelete) Where(ps ...predicate.User) *UserDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (ud *UserDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(ud.hooks) == 0 { - affected, err = ud.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - ud.mutation = mutation - affected, err = ud.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(ud.hooks) - 1; i >= 0; i-- { - if ud.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ud.hooks[i](mut) - } - n, err := mut.Mutate(ctx, ud.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, UserMutation](ctx, ud.sqlExec, ud.mutation, ud.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (ud *UserDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + ud.mutation.done = true return affected, err } diff --git a/entc/integration/hooks/ent/user_update.go b/entc/integration/hooks/ent/user_update.go index 5b1338e77..fd0220390 100644 --- a/entc/integration/hooks/ent/user_update.go +++ b/entc/integration/hooks/ent/user_update.go @@ -261,34 +261,7 @@ func (uu *UserUpdate) ClearBestFriend() *UserUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (uu *UserUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(uu.hooks) == 0 { - affected, err = uu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - uu.mutation = mutation - affected, err = uu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(uu.hooks) - 1; i >= 0; i-- { - if uu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, uu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, UserMutation](ctx, uu.sqlSave, uu.mutation, uu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -563,6 +536,7 @@ func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + uu.mutation.done = true return n, nil } @@ -809,40 +783,7 @@ func (uuo *UserUpdateOne) Select(field string, fields ...string) *UserUpdateOne // Save executes the query and returns the updated User entity. func (uuo *UserUpdateOne) Save(ctx context.Context) (*User, error) { - var ( - err error - node *User - ) - if len(uuo.hooks) == 0 { - node, err = uuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - uuo.mutation = mutation - node, err = uuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(uuo.hooks) - 1; i >= 0; i-- { - if uuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*User) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) - } - node = nv - } - return node, err + return withHooks[*User, UserMutation](ctx, uuo.sqlSave, uuo.mutation, uuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -1137,5 +1078,6 @@ func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) } return nil, err } + uuo.mutation.done = true return _node, nil } diff --git a/entc/integration/idtype/ent/ent.go b/entc/integration/idtype/ent/ent.go index 8a8dd706c..d15f22927 100644 --- a/entc/integration/idtype/ent/ent.go +++ b/entc/integration/idtype/ent/ent.go @@ -473,6 +473,40 @@ func (s *selector) BoolX(ctx context.Context) bool { return v } +// withHooks invokes the builder operation with the given hooks, if any. +func withHooks[V Value, M any, PM interface { + *M + Mutation +}](ctx context.Context, exec func(context.Context) (V, error), mutation PM, hooks []Hook) (value V, err error) { + if len(hooks) == 0 { + return exec(ctx) + } + var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { + mutationT, ok := m.(PM) + if !ok { + return nil, fmt.Errorf("unexpected mutation type %T", m) + } + // Set the mutation to the builder. + *mutation = *mutationT + return exec(ctx) + }) + for i := len(hooks) - 1; i >= 0; i-- { + if hooks[i] == nil { + return value, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") + } + mut = hooks[i](mut) + } + v, err := mut.Mutate(ctx, mutation) + if err != nil { + return value, err + } + nv, ok := v.(V) + if !ok { + return value, fmt.Errorf("unexpected node type %T returned from %T", v, mutation) + } + return nv, nil +} + // newQueryContext returns a new context with the given QueryContext attached in case it does not exist. func newQueryContext(ctx context.Context, typ, op string) context.Context { if ent.QueryFromContext(ctx) == nil { diff --git a/entc/integration/idtype/ent/user_create.go b/entc/integration/idtype/ent/user_create.go index 61a15a67b..9013450c8 100644 --- a/entc/integration/idtype/ent/user_create.go +++ b/entc/integration/idtype/ent/user_create.go @@ -85,49 +85,7 @@ func (uc *UserCreate) Mutation() *UserMutation { // Save creates the User in the database. func (uc *UserCreate) Save(ctx context.Context) (*User, error) { - var ( - err error - node *User - ) - if len(uc.hooks) == 0 { - if err = uc.check(); err != nil { - return nil, err - } - node, err = uc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = uc.check(); err != nil { - return nil, err - } - uc.mutation = mutation - if node, err = uc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(uc.hooks) - 1; i >= 0; i-- { - if uc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*User) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) - } - node = nv - } - return node, err + return withHooks[*User, UserMutation](ctx, uc.sqlSave, uc.mutation, uc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -161,6 +119,9 @@ func (uc *UserCreate) check() error { } func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { + if err := uc.check(); err != nil { + return nil, err + } _node, _spec := uc.createSpec() if err := sqlgraph.CreateNode(ctx, uc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -170,6 +131,8 @@ func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { } id := _spec.ID.Value.(int64) _node.ID = uint64(id) + uc.mutation.id = &_node.ID + uc.mutation.done = true return _node, nil } diff --git a/entc/integration/idtype/ent/user_delete.go b/entc/integration/idtype/ent/user_delete.go index 23dcd01ba..d2e1ccdc4 100644 --- a/entc/integration/idtype/ent/user_delete.go +++ b/entc/integration/idtype/ent/user_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (ud *UserDelete) Where(ps ...predicate.User) *UserDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (ud *UserDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(ud.hooks) == 0 { - affected, err = ud.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - ud.mutation = mutation - affected, err = ud.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(ud.hooks) - 1; i >= 0; i-- { - if ud.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ud.hooks[i](mut) - } - n, err := mut.Mutate(ctx, ud.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, UserMutation](ctx, ud.sqlExec, ud.mutation, ud.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (ud *UserDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + ud.mutation.done = true return affected, err } diff --git a/entc/integration/idtype/ent/user_update.go b/entc/integration/idtype/ent/user_update.go index e3b835fb2..b0a4c7547 100644 --- a/entc/integration/idtype/ent/user_update.go +++ b/entc/integration/idtype/ent/user_update.go @@ -141,34 +141,7 @@ func (uu *UserUpdate) RemoveFollowing(u ...*User) *UserUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (uu *UserUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(uu.hooks) == 0 { - affected, err = uu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - uu.mutation = mutation - affected, err = uu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(uu.hooks) - 1; i >= 0; i-- { - if uu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, uu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, UserMutation](ctx, uu.sqlSave, uu.mutation, uu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -365,6 +338,7 @@ func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + uu.mutation.done = true return n, nil } @@ -493,40 +467,7 @@ func (uuo *UserUpdateOne) Select(field string, fields ...string) *UserUpdateOne // Save executes the query and returns the updated User entity. func (uuo *UserUpdateOne) Save(ctx context.Context) (*User, error) { - var ( - err error - node *User - ) - if len(uuo.hooks) == 0 { - node, err = uuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - uuo.mutation = mutation - node, err = uuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(uuo.hooks) - 1; i >= 0; i-- { - if uuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*User) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) - } - node = nv - } - return node, err + return withHooks[*User, UserMutation](ctx, uuo.sqlSave, uuo.mutation, uuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -743,5 +684,6 @@ func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) } return nil, err } + uuo.mutation.done = true return _node, nil } diff --git a/entc/integration/json/ent/ent.go b/entc/integration/json/ent/ent.go index 440fc2bb6..8b933b46a 100644 --- a/entc/integration/json/ent/ent.go +++ b/entc/integration/json/ent/ent.go @@ -473,6 +473,40 @@ func (s *selector) BoolX(ctx context.Context) bool { return v } +// withHooks invokes the builder operation with the given hooks, if any. +func withHooks[V Value, M any, PM interface { + *M + Mutation +}](ctx context.Context, exec func(context.Context) (V, error), mutation PM, hooks []Hook) (value V, err error) { + if len(hooks) == 0 { + return exec(ctx) + } + var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { + mutationT, ok := m.(PM) + if !ok { + return nil, fmt.Errorf("unexpected mutation type %T", m) + } + // Set the mutation to the builder. + *mutation = *mutationT + return exec(ctx) + }) + for i := len(hooks) - 1; i >= 0; i-- { + if hooks[i] == nil { + return value, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") + } + mut = hooks[i](mut) + } + v, err := mut.Mutate(ctx, mutation) + if err != nil { + return value, err + } + nv, ok := v.(V) + if !ok { + return value, fmt.Errorf("unexpected node type %T returned from %T", v, mutation) + } + return nv, nil +} + // newQueryContext returns a new context with the given QueryContext attached in case it does not exist. func newQueryContext(ctx context.Context, typ, op string) context.Context { if ent.QueryFromContext(ctx) == nil { diff --git a/entc/integration/json/ent/user_create.go b/entc/integration/json/ent/user_create.go index d04c00bac..a05a5e5df 100644 --- a/entc/integration/json/ent/user_create.go +++ b/entc/integration/json/ent/user_create.go @@ -96,50 +96,8 @@ func (uc *UserCreate) Mutation() *UserMutation { // Save creates the User in the database. func (uc *UserCreate) Save(ctx context.Context) (*User, error) { - var ( - err error - node *User - ) uc.defaults() - if len(uc.hooks) == 0 { - if err = uc.check(); err != nil { - return nil, err - } - node, err = uc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = uc.check(); err != nil { - return nil, err - } - uc.mutation = mutation - if node, err = uc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(uc.hooks) - 1; i >= 0; i-- { - if uc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*User) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) - } - node = nv - } - return node, err + return withHooks[*User, UserMutation](ctx, uc.sqlSave, uc.mutation, uc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -185,6 +143,9 @@ func (uc *UserCreate) check() error { } func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { + if err := uc.check(); err != nil { + return nil, err + } _node, _spec := uc.createSpec() if err := sqlgraph.CreateNode(ctx, uc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -194,6 +155,8 @@ func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + uc.mutation.id = &_node.ID + uc.mutation.done = true return _node, nil } diff --git a/entc/integration/json/ent/user_delete.go b/entc/integration/json/ent/user_delete.go index 8e35543d6..d02bb6f73 100644 --- a/entc/integration/json/ent/user_delete.go +++ b/entc/integration/json/ent/user_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (ud *UserDelete) Where(ps ...predicate.User) *UserDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (ud *UserDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(ud.hooks) == 0 { - affected, err = ud.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - ud.mutation = mutation - affected, err = ud.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(ud.hooks) - 1; i >= 0; i-- { - if ud.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ud.hooks[i](mut) - } - n, err := mut.Mutate(ctx, ud.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, UserMutation](ctx, ud.sqlExec, ud.mutation, ud.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (ud *UserDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + ud.mutation.done = true return affected, err } diff --git a/entc/integration/json/ent/user_update.go b/entc/integration/json/ent/user_update.go index 8e82b0993..97e48644c 100644 --- a/entc/integration/json/ent/user_update.go +++ b/entc/integration/json/ent/user_update.go @@ -190,34 +190,7 @@ func (uu *UserUpdate) Mutation() *UserMutation { // Save executes the query and returns the number of nodes affected by the update operation. func (uu *UserUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(uu.hooks) == 0 { - affected, err = uu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - uu.mutation = mutation - affected, err = uu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(uu.hooks) - 1; i >= 0; i-- { - if uu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, uu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, UserMutation](ctx, uu.sqlSave, uu.mutation, uu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -356,6 +329,7 @@ func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + uu.mutation.done = true return n, nil } @@ -528,40 +502,7 @@ func (uuo *UserUpdateOne) Select(field string, fields ...string) *UserUpdateOne // Save executes the query and returns the updated User entity. func (uuo *UserUpdateOne) Save(ctx context.Context) (*User, error) { - var ( - err error - node *User - ) - if len(uuo.hooks) == 0 { - node, err = uuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - uuo.mutation = mutation - node, err = uuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(uuo.hooks) - 1; i >= 0; i-- { - if uuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*User) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) - } - node = nv - } - return node, err + return withHooks[*User, UserMutation](ctx, uuo.sqlSave, uuo.mutation, uuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -720,5 +661,6 @@ func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) } return nil, err } + uuo.mutation.done = true return _node, nil } diff --git a/entc/integration/migrate/entv1/car_create.go b/entc/integration/migrate/entv1/car_create.go index f7f53c104..8e5596812 100644 --- a/entc/integration/migrate/entv1/car_create.go +++ b/entc/integration/migrate/entv1/car_create.go @@ -49,49 +49,7 @@ func (cc *CarCreate) Mutation() *CarMutation { // Save creates the Car in the database. func (cc *CarCreate) Save(ctx context.Context) (*Car, error) { - var ( - err error - node *Car - ) - if len(cc.hooks) == 0 { - if err = cc.check(); err != nil { - return nil, err - } - node, err = cc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CarMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = cc.check(); err != nil { - return nil, err - } - cc.mutation = mutation - if node, err = cc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(cc.hooks) - 1; i >= 0; i-- { - if cc.hooks[i] == nil { - return nil, fmt.Errorf("entv1: uninitialized hook (forgotten import entv1/runtime?)") - } - mut = cc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, cc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Car) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from CarMutation", v) - } - node = nv - } - return node, err + return withHooks[*Car, CarMutation](ctx, cc.sqlSave, cc.mutation, cc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -122,6 +80,9 @@ func (cc *CarCreate) check() error { } func (cc *CarCreate) sqlSave(ctx context.Context) (*Car, error) { + if err := cc.check(); err != nil { + return nil, err + } _node, _spec := cc.createSpec() if err := sqlgraph.CreateNode(ctx, cc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -131,6 +92,8 @@ func (cc *CarCreate) sqlSave(ctx context.Context) (*Car, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + cc.mutation.id = &_node.ID + cc.mutation.done = true return _node, nil } diff --git a/entc/integration/migrate/entv1/car_delete.go b/entc/integration/migrate/entv1/car_delete.go index 8da25c837..adfa60b7c 100644 --- a/entc/integration/migrate/entv1/car_delete.go +++ b/entc/integration/migrate/entv1/car_delete.go @@ -8,7 +8,6 @@ package entv1 import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (cd *CarDelete) Where(ps ...predicate.Car) *CarDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (cd *CarDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(cd.hooks) == 0 { - affected, err = cd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CarMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - cd.mutation = mutation - affected, err = cd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(cd.hooks) - 1; i >= 0; i-- { - if cd.hooks[i] == nil { - return 0, fmt.Errorf("entv1: uninitialized hook (forgotten import entv1/runtime?)") - } - mut = cd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, cd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, CarMutation](ctx, cd.sqlExec, cd.mutation, cd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (cd *CarDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + cd.mutation.done = true return affected, err } diff --git a/entc/integration/migrate/entv1/car_update.go b/entc/integration/migrate/entv1/car_update.go index 135e47b47..aa18ad87d 100644 --- a/entc/integration/migrate/entv1/car_update.go +++ b/entc/integration/migrate/entv1/car_update.go @@ -64,34 +64,7 @@ func (cu *CarUpdate) ClearOwner() *CarUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (cu *CarUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(cu.hooks) == 0 { - affected, err = cu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CarMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - cu.mutation = mutation - affected, err = cu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(cu.hooks) - 1; i >= 0; i-- { - if cu.hooks[i] == nil { - return 0, fmt.Errorf("entv1: uninitialized hook (forgotten import entv1/runtime?)") - } - mut = cu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, cu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, CarMutation](ctx, cu.sqlSave, cu.mutation, cu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -177,6 +150,7 @@ func (cu *CarUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + cu.mutation.done = true return n, nil } @@ -227,40 +201,7 @@ func (cuo *CarUpdateOne) Select(field string, fields ...string) *CarUpdateOne { // Save executes the query and returns the updated Car entity. func (cuo *CarUpdateOne) Save(ctx context.Context) (*Car, error) { - var ( - err error - node *Car - ) - if len(cuo.hooks) == 0 { - node, err = cuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CarMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - cuo.mutation = mutation - node, err = cuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(cuo.hooks) - 1; i >= 0; i-- { - if cuo.hooks[i] == nil { - return nil, fmt.Errorf("entv1: uninitialized hook (forgotten import entv1/runtime?)") - } - mut = cuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, cuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Car) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from CarMutation", v) - } - node = nv - } - return node, err + return withHooks[*Car, CarMutation](ctx, cuo.sqlSave, cuo.mutation, cuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -366,5 +307,6 @@ func (cuo *CarUpdateOne) sqlSave(ctx context.Context) (_node *Car, err error) { } return nil, err } + cuo.mutation.done = true return _node, nil } diff --git a/entc/integration/migrate/entv1/conversion_create.go b/entc/integration/migrate/entv1/conversion_create.go index d2ef32a59..6fc3f336b 100644 --- a/entc/integration/migrate/entv1/conversion_create.go +++ b/entc/integration/migrate/entv1/conversion_create.go @@ -155,49 +155,7 @@ func (cc *ConversionCreate) Mutation() *ConversionMutation { // Save creates the Conversion in the database. func (cc *ConversionCreate) Save(ctx context.Context) (*Conversion, error) { - var ( - err error - node *Conversion - ) - if len(cc.hooks) == 0 { - if err = cc.check(); err != nil { - return nil, err - } - node, err = cc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*ConversionMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = cc.check(); err != nil { - return nil, err - } - cc.mutation = mutation - if node, err = cc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(cc.hooks) - 1; i >= 0; i-- { - if cc.hooks[i] == nil { - return nil, fmt.Errorf("entv1: uninitialized hook (forgotten import entv1/runtime?)") - } - mut = cc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, cc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Conversion) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from ConversionMutation", v) - } - node = nv - } - return node, err + return withHooks[*Conversion, ConversionMutation](ctx, cc.sqlSave, cc.mutation, cc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -228,6 +186,9 @@ func (cc *ConversionCreate) check() error { } func (cc *ConversionCreate) sqlSave(ctx context.Context) (*Conversion, error) { + if err := cc.check(); err != nil { + return nil, err + } _node, _spec := cc.createSpec() if err := sqlgraph.CreateNode(ctx, cc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -237,6 +198,8 @@ func (cc *ConversionCreate) sqlSave(ctx context.Context) (*Conversion, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + cc.mutation.id = &_node.ID + cc.mutation.done = true return _node, nil } diff --git a/entc/integration/migrate/entv1/conversion_delete.go b/entc/integration/migrate/entv1/conversion_delete.go index fe26f11c0..c1b2f0af7 100644 --- a/entc/integration/migrate/entv1/conversion_delete.go +++ b/entc/integration/migrate/entv1/conversion_delete.go @@ -8,7 +8,6 @@ package entv1 import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (cd *ConversionDelete) Where(ps ...predicate.Conversion) *ConversionDelete // Exec executes the deletion query and returns how many vertices were deleted. func (cd *ConversionDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(cd.hooks) == 0 { - affected, err = cd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*ConversionMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - cd.mutation = mutation - affected, err = cd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(cd.hooks) - 1; i >= 0; i-- { - if cd.hooks[i] == nil { - return 0, fmt.Errorf("entv1: uninitialized hook (forgotten import entv1/runtime?)") - } - mut = cd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, cd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, ConversionMutation](ctx, cd.sqlExec, cd.mutation, cd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (cd *ConversionDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + cd.mutation.done = true return affected, err } diff --git a/entc/integration/migrate/entv1/conversion_update.go b/entc/integration/migrate/entv1/conversion_update.go index 55b3af9b5..90aafce03 100644 --- a/entc/integration/migrate/entv1/conversion_update.go +++ b/entc/integration/migrate/entv1/conversion_update.go @@ -274,34 +274,7 @@ func (cu *ConversionUpdate) Mutation() *ConversionMutation { // Save executes the query and returns the number of nodes affected by the update operation. func (cu *ConversionUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(cu.hooks) == 0 { - affected, err = cu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*ConversionMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - cu.mutation = mutation - affected, err = cu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(cu.hooks) - 1; i >= 0; i-- { - if cu.hooks[i] == nil { - return 0, fmt.Errorf("entv1: uninitialized hook (forgotten import entv1/runtime?)") - } - mut = cu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, cu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, ConversionMutation](ctx, cu.sqlSave, cu.mutation, cu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -430,6 +403,7 @@ func (cu *ConversionUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + cu.mutation.done = true return n, nil } @@ -691,40 +665,7 @@ func (cuo *ConversionUpdateOne) Select(field string, fields ...string) *Conversi // Save executes the query and returns the updated Conversion entity. func (cuo *ConversionUpdateOne) Save(ctx context.Context) (*Conversion, error) { - var ( - err error - node *Conversion - ) - if len(cuo.hooks) == 0 { - node, err = cuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*ConversionMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - cuo.mutation = mutation - node, err = cuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(cuo.hooks) - 1; i >= 0; i-- { - if cuo.hooks[i] == nil { - return nil, fmt.Errorf("entv1: uninitialized hook (forgotten import entv1/runtime?)") - } - mut = cuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, cuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Conversion) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from ConversionMutation", v) - } - node = nv - } - return node, err + return withHooks[*Conversion, ConversionMutation](ctx, cuo.sqlSave, cuo.mutation, cuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -873,5 +814,6 @@ func (cuo *ConversionUpdateOne) sqlSave(ctx context.Context) (_node *Conversion, } return nil, err } + cuo.mutation.done = true return _node, nil } diff --git a/entc/integration/migrate/entv1/customtype_create.go b/entc/integration/migrate/entv1/customtype_create.go index a320c09f5..a489d5134 100644 --- a/entc/integration/migrate/entv1/customtype_create.go +++ b/entc/integration/migrate/entv1/customtype_create.go @@ -43,49 +43,7 @@ func (ctc *CustomTypeCreate) Mutation() *CustomTypeMutation { // Save creates the CustomType in the database. func (ctc *CustomTypeCreate) Save(ctx context.Context) (*CustomType, error) { - var ( - err error - node *CustomType - ) - if len(ctc.hooks) == 0 { - if err = ctc.check(); err != nil { - return nil, err - } - node, err = ctc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CustomTypeMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = ctc.check(); err != nil { - return nil, err - } - ctc.mutation = mutation - if node, err = ctc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(ctc.hooks) - 1; i >= 0; i-- { - if ctc.hooks[i] == nil { - return nil, fmt.Errorf("entv1: uninitialized hook (forgotten import entv1/runtime?)") - } - mut = ctc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, ctc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*CustomType) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from CustomTypeMutation", v) - } - node = nv - } - return node, err + return withHooks[*CustomType, CustomTypeMutation](ctx, ctc.sqlSave, ctc.mutation, ctc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -116,6 +74,9 @@ func (ctc *CustomTypeCreate) check() error { } func (ctc *CustomTypeCreate) sqlSave(ctx context.Context) (*CustomType, error) { + if err := ctc.check(); err != nil { + return nil, err + } _node, _spec := ctc.createSpec() if err := sqlgraph.CreateNode(ctx, ctc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -125,6 +86,8 @@ func (ctc *CustomTypeCreate) sqlSave(ctx context.Context) (*CustomType, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + ctc.mutation.id = &_node.ID + ctc.mutation.done = true return _node, nil } diff --git a/entc/integration/migrate/entv1/customtype_delete.go b/entc/integration/migrate/entv1/customtype_delete.go index 80cabe405..c7d4c1890 100644 --- a/entc/integration/migrate/entv1/customtype_delete.go +++ b/entc/integration/migrate/entv1/customtype_delete.go @@ -8,7 +8,6 @@ package entv1 import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (ctd *CustomTypeDelete) Where(ps ...predicate.CustomType) *CustomTypeDelete // Exec executes the deletion query and returns how many vertices were deleted. func (ctd *CustomTypeDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(ctd.hooks) == 0 { - affected, err = ctd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CustomTypeMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - ctd.mutation = mutation - affected, err = ctd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(ctd.hooks) - 1; i >= 0; i-- { - if ctd.hooks[i] == nil { - return 0, fmt.Errorf("entv1: uninitialized hook (forgotten import entv1/runtime?)") - } - mut = ctd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, ctd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, CustomTypeMutation](ctx, ctd.sqlExec, ctd.mutation, ctd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (ctd *CustomTypeDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + ctd.mutation.done = true return affected, err } diff --git a/entc/integration/migrate/entv1/customtype_update.go b/entc/integration/migrate/entv1/customtype_update.go index 8ec5a3707..df5218c33 100644 --- a/entc/integration/migrate/entv1/customtype_update.go +++ b/entc/integration/migrate/entv1/customtype_update.go @@ -58,34 +58,7 @@ func (ctu *CustomTypeUpdate) Mutation() *CustomTypeMutation { // Save executes the query and returns the number of nodes affected by the update operation. func (ctu *CustomTypeUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(ctu.hooks) == 0 { - affected, err = ctu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CustomTypeMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - ctu.mutation = mutation - affected, err = ctu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(ctu.hooks) - 1; i >= 0; i-- { - if ctu.hooks[i] == nil { - return 0, fmt.Errorf("entv1: uninitialized hook (forgotten import entv1/runtime?)") - } - mut = ctu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, ctu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, CustomTypeMutation](ctx, ctu.sqlSave, ctu.mutation, ctu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -142,6 +115,7 @@ func (ctu *CustomTypeUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + ctu.mutation.done = true return n, nil } @@ -187,40 +161,7 @@ func (ctuo *CustomTypeUpdateOne) Select(field string, fields ...string) *CustomT // Save executes the query and returns the updated CustomType entity. func (ctuo *CustomTypeUpdateOne) Save(ctx context.Context) (*CustomType, error) { - var ( - err error - node *CustomType - ) - if len(ctuo.hooks) == 0 { - node, err = ctuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CustomTypeMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - ctuo.mutation = mutation - node, err = ctuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(ctuo.hooks) - 1; i >= 0; i-- { - if ctuo.hooks[i] == nil { - return nil, fmt.Errorf("entv1: uninitialized hook (forgotten import entv1/runtime?)") - } - mut = ctuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, ctuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*CustomType) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from CustomTypeMutation", v) - } - node = nv - } - return node, err + return withHooks[*CustomType, CustomTypeMutation](ctx, ctuo.sqlSave, ctuo.mutation, ctuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -297,5 +238,6 @@ func (ctuo *CustomTypeUpdateOne) sqlSave(ctx context.Context) (_node *CustomType } return nil, err } + ctuo.mutation.done = true return _node, nil } diff --git a/entc/integration/migrate/entv1/ent.go b/entc/integration/migrate/entv1/ent.go index 6401ad318..5c7909f26 100644 --- a/entc/integration/migrate/entv1/ent.go +++ b/entc/integration/migrate/entv1/ent.go @@ -479,6 +479,40 @@ func (s *selector) BoolX(ctx context.Context) bool { return v } +// withHooks invokes the builder operation with the given hooks, if any. +func withHooks[V Value, M any, PM interface { + *M + Mutation +}](ctx context.Context, exec func(context.Context) (V, error), mutation PM, hooks []Hook) (value V, err error) { + if len(hooks) == 0 { + return exec(ctx) + } + var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { + mutationT, ok := m.(PM) + if !ok { + return nil, fmt.Errorf("unexpected mutation type %T", m) + } + // Set the mutation to the builder. + *mutation = *mutationT + return exec(ctx) + }) + for i := len(hooks) - 1; i >= 0; i-- { + if hooks[i] == nil { + return value, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") + } + mut = hooks[i](mut) + } + v, err := mut.Mutate(ctx, mutation) + if err != nil { + return value, err + } + nv, ok := v.(V) + if !ok { + return value, fmt.Errorf("unexpected node type %T returned from %T", v, mutation) + } + return nv, nil +} + // newQueryContext returns a new context with the given QueryContext attached in case it does not exist. func newQueryContext(ctx context.Context, typ, op string) context.Context { if ent.QueryFromContext(ctx) == nil { diff --git a/entc/integration/migrate/entv1/user_create.go b/entc/integration/migrate/entv1/user_create.go index bbffa6bd3..cd3494471 100644 --- a/entc/integration/migrate/entv1/user_create.go +++ b/entc/integration/migrate/entv1/user_create.go @@ -245,50 +245,8 @@ func (uc *UserCreate) Mutation() *UserMutation { // Save creates the User in the database. func (uc *UserCreate) Save(ctx context.Context) (*User, error) { - var ( - err error - node *User - ) uc.defaults() - if len(uc.hooks) == 0 { - if err = uc.check(); err != nil { - return nil, err - } - node, err = uc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = uc.check(); err != nil { - return nil, err - } - uc.mutation = mutation - if node, err = uc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(uc.hooks) - 1; i >= 0; i-- { - if uc.hooks[i] == nil { - return nil, fmt.Errorf("entv1: uninitialized hook (forgotten import entv1/runtime?)") - } - mut = uc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*User) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) - } - node = nv - } - return node, err + return withHooks[*User, UserMutation](ctx, uc.sqlSave, uc.mutation, uc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -363,6 +321,9 @@ func (uc *UserCreate) check() error { } func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { + if err := uc.check(); err != nil { + return nil, err + } _node, _spec := uc.createSpec() if err := sqlgraph.CreateNode(ctx, uc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -374,6 +335,8 @@ func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { id := _spec.ID.Value.(int64) _node.ID = int(id) } + uc.mutation.id = &_node.ID + uc.mutation.done = true return _node, nil } diff --git a/entc/integration/migrate/entv1/user_delete.go b/entc/integration/migrate/entv1/user_delete.go index 1ca0ea06e..c045535c6 100644 --- a/entc/integration/migrate/entv1/user_delete.go +++ b/entc/integration/migrate/entv1/user_delete.go @@ -8,7 +8,6 @@ package entv1 import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (ud *UserDelete) Where(ps ...predicate.User) *UserDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (ud *UserDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(ud.hooks) == 0 { - affected, err = ud.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - ud.mutation = mutation - affected, err = ud.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(ud.hooks) - 1; i >= 0; i-- { - if ud.hooks[i] == nil { - return 0, fmt.Errorf("entv1: uninitialized hook (forgotten import entv1/runtime?)") - } - mut = ud.hooks[i](mut) - } - n, err := mut.Mutate(ctx, ud.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, UserMutation](ctx, ud.sqlExec, ud.mutation, ud.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (ud *UserDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + ud.mutation.done = true return affected, err } diff --git a/entc/integration/migrate/entv1/user_update.go b/entc/integration/migrate/entv1/user_update.go index 96e4838c0..54194e9d7 100644 --- a/entc/integration/migrate/entv1/user_update.go +++ b/entc/integration/migrate/entv1/user_update.go @@ -341,40 +341,7 @@ func (uu *UserUpdate) ClearCar() *UserUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (uu *UserUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(uu.hooks) == 0 { - if err = uu.check(); err != nil { - return 0, err - } - affected, err = uu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = uu.check(); err != nil { - return 0, err - } - uu.mutation = mutation - affected, err = uu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(uu.hooks) - 1; i >= 0; i-- { - if uu.hooks[i] == nil { - return 0, fmt.Errorf("entv1: uninitialized hook (forgotten import entv1/runtime?)") - } - mut = uu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, uu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, UserMutation](ctx, uu.sqlSave, uu.mutation, uu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -425,6 +392,9 @@ func (uu *UserUpdate) check() error { } func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) { + if err := uu.check(); err != nil { + return n, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: user.Table, @@ -672,6 +642,7 @@ func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + uu.mutation.done = true return n, nil } @@ -999,46 +970,7 @@ func (uuo *UserUpdateOne) Select(field string, fields ...string) *UserUpdateOne // Save executes the query and returns the updated User entity. func (uuo *UserUpdateOne) Save(ctx context.Context) (*User, error) { - var ( - err error - node *User - ) - if len(uuo.hooks) == 0 { - if err = uuo.check(); err != nil { - return nil, err - } - node, err = uuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = uuo.check(); err != nil { - return nil, err - } - uuo.mutation = mutation - node, err = uuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(uuo.hooks) - 1; i >= 0; i-- { - if uuo.hooks[i] == nil { - return nil, fmt.Errorf("entv1: uninitialized hook (forgotten import entv1/runtime?)") - } - mut = uuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*User) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) - } - node = nv - } - return node, err + return withHooks[*User, UserMutation](ctx, uuo.sqlSave, uuo.mutation, uuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -1089,6 +1021,9 @@ func (uuo *UserUpdateOne) check() error { } func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) { + if err := uuo.check(); err != nil { + return _node, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: user.Table, @@ -1356,5 +1291,6 @@ func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) } return nil, err } + uuo.mutation.done = true return _node, nil } diff --git a/entc/integration/migrate/entv2/blog_create.go b/entc/integration/migrate/entv2/blog_create.go index 2d2758042..72499f88c 100644 --- a/entc/integration/migrate/entv2/blog_create.go +++ b/entc/integration/migrate/entv2/blog_create.go @@ -51,49 +51,7 @@ func (bc *BlogCreate) Mutation() *BlogMutation { // Save creates the Blog in the database. func (bc *BlogCreate) Save(ctx context.Context) (*Blog, error) { - var ( - err error - node *Blog - ) - if len(bc.hooks) == 0 { - if err = bc.check(); err != nil { - return nil, err - } - node, err = bc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*BlogMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = bc.check(); err != nil { - return nil, err - } - bc.mutation = mutation - if node, err = bc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(bc.hooks) - 1; i >= 0; i-- { - if bc.hooks[i] == nil { - return nil, fmt.Errorf("entv2: uninitialized hook (forgotten import entv2/runtime?)") - } - mut = bc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, bc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Blog) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from BlogMutation", v) - } - node = nv - } - return node, err + return withHooks[*Blog, BlogMutation](ctx, bc.sqlSave, bc.mutation, bc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -124,6 +82,9 @@ func (bc *BlogCreate) check() error { } func (bc *BlogCreate) sqlSave(ctx context.Context) (*Blog, error) { + if err := bc.check(); err != nil { + return nil, err + } _node, _spec := bc.createSpec() if err := sqlgraph.CreateNode(ctx, bc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -135,6 +96,8 @@ func (bc *BlogCreate) sqlSave(ctx context.Context) (*Blog, error) { id := _spec.ID.Value.(int64) _node.ID = int(id) } + bc.mutation.id = &_node.ID + bc.mutation.done = true return _node, nil } diff --git a/entc/integration/migrate/entv2/blog_delete.go b/entc/integration/migrate/entv2/blog_delete.go index ecd428078..3efb90b36 100644 --- a/entc/integration/migrate/entv2/blog_delete.go +++ b/entc/integration/migrate/entv2/blog_delete.go @@ -8,7 +8,6 @@ package entv2 import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (bd *BlogDelete) Where(ps ...predicate.Blog) *BlogDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (bd *BlogDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(bd.hooks) == 0 { - affected, err = bd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*BlogMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - bd.mutation = mutation - affected, err = bd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(bd.hooks) - 1; i >= 0; i-- { - if bd.hooks[i] == nil { - return 0, fmt.Errorf("entv2: uninitialized hook (forgotten import entv2/runtime?)") - } - mut = bd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, bd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, BlogMutation](ctx, bd.sqlExec, bd.mutation, bd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (bd *BlogDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + bd.mutation.done = true return affected, err } diff --git a/entc/integration/migrate/entv2/blog_update.go b/entc/integration/migrate/entv2/blog_update.go index 73f845b95..50b420acd 100644 --- a/entc/integration/migrate/entv2/blog_update.go +++ b/entc/integration/migrate/entv2/blog_update.go @@ -75,34 +75,7 @@ func (bu *BlogUpdate) RemoveAdmins(u ...*User) *BlogUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (bu *BlogUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(bu.hooks) == 0 { - affected, err = bu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*BlogMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - bu.mutation = mutation - affected, err = bu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(bu.hooks) - 1; i >= 0; i-- { - if bu.hooks[i] == nil { - return 0, fmt.Errorf("entv2: uninitialized hook (forgotten import entv2/runtime?)") - } - mut = bu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, bu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, BlogMutation](ctx, bu.sqlSave, bu.mutation, bu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -207,6 +180,7 @@ func (bu *BlogUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + bu.mutation.done = true return n, nil } @@ -268,40 +242,7 @@ func (buo *BlogUpdateOne) Select(field string, fields ...string) *BlogUpdateOne // Save executes the query and returns the updated Blog entity. func (buo *BlogUpdateOne) Save(ctx context.Context) (*Blog, error) { - var ( - err error - node *Blog - ) - if len(buo.hooks) == 0 { - node, err = buo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*BlogMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - buo.mutation = mutation - node, err = buo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(buo.hooks) - 1; i >= 0; i-- { - if buo.hooks[i] == nil { - return nil, fmt.Errorf("entv2: uninitialized hook (forgotten import entv2/runtime?)") - } - mut = buo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, buo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Blog) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from BlogMutation", v) - } - node = nv - } - return node, err + return withHooks[*Blog, BlogMutation](ctx, buo.sqlSave, buo.mutation, buo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -426,5 +367,6 @@ func (buo *BlogUpdateOne) sqlSave(ctx context.Context) (_node *Blog, err error) } return nil, err } + buo.mutation.done = true return _node, nil } diff --git a/entc/integration/migrate/entv2/car_create.go b/entc/integration/migrate/entv2/car_create.go index ec9ffab34..e67c4f4f9 100644 --- a/entc/integration/migrate/entv2/car_create.go +++ b/entc/integration/migrate/entv2/car_create.go @@ -56,49 +56,7 @@ func (cc *CarCreate) Mutation() *CarMutation { // Save creates the Car in the database. func (cc *CarCreate) Save(ctx context.Context) (*Car, error) { - var ( - err error - node *Car - ) - if len(cc.hooks) == 0 { - if err = cc.check(); err != nil { - return nil, err - } - node, err = cc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CarMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = cc.check(); err != nil { - return nil, err - } - cc.mutation = mutation - if node, err = cc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(cc.hooks) - 1; i >= 0; i-- { - if cc.hooks[i] == nil { - return nil, fmt.Errorf("entv2: uninitialized hook (forgotten import entv2/runtime?)") - } - mut = cc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, cc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Car) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from CarMutation", v) - } - node = nv - } - return node, err + return withHooks[*Car, CarMutation](ctx, cc.sqlSave, cc.mutation, cc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -132,6 +90,9 @@ func (cc *CarCreate) check() error { } func (cc *CarCreate) sqlSave(ctx context.Context) (*Car, error) { + if err := cc.check(); err != nil { + return nil, err + } _node, _spec := cc.createSpec() if err := sqlgraph.CreateNode(ctx, cc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -141,6 +102,8 @@ func (cc *CarCreate) sqlSave(ctx context.Context) (*Car, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + cc.mutation.id = &_node.ID + cc.mutation.done = true return _node, nil } diff --git a/entc/integration/migrate/entv2/car_delete.go b/entc/integration/migrate/entv2/car_delete.go index 0fd378cd5..18d07f09b 100644 --- a/entc/integration/migrate/entv2/car_delete.go +++ b/entc/integration/migrate/entv2/car_delete.go @@ -8,7 +8,6 @@ package entv2 import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (cd *CarDelete) Where(ps ...predicate.Car) *CarDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (cd *CarDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(cd.hooks) == 0 { - affected, err = cd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CarMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - cd.mutation = mutation - affected, err = cd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(cd.hooks) - 1; i >= 0; i-- { - if cd.hooks[i] == nil { - return 0, fmt.Errorf("entv2: uninitialized hook (forgotten import entv2/runtime?)") - } - mut = cd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, cd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, CarMutation](ctx, cd.sqlExec, cd.mutation, cd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (cd *CarDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + cd.mutation.done = true return affected, err } diff --git a/entc/integration/migrate/entv2/car_update.go b/entc/integration/migrate/entv2/car_update.go index 162a508e3..4297778ed 100644 --- a/entc/integration/migrate/entv2/car_update.go +++ b/entc/integration/migrate/entv2/car_update.go @@ -76,40 +76,7 @@ func (cu *CarUpdate) ClearOwner() *CarUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (cu *CarUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(cu.hooks) == 0 { - if err = cu.check(); err != nil { - return 0, err - } - affected, err = cu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CarMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = cu.check(); err != nil { - return 0, err - } - cu.mutation = mutation - affected, err = cu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(cu.hooks) - 1; i >= 0; i-- { - if cu.hooks[i] == nil { - return 0, fmt.Errorf("entv2: uninitialized hook (forgotten import entv2/runtime?)") - } - mut = cu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, cu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, CarMutation](ctx, cu.sqlSave, cu.mutation, cu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -143,6 +110,9 @@ func (cu *CarUpdate) check() error { } func (cu *CarUpdate) sqlSave(ctx context.Context) (n int, err error) { + if err := cu.check(); err != nil { + return n, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: car.Table, @@ -209,6 +179,7 @@ func (cu *CarUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + cu.mutation.done = true return n, nil } @@ -271,46 +242,7 @@ func (cuo *CarUpdateOne) Select(field string, fields ...string) *CarUpdateOne { // Save executes the query and returns the updated Car entity. func (cuo *CarUpdateOne) Save(ctx context.Context) (*Car, error) { - var ( - err error - node *Car - ) - if len(cuo.hooks) == 0 { - if err = cuo.check(); err != nil { - return nil, err - } - node, err = cuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CarMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = cuo.check(); err != nil { - return nil, err - } - cuo.mutation = mutation - node, err = cuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(cuo.hooks) - 1; i >= 0; i-- { - if cuo.hooks[i] == nil { - return nil, fmt.Errorf("entv2: uninitialized hook (forgotten import entv2/runtime?)") - } - mut = cuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, cuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Car) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from CarMutation", v) - } - node = nv - } - return node, err + return withHooks[*Car, CarMutation](ctx, cuo.sqlSave, cuo.mutation, cuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -344,6 +276,9 @@ func (cuo *CarUpdateOne) check() error { } func (cuo *CarUpdateOne) sqlSave(ctx context.Context) (_node *Car, err error) { + if err := cuo.check(); err != nil { + return _node, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: car.Table, @@ -430,5 +365,6 @@ func (cuo *CarUpdateOne) sqlSave(ctx context.Context) (_node *Car, err error) { } return nil, err } + cuo.mutation.done = true return _node, nil } diff --git a/entc/integration/migrate/entv2/conversion_create.go b/entc/integration/migrate/entv2/conversion_create.go index 36a57c1de..2d62802ac 100644 --- a/entc/integration/migrate/entv2/conversion_create.go +++ b/entc/integration/migrate/entv2/conversion_create.go @@ -155,49 +155,7 @@ func (cc *ConversionCreate) Mutation() *ConversionMutation { // Save creates the Conversion in the database. func (cc *ConversionCreate) Save(ctx context.Context) (*Conversion, error) { - var ( - err error - node *Conversion - ) - if len(cc.hooks) == 0 { - if err = cc.check(); err != nil { - return nil, err - } - node, err = cc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*ConversionMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = cc.check(); err != nil { - return nil, err - } - cc.mutation = mutation - if node, err = cc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(cc.hooks) - 1; i >= 0; i-- { - if cc.hooks[i] == nil { - return nil, fmt.Errorf("entv2: uninitialized hook (forgotten import entv2/runtime?)") - } - mut = cc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, cc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Conversion) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from ConversionMutation", v) - } - node = nv - } - return node, err + return withHooks[*Conversion, ConversionMutation](ctx, cc.sqlSave, cc.mutation, cc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -228,6 +186,9 @@ func (cc *ConversionCreate) check() error { } func (cc *ConversionCreate) sqlSave(ctx context.Context) (*Conversion, error) { + if err := cc.check(); err != nil { + return nil, err + } _node, _spec := cc.createSpec() if err := sqlgraph.CreateNode(ctx, cc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -237,6 +198,8 @@ func (cc *ConversionCreate) sqlSave(ctx context.Context) (*Conversion, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + cc.mutation.id = &_node.ID + cc.mutation.done = true return _node, nil } diff --git a/entc/integration/migrate/entv2/conversion_delete.go b/entc/integration/migrate/entv2/conversion_delete.go index 77f85b9fa..b85e97459 100644 --- a/entc/integration/migrate/entv2/conversion_delete.go +++ b/entc/integration/migrate/entv2/conversion_delete.go @@ -8,7 +8,6 @@ package entv2 import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (cd *ConversionDelete) Where(ps ...predicate.Conversion) *ConversionDelete // Exec executes the deletion query and returns how many vertices were deleted. func (cd *ConversionDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(cd.hooks) == 0 { - affected, err = cd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*ConversionMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - cd.mutation = mutation - affected, err = cd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(cd.hooks) - 1; i >= 0; i-- { - if cd.hooks[i] == nil { - return 0, fmt.Errorf("entv2: uninitialized hook (forgotten import entv2/runtime?)") - } - mut = cd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, cd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, ConversionMutation](ctx, cd.sqlExec, cd.mutation, cd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (cd *ConversionDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + cd.mutation.done = true return affected, err } diff --git a/entc/integration/migrate/entv2/conversion_update.go b/entc/integration/migrate/entv2/conversion_update.go index b8dd4a142..89701d616 100644 --- a/entc/integration/migrate/entv2/conversion_update.go +++ b/entc/integration/migrate/entv2/conversion_update.go @@ -218,34 +218,7 @@ func (cu *ConversionUpdate) Mutation() *ConversionMutation { // Save executes the query and returns the number of nodes affected by the update operation. func (cu *ConversionUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(cu.hooks) == 0 { - affected, err = cu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*ConversionMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - cu.mutation = mutation - affected, err = cu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(cu.hooks) - 1; i >= 0; i-- { - if cu.hooks[i] == nil { - return 0, fmt.Errorf("entv2: uninitialized hook (forgotten import entv2/runtime?)") - } - mut = cu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, cu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, ConversionMutation](ctx, cu.sqlSave, cu.mutation, cu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -350,6 +323,7 @@ func (cu *ConversionUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + cu.mutation.done = true return n, nil } @@ -555,40 +529,7 @@ func (cuo *ConversionUpdateOne) Select(field string, fields ...string) *Conversi // Save executes the query and returns the updated Conversion entity. func (cuo *ConversionUpdateOne) Save(ctx context.Context) (*Conversion, error) { - var ( - err error - node *Conversion - ) - if len(cuo.hooks) == 0 { - node, err = cuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*ConversionMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - cuo.mutation = mutation - node, err = cuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(cuo.hooks) - 1; i >= 0; i-- { - if cuo.hooks[i] == nil { - return nil, fmt.Errorf("entv2: uninitialized hook (forgotten import entv2/runtime?)") - } - mut = cuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, cuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Conversion) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from ConversionMutation", v) - } - node = nv - } - return node, err + return withHooks[*Conversion, ConversionMutation](ctx, cuo.sqlSave, cuo.mutation, cuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -713,5 +654,6 @@ func (cuo *ConversionUpdateOne) sqlSave(ctx context.Context) (_node *Conversion, } return nil, err } + cuo.mutation.done = true return _node, nil } diff --git a/entc/integration/migrate/entv2/customtype_create.go b/entc/integration/migrate/entv2/customtype_create.go index ab498eba7..7ecf58cf7 100644 --- a/entc/integration/migrate/entv2/customtype_create.go +++ b/entc/integration/migrate/entv2/customtype_create.go @@ -72,49 +72,7 @@ func (ctc *CustomTypeCreate) Mutation() *CustomTypeMutation { // Save creates the CustomType in the database. func (ctc *CustomTypeCreate) Save(ctx context.Context) (*CustomType, error) { - var ( - err error - node *CustomType - ) - if len(ctc.hooks) == 0 { - if err = ctc.check(); err != nil { - return nil, err - } - node, err = ctc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CustomTypeMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = ctc.check(); err != nil { - return nil, err - } - ctc.mutation = mutation - if node, err = ctc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(ctc.hooks) - 1; i >= 0; i-- { - if ctc.hooks[i] == nil { - return nil, fmt.Errorf("entv2: uninitialized hook (forgotten import entv2/runtime?)") - } - mut = ctc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, ctc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*CustomType) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from CustomTypeMutation", v) - } - node = nv - } - return node, err + return withHooks[*CustomType, CustomTypeMutation](ctx, ctc.sqlSave, ctc.mutation, ctc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -145,6 +103,9 @@ func (ctc *CustomTypeCreate) check() error { } func (ctc *CustomTypeCreate) sqlSave(ctx context.Context) (*CustomType, error) { + if err := ctc.check(); err != nil { + return nil, err + } _node, _spec := ctc.createSpec() if err := sqlgraph.CreateNode(ctx, ctc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -154,6 +115,8 @@ func (ctc *CustomTypeCreate) sqlSave(ctx context.Context) (*CustomType, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + ctc.mutation.id = &_node.ID + ctc.mutation.done = true return _node, nil } diff --git a/entc/integration/migrate/entv2/customtype_delete.go b/entc/integration/migrate/entv2/customtype_delete.go index ca2c5ab31..684a883b4 100644 --- a/entc/integration/migrate/entv2/customtype_delete.go +++ b/entc/integration/migrate/entv2/customtype_delete.go @@ -8,7 +8,6 @@ package entv2 import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (ctd *CustomTypeDelete) Where(ps ...predicate.CustomType) *CustomTypeDelete // Exec executes the deletion query and returns how many vertices were deleted. func (ctd *CustomTypeDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(ctd.hooks) == 0 { - affected, err = ctd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CustomTypeMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - ctd.mutation = mutation - affected, err = ctd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(ctd.hooks) - 1; i >= 0; i-- { - if ctd.hooks[i] == nil { - return 0, fmt.Errorf("entv2: uninitialized hook (forgotten import entv2/runtime?)") - } - mut = ctd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, ctd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, CustomTypeMutation](ctx, ctd.sqlExec, ctd.mutation, ctd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (ctd *CustomTypeDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + ctd.mutation.done = true return affected, err } diff --git a/entc/integration/migrate/entv2/customtype_update.go b/entc/integration/migrate/entv2/customtype_update.go index 841e34a40..8ec770a35 100644 --- a/entc/integration/migrate/entv2/customtype_update.go +++ b/entc/integration/migrate/entv2/customtype_update.go @@ -99,34 +99,7 @@ func (ctu *CustomTypeUpdate) Mutation() *CustomTypeMutation { // Save executes the query and returns the number of nodes affected by the update operation. func (ctu *CustomTypeUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(ctu.hooks) == 0 { - affected, err = ctu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CustomTypeMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - ctu.mutation = mutation - affected, err = ctu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(ctu.hooks) - 1; i >= 0; i-- { - if ctu.hooks[i] == nil { - return 0, fmt.Errorf("entv2: uninitialized hook (forgotten import entv2/runtime?)") - } - mut = ctu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, ctu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, CustomTypeMutation](ctx, ctu.sqlSave, ctu.mutation, ctu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -195,6 +168,7 @@ func (ctu *CustomTypeUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + ctu.mutation.done = true return n, nil } @@ -280,40 +254,7 @@ func (ctuo *CustomTypeUpdateOne) Select(field string, fields ...string) *CustomT // Save executes the query and returns the updated CustomType entity. func (ctuo *CustomTypeUpdateOne) Save(ctx context.Context) (*CustomType, error) { - var ( - err error - node *CustomType - ) - if len(ctuo.hooks) == 0 { - node, err = ctuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CustomTypeMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - ctuo.mutation = mutation - node, err = ctuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(ctuo.hooks) - 1; i >= 0; i-- { - if ctuo.hooks[i] == nil { - return nil, fmt.Errorf("entv2: uninitialized hook (forgotten import entv2/runtime?)") - } - mut = ctuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, ctuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*CustomType) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from CustomTypeMutation", v) - } - node = nv - } - return node, err + return withHooks[*CustomType, CustomTypeMutation](ctx, ctuo.sqlSave, ctuo.mutation, ctuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -402,5 +343,6 @@ func (ctuo *CustomTypeUpdateOne) sqlSave(ctx context.Context) (_node *CustomType } return nil, err } + ctuo.mutation.done = true return _node, nil } diff --git a/entc/integration/migrate/entv2/ent.go b/entc/integration/migrate/entv2/ent.go index 46475a80d..0a36f3efc 100644 --- a/entc/integration/migrate/entv2/ent.go +++ b/entc/integration/migrate/entv2/ent.go @@ -489,6 +489,40 @@ func (s *selector) BoolX(ctx context.Context) bool { return v } +// withHooks invokes the builder operation with the given hooks, if any. +func withHooks[V Value, M any, PM interface { + *M + Mutation +}](ctx context.Context, exec func(context.Context) (V, error), mutation PM, hooks []Hook) (value V, err error) { + if len(hooks) == 0 { + return exec(ctx) + } + var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { + mutationT, ok := m.(PM) + if !ok { + return nil, fmt.Errorf("unexpected mutation type %T", m) + } + // Set the mutation to the builder. + *mutation = *mutationT + return exec(ctx) + }) + for i := len(hooks) - 1; i >= 0; i-- { + if hooks[i] == nil { + return value, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") + } + mut = hooks[i](mut) + } + v, err := mut.Mutate(ctx, mutation) + if err != nil { + return value, err + } + nv, ok := v.(V) + if !ok { + return value, fmt.Errorf("unexpected node type %T returned from %T", v, mutation) + } + return nv, nil +} + // newQueryContext returns a new context with the given QueryContext attached in case it does not exist. func newQueryContext(ctx context.Context, typ, op string) context.Context { if ent.QueryFromContext(ctx) == nil { diff --git a/entc/integration/migrate/entv2/group_create.go b/entc/integration/migrate/entv2/group_create.go index 820b14cf2..474f47891 100644 --- a/entc/integration/migrate/entv2/group_create.go +++ b/entc/integration/migrate/entv2/group_create.go @@ -29,49 +29,7 @@ func (gc *GroupCreate) Mutation() *GroupMutation { // Save creates the Group in the database. func (gc *GroupCreate) Save(ctx context.Context) (*Group, error) { - var ( - err error - node *Group - ) - if len(gc.hooks) == 0 { - if err = gc.check(); err != nil { - return nil, err - } - node, err = gc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GroupMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = gc.check(); err != nil { - return nil, err - } - gc.mutation = mutation - if node, err = gc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(gc.hooks) - 1; i >= 0; i-- { - if gc.hooks[i] == nil { - return nil, fmt.Errorf("entv2: uninitialized hook (forgotten import entv2/runtime?)") - } - mut = gc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, gc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Group) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from GroupMutation", v) - } - node = nv - } - return node, err + return withHooks[*Group, GroupMutation](ctx, gc.sqlSave, gc.mutation, gc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -102,6 +60,9 @@ func (gc *GroupCreate) check() error { } func (gc *GroupCreate) sqlSave(ctx context.Context) (*Group, error) { + if err := gc.check(); err != nil { + return nil, err + } _node, _spec := gc.createSpec() if err := sqlgraph.CreateNode(ctx, gc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -111,6 +72,8 @@ func (gc *GroupCreate) sqlSave(ctx context.Context) (*Group, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + gc.mutation.id = &_node.ID + gc.mutation.done = true return _node, nil } diff --git a/entc/integration/migrate/entv2/group_delete.go b/entc/integration/migrate/entv2/group_delete.go index 8f0f00385..c33525153 100644 --- a/entc/integration/migrate/entv2/group_delete.go +++ b/entc/integration/migrate/entv2/group_delete.go @@ -8,7 +8,6 @@ package entv2 import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (gd *GroupDelete) Where(ps ...predicate.Group) *GroupDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (gd *GroupDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(gd.hooks) == 0 { - affected, err = gd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GroupMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - gd.mutation = mutation - affected, err = gd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(gd.hooks) - 1; i >= 0; i-- { - if gd.hooks[i] == nil { - return 0, fmt.Errorf("entv2: uninitialized hook (forgotten import entv2/runtime?)") - } - mut = gd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, gd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, GroupMutation](ctx, gd.sqlExec, gd.mutation, gd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (gd *GroupDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + gd.mutation.done = true return affected, err } diff --git a/entc/integration/migrate/entv2/group_update.go b/entc/integration/migrate/entv2/group_update.go index 6aeda94a0..9a5c2f866 100644 --- a/entc/integration/migrate/entv2/group_update.go +++ b/entc/integration/migrate/entv2/group_update.go @@ -38,34 +38,7 @@ func (gu *GroupUpdate) Mutation() *GroupMutation { // Save executes the query and returns the number of nodes affected by the update operation. func (gu *GroupUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(gu.hooks) == 0 { - affected, err = gu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GroupMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - gu.mutation = mutation - affected, err = gu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(gu.hooks) - 1; i >= 0; i-- { - if gu.hooks[i] == nil { - return 0, fmt.Errorf("entv2: uninitialized hook (forgotten import entv2/runtime?)") - } - mut = gu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, gu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, GroupMutation](ctx, gu.sqlSave, gu.mutation, gu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -116,6 +89,7 @@ func (gu *GroupUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + gu.mutation.done = true return n, nil } @@ -141,40 +115,7 @@ func (guo *GroupUpdateOne) Select(field string, fields ...string) *GroupUpdateOn // Save executes the query and returns the updated Group entity. func (guo *GroupUpdateOne) Save(ctx context.Context) (*Group, error) { - var ( - err error - node *Group - ) - if len(guo.hooks) == 0 { - node, err = guo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GroupMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - guo.mutation = mutation - node, err = guo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(guo.hooks) - 1; i >= 0; i-- { - if guo.hooks[i] == nil { - return nil, fmt.Errorf("entv2: uninitialized hook (forgotten import entv2/runtime?)") - } - mut = guo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, guo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Group) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from GroupMutation", v) - } - node = nv - } - return node, err + return withHooks[*Group, GroupMutation](ctx, guo.sqlSave, guo.mutation, guo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -245,5 +186,6 @@ func (guo *GroupUpdateOne) sqlSave(ctx context.Context) (_node *Group, err error } return nil, err } + guo.mutation.done = true return _node, nil } diff --git a/entc/integration/migrate/entv2/media_create.go b/entc/integration/migrate/entv2/media_create.go index 94c75d3ac..8ee6d5e4f 100644 --- a/entc/integration/migrate/entv2/media_create.go +++ b/entc/integration/migrate/entv2/media_create.go @@ -71,49 +71,7 @@ func (mc *MediaCreate) Mutation() *MediaMutation { // Save creates the Media in the database. func (mc *MediaCreate) Save(ctx context.Context) (*Media, error) { - var ( - err error - node *Media - ) - if len(mc.hooks) == 0 { - if err = mc.check(); err != nil { - return nil, err - } - node, err = mc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*MediaMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = mc.check(); err != nil { - return nil, err - } - mc.mutation = mutation - if node, err = mc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(mc.hooks) - 1; i >= 0; i-- { - if mc.hooks[i] == nil { - return nil, fmt.Errorf("entv2: uninitialized hook (forgotten import entv2/runtime?)") - } - mut = mc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, mc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Media) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from MediaMutation", v) - } - node = nv - } - return node, err + return withHooks[*Media, MediaMutation](ctx, mc.sqlSave, mc.mutation, mc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -144,6 +102,9 @@ func (mc *MediaCreate) check() error { } func (mc *MediaCreate) sqlSave(ctx context.Context) (*Media, error) { + if err := mc.check(); err != nil { + return nil, err + } _node, _spec := mc.createSpec() if err := sqlgraph.CreateNode(ctx, mc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -153,6 +114,8 @@ func (mc *MediaCreate) sqlSave(ctx context.Context) (*Media, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + mc.mutation.id = &_node.ID + mc.mutation.done = true return _node, nil } diff --git a/entc/integration/migrate/entv2/media_delete.go b/entc/integration/migrate/entv2/media_delete.go index 27d6edc2c..2e9b6af45 100644 --- a/entc/integration/migrate/entv2/media_delete.go +++ b/entc/integration/migrate/entv2/media_delete.go @@ -8,7 +8,6 @@ package entv2 import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (md *MediaDelete) Where(ps ...predicate.Media) *MediaDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (md *MediaDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(md.hooks) == 0 { - affected, err = md.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*MediaMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - md.mutation = mutation - affected, err = md.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(md.hooks) - 1; i >= 0; i-- { - if md.hooks[i] == nil { - return 0, fmt.Errorf("entv2: uninitialized hook (forgotten import entv2/runtime?)") - } - mut = md.hooks[i](mut) - } - n, err := mut.Mutate(ctx, md.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, MediaMutation](ctx, md.sqlExec, md.mutation, md.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (md *MediaDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + md.mutation.done = true return affected, err } diff --git a/entc/integration/migrate/entv2/media_update.go b/entc/integration/migrate/entv2/media_update.go index 5a4b0c3f5..b6aff558d 100644 --- a/entc/integration/migrate/entv2/media_update.go +++ b/entc/integration/migrate/entv2/media_update.go @@ -98,34 +98,7 @@ func (mu *MediaUpdate) Mutation() *MediaMutation { // Save executes the query and returns the number of nodes affected by the update operation. func (mu *MediaUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(mu.hooks) == 0 { - affected, err = mu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*MediaMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - mu.mutation = mutation - affected, err = mu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(mu.hooks) - 1; i >= 0; i-- { - if mu.hooks[i] == nil { - return 0, fmt.Errorf("entv2: uninitialized hook (forgotten import entv2/runtime?)") - } - mut = mu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, mu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, MediaMutation](ctx, mu.sqlSave, mu.mutation, mu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -194,6 +167,7 @@ func (mu *MediaUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + mu.mutation.done = true return n, nil } @@ -279,40 +253,7 @@ func (muo *MediaUpdateOne) Select(field string, fields ...string) *MediaUpdateOn // Save executes the query and returns the updated Media entity. func (muo *MediaUpdateOne) Save(ctx context.Context) (*Media, error) { - var ( - err error - node *Media - ) - if len(muo.hooks) == 0 { - node, err = muo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*MediaMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - muo.mutation = mutation - node, err = muo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(muo.hooks) - 1; i >= 0; i-- { - if muo.hooks[i] == nil { - return nil, fmt.Errorf("entv2: uninitialized hook (forgotten import entv2/runtime?)") - } - mut = muo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, muo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Media) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from MediaMutation", v) - } - node = nv - } - return node, err + return withHooks[*Media, MediaMutation](ctx, muo.sqlSave, muo.mutation, muo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -401,5 +342,6 @@ func (muo *MediaUpdateOne) sqlSave(ctx context.Context) (_node *Media, err error } return nil, err } + muo.mutation.done = true return _node, nil } diff --git a/entc/integration/migrate/entv2/pet_create.go b/entc/integration/migrate/entv2/pet_create.go index 72751ffd7..a45226f97 100644 --- a/entc/integration/migrate/entv2/pet_create.go +++ b/entc/integration/migrate/entv2/pet_create.go @@ -63,49 +63,7 @@ func (pc *PetCreate) Mutation() *PetMutation { // Save creates the Pet in the database. func (pc *PetCreate) Save(ctx context.Context) (*Pet, error) { - var ( - err error - node *Pet - ) - if len(pc.hooks) == 0 { - if err = pc.check(); err != nil { - return nil, err - } - node, err = pc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*PetMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = pc.check(); err != nil { - return nil, err - } - pc.mutation = mutation - if node, err = pc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(pc.hooks) - 1; i >= 0; i-- { - if pc.hooks[i] == nil { - return nil, fmt.Errorf("entv2: uninitialized hook (forgotten import entv2/runtime?)") - } - mut = pc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, pc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Pet) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from PetMutation", v) - } - node = nv - } - return node, err + return withHooks[*Pet, PetMutation](ctx, pc.sqlSave, pc.mutation, pc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -136,6 +94,9 @@ func (pc *PetCreate) check() error { } func (pc *PetCreate) sqlSave(ctx context.Context) (*Pet, error) { + if err := pc.check(); err != nil { + return nil, err + } _node, _spec := pc.createSpec() if err := sqlgraph.CreateNode(ctx, pc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -145,6 +106,8 @@ func (pc *PetCreate) sqlSave(ctx context.Context) (*Pet, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + pc.mutation.id = &_node.ID + pc.mutation.done = true return _node, nil } diff --git a/entc/integration/migrate/entv2/pet_delete.go b/entc/integration/migrate/entv2/pet_delete.go index 19eaf119e..a0817616b 100644 --- a/entc/integration/migrate/entv2/pet_delete.go +++ b/entc/integration/migrate/entv2/pet_delete.go @@ -8,7 +8,6 @@ package entv2 import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (pd *PetDelete) Where(ps ...predicate.Pet) *PetDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (pd *PetDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(pd.hooks) == 0 { - affected, err = pd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*PetMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - pd.mutation = mutation - affected, err = pd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(pd.hooks) - 1; i >= 0; i-- { - if pd.hooks[i] == nil { - return 0, fmt.Errorf("entv2: uninitialized hook (forgotten import entv2/runtime?)") - } - mut = pd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, pd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, PetMutation](ctx, pd.sqlExec, pd.mutation, pd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (pd *PetDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + pd.mutation.done = true return affected, err } diff --git a/entc/integration/migrate/entv2/pet_update.go b/entc/integration/migrate/entv2/pet_update.go index 0d3853e16..09bf49c24 100644 --- a/entc/integration/migrate/entv2/pet_update.go +++ b/entc/integration/migrate/entv2/pet_update.go @@ -84,34 +84,7 @@ func (pu *PetUpdate) ClearOwner() *PetUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (pu *PetUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(pu.hooks) == 0 { - affected, err = pu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*PetMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - pu.mutation = mutation - affected, err = pu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(pu.hooks) - 1; i >= 0; i-- { - if pu.hooks[i] == nil { - return 0, fmt.Errorf("entv2: uninitialized hook (forgotten import entv2/runtime?)") - } - mut = pu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, pu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, PetMutation](ctx, pu.sqlSave, pu.mutation, pu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -203,6 +176,7 @@ func (pu *PetUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + pu.mutation.done = true return n, nil } @@ -273,40 +247,7 @@ func (puo *PetUpdateOne) Select(field string, fields ...string) *PetUpdateOne { // Save executes the query and returns the updated Pet entity. func (puo *PetUpdateOne) Save(ctx context.Context) (*Pet, error) { - var ( - err error - node *Pet - ) - if len(puo.hooks) == 0 { - node, err = puo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*PetMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - puo.mutation = mutation - node, err = puo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(puo.hooks) - 1; i >= 0; i-- { - if puo.hooks[i] == nil { - return nil, fmt.Errorf("entv2: uninitialized hook (forgotten import entv2/runtime?)") - } - mut = puo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, puo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Pet) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from PetMutation", v) - } - node = nv - } - return node, err + return withHooks[*Pet, PetMutation](ctx, puo.sqlSave, puo.mutation, puo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -418,5 +359,6 @@ func (puo *PetUpdateOne) sqlSave(ctx context.Context) (_node *Pet, err error) { } return nil, err } + puo.mutation.done = true return _node, nil } diff --git a/entc/integration/migrate/entv2/user_create.go b/entc/integration/migrate/entv2/user_create.go index 0039ec5e9..3b3b12833 100644 --- a/entc/integration/migrate/entv2/user_create.go +++ b/entc/integration/migrate/entv2/user_create.go @@ -334,50 +334,8 @@ func (uc *UserCreate) Mutation() *UserMutation { // Save creates the User in the database. func (uc *UserCreate) Save(ctx context.Context) (*User, error) { - var ( - err error - node *User - ) uc.defaults() - if len(uc.hooks) == 0 { - if err = uc.check(); err != nil { - return nil, err - } - node, err = uc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = uc.check(); err != nil { - return nil, err - } - uc.mutation = mutation - if node, err = uc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(uc.hooks) - 1; i >= 0; i-- { - if uc.hooks[i] == nil { - return nil, fmt.Errorf("entv2: uninitialized hook (forgotten import entv2/runtime?)") - } - mut = uc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*User) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) - } - node = nv - } - return node, err + return withHooks[*User, UserMutation](ctx, uc.sqlSave, uc.mutation, uc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -510,6 +468,9 @@ func (uc *UserCreate) check() error { } func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { + if err := uc.check(); err != nil { + return nil, err + } _node, _spec := uc.createSpec() if err := sqlgraph.CreateNode(ctx, uc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -521,6 +482,8 @@ func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { id := _spec.ID.Value.(int64) _node.ID = int(id) } + uc.mutation.id = &_node.ID + uc.mutation.done = true return _node, nil } diff --git a/entc/integration/migrate/entv2/user_delete.go b/entc/integration/migrate/entv2/user_delete.go index 91523e71e..14cc03f21 100644 --- a/entc/integration/migrate/entv2/user_delete.go +++ b/entc/integration/migrate/entv2/user_delete.go @@ -8,7 +8,6 @@ package entv2 import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (ud *UserDelete) Where(ps ...predicate.User) *UserDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (ud *UserDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(ud.hooks) == 0 { - affected, err = ud.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - ud.mutation = mutation - affected, err = ud.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(ud.hooks) - 1; i >= 0; i-- { - if ud.hooks[i] == nil { - return 0, fmt.Errorf("entv2: uninitialized hook (forgotten import entv2/runtime?)") - } - mut = ud.hooks[i](mut) - } - n, err := mut.Mutate(ctx, ud.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, UserMutation](ctx, ud.sqlExec, ud.mutation, ud.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (ud *UserDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + ud.mutation.done = true return affected, err } diff --git a/entc/integration/migrate/entv2/user_update.go b/entc/integration/migrate/entv2/user_update.go index 60ce10b8b..f62640119 100644 --- a/entc/integration/migrate/entv2/user_update.go +++ b/entc/integration/migrate/entv2/user_update.go @@ -458,40 +458,7 @@ func (uu *UserUpdate) RemoveFriends(u ...*User) *UserUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (uu *UserUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(uu.hooks) == 0 { - if err = uu.check(); err != nil { - return 0, err - } - affected, err = uu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = uu.check(); err != nil { - return 0, err - } - uu.mutation = mutation - affected, err = uu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(uu.hooks) - 1; i >= 0; i-- { - if uu.hooks[i] == nil { - return 0, fmt.Errorf("entv2: uninitialized hook (forgotten import entv2/runtime?)") - } - mut = uu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, uu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, UserMutation](ctx, uu.sqlSave, uu.mutation, uu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -547,6 +514,9 @@ func (uu *UserUpdate) check() error { } func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) { + if err := uu.check(); err != nil { + return n, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: user.Table, @@ -816,6 +786,7 @@ func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + uu.mutation.done = true return n, nil } @@ -1257,46 +1228,7 @@ func (uuo *UserUpdateOne) Select(field string, fields ...string) *UserUpdateOne // Save executes the query and returns the updated User entity. func (uuo *UserUpdateOne) Save(ctx context.Context) (*User, error) { - var ( - err error - node *User - ) - if len(uuo.hooks) == 0 { - if err = uuo.check(); err != nil { - return nil, err - } - node, err = uuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = uuo.check(); err != nil { - return nil, err - } - uuo.mutation = mutation - node, err = uuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(uuo.hooks) - 1; i >= 0; i-- { - if uuo.hooks[i] == nil { - return nil, fmt.Errorf("entv2: uninitialized hook (forgotten import entv2/runtime?)") - } - mut = uuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*User) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) - } - node = nv - } - return node, err + return withHooks[*User, UserMutation](ctx, uuo.sqlSave, uuo.mutation, uuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -1352,6 +1284,9 @@ func (uuo *UserUpdateOne) check() error { } func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) { + if err := uuo.check(); err != nil { + return _node, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: user.Table, @@ -1641,5 +1576,6 @@ func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) } return nil, err } + uuo.mutation.done = true return _node, nil } diff --git a/entc/integration/migrate/entv2/zoo_create.go b/entc/integration/migrate/entv2/zoo_create.go index 29e5b35f1..632cd13dc 100644 --- a/entc/integration/migrate/entv2/zoo_create.go +++ b/entc/integration/migrate/entv2/zoo_create.go @@ -35,49 +35,7 @@ func (zc *ZooCreate) Mutation() *ZooMutation { // Save creates the Zoo in the database. func (zc *ZooCreate) Save(ctx context.Context) (*Zoo, error) { - var ( - err error - node *Zoo - ) - if len(zc.hooks) == 0 { - if err = zc.check(); err != nil { - return nil, err - } - node, err = zc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*ZooMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = zc.check(); err != nil { - return nil, err - } - zc.mutation = mutation - if node, err = zc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(zc.hooks) - 1; i >= 0; i-- { - if zc.hooks[i] == nil { - return nil, fmt.Errorf("entv2: uninitialized hook (forgotten import entv2/runtime?)") - } - mut = zc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, zc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Zoo) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from ZooMutation", v) - } - node = nv - } - return node, err + return withHooks[*Zoo, ZooMutation](ctx, zc.sqlSave, zc.mutation, zc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -108,6 +66,9 @@ func (zc *ZooCreate) check() error { } func (zc *ZooCreate) sqlSave(ctx context.Context) (*Zoo, error) { + if err := zc.check(); err != nil { + return nil, err + } _node, _spec := zc.createSpec() if err := sqlgraph.CreateNode(ctx, zc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -119,6 +80,8 @@ func (zc *ZooCreate) sqlSave(ctx context.Context) (*Zoo, error) { id := _spec.ID.Value.(int64) _node.ID = int(id) } + zc.mutation.id = &_node.ID + zc.mutation.done = true return _node, nil } diff --git a/entc/integration/migrate/entv2/zoo_delete.go b/entc/integration/migrate/entv2/zoo_delete.go index 4a697fc8b..1677a0d7e 100644 --- a/entc/integration/migrate/entv2/zoo_delete.go +++ b/entc/integration/migrate/entv2/zoo_delete.go @@ -8,7 +8,6 @@ package entv2 import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (zd *ZooDelete) Where(ps ...predicate.Zoo) *ZooDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (zd *ZooDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(zd.hooks) == 0 { - affected, err = zd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*ZooMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - zd.mutation = mutation - affected, err = zd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(zd.hooks) - 1; i >= 0; i-- { - if zd.hooks[i] == nil { - return 0, fmt.Errorf("entv2: uninitialized hook (forgotten import entv2/runtime?)") - } - mut = zd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, zd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, ZooMutation](ctx, zd.sqlExec, zd.mutation, zd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (zd *ZooDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + zd.mutation.done = true return affected, err } diff --git a/entc/integration/migrate/entv2/zoo_update.go b/entc/integration/migrate/entv2/zoo_update.go index 3836bac98..14f17be2d 100644 --- a/entc/integration/migrate/entv2/zoo_update.go +++ b/entc/integration/migrate/entv2/zoo_update.go @@ -38,34 +38,7 @@ func (zu *ZooUpdate) Mutation() *ZooMutation { // Save executes the query and returns the number of nodes affected by the update operation. func (zu *ZooUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(zu.hooks) == 0 { - affected, err = zu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*ZooMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - zu.mutation = mutation - affected, err = zu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(zu.hooks) - 1; i >= 0; i-- { - if zu.hooks[i] == nil { - return 0, fmt.Errorf("entv2: uninitialized hook (forgotten import entv2/runtime?)") - } - mut = zu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, zu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, ZooMutation](ctx, zu.sqlSave, zu.mutation, zu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -116,6 +89,7 @@ func (zu *ZooUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + zu.mutation.done = true return n, nil } @@ -141,40 +115,7 @@ func (zuo *ZooUpdateOne) Select(field string, fields ...string) *ZooUpdateOne { // Save executes the query and returns the updated Zoo entity. func (zuo *ZooUpdateOne) Save(ctx context.Context) (*Zoo, error) { - var ( - err error - node *Zoo - ) - if len(zuo.hooks) == 0 { - node, err = zuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*ZooMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - zuo.mutation = mutation - node, err = zuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(zuo.hooks) - 1; i >= 0; i-- { - if zuo.hooks[i] == nil { - return nil, fmt.Errorf("entv2: uninitialized hook (forgotten import entv2/runtime?)") - } - mut = zuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, zuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Zoo) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from ZooMutation", v) - } - node = nv - } - return node, err + return withHooks[*Zoo, ZooMutation](ctx, zuo.sqlSave, zuo.mutation, zuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -245,5 +186,6 @@ func (zuo *ZooUpdateOne) sqlSave(ctx context.Context) (_node *Zoo, err error) { } return nil, err } + zuo.mutation.done = true return _node, nil } diff --git a/entc/integration/migrate/versioned/ent.go b/entc/integration/migrate/versioned/ent.go index 323a3c74c..9a89b1e2e 100644 --- a/entc/integration/migrate/versioned/ent.go +++ b/entc/integration/migrate/versioned/ent.go @@ -475,6 +475,40 @@ func (s *selector) BoolX(ctx context.Context) bool { return v } +// withHooks invokes the builder operation with the given hooks, if any. +func withHooks[V Value, M any, PM interface { + *M + Mutation +}](ctx context.Context, exec func(context.Context) (V, error), mutation PM, hooks []Hook) (value V, err error) { + if len(hooks) == 0 { + return exec(ctx) + } + var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { + mutationT, ok := m.(PM) + if !ok { + return nil, fmt.Errorf("unexpected mutation type %T", m) + } + // Set the mutation to the builder. + *mutation = *mutationT + return exec(ctx) + }) + for i := len(hooks) - 1; i >= 0; i-- { + if hooks[i] == nil { + return value, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") + } + mut = hooks[i](mut) + } + v, err := mut.Mutate(ctx, mutation) + if err != nil { + return value, err + } + nv, ok := v.(V) + if !ok { + return value, fmt.Errorf("unexpected node type %T returned from %T", v, mutation) + } + return nv, nil +} + // newQueryContext returns a new context with the given QueryContext attached in case it does not exist. func newQueryContext(ctx context.Context, typ, op string) context.Context { if ent.QueryFromContext(ctx) == nil { diff --git a/entc/integration/migrate/versioned/group_create.go b/entc/integration/migrate/versioned/group_create.go index a210b84d8..f2c79f1fe 100644 --- a/entc/integration/migrate/versioned/group_create.go +++ b/entc/integration/migrate/versioned/group_create.go @@ -36,49 +36,7 @@ func (gc *GroupCreate) Mutation() *GroupMutation { // Save creates the Group in the database. func (gc *GroupCreate) Save(ctx context.Context) (*Group, error) { - var ( - err error - node *Group - ) - if len(gc.hooks) == 0 { - if err = gc.check(); err != nil { - return nil, err - } - node, err = gc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GroupMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = gc.check(); err != nil { - return nil, err - } - gc.mutation = mutation - if node, err = gc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(gc.hooks) - 1; i >= 0; i-- { - if gc.hooks[i] == nil { - return nil, fmt.Errorf("versioned: uninitialized hook (forgotten import versioned/runtime?)") - } - mut = gc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, gc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Group) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from GroupMutation", v) - } - node = nv - } - return node, err + return withHooks[*Group, GroupMutation](ctx, gc.sqlSave, gc.mutation, gc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -112,6 +70,9 @@ func (gc *GroupCreate) check() error { } func (gc *GroupCreate) sqlSave(ctx context.Context) (*Group, error) { + if err := gc.check(); err != nil { + return nil, err + } _node, _spec := gc.createSpec() if err := sqlgraph.CreateNode(ctx, gc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -121,6 +82,8 @@ func (gc *GroupCreate) sqlSave(ctx context.Context) (*Group, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + gc.mutation.id = &_node.ID + gc.mutation.done = true return _node, nil } diff --git a/entc/integration/migrate/versioned/group_delete.go b/entc/integration/migrate/versioned/group_delete.go index 0027fa683..8cdceb7e8 100644 --- a/entc/integration/migrate/versioned/group_delete.go +++ b/entc/integration/migrate/versioned/group_delete.go @@ -8,7 +8,6 @@ package versioned import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (gd *GroupDelete) Where(ps ...predicate.Group) *GroupDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (gd *GroupDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(gd.hooks) == 0 { - affected, err = gd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GroupMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - gd.mutation = mutation - affected, err = gd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(gd.hooks) - 1; i >= 0; i-- { - if gd.hooks[i] == nil { - return 0, fmt.Errorf("versioned: uninitialized hook (forgotten import versioned/runtime?)") - } - mut = gd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, gd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, GroupMutation](ctx, gd.sqlExec, gd.mutation, gd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (gd *GroupDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + gd.mutation.done = true return affected, err } diff --git a/entc/integration/migrate/versioned/group_update.go b/entc/integration/migrate/versioned/group_update.go index 69fde8807..6e08ebd87 100644 --- a/entc/integration/migrate/versioned/group_update.go +++ b/entc/integration/migrate/versioned/group_update.go @@ -44,34 +44,7 @@ func (gu *GroupUpdate) Mutation() *GroupMutation { // Save executes the query and returns the number of nodes affected by the update operation. func (gu *GroupUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(gu.hooks) == 0 { - affected, err = gu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GroupMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - gu.mutation = mutation - affected, err = gu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(gu.hooks) - 1; i >= 0; i-- { - if gu.hooks[i] == nil { - return 0, fmt.Errorf("versioned: uninitialized hook (forgotten import versioned/runtime?)") - } - mut = gu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, gu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, GroupMutation](ctx, gu.sqlSave, gu.mutation, gu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -125,6 +98,7 @@ func (gu *GroupUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + gu.mutation.done = true return n, nil } @@ -156,40 +130,7 @@ func (guo *GroupUpdateOne) Select(field string, fields ...string) *GroupUpdateOn // Save executes the query and returns the updated Group entity. func (guo *GroupUpdateOne) Save(ctx context.Context) (*Group, error) { - var ( - err error - node *Group - ) - if len(guo.hooks) == 0 { - node, err = guo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GroupMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - guo.mutation = mutation - node, err = guo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(guo.hooks) - 1; i >= 0; i-- { - if guo.hooks[i] == nil { - return nil, fmt.Errorf("versioned: uninitialized hook (forgotten import versioned/runtime?)") - } - mut = guo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, guo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Group) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from GroupMutation", v) - } - node = nv - } - return node, err + return withHooks[*Group, GroupMutation](ctx, guo.sqlSave, guo.mutation, guo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -263,5 +204,6 @@ func (guo *GroupUpdateOne) sqlSave(ctx context.Context) (_node *Group, err error } return nil, err } + guo.mutation.done = true return _node, nil } diff --git a/entc/integration/migrate/versioned/user_create.go b/entc/integration/migrate/versioned/user_create.go index c9426851e..586dd0203 100644 --- a/entc/integration/migrate/versioned/user_create.go +++ b/entc/integration/migrate/versioned/user_create.go @@ -56,49 +56,7 @@ func (uc *UserCreate) Mutation() *UserMutation { // Save creates the User in the database. func (uc *UserCreate) Save(ctx context.Context) (*User, error) { - var ( - err error - node *User - ) - if len(uc.hooks) == 0 { - if err = uc.check(); err != nil { - return nil, err - } - node, err = uc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = uc.check(); err != nil { - return nil, err - } - uc.mutation = mutation - if node, err = uc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(uc.hooks) - 1; i >= 0; i-- { - if uc.hooks[i] == nil { - return nil, fmt.Errorf("versioned: uninitialized hook (forgotten import versioned/runtime?)") - } - mut = uc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*User) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) - } - node = nv - } - return node, err + return withHooks[*User, UserMutation](ctx, uc.sqlSave, uc.mutation, uc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -140,6 +98,9 @@ func (uc *UserCreate) check() error { } func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { + if err := uc.check(); err != nil { + return nil, err + } _node, _spec := uc.createSpec() if err := sqlgraph.CreateNode(ctx, uc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -149,6 +110,8 @@ func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + uc.mutation.id = &_node.ID + uc.mutation.done = true return _node, nil } diff --git a/entc/integration/migrate/versioned/user_delete.go b/entc/integration/migrate/versioned/user_delete.go index c86d7136e..89d8bcd22 100644 --- a/entc/integration/migrate/versioned/user_delete.go +++ b/entc/integration/migrate/versioned/user_delete.go @@ -8,7 +8,6 @@ package versioned import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (ud *UserDelete) Where(ps ...predicate.User) *UserDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (ud *UserDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(ud.hooks) == 0 { - affected, err = ud.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - ud.mutation = mutation - affected, err = ud.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(ud.hooks) - 1; i >= 0; i-- { - if ud.hooks[i] == nil { - return 0, fmt.Errorf("versioned: uninitialized hook (forgotten import versioned/runtime?)") - } - mut = ud.hooks[i](mut) - } - n, err := mut.Mutate(ctx, ud.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, UserMutation](ctx, ud.sqlExec, ud.mutation, ud.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (ud *UserDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + ud.mutation.done = true return affected, err } diff --git a/entc/integration/migrate/versioned/user_update.go b/entc/integration/migrate/versioned/user_update.go index 02858c329..6e73c918e 100644 --- a/entc/integration/migrate/versioned/user_update.go +++ b/entc/integration/migrate/versioned/user_update.go @@ -77,40 +77,7 @@ func (uu *UserUpdate) Mutation() *UserMutation { // Save executes the query and returns the number of nodes affected by the update operation. func (uu *UserUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(uu.hooks) == 0 { - if err = uu.check(); err != nil { - return 0, err - } - affected, err = uu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = uu.check(); err != nil { - return 0, err - } - uu.mutation = mutation - affected, err = uu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(uu.hooks) - 1; i >= 0; i-- { - if uu.hooks[i] == nil { - return 0, fmt.Errorf("versioned: uninitialized hook (forgotten import versioned/runtime?)") - } - mut = uu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, uu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, UserMutation](ctx, uu.sqlSave, uu.mutation, uu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -146,6 +113,9 @@ func (uu *UserUpdate) check() error { } func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) { + if err := uu.check(); err != nil { + return n, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: user.Table, @@ -186,6 +156,7 @@ func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + uu.mutation.done = true return n, nil } @@ -250,46 +221,7 @@ func (uuo *UserUpdateOne) Select(field string, fields ...string) *UserUpdateOne // Save executes the query and returns the updated User entity. func (uuo *UserUpdateOne) Save(ctx context.Context) (*User, error) { - var ( - err error - node *User - ) - if len(uuo.hooks) == 0 { - if err = uuo.check(); err != nil { - return nil, err - } - node, err = uuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = uuo.check(); err != nil { - return nil, err - } - uuo.mutation = mutation - node, err = uuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(uuo.hooks) - 1; i >= 0; i-- { - if uuo.hooks[i] == nil { - return nil, fmt.Errorf("versioned: uninitialized hook (forgotten import versioned/runtime?)") - } - mut = uuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*User) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) - } - node = nv - } - return node, err + return withHooks[*User, UserMutation](ctx, uuo.sqlSave, uuo.mutation, uuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -325,6 +257,9 @@ func (uuo *UserUpdateOne) check() error { } func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) { + if err := uuo.check(); err != nil { + return _node, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: user.Table, @@ -385,5 +320,6 @@ func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) } return nil, err } + uuo.mutation.done = true return _node, nil } diff --git a/entc/integration/multischema/ent/ent.go b/entc/integration/multischema/ent/ent.go index 280344a38..141619838 100644 --- a/entc/integration/multischema/ent/ent.go +++ b/entc/integration/multischema/ent/ent.go @@ -479,6 +479,40 @@ func (s *selector) BoolX(ctx context.Context) bool { return v } +// withHooks invokes the builder operation with the given hooks, if any. +func withHooks[V Value, M any, PM interface { + *M + Mutation +}](ctx context.Context, exec func(context.Context) (V, error), mutation PM, hooks []Hook) (value V, err error) { + if len(hooks) == 0 { + return exec(ctx) + } + var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { + mutationT, ok := m.(PM) + if !ok { + return nil, fmt.Errorf("unexpected mutation type %T", m) + } + // Set the mutation to the builder. + *mutation = *mutationT + return exec(ctx) + }) + for i := len(hooks) - 1; i >= 0; i-- { + if hooks[i] == nil { + return value, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") + } + mut = hooks[i](mut) + } + v, err := mut.Mutate(ctx, mutation) + if err != nil { + return value, err + } + nv, ok := v.(V) + if !ok { + return value, fmt.Errorf("unexpected node type %T returned from %T", v, mutation) + } + return nv, nil +} + // newQueryContext returns a new context with the given QueryContext attached in case it does not exist. func newQueryContext(ctx context.Context, typ, op string) context.Context { if ent.QueryFromContext(ctx) == nil { diff --git a/entc/integration/multischema/ent/friendship_create.go b/entc/integration/multischema/ent/friendship_create.go index b151bfd72..fd7611218 100644 --- a/entc/integration/multischema/ent/friendship_create.go +++ b/entc/integration/multischema/ent/friendship_create.go @@ -82,50 +82,8 @@ func (fc *FriendshipCreate) Mutation() *FriendshipMutation { // Save creates the Friendship in the database. func (fc *FriendshipCreate) Save(ctx context.Context) (*Friendship, error) { - var ( - err error - node *Friendship - ) fc.defaults() - if len(fc.hooks) == 0 { - if err = fc.check(); err != nil { - return nil, err - } - node, err = fc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*FriendshipMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = fc.check(); err != nil { - return nil, err - } - fc.mutation = mutation - if node, err = fc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(fc.hooks) - 1; i >= 0; i-- { - if fc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = fc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, fc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Friendship) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from FriendshipMutation", v) - } - node = nv - } - return node, err + return withHooks[*Friendship, FriendshipMutation](ctx, fc.sqlSave, fc.mutation, fc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -186,6 +144,9 @@ func (fc *FriendshipCreate) check() error { } func (fc *FriendshipCreate) sqlSave(ctx context.Context) (*Friendship, error) { + if err := fc.check(); err != nil { + return nil, err + } _node, _spec := fc.createSpec() if err := sqlgraph.CreateNode(ctx, fc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -195,6 +156,8 @@ func (fc *FriendshipCreate) sqlSave(ctx context.Context) (*Friendship, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + fc.mutation.id = &_node.ID + fc.mutation.done = true return _node, nil } diff --git a/entc/integration/multischema/ent/friendship_delete.go b/entc/integration/multischema/ent/friendship_delete.go index be01afae6..a1dc3378e 100644 --- a/entc/integration/multischema/ent/friendship_delete.go +++ b/entc/integration/multischema/ent/friendship_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -33,40 +32,7 @@ func (fd *FriendshipDelete) Where(ps ...predicate.Friendship) *FriendshipDelete // Exec executes the deletion query and returns how many vertices were deleted. func (fd *FriendshipDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(fd.hooks) == 0 { - affected, err = fd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*FriendshipMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - fd.mutation = mutation - affected, err = fd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(fd.hooks) - 1; i >= 0; i-- { - if fd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = fd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, fd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, FriendshipMutation](ctx, fd.sqlExec, fd.mutation, fd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -101,6 +67,7 @@ func (fd *FriendshipDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + fd.mutation.done = true return affected, err } diff --git a/entc/integration/multischema/ent/friendship_update.go b/entc/integration/multischema/ent/friendship_update.go index 6fea4b241..0a6a06e22 100644 --- a/entc/integration/multischema/ent/friendship_update.go +++ b/entc/integration/multischema/ent/friendship_update.go @@ -76,40 +76,7 @@ func (fu *FriendshipUpdate) Mutation() *FriendshipMutation { // Save executes the query and returns the number of nodes affected by the update operation. func (fu *FriendshipUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(fu.hooks) == 0 { - if err = fu.check(); err != nil { - return 0, err - } - affected, err = fu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*FriendshipMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = fu.check(); err != nil { - return 0, err - } - fu.mutation = mutation - affected, err = fu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(fu.hooks) - 1; i >= 0; i-- { - if fu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = fu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, fu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, FriendshipMutation](ctx, fu.sqlSave, fu.mutation, fu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -152,6 +119,9 @@ func (fu *FriendshipUpdate) Modify(modifiers ...func(u *sql.UpdateBuilder)) *Fri } func (fu *FriendshipUpdate) sqlSave(ctx context.Context) (n int, err error) { + if err := fu.check(); err != nil { + return n, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: friendship.Table, @@ -189,6 +159,7 @@ func (fu *FriendshipUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + fu.mutation.done = true return n, nil } @@ -250,46 +221,7 @@ func (fuo *FriendshipUpdateOne) Select(field string, fields ...string) *Friendsh // Save executes the query and returns the updated Friendship entity. func (fuo *FriendshipUpdateOne) Save(ctx context.Context) (*Friendship, error) { - var ( - err error - node *Friendship - ) - if len(fuo.hooks) == 0 { - if err = fuo.check(); err != nil { - return nil, err - } - node, err = fuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*FriendshipMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = fuo.check(); err != nil { - return nil, err - } - fuo.mutation = mutation - node, err = fuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(fuo.hooks) - 1; i >= 0; i-- { - if fuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = fuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, fuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Friendship) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from FriendshipMutation", v) - } - node = nv - } - return node, err + return withHooks[*Friendship, FriendshipMutation](ctx, fuo.sqlSave, fuo.mutation, fuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -332,6 +264,9 @@ func (fuo *FriendshipUpdateOne) Modify(modifiers ...func(u *sql.UpdateBuilder)) } func (fuo *FriendshipUpdateOne) sqlSave(ctx context.Context) (_node *Friendship, err error) { + if err := fuo.check(); err != nil { + return _node, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: friendship.Table, @@ -389,5 +324,6 @@ func (fuo *FriendshipUpdateOne) sqlSave(ctx context.Context) (_node *Friendship, } return nil, err } + fuo.mutation.done = true return _node, nil } diff --git a/entc/integration/multischema/ent/group_create.go b/entc/integration/multischema/ent/group_create.go index 4cd00ab1d..cfe46ec53 100644 --- a/entc/integration/multischema/ent/group_create.go +++ b/entc/integration/multischema/ent/group_create.go @@ -60,50 +60,8 @@ func (gc *GroupCreate) Mutation() *GroupMutation { // Save creates the Group in the database. func (gc *GroupCreate) Save(ctx context.Context) (*Group, error) { - var ( - err error - node *Group - ) gc.defaults() - if len(gc.hooks) == 0 { - if err = gc.check(); err != nil { - return nil, err - } - node, err = gc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GroupMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = gc.check(); err != nil { - return nil, err - } - gc.mutation = mutation - if node, err = gc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(gc.hooks) - 1; i >= 0; i-- { - if gc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = gc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, gc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Group) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from GroupMutation", v) - } - node = nv - } - return node, err + return withHooks[*Group, GroupMutation](ctx, gc.sqlSave, gc.mutation, gc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -145,6 +103,9 @@ func (gc *GroupCreate) check() error { } func (gc *GroupCreate) sqlSave(ctx context.Context) (*Group, error) { + if err := gc.check(); err != nil { + return nil, err + } _node, _spec := gc.createSpec() if err := sqlgraph.CreateNode(ctx, gc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -154,6 +115,8 @@ func (gc *GroupCreate) sqlSave(ctx context.Context) (*Group, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + gc.mutation.id = &_node.ID + gc.mutation.done = true return _node, nil } diff --git a/entc/integration/multischema/ent/group_delete.go b/entc/integration/multischema/ent/group_delete.go index 474f4bcff..e5e37d795 100644 --- a/entc/integration/multischema/ent/group_delete.go +++ b/entc/integration/multischema/ent/group_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -33,40 +32,7 @@ func (gd *GroupDelete) Where(ps ...predicate.Group) *GroupDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (gd *GroupDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(gd.hooks) == 0 { - affected, err = gd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GroupMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - gd.mutation = mutation - affected, err = gd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(gd.hooks) - 1; i >= 0; i-- { - if gd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = gd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, gd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, GroupMutation](ctx, gd.sqlExec, gd.mutation, gd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -101,6 +67,7 @@ func (gd *GroupDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + gd.mutation.done = true return affected, err } diff --git a/entc/integration/multischema/ent/group_update.go b/entc/integration/multischema/ent/group_update.go index b40c3d287..a64930ac1 100644 --- a/entc/integration/multischema/ent/group_update.go +++ b/entc/integration/multischema/ent/group_update.go @@ -91,34 +91,7 @@ func (gu *GroupUpdate) RemoveUsers(u ...*User) *GroupUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (gu *GroupUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(gu.hooks) == 0 { - affected, err = gu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GroupMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - gu.mutation = mutation - affected, err = gu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(gu.hooks) - 1; i >= 0; i-- { - if gu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = gu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, gu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, GroupMutation](ctx, gu.sqlSave, gu.mutation, gu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -238,6 +211,7 @@ func (gu *GroupUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + gu.mutation.done = true return n, nil } @@ -314,40 +288,7 @@ func (guo *GroupUpdateOne) Select(field string, fields ...string) *GroupUpdateOn // Save executes the query and returns the updated Group entity. func (guo *GroupUpdateOne) Save(ctx context.Context) (*Group, error) { - var ( - err error - node *Group - ) - if len(guo.hooks) == 0 { - node, err = guo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GroupMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - guo.mutation = mutation - node, err = guo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(guo.hooks) - 1; i >= 0; i-- { - if guo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = guo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, guo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Group) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from GroupMutation", v) - } - node = nv - } - return node, err + return withHooks[*Group, GroupMutation](ctx, guo.sqlSave, guo.mutation, guo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -487,5 +428,6 @@ func (guo *GroupUpdateOne) sqlSave(ctx context.Context) (_node *Group, err error } return nil, err } + guo.mutation.done = true return _node, nil } diff --git a/entc/integration/multischema/ent/pet_create.go b/entc/integration/multischema/ent/pet_create.go index ce81677e3..100d5a5b2 100644 --- a/entc/integration/multischema/ent/pet_create.go +++ b/entc/integration/multischema/ent/pet_create.go @@ -64,50 +64,8 @@ func (pc *PetCreate) Mutation() *PetMutation { // Save creates the Pet in the database. func (pc *PetCreate) Save(ctx context.Context) (*Pet, error) { - var ( - err error - node *Pet - ) pc.defaults() - if len(pc.hooks) == 0 { - if err = pc.check(); err != nil { - return nil, err - } - node, err = pc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*PetMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = pc.check(); err != nil { - return nil, err - } - pc.mutation = mutation - if node, err = pc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(pc.hooks) - 1; i >= 0; i-- { - if pc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = pc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, pc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Pet) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from PetMutation", v) - } - node = nv - } - return node, err + return withHooks[*Pet, PetMutation](ctx, pc.sqlSave, pc.mutation, pc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -149,6 +107,9 @@ func (pc *PetCreate) check() error { } func (pc *PetCreate) sqlSave(ctx context.Context) (*Pet, error) { + if err := pc.check(); err != nil { + return nil, err + } _node, _spec := pc.createSpec() if err := sqlgraph.CreateNode(ctx, pc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -158,6 +119,8 @@ func (pc *PetCreate) sqlSave(ctx context.Context) (*Pet, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + pc.mutation.id = &_node.ID + pc.mutation.done = true return _node, nil } diff --git a/entc/integration/multischema/ent/pet_delete.go b/entc/integration/multischema/ent/pet_delete.go index 0cff4cee8..82ec94d76 100644 --- a/entc/integration/multischema/ent/pet_delete.go +++ b/entc/integration/multischema/ent/pet_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -33,40 +32,7 @@ func (pd *PetDelete) Where(ps ...predicate.Pet) *PetDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (pd *PetDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(pd.hooks) == 0 { - affected, err = pd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*PetMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - pd.mutation = mutation - affected, err = pd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(pd.hooks) - 1; i >= 0; i-- { - if pd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = pd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, pd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, PetMutation](ctx, pd.sqlExec, pd.mutation, pd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -101,6 +67,7 @@ func (pd *PetDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + pd.mutation.done = true return affected, err } diff --git a/entc/integration/multischema/ent/pet_update.go b/entc/integration/multischema/ent/pet_update.go index 5cdd952c3..4bddfee64 100644 --- a/entc/integration/multischema/ent/pet_update.go +++ b/entc/integration/multischema/ent/pet_update.go @@ -86,34 +86,7 @@ func (pu *PetUpdate) ClearOwner() *PetUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (pu *PetUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(pu.hooks) == 0 { - affected, err = pu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*PetMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - pu.mutation = mutation - affected, err = pu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(pu.hooks) - 1; i >= 0; i-- { - if pu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = pu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, pu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, PetMutation](ctx, pu.sqlSave, pu.mutation, pu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -213,6 +186,7 @@ func (pu *PetUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + pu.mutation.done = true return n, nil } @@ -284,40 +258,7 @@ func (puo *PetUpdateOne) Select(field string, fields ...string) *PetUpdateOne { // Save executes the query and returns the updated Pet entity. func (puo *PetUpdateOne) Save(ctx context.Context) (*Pet, error) { - var ( - err error - node *Pet - ) - if len(puo.hooks) == 0 { - node, err = puo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*PetMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - puo.mutation = mutation - node, err = puo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(puo.hooks) - 1; i >= 0; i-- { - if puo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = puo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, puo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Pet) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from PetMutation", v) - } - node = nv - } - return node, err + return withHooks[*Pet, PetMutation](ctx, puo.sqlSave, puo.mutation, puo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -437,5 +378,6 @@ func (puo *PetUpdateOne) sqlSave(ctx context.Context) (_node *Pet, err error) { } return nil, err } + puo.mutation.done = true return _node, nil } diff --git a/entc/integration/multischema/ent/user_create.go b/entc/integration/multischema/ent/user_create.go index af95a074e..47ea6133c 100644 --- a/entc/integration/multischema/ent/user_create.go +++ b/entc/integration/multischema/ent/user_create.go @@ -107,50 +107,8 @@ func (uc *UserCreate) Mutation() *UserMutation { // Save creates the User in the database. func (uc *UserCreate) Save(ctx context.Context) (*User, error) { - var ( - err error - node *User - ) uc.defaults() - if len(uc.hooks) == 0 { - if err = uc.check(); err != nil { - return nil, err - } - node, err = uc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = uc.check(); err != nil { - return nil, err - } - uc.mutation = mutation - if node, err = uc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(uc.hooks) - 1; i >= 0; i-- { - if uc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*User) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) - } - node = nv - } - return node, err + return withHooks[*User, UserMutation](ctx, uc.sqlSave, uc.mutation, uc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -192,6 +150,9 @@ func (uc *UserCreate) check() error { } func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { + if err := uc.check(); err != nil { + return nil, err + } _node, _spec := uc.createSpec() if err := sqlgraph.CreateNode(ctx, uc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -201,6 +162,8 @@ func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + uc.mutation.id = &_node.ID + uc.mutation.done = true return _node, nil } diff --git a/entc/integration/multischema/ent/user_delete.go b/entc/integration/multischema/ent/user_delete.go index 47c7db6a9..0efe11436 100644 --- a/entc/integration/multischema/ent/user_delete.go +++ b/entc/integration/multischema/ent/user_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -33,40 +32,7 @@ func (ud *UserDelete) Where(ps ...predicate.User) *UserDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (ud *UserDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(ud.hooks) == 0 { - affected, err = ud.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - ud.mutation = mutation - affected, err = ud.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(ud.hooks) - 1; i >= 0; i-- { - if ud.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ud.hooks[i](mut) - } - n, err := mut.Mutate(ctx, ud.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, UserMutation](ctx, ud.sqlExec, ud.mutation, ud.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -101,6 +67,7 @@ func (ud *UserDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + ud.mutation.done = true return affected, err } diff --git a/entc/integration/multischema/ent/user_update.go b/entc/integration/multischema/ent/user_update.go index 2a45fcb71..dbe2454f4 100644 --- a/entc/integration/multischema/ent/user_update.go +++ b/entc/integration/multischema/ent/user_update.go @@ -201,34 +201,7 @@ func (uu *UserUpdate) RemoveFriendships(f ...*Friendship) *UserUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (uu *UserUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(uu.hooks) == 0 { - affected, err = uu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - uu.mutation = mutation - affected, err = uu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(uu.hooks) - 1; i >= 0; i-- { - if uu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, uu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, UserMutation](ctx, uu.sqlSave, uu.mutation, uu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -531,6 +504,7 @@ func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + uu.mutation.done = true return n, nil } @@ -715,40 +689,7 @@ func (uuo *UserUpdateOne) Select(field string, fields ...string) *UserUpdateOne // Save executes the query and returns the updated User entity. func (uuo *UserUpdateOne) Save(ctx context.Context) (*User, error) { - var ( - err error - node *User - ) - if len(uuo.hooks) == 0 { - node, err = uuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - uuo.mutation = mutation - node, err = uuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(uuo.hooks) - 1; i >= 0; i-- { - if uuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*User) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) - } - node = nv - } - return node, err + return withHooks[*User, UserMutation](ctx, uuo.sqlSave, uuo.mutation, uuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -1071,5 +1012,6 @@ func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) } return nil, err } + uuo.mutation.done = true return _node, nil } diff --git a/entc/integration/privacy/ent/ent.go b/entc/integration/privacy/ent/ent.go index 5fa14351e..198dff1b6 100644 --- a/entc/integration/privacy/ent/ent.go +++ b/entc/integration/privacy/ent/ent.go @@ -477,6 +477,40 @@ func (s *selector) BoolX(ctx context.Context) bool { return v } +// withHooks invokes the builder operation with the given hooks, if any. +func withHooks[V Value, M any, PM interface { + *M + Mutation +}](ctx context.Context, exec func(context.Context) (V, error), mutation PM, hooks []Hook) (value V, err error) { + if len(hooks) == 0 { + return exec(ctx) + } + var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { + mutationT, ok := m.(PM) + if !ok { + return nil, fmt.Errorf("unexpected mutation type %T", m) + } + // Set the mutation to the builder. + *mutation = *mutationT + return exec(ctx) + }) + for i := len(hooks) - 1; i >= 0; i-- { + if hooks[i] == nil { + return value, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") + } + mut = hooks[i](mut) + } + v, err := mut.Mutate(ctx, mutation) + if err != nil { + return value, err + } + nv, ok := v.(V) + if !ok { + return value, fmt.Errorf("unexpected node type %T returned from %T", v, mutation) + } + return nv, nil +} + // newQueryContext returns a new context with the given QueryContext attached in case it does not exist. func newQueryContext(ctx context.Context, typ, op string) context.Context { if ent.QueryFromContext(ctx) == nil { diff --git a/entc/integration/privacy/ent/task_create.go b/entc/integration/privacy/ent/task_create.go index 4b76c06c2..3ed673dc9 100644 --- a/entc/integration/privacy/ent/task_create.go +++ b/entc/integration/privacy/ent/task_create.go @@ -115,52 +115,10 @@ func (tc *TaskCreate) Mutation() *TaskMutation { // Save creates the Task in the database. func (tc *TaskCreate) Save(ctx context.Context) (*Task, error) { - var ( - err error - node *Task - ) if err := tc.defaults(); err != nil { return nil, err } - if len(tc.hooks) == 0 { - if err = tc.check(); err != nil { - return nil, err - } - node, err = tc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*TaskMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = tc.check(); err != nil { - return nil, err - } - tc.mutation = mutation - if node, err = tc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(tc.hooks) - 1; i >= 0; i-- { - if tc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = tc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, tc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Task) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from TaskMutation", v) - } - node = nv - } - return node, err + return withHooks[*Task, TaskMutation](ctx, tc.sqlSave, tc.mutation, tc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -216,6 +174,9 @@ func (tc *TaskCreate) check() error { } func (tc *TaskCreate) sqlSave(ctx context.Context) (*Task, error) { + if err := tc.check(); err != nil { + return nil, err + } _node, _spec := tc.createSpec() if err := sqlgraph.CreateNode(ctx, tc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -225,6 +186,8 @@ func (tc *TaskCreate) sqlSave(ctx context.Context) (*Task, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + tc.mutation.id = &_node.ID + tc.mutation.done = true return _node, nil } diff --git a/entc/integration/privacy/ent/task_delete.go b/entc/integration/privacy/ent/task_delete.go index 19b36d09c..8261c9205 100644 --- a/entc/integration/privacy/ent/task_delete.go +++ b/entc/integration/privacy/ent/task_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (td *TaskDelete) Where(ps ...predicate.Task) *TaskDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (td *TaskDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(td.hooks) == 0 { - affected, err = td.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*TaskMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - td.mutation = mutation - affected, err = td.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(td.hooks) - 1; i >= 0; i-- { - if td.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = td.hooks[i](mut) - } - n, err := mut.Mutate(ctx, td.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, TaskMutation](ctx, td.sqlExec, td.mutation, td.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (td *TaskDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + td.mutation.done = true return affected, err } diff --git a/entc/integration/privacy/ent/task_update.go b/entc/integration/privacy/ent/task_update.go index b3116d989..908edbc90 100644 --- a/entc/integration/privacy/ent/task_update.go +++ b/entc/integration/privacy/ent/task_update.go @@ -162,40 +162,7 @@ func (tu *TaskUpdate) ClearOwner() *TaskUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (tu *TaskUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(tu.hooks) == 0 { - if err = tu.check(); err != nil { - return 0, err - } - affected, err = tu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*TaskMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = tu.check(); err != nil { - return 0, err - } - tu.mutation = mutation - affected, err = tu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(tu.hooks) - 1; i >= 0; i-- { - if tu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = tu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, tu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, TaskMutation](ctx, tu.sqlSave, tu.mutation, tu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -236,6 +203,9 @@ func (tu *TaskUpdate) check() error { } func (tu *TaskUpdate) sqlSave(ctx context.Context) (n int, err error) { + if err := tu.check(); err != nil { + return n, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: task.Table, @@ -368,6 +338,7 @@ func (tu *TaskUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + tu.mutation.done = true return n, nil } @@ -514,46 +485,7 @@ func (tuo *TaskUpdateOne) Select(field string, fields ...string) *TaskUpdateOne // Save executes the query and returns the updated Task entity. func (tuo *TaskUpdateOne) Save(ctx context.Context) (*Task, error) { - var ( - err error - node *Task - ) - if len(tuo.hooks) == 0 { - if err = tuo.check(); err != nil { - return nil, err - } - node, err = tuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*TaskMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = tuo.check(); err != nil { - return nil, err - } - tuo.mutation = mutation - node, err = tuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(tuo.hooks) - 1; i >= 0; i-- { - if tuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = tuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, tuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Task) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from TaskMutation", v) - } - node = nv - } - return node, err + return withHooks[*Task, TaskMutation](ctx, tuo.sqlSave, tuo.mutation, tuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -594,6 +526,9 @@ func (tuo *TaskUpdateOne) check() error { } func (tuo *TaskUpdateOne) sqlSave(ctx context.Context) (_node *Task, err error) { + if err := tuo.check(); err != nil { + return _node, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: task.Table, @@ -746,5 +681,6 @@ func (tuo *TaskUpdateOne) sqlSave(ctx context.Context) (_node *Task, err error) } return nil, err } + tuo.mutation.done = true return _node, nil } diff --git a/entc/integration/privacy/ent/team_create.go b/entc/integration/privacy/ent/team_create.go index 65862418a..7ad235c31 100644 --- a/entc/integration/privacy/ent/team_create.go +++ b/entc/integration/privacy/ent/team_create.go @@ -68,49 +68,7 @@ func (tc *TeamCreate) Mutation() *TeamMutation { // Save creates the Team in the database. func (tc *TeamCreate) Save(ctx context.Context) (*Team, error) { - var ( - err error - node *Team - ) - if len(tc.hooks) == 0 { - if err = tc.check(); err != nil { - return nil, err - } - node, err = tc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*TeamMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = tc.check(); err != nil { - return nil, err - } - tc.mutation = mutation - if node, err = tc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(tc.hooks) - 1; i >= 0; i-- { - if tc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = tc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, tc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Team) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from TeamMutation", v) - } - node = nv - } - return node, err + return withHooks[*Team, TeamMutation](ctx, tc.sqlSave, tc.mutation, tc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -149,6 +107,9 @@ func (tc *TeamCreate) check() error { } func (tc *TeamCreate) sqlSave(ctx context.Context) (*Team, error) { + if err := tc.check(); err != nil { + return nil, err + } _node, _spec := tc.createSpec() if err := sqlgraph.CreateNode(ctx, tc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -158,6 +119,8 @@ func (tc *TeamCreate) sqlSave(ctx context.Context) (*Team, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + tc.mutation.id = &_node.ID + tc.mutation.done = true return _node, nil } diff --git a/entc/integration/privacy/ent/team_delete.go b/entc/integration/privacy/ent/team_delete.go index d7fe9bec7..f0f6d1da5 100644 --- a/entc/integration/privacy/ent/team_delete.go +++ b/entc/integration/privacy/ent/team_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (td *TeamDelete) Where(ps ...predicate.Team) *TeamDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (td *TeamDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(td.hooks) == 0 { - affected, err = td.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*TeamMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - td.mutation = mutation - affected, err = td.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(td.hooks) - 1; i >= 0; i-- { - if td.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = td.hooks[i](mut) - } - n, err := mut.Mutate(ctx, td.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, TeamMutation](ctx, td.sqlExec, td.mutation, td.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (td *TeamDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + td.mutation.done = true return affected, err } diff --git a/entc/integration/privacy/ent/team_update.go b/entc/integration/privacy/ent/team_update.go index d979dae27..af5a6d10d 100644 --- a/entc/integration/privacy/ent/team_update.go +++ b/entc/integration/privacy/ent/team_update.go @@ -118,40 +118,7 @@ func (tu *TeamUpdate) RemoveUsers(u ...*User) *TeamUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (tu *TeamUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(tu.hooks) == 0 { - if err = tu.check(); err != nil { - return 0, err - } - affected, err = tu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*TeamMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = tu.check(); err != nil { - return 0, err - } - tu.mutation = mutation - affected, err = tu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(tu.hooks) - 1; i >= 0; i-- { - if tu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = tu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, tu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, TeamMutation](ctx, tu.sqlSave, tu.mutation, tu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -187,6 +154,9 @@ func (tu *TeamUpdate) check() error { } func (tu *TeamUpdate) sqlSave(ctx context.Context) (n int, err error) { + if err := tu.check(); err != nil { + return n, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: team.Table, @@ -323,6 +293,7 @@ func (tu *TeamUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + tu.mutation.done = true return n, nil } @@ -426,46 +397,7 @@ func (tuo *TeamUpdateOne) Select(field string, fields ...string) *TeamUpdateOne // Save executes the query and returns the updated Team entity. func (tuo *TeamUpdateOne) Save(ctx context.Context) (*Team, error) { - var ( - err error - node *Team - ) - if len(tuo.hooks) == 0 { - if err = tuo.check(); err != nil { - return nil, err - } - node, err = tuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*TeamMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = tuo.check(); err != nil { - return nil, err - } - tuo.mutation = mutation - node, err = tuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(tuo.hooks) - 1; i >= 0; i-- { - if tuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = tuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, tuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Team) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from TeamMutation", v) - } - node = nv - } - return node, err + return withHooks[*Team, TeamMutation](ctx, tuo.sqlSave, tuo.mutation, tuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -501,6 +433,9 @@ func (tuo *TeamUpdateOne) check() error { } func (tuo *TeamUpdateOne) sqlSave(ctx context.Context) (_node *Team, err error) { + if err := tuo.check(); err != nil { + return _node, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: team.Table, @@ -657,5 +592,6 @@ func (tuo *TeamUpdateOne) sqlSave(ctx context.Context) (_node *Team, err error) } return nil, err } + tuo.mutation.done = true return _node, nil } diff --git a/entc/integration/privacy/ent/user_create.go b/entc/integration/privacy/ent/user_create.go index 17cc1fea8..80efbb9d0 100644 --- a/entc/integration/privacy/ent/user_create.go +++ b/entc/integration/privacy/ent/user_create.go @@ -82,49 +82,7 @@ func (uc *UserCreate) Mutation() *UserMutation { // Save creates the User in the database. func (uc *UserCreate) Save(ctx context.Context) (*User, error) { - var ( - err error - node *User - ) - if len(uc.hooks) == 0 { - if err = uc.check(); err != nil { - return nil, err - } - node, err = uc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = uc.check(); err != nil { - return nil, err - } - uc.mutation = mutation - if node, err = uc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(uc.hooks) - 1; i >= 0; i-- { - if uc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*User) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) - } - node = nv - } - return node, err + return withHooks[*User, UserMutation](ctx, uc.sqlSave, uc.mutation, uc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -163,6 +121,9 @@ func (uc *UserCreate) check() error { } func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { + if err := uc.check(); err != nil { + return nil, err + } _node, _spec := uc.createSpec() if err := sqlgraph.CreateNode(ctx, uc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -172,6 +133,8 @@ func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + uc.mutation.id = &_node.ID + uc.mutation.done = true return _node, nil } diff --git a/entc/integration/privacy/ent/user_delete.go b/entc/integration/privacy/ent/user_delete.go index f90aa23da..8c5d620c2 100644 --- a/entc/integration/privacy/ent/user_delete.go +++ b/entc/integration/privacy/ent/user_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (ud *UserDelete) Where(ps ...predicate.User) *UserDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (ud *UserDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(ud.hooks) == 0 { - affected, err = ud.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - ud.mutation = mutation - affected, err = ud.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(ud.hooks) - 1; i >= 0; i-- { - if ud.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ud.hooks[i](mut) - } - n, err := mut.Mutate(ctx, ud.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, UserMutation](ctx, ud.sqlExec, ud.mutation, ud.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (ud *UserDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + ud.mutation.done = true return affected, err } diff --git a/entc/integration/privacy/ent/user_update.go b/entc/integration/privacy/ent/user_update.go index bbb4334f3..3cbbfcd07 100644 --- a/entc/integration/privacy/ent/user_update.go +++ b/entc/integration/privacy/ent/user_update.go @@ -139,34 +139,7 @@ func (uu *UserUpdate) RemoveTasks(t ...*Task) *UserUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (uu *UserUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(uu.hooks) == 0 { - affected, err = uu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - uu.mutation = mutation - affected, err = uu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(uu.hooks) - 1; i >= 0; i-- { - if uu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, uu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, UserMutation](ctx, uu.sqlSave, uu.mutation, uu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -334,6 +307,7 @@ func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + uu.mutation.done = true return n, nil } @@ -458,40 +432,7 @@ func (uuo *UserUpdateOne) Select(field string, fields ...string) *UserUpdateOne // Save executes the query and returns the updated User entity. func (uuo *UserUpdateOne) Save(ctx context.Context) (*User, error) { - var ( - err error - node *User - ) - if len(uuo.hooks) == 0 { - node, err = uuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - uuo.mutation = mutation - node, err = uuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(uuo.hooks) - 1; i >= 0; i-- { - if uuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*User) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) - } - node = nv - } - return node, err + return withHooks[*User, UserMutation](ctx, uuo.sqlSave, uuo.mutation, uuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -679,5 +620,6 @@ func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) } return nil, err } + uuo.mutation.done = true return _node, nil } diff --git a/entc/integration/template/ent/ent.go b/entc/integration/template/ent/ent.go index bc0c923fb..c8ec65854 100644 --- a/entc/integration/template/ent/ent.go +++ b/entc/integration/template/ent/ent.go @@ -477,6 +477,40 @@ func (s *selector) BoolX(ctx context.Context) bool { return v } +// withHooks invokes the builder operation with the given hooks, if any. +func withHooks[V Value, M any, PM interface { + *M + Mutation +}](ctx context.Context, exec func(context.Context) (V, error), mutation PM, hooks []Hook) (value V, err error) { + if len(hooks) == 0 { + return exec(ctx) + } + var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { + mutationT, ok := m.(PM) + if !ok { + return nil, fmt.Errorf("unexpected mutation type %T", m) + } + // Set the mutation to the builder. + *mutation = *mutationT + return exec(ctx) + }) + for i := len(hooks) - 1; i >= 0; i-- { + if hooks[i] == nil { + return value, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") + } + mut = hooks[i](mut) + } + v, err := mut.Mutate(ctx, mutation) + if err != nil { + return value, err + } + nv, ok := v.(V) + if !ok { + return value, fmt.Errorf("unexpected node type %T returned from %T", v, mutation) + } + return nv, nil +} + // newQueryContext returns a new context with the given QueryContext attached in case it does not exist. func newQueryContext(ctx context.Context, typ, op string) context.Context { if ent.QueryFromContext(ctx) == nil { diff --git a/entc/integration/template/ent/group_create.go b/entc/integration/template/ent/group_create.go index c81b00c37..4a880f80b 100644 --- a/entc/integration/template/ent/group_create.go +++ b/entc/integration/template/ent/group_create.go @@ -36,49 +36,7 @@ func (gc *GroupCreate) Mutation() *GroupMutation { // Save creates the Group in the database. func (gc *GroupCreate) Save(ctx context.Context) (*Group, error) { - var ( - err error - node *Group - ) - if len(gc.hooks) == 0 { - if err = gc.check(); err != nil { - return nil, err - } - node, err = gc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GroupMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = gc.check(); err != nil { - return nil, err - } - gc.mutation = mutation - if node, err = gc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(gc.hooks) - 1; i >= 0; i-- { - if gc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = gc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, gc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Group) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from GroupMutation", v) - } - node = nv - } - return node, err + return withHooks[*Group, GroupMutation](ctx, gc.sqlSave, gc.mutation, gc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -112,6 +70,9 @@ func (gc *GroupCreate) check() error { } func (gc *GroupCreate) sqlSave(ctx context.Context) (*Group, error) { + if err := gc.check(); err != nil { + return nil, err + } _node, _spec := gc.createSpec() if err := sqlgraph.CreateNode(ctx, gc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -121,6 +82,8 @@ func (gc *GroupCreate) sqlSave(ctx context.Context) (*Group, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + gc.mutation.id = &_node.ID + gc.mutation.done = true return _node, nil } diff --git a/entc/integration/template/ent/group_delete.go b/entc/integration/template/ent/group_delete.go index 4d25598d4..bbb46ddc0 100644 --- a/entc/integration/template/ent/group_delete.go +++ b/entc/integration/template/ent/group_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (gd *GroupDelete) Where(ps ...predicate.Group) *GroupDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (gd *GroupDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(gd.hooks) == 0 { - affected, err = gd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GroupMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - gd.mutation = mutation - affected, err = gd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(gd.hooks) - 1; i >= 0; i-- { - if gd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = gd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, gd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, GroupMutation](ctx, gd.sqlExec, gd.mutation, gd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (gd *GroupDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + gd.mutation.done = true return affected, err } diff --git a/entc/integration/template/ent/group_update.go b/entc/integration/template/ent/group_update.go index 7ed15767f..e0c804f3f 100644 --- a/entc/integration/template/ent/group_update.go +++ b/entc/integration/template/ent/group_update.go @@ -51,34 +51,7 @@ func (gu *GroupUpdate) Mutation() *GroupMutation { // Save executes the query and returns the number of nodes affected by the update operation. func (gu *GroupUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(gu.hooks) == 0 { - affected, err = gu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GroupMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - gu.mutation = mutation - affected, err = gu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(gu.hooks) - 1; i >= 0; i-- { - if gu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = gu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, gu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, GroupMutation](ctx, gu.sqlSave, gu.mutation, gu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -135,6 +108,7 @@ func (gu *GroupUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + gu.mutation.done = true return n, nil } @@ -173,40 +147,7 @@ func (guo *GroupUpdateOne) Select(field string, fields ...string) *GroupUpdateOn // Save executes the query and returns the updated Group entity. func (guo *GroupUpdateOne) Save(ctx context.Context) (*Group, error) { - var ( - err error - node *Group - ) - if len(guo.hooks) == 0 { - node, err = guo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GroupMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - guo.mutation = mutation - node, err = guo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(guo.hooks) - 1; i >= 0; i-- { - if guo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = guo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, guo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Group) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from GroupMutation", v) - } - node = nv - } - return node, err + return withHooks[*Group, GroupMutation](ctx, guo.sqlSave, guo.mutation, guo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -283,5 +224,6 @@ func (guo *GroupUpdateOne) sqlSave(ctx context.Context) (_node *Group, err error } return nil, err } + guo.mutation.done = true return _node, nil } diff --git a/entc/integration/template/ent/pet_create.go b/entc/integration/template/ent/pet_create.go index 047db8d76..5673de5a0 100644 --- a/entc/integration/template/ent/pet_create.go +++ b/entc/integration/template/ent/pet_create.go @@ -71,49 +71,7 @@ func (pc *PetCreate) Mutation() *PetMutation { // Save creates the Pet in the database. func (pc *PetCreate) Save(ctx context.Context) (*Pet, error) { - var ( - err error - node *Pet - ) - if len(pc.hooks) == 0 { - if err = pc.check(); err != nil { - return nil, err - } - node, err = pc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*PetMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = pc.check(); err != nil { - return nil, err - } - pc.mutation = mutation - if node, err = pc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(pc.hooks) - 1; i >= 0; i-- { - if pc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = pc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, pc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Pet) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from PetMutation", v) - } - node = nv - } - return node, err + return withHooks[*Pet, PetMutation](ctx, pc.sqlSave, pc.mutation, pc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -147,6 +105,9 @@ func (pc *PetCreate) check() error { } func (pc *PetCreate) sqlSave(ctx context.Context) (*Pet, error) { + if err := pc.check(); err != nil { + return nil, err + } _node, _spec := pc.createSpec() if err := sqlgraph.CreateNode(ctx, pc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -156,6 +117,8 @@ func (pc *PetCreate) sqlSave(ctx context.Context) (*Pet, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + pc.mutation.id = &_node.ID + pc.mutation.done = true return _node, nil } diff --git a/entc/integration/template/ent/pet_delete.go b/entc/integration/template/ent/pet_delete.go index 089cc9373..1247b0789 100644 --- a/entc/integration/template/ent/pet_delete.go +++ b/entc/integration/template/ent/pet_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (pd *PetDelete) Where(ps ...predicate.Pet) *PetDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (pd *PetDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(pd.hooks) == 0 { - affected, err = pd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*PetMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - pd.mutation = mutation - affected, err = pd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(pd.hooks) - 1; i >= 0; i-- { - if pd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = pd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, pd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, PetMutation](ctx, pd.sqlExec, pd.mutation, pd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (pd *PetDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + pd.mutation.done = true return affected, err } diff --git a/entc/integration/template/ent/pet_update.go b/entc/integration/template/ent/pet_update.go index 66604c904..7ff09b53e 100644 --- a/entc/integration/template/ent/pet_update.go +++ b/entc/integration/template/ent/pet_update.go @@ -98,34 +98,7 @@ func (pu *PetUpdate) ClearOwner() *PetUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (pu *PetUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(pu.hooks) == 0 { - affected, err = pu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*PetMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - pu.mutation = mutation - affected, err = pu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(pu.hooks) - 1; i >= 0; i-- { - if pu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = pu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, pu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, PetMutation](ctx, pu.sqlSave, pu.mutation, pu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -223,6 +196,7 @@ func (pu *PetUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + pu.mutation.done = true return n, nil } @@ -306,40 +280,7 @@ func (puo *PetUpdateOne) Select(field string, fields ...string) *PetUpdateOne { // Save executes the query and returns the updated Pet entity. func (puo *PetUpdateOne) Save(ctx context.Context) (*Pet, error) { - var ( - err error - node *Pet - ) - if len(puo.hooks) == 0 { - node, err = puo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*PetMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - puo.mutation = mutation - node, err = puo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(puo.hooks) - 1; i >= 0; i-- { - if puo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = puo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, puo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Pet) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from PetMutation", v) - } - node = nv - } - return node, err + return withHooks[*Pet, PetMutation](ctx, puo.sqlSave, puo.mutation, puo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -457,5 +398,6 @@ func (puo *PetUpdateOne) sqlSave(ctx context.Context) (_node *Pet, err error) { } return nil, err } + puo.mutation.done = true return _node, nil } diff --git a/entc/integration/template/ent/user_create.go b/entc/integration/template/ent/user_create.go index 656e18f7b..61f72fe66 100644 --- a/entc/integration/template/ent/user_create.go +++ b/entc/integration/template/ent/user_create.go @@ -67,49 +67,7 @@ func (uc *UserCreate) Mutation() *UserMutation { // Save creates the User in the database. func (uc *UserCreate) Save(ctx context.Context) (*User, error) { - var ( - err error - node *User - ) - if len(uc.hooks) == 0 { - if err = uc.check(); err != nil { - return nil, err - } - node, err = uc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = uc.check(); err != nil { - return nil, err - } - uc.mutation = mutation - if node, err = uc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(uc.hooks) - 1; i >= 0; i-- { - if uc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*User) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) - } - node = nv - } - return node, err + return withHooks[*User, UserMutation](ctx, uc.sqlSave, uc.mutation, uc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -143,6 +101,9 @@ func (uc *UserCreate) check() error { } func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { + if err := uc.check(); err != nil { + return nil, err + } _node, _spec := uc.createSpec() if err := sqlgraph.CreateNode(ctx, uc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -152,6 +113,8 @@ func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + uc.mutation.id = &_node.ID + uc.mutation.done = true return _node, nil } diff --git a/entc/integration/template/ent/user_delete.go b/entc/integration/template/ent/user_delete.go index 81fda2262..a050db895 100644 --- a/entc/integration/template/ent/user_delete.go +++ b/entc/integration/template/ent/user_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (ud *UserDelete) Where(ps ...predicate.User) *UserDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (ud *UserDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(ud.hooks) == 0 { - affected, err = ud.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - ud.mutation = mutation - affected, err = ud.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(ud.hooks) - 1; i >= 0; i-- { - if ud.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ud.hooks[i](mut) - } - n, err := mut.Mutate(ctx, ud.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, UserMutation](ctx, ud.sqlExec, ud.mutation, ud.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (ud *UserDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + ud.mutation.done = true return affected, err } diff --git a/entc/integration/template/ent/user_update.go b/entc/integration/template/ent/user_update.go index 0f4372248..ffea6c2ef 100644 --- a/entc/integration/template/ent/user_update.go +++ b/entc/integration/template/ent/user_update.go @@ -117,34 +117,7 @@ func (uu *UserUpdate) RemoveFriends(u ...*User) *UserUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (uu *UserUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(uu.hooks) == 0 { - affected, err = uu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - uu.mutation = mutation - affected, err = uu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(uu.hooks) - 1; i >= 0; i-- { - if uu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, uu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, UserMutation](ctx, uu.sqlSave, uu.mutation, uu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -306,6 +279,7 @@ func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + uu.mutation.done = true return n, nil } @@ -409,40 +383,7 @@ func (uuo *UserUpdateOne) Select(field string, fields ...string) *UserUpdateOne // Save executes the query and returns the updated User entity. func (uuo *UserUpdateOne) Save(ctx context.Context) (*User, error) { - var ( - err error - node *User - ) - if len(uuo.hooks) == 0 { - node, err = uuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - uuo.mutation = mutation - node, err = uuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(uuo.hooks) - 1; i >= 0; i-- { - if uuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*User) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) - } - node = nv - } - return node, err + return withHooks[*User, UserMutation](ctx, uuo.sqlSave, uuo.mutation, uuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -624,5 +565,6 @@ func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) } return nil, err } + uuo.mutation.done = true return _node, nil } diff --git a/examples/edgeindex/ent/city_create.go b/examples/edgeindex/ent/city_create.go index 59367cfcc..cdb1ef50d 100644 --- a/examples/edgeindex/ent/city_create.go +++ b/examples/edgeindex/ent/city_create.go @@ -52,49 +52,7 @@ func (cc *CityCreate) Mutation() *CityMutation { // Save creates the City in the database. func (cc *CityCreate) Save(ctx context.Context) (*City, error) { - var ( - err error - node *City - ) - if len(cc.hooks) == 0 { - if err = cc.check(); err != nil { - return nil, err - } - node, err = cc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CityMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = cc.check(); err != nil { - return nil, err - } - cc.mutation = mutation - if node, err = cc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(cc.hooks) - 1; i >= 0; i-- { - if cc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = cc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, cc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*City) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from CityMutation", v) - } - node = nv - } - return node, err + return withHooks[*City, CityMutation](ctx, cc.sqlSave, cc.mutation, cc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -128,6 +86,9 @@ func (cc *CityCreate) check() error { } func (cc *CityCreate) sqlSave(ctx context.Context) (*City, error) { + if err := cc.check(); err != nil { + return nil, err + } _node, _spec := cc.createSpec() if err := sqlgraph.CreateNode(ctx, cc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -137,6 +98,8 @@ func (cc *CityCreate) sqlSave(ctx context.Context) (*City, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + cc.mutation.id = &_node.ID + cc.mutation.done = true return _node, nil } diff --git a/examples/edgeindex/ent/city_delete.go b/examples/edgeindex/ent/city_delete.go index 325211d75..4dbb36c2f 100644 --- a/examples/edgeindex/ent/city_delete.go +++ b/examples/edgeindex/ent/city_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (cd *CityDelete) Where(ps ...predicate.City) *CityDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (cd *CityDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(cd.hooks) == 0 { - affected, err = cd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CityMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - cd.mutation = mutation - affected, err = cd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(cd.hooks) - 1; i >= 0; i-- { - if cd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = cd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, cd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, CityMutation](ctx, cd.sqlExec, cd.mutation, cd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (cd *CityDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + cd.mutation.done = true return affected, err } diff --git a/examples/edgeindex/ent/city_update.go b/examples/edgeindex/ent/city_update.go index facaf703b..eb570f7e5 100644 --- a/examples/edgeindex/ent/city_update.go +++ b/examples/edgeindex/ent/city_update.go @@ -81,34 +81,7 @@ func (cu *CityUpdate) RemoveStreets(s ...*Street) *CityUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (cu *CityUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(cu.hooks) == 0 { - affected, err = cu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CityMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - cu.mutation = mutation - affected, err = cu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(cu.hooks) - 1; i >= 0; i-- { - if cu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = cu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, cu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, CityMutation](ctx, cu.sqlSave, cu.mutation, cu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -216,6 +189,7 @@ func (cu *CityUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + cu.mutation.done = true return n, nil } @@ -283,40 +257,7 @@ func (cuo *CityUpdateOne) Select(field string, fields ...string) *CityUpdateOne // Save executes the query and returns the updated City entity. func (cuo *CityUpdateOne) Save(ctx context.Context) (*City, error) { - var ( - err error - node *City - ) - if len(cuo.hooks) == 0 { - node, err = cuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CityMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - cuo.mutation = mutation - node, err = cuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(cuo.hooks) - 1; i >= 0; i-- { - if cuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = cuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, cuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*City) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from CityMutation", v) - } - node = nv - } - return node, err + return withHooks[*City, CityMutation](ctx, cuo.sqlSave, cuo.mutation, cuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -444,5 +385,6 @@ func (cuo *CityUpdateOne) sqlSave(ctx context.Context) (_node *City, err error) } return nil, err } + cuo.mutation.done = true return _node, nil } diff --git a/examples/edgeindex/ent/ent.go b/examples/edgeindex/ent/ent.go index e674db928..ecd1f0533 100644 --- a/examples/edgeindex/ent/ent.go +++ b/examples/edgeindex/ent/ent.go @@ -475,6 +475,40 @@ func (s *selector) BoolX(ctx context.Context) bool { return v } +// withHooks invokes the builder operation with the given hooks, if any. +func withHooks[V Value, M any, PM interface { + *M + Mutation +}](ctx context.Context, exec func(context.Context) (V, error), mutation PM, hooks []Hook) (value V, err error) { + if len(hooks) == 0 { + return exec(ctx) + } + var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { + mutationT, ok := m.(PM) + if !ok { + return nil, fmt.Errorf("unexpected mutation type %T", m) + } + // Set the mutation to the builder. + *mutation = *mutationT + return exec(ctx) + }) + for i := len(hooks) - 1; i >= 0; i-- { + if hooks[i] == nil { + return value, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") + } + mut = hooks[i](mut) + } + v, err := mut.Mutate(ctx, mutation) + if err != nil { + return value, err + } + nv, ok := v.(V) + if !ok { + return value, fmt.Errorf("unexpected node type %T returned from %T", v, mutation) + } + return nv, nil +} + // newQueryContext returns a new context with the given QueryContext attached in case it does not exist. func newQueryContext(ctx context.Context, typ, op string) context.Context { if ent.QueryFromContext(ctx) == nil { diff --git a/examples/edgeindex/ent/street_create.go b/examples/edgeindex/ent/street_create.go index 7d9258770..54c35b7cd 100644 --- a/examples/edgeindex/ent/street_create.go +++ b/examples/edgeindex/ent/street_create.go @@ -56,49 +56,7 @@ func (sc *StreetCreate) Mutation() *StreetMutation { // Save creates the Street in the database. func (sc *StreetCreate) Save(ctx context.Context) (*Street, error) { - var ( - err error - node *Street - ) - if len(sc.hooks) == 0 { - if err = sc.check(); err != nil { - return nil, err - } - node, err = sc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*StreetMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = sc.check(); err != nil { - return nil, err - } - sc.mutation = mutation - if node, err = sc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(sc.hooks) - 1; i >= 0; i-- { - if sc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = sc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, sc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Street) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from StreetMutation", v) - } - node = nv - } - return node, err + return withHooks[*Street, StreetMutation](ctx, sc.sqlSave, sc.mutation, sc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -132,6 +90,9 @@ func (sc *StreetCreate) check() error { } func (sc *StreetCreate) sqlSave(ctx context.Context) (*Street, error) { + if err := sc.check(); err != nil { + return nil, err + } _node, _spec := sc.createSpec() if err := sqlgraph.CreateNode(ctx, sc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -141,6 +102,8 @@ func (sc *StreetCreate) sqlSave(ctx context.Context) (*Street, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + sc.mutation.id = &_node.ID + sc.mutation.done = true return _node, nil } diff --git a/examples/edgeindex/ent/street_delete.go b/examples/edgeindex/ent/street_delete.go index a8e35225c..108e64add 100644 --- a/examples/edgeindex/ent/street_delete.go +++ b/examples/edgeindex/ent/street_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (sd *StreetDelete) Where(ps ...predicate.Street) *StreetDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (sd *StreetDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(sd.hooks) == 0 { - affected, err = sd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*StreetMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - sd.mutation = mutation - affected, err = sd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(sd.hooks) - 1; i >= 0; i-- { - if sd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = sd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, sd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, StreetMutation](ctx, sd.sqlExec, sd.mutation, sd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (sd *StreetDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + sd.mutation.done = true return affected, err } diff --git a/examples/edgeindex/ent/street_update.go b/examples/edgeindex/ent/street_update.go index ed6a4e3e4..ac5ac0b8c 100644 --- a/examples/edgeindex/ent/street_update.go +++ b/examples/edgeindex/ent/street_update.go @@ -70,34 +70,7 @@ func (su *StreetUpdate) ClearCity() *StreetUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (su *StreetUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(su.hooks) == 0 { - affected, err = su.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*StreetMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - su.mutation = mutation - affected, err = su.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(su.hooks) - 1; i >= 0; i-- { - if su.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = su.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, su.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, StreetMutation](ctx, su.sqlSave, su.mutation, su.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -186,6 +159,7 @@ func (su *StreetUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + su.mutation.done = true return n, nil } @@ -242,40 +216,7 @@ func (suo *StreetUpdateOne) Select(field string, fields ...string) *StreetUpdate // Save executes the query and returns the updated Street entity. func (suo *StreetUpdateOne) Save(ctx context.Context) (*Street, error) { - var ( - err error - node *Street - ) - if len(suo.hooks) == 0 { - node, err = suo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*StreetMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - suo.mutation = mutation - node, err = suo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(suo.hooks) - 1; i >= 0; i-- { - if suo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = suo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, suo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Street) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from StreetMutation", v) - } - node = nv - } - return node, err + return withHooks[*Street, StreetMutation](ctx, suo.sqlSave, suo.mutation, suo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -384,5 +325,6 @@ func (suo *StreetUpdateOne) sqlSave(ctx context.Context) (_node *Street, err err } return nil, err } + suo.mutation.done = true return _node, nil } diff --git a/examples/entcpkg/ent/ent.go b/examples/entcpkg/ent/ent.go index f22d94a09..ceea144e9 100644 --- a/examples/entcpkg/ent/ent.go +++ b/examples/entcpkg/ent/ent.go @@ -473,6 +473,40 @@ func (s *selector) BoolX(ctx context.Context) bool { return v } +// withHooks invokes the builder operation with the given hooks, if any. +func withHooks[V Value, M any, PM interface { + *M + Mutation +}](ctx context.Context, exec func(context.Context) (V, error), mutation PM, hooks []Hook) (value V, err error) { + if len(hooks) == 0 { + return exec(ctx) + } + var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { + mutationT, ok := m.(PM) + if !ok { + return nil, fmt.Errorf("unexpected mutation type %T", m) + } + // Set the mutation to the builder. + *mutation = *mutationT + return exec(ctx) + }) + for i := len(hooks) - 1; i >= 0; i-- { + if hooks[i] == nil { + return value, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") + } + mut = hooks[i](mut) + } + v, err := mut.Mutate(ctx, mutation) + if err != nil { + return value, err + } + nv, ok := v.(V) + if !ok { + return value, fmt.Errorf("unexpected node type %T returned from %T", v, mutation) + } + return nv, nil +} + // newQueryContext returns a new context with the given QueryContext attached in case it does not exist. func newQueryContext(ctx context.Context, typ, op string) context.Context { if ent.QueryFromContext(ctx) == nil { diff --git a/examples/entcpkg/ent/user_create.go b/examples/entcpkg/ent/user_create.go index 852ff42c9..a1181b61d 100644 --- a/examples/entcpkg/ent/user_create.go +++ b/examples/entcpkg/ent/user_create.go @@ -57,49 +57,7 @@ func (uc *UserCreate) Mutation() *UserMutation { // Save creates the User in the database. func (uc *UserCreate) Save(ctx context.Context) (*User, error) { - var ( - err error - node *User - ) - if len(uc.hooks) == 0 { - if err = uc.check(); err != nil { - return nil, err - } - node, err = uc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = uc.check(); err != nil { - return nil, err - } - uc.mutation = mutation - if node, err = uc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(uc.hooks) - 1; i >= 0; i-- { - if uc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*User) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) - } - node = nv - } - return node, err + return withHooks[*User, UserMutation](ctx, uc.sqlSave, uc.mutation, uc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -130,6 +88,9 @@ func (uc *UserCreate) check() error { } func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { + if err := uc.check(); err != nil { + return nil, err + } _node, _spec := uc.createSpec() if err := sqlgraph.CreateNode(ctx, uc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -139,6 +100,8 @@ func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + uc.mutation.id = &_node.ID + uc.mutation.done = true return _node, nil } diff --git a/examples/entcpkg/ent/user_delete.go b/examples/entcpkg/ent/user_delete.go index 60c3f19a9..32b6ecad7 100644 --- a/examples/entcpkg/ent/user_delete.go +++ b/examples/entcpkg/ent/user_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (ud *UserDelete) Where(ps ...predicate.User) *UserDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (ud *UserDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(ud.hooks) == 0 { - affected, err = ud.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - ud.mutation = mutation - affected, err = ud.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(ud.hooks) - 1; i >= 0; i-- { - if ud.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ud.hooks[i](mut) - } - n, err := mut.Mutate(ctx, ud.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, UserMutation](ctx, ud.sqlExec, ud.mutation, ud.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (ud *UserDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + ud.mutation.done = true return affected, err } diff --git a/examples/entcpkg/ent/user_update.go b/examples/entcpkg/ent/user_update.go index a16766a28..8a2a4f844 100644 --- a/examples/entcpkg/ent/user_update.go +++ b/examples/entcpkg/ent/user_update.go @@ -85,34 +85,7 @@ func (uu *UserUpdate) Mutation() *UserMutation { // Save executes the query and returns the number of nodes affected by the update operation. func (uu *UserUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(uu.hooks) == 0 { - affected, err = uu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - uu.mutation = mutation - affected, err = uu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(uu.hooks) - 1; i >= 0; i-- { - if uu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, uu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, UserMutation](ctx, uu.sqlSave, uu.mutation, uu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -178,6 +151,7 @@ func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + uu.mutation.done = true return n, nil } @@ -250,40 +224,7 @@ func (uuo *UserUpdateOne) Select(field string, fields ...string) *UserUpdateOne // Save executes the query and returns the updated User entity. func (uuo *UserUpdateOne) Save(ctx context.Context) (*User, error) { - var ( - err error - node *User - ) - if len(uuo.hooks) == 0 { - node, err = uuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - uuo.mutation = mutation - node, err = uuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(uuo.hooks) - 1; i >= 0; i-- { - if uuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*User) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) - } - node = nv - } - return node, err + return withHooks[*User, UserMutation](ctx, uuo.sqlSave, uuo.mutation, uuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -369,5 +310,6 @@ func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) } return nil, err } + uuo.mutation.done = true return _node, nil } diff --git a/examples/fs/ent/ent.go b/examples/fs/ent/ent.go index 0c3ec7283..6a531aa8b 100644 --- a/examples/fs/ent/ent.go +++ b/examples/fs/ent/ent.go @@ -473,6 +473,40 @@ func (s *selector) BoolX(ctx context.Context) bool { return v } +// withHooks invokes the builder operation with the given hooks, if any. +func withHooks[V Value, M any, PM interface { + *M + Mutation +}](ctx context.Context, exec func(context.Context) (V, error), mutation PM, hooks []Hook) (value V, err error) { + if len(hooks) == 0 { + return exec(ctx) + } + var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { + mutationT, ok := m.(PM) + if !ok { + return nil, fmt.Errorf("unexpected mutation type %T", m) + } + // Set the mutation to the builder. + *mutation = *mutationT + return exec(ctx) + }) + for i := len(hooks) - 1; i >= 0; i-- { + if hooks[i] == nil { + return value, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") + } + mut = hooks[i](mut) + } + v, err := mut.Mutate(ctx, mutation) + if err != nil { + return value, err + } + nv, ok := v.(V) + if !ok { + return value, fmt.Errorf("unexpected node type %T returned from %T", v, mutation) + } + return nv, nil +} + // newQueryContext returns a new context with the given QueryContext attached in case it does not exist. func newQueryContext(ctx context.Context, typ, op string) context.Context { if ent.QueryFromContext(ctx) == nil { diff --git a/examples/fs/ent/file_create.go b/examples/fs/ent/file_create.go index a66a76c36..60218180c 100644 --- a/examples/fs/ent/file_create.go +++ b/examples/fs/ent/file_create.go @@ -84,50 +84,8 @@ func (fc *FileCreate) Mutation() *FileMutation { // Save creates the File in the database. func (fc *FileCreate) Save(ctx context.Context) (*File, error) { - var ( - err error - node *File - ) fc.defaults() - if len(fc.hooks) == 0 { - if err = fc.check(); err != nil { - return nil, err - } - node, err = fc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*FileMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = fc.check(); err != nil { - return nil, err - } - fc.mutation = mutation - if node, err = fc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(fc.hooks) - 1; i >= 0; i-- { - if fc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = fc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, fc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*File) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from FileMutation", v) - } - node = nv - } - return node, err + return withHooks[*File, FileMutation](ctx, fc.sqlSave, fc.mutation, fc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -172,6 +130,9 @@ func (fc *FileCreate) check() error { } func (fc *FileCreate) sqlSave(ctx context.Context) (*File, error) { + if err := fc.check(); err != nil { + return nil, err + } _node, _spec := fc.createSpec() if err := sqlgraph.CreateNode(ctx, fc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -181,6 +142,8 @@ func (fc *FileCreate) sqlSave(ctx context.Context) (*File, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + fc.mutation.id = &_node.ID + fc.mutation.done = true return _node, nil } diff --git a/examples/fs/ent/file_delete.go b/examples/fs/ent/file_delete.go index 4cb8b5b05..e28b9a5fd 100644 --- a/examples/fs/ent/file_delete.go +++ b/examples/fs/ent/file_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (fd *FileDelete) Where(ps ...predicate.File) *FileDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (fd *FileDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(fd.hooks) == 0 { - affected, err = fd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*FileMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - fd.mutation = mutation - affected, err = fd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(fd.hooks) - 1; i >= 0; i-- { - if fd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = fd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, fd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, FileMutation](ctx, fd.sqlExec, fd.mutation, fd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (fd *FileDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + fd.mutation.done = true return affected, err } diff --git a/examples/fs/ent/file_update.go b/examples/fs/ent/file_update.go index f7aefe856..f547e3cd3 100644 --- a/examples/fs/ent/file_update.go +++ b/examples/fs/ent/file_update.go @@ -125,34 +125,7 @@ func (fu *FileUpdate) RemoveChildren(f ...*File) *FileUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (fu *FileUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(fu.hooks) == 0 { - affected, err = fu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*FileMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - fu.mutation = mutation - affected, err = fu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(fu.hooks) - 1; i >= 0; i-- { - if fu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = fu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, fu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, FileMutation](ctx, fu.sqlSave, fu.mutation, fu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -298,6 +271,7 @@ func (fu *FileUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + fu.mutation.done = true return n, nil } @@ -410,40 +384,7 @@ func (fuo *FileUpdateOne) Select(field string, fields ...string) *FileUpdateOne // Save executes the query and returns the updated File entity. func (fuo *FileUpdateOne) Save(ctx context.Context) (*File, error) { - var ( - err error - node *File - ) - if len(fuo.hooks) == 0 { - node, err = fuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*FileMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - fuo.mutation = mutation - node, err = fuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(fuo.hooks) - 1; i >= 0; i-- { - if fuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = fuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, fuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*File) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from FileMutation", v) - } - node = nv - } - return node, err + return withHooks[*File, FileMutation](ctx, fuo.sqlSave, fuo.mutation, fuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -609,5 +550,6 @@ func (fuo *FileUpdateOne) sqlSave(ctx context.Context) (_node *File, err error) } return nil, err } + fuo.mutation.done = true return _node, nil } diff --git a/examples/jsonencode/ent/ent.go b/examples/jsonencode/ent/ent.go index 944d2ccbd..344fd56d0 100644 --- a/examples/jsonencode/ent/ent.go +++ b/examples/jsonencode/ent/ent.go @@ -475,6 +475,40 @@ func (s *selector) BoolX(ctx context.Context) bool { return v } +// withHooks invokes the builder operation with the given hooks, if any. +func withHooks[V Value, M any, PM interface { + *M + Mutation +}](ctx context.Context, exec func(context.Context) (V, error), mutation PM, hooks []Hook) (value V, err error) { + if len(hooks) == 0 { + return exec(ctx) + } + var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { + mutationT, ok := m.(PM) + if !ok { + return nil, fmt.Errorf("unexpected mutation type %T", m) + } + // Set the mutation to the builder. + *mutation = *mutationT + return exec(ctx) + }) + for i := len(hooks) - 1; i >= 0; i-- { + if hooks[i] == nil { + return value, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") + } + mut = hooks[i](mut) + } + v, err := mut.Mutate(ctx, mutation) + if err != nil { + return value, err + } + nv, ok := v.(V) + if !ok { + return value, fmt.Errorf("unexpected node type %T returned from %T", v, mutation) + } + return nv, nil +} + // newQueryContext returns a new context with the given QueryContext attached in case it does not exist. func newQueryContext(ctx context.Context, typ, op string) context.Context { if ent.QueryFromContext(ctx) == nil { diff --git a/examples/jsonencode/ent/pet_create.go b/examples/jsonencode/ent/pet_create.go index 347c11849..5a3482fcf 100644 --- a/examples/jsonencode/ent/pet_create.go +++ b/examples/jsonencode/ent/pet_create.go @@ -54,49 +54,7 @@ func (pc *PetCreate) Mutation() *PetMutation { // Save creates the Pet in the database. func (pc *PetCreate) Save(ctx context.Context) (*Pet, error) { - var ( - err error - node *Pet - ) - if len(pc.hooks) == 0 { - if err = pc.check(); err != nil { - return nil, err - } - node, err = pc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*PetMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = pc.check(); err != nil { - return nil, err - } - pc.mutation = mutation - if node, err = pc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(pc.hooks) - 1; i >= 0; i-- { - if pc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = pc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, pc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Pet) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from PetMutation", v) - } - node = nv - } - return node, err + return withHooks[*Pet, PetMutation](ctx, pc.sqlSave, pc.mutation, pc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -139,6 +97,9 @@ func (pc *PetCreate) check() error { } func (pc *PetCreate) sqlSave(ctx context.Context) (*Pet, error) { + if err := pc.check(); err != nil { + return nil, err + } _node, _spec := pc.createSpec() if err := sqlgraph.CreateNode(ctx, pc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -148,6 +109,8 @@ func (pc *PetCreate) sqlSave(ctx context.Context) (*Pet, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + pc.mutation.id = &_node.ID + pc.mutation.done = true return _node, nil } diff --git a/examples/jsonencode/ent/pet_delete.go b/examples/jsonencode/ent/pet_delete.go index 1b44fadba..4f12d6d14 100644 --- a/examples/jsonencode/ent/pet_delete.go +++ b/examples/jsonencode/ent/pet_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (pd *PetDelete) Where(ps ...predicate.Pet) *PetDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (pd *PetDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(pd.hooks) == 0 { - affected, err = pd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*PetMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - pd.mutation = mutation - affected, err = pd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(pd.hooks) - 1; i >= 0; i-- { - if pd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = pd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, pd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, PetMutation](ctx, pd.sqlExec, pd.mutation, pd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (pd *PetDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + pd.mutation.done = true return affected, err } diff --git a/examples/jsonencode/ent/pet_update.go b/examples/jsonencode/ent/pet_update.go index 1525191ed..b529fd316 100644 --- a/examples/jsonencode/ent/pet_update.go +++ b/examples/jsonencode/ent/pet_update.go @@ -75,40 +75,7 @@ func (pu *PetUpdate) ClearOwner() *PetUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (pu *PetUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(pu.hooks) == 0 { - if err = pu.check(); err != nil { - return 0, err - } - affected, err = pu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*PetMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = pu.check(); err != nil { - return 0, err - } - pu.mutation = mutation - affected, err = pu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(pu.hooks) - 1; i >= 0; i-- { - if pu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = pu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, pu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, PetMutation](ctx, pu.sqlSave, pu.mutation, pu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -142,6 +109,9 @@ func (pu *PetUpdate) check() error { } func (pu *PetUpdate) sqlSave(ctx context.Context) (n int, err error) { + if err := pu.check(); err != nil { + return n, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: pet.Table, @@ -211,6 +181,7 @@ func (pu *PetUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + pu.mutation.done = true return n, nil } @@ -272,46 +243,7 @@ func (puo *PetUpdateOne) Select(field string, fields ...string) *PetUpdateOne { // Save executes the query and returns the updated Pet entity. func (puo *PetUpdateOne) Save(ctx context.Context) (*Pet, error) { - var ( - err error - node *Pet - ) - if len(puo.hooks) == 0 { - if err = puo.check(); err != nil { - return nil, err - } - node, err = puo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*PetMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = puo.check(); err != nil { - return nil, err - } - puo.mutation = mutation - node, err = puo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(puo.hooks) - 1; i >= 0; i-- { - if puo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = puo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, puo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Pet) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from PetMutation", v) - } - node = nv - } - return node, err + return withHooks[*Pet, PetMutation](ctx, puo.sqlSave, puo.mutation, puo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -345,6 +277,9 @@ func (puo *PetUpdateOne) check() error { } func (puo *PetUpdateOne) sqlSave(ctx context.Context) (_node *Pet, err error) { + if err := puo.check(); err != nil { + return _node, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: pet.Table, @@ -434,5 +369,6 @@ func (puo *PetUpdateOne) sqlSave(ctx context.Context) (_node *Pet, err error) { } return nil, err } + puo.mutation.done = true return _node, nil } diff --git a/examples/jsonencode/ent/user_create.go b/examples/jsonencode/ent/user_create.go index 74052b63e..59ab447ee 100644 --- a/examples/jsonencode/ent/user_create.go +++ b/examples/jsonencode/ent/user_create.go @@ -58,49 +58,7 @@ func (uc *UserCreate) Mutation() *UserMutation { // Save creates the User in the database. func (uc *UserCreate) Save(ctx context.Context) (*User, error) { - var ( - err error - node *User - ) - if len(uc.hooks) == 0 { - if err = uc.check(); err != nil { - return nil, err - } - node, err = uc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = uc.check(); err != nil { - return nil, err - } - uc.mutation = mutation - if node, err = uc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(uc.hooks) - 1; i >= 0; i-- { - if uc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*User) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) - } - node = nv - } - return node, err + return withHooks[*User, UserMutation](ctx, uc.sqlSave, uc.mutation, uc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -137,6 +95,9 @@ func (uc *UserCreate) check() error { } func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { + if err := uc.check(); err != nil { + return nil, err + } _node, _spec := uc.createSpec() if err := sqlgraph.CreateNode(ctx, uc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -146,6 +107,8 @@ func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + uc.mutation.id = &_node.ID + uc.mutation.done = true return _node, nil } diff --git a/examples/jsonencode/ent/user_delete.go b/examples/jsonencode/ent/user_delete.go index 28503af3b..9170cbd45 100644 --- a/examples/jsonencode/ent/user_delete.go +++ b/examples/jsonencode/ent/user_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (ud *UserDelete) Where(ps ...predicate.User) *UserDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (ud *UserDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(ud.hooks) == 0 { - affected, err = ud.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - ud.mutation = mutation - affected, err = ud.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(ud.hooks) - 1; i >= 0; i-- { - if ud.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ud.hooks[i](mut) - } - n, err := mut.Mutate(ctx, ud.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, UserMutation](ctx, ud.sqlExec, ud.mutation, ud.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (ud *UserDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + ud.mutation.done = true return affected, err } diff --git a/examples/jsonencode/ent/user_update.go b/examples/jsonencode/ent/user_update.go index eecfdcd66..74e9f02d8 100644 --- a/examples/jsonencode/ent/user_update.go +++ b/examples/jsonencode/ent/user_update.go @@ -94,34 +94,7 @@ func (uu *UserUpdate) RemovePets(p ...*Pet) *UserUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (uu *UserUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(uu.hooks) == 0 { - affected, err = uu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - uu.mutation = mutation - affected, err = uu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(uu.hooks) - 1; i >= 0; i-- { - if uu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, uu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, UserMutation](ctx, uu.sqlSave, uu.mutation, uu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -235,6 +208,7 @@ func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + uu.mutation.done = true return n, nil } @@ -315,40 +289,7 @@ func (uuo *UserUpdateOne) Select(field string, fields ...string) *UserUpdateOne // Save executes the query and returns the updated User entity. func (uuo *UserUpdateOne) Save(ctx context.Context) (*User, error) { - var ( - err error - node *User - ) - if len(uuo.hooks) == 0 { - node, err = uuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - uuo.mutation = mutation - node, err = uuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(uuo.hooks) - 1; i >= 0; i-- { - if uuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*User) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) - } - node = nv - } - return node, err + return withHooks[*User, UserMutation](ctx, uuo.sqlSave, uuo.mutation, uuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -482,5 +423,6 @@ func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) } return nil, err } + uuo.mutation.done = true return _node, nil } diff --git a/examples/m2m2types/ent/ent.go b/examples/m2m2types/ent/ent.go index aff8ae8cd..343961a3f 100644 --- a/examples/m2m2types/ent/ent.go +++ b/examples/m2m2types/ent/ent.go @@ -475,6 +475,40 @@ func (s *selector) BoolX(ctx context.Context) bool { return v } +// withHooks invokes the builder operation with the given hooks, if any. +func withHooks[V Value, M any, PM interface { + *M + Mutation +}](ctx context.Context, exec func(context.Context) (V, error), mutation PM, hooks []Hook) (value V, err error) { + if len(hooks) == 0 { + return exec(ctx) + } + var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { + mutationT, ok := m.(PM) + if !ok { + return nil, fmt.Errorf("unexpected mutation type %T", m) + } + // Set the mutation to the builder. + *mutation = *mutationT + return exec(ctx) + }) + for i := len(hooks) - 1; i >= 0; i-- { + if hooks[i] == nil { + return value, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") + } + mut = hooks[i](mut) + } + v, err := mut.Mutate(ctx, mutation) + if err != nil { + return value, err + } + nv, ok := v.(V) + if !ok { + return value, fmt.Errorf("unexpected node type %T returned from %T", v, mutation) + } + return nv, nil +} + // newQueryContext returns a new context with the given QueryContext attached in case it does not exist. func newQueryContext(ctx context.Context, typ, op string) context.Context { if ent.QueryFromContext(ctx) == nil { diff --git a/examples/m2m2types/ent/group_create.go b/examples/m2m2types/ent/group_create.go index 558ef2bc4..ca035a741 100644 --- a/examples/m2m2types/ent/group_create.go +++ b/examples/m2m2types/ent/group_create.go @@ -52,49 +52,7 @@ func (gc *GroupCreate) Mutation() *GroupMutation { // Save creates the Group in the database. func (gc *GroupCreate) Save(ctx context.Context) (*Group, error) { - var ( - err error - node *Group - ) - if len(gc.hooks) == 0 { - if err = gc.check(); err != nil { - return nil, err - } - node, err = gc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GroupMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = gc.check(); err != nil { - return nil, err - } - gc.mutation = mutation - if node, err = gc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(gc.hooks) - 1; i >= 0; i-- { - if gc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = gc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, gc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Group) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from GroupMutation", v) - } - node = nv - } - return node, err + return withHooks[*Group, GroupMutation](ctx, gc.sqlSave, gc.mutation, gc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -128,6 +86,9 @@ func (gc *GroupCreate) check() error { } func (gc *GroupCreate) sqlSave(ctx context.Context) (*Group, error) { + if err := gc.check(); err != nil { + return nil, err + } _node, _spec := gc.createSpec() if err := sqlgraph.CreateNode(ctx, gc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -137,6 +98,8 @@ func (gc *GroupCreate) sqlSave(ctx context.Context) (*Group, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + gc.mutation.id = &_node.ID + gc.mutation.done = true return _node, nil } diff --git a/examples/m2m2types/ent/group_delete.go b/examples/m2m2types/ent/group_delete.go index daa815855..939a43067 100644 --- a/examples/m2m2types/ent/group_delete.go +++ b/examples/m2m2types/ent/group_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (gd *GroupDelete) Where(ps ...predicate.Group) *GroupDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (gd *GroupDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(gd.hooks) == 0 { - affected, err = gd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GroupMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - gd.mutation = mutation - affected, err = gd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(gd.hooks) - 1; i >= 0; i-- { - if gd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = gd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, gd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, GroupMutation](ctx, gd.sqlExec, gd.mutation, gd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (gd *GroupDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + gd.mutation.done = true return affected, err } diff --git a/examples/m2m2types/ent/group_update.go b/examples/m2m2types/ent/group_update.go index 1022d0c01..5fe3d215d 100644 --- a/examples/m2m2types/ent/group_update.go +++ b/examples/m2m2types/ent/group_update.go @@ -81,34 +81,7 @@ func (gu *GroupUpdate) RemoveUsers(u ...*User) *GroupUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (gu *GroupUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(gu.hooks) == 0 { - affected, err = gu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GroupMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - gu.mutation = mutation - affected, err = gu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(gu.hooks) - 1; i >= 0; i-- { - if gu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = gu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, gu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, GroupMutation](ctx, gu.sqlSave, gu.mutation, gu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -216,6 +189,7 @@ func (gu *GroupUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + gu.mutation.done = true return n, nil } @@ -283,40 +257,7 @@ func (guo *GroupUpdateOne) Select(field string, fields ...string) *GroupUpdateOn // Save executes the query and returns the updated Group entity. func (guo *GroupUpdateOne) Save(ctx context.Context) (*Group, error) { - var ( - err error - node *Group - ) - if len(guo.hooks) == 0 { - node, err = guo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GroupMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - guo.mutation = mutation - node, err = guo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(guo.hooks) - 1; i >= 0; i-- { - if guo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = guo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, guo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Group) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from GroupMutation", v) - } - node = nv - } - return node, err + return withHooks[*Group, GroupMutation](ctx, guo.sqlSave, guo.mutation, guo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -444,5 +385,6 @@ func (guo *GroupUpdateOne) sqlSave(ctx context.Context) (_node *Group, err error } return nil, err } + guo.mutation.done = true return _node, nil } diff --git a/examples/m2m2types/ent/user_create.go b/examples/m2m2types/ent/user_create.go index 2a79767b7..0289644d8 100644 --- a/examples/m2m2types/ent/user_create.go +++ b/examples/m2m2types/ent/user_create.go @@ -58,49 +58,7 @@ func (uc *UserCreate) Mutation() *UserMutation { // Save creates the User in the database. func (uc *UserCreate) Save(ctx context.Context) (*User, error) { - var ( - err error - node *User - ) - if len(uc.hooks) == 0 { - if err = uc.check(); err != nil { - return nil, err - } - node, err = uc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = uc.check(); err != nil { - return nil, err - } - uc.mutation = mutation - if node, err = uc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(uc.hooks) - 1; i >= 0; i-- { - if uc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*User) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) - } - node = nv - } - return node, err + return withHooks[*User, UserMutation](ctx, uc.sqlSave, uc.mutation, uc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -137,6 +95,9 @@ func (uc *UserCreate) check() error { } func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { + if err := uc.check(); err != nil { + return nil, err + } _node, _spec := uc.createSpec() if err := sqlgraph.CreateNode(ctx, uc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -146,6 +107,8 @@ func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + uc.mutation.id = &_node.ID + uc.mutation.done = true return _node, nil } diff --git a/examples/m2m2types/ent/user_delete.go b/examples/m2m2types/ent/user_delete.go index 66e2de0b1..71bd7b0bc 100644 --- a/examples/m2m2types/ent/user_delete.go +++ b/examples/m2m2types/ent/user_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (ud *UserDelete) Where(ps ...predicate.User) *UserDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (ud *UserDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(ud.hooks) == 0 { - affected, err = ud.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - ud.mutation = mutation - affected, err = ud.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(ud.hooks) - 1; i >= 0; i-- { - if ud.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ud.hooks[i](mut) - } - n, err := mut.Mutate(ctx, ud.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, UserMutation](ctx, ud.sqlExec, ud.mutation, ud.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (ud *UserDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + ud.mutation.done = true return affected, err } diff --git a/examples/m2m2types/ent/user_update.go b/examples/m2m2types/ent/user_update.go index a32297be4..4dd33134a 100644 --- a/examples/m2m2types/ent/user_update.go +++ b/examples/m2m2types/ent/user_update.go @@ -94,34 +94,7 @@ func (uu *UserUpdate) RemoveGroups(g ...*Group) *UserUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (uu *UserUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(uu.hooks) == 0 { - affected, err = uu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - uu.mutation = mutation - affected, err = uu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(uu.hooks) - 1; i >= 0; i-- { - if uu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, uu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, UserMutation](ctx, uu.sqlSave, uu.mutation, uu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -235,6 +208,7 @@ func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + uu.mutation.done = true return n, nil } @@ -315,40 +289,7 @@ func (uuo *UserUpdateOne) Select(field string, fields ...string) *UserUpdateOne // Save executes the query and returns the updated User entity. func (uuo *UserUpdateOne) Save(ctx context.Context) (*User, error) { - var ( - err error - node *User - ) - if len(uuo.hooks) == 0 { - node, err = uuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - uuo.mutation = mutation - node, err = uuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(uuo.hooks) - 1; i >= 0; i-- { - if uuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*User) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) - } - node = nv - } - return node, err + return withHooks[*User, UserMutation](ctx, uuo.sqlSave, uuo.mutation, uuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -482,5 +423,6 @@ func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) } return nil, err } + uuo.mutation.done = true return _node, nil } diff --git a/examples/m2mbidi/ent/ent.go b/examples/m2mbidi/ent/ent.go index f1e5af67d..e46b6e7c5 100644 --- a/examples/m2mbidi/ent/ent.go +++ b/examples/m2mbidi/ent/ent.go @@ -473,6 +473,40 @@ func (s *selector) BoolX(ctx context.Context) bool { return v } +// withHooks invokes the builder operation with the given hooks, if any. +func withHooks[V Value, M any, PM interface { + *M + Mutation +}](ctx context.Context, exec func(context.Context) (V, error), mutation PM, hooks []Hook) (value V, err error) { + if len(hooks) == 0 { + return exec(ctx) + } + var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { + mutationT, ok := m.(PM) + if !ok { + return nil, fmt.Errorf("unexpected mutation type %T", m) + } + // Set the mutation to the builder. + *mutation = *mutationT + return exec(ctx) + }) + for i := len(hooks) - 1; i >= 0; i-- { + if hooks[i] == nil { + return value, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") + } + mut = hooks[i](mut) + } + v, err := mut.Mutate(ctx, mutation) + if err != nil { + return value, err + } + nv, ok := v.(V) + if !ok { + return value, fmt.Errorf("unexpected node type %T returned from %T", v, mutation) + } + return nv, nil +} + // newQueryContext returns a new context with the given QueryContext attached in case it does not exist. func newQueryContext(ctx context.Context, typ, op string) context.Context { if ent.QueryFromContext(ctx) == nil { diff --git a/examples/m2mbidi/ent/user_create.go b/examples/m2mbidi/ent/user_create.go index 1b46d37dd..7b5a6270a 100644 --- a/examples/m2mbidi/ent/user_create.go +++ b/examples/m2mbidi/ent/user_create.go @@ -57,49 +57,7 @@ func (uc *UserCreate) Mutation() *UserMutation { // Save creates the User in the database. func (uc *UserCreate) Save(ctx context.Context) (*User, error) { - var ( - err error - node *User - ) - if len(uc.hooks) == 0 { - if err = uc.check(); err != nil { - return nil, err - } - node, err = uc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = uc.check(); err != nil { - return nil, err - } - uc.mutation = mutation - if node, err = uc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(uc.hooks) - 1; i >= 0; i-- { - if uc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*User) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) - } - node = nv - } - return node, err + return withHooks[*User, UserMutation](ctx, uc.sqlSave, uc.mutation, uc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -136,6 +94,9 @@ func (uc *UserCreate) check() error { } func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { + if err := uc.check(); err != nil { + return nil, err + } _node, _spec := uc.createSpec() if err := sqlgraph.CreateNode(ctx, uc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -145,6 +106,8 @@ func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + uc.mutation.id = &_node.ID + uc.mutation.done = true return _node, nil } diff --git a/examples/m2mbidi/ent/user_delete.go b/examples/m2mbidi/ent/user_delete.go index de7db685f..35ff9355c 100644 --- a/examples/m2mbidi/ent/user_delete.go +++ b/examples/m2mbidi/ent/user_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (ud *UserDelete) Where(ps ...predicate.User) *UserDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (ud *UserDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(ud.hooks) == 0 { - affected, err = ud.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - ud.mutation = mutation - affected, err = ud.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(ud.hooks) - 1; i >= 0; i-- { - if ud.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ud.hooks[i](mut) - } - n, err := mut.Mutate(ctx, ud.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, UserMutation](ctx, ud.sqlExec, ud.mutation, ud.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (ud *UserDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + ud.mutation.done = true return affected, err } diff --git a/examples/m2mbidi/ent/user_update.go b/examples/m2mbidi/ent/user_update.go index 658c3cbe4..009a73bc2 100644 --- a/examples/m2mbidi/ent/user_update.go +++ b/examples/m2mbidi/ent/user_update.go @@ -93,34 +93,7 @@ func (uu *UserUpdate) RemoveFriends(u ...*User) *UserUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (uu *UserUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(uu.hooks) == 0 { - affected, err = uu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - uu.mutation = mutation - affected, err = uu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(uu.hooks) - 1; i >= 0; i-- { - if uu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, uu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, UserMutation](ctx, uu.sqlSave, uu.mutation, uu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -234,6 +207,7 @@ func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + uu.mutation.done = true return n, nil } @@ -314,40 +288,7 @@ func (uuo *UserUpdateOne) Select(field string, fields ...string) *UserUpdateOne // Save executes the query and returns the updated User entity. func (uuo *UserUpdateOne) Save(ctx context.Context) (*User, error) { - var ( - err error - node *User - ) - if len(uuo.hooks) == 0 { - node, err = uuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - uuo.mutation = mutation - node, err = uuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(uuo.hooks) - 1; i >= 0; i-- { - if uuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*User) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) - } - node = nv - } - return node, err + return withHooks[*User, UserMutation](ctx, uuo.sqlSave, uuo.mutation, uuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -481,5 +422,6 @@ func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) } return nil, err } + uuo.mutation.done = true return _node, nil } diff --git a/examples/m2mrecur/ent/ent.go b/examples/m2mrecur/ent/ent.go index 8aa12c35d..ccc15f88c 100644 --- a/examples/m2mrecur/ent/ent.go +++ b/examples/m2mrecur/ent/ent.go @@ -473,6 +473,40 @@ func (s *selector) BoolX(ctx context.Context) bool { return v } +// withHooks invokes the builder operation with the given hooks, if any. +func withHooks[V Value, M any, PM interface { + *M + Mutation +}](ctx context.Context, exec func(context.Context) (V, error), mutation PM, hooks []Hook) (value V, err error) { + if len(hooks) == 0 { + return exec(ctx) + } + var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { + mutationT, ok := m.(PM) + if !ok { + return nil, fmt.Errorf("unexpected mutation type %T", m) + } + // Set the mutation to the builder. + *mutation = *mutationT + return exec(ctx) + }) + for i := len(hooks) - 1; i >= 0; i-- { + if hooks[i] == nil { + return value, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") + } + mut = hooks[i](mut) + } + v, err := mut.Mutate(ctx, mutation) + if err != nil { + return value, err + } + nv, ok := v.(V) + if !ok { + return value, fmt.Errorf("unexpected node type %T returned from %T", v, mutation) + } + return nv, nil +} + // newQueryContext returns a new context with the given QueryContext attached in case it does not exist. func newQueryContext(ctx context.Context, typ, op string) context.Context { if ent.QueryFromContext(ctx) == nil { diff --git a/examples/m2mrecur/ent/user_create.go b/examples/m2mrecur/ent/user_create.go index f41153e07..e0bfe79d1 100644 --- a/examples/m2mrecur/ent/user_create.go +++ b/examples/m2mrecur/ent/user_create.go @@ -72,49 +72,7 @@ func (uc *UserCreate) Mutation() *UserMutation { // Save creates the User in the database. func (uc *UserCreate) Save(ctx context.Context) (*User, error) { - var ( - err error - node *User - ) - if len(uc.hooks) == 0 { - if err = uc.check(); err != nil { - return nil, err - } - node, err = uc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = uc.check(); err != nil { - return nil, err - } - uc.mutation = mutation - if node, err = uc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(uc.hooks) - 1; i >= 0; i-- { - if uc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*User) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) - } - node = nv - } - return node, err + return withHooks[*User, UserMutation](ctx, uc.sqlSave, uc.mutation, uc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -151,6 +109,9 @@ func (uc *UserCreate) check() error { } func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { + if err := uc.check(); err != nil { + return nil, err + } _node, _spec := uc.createSpec() if err := sqlgraph.CreateNode(ctx, uc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -160,6 +121,8 @@ func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + uc.mutation.id = &_node.ID + uc.mutation.done = true return _node, nil } diff --git a/examples/m2mrecur/ent/user_delete.go b/examples/m2mrecur/ent/user_delete.go index 04cf350f2..d3c4150a5 100644 --- a/examples/m2mrecur/ent/user_delete.go +++ b/examples/m2mrecur/ent/user_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (ud *UserDelete) Where(ps ...predicate.User) *UserDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (ud *UserDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(ud.hooks) == 0 { - affected, err = ud.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - ud.mutation = mutation - affected, err = ud.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(ud.hooks) - 1; i >= 0; i-- { - if ud.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ud.hooks[i](mut) - } - n, err := mut.Mutate(ctx, ud.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, UserMutation](ctx, ud.sqlExec, ud.mutation, ud.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (ud *UserDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + ud.mutation.done = true return affected, err } diff --git a/examples/m2mrecur/ent/user_update.go b/examples/m2mrecur/ent/user_update.go index f6771ce55..d1d828cf4 100644 --- a/examples/m2mrecur/ent/user_update.go +++ b/examples/m2mrecur/ent/user_update.go @@ -129,34 +129,7 @@ func (uu *UserUpdate) RemoveFollowing(u ...*User) *UserUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (uu *UserUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(uu.hooks) == 0 { - affected, err = uu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - uu.mutation = mutation - affected, err = uu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(uu.hooks) - 1; i >= 0; i-- { - if uu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, uu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, UserMutation](ctx, uu.sqlSave, uu.mutation, uu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -324,6 +297,7 @@ func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + uu.mutation.done = true return n, nil } @@ -440,40 +414,7 @@ func (uuo *UserUpdateOne) Select(field string, fields ...string) *UserUpdateOne // Save executes the query and returns the updated User entity. func (uuo *UserUpdateOne) Save(ctx context.Context) (*User, error) { - var ( - err error - node *User - ) - if len(uuo.hooks) == 0 { - node, err = uuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - uuo.mutation = mutation - node, err = uuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(uuo.hooks) - 1; i >= 0; i-- { - if uuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*User) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) - } - node = nv - } - return node, err + return withHooks[*User, UserMutation](ctx, uuo.sqlSave, uuo.mutation, uuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -661,5 +602,6 @@ func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) } return nil, err } + uuo.mutation.done = true return _node, nil } diff --git a/examples/migration/ent/ent.go b/examples/migration/ent/ent.go index 0c1d4255a..2f7e682a0 100644 --- a/examples/migration/ent/ent.go +++ b/examples/migration/ent/ent.go @@ -473,6 +473,40 @@ func (s *selector) BoolX(ctx context.Context) bool { return v } +// withHooks invokes the builder operation with the given hooks, if any. +func withHooks[V Value, M any, PM interface { + *M + Mutation +}](ctx context.Context, exec func(context.Context) (V, error), mutation PM, hooks []Hook) (value V, err error) { + if len(hooks) == 0 { + return exec(ctx) + } + var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { + mutationT, ok := m.(PM) + if !ok { + return nil, fmt.Errorf("unexpected mutation type %T", m) + } + // Set the mutation to the builder. + *mutation = *mutationT + return exec(ctx) + }) + for i := len(hooks) - 1; i >= 0; i-- { + if hooks[i] == nil { + return value, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") + } + mut = hooks[i](mut) + } + v, err := mut.Mutate(ctx, mutation) + if err != nil { + return value, err + } + nv, ok := v.(V) + if !ok { + return value, fmt.Errorf("unexpected node type %T returned from %T", v, mutation) + } + return nv, nil +} + // newQueryContext returns a new context with the given QueryContext attached in case it does not exist. func newQueryContext(ctx context.Context, typ, op string) context.Context { if ent.QueryFromContext(ctx) == nil { diff --git a/examples/migration/ent/user_create.go b/examples/migration/ent/user_create.go index f06b00f63..c1ef334b5 100644 --- a/examples/migration/ent/user_create.go +++ b/examples/migration/ent/user_create.go @@ -48,49 +48,7 @@ func (uc *UserCreate) Mutation() *UserMutation { // Save creates the User in the database. func (uc *UserCreate) Save(ctx context.Context) (*User, error) { - var ( - err error - node *User - ) - if len(uc.hooks) == 0 { - if err = uc.check(); err != nil { - return nil, err - } - node, err = uc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = uc.check(); err != nil { - return nil, err - } - uc.mutation = mutation - if node, err = uc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(uc.hooks) - 1; i >= 0; i-- { - if uc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*User) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) - } - node = nv - } - return node, err + return withHooks[*User, UserMutation](ctx, uc.sqlSave, uc.mutation, uc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -127,6 +85,9 @@ func (uc *UserCreate) check() error { } func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { + if err := uc.check(); err != nil { + return nil, err + } _node, _spec := uc.createSpec() if err := sqlgraph.CreateNode(ctx, uc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -136,6 +97,8 @@ func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + uc.mutation.id = &_node.ID + uc.mutation.done = true return _node, nil } diff --git a/examples/migration/ent/user_delete.go b/examples/migration/ent/user_delete.go index 8ac0d82b8..35f823650 100644 --- a/examples/migration/ent/user_delete.go +++ b/examples/migration/ent/user_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (ud *UserDelete) Where(ps ...predicate.User) *UserDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (ud *UserDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(ud.hooks) == 0 { - affected, err = ud.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - ud.mutation = mutation - affected, err = ud.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(ud.hooks) - 1; i >= 0; i-- { - if ud.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ud.hooks[i](mut) - } - n, err := mut.Mutate(ctx, ud.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, UserMutation](ctx, ud.sqlExec, ud.mutation, ud.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (ud *UserDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + ud.mutation.done = true return affected, err } diff --git a/examples/migration/ent/user_update.go b/examples/migration/ent/user_update.go index 39ee46899..a77170cef 100644 --- a/examples/migration/ent/user_update.go +++ b/examples/migration/ent/user_update.go @@ -76,34 +76,7 @@ func (uu *UserUpdate) Mutation() *UserMutation { // Save executes the query and returns the number of nodes affected by the update operation. func (uu *UserUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(uu.hooks) == 0 { - affected, err = uu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - uu.mutation = mutation - affected, err = uu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(uu.hooks) - 1; i >= 0; i-- { - if uu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, uu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, UserMutation](ctx, uu.sqlSave, uu.mutation, uu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -174,6 +147,7 @@ func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + uu.mutation.done = true return n, nil } @@ -236,40 +210,7 @@ func (uuo *UserUpdateOne) Select(field string, fields ...string) *UserUpdateOne // Save executes the query and returns the updated User entity. func (uuo *UserUpdateOne) Save(ctx context.Context) (*User, error) { - var ( - err error - node *User - ) - if len(uuo.hooks) == 0 { - node, err = uuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - uuo.mutation = mutation - node, err = uuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(uuo.hooks) - 1; i >= 0; i-- { - if uuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*User) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) - } - node = nv - } - return node, err + return withHooks[*User, UserMutation](ctx, uuo.sqlSave, uuo.mutation, uuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -360,5 +301,6 @@ func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) } return nil, err } + uuo.mutation.done = true return _node, nil } diff --git a/examples/o2m2types/ent/ent.go b/examples/o2m2types/ent/ent.go index 468019b9c..1591a98fa 100644 --- a/examples/o2m2types/ent/ent.go +++ b/examples/o2m2types/ent/ent.go @@ -475,6 +475,40 @@ func (s *selector) BoolX(ctx context.Context) bool { return v } +// withHooks invokes the builder operation with the given hooks, if any. +func withHooks[V Value, M any, PM interface { + *M + Mutation +}](ctx context.Context, exec func(context.Context) (V, error), mutation PM, hooks []Hook) (value V, err error) { + if len(hooks) == 0 { + return exec(ctx) + } + var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { + mutationT, ok := m.(PM) + if !ok { + return nil, fmt.Errorf("unexpected mutation type %T", m) + } + // Set the mutation to the builder. + *mutation = *mutationT + return exec(ctx) + }) + for i := len(hooks) - 1; i >= 0; i-- { + if hooks[i] == nil { + return value, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") + } + mut = hooks[i](mut) + } + v, err := mut.Mutate(ctx, mutation) + if err != nil { + return value, err + } + nv, ok := v.(V) + if !ok { + return value, fmt.Errorf("unexpected node type %T returned from %T", v, mutation) + } + return nv, nil +} + // newQueryContext returns a new context with the given QueryContext attached in case it does not exist. func newQueryContext(ctx context.Context, typ, op string) context.Context { if ent.QueryFromContext(ctx) == nil { diff --git a/examples/o2m2types/ent/pet_create.go b/examples/o2m2types/ent/pet_create.go index 2b5f8a03a..5b0390919 100644 --- a/examples/o2m2types/ent/pet_create.go +++ b/examples/o2m2types/ent/pet_create.go @@ -56,49 +56,7 @@ func (pc *PetCreate) Mutation() *PetMutation { // Save creates the Pet in the database. func (pc *PetCreate) Save(ctx context.Context) (*Pet, error) { - var ( - err error - node *Pet - ) - if len(pc.hooks) == 0 { - if err = pc.check(); err != nil { - return nil, err - } - node, err = pc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*PetMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = pc.check(); err != nil { - return nil, err - } - pc.mutation = mutation - if node, err = pc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(pc.hooks) - 1; i >= 0; i-- { - if pc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = pc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, pc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Pet) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from PetMutation", v) - } - node = nv - } - return node, err + return withHooks[*Pet, PetMutation](ctx, pc.sqlSave, pc.mutation, pc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -132,6 +90,9 @@ func (pc *PetCreate) check() error { } func (pc *PetCreate) sqlSave(ctx context.Context) (*Pet, error) { + if err := pc.check(); err != nil { + return nil, err + } _node, _spec := pc.createSpec() if err := sqlgraph.CreateNode(ctx, pc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -141,6 +102,8 @@ func (pc *PetCreate) sqlSave(ctx context.Context) (*Pet, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + pc.mutation.id = &_node.ID + pc.mutation.done = true return _node, nil } diff --git a/examples/o2m2types/ent/pet_delete.go b/examples/o2m2types/ent/pet_delete.go index b5518dce4..65f668e25 100644 --- a/examples/o2m2types/ent/pet_delete.go +++ b/examples/o2m2types/ent/pet_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (pd *PetDelete) Where(ps ...predicate.Pet) *PetDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (pd *PetDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(pd.hooks) == 0 { - affected, err = pd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*PetMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - pd.mutation = mutation - affected, err = pd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(pd.hooks) - 1; i >= 0; i-- { - if pd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = pd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, pd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, PetMutation](ctx, pd.sqlExec, pd.mutation, pd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (pd *PetDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + pd.mutation.done = true return affected, err } diff --git a/examples/o2m2types/ent/pet_update.go b/examples/o2m2types/ent/pet_update.go index fa5f3f783..d25f23098 100644 --- a/examples/o2m2types/ent/pet_update.go +++ b/examples/o2m2types/ent/pet_update.go @@ -70,34 +70,7 @@ func (pu *PetUpdate) ClearOwner() *PetUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (pu *PetUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(pu.hooks) == 0 { - affected, err = pu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*PetMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - pu.mutation = mutation - affected, err = pu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(pu.hooks) - 1; i >= 0; i-- { - if pu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = pu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, pu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, PetMutation](ctx, pu.sqlSave, pu.mutation, pu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -186,6 +159,7 @@ func (pu *PetUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + pu.mutation.done = true return n, nil } @@ -242,40 +216,7 @@ func (puo *PetUpdateOne) Select(field string, fields ...string) *PetUpdateOne { // Save executes the query and returns the updated Pet entity. func (puo *PetUpdateOne) Save(ctx context.Context) (*Pet, error) { - var ( - err error - node *Pet - ) - if len(puo.hooks) == 0 { - node, err = puo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*PetMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - puo.mutation = mutation - node, err = puo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(puo.hooks) - 1; i >= 0; i-- { - if puo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = puo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, puo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Pet) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from PetMutation", v) - } - node = nv - } - return node, err + return withHooks[*Pet, PetMutation](ctx, puo.sqlSave, puo.mutation, puo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -384,5 +325,6 @@ func (puo *PetUpdateOne) sqlSave(ctx context.Context) (_node *Pet, err error) { } return nil, err } + puo.mutation.done = true return _node, nil } diff --git a/examples/o2m2types/ent/user_create.go b/examples/o2m2types/ent/user_create.go index 760890b30..d5d423ff1 100644 --- a/examples/o2m2types/ent/user_create.go +++ b/examples/o2m2types/ent/user_create.go @@ -58,49 +58,7 @@ func (uc *UserCreate) Mutation() *UserMutation { // Save creates the User in the database. func (uc *UserCreate) Save(ctx context.Context) (*User, error) { - var ( - err error - node *User - ) - if len(uc.hooks) == 0 { - if err = uc.check(); err != nil { - return nil, err - } - node, err = uc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = uc.check(); err != nil { - return nil, err - } - uc.mutation = mutation - if node, err = uc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(uc.hooks) - 1; i >= 0; i-- { - if uc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*User) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) - } - node = nv - } - return node, err + return withHooks[*User, UserMutation](ctx, uc.sqlSave, uc.mutation, uc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -137,6 +95,9 @@ func (uc *UserCreate) check() error { } func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { + if err := uc.check(); err != nil { + return nil, err + } _node, _spec := uc.createSpec() if err := sqlgraph.CreateNode(ctx, uc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -146,6 +107,8 @@ func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + uc.mutation.id = &_node.ID + uc.mutation.done = true return _node, nil } diff --git a/examples/o2m2types/ent/user_delete.go b/examples/o2m2types/ent/user_delete.go index e10de0a09..21dad5360 100644 --- a/examples/o2m2types/ent/user_delete.go +++ b/examples/o2m2types/ent/user_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (ud *UserDelete) Where(ps ...predicate.User) *UserDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (ud *UserDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(ud.hooks) == 0 { - affected, err = ud.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - ud.mutation = mutation - affected, err = ud.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(ud.hooks) - 1; i >= 0; i-- { - if ud.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ud.hooks[i](mut) - } - n, err := mut.Mutate(ctx, ud.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, UserMutation](ctx, ud.sqlExec, ud.mutation, ud.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (ud *UserDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + ud.mutation.done = true return affected, err } diff --git a/examples/o2m2types/ent/user_update.go b/examples/o2m2types/ent/user_update.go index 355b72beb..b9abf23ab 100644 --- a/examples/o2m2types/ent/user_update.go +++ b/examples/o2m2types/ent/user_update.go @@ -94,34 +94,7 @@ func (uu *UserUpdate) RemovePets(p ...*Pet) *UserUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (uu *UserUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(uu.hooks) == 0 { - affected, err = uu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - uu.mutation = mutation - affected, err = uu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(uu.hooks) - 1; i >= 0; i-- { - if uu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, uu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, UserMutation](ctx, uu.sqlSave, uu.mutation, uu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -235,6 +208,7 @@ func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + uu.mutation.done = true return n, nil } @@ -315,40 +289,7 @@ func (uuo *UserUpdateOne) Select(field string, fields ...string) *UserUpdateOne // Save executes the query and returns the updated User entity. func (uuo *UserUpdateOne) Save(ctx context.Context) (*User, error) { - var ( - err error - node *User - ) - if len(uuo.hooks) == 0 { - node, err = uuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - uuo.mutation = mutation - node, err = uuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(uuo.hooks) - 1; i >= 0; i-- { - if uuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*User) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) - } - node = nv - } - return node, err + return withHooks[*User, UserMutation](ctx, uuo.sqlSave, uuo.mutation, uuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -482,5 +423,6 @@ func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) } return nil, err } + uuo.mutation.done = true return _node, nil } diff --git a/examples/o2mrecur/ent/ent.go b/examples/o2mrecur/ent/ent.go index 649edf669..97d8b0e75 100644 --- a/examples/o2mrecur/ent/ent.go +++ b/examples/o2mrecur/ent/ent.go @@ -473,6 +473,40 @@ func (s *selector) BoolX(ctx context.Context) bool { return v } +// withHooks invokes the builder operation with the given hooks, if any. +func withHooks[V Value, M any, PM interface { + *M + Mutation +}](ctx context.Context, exec func(context.Context) (V, error), mutation PM, hooks []Hook) (value V, err error) { + if len(hooks) == 0 { + return exec(ctx) + } + var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { + mutationT, ok := m.(PM) + if !ok { + return nil, fmt.Errorf("unexpected mutation type %T", m) + } + // Set the mutation to the builder. + *mutation = *mutationT + return exec(ctx) + }) + for i := len(hooks) - 1; i >= 0; i-- { + if hooks[i] == nil { + return value, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") + } + mut = hooks[i](mut) + } + v, err := mut.Mutate(ctx, mutation) + if err != nil { + return value, err + } + nv, ok := v.(V) + if !ok { + return value, fmt.Errorf("unexpected node type %T returned from %T", v, mutation) + } + return nv, nil +} + // newQueryContext returns a new context with the given QueryContext attached in case it does not exist. func newQueryContext(ctx context.Context, typ, op string) context.Context { if ent.QueryFromContext(ctx) == nil { diff --git a/examples/o2mrecur/ent/node_create.go b/examples/o2mrecur/ent/node_create.go index 3b0de5a3c..32b1d0144 100644 --- a/examples/o2mrecur/ent/node_create.go +++ b/examples/o2mrecur/ent/node_create.go @@ -70,49 +70,7 @@ func (nc *NodeCreate) Mutation() *NodeMutation { // Save creates the Node in the database. func (nc *NodeCreate) Save(ctx context.Context) (*Node, error) { - var ( - err error - node *Node - ) - if len(nc.hooks) == 0 { - if err = nc.check(); err != nil { - return nil, err - } - node, err = nc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*NodeMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = nc.check(); err != nil { - return nil, err - } - nc.mutation = mutation - if node, err = nc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(nc.hooks) - 1; i >= 0; i-- { - if nc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = nc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, nc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Node) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from NodeMutation", v) - } - node = nv - } - return node, err + return withHooks[*Node, NodeMutation](ctx, nc.sqlSave, nc.mutation, nc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -146,6 +104,9 @@ func (nc *NodeCreate) check() error { } func (nc *NodeCreate) sqlSave(ctx context.Context) (*Node, error) { + if err := nc.check(); err != nil { + return nil, err + } _node, _spec := nc.createSpec() if err := sqlgraph.CreateNode(ctx, nc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -155,6 +116,8 @@ func (nc *NodeCreate) sqlSave(ctx context.Context) (*Node, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + nc.mutation.id = &_node.ID + nc.mutation.done = true return _node, nil } diff --git a/examples/o2mrecur/ent/node_delete.go b/examples/o2mrecur/ent/node_delete.go index 63285ea05..527961942 100644 --- a/examples/o2mrecur/ent/node_delete.go +++ b/examples/o2mrecur/ent/node_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (nd *NodeDelete) Where(ps ...predicate.Node) *NodeDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (nd *NodeDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(nd.hooks) == 0 { - affected, err = nd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*NodeMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - nd.mutation = mutation - affected, err = nd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(nd.hooks) - 1; i >= 0; i-- { - if nd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = nd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, nd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, NodeMutation](ctx, nd.sqlExec, nd.mutation, nd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (nd *NodeDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + nd.mutation.done = true return affected, err } diff --git a/examples/o2mrecur/ent/node_update.go b/examples/o2mrecur/ent/node_update.go index 0d595a194..d297c1db6 100644 --- a/examples/o2mrecur/ent/node_update.go +++ b/examples/o2mrecur/ent/node_update.go @@ -112,34 +112,7 @@ func (nu *NodeUpdate) RemoveChildren(n ...*Node) *NodeUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (nu *NodeUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(nu.hooks) == 0 { - affected, err = nu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*NodeMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - nu.mutation = mutation - affected, err = nu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(nu.hooks) - 1; i >= 0; i-- { - if nu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = nu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, nu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, NodeMutation](ctx, nu.sqlSave, nu.mutation, nu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -285,6 +258,7 @@ func (nu *NodeUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + nu.mutation.done = true return n, nil } @@ -384,40 +358,7 @@ func (nuo *NodeUpdateOne) Select(field string, fields ...string) *NodeUpdateOne // Save executes the query and returns the updated Node entity. func (nuo *NodeUpdateOne) Save(ctx context.Context) (*Node, error) { - var ( - err error - node *Node - ) - if len(nuo.hooks) == 0 { - node, err = nuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*NodeMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - nuo.mutation = mutation - node, err = nuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(nuo.hooks) - 1; i >= 0; i-- { - if nuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = nuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, nuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Node) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from NodeMutation", v) - } - node = nv - } - return node, err + return withHooks[*Node, NodeMutation](ctx, nuo.sqlSave, nuo.mutation, nuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -583,5 +524,6 @@ func (nuo *NodeUpdateOne) sqlSave(ctx context.Context) (_node *Node, err error) } return nil, err } + nuo.mutation.done = true return _node, nil } diff --git a/examples/o2o2types/ent/card_create.go b/examples/o2o2types/ent/card_create.go index a9a79935c..8556c8b6c 100644 --- a/examples/o2o2types/ent/card_create.go +++ b/examples/o2o2types/ent/card_create.go @@ -55,49 +55,7 @@ func (cc *CardCreate) Mutation() *CardMutation { // Save creates the Card in the database. func (cc *CardCreate) Save(ctx context.Context) (*Card, error) { - var ( - err error - node *Card - ) - if len(cc.hooks) == 0 { - if err = cc.check(); err != nil { - return nil, err - } - node, err = cc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CardMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = cc.check(); err != nil { - return nil, err - } - cc.mutation = mutation - if node, err = cc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(cc.hooks) - 1; i >= 0; i-- { - if cc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = cc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, cc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Card) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from CardMutation", v) - } - node = nv - } - return node, err + return withHooks[*Card, CardMutation](ctx, cc.sqlSave, cc.mutation, cc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -137,6 +95,9 @@ func (cc *CardCreate) check() error { } func (cc *CardCreate) sqlSave(ctx context.Context) (*Card, error) { + if err := cc.check(); err != nil { + return nil, err + } _node, _spec := cc.createSpec() if err := sqlgraph.CreateNode(ctx, cc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -146,6 +107,8 @@ func (cc *CardCreate) sqlSave(ctx context.Context) (*Card, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + cc.mutation.id = &_node.ID + cc.mutation.done = true return _node, nil } diff --git a/examples/o2o2types/ent/card_delete.go b/examples/o2o2types/ent/card_delete.go index 241feeb15..a7949c1de 100644 --- a/examples/o2o2types/ent/card_delete.go +++ b/examples/o2o2types/ent/card_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (cd *CardDelete) Where(ps ...predicate.Card) *CardDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (cd *CardDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(cd.hooks) == 0 { - affected, err = cd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CardMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - cd.mutation = mutation - affected, err = cd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(cd.hooks) - 1; i >= 0; i-- { - if cd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = cd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, cd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, CardMutation](ctx, cd.sqlExec, cd.mutation, cd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (cd *CardDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + cd.mutation.done = true return affected, err } diff --git a/examples/o2o2types/ent/card_update.go b/examples/o2o2types/ent/card_update.go index 3de227ebd..e1b1d4010 100644 --- a/examples/o2o2types/ent/card_update.go +++ b/examples/o2o2types/ent/card_update.go @@ -69,40 +69,7 @@ func (cu *CardUpdate) ClearOwner() *CardUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (cu *CardUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(cu.hooks) == 0 { - if err = cu.check(); err != nil { - return 0, err - } - affected, err = cu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CardMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = cu.check(); err != nil { - return 0, err - } - cu.mutation = mutation - affected, err = cu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(cu.hooks) - 1; i >= 0; i-- { - if cu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = cu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, cu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, CardMutation](ctx, cu.sqlSave, cu.mutation, cu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -136,6 +103,9 @@ func (cu *CardUpdate) check() error { } func (cu *CardUpdate) sqlSave(ctx context.Context) (n int, err error) { + if err := cu.check(); err != nil { + return n, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: card.Table, @@ -202,6 +172,7 @@ func (cu *CardUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + cu.mutation.done = true return n, nil } @@ -256,46 +227,7 @@ func (cuo *CardUpdateOne) Select(field string, fields ...string) *CardUpdateOne // Save executes the query and returns the updated Card entity. func (cuo *CardUpdateOne) Save(ctx context.Context) (*Card, error) { - var ( - err error - node *Card - ) - if len(cuo.hooks) == 0 { - if err = cuo.check(); err != nil { - return nil, err - } - node, err = cuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CardMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = cuo.check(); err != nil { - return nil, err - } - cuo.mutation = mutation - node, err = cuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(cuo.hooks) - 1; i >= 0; i-- { - if cuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = cuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, cuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Card) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from CardMutation", v) - } - node = nv - } - return node, err + return withHooks[*Card, CardMutation](ctx, cuo.sqlSave, cuo.mutation, cuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -329,6 +261,9 @@ func (cuo *CardUpdateOne) check() error { } func (cuo *CardUpdateOne) sqlSave(ctx context.Context) (_node *Card, err error) { + if err := cuo.check(); err != nil { + return _node, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: card.Table, @@ -415,5 +350,6 @@ func (cuo *CardUpdateOne) sqlSave(ctx context.Context) (_node *Card, err error) } return nil, err } + cuo.mutation.done = true return _node, nil } diff --git a/examples/o2o2types/ent/ent.go b/examples/o2o2types/ent/ent.go index 786ee37fd..e421bb712 100644 --- a/examples/o2o2types/ent/ent.go +++ b/examples/o2o2types/ent/ent.go @@ -475,6 +475,40 @@ func (s *selector) BoolX(ctx context.Context) bool { return v } +// withHooks invokes the builder operation with the given hooks, if any. +func withHooks[V Value, M any, PM interface { + *M + Mutation +}](ctx context.Context, exec func(context.Context) (V, error), mutation PM, hooks []Hook) (value V, err error) { + if len(hooks) == 0 { + return exec(ctx) + } + var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { + mutationT, ok := m.(PM) + if !ok { + return nil, fmt.Errorf("unexpected mutation type %T", m) + } + // Set the mutation to the builder. + *mutation = *mutationT + return exec(ctx) + }) + for i := len(hooks) - 1; i >= 0; i-- { + if hooks[i] == nil { + return value, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") + } + mut = hooks[i](mut) + } + v, err := mut.Mutate(ctx, mutation) + if err != nil { + return value, err + } + nv, ok := v.(V) + if !ok { + return value, fmt.Errorf("unexpected node type %T returned from %T", v, mutation) + } + return nv, nil +} + // newQueryContext returns a new context with the given QueryContext attached in case it does not exist. func newQueryContext(ctx context.Context, typ, op string) context.Context { if ent.QueryFromContext(ctx) == nil { diff --git a/examples/o2o2types/ent/user_create.go b/examples/o2o2types/ent/user_create.go index 954634444..92bbdb973 100644 --- a/examples/o2o2types/ent/user_create.go +++ b/examples/o2o2types/ent/user_create.go @@ -62,49 +62,7 @@ func (uc *UserCreate) Mutation() *UserMutation { // Save creates the User in the database. func (uc *UserCreate) Save(ctx context.Context) (*User, error) { - var ( - err error - node *User - ) - if len(uc.hooks) == 0 { - if err = uc.check(); err != nil { - return nil, err - } - node, err = uc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = uc.check(); err != nil { - return nil, err - } - uc.mutation = mutation - if node, err = uc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(uc.hooks) - 1; i >= 0; i-- { - if uc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*User) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) - } - node = nv - } - return node, err + return withHooks[*User, UserMutation](ctx, uc.sqlSave, uc.mutation, uc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -141,6 +99,9 @@ func (uc *UserCreate) check() error { } func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { + if err := uc.check(); err != nil { + return nil, err + } _node, _spec := uc.createSpec() if err := sqlgraph.CreateNode(ctx, uc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -150,6 +111,8 @@ func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + uc.mutation.id = &_node.ID + uc.mutation.done = true return _node, nil } diff --git a/examples/o2o2types/ent/user_delete.go b/examples/o2o2types/ent/user_delete.go index f2e7f1c94..80394a647 100644 --- a/examples/o2o2types/ent/user_delete.go +++ b/examples/o2o2types/ent/user_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (ud *UserDelete) Where(ps ...predicate.User) *UserDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (ud *UserDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(ud.hooks) == 0 { - affected, err = ud.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - ud.mutation = mutation - affected, err = ud.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(ud.hooks) - 1; i >= 0; i-- { - if ud.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ud.hooks[i](mut) - } - n, err := mut.Mutate(ctx, ud.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, UserMutation](ctx, ud.sqlExec, ud.mutation, ud.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (ud *UserDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + ud.mutation.done = true return affected, err } diff --git a/examples/o2o2types/ent/user_update.go b/examples/o2o2types/ent/user_update.go index 2f55903c5..5108f4d78 100644 --- a/examples/o2o2types/ent/user_update.go +++ b/examples/o2o2types/ent/user_update.go @@ -83,34 +83,7 @@ func (uu *UserUpdate) ClearCard() *UserUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (uu *UserUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(uu.hooks) == 0 { - affected, err = uu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - uu.mutation = mutation - affected, err = uu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(uu.hooks) - 1; i >= 0; i-- { - if uu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, uu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, UserMutation](ctx, uu.sqlSave, uu.mutation, uu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -205,6 +178,7 @@ func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + uu.mutation.done = true return n, nil } @@ -274,40 +248,7 @@ func (uuo *UserUpdateOne) Select(field string, fields ...string) *UserUpdateOne // Save executes the query and returns the updated User entity. func (uuo *UserUpdateOne) Save(ctx context.Context) (*User, error) { - var ( - err error - node *User - ) - if len(uuo.hooks) == 0 { - node, err = uuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - uuo.mutation = mutation - node, err = uuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(uuo.hooks) - 1; i >= 0; i-- { - if uuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*User) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) - } - node = nv - } - return node, err + return withHooks[*User, UserMutation](ctx, uuo.sqlSave, uuo.mutation, uuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -422,5 +363,6 @@ func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) } return nil, err } + uuo.mutation.done = true return _node, nil } diff --git a/examples/o2obidi/ent/ent.go b/examples/o2obidi/ent/ent.go index b7d160798..e8908b5f7 100644 --- a/examples/o2obidi/ent/ent.go +++ b/examples/o2obidi/ent/ent.go @@ -473,6 +473,40 @@ func (s *selector) BoolX(ctx context.Context) bool { return v } +// withHooks invokes the builder operation with the given hooks, if any. +func withHooks[V Value, M any, PM interface { + *M + Mutation +}](ctx context.Context, exec func(context.Context) (V, error), mutation PM, hooks []Hook) (value V, err error) { + if len(hooks) == 0 { + return exec(ctx) + } + var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { + mutationT, ok := m.(PM) + if !ok { + return nil, fmt.Errorf("unexpected mutation type %T", m) + } + // Set the mutation to the builder. + *mutation = *mutationT + return exec(ctx) + }) + for i := len(hooks) - 1; i >= 0; i-- { + if hooks[i] == nil { + return value, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") + } + mut = hooks[i](mut) + } + v, err := mut.Mutate(ctx, mutation) + if err != nil { + return value, err + } + nv, ok := v.(V) + if !ok { + return value, fmt.Errorf("unexpected node type %T returned from %T", v, mutation) + } + return nv, nil +} + // newQueryContext returns a new context with the given QueryContext attached in case it does not exist. func newQueryContext(ctx context.Context, typ, op string) context.Context { if ent.QueryFromContext(ctx) == nil { diff --git a/examples/o2obidi/ent/user_create.go b/examples/o2obidi/ent/user_create.go index 44a67ce53..71cd80498 100644 --- a/examples/o2obidi/ent/user_create.go +++ b/examples/o2obidi/ent/user_create.go @@ -61,49 +61,7 @@ func (uc *UserCreate) Mutation() *UserMutation { // Save creates the User in the database. func (uc *UserCreate) Save(ctx context.Context) (*User, error) { - var ( - err error - node *User - ) - if len(uc.hooks) == 0 { - if err = uc.check(); err != nil { - return nil, err - } - node, err = uc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = uc.check(); err != nil { - return nil, err - } - uc.mutation = mutation - if node, err = uc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(uc.hooks) - 1; i >= 0; i-- { - if uc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*User) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) - } - node = nv - } - return node, err + return withHooks[*User, UserMutation](ctx, uc.sqlSave, uc.mutation, uc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -140,6 +98,9 @@ func (uc *UserCreate) check() error { } func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { + if err := uc.check(); err != nil { + return nil, err + } _node, _spec := uc.createSpec() if err := sqlgraph.CreateNode(ctx, uc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -149,6 +110,8 @@ func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + uc.mutation.id = &_node.ID + uc.mutation.done = true return _node, nil } diff --git a/examples/o2obidi/ent/user_delete.go b/examples/o2obidi/ent/user_delete.go index b309fec4c..021c40989 100644 --- a/examples/o2obidi/ent/user_delete.go +++ b/examples/o2obidi/ent/user_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (ud *UserDelete) Where(ps ...predicate.User) *UserDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (ud *UserDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(ud.hooks) == 0 { - affected, err = ud.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - ud.mutation = mutation - affected, err = ud.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(ud.hooks) - 1; i >= 0; i-- { - if ud.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ud.hooks[i](mut) - } - n, err := mut.Mutate(ctx, ud.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, UserMutation](ctx, ud.sqlExec, ud.mutation, ud.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (ud *UserDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + ud.mutation.done = true return affected, err } diff --git a/examples/o2obidi/ent/user_update.go b/examples/o2obidi/ent/user_update.go index 0151a9fa7..68d4c5bf4 100644 --- a/examples/o2obidi/ent/user_update.go +++ b/examples/o2obidi/ent/user_update.go @@ -82,34 +82,7 @@ func (uu *UserUpdate) ClearSpouse() *UserUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (uu *UserUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(uu.hooks) == 0 { - affected, err = uu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - uu.mutation = mutation - affected, err = uu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(uu.hooks) - 1; i >= 0; i-- { - if uu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, uu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, UserMutation](ctx, uu.sqlSave, uu.mutation, uu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -204,6 +177,7 @@ func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + uu.mutation.done = true return n, nil } @@ -273,40 +247,7 @@ func (uuo *UserUpdateOne) Select(field string, fields ...string) *UserUpdateOne // Save executes the query and returns the updated User entity. func (uuo *UserUpdateOne) Save(ctx context.Context) (*User, error) { - var ( - err error - node *User - ) - if len(uuo.hooks) == 0 { - node, err = uuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - uuo.mutation = mutation - node, err = uuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(uuo.hooks) - 1; i >= 0; i-- { - if uuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*User) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) - } - node = nv - } - return node, err + return withHooks[*User, UserMutation](ctx, uuo.sqlSave, uuo.mutation, uuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -421,5 +362,6 @@ func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) } return nil, err } + uuo.mutation.done = true return _node, nil } diff --git a/examples/o2orecur/ent/ent.go b/examples/o2orecur/ent/ent.go index bf11f8631..6deb9be94 100644 --- a/examples/o2orecur/ent/ent.go +++ b/examples/o2orecur/ent/ent.go @@ -473,6 +473,40 @@ func (s *selector) BoolX(ctx context.Context) bool { return v } +// withHooks invokes the builder operation with the given hooks, if any. +func withHooks[V Value, M any, PM interface { + *M + Mutation +}](ctx context.Context, exec func(context.Context) (V, error), mutation PM, hooks []Hook) (value V, err error) { + if len(hooks) == 0 { + return exec(ctx) + } + var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { + mutationT, ok := m.(PM) + if !ok { + return nil, fmt.Errorf("unexpected mutation type %T", m) + } + // Set the mutation to the builder. + *mutation = *mutationT + return exec(ctx) + }) + for i := len(hooks) - 1; i >= 0; i-- { + if hooks[i] == nil { + return value, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") + } + mut = hooks[i](mut) + } + v, err := mut.Mutate(ctx, mutation) + if err != nil { + return value, err + } + nv, ok := v.(V) + if !ok { + return value, fmt.Errorf("unexpected node type %T returned from %T", v, mutation) + } + return nv, nil +} + // newQueryContext returns a new context with the given QueryContext attached in case it does not exist. func newQueryContext(ctx context.Context, typ, op string) context.Context { if ent.QueryFromContext(ctx) == nil { diff --git a/examples/o2orecur/ent/node_create.go b/examples/o2orecur/ent/node_create.go index c77966439..bf95e0876 100644 --- a/examples/o2orecur/ent/node_create.go +++ b/examples/o2orecur/ent/node_create.go @@ -74,49 +74,7 @@ func (nc *NodeCreate) Mutation() *NodeMutation { // Save creates the Node in the database. func (nc *NodeCreate) Save(ctx context.Context) (*Node, error) { - var ( - err error - node *Node - ) - if len(nc.hooks) == 0 { - if err = nc.check(); err != nil { - return nil, err - } - node, err = nc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*NodeMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = nc.check(); err != nil { - return nil, err - } - nc.mutation = mutation - if node, err = nc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(nc.hooks) - 1; i >= 0; i-- { - if nc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = nc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, nc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Node) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from NodeMutation", v) - } - node = nv - } - return node, err + return withHooks[*Node, NodeMutation](ctx, nc.sqlSave, nc.mutation, nc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -150,6 +108,9 @@ func (nc *NodeCreate) check() error { } func (nc *NodeCreate) sqlSave(ctx context.Context) (*Node, error) { + if err := nc.check(); err != nil { + return nil, err + } _node, _spec := nc.createSpec() if err := sqlgraph.CreateNode(ctx, nc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -159,6 +120,8 @@ func (nc *NodeCreate) sqlSave(ctx context.Context) (*Node, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + nc.mutation.id = &_node.ID + nc.mutation.done = true return _node, nil } diff --git a/examples/o2orecur/ent/node_delete.go b/examples/o2orecur/ent/node_delete.go index 85093eb5f..cc15079df 100644 --- a/examples/o2orecur/ent/node_delete.go +++ b/examples/o2orecur/ent/node_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (nd *NodeDelete) Where(ps ...predicate.Node) *NodeDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (nd *NodeDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(nd.hooks) == 0 { - affected, err = nd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*NodeMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - nd.mutation = mutation - affected, err = nd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(nd.hooks) - 1; i >= 0; i-- { - if nd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = nd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, nd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, NodeMutation](ctx, nd.sqlExec, nd.mutation, nd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (nd *NodeDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + nd.mutation.done = true return affected, err } diff --git a/examples/o2orecur/ent/node_update.go b/examples/o2orecur/ent/node_update.go index 74c82f0e1..6bfa2afa0 100644 --- a/examples/o2orecur/ent/node_update.go +++ b/examples/o2orecur/ent/node_update.go @@ -101,34 +101,7 @@ func (nu *NodeUpdate) ClearNext() *NodeUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (nu *NodeUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(nu.hooks) == 0 { - affected, err = nu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*NodeMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - nu.mutation = mutation - affected, err = nu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(nu.hooks) - 1; i >= 0; i-- { - if nu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = nu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, nu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, NodeMutation](ctx, nu.sqlSave, nu.mutation, nu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -255,6 +228,7 @@ func (nu *NodeUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + nu.mutation.done = true return n, nil } @@ -343,40 +317,7 @@ func (nuo *NodeUpdateOne) Select(field string, fields ...string) *NodeUpdateOne // Save executes the query and returns the updated Node entity. func (nuo *NodeUpdateOne) Save(ctx context.Context) (*Node, error) { - var ( - err error - node *Node - ) - if len(nuo.hooks) == 0 { - node, err = nuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*NodeMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - nuo.mutation = mutation - node, err = nuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(nuo.hooks) - 1; i >= 0; i-- { - if nuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = nuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, nuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Node) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from NodeMutation", v) - } - node = nv - } - return node, err + return withHooks[*Node, NodeMutation](ctx, nuo.sqlSave, nuo.mutation, nuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -523,5 +464,6 @@ func (nuo *NodeUpdateOne) sqlSave(ctx context.Context) (_node *Node, err error) } return nil, err } + nuo.mutation.done = true return _node, nil } diff --git a/examples/privacyadmin/ent/ent.go b/examples/privacyadmin/ent/ent.go index 8ad51bbd7..aea7ed92d 100644 --- a/examples/privacyadmin/ent/ent.go +++ b/examples/privacyadmin/ent/ent.go @@ -473,6 +473,40 @@ func (s *selector) BoolX(ctx context.Context) bool { return v } +// withHooks invokes the builder operation with the given hooks, if any. +func withHooks[V Value, M any, PM interface { + *M + Mutation +}](ctx context.Context, exec func(context.Context) (V, error), mutation PM, hooks []Hook) (value V, err error) { + if len(hooks) == 0 { + return exec(ctx) + } + var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { + mutationT, ok := m.(PM) + if !ok { + return nil, fmt.Errorf("unexpected mutation type %T", m) + } + // Set the mutation to the builder. + *mutation = *mutationT + return exec(ctx) + }) + for i := len(hooks) - 1; i >= 0; i-- { + if hooks[i] == nil { + return value, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") + } + mut = hooks[i](mut) + } + v, err := mut.Mutate(ctx, mutation) + if err != nil { + return value, err + } + nv, ok := v.(V) + if !ok { + return value, fmt.Errorf("unexpected node type %T returned from %T", v, mutation) + } + return nv, nil +} + // newQueryContext returns a new context with the given QueryContext attached in case it does not exist. func newQueryContext(ctx context.Context, typ, op string) context.Context { if ent.QueryFromContext(ctx) == nil { diff --git a/examples/privacyadmin/ent/user_create.go b/examples/privacyadmin/ent/user_create.go index 86a220d09..cfbd20bad 100644 --- a/examples/privacyadmin/ent/user_create.go +++ b/examples/privacyadmin/ent/user_create.go @@ -44,52 +44,10 @@ func (uc *UserCreate) Mutation() *UserMutation { // Save creates the User in the database. func (uc *UserCreate) Save(ctx context.Context) (*User, error) { - var ( - err error - node *User - ) if err := uc.defaults(); err != nil { return nil, err } - if len(uc.hooks) == 0 { - if err = uc.check(); err != nil { - return nil, err - } - node, err = uc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = uc.check(); err != nil { - return nil, err - } - uc.mutation = mutation - if node, err = uc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(uc.hooks) - 1; i >= 0; i-- { - if uc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*User) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) - } - node = nv - } - return node, err + return withHooks[*User, UserMutation](ctx, uc.sqlSave, uc.mutation, uc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -132,6 +90,9 @@ func (uc *UserCreate) check() error { } func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { + if err := uc.check(); err != nil { + return nil, err + } _node, _spec := uc.createSpec() if err := sqlgraph.CreateNode(ctx, uc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -141,6 +102,8 @@ func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + uc.mutation.id = &_node.ID + uc.mutation.done = true return _node, nil } diff --git a/examples/privacyadmin/ent/user_delete.go b/examples/privacyadmin/ent/user_delete.go index dc30ad3ac..7d6db9d27 100644 --- a/examples/privacyadmin/ent/user_delete.go +++ b/examples/privacyadmin/ent/user_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (ud *UserDelete) Where(ps ...predicate.User) *UserDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (ud *UserDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(ud.hooks) == 0 { - affected, err = ud.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - ud.mutation = mutation - affected, err = ud.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(ud.hooks) - 1; i >= 0; i-- { - if ud.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ud.hooks[i](mut) - } - n, err := mut.Mutate(ctx, ud.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, UserMutation](ctx, ud.sqlExec, ud.mutation, ud.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (ud *UserDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + ud.mutation.done = true return affected, err } diff --git a/examples/privacyadmin/ent/user_update.go b/examples/privacyadmin/ent/user_update.go index ab9acbe30..f6cc374be 100644 --- a/examples/privacyadmin/ent/user_update.go +++ b/examples/privacyadmin/ent/user_update.go @@ -52,34 +52,7 @@ func (uu *UserUpdate) Mutation() *UserMutation { // Save executes the query and returns the number of nodes affected by the update operation. func (uu *UserUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(uu.hooks) == 0 { - affected, err = uu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - uu.mutation = mutation - affected, err = uu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(uu.hooks) - 1; i >= 0; i-- { - if uu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, uu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, UserMutation](ctx, uu.sqlSave, uu.mutation, uu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -133,6 +106,7 @@ func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + uu.mutation.done = true return n, nil } @@ -172,40 +146,7 @@ func (uuo *UserUpdateOne) Select(field string, fields ...string) *UserUpdateOne // Save executes the query and returns the updated User entity. func (uuo *UserUpdateOne) Save(ctx context.Context) (*User, error) { - var ( - err error - node *User - ) - if len(uuo.hooks) == 0 { - node, err = uuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - uuo.mutation = mutation - node, err = uuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(uuo.hooks) - 1; i >= 0; i-- { - if uuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*User) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) - } - node = nv - } - return node, err + return withHooks[*User, UserMutation](ctx, uuo.sqlSave, uuo.mutation, uuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -279,5 +220,6 @@ func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) } return nil, err } + uuo.mutation.done = true return _node, nil } diff --git a/examples/privacytenant/ent/ent.go b/examples/privacytenant/ent/ent.go index adeb01de2..6d3cdbbcd 100644 --- a/examples/privacytenant/ent/ent.go +++ b/examples/privacytenant/ent/ent.go @@ -477,6 +477,40 @@ func (s *selector) BoolX(ctx context.Context) bool { return v } +// withHooks invokes the builder operation with the given hooks, if any. +func withHooks[V Value, M any, PM interface { + *M + Mutation +}](ctx context.Context, exec func(context.Context) (V, error), mutation PM, hooks []Hook) (value V, err error) { + if len(hooks) == 0 { + return exec(ctx) + } + var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { + mutationT, ok := m.(PM) + if !ok { + return nil, fmt.Errorf("unexpected mutation type %T", m) + } + // Set the mutation to the builder. + *mutation = *mutationT + return exec(ctx) + }) + for i := len(hooks) - 1; i >= 0; i-- { + if hooks[i] == nil { + return value, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") + } + mut = hooks[i](mut) + } + v, err := mut.Mutate(ctx, mutation) + if err != nil { + return value, err + } + nv, ok := v.(V) + if !ok { + return value, fmt.Errorf("unexpected node type %T returned from %T", v, mutation) + } + return nv, nil +} + // newQueryContext returns a new context with the given QueryContext attached in case it does not exist. func newQueryContext(ctx context.Context, typ, op string) context.Context { if ent.QueryFromContext(ctx) == nil { diff --git a/examples/privacytenant/ent/group_create.go b/examples/privacytenant/ent/group_create.go index be7d3a140..95cc30e8a 100644 --- a/examples/privacytenant/ent/group_create.go +++ b/examples/privacytenant/ent/group_create.go @@ -72,52 +72,10 @@ func (gc *GroupCreate) Mutation() *GroupMutation { // Save creates the Group in the database. func (gc *GroupCreate) Save(ctx context.Context) (*Group, error) { - var ( - err error - node *Group - ) if err := gc.defaults(); err != nil { return nil, err } - if len(gc.hooks) == 0 { - if err = gc.check(); err != nil { - return nil, err - } - node, err = gc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GroupMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = gc.check(); err != nil { - return nil, err - } - gc.mutation = mutation - if node, err = gc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(gc.hooks) - 1; i >= 0; i-- { - if gc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = gc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, gc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Group) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from GroupMutation", v) - } - node = nv - } - return node, err + return withHooks[*Group, GroupMutation](ctx, gc.sqlSave, gc.mutation, gc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -166,6 +124,9 @@ func (gc *GroupCreate) check() error { } func (gc *GroupCreate) sqlSave(ctx context.Context) (*Group, error) { + if err := gc.check(); err != nil { + return nil, err + } _node, _spec := gc.createSpec() if err := sqlgraph.CreateNode(ctx, gc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -175,6 +136,8 @@ func (gc *GroupCreate) sqlSave(ctx context.Context) (*Group, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + gc.mutation.id = &_node.ID + gc.mutation.done = true return _node, nil } diff --git a/examples/privacytenant/ent/group_delete.go b/examples/privacytenant/ent/group_delete.go index f77f1e55b..5dda3f3e8 100644 --- a/examples/privacytenant/ent/group_delete.go +++ b/examples/privacytenant/ent/group_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (gd *GroupDelete) Where(ps ...predicate.Group) *GroupDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (gd *GroupDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(gd.hooks) == 0 { - affected, err = gd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GroupMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - gd.mutation = mutation - affected, err = gd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(gd.hooks) - 1; i >= 0; i-- { - if gd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = gd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, gd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, GroupMutation](ctx, gd.sqlExec, gd.mutation, gd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (gd *GroupDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + gd.mutation.done = true return affected, err } diff --git a/examples/privacytenant/ent/group_update.go b/examples/privacytenant/ent/group_update.go index a6c4c931e..92760b578 100644 --- a/examples/privacytenant/ent/group_update.go +++ b/examples/privacytenant/ent/group_update.go @@ -89,40 +89,7 @@ func (gu *GroupUpdate) RemoveUsers(u ...*User) *GroupUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (gu *GroupUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(gu.hooks) == 0 { - if err = gu.check(); err != nil { - return 0, err - } - affected, err = gu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GroupMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = gu.check(); err != nil { - return 0, err - } - gu.mutation = mutation - affected, err = gu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(gu.hooks) - 1; i >= 0; i-- { - if gu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = gu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, gu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, GroupMutation](ctx, gu.sqlSave, gu.mutation, gu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -156,6 +123,9 @@ func (gu *GroupUpdate) check() error { } func (gu *GroupUpdate) sqlSave(ctx context.Context) (n int, err error) { + if err := gu.check(); err != nil { + return n, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: group.Table, @@ -238,6 +208,7 @@ func (gu *GroupUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + gu.mutation.done = true return n, nil } @@ -313,46 +284,7 @@ func (guo *GroupUpdateOne) Select(field string, fields ...string) *GroupUpdateOn // Save executes the query and returns the updated Group entity. func (guo *GroupUpdateOne) Save(ctx context.Context) (*Group, error) { - var ( - err error - node *Group - ) - if len(guo.hooks) == 0 { - if err = guo.check(); err != nil { - return nil, err - } - node, err = guo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GroupMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = guo.check(); err != nil { - return nil, err - } - guo.mutation = mutation - node, err = guo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(guo.hooks) - 1; i >= 0; i-- { - if guo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = guo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, guo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Group) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from GroupMutation", v) - } - node = nv - } - return node, err + return withHooks[*Group, GroupMutation](ctx, guo.sqlSave, guo.mutation, guo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -386,6 +318,9 @@ func (guo *GroupUpdateOne) check() error { } func (guo *GroupUpdateOne) sqlSave(ctx context.Context) (_node *Group, err error) { + if err := guo.check(); err != nil { + return _node, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: group.Table, @@ -488,5 +423,6 @@ func (guo *GroupUpdateOne) sqlSave(ctx context.Context) (_node *Group, err error } return nil, err } + guo.mutation.done = true return _node, nil } diff --git a/examples/privacytenant/ent/tenant_create.go b/examples/privacytenant/ent/tenant_create.go index 0b0f6629b..8930fabb1 100644 --- a/examples/privacytenant/ent/tenant_create.go +++ b/examples/privacytenant/ent/tenant_create.go @@ -36,49 +36,7 @@ func (tc *TenantCreate) Mutation() *TenantMutation { // Save creates the Tenant in the database. func (tc *TenantCreate) Save(ctx context.Context) (*Tenant, error) { - var ( - err error - node *Tenant - ) - if len(tc.hooks) == 0 { - if err = tc.check(); err != nil { - return nil, err - } - node, err = tc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*TenantMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = tc.check(); err != nil { - return nil, err - } - tc.mutation = mutation - if node, err = tc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(tc.hooks) - 1; i >= 0; i-- { - if tc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = tc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, tc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Tenant) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from TenantMutation", v) - } - node = nv - } - return node, err + return withHooks[*Tenant, TenantMutation](ctx, tc.sqlSave, tc.mutation, tc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -117,6 +75,9 @@ func (tc *TenantCreate) check() error { } func (tc *TenantCreate) sqlSave(ctx context.Context) (*Tenant, error) { + if err := tc.check(); err != nil { + return nil, err + } _node, _spec := tc.createSpec() if err := sqlgraph.CreateNode(ctx, tc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -126,6 +87,8 @@ func (tc *TenantCreate) sqlSave(ctx context.Context) (*Tenant, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + tc.mutation.id = &_node.ID + tc.mutation.done = true return _node, nil } diff --git a/examples/privacytenant/ent/tenant_delete.go b/examples/privacytenant/ent/tenant_delete.go index f3f28c791..625412978 100644 --- a/examples/privacytenant/ent/tenant_delete.go +++ b/examples/privacytenant/ent/tenant_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (td *TenantDelete) Where(ps ...predicate.Tenant) *TenantDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (td *TenantDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(td.hooks) == 0 { - affected, err = td.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*TenantMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - td.mutation = mutation - affected, err = td.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(td.hooks) - 1; i >= 0; i-- { - if td.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = td.hooks[i](mut) - } - n, err := mut.Mutate(ctx, td.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, TenantMutation](ctx, td.sqlExec, td.mutation, td.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (td *TenantDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + td.mutation.done = true return affected, err } diff --git a/examples/privacytenant/ent/tenant_update.go b/examples/privacytenant/ent/tenant_update.go index 68a3a2f2e..e95cafa9f 100644 --- a/examples/privacytenant/ent/tenant_update.go +++ b/examples/privacytenant/ent/tenant_update.go @@ -44,40 +44,7 @@ func (tu *TenantUpdate) Mutation() *TenantMutation { // Save executes the query and returns the number of nodes affected by the update operation. func (tu *TenantUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(tu.hooks) == 0 { - if err = tu.check(); err != nil { - return 0, err - } - affected, err = tu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*TenantMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = tu.check(); err != nil { - return 0, err - } - tu.mutation = mutation - affected, err = tu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(tu.hooks) - 1; i >= 0; i-- { - if tu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = tu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, tu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, TenantMutation](ctx, tu.sqlSave, tu.mutation, tu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -113,6 +80,9 @@ func (tu *TenantUpdate) check() error { } func (tu *TenantUpdate) sqlSave(ctx context.Context) (n int, err error) { + if err := tu.check(); err != nil { + return n, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: tenant.Table, @@ -141,6 +111,7 @@ func (tu *TenantUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + tu.mutation.done = true return n, nil } @@ -172,46 +143,7 @@ func (tuo *TenantUpdateOne) Select(field string, fields ...string) *TenantUpdate // Save executes the query and returns the updated Tenant entity. func (tuo *TenantUpdateOne) Save(ctx context.Context) (*Tenant, error) { - var ( - err error - node *Tenant - ) - if len(tuo.hooks) == 0 { - if err = tuo.check(); err != nil { - return nil, err - } - node, err = tuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*TenantMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = tuo.check(); err != nil { - return nil, err - } - tuo.mutation = mutation - node, err = tuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(tuo.hooks) - 1; i >= 0; i-- { - if tuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = tuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, tuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Tenant) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from TenantMutation", v) - } - node = nv - } - return node, err + return withHooks[*Tenant, TenantMutation](ctx, tuo.sqlSave, tuo.mutation, tuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -247,6 +179,9 @@ func (tuo *TenantUpdateOne) check() error { } func (tuo *TenantUpdateOne) sqlSave(ctx context.Context) (_node *Tenant, err error) { + if err := tuo.check(); err != nil { + return _node, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: tenant.Table, @@ -295,5 +230,6 @@ func (tuo *TenantUpdateOne) sqlSave(ctx context.Context) (_node *Tenant, err err } return nil, err } + tuo.mutation.done = true return _node, nil } diff --git a/examples/privacytenant/ent/user_create.go b/examples/privacytenant/ent/user_create.go index 88a5b4739..d29c6fe0b 100644 --- a/examples/privacytenant/ent/user_create.go +++ b/examples/privacytenant/ent/user_create.go @@ -78,52 +78,10 @@ func (uc *UserCreate) Mutation() *UserMutation { // Save creates the User in the database. func (uc *UserCreate) Save(ctx context.Context) (*User, error) { - var ( - err error - node *User - ) if err := uc.defaults(); err != nil { return nil, err } - if len(uc.hooks) == 0 { - if err = uc.check(); err != nil { - return nil, err - } - node, err = uc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = uc.check(); err != nil { - return nil, err - } - uc.mutation = mutation - if node, err = uc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(uc.hooks) - 1; i >= 0; i-- { - if uc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*User) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) - } - node = nv - } - return node, err + return withHooks[*User, UserMutation](ctx, uc.sqlSave, uc.mutation, uc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -172,6 +130,9 @@ func (uc *UserCreate) check() error { } func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { + if err := uc.check(); err != nil { + return nil, err + } _node, _spec := uc.createSpec() if err := sqlgraph.CreateNode(ctx, uc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -181,6 +142,8 @@ func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + uc.mutation.id = &_node.ID + uc.mutation.done = true return _node, nil } diff --git a/examples/privacytenant/ent/user_delete.go b/examples/privacytenant/ent/user_delete.go index 4d4e236f1..f28a5b0b7 100644 --- a/examples/privacytenant/ent/user_delete.go +++ b/examples/privacytenant/ent/user_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (ud *UserDelete) Where(ps ...predicate.User) *UserDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (ud *UserDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(ud.hooks) == 0 { - affected, err = ud.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - ud.mutation = mutation - affected, err = ud.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(ud.hooks) - 1; i >= 0; i-- { - if ud.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ud.hooks[i](mut) - } - n, err := mut.Mutate(ctx, ud.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, UserMutation](ctx, ud.sqlExec, ud.mutation, ud.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (ud *UserDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + ud.mutation.done = true return affected, err } diff --git a/examples/privacytenant/ent/user_update.go b/examples/privacytenant/ent/user_update.go index 9a9c7744a..5cd73a432 100644 --- a/examples/privacytenant/ent/user_update.go +++ b/examples/privacytenant/ent/user_update.go @@ -108,40 +108,7 @@ func (uu *UserUpdate) RemoveGroups(g ...*Group) *UserUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (uu *UserUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(uu.hooks) == 0 { - if err = uu.check(); err != nil { - return 0, err - } - affected, err = uu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = uu.check(); err != nil { - return 0, err - } - uu.mutation = mutation - affected, err = uu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(uu.hooks) - 1; i >= 0; i-- { - if uu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, uu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, UserMutation](ctx, uu.sqlSave, uu.mutation, uu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -175,6 +142,9 @@ func (uu *UserUpdate) check() error { } func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) { + if err := uu.check(); err != nil { + return n, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: user.Table, @@ -268,6 +238,7 @@ func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + uu.mutation.done = true return n, nil } @@ -361,46 +332,7 @@ func (uuo *UserUpdateOne) Select(field string, fields ...string) *UserUpdateOne // Save executes the query and returns the updated User entity. func (uuo *UserUpdateOne) Save(ctx context.Context) (*User, error) { - var ( - err error - node *User - ) - if len(uuo.hooks) == 0 { - if err = uuo.check(); err != nil { - return nil, err - } - node, err = uuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = uuo.check(); err != nil { - return nil, err - } - uuo.mutation = mutation - node, err = uuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(uuo.hooks) - 1; i >= 0; i-- { - if uuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*User) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) - } - node = nv - } - return node, err + return withHooks[*User, UserMutation](ctx, uuo.sqlSave, uuo.mutation, uuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -434,6 +366,9 @@ func (uuo *UserUpdateOne) check() error { } func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) { + if err := uuo.check(); err != nil { + return _node, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: user.Table, @@ -547,5 +482,6 @@ func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) } return nil, err } + uuo.mutation.done = true return _node, nil } diff --git a/examples/start/ent/car_create.go b/examples/start/ent/car_create.go index 5c64a3d1c..a2ce1615f 100644 --- a/examples/start/ent/car_create.go +++ b/examples/start/ent/car_create.go @@ -63,49 +63,7 @@ func (cc *CarCreate) Mutation() *CarMutation { // Save creates the Car in the database. func (cc *CarCreate) Save(ctx context.Context) (*Car, error) { - var ( - err error - node *Car - ) - if len(cc.hooks) == 0 { - if err = cc.check(); err != nil { - return nil, err - } - node, err = cc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CarMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = cc.check(); err != nil { - return nil, err - } - cc.mutation = mutation - if node, err = cc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(cc.hooks) - 1; i >= 0; i-- { - if cc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = cc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, cc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Car) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from CarMutation", v) - } - node = nv - } - return node, err + return withHooks[*Car, CarMutation](ctx, cc.sqlSave, cc.mutation, cc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -142,6 +100,9 @@ func (cc *CarCreate) check() error { } func (cc *CarCreate) sqlSave(ctx context.Context) (*Car, error) { + if err := cc.check(); err != nil { + return nil, err + } _node, _spec := cc.createSpec() if err := sqlgraph.CreateNode(ctx, cc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -151,6 +112,8 @@ func (cc *CarCreate) sqlSave(ctx context.Context) (*Car, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + cc.mutation.id = &_node.ID + cc.mutation.done = true return _node, nil } diff --git a/examples/start/ent/car_delete.go b/examples/start/ent/car_delete.go index 8a3843391..cedcabf30 100644 --- a/examples/start/ent/car_delete.go +++ b/examples/start/ent/car_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (cd *CarDelete) Where(ps ...predicate.Car) *CarDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (cd *CarDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(cd.hooks) == 0 { - affected, err = cd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CarMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - cd.mutation = mutation - affected, err = cd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(cd.hooks) - 1; i >= 0; i-- { - if cd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = cd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, cd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, CarMutation](ctx, cd.sqlExec, cd.mutation, cd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (cd *CarDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + cd.mutation.done = true return affected, err } diff --git a/examples/start/ent/car_update.go b/examples/start/ent/car_update.go index b95a4dec3..8e59f10ec 100644 --- a/examples/start/ent/car_update.go +++ b/examples/start/ent/car_update.go @@ -77,34 +77,7 @@ func (cu *CarUpdate) ClearOwner() *CarUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (cu *CarUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(cu.hooks) == 0 { - affected, err = cu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CarMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - cu.mutation = mutation - affected, err = cu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(cu.hooks) - 1; i >= 0; i-- { - if cu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = cu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, cu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, CarMutation](ctx, cu.sqlSave, cu.mutation, cu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -196,6 +169,7 @@ func (cu *CarUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + cu.mutation.done = true return n, nil } @@ -258,40 +232,7 @@ func (cuo *CarUpdateOne) Select(field string, fields ...string) *CarUpdateOne { // Save executes the query and returns the updated Car entity. func (cuo *CarUpdateOne) Save(ctx context.Context) (*Car, error) { - var ( - err error - node *Car - ) - if len(cuo.hooks) == 0 { - node, err = cuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*CarMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - cuo.mutation = mutation - node, err = cuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(cuo.hooks) - 1; i >= 0; i-- { - if cuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = cuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, cuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Car) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from CarMutation", v) - } - node = nv - } - return node, err + return withHooks[*Car, CarMutation](ctx, cuo.sqlSave, cuo.mutation, cuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -403,5 +344,6 @@ func (cuo *CarUpdateOne) sqlSave(ctx context.Context) (_node *Car, err error) { } return nil, err } + cuo.mutation.done = true return _node, nil } diff --git a/examples/start/ent/ent.go b/examples/start/ent/ent.go index c965fec65..9a3dc583d 100644 --- a/examples/start/ent/ent.go +++ b/examples/start/ent/ent.go @@ -477,6 +477,40 @@ func (s *selector) BoolX(ctx context.Context) bool { return v } +// withHooks invokes the builder operation with the given hooks, if any. +func withHooks[V Value, M any, PM interface { + *M + Mutation +}](ctx context.Context, exec func(context.Context) (V, error), mutation PM, hooks []Hook) (value V, err error) { + if len(hooks) == 0 { + return exec(ctx) + } + var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { + mutationT, ok := m.(PM) + if !ok { + return nil, fmt.Errorf("unexpected mutation type %T", m) + } + // Set the mutation to the builder. + *mutation = *mutationT + return exec(ctx) + }) + for i := len(hooks) - 1; i >= 0; i-- { + if hooks[i] == nil { + return value, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") + } + mut = hooks[i](mut) + } + v, err := mut.Mutate(ctx, mutation) + if err != nil { + return value, err + } + nv, ok := v.(V) + if !ok { + return value, fmt.Errorf("unexpected node type %T returned from %T", v, mutation) + } + return nv, nil +} + // newQueryContext returns a new context with the given QueryContext attached in case it does not exist. func newQueryContext(ctx context.Context, typ, op string) context.Context { if ent.QueryFromContext(ctx) == nil { diff --git a/examples/start/ent/group_create.go b/examples/start/ent/group_create.go index 15377ef8d..4b940c444 100644 --- a/examples/start/ent/group_create.go +++ b/examples/start/ent/group_create.go @@ -52,49 +52,7 @@ func (gc *GroupCreate) Mutation() *GroupMutation { // Save creates the Group in the database. func (gc *GroupCreate) Save(ctx context.Context) (*Group, error) { - var ( - err error - node *Group - ) - if len(gc.hooks) == 0 { - if err = gc.check(); err != nil { - return nil, err - } - node, err = gc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GroupMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = gc.check(); err != nil { - return nil, err - } - gc.mutation = mutation - if node, err = gc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(gc.hooks) - 1; i >= 0; i-- { - if gc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = gc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, gc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Group) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from GroupMutation", v) - } - node = nv - } - return node, err + return withHooks[*Group, GroupMutation](ctx, gc.sqlSave, gc.mutation, gc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -133,6 +91,9 @@ func (gc *GroupCreate) check() error { } func (gc *GroupCreate) sqlSave(ctx context.Context) (*Group, error) { + if err := gc.check(); err != nil { + return nil, err + } _node, _spec := gc.createSpec() if err := sqlgraph.CreateNode(ctx, gc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -142,6 +103,8 @@ func (gc *GroupCreate) sqlSave(ctx context.Context) (*Group, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + gc.mutation.id = &_node.ID + gc.mutation.done = true return _node, nil } diff --git a/examples/start/ent/group_delete.go b/examples/start/ent/group_delete.go index f66371184..104a860e4 100644 --- a/examples/start/ent/group_delete.go +++ b/examples/start/ent/group_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (gd *GroupDelete) Where(ps ...predicate.Group) *GroupDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (gd *GroupDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(gd.hooks) == 0 { - affected, err = gd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GroupMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - gd.mutation = mutation - affected, err = gd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(gd.hooks) - 1; i >= 0; i-- { - if gd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = gd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, gd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, GroupMutation](ctx, gd.sqlExec, gd.mutation, gd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (gd *GroupDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + gd.mutation.done = true return affected, err } diff --git a/examples/start/ent/group_update.go b/examples/start/ent/group_update.go index 24bee1b6a..328902dd6 100644 --- a/examples/start/ent/group_update.go +++ b/examples/start/ent/group_update.go @@ -81,40 +81,7 @@ func (gu *GroupUpdate) RemoveUsers(u ...*User) *GroupUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (gu *GroupUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(gu.hooks) == 0 { - if err = gu.check(); err != nil { - return 0, err - } - affected, err = gu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GroupMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = gu.check(); err != nil { - return 0, err - } - gu.mutation = mutation - affected, err = gu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(gu.hooks) - 1; i >= 0; i-- { - if gu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = gu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, gu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, GroupMutation](ctx, gu.sqlSave, gu.mutation, gu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -150,6 +117,9 @@ func (gu *GroupUpdate) check() error { } func (gu *GroupUpdate) sqlSave(ctx context.Context) (n int, err error) { + if err := gu.check(); err != nil { + return n, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: group.Table, @@ -232,6 +202,7 @@ func (gu *GroupUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + gu.mutation.done = true return n, nil } @@ -299,46 +270,7 @@ func (guo *GroupUpdateOne) Select(field string, fields ...string) *GroupUpdateOn // Save executes the query and returns the updated Group entity. func (guo *GroupUpdateOne) Save(ctx context.Context) (*Group, error) { - var ( - err error - node *Group - ) - if len(guo.hooks) == 0 { - if err = guo.check(); err != nil { - return nil, err - } - node, err = guo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GroupMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = guo.check(); err != nil { - return nil, err - } - guo.mutation = mutation - node, err = guo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(guo.hooks) - 1; i >= 0; i-- { - if guo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = guo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, guo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Group) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from GroupMutation", v) - } - node = nv - } - return node, err + return withHooks[*Group, GroupMutation](ctx, guo.sqlSave, guo.mutation, guo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -374,6 +306,9 @@ func (guo *GroupUpdateOne) check() error { } func (guo *GroupUpdateOne) sqlSave(ctx context.Context) (_node *Group, err error) { + if err := guo.check(); err != nil { + return _node, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: group.Table, @@ -476,5 +411,6 @@ func (guo *GroupUpdateOne) sqlSave(ctx context.Context) (_node *Group, err error } return nil, err } + guo.mutation.done = true return _node, nil } diff --git a/examples/start/ent/user_create.go b/examples/start/ent/user_create.go index e556d6c9b..a7d2bb69c 100644 --- a/examples/start/ent/user_create.go +++ b/examples/start/ent/user_create.go @@ -82,50 +82,8 @@ func (uc *UserCreate) Mutation() *UserMutation { // Save creates the User in the database. func (uc *UserCreate) Save(ctx context.Context) (*User, error) { - var ( - err error - node *User - ) uc.defaults() - if len(uc.hooks) == 0 { - if err = uc.check(); err != nil { - return nil, err - } - node, err = uc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = uc.check(); err != nil { - return nil, err - } - uc.mutation = mutation - if node, err = uc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(uc.hooks) - 1; i >= 0; i-- { - if uc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*User) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) - } - node = nv - } - return node, err + return withHooks[*User, UserMutation](ctx, uc.sqlSave, uc.mutation, uc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -175,6 +133,9 @@ func (uc *UserCreate) check() error { } func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { + if err := uc.check(); err != nil { + return nil, err + } _node, _spec := uc.createSpec() if err := sqlgraph.CreateNode(ctx, uc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -184,6 +145,8 @@ func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + uc.mutation.id = &_node.ID + uc.mutation.done = true return _node, nil } diff --git a/examples/start/ent/user_delete.go b/examples/start/ent/user_delete.go index 39ab739bf..b50135bba 100644 --- a/examples/start/ent/user_delete.go +++ b/examples/start/ent/user_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (ud *UserDelete) Where(ps ...predicate.User) *UserDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (ud *UserDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(ud.hooks) == 0 { - affected, err = ud.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - ud.mutation = mutation - affected, err = ud.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(ud.hooks) - 1; i >= 0; i-- { - if ud.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ud.hooks[i](mut) - } - n, err := mut.Mutate(ctx, ud.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, UserMutation](ctx, ud.sqlExec, ud.mutation, ud.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (ud *UserDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + ud.mutation.done = true return affected, err } diff --git a/examples/start/ent/user_update.go b/examples/start/ent/user_update.go index 0be0201d4..3dd9a4919 100644 --- a/examples/start/ent/user_update.go +++ b/examples/start/ent/user_update.go @@ -139,40 +139,7 @@ func (uu *UserUpdate) RemoveGroups(g ...*Group) *UserUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (uu *UserUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(uu.hooks) == 0 { - if err = uu.check(); err != nil { - return 0, err - } - affected, err = uu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = uu.check(); err != nil { - return 0, err - } - uu.mutation = mutation - affected, err = uu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(uu.hooks) - 1; i >= 0; i-- { - if uu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, uu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, UserMutation](ctx, uu.sqlSave, uu.mutation, uu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -208,6 +175,9 @@ func (uu *UserUpdate) check() error { } func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) { + if err := uu.check(); err != nil { + return n, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: user.Table, @@ -350,6 +320,7 @@ func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + uu.mutation.done = true return n, nil } @@ -474,46 +445,7 @@ func (uuo *UserUpdateOne) Select(field string, fields ...string) *UserUpdateOne // Save executes the query and returns the updated User entity. func (uuo *UserUpdateOne) Save(ctx context.Context) (*User, error) { - var ( - err error - node *User - ) - if len(uuo.hooks) == 0 { - if err = uuo.check(); err != nil { - return nil, err - } - node, err = uuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = uuo.check(); err != nil { - return nil, err - } - uuo.mutation = mutation - node, err = uuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(uuo.hooks) - 1; i >= 0; i-- { - if uuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*User) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) - } - node = nv - } - return node, err + return withHooks[*User, UserMutation](ctx, uuo.sqlSave, uuo.mutation, uuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -549,6 +481,9 @@ func (uuo *UserUpdateOne) check() error { } func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) { + if err := uuo.check(); err != nil { + return _node, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: user.Table, @@ -711,5 +646,6 @@ func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) } return nil, err } + uuo.mutation.done = true return _node, nil } diff --git a/examples/traversal/ent/ent.go b/examples/traversal/ent/ent.go index 63720deb3..730667132 100644 --- a/examples/traversal/ent/ent.go +++ b/examples/traversal/ent/ent.go @@ -477,6 +477,40 @@ func (s *selector) BoolX(ctx context.Context) bool { return v } +// withHooks invokes the builder operation with the given hooks, if any. +func withHooks[V Value, M any, PM interface { + *M + Mutation +}](ctx context.Context, exec func(context.Context) (V, error), mutation PM, hooks []Hook) (value V, err error) { + if len(hooks) == 0 { + return exec(ctx) + } + var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { + mutationT, ok := m.(PM) + if !ok { + return nil, fmt.Errorf("unexpected mutation type %T", m) + } + // Set the mutation to the builder. + *mutation = *mutationT + return exec(ctx) + }) + for i := len(hooks) - 1; i >= 0; i-- { + if hooks[i] == nil { + return value, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") + } + mut = hooks[i](mut) + } + v, err := mut.Mutate(ctx, mutation) + if err != nil { + return value, err + } + nv, ok := v.(V) + if !ok { + return value, fmt.Errorf("unexpected node type %T returned from %T", v, mutation) + } + return nv, nil +} + // newQueryContext returns a new context with the given QueryContext attached in case it does not exist. func newQueryContext(ctx context.Context, typ, op string) context.Context { if ent.QueryFromContext(ctx) == nil { diff --git a/examples/traversal/ent/group_create.go b/examples/traversal/ent/group_create.go index b28fd6e89..60b6d8435 100644 --- a/examples/traversal/ent/group_create.go +++ b/examples/traversal/ent/group_create.go @@ -71,49 +71,7 @@ func (gc *GroupCreate) Mutation() *GroupMutation { // Save creates the Group in the database. func (gc *GroupCreate) Save(ctx context.Context) (*Group, error) { - var ( - err error - node *Group - ) - if len(gc.hooks) == 0 { - if err = gc.check(); err != nil { - return nil, err - } - node, err = gc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GroupMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = gc.check(); err != nil { - return nil, err - } - gc.mutation = mutation - if node, err = gc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(gc.hooks) - 1; i >= 0; i-- { - if gc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = gc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, gc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Group) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from GroupMutation", v) - } - node = nv - } - return node, err + return withHooks[*Group, GroupMutation](ctx, gc.sqlSave, gc.mutation, gc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -147,6 +105,9 @@ func (gc *GroupCreate) check() error { } func (gc *GroupCreate) sqlSave(ctx context.Context) (*Group, error) { + if err := gc.check(); err != nil { + return nil, err + } _node, _spec := gc.createSpec() if err := sqlgraph.CreateNode(ctx, gc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -156,6 +117,8 @@ func (gc *GroupCreate) sqlSave(ctx context.Context) (*Group, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + gc.mutation.id = &_node.ID + gc.mutation.done = true return _node, nil } diff --git a/examples/traversal/ent/group_delete.go b/examples/traversal/ent/group_delete.go index e1da39198..e607a1916 100644 --- a/examples/traversal/ent/group_delete.go +++ b/examples/traversal/ent/group_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (gd *GroupDelete) Where(ps ...predicate.Group) *GroupDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (gd *GroupDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(gd.hooks) == 0 { - affected, err = gd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GroupMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - gd.mutation = mutation - affected, err = gd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(gd.hooks) - 1; i >= 0; i-- { - if gd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = gd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, gd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, GroupMutation](ctx, gd.sqlExec, gd.mutation, gd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (gd *GroupDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + gd.mutation.done = true return affected, err } diff --git a/examples/traversal/ent/group_update.go b/examples/traversal/ent/group_update.go index 741e40a1a..c065d9163 100644 --- a/examples/traversal/ent/group_update.go +++ b/examples/traversal/ent/group_update.go @@ -106,34 +106,7 @@ func (gu *GroupUpdate) ClearAdmin() *GroupUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (gu *GroupUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(gu.hooks) == 0 { - affected, err = gu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GroupMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - gu.mutation = mutation - affected, err = gu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(gu.hooks) - 1; i >= 0; i-- { - if gu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = gu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, gu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, GroupMutation](ctx, gu.sqlSave, gu.mutation, gu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -276,6 +249,7 @@ func (gu *GroupUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + gu.mutation.done = true return n, nil } @@ -368,40 +342,7 @@ func (guo *GroupUpdateOne) Select(field string, fields ...string) *GroupUpdateOn // Save executes the query and returns the updated Group entity. func (guo *GroupUpdateOne) Save(ctx context.Context) (*Group, error) { - var ( - err error - node *Group - ) - if len(guo.hooks) == 0 { - node, err = guo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*GroupMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - guo.mutation = mutation - node, err = guo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(guo.hooks) - 1; i >= 0; i-- { - if guo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = guo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, guo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Group) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from GroupMutation", v) - } - node = nv - } - return node, err + return withHooks[*Group, GroupMutation](ctx, guo.sqlSave, guo.mutation, guo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -564,5 +505,6 @@ func (guo *GroupUpdateOne) sqlSave(ctx context.Context) (_node *Group, err error } return nil, err } + guo.mutation.done = true return _node, nil } diff --git a/examples/traversal/ent/pet_create.go b/examples/traversal/ent/pet_create.go index e52a66a10..f2502d343 100644 --- a/examples/traversal/ent/pet_create.go +++ b/examples/traversal/ent/pet_create.go @@ -71,49 +71,7 @@ func (pc *PetCreate) Mutation() *PetMutation { // Save creates the Pet in the database. func (pc *PetCreate) Save(ctx context.Context) (*Pet, error) { - var ( - err error - node *Pet - ) - if len(pc.hooks) == 0 { - if err = pc.check(); err != nil { - return nil, err - } - node, err = pc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*PetMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = pc.check(); err != nil { - return nil, err - } - pc.mutation = mutation - if node, err = pc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(pc.hooks) - 1; i >= 0; i-- { - if pc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = pc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, pc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Pet) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from PetMutation", v) - } - node = nv - } - return node, err + return withHooks[*Pet, PetMutation](ctx, pc.sqlSave, pc.mutation, pc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -147,6 +105,9 @@ func (pc *PetCreate) check() error { } func (pc *PetCreate) sqlSave(ctx context.Context) (*Pet, error) { + if err := pc.check(); err != nil { + return nil, err + } _node, _spec := pc.createSpec() if err := sqlgraph.CreateNode(ctx, pc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -156,6 +117,8 @@ func (pc *PetCreate) sqlSave(ctx context.Context) (*Pet, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + pc.mutation.id = &_node.ID + pc.mutation.done = true return _node, nil } diff --git a/examples/traversal/ent/pet_delete.go b/examples/traversal/ent/pet_delete.go index 26e395d7b..460f243bc 100644 --- a/examples/traversal/ent/pet_delete.go +++ b/examples/traversal/ent/pet_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (pd *PetDelete) Where(ps ...predicate.Pet) *PetDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (pd *PetDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(pd.hooks) == 0 { - affected, err = pd.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*PetMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - pd.mutation = mutation - affected, err = pd.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(pd.hooks) - 1; i >= 0; i-- { - if pd.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = pd.hooks[i](mut) - } - n, err := mut.Mutate(ctx, pd.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, PetMutation](ctx, pd.sqlExec, pd.mutation, pd.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (pd *PetDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + pd.mutation.done = true return affected, err } diff --git a/examples/traversal/ent/pet_update.go b/examples/traversal/ent/pet_update.go index f6d88f2fb..f3fc234ca 100644 --- a/examples/traversal/ent/pet_update.go +++ b/examples/traversal/ent/pet_update.go @@ -106,34 +106,7 @@ func (pu *PetUpdate) ClearOwner() *PetUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (pu *PetUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(pu.hooks) == 0 { - affected, err = pu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*PetMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - pu.mutation = mutation - affected, err = pu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(pu.hooks) - 1; i >= 0; i-- { - if pu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = pu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, pu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, PetMutation](ctx, pu.sqlSave, pu.mutation, pu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -276,6 +249,7 @@ func (pu *PetUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + pu.mutation.done = true return n, nil } @@ -368,40 +342,7 @@ func (puo *PetUpdateOne) Select(field string, fields ...string) *PetUpdateOne { // Save executes the query and returns the updated Pet entity. func (puo *PetUpdateOne) Save(ctx context.Context) (*Pet, error) { - var ( - err error - node *Pet - ) - if len(puo.hooks) == 0 { - node, err = puo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*PetMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - puo.mutation = mutation - node, err = puo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(puo.hooks) - 1; i >= 0; i-- { - if puo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = puo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, puo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*Pet) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from PetMutation", v) - } - node = nv - } - return node, err + return withHooks[*Pet, PetMutation](ctx, puo.sqlSave, puo.mutation, puo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -564,5 +505,6 @@ func (puo *PetUpdateOne) sqlSave(ctx context.Context) (_node *Pet, err error) { } return nil, err } + puo.mutation.done = true return _node, nil } diff --git a/examples/traversal/ent/user_create.go b/examples/traversal/ent/user_create.go index 643137c4b..b5366b066 100644 --- a/examples/traversal/ent/user_create.go +++ b/examples/traversal/ent/user_create.go @@ -104,49 +104,7 @@ func (uc *UserCreate) Mutation() *UserMutation { // Save creates the User in the database. func (uc *UserCreate) Save(ctx context.Context) (*User, error) { - var ( - err error - node *User - ) - if len(uc.hooks) == 0 { - if err = uc.check(); err != nil { - return nil, err - } - node, err = uc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = uc.check(); err != nil { - return nil, err - } - uc.mutation = mutation - if node, err = uc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(uc.hooks) - 1; i >= 0; i-- { - if uc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*User) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) - } - node = nv - } - return node, err + return withHooks[*User, UserMutation](ctx, uc.sqlSave, uc.mutation, uc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -183,6 +141,9 @@ func (uc *UserCreate) check() error { } func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { + if err := uc.check(); err != nil { + return nil, err + } _node, _spec := uc.createSpec() if err := sqlgraph.CreateNode(ctx, uc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -192,6 +153,8 @@ func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + uc.mutation.id = &_node.ID + uc.mutation.done = true return _node, nil } diff --git a/examples/traversal/ent/user_delete.go b/examples/traversal/ent/user_delete.go index a74df2730..d09b7cdab 100644 --- a/examples/traversal/ent/user_delete.go +++ b/examples/traversal/ent/user_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (ud *UserDelete) Where(ps ...predicate.User) *UserDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (ud *UserDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(ud.hooks) == 0 { - affected, err = ud.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - ud.mutation = mutation - affected, err = ud.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(ud.hooks) - 1; i >= 0; i-- { - if ud.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ud.hooks[i](mut) - } - n, err := mut.Mutate(ctx, ud.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, UserMutation](ctx, ud.sqlExec, ud.mutation, ud.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (ud *UserDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + ud.mutation.done = true return affected, err } diff --git a/examples/traversal/ent/user_update.go b/examples/traversal/ent/user_update.go index bb9ebfc13..953f11ce0 100644 --- a/examples/traversal/ent/user_update.go +++ b/examples/traversal/ent/user_update.go @@ -203,34 +203,7 @@ func (uu *UserUpdate) RemoveManage(g ...*Group) *UserUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (uu *UserUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(uu.hooks) == 0 { - affected, err = uu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - uu.mutation = mutation - affected, err = uu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(uu.hooks) - 1; i >= 0; i-- { - if uu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, uu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, UserMutation](ctx, uu.sqlSave, uu.mutation, uu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -506,6 +479,7 @@ func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + uu.mutation.done = true return n, nil } @@ -694,40 +668,7 @@ func (uuo *UserUpdateOne) Select(field string, fields ...string) *UserUpdateOne // Save executes the query and returns the updated User entity. func (uuo *UserUpdateOne) Save(ctx context.Context) (*User, error) { - var ( - err error - node *User - ) - if len(uuo.hooks) == 0 { - node, err = uuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - uuo.mutation = mutation - node, err = uuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(uuo.hooks) - 1; i >= 0; i-- { - if uuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*User) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) - } - node = nv - } - return node, err + return withHooks[*User, UserMutation](ctx, uuo.sqlSave, uuo.mutation, uuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -1023,5 +964,6 @@ func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) } return nil, err } + uuo.mutation.done = true return _node, nil } diff --git a/examples/version/ent/ent.go b/examples/version/ent/ent.go index 083b4eb1f..cf7da9d08 100644 --- a/examples/version/ent/ent.go +++ b/examples/version/ent/ent.go @@ -473,6 +473,40 @@ func (s *selector) BoolX(ctx context.Context) bool { return v } +// withHooks invokes the builder operation with the given hooks, if any. +func withHooks[V Value, M any, PM interface { + *M + Mutation +}](ctx context.Context, exec func(context.Context) (V, error), mutation PM, hooks []Hook) (value V, err error) { + if len(hooks) == 0 { + return exec(ctx) + } + var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { + mutationT, ok := m.(PM) + if !ok { + return nil, fmt.Errorf("unexpected mutation type %T", m) + } + // Set the mutation to the builder. + *mutation = *mutationT + return exec(ctx) + }) + for i := len(hooks) - 1; i >= 0; i-- { + if hooks[i] == nil { + return value, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") + } + mut = hooks[i](mut) + } + v, err := mut.Mutate(ctx, mutation) + if err != nil { + return value, err + } + nv, ok := v.(V) + if !ok { + return value, fmt.Errorf("unexpected node type %T returned from %T", v, mutation) + } + return nv, nil +} + // newQueryContext returns a new context with the given QueryContext attached in case it does not exist. func newQueryContext(ctx context.Context, typ, op string) context.Context { if ent.QueryFromContext(ctx) == nil { diff --git a/examples/version/ent/user_create.go b/examples/version/ent/user_create.go index cd0c72854..2e9f89c97 100644 --- a/examples/version/ent/user_create.go +++ b/examples/version/ent/user_create.go @@ -50,50 +50,8 @@ func (uc *UserCreate) Mutation() *UserMutation { // Save creates the User in the database. func (uc *UserCreate) Save(ctx context.Context) (*User, error) { - var ( - err error - node *User - ) uc.defaults() - if len(uc.hooks) == 0 { - if err = uc.check(); err != nil { - return nil, err - } - node, err = uc.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = uc.check(); err != nil { - return nil, err - } - uc.mutation = mutation - if node, err = uc.sqlSave(ctx); err != nil { - return nil, err - } - mutation.id = &node.ID - mutation.done = true - return node, err - }) - for i := len(uc.hooks) - 1; i >= 0; i-- { - if uc.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uc.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uc.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*User) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) - } - node = nv - } - return node, err + return withHooks[*User, UserMutation](ctx, uc.sqlSave, uc.mutation, uc.hooks) } // SaveX calls Save and panics if Save returns an error. @@ -143,6 +101,9 @@ func (uc *UserCreate) check() error { } func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { + if err := uc.check(); err != nil { + return nil, err + } _node, _spec := uc.createSpec() if err := sqlgraph.CreateNode(ctx, uc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { @@ -152,6 +113,8 @@ func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { } id := _spec.ID.Value.(int64) _node.ID = int(id) + uc.mutation.id = &_node.ID + uc.mutation.done = true return _node, nil } diff --git a/examples/version/ent/user_delete.go b/examples/version/ent/user_delete.go index ae7cc0ac2..168bd2cca 100644 --- a/examples/version/ent/user_delete.go +++ b/examples/version/ent/user_delete.go @@ -8,7 +8,6 @@ package ent import ( "context" - "fmt" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -32,40 +31,7 @@ func (ud *UserDelete) Where(ps ...predicate.User) *UserDelete { // Exec executes the deletion query and returns how many vertices were deleted. func (ud *UserDelete) Exec(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(ud.hooks) == 0 { - affected, err = ud.sqlExec(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - ud.mutation = mutation - affected, err = ud.sqlExec(ctx) - mutation.done = true - return affected, err - }) - for i := len(ud.hooks) - 1; i >= 0; i-- { - if ud.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = ud.hooks[i](mut) - } - n, err := mut.Mutate(ctx, ud.mutation) - if err != nil { - return 0, err - } - nv, ok := n.(int) - if !ok { - return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n) - } - affected = nv - } - return affected, err + return withHooks[int, UserMutation](ctx, ud.sqlExec, ud.mutation, ud.hooks) } // ExecX is like Exec, but panics if an error occurs. @@ -98,6 +64,7 @@ func (ud *UserDelete) sqlExec(ctx context.Context) (int, error) { if err != nil && sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } + ud.mutation.done = true return affected, err } diff --git a/examples/version/ent/user_update.go b/examples/version/ent/user_update.go index 513d1739e..a291d32fc 100644 --- a/examples/version/ent/user_update.go +++ b/examples/version/ent/user_update.go @@ -65,40 +65,7 @@ func (uu *UserUpdate) Mutation() *UserMutation { // Save executes the query and returns the number of nodes affected by the update operation. func (uu *UserUpdate) Save(ctx context.Context) (int, error) { - var ( - err error - affected int - ) - if len(uu.hooks) == 0 { - if err = uu.check(); err != nil { - return 0, err - } - affected, err = uu.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = uu.check(); err != nil { - return 0, err - } - uu.mutation = mutation - affected, err = uu.sqlSave(ctx) - mutation.done = true - return affected, err - }) - for i := len(uu.hooks) - 1; i >= 0; i-- { - if uu.hooks[i] == nil { - return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uu.hooks[i](mut) - } - if _, err := mut.Mutate(ctx, uu.mutation); err != nil { - return 0, err - } - } - return affected, err + return withHooks[int, UserMutation](ctx, uu.sqlSave, uu.mutation, uu.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -134,6 +101,9 @@ func (uu *UserUpdate) check() error { } func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) { + if err := uu.check(); err != nil { + return n, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: user.Table, @@ -168,6 +138,7 @@ func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) { } return 0, err } + uu.mutation.done = true return n, nil } @@ -220,46 +191,7 @@ func (uuo *UserUpdateOne) Select(field string, fields ...string) *UserUpdateOne // Save executes the query and returns the updated User entity. func (uuo *UserUpdateOne) Save(ctx context.Context) (*User, error) { - var ( - err error - node *User - ) - if len(uuo.hooks) == 0 { - if err = uuo.check(); err != nil { - return nil, err - } - node, err = uuo.sqlSave(ctx) - } else { - var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { - mutation, ok := m.(*UserMutation) - if !ok { - return nil, fmt.Errorf("unexpected mutation type %T", m) - } - if err = uuo.check(); err != nil { - return nil, err - } - uuo.mutation = mutation - node, err = uuo.sqlSave(ctx) - mutation.done = true - return node, err - }) - for i := len(uuo.hooks) - 1; i >= 0; i-- { - if uuo.hooks[i] == nil { - return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") - } - mut = uuo.hooks[i](mut) - } - v, err := mut.Mutate(ctx, uuo.mutation) - if err != nil { - return nil, err - } - nv, ok := v.(*User) - if !ok { - return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v) - } - node = nv - } - return node, err + return withHooks[*User, UserMutation](ctx, uuo.sqlSave, uuo.mutation, uuo.hooks) } // SaveX is like Save, but panics if an error occurs. @@ -295,6 +227,9 @@ func (uuo *UserUpdateOne) check() error { } func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) { + if err := uuo.check(); err != nil { + return _node, err + } _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: user.Table, @@ -349,5 +284,6 @@ func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) } return nil, err } + uuo.mutation.done = true return _node, nil }