From e6c91e1dbff95201fd7bbaa5ebf01ef2501566b7 Mon Sep 17 00:00:00 2001 From: Ariel Mashraki <7413593+a8m@users.noreply.github.com> Date: Sun, 12 Jul 2020 11:07:14 +0300 Subject: [PATCH] entc/gen: add support for tx hooks (#575) --- entc/gen/internal/bindata.go | 8 +- entc/gen/template/client.tmpl | 4 +- entc/gen/template/tx.tmpl | 63 ++++++++++-- entc/integration/config/ent/client.go | 4 +- entc/integration/config/ent/tx.go | 116 +++++++++++++++++++---- entc/integration/customid/ent/client.go | 4 +- entc/integration/customid/ent/tx.go | 116 +++++++++++++++++++---- entc/integration/ent/client.go | 4 +- entc/integration/ent/tx.go | 116 +++++++++++++++++++---- entc/integration/gremlin/ent/client.go | 4 +- entc/integration/gremlin/ent/tx.go | 116 +++++++++++++++++++---- entc/integration/hooks/ent/client.go | 4 +- entc/integration/hooks/ent/tx.go | 116 +++++++++++++++++++---- entc/integration/idtype/ent/client.go | 4 +- entc/integration/idtype/ent/tx.go | 116 +++++++++++++++++++---- entc/integration/integration_test.go | 23 ++++- entc/integration/json/ent/client.go | 4 +- entc/integration/json/ent/tx.go | 116 +++++++++++++++++++---- entc/integration/migrate/entv1/client.go | 4 +- entc/integration/migrate/entv1/tx.go | 116 +++++++++++++++++++---- entc/integration/migrate/entv2/client.go | 4 +- entc/integration/migrate/entv2/tx.go | 116 +++++++++++++++++++---- entc/integration/privacy/ent/client.go | 4 +- entc/integration/privacy/ent/tx.go | 116 +++++++++++++++++++---- entc/integration/template/ent/client.go | 4 +- entc/integration/template/ent/tx.go | 116 +++++++++++++++++++---- examples/edgeindex/ent/client.go | 4 +- examples/edgeindex/ent/tx.go | 116 +++++++++++++++++++---- examples/entcpkg/ent/client.go | 4 +- examples/entcpkg/ent/tx.go | 116 +++++++++++++++++++---- examples/m2m2types/ent/client.go | 4 +- examples/m2m2types/ent/tx.go | 116 +++++++++++++++++++---- examples/m2mbidi/ent/client.go | 4 +- examples/m2mbidi/ent/tx.go | 116 +++++++++++++++++++---- examples/m2mrecur/ent/client.go | 4 +- examples/m2mrecur/ent/tx.go | 116 +++++++++++++++++++---- examples/o2m2types/ent/client.go | 4 +- examples/o2m2types/ent/tx.go | 116 +++++++++++++++++++---- examples/o2mrecur/ent/client.go | 4 +- examples/o2mrecur/ent/tx.go | 116 +++++++++++++++++++---- examples/o2o2types/ent/client.go | 4 +- examples/o2o2types/ent/tx.go | 116 +++++++++++++++++++---- examples/o2obidi/ent/client.go | 4 +- examples/o2obidi/ent/tx.go | 116 +++++++++++++++++++---- examples/o2orecur/ent/client.go | 4 +- examples/o2orecur/ent/tx.go | 116 +++++++++++++++++++---- examples/start/ent/client.go | 4 +- examples/start/ent/tx.go | 116 +++++++++++++++++++---- examples/traversal/ent/client.go | 4 +- examples/traversal/ent/tx.go | 116 +++++++++++++++++++---- 50 files changed, 2379 insertions(+), 479 deletions(-) diff --git a/entc/gen/internal/bindata.go b/entc/gen/internal/bindata.go index e278d8282..d208384e9 100644 --- a/entc/gen/internal/bindata.go +++ b/entc/gen/internal/bindata.go @@ -266,7 +266,7 @@ func templateBuilderUpdateTmpl() (*asset, error) { return a, nil } -var _templateClientTmpl = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xc4\x59\xdf\x73\xdb\xb8\xf1\x7f\x16\xff\x8a\xfd\x72\x9c\x7c\x49\x8f\x0c\x5e\xef\xad\xea\xf8\x21\x67\xa7\x77\x9a\xb9\xb3\x73\x8d\xaf\xbd\x99\x4c\x26\x81\xc1\xa5\x84\x9a\x02\x18\x10\xb4\xe5\x51\xf5\xbf\x77\x16\x00\x7f\x49\xb2\xe2\x73\xd3\xe9\x8b\x2d\x02\xc4\xee\xe2\xb3\x9f\x5d\xec\x82\x9b\x4d\x76\x1a\x5d\xe8\xea\xd1\xc8\xc5\xd2\xc2\xf7\xdf\xfd\xe9\xcf\x67\x95\xc1\x1a\x95\x85\xbf\x72\x81\xb7\x5a\xdf\xc1\x5c\x09\x06\x6f\xca\x12\xdc\x4b\x35\xd0\xbc\xb9\xc7\x9c\x45\x37\x4b\x59\x43\xad\x1b\x23\x10\x84\xce\x11\x64\x0d\xa5\x14\xa8\x6a\xcc\xa1\x51\x39\x1a\xb0\x4b\x84\x37\x15\x17\x4b\x84\xef\xd9\x77\xed\x2c\x14\xba\x51\x79\x24\x95\x9b\xff\x79\x7e\xf1\xf6\xea\xfd\x5b\x28\x64\x89\x10\xc6\x8c\xd6\x16\x72\x69\x50\x58\x6d\x1e\x41\x17\x60\x07\xca\xac\x41\x64\xd1\x69\xb6\xdd\x46\xd1\x66\x03\x39\x16\x52\x21\xc4\xa2\x94\xa8\x6c\x0c\x61\xf8\xa4\xba\x5b\xc0\xec\x1c\x6e\x79\x8d\x70\xc2\x2e\xb4\x2a\xe4\x82\xbd\xe3\xe2\x8e\x2f\x90\x5e\xda\x6c\xc0\xe2\xaa\x2a\xb9\x45\x88\x97\xc8\x73\x34\x31\x9c\xb8\xe5\x72\x55\x69\x63\x21\x89\x26\x71\xa9\x17\x71\x14\x4d\x62\x92\xb8\x2f\x24\x5b\xc9\x85\xe1\x16\xe3\x68\xb2\xd9\x80\xe1\x6a\x81\x70\xf2\x69\x0a\x27\x8a\x54\x9f\xb0\x2b\x9d\x63\x4d\x22\x27\x5e\x82\x3a\x20\xc2\x8f\xf7\x03\x4e\xd6\x19\xa0\xca\x9d\x2d\x93\x78\x21\xed\xb2\xb9\x65\x42\xaf\xb2\x22\xb8\x45\x2a\xd1\xdc\x72\xab\x4d\x86\xca\x66\xb9\xe4\x25\x0a\xbb\x67\x44\xd8\x86\xb3\xe4\xbd\xd5\x86\x2f\x90\xcd\xdd\x58\x0d\x67\xbd\x51\xe1\xb5\xa0\xd9\x29\xa6\xd9\x34\x8a\xb2\x0c\x2e\x1c\xaa\xe4\x5b\x72\x8c\xc7\x18\xec\x92\x5b\x58\xea\x32\xaf\x81\x97\x25\xd0\xd0\x6d\x23\xcb\x1c\x4d\xcd\x22\xfb\x58\x61\xbb\xac\xb6\xa6\x11\x16\x36\xd1\x44\xb8\x7d\xfb\xad\xc9\x82\x0c\x6a\x2a\x52\xfb\x8b\x07\xd0\x63\x94\x65\xf0\x5e\x2c\x71\xc5\x77\xf4\x15\xda\x80\x30\xc8\xad\x54\x8b\x29\x78\xcc\xa5\x5a\x00\x57\x39\xe4\x46\x57\x15\x3d\xd4\x6e\x25\x8b\x26\x93\x20\xe3\x34\x38\x87\xf9\xe7\x11\xac\xee\x77\x80\x6a\xdf\x57\x59\x06\xde\x2b\x57\x7c\x45\xa6\x1d\x30\x47\x2a\x8b\x86\x0b\x67\xc6\x83\xb4\x4b\x37\x3f\x5e\xd4\x43\x32\x99\x8c\x67\x4e\x47\x8f\x1e\xab\x5d\xf3\x06\xe4\xf4\x6a\xb3\x42\x62\x99\xd7\x19\xcf\x73\x69\xa5\x56\xbc\x0c\x74\xdd\x3a\x47\x5d\xe1\x43\x00\xdd\x21\x85\x35\x70\x50\xf8\xd0\xda\xec\xf1\x6f\x0c\xe6\xbd\xb9\x0b\x79\x8f\x0a\x74\x45\xd2\x6a\x16\x15\x8d\x12\xbd\x98\x44\x57\xb6\x06\xc6\xd8\xb5\x9b\x4f\xe1\x34\x88\x27\x67\x16\x2e\xb4\xbc\xcc\x4d\xa9\x17\x33\x28\xf5\x82\xbd\x33\x52\xd9\x52\x4d\x61\xa9\xf5\x5d\x3d\x83\xd7\xee\xff\x86\xf6\x23\x8a\x05\x0b\x8a\x9c\x60\xc6\x58\x1a\x4d\x82\x6d\xb3\x73\x78\xed\x85\x6f\xbc\xc8\x19\x88\x62\xb1\x6d\xe7\x99\x54\xd2\x26\x69\x34\x31\x68\x1b\xa3\xc2\x8e\x68\xdb\xce\xe2\x44\xb4\xa6\xa5\xe0\xdf\x24\x13\x8f\xf2\x4c\x04\x4a\xc0\x39\xb4\x1c\xb9\xc2\x07\x3f\x96\x08\x96\x1b\x79\x8f\x26\x7d\x36\x61\x00\x00\x26\x82\x8d\x7d\x7c\x0e\x84\xe5\x01\x47\x27\x82\xf9\x5d\x8e\x15\x78\x2f\x5e\x57\xce\x23\xa8\xc8\x7d\x39\xb7\x9c\xd2\x57\x56\x7f\x29\xd9\xe5\x0f\x50\x57\x28\x64\x21\x31\x87\xdb\x47\xe7\x40\x6f\x28\x28\x12\xcf\x55\x4e\x02\xdc\x30\xb7\xbc\x4d\x96\x34\x37\x75\x81\xe2\xd1\xdb\xa1\x05\xb7\x96\xd2\x73\x0e\x56\x83\xb4\xcc\x9b\xe0\xd9\x05\x15\x37\x7c\x85\x16\x4d\x0d\x82\x2b\xb8\x45\xe0\x79\x8e\xb9\x8f\xc6\x40\x27\xa2\x7f\x1f\x19\x81\x43\xb4\x89\xc4\xdb\x76\xe5\xd4\x93\x41\xef\x9d\x3d\x0e\x89\xda\x1a\x17\xc8\x81\x10\x43\x92\x25\xc1\x95\x53\x40\x63\xb4\x71\xae\xac\x1f\xa4\x15\x4b\xe8\x05\x3a\x0a\x52\x5a\xdf\x6c\xe0\x9f\x5a\xaa\x41\x7a\xbb\xf4\xa9\xb0\x86\x78\x0a\x74\x14\xcc\x5c\xec\x9d\xc1\x89\x5d\x55\x25\xb9\xad\x22\x8e\x16\x10\x87\x9c\x99\xbd\xaa\xb3\x10\x5e\x84\x7a\xdc\x8b\x0a\x19\x92\x16\xaf\xbb\x50\xf4\x62\x98\x9f\xcb\xb1\xe0\x4d\x69\x49\x45\x60\xa6\x92\xe5\x14\x8a\x95\x65\x6f\xc9\xf8\x22\x89\x1b\x55\x7b\xfa\x61\x1e\xec\x9f\xc1\xab\x2f\xf1\x74\xb0\x99\x34\x9a\xb4\xce\xbf\x59\xef\x38\xc9\x1a\xae\x6a\x4a\x32\xce\x1f\x23\x8c\x87\xac\xbf\x59\x27\xc2\xae\xc9\x27\x16\xd7\x96\x8e\x18\xfa\x4f\x60\xde\xac\x87\x40\xca\x02\x3e\x4d\x41\xdf\xb9\xd8\x0d\x2c\x67\xc9\xa9\x5d\x5f\x7a\xc2\xff\x85\xe6\x36\x47\xb6\xd3\x1e\xab\xdb\xed\x8c\x28\xa1\x34\x65\x78\x6e\x2c\xf0\xa1\xa9\x2e\xc1\x48\x35\x1e\x8c\xdd\x3e\x27\xd6\x1b\x44\x16\x28\x7c\xf0\x86\x4f\x61\x10\x72\xb2\x70\xf3\xff\x77\x4e\xda\x9f\x6d\x8c\xb3\xc2\x9d\x08\x43\x9d\x33\x78\x75\x1f\x3b\x7d\x5e\xf9\x38\x6d\xb5\xfe\x20\x03\x5c\x0a\x13\xac\xd4\x8b\x29\xe4\x78\xdb\xb8\x27\xf7\xa3\x4b\x66\x82\xb9\x1f\xdb\x2e\x0d\xbd\xbe\x59\x93\x79\x83\x8c\x35\xf5\x89\xfe\xa9\x32\xc0\x13\x6a\x7c\x14\xcc\x9e\x4c\x12\xc5\x22\x0d\xf2\xda\x03\x79\xb2\x9d\x12\x0a\x91\xab\x6f\xce\x20\x3b\x85\x79\xe1\x82\xaf\x0e\x8c\x0d\xe9\x20\x50\xae\x86\x9b\xf5\x75\x88\xb0\xa4\x94\x77\x08\xef\x7f\xfd\x39\x05\x57\x37\xf5\x21\x71\x30\x22\xec\x3a\x84\xe6\x30\x1e\xc2\x32\x59\xc0\x92\xd7\x37\xe3\x88\x08\x49\xf0\x70\xb0\x84\x85\x6d\x41\x93\x65\x70\x49\xc8\xee\x70\xdd\xa1\x7d\x16\x38\x0e\x73\xfb\xff\x35\x34\xb5\x4f\x4c\x0b\xb4\x70\x8f\xe6\x56\xd7\x48\x9e\x5a\x90\xa3\xb5\x6a\xd3\xa1\xa0\x7c\x49\x05\x81\x3b\xc4\xb2\x2c\xca\xb2\xf6\xe0\x70\x7a\x92\x94\x46\x1d\x92\x89\x54\x39\xae\x3b\x87\x7c\x97\xb6\xa0\xfb\x37\x7e\x6d\xd0\x3c\xb6\xaf\x5f\xe8\x86\xdc\x60\xd7\x29\xc9\xdc\x8b\xb9\x20\x7a\x78\x2a\xca\xa2\x25\xcd\x90\xb7\xe2\x08\xf5\x02\xe4\xc1\xce\x36\x0a\xa6\x9e\x89\xe9\x41\x5a\x5a\xd3\xe0\x01\x4e\xfe\xa7\x27\xa9\xab\xf4\x08\x5f\x41\x7f\xeb\xee\x18\x71\xd5\xb3\xd0\x4a\xa1\x0f\x6d\x3a\x48\x2a\x83\xf7\xa8\x6c\xed\xdc\xf6\xa5\x41\x23\xb1\x86\xc2\xe8\x55\x17\x86\x07\x72\x94\x93\x9e\xa4\x3e\x1b\x11\x3e\xad\x09\x6d\x1e\x0a\x2f\x04\x63\x7e\xab\xdd\x69\xe3\x0d\x59\x35\xd6\xb9\xd7\x6f\x9b\x18\x41\x55\x27\xcd\xa0\xb2\xd2\x3e\x86\x7d\x38\xef\xc3\x5c\x81\x36\xae\xf9\xd0\x24\x61\xb0\xa6\x27\x8c\x08\x67\x8c\xe0\x65\x39\x83\xcf\x01\x1c\x22\x05\xfb\xad\xc6\x84\x8a\x93\xcf\x07\xf6\x40\x73\x5e\x1c\x63\xec\x27\xad\xef\xba\x4a\xe3\x68\xe5\xbf\x53\x19\xb0\x4e\x8c\x2f\x82\xc6\x35\x40\x74\xbc\x8f\x20\x49\xbd\xaf\x5d\xe8\x76\xa2\xe3\x8b\xbe\x03\x0a\x15\x6c\x78\xd5\x57\xb0\x7c\x58\xbf\xee\x97\xab\x6d\xfd\xec\xea\xf7\xf1\xe2\xbd\x32\x3e\xb4\x58\x06\x85\xb3\x4f\xb1\xbf\xa1\x40\x97\x7c\xb6\xdb\xcd\x86\x72\x04\x7e\xf1\xd3\xb1\x88\xfd\x98\x7b\xea\xb3\xcd\x2b\xf6\x3d\x65\x97\xa0\xfe\x5f\x50\xea\x87\x76\xf5\x20\x51\x84\xe4\xd8\x5b\xd2\xe7\x8c\xa3\x7b\x71\x6c\xec\x4b\x5c\x6f\x75\x5f\xe1\x8e\x64\x26\x22\xcc\xa7\xbe\x2e\xef\x95\xf5\x2c\x7d\x3d\x9a\xe8\x63\x6b\xbb\x4b\x57\x0e\xa5\xac\x2d\x75\xac\xfb\xa4\x25\x7b\xfc\x43\x6d\xb9\xb8\x73\x6c\x7d\xe3\x38\x48\xb3\x9f\x89\x16\xc5\x14\xe8\xc4\x49\x3f\x03\x7e\x69\x78\xe9\x96\x7d\xde\x6d\x10\x1d\xf5\xea\xa4\x48\x16\xc9\x32\x49\xd3\x74\xc4\xd5\x91\xa1\x4f\x51\x36\xe4\x8d\xbd\x8a\x95\x57\x15\xaa\x3c\x39\x38\x1d\x92\x8e\xe3\x6c\x48\x18\xae\xcf\x18\xba\xc4\x0f\x84\xbe\xc7\xb9\x66\xcc\xfc\x27\xcd\xf4\xa2\x92\x74\xb7\x33\xf2\x02\x37\xd1\xa4\x43\xd3\x17\x0e\xfe\xad\x5f\xc2\x60\x78\xbb\x2b\xac\xa7\x70\x5d\xf9\xa5\xe9\xd8\x83\x3b\x82\x7b\x3f\x76\x0b\xbb\xc4\xea\x31\x4e\xa7\x9d\x1f\x67\xdd\xaf\xce\xe9\x55\x3e\xda\xbf\x82\xc6\x8f\xbc\x00\x00\x2f\x6b\x0f\x80\xa0\xe2\x25\x00\xf8\xa5\x4f\x01\xe0\x67\xbf\x09\x00\xd7\xea\x6b\x18\xf4\x81\xe8\xf3\xf5\xd7\x60\xb8\x56\x98\xb4\x19\x63\xaf\x5b\x3e\x0c\x11\x19\xf1\x72\x94\xae\x15\x4e\xc9\x53\x3e\x9f\xc6\x54\xbf\xc6\x03\x15\x03\x63\xd2\xe3\x80\x5e\xab\x6f\x86\xe9\xfc\xf2\xd9\xa8\xca\xfc\x19\x88\xce\x2f\x13\x99\x07\x3a\xce\x2f\xd9\x0d\x65\xf9\xff\x01\x9a\xf1\xfc\x92\x0e\x84\x44\xe6\xff\x75\x28\x2f\xb1\xc4\x51\x7e\xca\xfd\xc0\x0b\xc2\xd3\x8b\xda\x0b\xcf\xa0\xe1\x25\x50\xf9\xa5\x4f\x41\xe0\x67\xbf\xc9\xfe\x47\xe1\x79\x08\x82\xe7\x47\x67\x27\xf0\xf9\xd1\xd9\xdb\x30\x2c\xf9\xba\xd1\xf9\xe5\x40\x14\x9b\x5f\xa6\xbb\xa6\x0f\xa3\xe0\xb8\xf1\xc7\x82\x60\xa8\xef\x19\x41\x30\x32\xba\xd5\xe6\x7a\xe6\x96\x07\xec\x1f\x4b\x34\x1e\x86\xd1\xc9\xec\xe4\x13\xb1\xc3\x2a\xd6\xfa\x84\xc9\x1c\xce\xe1\xb5\xcc\x0f\x4c\xe9\x0a\xce\x3b\x46\x5c\x2b\x3c\xca\x09\x0a\x8b\x20\x61\xfb\xd4\x39\x4c\x35\xf9\xe3\x4b\x68\x1e\x1a\xa0\x1d\x38\xdc\xe8\x6e\x31\x34\x9e\xdd\x63\x6a\x6b\xdb\x8f\x68\x07\x86\x8d\xeb\x8d\x50\xbc\xdf\x3e\x82\xb4\xf5\x51\xff\xfd\x88\xf6\xd0\x0d\xc7\x14\x0e\x3a\x33\x39\xdd\x29\x5c\xfa\x1b\x90\x8e\x81\x6d\xab\x77\xdc\x8f\xec\x5a\x95\x8f\xbe\x07\xec\xb6\xf3\xbb\xff\xce\x71\x87\xf4\x30\x85\xdb\xc6\x42\xc5\x95\x14\x35\x55\xbd\x5c\x85\x06\x47\x0b\xd1\x98\xfa\xe8\x8e\x7e\xff\x03\x5b\x1a\xef\xc8\x77\x1d\x6d\xd8\x74\x17\x2a\x82\x05\x9c\x48\xc8\xc1\xab\x14\x67\x68\xd2\xdd\x87\x04\x34\x7a\x51\xa1\xae\x1f\xf4\x1f\x18\xea\xfb\xb7\xf9\xa2\x6f\x40\x06\x21\x71\x82\xce\x48\x8f\x67\x30\x8f\x80\xf2\xac\xd8\x40\xc5\x6b\xc1\x4b\x7a\xad\xb5\xbd\x6d\x18\xdb\xa2\xbd\x9f\xc1\x7c\x81\x54\x39\xf3\x3f\x44\xd7\x43\x4a\xbe\x9a\x9f\xda\x1d\x78\x2c\x7d\xbc\x50\xef\x3c\x9e\x3b\xc0\x6a\xff\x2e\xab\xb8\x5d\xc2\x39\x90\x61\x4f\x5c\xbd\x51\x07\xf2\x77\xb7\x91\xee\x6e\xf2\x87\x4e\xf0\x14\x3e\x0d\x48\xe9\x3a\x40\x77\x29\x8f\x6b\x4b\xcd\xcf\x89\x82\xb8\x6d\xa8\xe2\xd0\x46\x91\x03\x62\xf2\x47\x3c\xcf\x5d\x93\x17\x3b\x0d\x31\xf4\x97\x4a\x47\xae\x38\x9d\xd5\x19\xad\xd8\xb9\xd1\x99\x1c\xbd\xe1\xec\x7a\x53\xff\x14\xf8\xe2\x14\xfb\xab\xa8\x01\x8b\x9c\x8a\xc8\x11\x64\xd0\xc0\xb9\x63\xaa\xcb\x00\x83\x6f\x28\xbe\x99\x78\xd2\xb5\xe1\x78\x83\x0f\x1f\xe9\xd7\xe0\x42\x5f\x1b\xe7\xcd\x66\xe5\x25\x9f\x28\xf6\x13\xaf\xdf\xe9\x52\x8a\x47\xbf\x1f\xdf\xed\xb8\x70\x38\xd0\xc5\xf4\xbb\x08\xbd\x8e\x7b\xe7\xc3\xac\x44\xe5\x7f\xa6\x83\x9f\x1f\xa7\x70\xb8\xf7\xfa\x30\xfb\x38\xe8\xdd\xcb\x7a\x2c\xf9\x09\xc5\xe3\x3e\xbf\x87\x69\x00\xd8\x66\x93\x9d\xc2\x9b\xfe\xcb\x8f\xfb\xce\x16\xee\xde\xf5\x3d\x1a\x23\x73\xcc\x41\xee\xdc\x70\xf4\x1f\x84\xc0\x7f\x22\x6a\x9b\xcd\x70\xaf\x11\x6e\xfc\x76\x3e\x94\x1e\xfa\x9c\x34\x6a\xbf\xff\x1d\x00\x00\xff\xff\x9a\x96\x75\x27\x1f\x1e\x00\x00") +var _templateClientTmpl = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xc4\x59\xdf\x73\xdb\xb8\xf1\x7f\x16\xff\x8a\xfd\x72\x9c\x7c\x49\x8f\x0c\x5e\xef\xad\xea\xf8\x21\x67\xa7\x77\x9a\xb9\xb3\x73\x8d\xaf\xbd\x99\x4c\x26\x81\xc1\xa5\x84\x9a\x02\x18\x10\xb4\xe5\x51\xf5\xbf\x77\x16\x00\x7f\x49\xb2\xe2\x73\xd3\xe9\x8b\x2d\x02\xc4\xee\xe2\xb3\x9f\x5d\xec\x82\x9b\x4d\x76\x1a\x5d\xe8\xea\xd1\xc8\xc5\xd2\xc2\xf7\xdf\xfd\xe9\xcf\x67\x95\xc1\x1a\x95\x85\xbf\x72\x81\xb7\x5a\xdf\xc1\x5c\x09\x06\x6f\xca\x12\xdc\x4b\x35\xd0\xbc\xb9\xc7\x9c\x45\x37\x4b\x59\x43\xad\x1b\x23\x10\x84\xce\x11\x64\x0d\xa5\x14\xa8\x6a\xcc\xa1\x51\x39\x1a\xb0\x4b\x84\x37\x15\x17\x4b\x84\xef\xd9\x77\xed\x2c\x14\xba\x51\x79\x24\x95\x9b\xff\x79\x7e\xf1\xf6\xea\xfd\x5b\x28\x64\x89\x10\xc6\x8c\xd6\x16\x72\x69\x50\x58\x6d\x1e\x41\x17\x60\x07\xca\xac\x41\x64\xd1\x69\xb6\xdd\x46\xd1\x66\x03\x39\x16\x52\x21\xc4\xa2\x94\xa8\x6c\x0c\x61\xf8\xa4\xba\x5b\xc0\xec\x1c\x6e\x79\x8d\x70\xc2\x2e\xb4\x2a\xe4\x82\xbd\xe3\xe2\x8e\x2f\x90\x5e\xda\x6c\xc0\xe2\xaa\x2a\xb9\x45\x88\x97\xc8\x73\x34\x31\x9c\xb8\xe5\x72\x55\x69\x63\x21\x89\x26\x71\xa9\x17\x71\x14\x4d\x62\x92\xb8\x2f\x24\x5b\xc9\x85\xe1\x16\xe3\x68\xb2\xd9\x80\xe1\x6a\x81\x70\xf2\x69\x0a\x27\x8a\x54\x9f\xb0\x2b\x9d\x63\x4d\x22\x27\x5e\x82\x3a\x20\xc2\x8f\xf7\x03\x4e\xd6\x19\xa0\xca\x9d\x2d\x93\x78\x21\xed\xb2\xb9\x65\x42\xaf\xb2\x22\xb8\x45\x2a\xd1\xdc\x72\xab\x4d\x86\xca\x66\xb9\xe4\x25\x0a\xbb\x67\x44\xd8\x86\xb3\xe4\xbd\xd5\x86\x2f\x90\xcd\xdd\x58\x0d\x67\xbd\x51\xe1\xb5\xa0\xd9\x29\xa6\xd9\x34\x8a\xb2\x0c\x2e\x1c\xaa\xe4\x5b\x72\x8c\xc7\x18\xec\x92\x5b\x58\xea\x32\xaf\x81\x97\x25\xd0\xd0\x6d\x23\xcb\x1c\x4d\xcd\x22\xfb\x58\x61\xbb\xac\xb6\xa6\x11\x16\x36\xd1\x44\xb8\x7d\xfb\xad\xc9\x82\x0c\x6a\x2a\x52\xfb\x8b\x07\xd0\x63\x94\x65\xf0\x5e\x2c\x71\xc5\x77\xf4\x15\xda\x80\x30\xc8\xad\x54\x8b\x29\x78\xcc\xa5\x5a\x00\x57\x39\xe4\x46\x57\x15\x3d\xd4\x6e\x25\x8b\x26\x93\x20\xe3\x34\x38\x87\xf9\xe7\x11\xac\xee\x77\x80\x6a\xdf\x57\x59\x06\xde\x2b\x57\x7c\x45\xa6\x1d\x30\x47\x2a\x8b\x86\x0b\x67\xc6\x83\xb4\x4b\x37\x3f\x5e\xd4\x43\x32\x99\x8c\x67\x4e\x47\x8f\x1e\xab\x5d\xf3\x06\xe4\xf4\x6a\xb3\x42\x62\x99\xd7\x19\xcf\x73\x69\xa5\x56\xbc\x0c\x74\xdd\x3a\x47\x5d\xe1\x43\x00\xdd\x21\x85\x35\x70\x50\xf8\xd0\xda\xec\xf1\x6f\x0c\xe6\xbd\xb9\x0b\x79\x8f\x0a\x74\x45\xd2\x6a\x16\x15\x8d\x12\xbd\x98\x44\x57\xb6\x06\xc6\xd8\xb5\x9b\x4f\xe1\x34\x88\x27\x67\x16\x2e\xb4\xbc\xcc\x4d\xa9\x17\x33\x28\xf5\x82\xbd\x33\x52\xd9\x52\x4d\x61\xa9\xf5\x5d\x3d\x83\xd7\xee\xff\x86\xf6\x23\x8a\x05\x0b\x8a\x9c\x60\xc6\x58\x1a\x4d\x82\x6d\xb3\x73\x78\xed\x85\x6f\xbc\xc8\x19\x88\x62\xb1\x6d\xe7\x99\x54\xd2\x26\x69\x34\x31\x68\x1b\xa3\xc2\x8e\x68\xdb\xce\xe2\x44\xb4\xa6\xa5\xe0\xdf\x24\x13\x8f\xf2\x4c\x04\x4a\xc0\x39\xb4\x1c\xb9\xc2\x07\x3f\x96\x08\x96\x1b\x79\x8f\x26\x7d\x36\x61\x00\x00\x26\x82\x8d\x7d\x7c\x0e\x84\xe5\x01\x47\x27\x82\xf9\x5d\x8e\x15\x78\x2f\x5e\x57\xce\x23\xa8\xc8\x7d\x39\xb7\x9c\xd2\x57\x56\x7f\x29\xd9\xe5\x0f\x50\x57\x28\x64\x21\x31\x87\xdb\x47\xe7\x40\x6f\x28\x28\x12\xcf\x55\x4e\x02\xdc\x30\xb7\xbc\x4d\x96\x34\x37\x75\x81\xe2\xd1\xdb\xa1\x05\xb7\x96\xd2\x73\x0e\x56\x83\xb4\xcc\x9b\xe0\xd9\x05\x15\x37\x7c\x85\x16\x4d\x0d\x82\x2b\xb8\x45\xe0\x79\x8e\xb9\x8f\xc6\x40\x27\xa2\x7f\x1f\x19\x81\x43\xb4\x89\xc4\xdb\x76\xe5\xd4\x93\x41\xef\x9d\x3d\x0e\x89\xda\x1a\x17\xc8\x81\x10\x43\x92\x25\xc1\x95\x53\x40\x63\xb4\x71\xae\xac\x1f\xa4\x15\x4b\xe8\x05\x3a\x0a\x52\x5a\xdf\x6c\xe0\x9f\x5a\xaa\x41\x7a\xbb\xf4\xa9\xb0\x86\x78\x0a\x74\x14\xcc\x5c\xec\x9d\xc1\x89\x5d\x55\x25\xb9\xad\x22\x8e\x16\x10\x87\x9c\x99\xbd\xaa\xb3\x10\x5e\x84\x7a\xdc\x8b\x0a\x19\x92\x16\xaf\xbb\x50\xf4\x62\x98\x9f\xcb\xb1\xe0\x4d\x69\x49\x45\x60\xa6\x92\xe5\x14\x8a\x95\x65\x6f\xc9\xf8\x22\x89\x1b\x55\x7b\xfa\x61\x1e\xec\x9f\xc1\xab\x2f\xf1\x74\xb0\x99\x34\x9a\xb4\xce\xbf\x59\xef\x38\xc9\x1a\xae\x6a\x4a\x32\xce\x1f\x01\x63\xb8\x59\x22\x54\x46\xdf\x4b\x72\x86\xd0\xca\xe2\xda\xd2\x72\x59\x43\xe3\xcf\x5e\x2b\x4b\xe7\x95\xc1\x7a\x9a\x15\x7a\xb5\x92\x96\x6c\xd1\x06\x8c\x2e\x4b\x62\x12\x17\x77\x6c\x3f\x90\x6e\xd6\x89\xb0\xeb\x56\x3a\x9d\x5a\xf4\x9f\xfc\x73\xb3\x1e\xfa\x46\x16\xf0\x69\x0a\xfa\xce\xa5\x83\x10\x38\x2c\x39\xb5\xeb\x4b\x1f\x43\x7f\xa1\xb9\xcd\x11\x84\xda\x93\x7a\xbb\x9d\x11\xcb\x94\xa6\x43\x83\x1b\x0b\x7c\x64\x3d\xe5\x2c\xa9\xc6\x83\xb1\x83\x6e\x62\xbd\x41\x64\x81\xc2\x07\x6f\xf8\x14\x06\x51\x2c\x0b\x37\xff\x7f\xe7\xa4\xfd\xd9\xc6\x38\x2b\xdc\x21\x33\xd4\x39\x83\x57\xf7\xb1\xd3\xe7\x95\x8f\x33\x61\xeb\x62\x32\xc0\x65\x45\xc1\x4a\xbd\x98\x42\x8e\xb7\x8d\x7b\x72\x3f\xba\xfc\x28\x98\xfb\xb1\xed\x32\xdb\xeb\x9b\x35\x99\x27\xec\x7a\x06\xb4\x0b\xfa\xdd\x27\xc4\xa9\x3f\x47\x9e\xaa\x32\x3c\x5f\xc7\x27\xcd\xec\xc9\x1c\x54\x2c\xd2\x20\xaf\x3d\xef\x27\xdb\x29\x21\x12\xb9\xf2\xe9\x0c\xb2\x53\x98\x17\x8e\x45\x75\x08\x88\x90\x6d\x02\xa3\x6b\xb8\x59\x5f\x87\x00\x4e\x4a\x79\x87\xf0\xfe\xd7\x9f\x53\x70\x65\x59\x1f\x71\x07\x03\xce\xae\x43\xe4\x0f\xc3\x2d\x2c\x93\x05\x2c\x79\x7d\x33\x0e\xb8\x90\x63\x0f\xc7\x62\x58\xd8\xd6\x4b\x59\x06\x97\x84\xf2\x4e\x28\x39\xe4\xcf\xda\x10\x9a\xdb\xff\x0f\xc1\x62\x35\x2c\xd0\xc2\x3d\x9a\x5b\x5d\x23\x79\x6d\x41\x4e\xd7\xaa\xcd\xb6\x82\xd2\x31\xd5\x1b\xee\x8c\xcc\xb2\x28\xcb\xda\x73\xc9\xe9\x49\x52\x1a\x75\x48\x26\x52\xe5\xb8\xee\x1c\xf2\x5d\xda\x82\xee\xdf\xf8\xb5\x41\xf3\xd8\xbe\x7e\xa1\x1b\x72\x83\x5d\xa7\x24\x73\x2f\xfe\x82\xe8\xe1\xa1\x2b\x8b\x96\x40\x43\x0e\x8b\x23\x34\x0c\x90\x07\x3b\xdb\x88\x98\x7a\x56\xa6\x07\x29\x6a\x4d\x83\x07\xf8\xf9\x9f\x1e\xd4\xae\x90\x24\x7c\x05\xfd\xad\xbb\x53\xca\x15\xe7\x42\x2b\x85\x3e\xcc\xe9\x9c\xaa\x0c\xde\xa3\xb2\xb5\x73\xdb\x97\x06\x8d\xc4\x1a\x0a\xa3\x57\x5d\x48\x1e\xc8\x57\x4e\x7a\x92\xfa\xcc\x44\xf8\xb4\x26\xb4\x39\x29\xbc\x10\x8c\xf9\xad\x76\x87\x99\x37\x64\xd5\x58\xe7\x5e\xbf\x6d\x62\x04\x15\xb5\x34\x83\xca\x4a\xfb\x18\xf6\xe1\xbc\x0f\x73\x05\xda\xb8\xde\x46\x93\x84\xc1\x9a\x9e\x30\x22\x1c\x61\x82\x97\xe5\x0c\x3e\x07\x70\x88\x14\xec\xb7\x1a\x13\xaa\x7d\x3e\x1f\xd8\x03\xcd\x79\x71\x8c\xb1\x9f\xb4\xbe\xeb\x0a\x99\xa3\x8d\xc5\x4e\xe1\xc1\x3a\x31\xbe\xc6\x1a\x97\x18\xd1\xf1\x36\x85\x24\xf5\xbe\x76\xa1\xdb\x89\x8e\x2f\xfa\x06\x2b\x14\xc8\xe1\x55\x5f\x20\xf3\x61\x79\xbc\x5f\x0d\xb7\xe5\xb9\x6b\x0f\xc6\x8b\xf7\xba\x84\xd0\xc1\x19\x14\xce\x3e\xc5\xfe\x86\x02\x5d\xf2\xd9\x6e\x37\x1b\xca\x11\xf8\xc5\x4f\xc7\x22\xf6\x63\xee\xa9\xcf\x36\xaf\xd8\xf7\x94\x5d\x82\xfa\x7f\x41\xa9\x1f\xda\xd5\x83\x44\x11\x92\x63\x6f\x49\x9f\x33\x8e\xee\xc5\xb1\xb1\xaf\xa0\xbd\xd5\x7d\x01\x3d\x92\x99\x88\x30\x9f\xfa\xb2\xbf\x57\xd6\xb3\xf4\xf5\x68\xa2\x8f\xad\xed\x2e\x5d\x39\x94\xb2\xb6\xd4\x10\xef\x93\x96\xec\xf1\x0f\xb5\x75\x87\x7a\x96\xc1\x1b\xc7\x41\x9a\xfd\x4c\xb4\x28\xa6\x40\xa7\x4f\xfa\x19\xf0\x4b\xc3\x4b\xb7\xec\xf3\x6e\xff\xe9\xa8\x57\x27\x45\xb2\x48\x96\x49\x9a\xa6\x23\xae\x8e\x0c\x7d\x8a\xb2\x21\x6f\xec\x15\xc4\xbc\xaa\x50\xe5\xc9\xc1\xe9\x90\x74\x1c\x67\x43\xc2\x70\x6d\xcc\xd0\x25\x7e\x20\xb4\x55\xce\x35\x63\xe6\x3f\x69\xa6\x17\x95\xa4\xbb\x8d\x97\x17\xb8\x89\x26\x1d\x9a\xbe\x88\xf0\x6f\xfd\x12\x06\xc3\xdb\x5d\xdd\x3e\x85\xeb\xca\x2f\x4d\xc7\x1e\xdc\x11\xdc\xfb\xb1\x5b\xd8\x25\x56\x8f\x71\x3a\xed\xfc\x38\xeb\x7e\x75\x4e\xaf\xf2\xd1\xfe\x15\x34\x7e\xe4\x05\x00\x78\x59\x7b\x00\x04\x15\x2f\x01\xc0\x2f\x7d\x0a\x00\x3f\xfb\x4d\x00\xb8\x56\x5f\xc3\xa0\x0f\x44\x9f\xaf\xbf\x06\xc3\xb5\xc2\xa4\xcd\x18\x7b\xcd\xf8\x61\x88\xc8\x88\x97\xa3\x74\xad\x70\x4a\x9e\xf2\xf9\x34\xa6\x5a\x36\x1e\xa8\x18\x18\x93\x1e\x07\xf4\x5a\x7d\x33\x4c\xe7\x97\xcf\x46\x55\xe6\xcf\x40\x74\x7e\x99\xc8\x3c\xd0\x71\x7e\xc9\x6e\x28\xcb\xff\x0f\xd0\x8c\xe7\x97\x74\x20\x24\x32\xff\xaf\x43\x79\x89\x25\x8e\xf2\x53\xee\x07\x5e\x10\x9e\x5e\xd4\x5e\x78\x06\x0d\x2f\x81\xca\x2f\x7d\x0a\x02\x3f\xfb\x4d\xf6\x3f\x0a\xcf\x43\x10\x3c\x3f\x3a\x3b\x81\xcf\x8f\xce\xde\x86\x61\xc9\xd7\x8d\xce\x2f\x07\xa2\xd8\xfc\x32\xdd\x35\x7d\x18\x05\xc7\x8d\x3f\x16\x04\x43\x7d\xcf\x08\x82\x91\xd1\xad\x36\xd7\x3f\xb7\x3c\x60\xff\x58\xa2\xf1\x30\x8c\x4e\x66\x27\x9f\x88\x1d\x56\xb1\xd6\x27\x4c\xe6\x70\x0e\xaf\x65\x7e\x60\x4a\x57\x70\xde\x31\xe2\x5a\xe1\x51\x4e\x50\x58\x04\x09\xdb\xa7\xce\x61\xaa\xc9\x1f\x5f\x42\xf3\xd0\x00\xed\xc0\xe1\x46\x77\x8b\xa1\xf1\xec\x1e\x53\x5b\xdb\x7e\x44\x3b\x30\x6c\x5c\x6f\x84\xe2\xfd\xf6\x11\xa4\xad\x8f\xfa\xef\x47\xb4\x87\x6e\x3b\xa6\x70\xd0\x99\xc9\xe9\x4e\xe1\xd2\xdf\x86\x74\x0c\x6c\x5b\xbd\xe3\x7e\x64\xd7\xaa\x7c\xf4\x3d\x60\xb7\x9d\xdf\xfd\x67\x94\x3b\xa4\x87\x29\xdc\x36\x16\x2a\xae\xa4\xa8\xa9\xea\xe5\x2a\x34\x38\x5a\x88\xc6\xd4\x47\x77\xf4\xfb\x1f\xd8\xd2\x78\x47\xbe\xeb\x68\xc3\xa6\xbb\x5c\x11\x2c\xe0\x44\x42\x0e\x5e\xab\x38\x43\x93\xee\x6e\x24\xa0\xd1\x8b\x0a\x75\xfd\xa0\xff\xc0\x50\xdf\xbf\xcd\x17\x7d\x03\x32\x08\x89\x13\x74\x46\x7a\x3c\x83\x79\x04\x94\x67\xc5\x06\x2a\x5e\x0b\x5e\xd2\x6b\xad\xed\x6d\xc3\xd8\x16\xed\xfd\x0c\xe6\x0b\xa4\xca\x99\xff\x21\xba\x1e\x52\xf2\xd5\xfc\xd4\xee\xc0\x63\xe9\xe3\x85\x7a\xe7\xf1\xdc\x01\x56\xfb\x77\x59\xc5\xed\x12\xce\x81\x0c\x7b\xe2\x1a\x8e\x3a\x90\xbf\xbb\x8d\x74\x57\x9f\x3f\x74\x82\xa7\xf0\x69\x40\x4a\xd7\x01\xba\x3b\x7f\x5c\x5b\x6a\x7e\x4e\x14\xc4\x6d\x43\x15\x87\x36\x8a\x1c\x10\x93\x3f\xe2\x79\xee\x9a\xbc\xd8\x69\x88\xa1\xbf\x54\x3a\x72\x83\xea\xac\xce\x68\xc5\xce\x8d\xce\xe4\xe8\x05\x6a\xd7\x9b\xfa\xa7\xc0\x17\xa7\xd8\x5f\x45\x0d\x58\xe4\x54\x44\x8e\x20\x83\x06\xce\x1d\x53\x5d\x06\x18\x7c\xa2\xf1\xcd\xc4\x93\xae\x0d\xc7\x1b\x7c\xf8\x48\xbf\x06\xdf\x0b\xb4\x71\xde\x6c\x56\x5e\xf2\x89\x62\x3f\xf1\xfa\x9d\x2e\xa5\x78\xf4\xfb\xf1\xdd\x8e\x0b\x87\x03\x5d\x4c\xbf\x8b\xd0\xeb\xb8\x77\x3e\xcc\x4a\x54\xfe\x67\x3a\xf8\xf9\x71\x0a\x87\x7b\xaf\x0f\xb3\x8f\x83\xde\xbd\xac\xc7\x92\x9f\x50\x3c\xee\xf3\x7b\x98\x06\x80\x6d\x36\xd9\x29\xbc\xe9\x3f\x2c\xb9\xcf\x78\xe1\x6a\x5f\xdf\xa3\x31\xee\x46\x59\xee\xdc\x70\xf4\xdf\x9b\xc0\x7f\x81\x6a\x9b\xcd\x70\xaf\x11\x6e\xfc\x76\xbe\xc3\x1e\xfa\x5a\x35\x6a\xbf\xff\x1d\x00\x00\xff\xff\x6b\xa2\x7c\x13\x7e\x1e\x00\x00") func templateClientTmplBytes() ([]byte, error) { return bindataRead( @@ -281,7 +281,7 @@ func templateClientTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "template/client.tmpl", size: 7711, mode: os.FileMode(420), modTime: time.Unix(1, 0)} + info := bindataFileInfo{name: "template/client.tmpl", size: 7806, mode: os.FileMode(420), modTime: time.Unix(1, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -1086,7 +1086,7 @@ func templateRuntimeTmpl() (*asset, error) { return a, nil } -var _templateTxTmpl = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xac\x57\xdf\x6f\xdb\xbe\x11\x7f\x96\xfe\x8a\x9b\x11\x14\x52\xe0\x4a\x5d\xdf\x16\x20\x0f\x45\xda\x02\x05\xba\x14\x5b\x5d\xac\xc0\x30\xb4\xb4\x78\xb2\x89\xca\xa4\x46\x51\x8e\x3c\xc3\xff\xfb\x70\x47\x52\x92\x63\xb7\xeb\x80\x6f\x1e\xda\x88\xf7\x83\xf7\xe3\x73\x9f\x63\x8e\xc7\xf2\x36\x7d\x30\xed\xc1\xaa\xcd\xd6\xc1\xeb\x57\x7f\xfe\xcb\xcb\xd6\x62\x87\xda\xc1\x7b\x51\xe1\xda\x98\x1f\xf0\x41\x57\x05\xbc\x69\x1a\x60\xa5\x0e\x48\x6e\xf7\x28\x8b\x74\xb5\x55\x1d\x74\xa6\xb7\x15\x42\x65\x24\x82\xea\xa0\x51\x15\xea\x0e\x25\xf4\x5a\xa2\x05\xb7\x45\x78\xd3\x8a\x6a\x8b\xf0\xba\x78\x15\xa5\x50\x9b\x5e\xcb\x54\x69\x96\x7f\xfc\xf0\xf0\xee\xf1\xf3\x3b\xa8\x55\x83\x10\xce\xac\x31\x0e\xa4\xb2\x58\x39\x63\x0f\x60\x6a\x70\xb3\xcb\x9c\x45\x2c\xd2\xdb\xf2\x74\x4a\xd3\xe3\x11\x24\xd6\x4a\x23\x2c\xdc\xb0\x80\x70\xe4\x70\xd7\x36\xc2\x21\x2c\xb6\x28\x24\xda\x05\xdc\xb0\x48\xed\x5a\x63\x1d\x64\x69\xb2\xa8\x8c\x76\x38\xb8\x45\x9a\x2c\xba\x83\xae\x16\x69\x9a\x2c\x36\xca\x6d\xfb\x75\x51\x99\x5d\x59\x87\xfc\x95\xae\xfa\xb5\x70\xc6\x96\xa8\x5d\x29\x95\x68\xb0\x72\x8b\x34\x4f\xd3\xb2\x84\xd5\x40\x39\x0b\x70\x56\xe8\x4e\x54\x4e\x19\x2d\x1a\xa8\x1a\x45\x15\x74\x5b\xe1\x48\x5c\x59\x14\x0e\x25\xac\x0f\x50\x89\xa6\x51\x7a\x03\x0f\xac\x51\xac\x86\x2c\x2f\x52\x77\x68\x91\x3c\x75\xce\xf6\x95\x83\x63\x9a\x54\x46\xd7\x6a\x93\x26\xc7\x23\x58\xa1\x37\x08\x37\xdf\x96\x70\xa3\xe1\xee\x1e\x6e\x8a\x47\x23\xb1\x83\x97\xa7\x53\x9a\x24\x65\x09\xc7\x23\xdc\xe8\xe2\x51\xec\x10\x4e\x27\xba\x8e\xca\x17\x22\xa8\x8d\x05\xa5\x1d\x5a\x0a\x4d\x6f\xe0\x49\xb9\x2d\xcb\xcf\x8d\xd6\xbd\x6a\x24\xda\xae\x48\x93\xe4\x5c\x72\x7b\xf6\xe9\xa3\xe6\xb0\x50\x4b\xae\x27\x45\xd0\x88\xff\xa8\xe6\x00\x8d\x11\x92\x50\x91\x84\xcb\xe9\xe7\x36\x9a\xf8\xb3\x4f\xba\x42\xa0\x62\x17\xf4\x9b\xb7\xae\xcc\xae\x6d\x90\x2a\xc7\xd5\x59\x8b\xea\x07\x05\xb2\xeb\x21\xfe\xb0\xc1\x5f\x7b\x87\x43\x9a\x18\xfd\x60\x76\x3b\x45\xde\xff\xf9\xaf\xba\xd7\x55\x86\xd6\x1a\x9b\x93\xe4\xef\xc6\x9b\x3f\x93\x30\x20\x5e\xc6\x42\x92\x84\xea\xd8\xa8\xce\xc1\xc2\x3b\x5b\xc0\x22\xda\x32\x80\x12\xd2\xbf\x31\xfa\x7d\xaf\xab\x8e\x94\x5b\xab\xb4\x83\x85\xd1\x8b\xe0\x80\x94\x42\xed\xc3\x37\xfd\xde\x98\x27\xb4\xe3\x89\xef\xc4\x0c\x19\x45\x9a\xb0\x28\x73\x03\xdc\xae\x86\x7c\x6e\x9e\xe5\xc0\xe1\x52\xf7\x13\xb4\x96\x6e\x75\x43\xe1\x81\x50\x48\xab\xf6\x68\x8b\xec\xd6\x0d\x6f\xf9\xd7\xbc\x70\x43\x71\x66\x9e\x26\x89\x1b\x8a\x5d\x5f\x7c\x34\xd5\x0f\xfe\x94\x58\xd3\x00\xf2\xe1\x17\xdd\xc4\x63\xc2\xc4\xb7\x25\xd4\x74\x83\xaf\x49\x70\x15\xf3\xa5\x5c\xd2\x24\x49\x6a\x2a\x20\x59\x10\xd0\x2c\xba\xde\x6a\x8a\x31\x4d\x42\xdb\x3f\xe9\x79\xfa\x42\x4a\x1a\x04\xfa\xe4\x5e\x3a\xc3\xed\x04\xa3\x2f\x2b\x73\x51\x88\x33\x57\x59\x0d\xb3\xf6\xe5\x1c\xcc\xef\xa5\x76\x99\xc8\x3d\x88\xb6\x45\x2d\xb3\x0b\xd1\x12\xea\x9c\x52\xa1\x56\x47\x30\x97\x65\x18\x4c\xf0\xe9\x52\x42\x0f\xb3\x59\x5e\x2b\x2d\x3b\xce\xac\xb7\x96\x4f\xe7\xcd\x3d\x4f\xc9\xdb\x65\x79\x1c\x01\x4a\x83\x1a\x3a\xce\x41\xf1\xd6\x64\x9c\xe7\x98\x61\x98\x9b\x7b\x78\xe1\x4d\x8e\xbe\xfb\x77\x13\x10\x4e\x73\xc5\x42\x69\xe5\x28\xef\x53\x9e\xc6\xfe\x8c\x42\x42\xfd\x79\x40\x5e\x9b\xee\xfa\x1f\xac\x42\x23\x17\x4b\x39\x31\xc1\x3d\x3c\xe2\xd3\x15\x36\xc8\xc6\xe0\xf2\x91\x18\xc8\x0b\x4f\x5d\x79\x0b\xb5\xb2\x9d\x03\x4d\xdb\x81\x90\x27\x4d\x05\x38\x08\x1a\x79\x60\xfe\xe6\x51\xf3\x4a\x77\xf7\xa0\xb4\xc4\x61\x8c\xe6\x55\xec\x4a\x84\x3d\x3c\x59\xd1\xfa\xb9\xda\xa8\x3d\x6a\x08\x74\x5c\xac\x06\xcf\x6d\x02\xb4\x69\xc7\xd3\x60\xa4\xe8\xb6\x1d\x6a\x27\x7c\xa3\x88\xb7\xb7\x08\x4a\xa2\x60\xbe\x34\xd0\xf5\x2d\xaf\x85\x59\x3f\x3b\x76\x68\x7a\x47\xc8\x26\xee\x14\xfa\x00\x38\x38\x2b\xfc\xaa\x73\x86\xc3\x98\xa8\xb3\x2c\xe1\x1f\x5b\xd4\x20\xe2\x19\xe3\x9f\xdd\x87\xf1\x25\xb6\x5f\x82\x72\xb0\x41\xe7\x93\xe8\xa8\x92\xb3\x1c\x94\xee\x9c\x20\x6c\x30\x14\x3d\xd1\x09\x2d\x61\x64\x36\x61\x91\x33\xa4\x52\x92\x03\x26\x77\x5a\x39\x31\x0e\x56\x27\x49\xdf\xa1\x85\x5d\xdf\xb9\x38\x86\x48\x3e\x79\x8f\xe2\x8e\xb6\xac\xb1\xbc\x9f\x0d\x11\x30\xdd\x63\x2c\xd8\x78\xcd\x05\x71\x95\x25\x59\x7f\xa8\x41\x40\xd5\x18\x5a\xef\x33\x31\x15\x11\x77\x6b\x94\x12\x25\x7b\xd6\x18\x2e\x82\x0d\x6a\xb4\xbc\xfc\x50\x3b\xe5\x14\x76\xcb\x31\x42\x3e\x39\x90\x5f\xd1\xb6\x8d\x42\x9a\xb6\x7f\xf7\x68\x0f\x4b\x4e\x2f\xa0\xe4\xce\xb3\x24\x01\x24\x02\xaf\xf8\x1b\x69\x7d\xfd\xfa\x95\xca\x49\x9e\xd8\x0a\x9e\x54\xd3\xc0\x1a\x01\x07\xac\x7a\x87\x92\x81\xb3\xb5\xa6\xdf\xf8\x9d\x27\x03\x84\xb6\xaa\xda\x8e\x3b\x99\x5f\x15\x57\x52\x7d\x34\x0e\xfd\xd0\x8f\xd8\x53\x1d\x68\xe3\x60\x63\xac\xe9\x1d\xbd\x37\x3a\x51\x63\xd8\xde\xa3\xd2\xb4\xc3\xf9\xf6\xe9\x56\x84\xce\x09\xeb\xaf\x3c\x2b\x2e\xd4\xd6\xec\x8a\x34\x91\x76\xff\x0c\xb8\xde\xc7\x10\x77\x3a\x3f\xa8\x9a\x03\x61\xf1\x7c\xa9\xb8\x61\x86\xa1\xd4\x4f\x8c\xc6\xa7\xd5\x10\xb2\xa4\xc2\x6a\x7c\x7a\xf6\x48\x09\xa0\xf4\x2c\xc1\xea\x59\xe5\x06\x08\xaf\xa2\xe2\xc1\xff\xbf\x84\xcb\xb8\x72\x98\x16\xd1\xd2\xaf\xad\xdc\xd3\x1b\x7f\xd1\x1c\x4b\xbb\x2f\xbc\xc3\x3c\x4d\x54\xcd\xc7\x7f\xba\x07\xad\x1a\x26\xbb\xc0\x56\x5a\x35\xcb\xb8\x52\xe2\xd9\x8b\xe8\xf9\xe8\x06\x62\x3e\x0e\xe0\x8e\xfe\x39\x2d\xc9\x20\xe4\xb7\x1a\x46\x8e\x7e\x5e\x4f\xe2\x88\x16\x2d\x31\x5f\x8c\xd7\x19\x10\x7b\xa3\x64\x9c\x29\x63\xa7\x91\xe2\x29\x25\x97\xd4\x87\xeb\x43\x55\xc0\xe7\xad\xe9\x1b\x49\xe8\x22\x75\x94\x60\x74\x73\xa0\x07\xdd\x75\xfd\x19\xf5\x4e\x41\x50\x3d\xce\x8b\x9b\x43\x36\x35\x6e\xaa\x24\x8c\x64\xce\x19\x83\xcf\xf8\xad\xd7\x3c\x4b\x3b\x58\xc7\x69\xfb\x5d\xac\x5d\x8b\x2e\xb8\xcf\x72\x82\x30\x61\x6c\x16\x46\x41\xed\x9c\x14\xe2\xa6\x34\x1d\xfa\xd7\x2f\x31\x12\xd3\x42\x74\x3d\xf3\xcb\x6a\xd3\xeb\x06\xa6\xd6\x47\x3f\xbe\x25\x93\x23\xff\xfd\x53\x86\x63\x6e\xfc\x72\xce\x6e\xdf\x57\x43\xe1\xfd\x7c\xbf\x46\x6d\x17\x74\x76\x19\x25\x2b\xfe\x2a\xcc\x11\x2f\x63\xa0\x23\x5b\xfe\xdf\xa1\x46\x5f\xe7\xc1\xfe\x9c\x7d\x2f\xc2\x8d\x0e\x7e\x15\xf0\xbb\x01\xab\xb8\x82\x86\x82\xbe\xae\x37\x9e\x24\xd7\x27\xdf\xd3\xaa\x87\xc3\x12\x84\xdd\x74\x4b\xd8\xfb\x2c\xe9\x8f\xa3\xe3\x69\xf6\x66\x9d\xb0\x12\x2e\x23\x97\xcb\xc8\xe7\xc1\x36\x0f\xc3\xcb\xfc\x3d\xc5\xc6\x9f\xd7\x83\x63\xd1\x1f\x1c\xdd\xe8\xf3\x6a\x78\x7b\x61\xe1\xdb\xf3\x17\xc4\xfd\xbc\xfa\x99\x56\x4d\xce\x7f\x64\x86\x77\xe3\x7f\x03\x00\x00\xff\xff\x9d\x65\x7e\xf5\x46\x0f\x00\x00") +var _templateTxTmpl = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xac\x58\xdf\x8f\xdb\xb8\x11\x7e\x96\xfe\x8a\xa9\xb1\x38\x58\x0b\xaf\x94\xbb\xb7\xee\xc1\x05\x82\x4d\x82\x06\xb8\xee\xa1\xbd\x3d\x34\x40\x10\xdc\xd1\xe2\xc8\x26\x56\x26\x55\x8a\xda\x95\x6b\xf8\x7f\x2f\x66\x48\xea\x87\xed\xe4\x5a\xa0\x79\x48\x64\x72\x38\xfc\x66\xe6\xe3\xf0\x63\x8e\xc7\xe2\x36\x7d\x30\xcd\xc1\xaa\xed\xce\xc1\x0f\x6f\xbe\xff\xf3\x5d\x63\xb1\x45\xed\xe0\x83\x28\x71\x63\xcc\x33\x7c\xd4\x65\x0e\x6f\xeb\x1a\xd8\xa8\x05\x9a\xb7\x2f\x28\xf3\xf4\x69\xa7\x5a\x68\x4d\x67\x4b\x84\xd2\x48\x04\xd5\x42\xad\x4a\xd4\x2d\x4a\xe8\xb4\x44\x0b\x6e\x87\xf0\xb6\x11\xe5\x0e\xe1\x87\xfc\x4d\x9c\x85\xca\x74\x5a\xa6\x4a\xf3\xfc\x4f\x1f\x1f\xde\x3f\xfe\xf2\x1e\x2a\x55\x23\x84\x31\x6b\x8c\x03\xa9\x2c\x96\xce\xd8\x03\x98\x0a\xdc\x64\x33\x67\x11\xf3\xf4\xb6\x38\x9d\xd2\xf4\x78\x04\x89\x95\xd2\x08\x0b\xd7\x2f\x20\x0c\x39\xdc\x37\xb5\x70\x08\x8b\x1d\x0a\x89\x76\x01\x37\x3c\xa5\xf6\x8d\xb1\x0e\x96\x69\xb2\x28\x8d\x76\xd8\xbb\x45\x9a\x2c\xda\x83\x2e\x17\x69\x9a\x2c\xb6\xca\xed\xba\x4d\x5e\x9a\x7d\x51\x85\xf8\x95\x2e\xbb\x8d\x70\xc6\x16\xa8\x5d\x21\x95\xa8\xb1\x74\x8b\x34\x4b\xd3\xa2\x80\xa7\x9e\x62\x16\xe0\xac\xd0\xad\x28\x9d\x32\x5a\xd4\x50\xd6\x8a\x32\xe8\x76\xc2\xd1\x74\x69\x51\x38\x94\xb0\x39\x40\x29\xea\x5a\xe9\x2d\x3c\xb0\x45\xfe\xd4\x2f\xb3\x3c\x75\x87\x06\xc9\x53\xeb\x6c\x57\x3a\x38\xa6\x49\x69\x74\xa5\xb6\x69\x72\x3c\x82\x15\x7a\x8b\x70\xf3\xdb\x0a\x6e\x34\xdc\xaf\xe1\x26\x7f\x34\x12\x5b\xb8\x3b\x9d\xd2\x24\x29\x0a\x38\x1e\xe1\x46\xe7\x8f\x62\x8f\x70\x3a\xd1\x76\x94\xbe\x80\xa0\x32\x16\x94\x76\x68\x09\x9a\xde\xc2\xab\x72\x3b\x9e\x9f\x2f\xda\x74\xaa\x96\x68\xdb\x3c\x4d\x92\xf9\xcc\xed\xec\xa7\x47\xcd\xb0\x50\x4b\xce\x27\x21\xa8\xc5\xbf\x55\x7d\x80\xda\x08\x49\xac\x48\xc2\xe6\xf4\xe7\x36\x2e\xf1\x63\x3f\xeb\x12\x81\x92\x9d\xd3\x97\x5f\x5d\x9a\x7d\x53\x23\x65\x8e\xb3\xb3\x11\xe5\x33\x01\xd9\x77\x10\xff\xf0\x82\xbf\x75\x0e\xfb\x34\x31\xfa\xc1\xec\xf7\x8a\xbc\x7f\xfe\xe2\x3f\xff\x6a\xcc\x33\x4d\xfc\xc3\xf8\xd5\xf0\xf9\x4b\xfc\xe4\x29\xbf\x8b\xeb\xa1\x56\x2f\xd8\x72\x4e\x28\x05\xb5\xaa\xd0\xf3\x0a\xa7\xe5\xcb\xe1\x23\x15\x8d\x17\xd1\x54\x4b\xa1\x07\xaa\x40\xd7\xfa\x32\xd2\x04\xf3\xbb\x3e\x50\x5a\x4b\xa3\x35\xfa\xd5\x69\x42\x3b\x05\xfb\xfc\xc1\xff\x9b\x7a\x4e\xde\x54\x9d\x2e\x5b\x2a\xa2\x54\xa5\x83\x85\x87\xbf\x88\x1f\x8e\x58\xba\x88\xd0\x27\x9f\x34\x7e\x3a\xa5\x23\x19\xc8\x0f\xb9\x79\xc6\x43\x1b\xbd\x12\x1d\x68\x0f\x45\xbc\xa5\xc9\x2d\xba\x38\xe7\x17\x90\x05\x53\x6d\x39\x12\xc7\x5b\x8f\xbc\x61\xb2\xf0\x18\x93\xf7\xd5\x8a\xa6\x1d\x08\x33\x18\xef\xd1\xed\x8c\x8c\x6c\x19\x7d\x0c\x8b\x8f\x69\xe2\xe7\xc2\xc6\xcb\xb3\x84\xac\xe0\xf6\xa9\xcf\x00\xad\x35\x36\x4d\x12\xe2\x11\x21\x7a\x0a\x1b\x85\x55\x1f\xe8\x1f\x46\x4c\x67\x4c\x83\x90\xa2\x71\xd4\x53\x0c\x88\xba\x36\xaf\xbe\x0c\x2d\x57\xd1\x58\xa9\xb4\xb0\x07\xef\x88\x1c\x30\xa3\x04\x1d\xce\x29\xc8\x1c\x3e\x56\x50\xf9\x43\x3b\x58\x0d\xc7\x42\x34\x8d\x35\x8d\x55\xc2\xa1\x77\xd4\xaa\xad\x16\xae\xb3\xb8\x3a\x07\xb6\xac\x32\xef\x65\x96\x02\xce\x1a\xd1\xb8\x85\x2a\xe6\x67\x1a\x0d\x7d\x7f\x33\x19\x63\x69\xc2\x32\xa2\x70\x68\x70\xbe\x12\x8b\xe3\x11\x6a\xf3\x8a\x76\x30\x81\xbd\x92\xb2\xc6\x57\x61\x71\x91\xc3\xdb\x31\x2e\x46\xb3\x45\x77\x0e\xd3\x6f\x22\xb4\x04\x8b\xae\xb3\xfa\x32\x49\x1f\x8c\x05\xec\x05\x9d\xcc\x7b\xb6\xe6\xbf\x92\x1d\x81\xb9\x5f\xfb\x30\x34\x9d\x08\xea\x62\xd3\xa5\xd9\xc5\x08\x93\xa1\x28\x92\xc4\xef\x35\xcc\xcf\x72\x79\x35\x2d\xae\x87\x5b\xdf\x25\x43\x76\x06\x57\x04\xff\x9d\x81\xd6\xec\x11\x5a\xd7\x55\x15\x6c\xb0\x32\x16\xf3\x38\xaf\x2a\x5a\x41\x50\x09\x65\x3e\xa3\xa2\xeb\xc9\x75\xf6\x23\x5b\xfc\x69\x0d\x5a\xd5\xa3\xe3\x01\xa5\xb5\x71\xe8\xf4\xd5\x4d\x45\xe5\xd0\x0e\x7b\x86\x95\x5a\xd5\x61\xe4\x94\xf9\x8f\x53\xcc\xe0\x79\x59\x39\xec\x59\xae\xb2\xb3\x3a\x65\xbe\x7b\x4d\x16\x46\x76\xf9\x38\xf6\x59\x9e\x26\x3c\xb3\xac\xce\x19\x9a\xc1\x59\xd8\xe7\x6d\xc9\x67\x78\x9e\xdd\x10\x44\x35\xa4\x29\xe5\xd3\x79\x3c\xde\xc1\x8d\xd1\x1f\x62\x03\x6b\xac\xd2\x0e\x16\x46\x2f\x26\xcd\xe5\x0c\xe8\x05\x4f\xdb\x8b\x5e\x1b\xb1\x47\x1c\x53\xc0\x53\x54\xae\x7f\x67\xd5\x0b\x72\x41\x5d\x9f\xfb\xeb\x31\x97\x3c\x96\x2f\x6f\xe3\x34\xe5\xfb\x45\x58\xa8\xf4\xfc\x54\xae\x2f\x0f\xef\x1f\x9c\x43\xdf\xc1\x42\x32\xa2\xfb\xdc\xf5\x33\x26\xd1\x76\x5c\x63\xd7\xe7\xfb\x2e\xff\xc9\x94\xcf\x3c\x46\xa7\x84\xb3\x24\x9a\x06\xb5\x5c\x7e\xfe\x72\x56\xf8\xa5\x56\x75\x46\xd9\x65\x77\x31\xad\xa7\x53\x9e\xe7\xa3\xbb\x5f\x75\x1d\x1d\xf2\x35\x4e\x0e\x6b\xd4\x4b\xf6\x9e\xc1\x1d\x7c\xff\x23\x28\xf8\xcb\x1a\xde\xfc\x08\xea\xee\xce\x23\xae\x34\xac\x81\x2d\x3e\xab\x2f\xcb\x4a\x33\xc4\x49\x59\xf5\x2c\x00\x4a\xe5\xac\xcc\x45\x01\x3f\xeb\x69\x0d\x85\x94\xd4\x1c\xf8\xdc\x3b\xc3\xdc\x03\xa3\x2f\x4b\x7b\x51\xc9\x99\x9b\x39\x37\x29\x03\x59\x28\xeb\x2c\x6f\x12\x2b\xea\xee\xe7\xd1\x5f\xa4\x09\x86\xcc\x5e\x4c\xad\xa0\xe2\x50\x88\xaf\x51\xa2\x14\x45\x90\x5b\x93\x6e\xf7\x30\x51\x68\x1b\xa5\x65\xcb\xd1\x75\xd6\xf2\xe8\x94\xa1\xf3\xb0\xfc\xba\x65\x16\x85\x0d\x85\x41\x59\x1c\xd4\x4d\xfe\xce\x78\x72\x0d\x11\x06\x35\xb4\x86\xef\xfc\x92\xa3\x67\xef\xfd\x48\xe4\xd3\xd4\x30\x57\x5a\x39\x8a\x9b\x98\x35\x10\x30\x4c\x92\x90\x98\x03\xf2\xd6\xb4\xd7\x1f\x68\x45\x12\x52\x31\x95\xa3\xbe\x5b\xc3\x23\xbe\x5e\xd1\x78\xcb\x01\x5c\x36\xc8\x3d\xf2\xc2\x42\xa6\xb8\x85\x4a\xd9\xd6\x81\x26\xcd\x4f\xe4\x94\xa6\x8c\xd7\x05\xb0\x2a\xe7\x7e\xe1\x8d\xee\xd7\xa0\xb4\xc4\x7e\x40\xf3\x26\x56\x65\x38\xd5\xa3\xcc\xd8\xaa\x17\xd4\x10\x44\x76\xfe\xd4\xfb\xab\x59\x80\x36\xcd\x30\x1a\x16\x29\xda\x6d\x8f\xda\x09\x5f\xa8\xa0\x1f\x94\x44\xc1\x6a\xc6\x40\xdb\x35\x2c\xf6\x27\xf5\x6c\xd9\xa1\xe9\x1c\x31\x9b\xa4\x9b\xd0\x07\xc0\xde\x59\xe1\x1f\x30\xce\x30\x8c\x51\x10\x17\x05\xfc\x73\x87\x1a\x44\x1c\x0b\xfd\xd7\x19\x08\xed\x87\x34\xfc\x0a\x54\xb8\x68\x07\xc9\x38\x89\x41\xe9\xd6\x09\xe2\x06\x53\xd1\xcb\x57\xba\x7c\x07\xc1\x2a\x2c\x72\x84\x51\x9a\xb2\x90\xa2\x87\x44\xc4\xc1\xe6\x41\xed\x58\xd8\x77\xad\x8b\x47\x11\xc9\xa7\x57\xb1\x7b\x7a\x3b\x19\x2b\xbd\x42\x2a\xfd\x3e\xc6\x82\x8d\xdb\x5c\x74\xdf\xa2\xa0\xd5\x1f\x2b\x10\x50\xd6\x86\xb4\xed\x64\x9a\x92\x88\xfb\x0d\x4a\x89\x92\x3d\xeb\x41\x2e\x6f\x51\xa3\xe5\x27\x0d\x6a\xa7\x9c\xc2\x76\x35\x20\xe4\x91\x03\xf9\x15\x4d\x53\x2b\xa4\xd3\xf6\xaf\x0e\xed\x61\xc5\xe1\x45\x51\xe1\x1b\x02\x11\x24\x12\x2f\xff\x3b\x59\x7d\xfa\xf4\x89\xd2\x49\x9e\x78\x15\xbc\xaa\xba\x86\x0d\x02\xf6\x58\x76\x0e\x25\x13\x67\x67\x4d\xb7\xf5\x92\x4d\x06\x0a\xed\x54\xb9\x1b\x5e\x5a\xfc\x56\xbc\x12\xea\xa3\x71\x41\xd9\x0e\xdc\x53\x2d\x68\xe3\x60\x6b\xac\xe9\x1c\xbd\x22\x5b\x51\x61\x78\x93\x0d\x46\xe3\xcb\x2c\xbc\x0a\xe2\xae\xa4\x04\x84\xf5\x5b\xce\x92\x0b\x95\x35\xfb\x3c\x4d\xa4\x7d\x39\x23\xae\xf7\xd1\x47\xc5\x3d\x79\x46\xcc\x6f\x46\xd7\x4f\x38\x94\xfa\x13\xa3\xf1\xf5\xa9\x0f\x51\x52\x62\x35\xbe\x9e\x3d\x3d\x03\x29\x7d\x97\x60\xf3\xeb\x37\xff\x25\xae\x0c\xc6\x8b\x74\xe5\x6f\xc1\xcc\xb7\xb7\x55\xd4\x52\xd2\xbe\xe4\xde\x61\x96\x46\x89\x35\x11\x50\xa3\x00\x5a\x79\xfd\x74\x1a\x3a\xd8\x77\xd1\xf3\xd1\xf5\xd4\xf9\x18\xc0\x3d\xfd\x75\x5a\xb1\x62\x3a\xc5\xe7\x74\xec\xd1\xe7\xf9\xa4\x1e\xd1\xa0\xa5\xce\x17\xf1\xd2\x3b\xe0\xc5\x28\x19\xcf\x94\xb1\xe3\x91\xe2\x53\x4a\x2e\xa9\x0e\xd7\x0f\x55\x0e\xbf\xec\x4c\x57\x4b\x62\x17\x99\xa3\x04\xa3\xeb\x43\x7c\xdf\x5d\xda\x4f\x5a\xef\x08\x82\xf2\x31\x4f\x6e\x06\xcb\xb1\x70\x63\x26\x61\x68\xe6\x1c\x31\xf8\x88\xdf\x79\xcb\x59\xd8\x61\x75\x3c\x6d\xff\x2d\xd7\xae\xa1\x0b\xee\x97\x19\x51\x98\x38\x36\x81\x91\x53\x39\x47\x83\x78\x53\x9a\xd6\xbf\xb7\xb8\x23\x71\x5b\x88\xae\x27\x7e\xd9\x6c\x94\x68\x30\x96\x3e\xfa\xf1\x25\x19\x1d\xf9\xdf\x5f\xed\x70\xdc\x1b\x7f\x9d\x77\xb7\xdf\x9f\xfa\xdc\xfb\xf9\xfd\x5a\x6b\xbb\x68\x67\x97\x28\xd9\xf0\x5b\x30\x07\xbe\x0c\x40\x87\x6e\xf9\x3f\x43\x8d\xbe\xe6\x60\xbf\xde\x7d\x2f\xe0\x46\x07\xdf\x02\xfc\xbe\xc7\x32\x5e\x41\x7d\x4e\xbf\xae\x17\x9e\x66\xae\x9f\x7c\xdf\x56\x3d\x1d\x56\x20\xec\xb6\x5d\xc1\xcb\xf8\x76\x3f\x9e\x26\x12\x78\xe4\x4a\xd8\xcc\x3f\x0b\x42\x3f\x0f\x6b\xb3\x70\x78\xb9\x7f\x8f\xd8\xf8\xe7\x75\x70\x3c\xf5\x7f\x46\x37\xf8\xbc\x0a\x8f\x5e\x05\xbf\x9d\x2b\x88\xf5\x34\xfb\xac\xc9\xf9\xbf\x69\x82\x6e\xfc\x4f\x00\x00\x00\xff\xff\x6e\x86\x3c\xbb\x1c\x15\x00\x00") func templateTxTmplBytes() ([]byte, error) { return bindataRead( @@ -1101,7 +1101,7 @@ func templateTxTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "template/tx.tmpl", size: 3910, mode: os.FileMode(420), modTime: time.Unix(1, 0)} + info := bindataFileInfo{name: "template/tx.tmpl", size: 5404, mode: os.FileMode(420), modTime: time.Unix(1, 0)} a := &asset{bytes: bytes, info: info} return a, nil } diff --git a/entc/gen/template/client.tmpl b/entc/gen/template/client.tmpl index 6a1ae4ce3..0a8141b0f 100644 --- a/entc/gen/template/client.tmpl +++ b/entc/gen/template/client.tmpl @@ -68,7 +68,8 @@ func Open(driverName, dataSourceName string, options ...Option) (*Client, error) } } -// Tx returns a new transactional client. +// Tx returns a new transactional client. The provided context +// is used until the transaction is committed or rolled back. func (c *Client) Tx(ctx context.Context) (*Tx, error) { if _, ok := c.driver.(*txDriver); ok { return nil, fmt.Errorf("{{ $pkg }}: cannot start a transaction within a transaction") @@ -79,6 +80,7 @@ func (c *Client) Tx(ctx context.Context) (*Tx, error) { } cfg := config{driver: tx, log: c.log, debug: c.debug, hooks: c.hooks} return &Tx{ + ctx: ctx, config: cfg, {{ range $_, $n := $.Nodes -}} {{ $n.Name }}: New{{ $n.Name }}Client(cfg), diff --git a/entc/gen/template/tx.tmpl b/entc/gen/template/tx.tmpl index 24dd6ed66..ecaac3503 100644 --- a/entc/gen/template/tx.tmpl +++ b/entc/gen/template/tx.tmpl @@ -29,25 +29,68 @@ type Tx struct { // completion callbacks. mu sync.Mutex - onCommit []func(error) - onRollback []func(error) + onCommit []CommitHook + onRollback []RollbackHook + + // ctx lives for the life of the transaction. It is + // the same context used by the underlying connection. + ctx context.Context } -{{- range $func := list "Commit" "Rollback" }} +{{ $funcs := dict "Commit" "Committer" "Rollback" "Rollbacker" }} +{{ range $func := keys $funcs }} + {{ $iface := get $funcs $func }} + type ( + // {{ $iface }} is the interface that wraps the {{ $iface }} method. + {{ $iface }} interface { + {{ $func }}(context.Context, *Tx) error + } + + // The {{ $func }}Func type is an adapter to allow the use of ordinary + // function as a {{ $iface }}. If f is a function with the appropriate + // signature, {{ $func }}Func(f) is a {{ $iface }} that calls f. + {{ $func }}Func func(context.Context, *Tx) error + + // {{ $func }}Hook defines the "{{ lower $func }} middleware". A function that gets a {{ $iface }} + // and returns a {{ $iface }}. For example: + // + // hook := func(next ent.{{ $iface }}) ent.{{ $iface }} { + // return ent.{{ $func }}Func(func(context.Context, tx *ent.Tx) error { + // // Do some stuff before. + // if err := next.{{ $func }}(ctx, tx); err != nil { + // return err + // } + // // Do some stuff after. + // return nil + // }) + // } + // + {{ $func }}Hook func({{ $iface }} ) {{ $iface }} + ) + + // {{ $func }} calls f(ctx, m). + func (f {{ $func }}Func) {{ $func }}(ctx context.Context, tx *Tx) error { + return f(ctx, tx) + } + {{- $onFuncs := print "on" $func }} // {{ $func }} {{ lower $func }}s the transaction. func (tx *Tx) {{ $func }}() error { - err := tx.config.driver.(*txDriver).tx.{{ $func }}() + txDriver := tx.config.driver.(*txDriver) + var fn {{ $iface }} = {{ $func }}Func(func(context.Context, *Tx) error { + return txDriver.tx.{{ $func }}() + }) tx.mu.Lock() - defer tx.mu.Unlock() - for _, f := range tx.{{ $onFuncs }} { - f(err) + hooks := append([]{{ $func }}Hook(nil), tx.{{ $onFuncs }}...) + tx.mu.Unlock() + for i := len(hooks) - 1; i >= 0; i-- { + fn = hooks[i](fn) } - return err + return fn.{{ $func }}(tx.ctx, tx) } - // On{{ $func }} adds a function to call on {{ lower $func }}. - func (tx *Tx) On{{ $func }}(f func(error)) { + // On{{ $func }} adds a hook to call on {{ lower $func }}. + func (tx *Tx) On{{ $func }}(f {{ $func }}Hook) { tx.mu.Lock() defer tx.mu.Unlock() tx.{{ $onFuncs }} = append(tx.{{ $onFuncs }}, f) diff --git a/entc/integration/config/ent/client.go b/entc/integration/config/ent/client.go index 4c25c2a02..42e47c543 100644 --- a/entc/integration/config/ent/client.go +++ b/entc/integration/config/ent/client.go @@ -58,7 +58,8 @@ func Open(driverName, dataSourceName string, options ...Option) (*Client, error) } } -// Tx returns a new transactional client. +// Tx returns a new transactional client. The provided context +// is used until the transaction is committed or rolled back. func (c *Client) Tx(ctx context.Context) (*Tx, error) { if _, ok := c.driver.(*txDriver); ok { return nil, fmt.Errorf("ent: cannot start a transaction within a transaction") @@ -69,6 +70,7 @@ func (c *Client) Tx(ctx context.Context) (*Tx, error) { } cfg := config{driver: tx, log: c.log, debug: c.debug, hooks: c.hooks} return &Tx{ + ctx: ctx, config: cfg, User: NewUserClient(cfg), }, nil diff --git a/entc/integration/config/ent/tx.go b/entc/integration/config/ent/tx.go index 62082b404..bd5cb64a3 100644 --- a/entc/integration/config/ent/tx.go +++ b/entc/integration/config/ent/tx.go @@ -25,41 +25,119 @@ type Tx struct { // completion callbacks. mu sync.Mutex - onCommit []func(error) - onRollback []func(error) + onCommit []CommitHook + onRollback []RollbackHook + + // ctx lives for the life of the transaction. It is + // the same context used by the underlying connection. + ctx context.Context +} + +type ( + // Committer is the interface that wraps the Committer method. + Committer interface { + Commit(context.Context, *Tx) error + } + + // The CommitFunc type is an adapter to allow the use of ordinary + // function as a Committer. If f is a function with the appropriate + // signature, CommitFunc(f) is a Committer that calls f. + CommitFunc func(context.Context, *Tx) error + + // CommitHook defines the "commit middleware". A function that gets a Committer + // and returns a Committer. For example: + // + // hook := func(next ent.Committer) ent.Committer { + // return ent.CommitFunc(func(context.Context, tx *ent.Tx) error { + // // Do some stuff before. + // if err := next.Commit(ctx, tx); err != nil { + // return err + // } + // // Do some stuff after. + // return nil + // }) + // } + // + CommitHook func(Committer) Committer +) + +// Commit calls f(ctx, m). +func (f CommitFunc) Commit(ctx context.Context, tx *Tx) error { + return f(ctx, tx) } // Commit commits the transaction. func (tx *Tx) Commit() error { - err := tx.config.driver.(*txDriver).tx.Commit() + txDriver := tx.config.driver.(*txDriver) + var fn Committer = CommitFunc(func(context.Context, *Tx) error { + return txDriver.tx.Commit() + }) tx.mu.Lock() - defer tx.mu.Unlock() - for _, f := range tx.onCommit { - f(err) + hooks := append([]CommitHook(nil), tx.onCommit...) + tx.mu.Unlock() + for i := len(hooks) - 1; i >= 0; i-- { + fn = hooks[i](fn) } - return err + return fn.Commit(tx.ctx, tx) } -// OnCommit adds a function to call on commit. -func (tx *Tx) OnCommit(f func(error)) { +// OnCommit adds a hook to call on commit. +func (tx *Tx) OnCommit(f CommitHook) { tx.mu.Lock() defer tx.mu.Unlock() tx.onCommit = append(tx.onCommit, f) } -// Rollback rollbacks the transaction. -func (tx *Tx) Rollback() error { - err := tx.config.driver.(*txDriver).tx.Rollback() - tx.mu.Lock() - defer tx.mu.Unlock() - for _, f := range tx.onRollback { - f(err) +type ( + // Rollbacker is the interface that wraps the Rollbacker method. + Rollbacker interface { + Rollback(context.Context, *Tx) error } - return err + + // The RollbackFunc type is an adapter to allow the use of ordinary + // function as a Rollbacker. If f is a function with the appropriate + // signature, RollbackFunc(f) is a Rollbacker that calls f. + RollbackFunc func(context.Context, *Tx) error + + // RollbackHook defines the "rollback middleware". A function that gets a Rollbacker + // and returns a Rollbacker. For example: + // + // hook := func(next ent.Rollbacker) ent.Rollbacker { + // return ent.RollbackFunc(func(context.Context, tx *ent.Tx) error { + // // Do some stuff before. + // if err := next.Rollback(ctx, tx); err != nil { + // return err + // } + // // Do some stuff after. + // return nil + // }) + // } + // + RollbackHook func(Rollbacker) Rollbacker +) + +// Rollback calls f(ctx, m). +func (f RollbackFunc) Rollback(ctx context.Context, tx *Tx) error { + return f(ctx, tx) } -// OnRollback adds a function to call on rollback. -func (tx *Tx) OnRollback(f func(error)) { +// Rollback rollbacks the transaction. +func (tx *Tx) Rollback() error { + txDriver := tx.config.driver.(*txDriver) + var fn Rollbacker = RollbackFunc(func(context.Context, *Tx) error { + return txDriver.tx.Rollback() + }) + tx.mu.Lock() + hooks := append([]RollbackHook(nil), tx.onRollback...) + tx.mu.Unlock() + for i := len(hooks) - 1; i >= 0; i-- { + fn = hooks[i](fn) + } + return fn.Rollback(tx.ctx, tx) +} + +// OnRollback adds a hook to call on rollback. +func (tx *Tx) OnRollback(f RollbackHook) { tx.mu.Lock() defer tx.mu.Unlock() tx.onRollback = append(tx.onRollback, f) diff --git a/entc/integration/customid/ent/client.go b/entc/integration/customid/ent/client.go index 17f8c8950..43d837a78 100644 --- a/entc/integration/customid/ent/client.go +++ b/entc/integration/customid/ent/client.go @@ -76,7 +76,8 @@ func Open(driverName, dataSourceName string, options ...Option) (*Client, error) } } -// Tx returns a new transactional client. +// Tx returns a new transactional client. The provided context +// is used until the transaction is committed or rolled back. func (c *Client) Tx(ctx context.Context) (*Tx, error) { if _, ok := c.driver.(*txDriver); ok { return nil, fmt.Errorf("ent: cannot start a transaction within a transaction") @@ -87,6 +88,7 @@ func (c *Client) Tx(ctx context.Context) (*Tx, error) { } cfg := config{driver: tx, log: c.log, debug: c.debug, hooks: c.hooks} return &Tx{ + ctx: ctx, config: cfg, Blob: NewBlobClient(cfg), Car: NewCarClient(cfg), diff --git a/entc/integration/customid/ent/tx.go b/entc/integration/customid/ent/tx.go index 1717d2258..4c8cdd8d8 100644 --- a/entc/integration/customid/ent/tx.go +++ b/entc/integration/customid/ent/tx.go @@ -33,41 +33,119 @@ type Tx struct { // completion callbacks. mu sync.Mutex - onCommit []func(error) - onRollback []func(error) + onCommit []CommitHook + onRollback []RollbackHook + + // ctx lives for the life of the transaction. It is + // the same context used by the underlying connection. + ctx context.Context +} + +type ( + // Committer is the interface that wraps the Committer method. + Committer interface { + Commit(context.Context, *Tx) error + } + + // The CommitFunc type is an adapter to allow the use of ordinary + // function as a Committer. If f is a function with the appropriate + // signature, CommitFunc(f) is a Committer that calls f. + CommitFunc func(context.Context, *Tx) error + + // CommitHook defines the "commit middleware". A function that gets a Committer + // and returns a Committer. For example: + // + // hook := func(next ent.Committer) ent.Committer { + // return ent.CommitFunc(func(context.Context, tx *ent.Tx) error { + // // Do some stuff before. + // if err := next.Commit(ctx, tx); err != nil { + // return err + // } + // // Do some stuff after. + // return nil + // }) + // } + // + CommitHook func(Committer) Committer +) + +// Commit calls f(ctx, m). +func (f CommitFunc) Commit(ctx context.Context, tx *Tx) error { + return f(ctx, tx) } // Commit commits the transaction. func (tx *Tx) Commit() error { - err := tx.config.driver.(*txDriver).tx.Commit() + txDriver := tx.config.driver.(*txDriver) + var fn Committer = CommitFunc(func(context.Context, *Tx) error { + return txDriver.tx.Commit() + }) tx.mu.Lock() - defer tx.mu.Unlock() - for _, f := range tx.onCommit { - f(err) + hooks := append([]CommitHook(nil), tx.onCommit...) + tx.mu.Unlock() + for i := len(hooks) - 1; i >= 0; i-- { + fn = hooks[i](fn) } - return err + return fn.Commit(tx.ctx, tx) } -// OnCommit adds a function to call on commit. -func (tx *Tx) OnCommit(f func(error)) { +// OnCommit adds a hook to call on commit. +func (tx *Tx) OnCommit(f CommitHook) { tx.mu.Lock() defer tx.mu.Unlock() tx.onCommit = append(tx.onCommit, f) } -// Rollback rollbacks the transaction. -func (tx *Tx) Rollback() error { - err := tx.config.driver.(*txDriver).tx.Rollback() - tx.mu.Lock() - defer tx.mu.Unlock() - for _, f := range tx.onRollback { - f(err) +type ( + // Rollbacker is the interface that wraps the Rollbacker method. + Rollbacker interface { + Rollback(context.Context, *Tx) error } - return err + + // The RollbackFunc type is an adapter to allow the use of ordinary + // function as a Rollbacker. If f is a function with the appropriate + // signature, RollbackFunc(f) is a Rollbacker that calls f. + RollbackFunc func(context.Context, *Tx) error + + // RollbackHook defines the "rollback middleware". A function that gets a Rollbacker + // and returns a Rollbacker. For example: + // + // hook := func(next ent.Rollbacker) ent.Rollbacker { + // return ent.RollbackFunc(func(context.Context, tx *ent.Tx) error { + // // Do some stuff before. + // if err := next.Rollback(ctx, tx); err != nil { + // return err + // } + // // Do some stuff after. + // return nil + // }) + // } + // + RollbackHook func(Rollbacker) Rollbacker +) + +// Rollback calls f(ctx, m). +func (f RollbackFunc) Rollback(ctx context.Context, tx *Tx) error { + return f(ctx, tx) } -// OnRollback adds a function to call on rollback. -func (tx *Tx) OnRollback(f func(error)) { +// Rollback rollbacks the transaction. +func (tx *Tx) Rollback() error { + txDriver := tx.config.driver.(*txDriver) + var fn Rollbacker = RollbackFunc(func(context.Context, *Tx) error { + return txDriver.tx.Rollback() + }) + tx.mu.Lock() + hooks := append([]RollbackHook(nil), tx.onRollback...) + tx.mu.Unlock() + for i := len(hooks) - 1; i >= 0; i-- { + fn = hooks[i](fn) + } + return fn.Rollback(tx.ctx, tx) +} + +// OnRollback adds a hook to call on rollback. +func (tx *Tx) OnRollback(f RollbackHook) { tx.mu.Lock() defer tx.mu.Unlock() tx.onRollback = append(tx.onRollback, f) diff --git a/entc/integration/ent/client.go b/entc/integration/ent/client.go index ee60f913c..ab490d250 100644 --- a/entc/integration/ent/client.go +++ b/entc/integration/ent/client.go @@ -103,7 +103,8 @@ func Open(driverName, dataSourceName string, options ...Option) (*Client, error) } } -// Tx returns a new transactional client. +// Tx returns a new transactional client. The provided context +// is used until the transaction is committed or rolled back. func (c *Client) Tx(ctx context.Context) (*Tx, error) { if _, ok := c.driver.(*txDriver); ok { return nil, fmt.Errorf("ent: cannot start a transaction within a transaction") @@ -114,6 +115,7 @@ func (c *Client) Tx(ctx context.Context) (*Tx, error) { } cfg := config{driver: tx, log: c.log, debug: c.debug, hooks: c.hooks} return &Tx{ + ctx: ctx, config: cfg, Card: NewCardClient(cfg), Comment: NewCommentClient(cfg), diff --git a/entc/integration/ent/tx.go b/entc/integration/ent/tx.go index 55bfc55ac..f5a5551c5 100644 --- a/entc/integration/ent/tx.go +++ b/entc/integration/ent/tx.go @@ -47,41 +47,119 @@ type Tx struct { // completion callbacks. mu sync.Mutex - onCommit []func(error) - onRollback []func(error) + onCommit []CommitHook + onRollback []RollbackHook + + // ctx lives for the life of the transaction. It is + // the same context used by the underlying connection. + ctx context.Context +} + +type ( + // Committer is the interface that wraps the Committer method. + Committer interface { + Commit(context.Context, *Tx) error + } + + // The CommitFunc type is an adapter to allow the use of ordinary + // function as a Committer. If f is a function with the appropriate + // signature, CommitFunc(f) is a Committer that calls f. + CommitFunc func(context.Context, *Tx) error + + // CommitHook defines the "commit middleware". A function that gets a Committer + // and returns a Committer. For example: + // + // hook := func(next ent.Committer) ent.Committer { + // return ent.CommitFunc(func(context.Context, tx *ent.Tx) error { + // // Do some stuff before. + // if err := next.Commit(ctx, tx); err != nil { + // return err + // } + // // Do some stuff after. + // return nil + // }) + // } + // + CommitHook func(Committer) Committer +) + +// Commit calls f(ctx, m). +func (f CommitFunc) Commit(ctx context.Context, tx *Tx) error { + return f(ctx, tx) } // Commit commits the transaction. func (tx *Tx) Commit() error { - err := tx.config.driver.(*txDriver).tx.Commit() + txDriver := tx.config.driver.(*txDriver) + var fn Committer = CommitFunc(func(context.Context, *Tx) error { + return txDriver.tx.Commit() + }) tx.mu.Lock() - defer tx.mu.Unlock() - for _, f := range tx.onCommit { - f(err) + hooks := append([]CommitHook(nil), tx.onCommit...) + tx.mu.Unlock() + for i := len(hooks) - 1; i >= 0; i-- { + fn = hooks[i](fn) } - return err + return fn.Commit(tx.ctx, tx) } -// OnCommit adds a function to call on commit. -func (tx *Tx) OnCommit(f func(error)) { +// OnCommit adds a hook to call on commit. +func (tx *Tx) OnCommit(f CommitHook) { tx.mu.Lock() defer tx.mu.Unlock() tx.onCommit = append(tx.onCommit, f) } -// Rollback rollbacks the transaction. -func (tx *Tx) Rollback() error { - err := tx.config.driver.(*txDriver).tx.Rollback() - tx.mu.Lock() - defer tx.mu.Unlock() - for _, f := range tx.onRollback { - f(err) +type ( + // Rollbacker is the interface that wraps the Rollbacker method. + Rollbacker interface { + Rollback(context.Context, *Tx) error } - return err + + // The RollbackFunc type is an adapter to allow the use of ordinary + // function as a Rollbacker. If f is a function with the appropriate + // signature, RollbackFunc(f) is a Rollbacker that calls f. + RollbackFunc func(context.Context, *Tx) error + + // RollbackHook defines the "rollback middleware". A function that gets a Rollbacker + // and returns a Rollbacker. For example: + // + // hook := func(next ent.Rollbacker) ent.Rollbacker { + // return ent.RollbackFunc(func(context.Context, tx *ent.Tx) error { + // // Do some stuff before. + // if err := next.Rollback(ctx, tx); err != nil { + // return err + // } + // // Do some stuff after. + // return nil + // }) + // } + // + RollbackHook func(Rollbacker) Rollbacker +) + +// Rollback calls f(ctx, m). +func (f RollbackFunc) Rollback(ctx context.Context, tx *Tx) error { + return f(ctx, tx) } -// OnRollback adds a function to call on rollback. -func (tx *Tx) OnRollback(f func(error)) { +// Rollback rollbacks the transaction. +func (tx *Tx) Rollback() error { + txDriver := tx.config.driver.(*txDriver) + var fn Rollbacker = RollbackFunc(func(context.Context, *Tx) error { + return txDriver.tx.Rollback() + }) + tx.mu.Lock() + hooks := append([]RollbackHook(nil), tx.onRollback...) + tx.mu.Unlock() + for i := len(hooks) - 1; i >= 0; i-- { + fn = hooks[i](fn) + } + return fn.Rollback(tx.ctx, tx) +} + +// OnRollback adds a hook to call on rollback. +func (tx *Tx) OnRollback(f RollbackHook) { tx.mu.Lock() defer tx.mu.Unlock() tx.onRollback = append(tx.onRollback, f) diff --git a/entc/integration/gremlin/ent/client.go b/entc/integration/gremlin/ent/client.go index 754939422..372410bee 100644 --- a/entc/integration/gremlin/ent/client.go +++ b/entc/integration/gremlin/ent/client.go @@ -109,7 +109,8 @@ func Open(driverName, dataSourceName string, options ...Option) (*Client, error) } } -// Tx returns a new transactional client. +// Tx returns a new transactional client. The provided context +// is used until the transaction is committed or rolled back. func (c *Client) Tx(ctx context.Context) (*Tx, error) { if _, ok := c.driver.(*txDriver); ok { return nil, fmt.Errorf("ent: cannot start a transaction within a transaction") @@ -120,6 +121,7 @@ func (c *Client) Tx(ctx context.Context) (*Tx, error) { } cfg := config{driver: tx, log: c.log, debug: c.debug, hooks: c.hooks} return &Tx{ + ctx: ctx, config: cfg, Card: NewCardClient(cfg), Comment: NewCommentClient(cfg), diff --git a/entc/integration/gremlin/ent/tx.go b/entc/integration/gremlin/ent/tx.go index 55bfc55ac..f5a5551c5 100644 --- a/entc/integration/gremlin/ent/tx.go +++ b/entc/integration/gremlin/ent/tx.go @@ -47,41 +47,119 @@ type Tx struct { // completion callbacks. mu sync.Mutex - onCommit []func(error) - onRollback []func(error) + onCommit []CommitHook + onRollback []RollbackHook + + // ctx lives for the life of the transaction. It is + // the same context used by the underlying connection. + ctx context.Context +} + +type ( + // Committer is the interface that wraps the Committer method. + Committer interface { + Commit(context.Context, *Tx) error + } + + // The CommitFunc type is an adapter to allow the use of ordinary + // function as a Committer. If f is a function with the appropriate + // signature, CommitFunc(f) is a Committer that calls f. + CommitFunc func(context.Context, *Tx) error + + // CommitHook defines the "commit middleware". A function that gets a Committer + // and returns a Committer. For example: + // + // hook := func(next ent.Committer) ent.Committer { + // return ent.CommitFunc(func(context.Context, tx *ent.Tx) error { + // // Do some stuff before. + // if err := next.Commit(ctx, tx); err != nil { + // return err + // } + // // Do some stuff after. + // return nil + // }) + // } + // + CommitHook func(Committer) Committer +) + +// Commit calls f(ctx, m). +func (f CommitFunc) Commit(ctx context.Context, tx *Tx) error { + return f(ctx, tx) } // Commit commits the transaction. func (tx *Tx) Commit() error { - err := tx.config.driver.(*txDriver).tx.Commit() + txDriver := tx.config.driver.(*txDriver) + var fn Committer = CommitFunc(func(context.Context, *Tx) error { + return txDriver.tx.Commit() + }) tx.mu.Lock() - defer tx.mu.Unlock() - for _, f := range tx.onCommit { - f(err) + hooks := append([]CommitHook(nil), tx.onCommit...) + tx.mu.Unlock() + for i := len(hooks) - 1; i >= 0; i-- { + fn = hooks[i](fn) } - return err + return fn.Commit(tx.ctx, tx) } -// OnCommit adds a function to call on commit. -func (tx *Tx) OnCommit(f func(error)) { +// OnCommit adds a hook to call on commit. +func (tx *Tx) OnCommit(f CommitHook) { tx.mu.Lock() defer tx.mu.Unlock() tx.onCommit = append(tx.onCommit, f) } -// Rollback rollbacks the transaction. -func (tx *Tx) Rollback() error { - err := tx.config.driver.(*txDriver).tx.Rollback() - tx.mu.Lock() - defer tx.mu.Unlock() - for _, f := range tx.onRollback { - f(err) +type ( + // Rollbacker is the interface that wraps the Rollbacker method. + Rollbacker interface { + Rollback(context.Context, *Tx) error } - return err + + // The RollbackFunc type is an adapter to allow the use of ordinary + // function as a Rollbacker. If f is a function with the appropriate + // signature, RollbackFunc(f) is a Rollbacker that calls f. + RollbackFunc func(context.Context, *Tx) error + + // RollbackHook defines the "rollback middleware". A function that gets a Rollbacker + // and returns a Rollbacker. For example: + // + // hook := func(next ent.Rollbacker) ent.Rollbacker { + // return ent.RollbackFunc(func(context.Context, tx *ent.Tx) error { + // // Do some stuff before. + // if err := next.Rollback(ctx, tx); err != nil { + // return err + // } + // // Do some stuff after. + // return nil + // }) + // } + // + RollbackHook func(Rollbacker) Rollbacker +) + +// Rollback calls f(ctx, m). +func (f RollbackFunc) Rollback(ctx context.Context, tx *Tx) error { + return f(ctx, tx) } -// OnRollback adds a function to call on rollback. -func (tx *Tx) OnRollback(f func(error)) { +// Rollback rollbacks the transaction. +func (tx *Tx) Rollback() error { + txDriver := tx.config.driver.(*txDriver) + var fn Rollbacker = RollbackFunc(func(context.Context, *Tx) error { + return txDriver.tx.Rollback() + }) + tx.mu.Lock() + hooks := append([]RollbackHook(nil), tx.onRollback...) + tx.mu.Unlock() + for i := len(hooks) - 1; i >= 0; i-- { + fn = hooks[i](fn) + } + return fn.Rollback(tx.ctx, tx) +} + +// OnRollback adds a hook to call on rollback. +func (tx *Tx) OnRollback(f RollbackHook) { tx.mu.Lock() defer tx.mu.Unlock() tx.onRollback = append(tx.onRollback, f) diff --git a/entc/integration/hooks/ent/client.go b/entc/integration/hooks/ent/client.go index 4c8162ddb..b589860f8 100644 --- a/entc/integration/hooks/ent/client.go +++ b/entc/integration/hooks/ent/client.go @@ -63,7 +63,8 @@ func Open(driverName, dataSourceName string, options ...Option) (*Client, error) } } -// Tx returns a new transactional client. +// Tx returns a new transactional client. The provided context +// is used until the transaction is committed or rolled back. func (c *Client) Tx(ctx context.Context) (*Tx, error) { if _, ok := c.driver.(*txDriver); ok { return nil, fmt.Errorf("ent: cannot start a transaction within a transaction") @@ -74,6 +75,7 @@ func (c *Client) Tx(ctx context.Context) (*Tx, error) { } cfg := config{driver: tx, log: c.log, debug: c.debug, hooks: c.hooks} return &Tx{ + ctx: ctx, config: cfg, Card: NewCardClient(cfg), User: NewUserClient(cfg), diff --git a/entc/integration/hooks/ent/tx.go b/entc/integration/hooks/ent/tx.go index cf4d56e06..8661f18f2 100644 --- a/entc/integration/hooks/ent/tx.go +++ b/entc/integration/hooks/ent/tx.go @@ -27,41 +27,119 @@ type Tx struct { // completion callbacks. mu sync.Mutex - onCommit []func(error) - onRollback []func(error) + onCommit []CommitHook + onRollback []RollbackHook + + // ctx lives for the life of the transaction. It is + // the same context used by the underlying connection. + ctx context.Context +} + +type ( + // Committer is the interface that wraps the Committer method. + Committer interface { + Commit(context.Context, *Tx) error + } + + // The CommitFunc type is an adapter to allow the use of ordinary + // function as a Committer. If f is a function with the appropriate + // signature, CommitFunc(f) is a Committer that calls f. + CommitFunc func(context.Context, *Tx) error + + // CommitHook defines the "commit middleware". A function that gets a Committer + // and returns a Committer. For example: + // + // hook := func(next ent.Committer) ent.Committer { + // return ent.CommitFunc(func(context.Context, tx *ent.Tx) error { + // // Do some stuff before. + // if err := next.Commit(ctx, tx); err != nil { + // return err + // } + // // Do some stuff after. + // return nil + // }) + // } + // + CommitHook func(Committer) Committer +) + +// Commit calls f(ctx, m). +func (f CommitFunc) Commit(ctx context.Context, tx *Tx) error { + return f(ctx, tx) } // Commit commits the transaction. func (tx *Tx) Commit() error { - err := tx.config.driver.(*txDriver).tx.Commit() + txDriver := tx.config.driver.(*txDriver) + var fn Committer = CommitFunc(func(context.Context, *Tx) error { + return txDriver.tx.Commit() + }) tx.mu.Lock() - defer tx.mu.Unlock() - for _, f := range tx.onCommit { - f(err) + hooks := append([]CommitHook(nil), tx.onCommit...) + tx.mu.Unlock() + for i := len(hooks) - 1; i >= 0; i-- { + fn = hooks[i](fn) } - return err + return fn.Commit(tx.ctx, tx) } -// OnCommit adds a function to call on commit. -func (tx *Tx) OnCommit(f func(error)) { +// OnCommit adds a hook to call on commit. +func (tx *Tx) OnCommit(f CommitHook) { tx.mu.Lock() defer tx.mu.Unlock() tx.onCommit = append(tx.onCommit, f) } -// Rollback rollbacks the transaction. -func (tx *Tx) Rollback() error { - err := tx.config.driver.(*txDriver).tx.Rollback() - tx.mu.Lock() - defer tx.mu.Unlock() - for _, f := range tx.onRollback { - f(err) +type ( + // Rollbacker is the interface that wraps the Rollbacker method. + Rollbacker interface { + Rollback(context.Context, *Tx) error } - return err + + // The RollbackFunc type is an adapter to allow the use of ordinary + // function as a Rollbacker. If f is a function with the appropriate + // signature, RollbackFunc(f) is a Rollbacker that calls f. + RollbackFunc func(context.Context, *Tx) error + + // RollbackHook defines the "rollback middleware". A function that gets a Rollbacker + // and returns a Rollbacker. For example: + // + // hook := func(next ent.Rollbacker) ent.Rollbacker { + // return ent.RollbackFunc(func(context.Context, tx *ent.Tx) error { + // // Do some stuff before. + // if err := next.Rollback(ctx, tx); err != nil { + // return err + // } + // // Do some stuff after. + // return nil + // }) + // } + // + RollbackHook func(Rollbacker) Rollbacker +) + +// Rollback calls f(ctx, m). +func (f RollbackFunc) Rollback(ctx context.Context, tx *Tx) error { + return f(ctx, tx) } -// OnRollback adds a function to call on rollback. -func (tx *Tx) OnRollback(f func(error)) { +// Rollback rollbacks the transaction. +func (tx *Tx) Rollback() error { + txDriver := tx.config.driver.(*txDriver) + var fn Rollbacker = RollbackFunc(func(context.Context, *Tx) error { + return txDriver.tx.Rollback() + }) + tx.mu.Lock() + hooks := append([]RollbackHook(nil), tx.onRollback...) + tx.mu.Unlock() + for i := len(hooks) - 1; i >= 0; i-- { + fn = hooks[i](fn) + } + return fn.Rollback(tx.ctx, tx) +} + +// OnRollback adds a hook to call on rollback. +func (tx *Tx) OnRollback(f RollbackHook) { tx.mu.Lock() defer tx.mu.Unlock() tx.onRollback = append(tx.onRollback, f) diff --git a/entc/integration/idtype/ent/client.go b/entc/integration/idtype/ent/client.go index 157452290..5bb37d8bb 100644 --- a/entc/integration/idtype/ent/client.go +++ b/entc/integration/idtype/ent/client.go @@ -59,7 +59,8 @@ func Open(driverName, dataSourceName string, options ...Option) (*Client, error) } } -// Tx returns a new transactional client. +// Tx returns a new transactional client. The provided context +// is used until the transaction is committed or rolled back. func (c *Client) Tx(ctx context.Context) (*Tx, error) { if _, ok := c.driver.(*txDriver); ok { return nil, fmt.Errorf("ent: cannot start a transaction within a transaction") @@ -70,6 +71,7 @@ func (c *Client) Tx(ctx context.Context) (*Tx, error) { } cfg := config{driver: tx, log: c.log, debug: c.debug, hooks: c.hooks} return &Tx{ + ctx: ctx, config: cfg, User: NewUserClient(cfg), }, nil diff --git a/entc/integration/idtype/ent/tx.go b/entc/integration/idtype/ent/tx.go index 62082b404..bd5cb64a3 100644 --- a/entc/integration/idtype/ent/tx.go +++ b/entc/integration/idtype/ent/tx.go @@ -25,41 +25,119 @@ type Tx struct { // completion callbacks. mu sync.Mutex - onCommit []func(error) - onRollback []func(error) + onCommit []CommitHook + onRollback []RollbackHook + + // ctx lives for the life of the transaction. It is + // the same context used by the underlying connection. + ctx context.Context +} + +type ( + // Committer is the interface that wraps the Committer method. + Committer interface { + Commit(context.Context, *Tx) error + } + + // The CommitFunc type is an adapter to allow the use of ordinary + // function as a Committer. If f is a function with the appropriate + // signature, CommitFunc(f) is a Committer that calls f. + CommitFunc func(context.Context, *Tx) error + + // CommitHook defines the "commit middleware". A function that gets a Committer + // and returns a Committer. For example: + // + // hook := func(next ent.Committer) ent.Committer { + // return ent.CommitFunc(func(context.Context, tx *ent.Tx) error { + // // Do some stuff before. + // if err := next.Commit(ctx, tx); err != nil { + // return err + // } + // // Do some stuff after. + // return nil + // }) + // } + // + CommitHook func(Committer) Committer +) + +// Commit calls f(ctx, m). +func (f CommitFunc) Commit(ctx context.Context, tx *Tx) error { + return f(ctx, tx) } // Commit commits the transaction. func (tx *Tx) Commit() error { - err := tx.config.driver.(*txDriver).tx.Commit() + txDriver := tx.config.driver.(*txDriver) + var fn Committer = CommitFunc(func(context.Context, *Tx) error { + return txDriver.tx.Commit() + }) tx.mu.Lock() - defer tx.mu.Unlock() - for _, f := range tx.onCommit { - f(err) + hooks := append([]CommitHook(nil), tx.onCommit...) + tx.mu.Unlock() + for i := len(hooks) - 1; i >= 0; i-- { + fn = hooks[i](fn) } - return err + return fn.Commit(tx.ctx, tx) } -// OnCommit adds a function to call on commit. -func (tx *Tx) OnCommit(f func(error)) { +// OnCommit adds a hook to call on commit. +func (tx *Tx) OnCommit(f CommitHook) { tx.mu.Lock() defer tx.mu.Unlock() tx.onCommit = append(tx.onCommit, f) } -// Rollback rollbacks the transaction. -func (tx *Tx) Rollback() error { - err := tx.config.driver.(*txDriver).tx.Rollback() - tx.mu.Lock() - defer tx.mu.Unlock() - for _, f := range tx.onRollback { - f(err) +type ( + // Rollbacker is the interface that wraps the Rollbacker method. + Rollbacker interface { + Rollback(context.Context, *Tx) error } - return err + + // The RollbackFunc type is an adapter to allow the use of ordinary + // function as a Rollbacker. If f is a function with the appropriate + // signature, RollbackFunc(f) is a Rollbacker that calls f. + RollbackFunc func(context.Context, *Tx) error + + // RollbackHook defines the "rollback middleware". A function that gets a Rollbacker + // and returns a Rollbacker. For example: + // + // hook := func(next ent.Rollbacker) ent.Rollbacker { + // return ent.RollbackFunc(func(context.Context, tx *ent.Tx) error { + // // Do some stuff before. + // if err := next.Rollback(ctx, tx); err != nil { + // return err + // } + // // Do some stuff after. + // return nil + // }) + // } + // + RollbackHook func(Rollbacker) Rollbacker +) + +// Rollback calls f(ctx, m). +func (f RollbackFunc) Rollback(ctx context.Context, tx *Tx) error { + return f(ctx, tx) } -// OnRollback adds a function to call on rollback. -func (tx *Tx) OnRollback(f func(error)) { +// Rollback rollbacks the transaction. +func (tx *Tx) Rollback() error { + txDriver := tx.config.driver.(*txDriver) + var fn Rollbacker = RollbackFunc(func(context.Context, *Tx) error { + return txDriver.tx.Rollback() + }) + tx.mu.Lock() + hooks := append([]RollbackHook(nil), tx.onRollback...) + tx.mu.Unlock() + for i := len(hooks) - 1; i >= 0; i-- { + fn = hooks[i](fn) + } + return fn.Rollback(tx.ctx, tx) +} + +// OnRollback adds a hook to call on rollback. +func (tx *Tx) OnRollback(f RollbackHook) { tx.mu.Lock() defer tx.mu.Unlock() tx.onRollback = append(tx.onRollback, f) diff --git a/entc/integration/integration_test.go b/entc/integration/integration_test.go index 74e478dd6..cffbede57 100644 --- a/entc/integration/integration_test.go +++ b/entc/integration/integration_test.go @@ -797,6 +797,15 @@ type mocker struct{ mock.Mock } func (m *mocker) onCommit(err error) { m.Called(err) } func (m *mocker) onRollback(err error) { m.Called(err) } +func (m *mocker) rHook() ent.RollbackHook { + return func(next ent.Rollbacker) ent.Rollbacker { + return ent.RollbackFunc(func(ctx context.Context, tx *ent.Tx) error { + err := next.Rollback(ctx, tx) + m.onRollback(err) + return err + }) + } +} func Tx(t *testing.T, client *ent.Client) { ctx := context.Background() @@ -806,7 +815,7 @@ func Tx(t *testing.T, client *ent.Client) { var m mocker m.On("onRollback", nil).Once() defer m.AssertExpectations(t) - tx.OnRollback(m.onRollback) + tx.OnRollback(m.rHook()) tx.Node.Create().SaveX(ctx) require.NoError(t, tx.Rollback()) require.Zero(t, client.Node.Query().CountX(ctx), "rollback should discard all changes") @@ -817,7 +826,13 @@ func Tx(t *testing.T, client *ent.Client) { var m mocker m.On("onCommit", mock.Anything).Twice() defer m.AssertExpectations(t) - tx.OnCommit(m.onCommit) + tx.OnCommit(func(next ent.Committer) ent.Committer { + return ent.CommitFunc(func(ctx context.Context, tx *ent.Tx) error { + err := next.Commit(ctx, tx) + m.onCommit(err) + return err + }) + }) nde := tx.Node.Create().SaveX(ctx) require.NoError(t, tx.Commit()) require.Error(t, tx.Commit(), "should return an error on the second call") @@ -832,7 +847,7 @@ func Tx(t *testing.T, client *ent.Client) { var m mocker m.On("onRollback", nil).Once() defer m.AssertExpectations(t) - tx.OnRollback(m.onRollback) + tx.OnRollback(m.rHook()) _, err = tx.Client().Tx(ctx) require.Error(t, err, "cannot start a transaction within a transaction") require.NoError(t, tx.Rollback()) @@ -846,7 +861,7 @@ func Tx(t *testing.T, client *ent.Client) { var m mocker m.On("onRollback", nil).Once() defer m.AssertExpectations(t) - tx.OnRollback(m.onRollback) + tx.OnRollback(m.rHook()) _, err = tx.Item.Create().Save(ctx) require.Error(t, err) require.NoError(t, tx.Rollback()) diff --git a/entc/integration/json/ent/client.go b/entc/integration/json/ent/client.go index 2ad473667..d6f6e26bc 100644 --- a/entc/integration/json/ent/client.go +++ b/entc/integration/json/ent/client.go @@ -58,7 +58,8 @@ func Open(driverName, dataSourceName string, options ...Option) (*Client, error) } } -// Tx returns a new transactional client. +// Tx returns a new transactional client. The provided context +// is used until the transaction is committed or rolled back. func (c *Client) Tx(ctx context.Context) (*Tx, error) { if _, ok := c.driver.(*txDriver); ok { return nil, fmt.Errorf("ent: cannot start a transaction within a transaction") @@ -69,6 +70,7 @@ func (c *Client) Tx(ctx context.Context) (*Tx, error) { } cfg := config{driver: tx, log: c.log, debug: c.debug, hooks: c.hooks} return &Tx{ + ctx: ctx, config: cfg, User: NewUserClient(cfg), }, nil diff --git a/entc/integration/json/ent/tx.go b/entc/integration/json/ent/tx.go index 62082b404..bd5cb64a3 100644 --- a/entc/integration/json/ent/tx.go +++ b/entc/integration/json/ent/tx.go @@ -25,41 +25,119 @@ type Tx struct { // completion callbacks. mu sync.Mutex - onCommit []func(error) - onRollback []func(error) + onCommit []CommitHook + onRollback []RollbackHook + + // ctx lives for the life of the transaction. It is + // the same context used by the underlying connection. + ctx context.Context +} + +type ( + // Committer is the interface that wraps the Committer method. + Committer interface { + Commit(context.Context, *Tx) error + } + + // The CommitFunc type is an adapter to allow the use of ordinary + // function as a Committer. If f is a function with the appropriate + // signature, CommitFunc(f) is a Committer that calls f. + CommitFunc func(context.Context, *Tx) error + + // CommitHook defines the "commit middleware". A function that gets a Committer + // and returns a Committer. For example: + // + // hook := func(next ent.Committer) ent.Committer { + // return ent.CommitFunc(func(context.Context, tx *ent.Tx) error { + // // Do some stuff before. + // if err := next.Commit(ctx, tx); err != nil { + // return err + // } + // // Do some stuff after. + // return nil + // }) + // } + // + CommitHook func(Committer) Committer +) + +// Commit calls f(ctx, m). +func (f CommitFunc) Commit(ctx context.Context, tx *Tx) error { + return f(ctx, tx) } // Commit commits the transaction. func (tx *Tx) Commit() error { - err := tx.config.driver.(*txDriver).tx.Commit() + txDriver := tx.config.driver.(*txDriver) + var fn Committer = CommitFunc(func(context.Context, *Tx) error { + return txDriver.tx.Commit() + }) tx.mu.Lock() - defer tx.mu.Unlock() - for _, f := range tx.onCommit { - f(err) + hooks := append([]CommitHook(nil), tx.onCommit...) + tx.mu.Unlock() + for i := len(hooks) - 1; i >= 0; i-- { + fn = hooks[i](fn) } - return err + return fn.Commit(tx.ctx, tx) } -// OnCommit adds a function to call on commit. -func (tx *Tx) OnCommit(f func(error)) { +// OnCommit adds a hook to call on commit. +func (tx *Tx) OnCommit(f CommitHook) { tx.mu.Lock() defer tx.mu.Unlock() tx.onCommit = append(tx.onCommit, f) } -// Rollback rollbacks the transaction. -func (tx *Tx) Rollback() error { - err := tx.config.driver.(*txDriver).tx.Rollback() - tx.mu.Lock() - defer tx.mu.Unlock() - for _, f := range tx.onRollback { - f(err) +type ( + // Rollbacker is the interface that wraps the Rollbacker method. + Rollbacker interface { + Rollback(context.Context, *Tx) error } - return err + + // The RollbackFunc type is an adapter to allow the use of ordinary + // function as a Rollbacker. If f is a function with the appropriate + // signature, RollbackFunc(f) is a Rollbacker that calls f. + RollbackFunc func(context.Context, *Tx) error + + // RollbackHook defines the "rollback middleware". A function that gets a Rollbacker + // and returns a Rollbacker. For example: + // + // hook := func(next ent.Rollbacker) ent.Rollbacker { + // return ent.RollbackFunc(func(context.Context, tx *ent.Tx) error { + // // Do some stuff before. + // if err := next.Rollback(ctx, tx); err != nil { + // return err + // } + // // Do some stuff after. + // return nil + // }) + // } + // + RollbackHook func(Rollbacker) Rollbacker +) + +// Rollback calls f(ctx, m). +func (f RollbackFunc) Rollback(ctx context.Context, tx *Tx) error { + return f(ctx, tx) } -// OnRollback adds a function to call on rollback. -func (tx *Tx) OnRollback(f func(error)) { +// Rollback rollbacks the transaction. +func (tx *Tx) Rollback() error { + txDriver := tx.config.driver.(*txDriver) + var fn Rollbacker = RollbackFunc(func(context.Context, *Tx) error { + return txDriver.tx.Rollback() + }) + tx.mu.Lock() + hooks := append([]RollbackHook(nil), tx.onRollback...) + tx.mu.Unlock() + for i := len(hooks) - 1; i >= 0; i-- { + fn = hooks[i](fn) + } + return fn.Rollback(tx.ctx, tx) +} + +// OnRollback adds a hook to call on rollback. +func (tx *Tx) OnRollback(f RollbackHook) { tx.mu.Lock() defer tx.mu.Unlock() tx.onRollback = append(tx.onRollback, f) diff --git a/entc/integration/migrate/entv1/client.go b/entc/integration/migrate/entv1/client.go index 64c12d452..4654dc0c0 100644 --- a/entc/integration/migrate/entv1/client.go +++ b/entc/integration/migrate/entv1/client.go @@ -63,7 +63,8 @@ func Open(driverName, dataSourceName string, options ...Option) (*Client, error) } } -// Tx returns a new transactional client. +// Tx returns a new transactional client. The provided context +// is used until the transaction is committed or rolled back. func (c *Client) Tx(ctx context.Context) (*Tx, error) { if _, ok := c.driver.(*txDriver); ok { return nil, fmt.Errorf("entv1: cannot start a transaction within a transaction") @@ -74,6 +75,7 @@ func (c *Client) Tx(ctx context.Context) (*Tx, error) { } cfg := config{driver: tx, log: c.log, debug: c.debug, hooks: c.hooks} return &Tx{ + ctx: ctx, config: cfg, Car: NewCarClient(cfg), User: NewUserClient(cfg), diff --git a/entc/integration/migrate/entv1/tx.go b/entc/integration/migrate/entv1/tx.go index 2a30d1271..f09eca039 100644 --- a/entc/integration/migrate/entv1/tx.go +++ b/entc/integration/migrate/entv1/tx.go @@ -27,41 +27,119 @@ type Tx struct { // completion callbacks. mu sync.Mutex - onCommit []func(error) - onRollback []func(error) + onCommit []CommitHook + onRollback []RollbackHook + + // ctx lives for the life of the transaction. It is + // the same context used by the underlying connection. + ctx context.Context +} + +type ( + // Committer is the interface that wraps the Committer method. + Committer interface { + Commit(context.Context, *Tx) error + } + + // The CommitFunc type is an adapter to allow the use of ordinary + // function as a Committer. If f is a function with the appropriate + // signature, CommitFunc(f) is a Committer that calls f. + CommitFunc func(context.Context, *Tx) error + + // CommitHook defines the "commit middleware". A function that gets a Committer + // and returns a Committer. For example: + // + // hook := func(next ent.Committer) ent.Committer { + // return ent.CommitFunc(func(context.Context, tx *ent.Tx) error { + // // Do some stuff before. + // if err := next.Commit(ctx, tx); err != nil { + // return err + // } + // // Do some stuff after. + // return nil + // }) + // } + // + CommitHook func(Committer) Committer +) + +// Commit calls f(ctx, m). +func (f CommitFunc) Commit(ctx context.Context, tx *Tx) error { + return f(ctx, tx) } // Commit commits the transaction. func (tx *Tx) Commit() error { - err := tx.config.driver.(*txDriver).tx.Commit() + txDriver := tx.config.driver.(*txDriver) + var fn Committer = CommitFunc(func(context.Context, *Tx) error { + return txDriver.tx.Commit() + }) tx.mu.Lock() - defer tx.mu.Unlock() - for _, f := range tx.onCommit { - f(err) + hooks := append([]CommitHook(nil), tx.onCommit...) + tx.mu.Unlock() + for i := len(hooks) - 1; i >= 0; i-- { + fn = hooks[i](fn) } - return err + return fn.Commit(tx.ctx, tx) } -// OnCommit adds a function to call on commit. -func (tx *Tx) OnCommit(f func(error)) { +// OnCommit adds a hook to call on commit. +func (tx *Tx) OnCommit(f CommitHook) { tx.mu.Lock() defer tx.mu.Unlock() tx.onCommit = append(tx.onCommit, f) } -// Rollback rollbacks the transaction. -func (tx *Tx) Rollback() error { - err := tx.config.driver.(*txDriver).tx.Rollback() - tx.mu.Lock() - defer tx.mu.Unlock() - for _, f := range tx.onRollback { - f(err) +type ( + // Rollbacker is the interface that wraps the Rollbacker method. + Rollbacker interface { + Rollback(context.Context, *Tx) error } - return err + + // The RollbackFunc type is an adapter to allow the use of ordinary + // function as a Rollbacker. If f is a function with the appropriate + // signature, RollbackFunc(f) is a Rollbacker that calls f. + RollbackFunc func(context.Context, *Tx) error + + // RollbackHook defines the "rollback middleware". A function that gets a Rollbacker + // and returns a Rollbacker. For example: + // + // hook := func(next ent.Rollbacker) ent.Rollbacker { + // return ent.RollbackFunc(func(context.Context, tx *ent.Tx) error { + // // Do some stuff before. + // if err := next.Rollback(ctx, tx); err != nil { + // return err + // } + // // Do some stuff after. + // return nil + // }) + // } + // + RollbackHook func(Rollbacker) Rollbacker +) + +// Rollback calls f(ctx, m). +func (f RollbackFunc) Rollback(ctx context.Context, tx *Tx) error { + return f(ctx, tx) } -// OnRollback adds a function to call on rollback. -func (tx *Tx) OnRollback(f func(error)) { +// Rollback rollbacks the transaction. +func (tx *Tx) Rollback() error { + txDriver := tx.config.driver.(*txDriver) + var fn Rollbacker = RollbackFunc(func(context.Context, *Tx) error { + return txDriver.tx.Rollback() + }) + tx.mu.Lock() + hooks := append([]RollbackHook(nil), tx.onRollback...) + tx.mu.Unlock() + for i := len(hooks) - 1; i >= 0; i-- { + fn = hooks[i](fn) + } + return fn.Rollback(tx.ctx, tx) +} + +// OnRollback adds a hook to call on rollback. +func (tx *Tx) OnRollback(f RollbackHook) { tx.mu.Lock() defer tx.mu.Unlock() tx.onRollback = append(tx.onRollback, f) diff --git a/entc/integration/migrate/entv2/client.go b/entc/integration/migrate/entv2/client.go index c8e14c0af..f9f75876f 100644 --- a/entc/integration/migrate/entv2/client.go +++ b/entc/integration/migrate/entv2/client.go @@ -71,7 +71,8 @@ func Open(driverName, dataSourceName string, options ...Option) (*Client, error) } } -// Tx returns a new transactional client. +// Tx returns a new transactional client. The provided context +// is used until the transaction is committed or rolled back. func (c *Client) Tx(ctx context.Context) (*Tx, error) { if _, ok := c.driver.(*txDriver); ok { return nil, fmt.Errorf("entv2: cannot start a transaction within a transaction") @@ -82,6 +83,7 @@ func (c *Client) Tx(ctx context.Context) (*Tx, error) { } cfg := config{driver: tx, log: c.log, debug: c.debug, hooks: c.hooks} return &Tx{ + ctx: ctx, config: cfg, Car: NewCarClient(cfg), Group: NewGroupClient(cfg), diff --git a/entc/integration/migrate/entv2/tx.go b/entc/integration/migrate/entv2/tx.go index 3364d0356..895ae9e69 100644 --- a/entc/integration/migrate/entv2/tx.go +++ b/entc/integration/migrate/entv2/tx.go @@ -31,41 +31,119 @@ type Tx struct { // completion callbacks. mu sync.Mutex - onCommit []func(error) - onRollback []func(error) + onCommit []CommitHook + onRollback []RollbackHook + + // ctx lives for the life of the transaction. It is + // the same context used by the underlying connection. + ctx context.Context +} + +type ( + // Committer is the interface that wraps the Committer method. + Committer interface { + Commit(context.Context, *Tx) error + } + + // The CommitFunc type is an adapter to allow the use of ordinary + // function as a Committer. If f is a function with the appropriate + // signature, CommitFunc(f) is a Committer that calls f. + CommitFunc func(context.Context, *Tx) error + + // CommitHook defines the "commit middleware". A function that gets a Committer + // and returns a Committer. For example: + // + // hook := func(next ent.Committer) ent.Committer { + // return ent.CommitFunc(func(context.Context, tx *ent.Tx) error { + // // Do some stuff before. + // if err := next.Commit(ctx, tx); err != nil { + // return err + // } + // // Do some stuff after. + // return nil + // }) + // } + // + CommitHook func(Committer) Committer +) + +// Commit calls f(ctx, m). +func (f CommitFunc) Commit(ctx context.Context, tx *Tx) error { + return f(ctx, tx) } // Commit commits the transaction. func (tx *Tx) Commit() error { - err := tx.config.driver.(*txDriver).tx.Commit() + txDriver := tx.config.driver.(*txDriver) + var fn Committer = CommitFunc(func(context.Context, *Tx) error { + return txDriver.tx.Commit() + }) tx.mu.Lock() - defer tx.mu.Unlock() - for _, f := range tx.onCommit { - f(err) + hooks := append([]CommitHook(nil), tx.onCommit...) + tx.mu.Unlock() + for i := len(hooks) - 1; i >= 0; i-- { + fn = hooks[i](fn) } - return err + return fn.Commit(tx.ctx, tx) } -// OnCommit adds a function to call on commit. -func (tx *Tx) OnCommit(f func(error)) { +// OnCommit adds a hook to call on commit. +func (tx *Tx) OnCommit(f CommitHook) { tx.mu.Lock() defer tx.mu.Unlock() tx.onCommit = append(tx.onCommit, f) } -// Rollback rollbacks the transaction. -func (tx *Tx) Rollback() error { - err := tx.config.driver.(*txDriver).tx.Rollback() - tx.mu.Lock() - defer tx.mu.Unlock() - for _, f := range tx.onRollback { - f(err) +type ( + // Rollbacker is the interface that wraps the Rollbacker method. + Rollbacker interface { + Rollback(context.Context, *Tx) error } - return err + + // The RollbackFunc type is an adapter to allow the use of ordinary + // function as a Rollbacker. If f is a function with the appropriate + // signature, RollbackFunc(f) is a Rollbacker that calls f. + RollbackFunc func(context.Context, *Tx) error + + // RollbackHook defines the "rollback middleware". A function that gets a Rollbacker + // and returns a Rollbacker. For example: + // + // hook := func(next ent.Rollbacker) ent.Rollbacker { + // return ent.RollbackFunc(func(context.Context, tx *ent.Tx) error { + // // Do some stuff before. + // if err := next.Rollback(ctx, tx); err != nil { + // return err + // } + // // Do some stuff after. + // return nil + // }) + // } + // + RollbackHook func(Rollbacker) Rollbacker +) + +// Rollback calls f(ctx, m). +func (f RollbackFunc) Rollback(ctx context.Context, tx *Tx) error { + return f(ctx, tx) } -// OnRollback adds a function to call on rollback. -func (tx *Tx) OnRollback(f func(error)) { +// Rollback rollbacks the transaction. +func (tx *Tx) Rollback() error { + txDriver := tx.config.driver.(*txDriver) + var fn Rollbacker = RollbackFunc(func(context.Context, *Tx) error { + return txDriver.tx.Rollback() + }) + tx.mu.Lock() + hooks := append([]RollbackHook(nil), tx.onRollback...) + tx.mu.Unlock() + for i := len(hooks) - 1; i >= 0; i-- { + fn = hooks[i](fn) + } + return fn.Rollback(tx.ctx, tx) +} + +// OnRollback adds a hook to call on rollback. +func (tx *Tx) OnRollback(f RollbackHook) { tx.mu.Lock() defer tx.mu.Unlock() tx.onRollback = append(tx.onRollback, f) diff --git a/entc/integration/privacy/ent/client.go b/entc/integration/privacy/ent/client.go index c945d0ae9..763e07b2c 100644 --- a/entc/integration/privacy/ent/client.go +++ b/entc/integration/privacy/ent/client.go @@ -63,7 +63,8 @@ func Open(driverName, dataSourceName string, options ...Option) (*Client, error) } } -// Tx returns a new transactional client. +// Tx returns a new transactional client. The provided context +// is used until the transaction is committed or rolled back. func (c *Client) Tx(ctx context.Context) (*Tx, error) { if _, ok := c.driver.(*txDriver); ok { return nil, fmt.Errorf("ent: cannot start a transaction within a transaction") @@ -74,6 +75,7 @@ func (c *Client) Tx(ctx context.Context) (*Tx, error) { } cfg := config{driver: tx, log: c.log, debug: c.debug, hooks: c.hooks} return &Tx{ + ctx: ctx, config: cfg, Galaxy: NewGalaxyClient(cfg), Planet: NewPlanetClient(cfg), diff --git a/entc/integration/privacy/ent/tx.go b/entc/integration/privacy/ent/tx.go index 70d91891b..cc5fe26c2 100644 --- a/entc/integration/privacy/ent/tx.go +++ b/entc/integration/privacy/ent/tx.go @@ -27,41 +27,119 @@ type Tx struct { // completion callbacks. mu sync.Mutex - onCommit []func(error) - onRollback []func(error) + onCommit []CommitHook + onRollback []RollbackHook + + // ctx lives for the life of the transaction. It is + // the same context used by the underlying connection. + ctx context.Context +} + +type ( + // Committer is the interface that wraps the Committer method. + Committer interface { + Commit(context.Context, *Tx) error + } + + // The CommitFunc type is an adapter to allow the use of ordinary + // function as a Committer. If f is a function with the appropriate + // signature, CommitFunc(f) is a Committer that calls f. + CommitFunc func(context.Context, *Tx) error + + // CommitHook defines the "commit middleware". A function that gets a Committer + // and returns a Committer. For example: + // + // hook := func(next ent.Committer) ent.Committer { + // return ent.CommitFunc(func(context.Context, tx *ent.Tx) error { + // // Do some stuff before. + // if err := next.Commit(ctx, tx); err != nil { + // return err + // } + // // Do some stuff after. + // return nil + // }) + // } + // + CommitHook func(Committer) Committer +) + +// Commit calls f(ctx, m). +func (f CommitFunc) Commit(ctx context.Context, tx *Tx) error { + return f(ctx, tx) } // Commit commits the transaction. func (tx *Tx) Commit() error { - err := tx.config.driver.(*txDriver).tx.Commit() + txDriver := tx.config.driver.(*txDriver) + var fn Committer = CommitFunc(func(context.Context, *Tx) error { + return txDriver.tx.Commit() + }) tx.mu.Lock() - defer tx.mu.Unlock() - for _, f := range tx.onCommit { - f(err) + hooks := append([]CommitHook(nil), tx.onCommit...) + tx.mu.Unlock() + for i := len(hooks) - 1; i >= 0; i-- { + fn = hooks[i](fn) } - return err + return fn.Commit(tx.ctx, tx) } -// OnCommit adds a function to call on commit. -func (tx *Tx) OnCommit(f func(error)) { +// OnCommit adds a hook to call on commit. +func (tx *Tx) OnCommit(f CommitHook) { tx.mu.Lock() defer tx.mu.Unlock() tx.onCommit = append(tx.onCommit, f) } -// Rollback rollbacks the transaction. -func (tx *Tx) Rollback() error { - err := tx.config.driver.(*txDriver).tx.Rollback() - tx.mu.Lock() - defer tx.mu.Unlock() - for _, f := range tx.onRollback { - f(err) +type ( + // Rollbacker is the interface that wraps the Rollbacker method. + Rollbacker interface { + Rollback(context.Context, *Tx) error } - return err + + // The RollbackFunc type is an adapter to allow the use of ordinary + // function as a Rollbacker. If f is a function with the appropriate + // signature, RollbackFunc(f) is a Rollbacker that calls f. + RollbackFunc func(context.Context, *Tx) error + + // RollbackHook defines the "rollback middleware". A function that gets a Rollbacker + // and returns a Rollbacker. For example: + // + // hook := func(next ent.Rollbacker) ent.Rollbacker { + // return ent.RollbackFunc(func(context.Context, tx *ent.Tx) error { + // // Do some stuff before. + // if err := next.Rollback(ctx, tx); err != nil { + // return err + // } + // // Do some stuff after. + // return nil + // }) + // } + // + RollbackHook func(Rollbacker) Rollbacker +) + +// Rollback calls f(ctx, m). +func (f RollbackFunc) Rollback(ctx context.Context, tx *Tx) error { + return f(ctx, tx) } -// OnRollback adds a function to call on rollback. -func (tx *Tx) OnRollback(f func(error)) { +// Rollback rollbacks the transaction. +func (tx *Tx) Rollback() error { + txDriver := tx.config.driver.(*txDriver) + var fn Rollbacker = RollbackFunc(func(context.Context, *Tx) error { + return txDriver.tx.Rollback() + }) + tx.mu.Lock() + hooks := append([]RollbackHook(nil), tx.onRollback...) + tx.mu.Unlock() + for i := len(hooks) - 1; i >= 0; i-- { + fn = hooks[i](fn) + } + return fn.Rollback(tx.ctx, tx) +} + +// OnRollback adds a hook to call on rollback. +func (tx *Tx) OnRollback(f RollbackHook) { tx.mu.Lock() defer tx.mu.Unlock() tx.onRollback = append(tx.onRollback, f) diff --git a/entc/integration/template/ent/client.go b/entc/integration/template/ent/client.go index 876d953ee..4b58845a6 100644 --- a/entc/integration/template/ent/client.go +++ b/entc/integration/template/ent/client.go @@ -70,7 +70,8 @@ func Open(driverName, dataSourceName string, options ...Option) (*Client, error) } } -// Tx returns a new transactional client. +// Tx returns a new transactional client. The provided context +// is used until the transaction is committed or rolled back. func (c *Client) Tx(ctx context.Context) (*Tx, error) { if _, ok := c.driver.(*txDriver); ok { return nil, fmt.Errorf("ent: cannot start a transaction within a transaction") @@ -81,6 +82,7 @@ func (c *Client) Tx(ctx context.Context) (*Tx, error) { } cfg := config{driver: tx, log: c.log, debug: c.debug, hooks: c.hooks} return &Tx{ + ctx: ctx, config: cfg, Group: NewGroupClient(cfg), Pet: NewPetClient(cfg), diff --git a/entc/integration/template/ent/tx.go b/entc/integration/template/ent/tx.go index be5bd0e86..2b63ca443 100644 --- a/entc/integration/template/ent/tx.go +++ b/entc/integration/template/ent/tx.go @@ -29,41 +29,119 @@ type Tx struct { // completion callbacks. mu sync.Mutex - onCommit []func(error) - onRollback []func(error) + onCommit []CommitHook + onRollback []RollbackHook + + // ctx lives for the life of the transaction. It is + // the same context used by the underlying connection. + ctx context.Context +} + +type ( + // Committer is the interface that wraps the Committer method. + Committer interface { + Commit(context.Context, *Tx) error + } + + // The CommitFunc type is an adapter to allow the use of ordinary + // function as a Committer. If f is a function with the appropriate + // signature, CommitFunc(f) is a Committer that calls f. + CommitFunc func(context.Context, *Tx) error + + // CommitHook defines the "commit middleware". A function that gets a Committer + // and returns a Committer. For example: + // + // hook := func(next ent.Committer) ent.Committer { + // return ent.CommitFunc(func(context.Context, tx *ent.Tx) error { + // // Do some stuff before. + // if err := next.Commit(ctx, tx); err != nil { + // return err + // } + // // Do some stuff after. + // return nil + // }) + // } + // + CommitHook func(Committer) Committer +) + +// Commit calls f(ctx, m). +func (f CommitFunc) Commit(ctx context.Context, tx *Tx) error { + return f(ctx, tx) } // Commit commits the transaction. func (tx *Tx) Commit() error { - err := tx.config.driver.(*txDriver).tx.Commit() + txDriver := tx.config.driver.(*txDriver) + var fn Committer = CommitFunc(func(context.Context, *Tx) error { + return txDriver.tx.Commit() + }) tx.mu.Lock() - defer tx.mu.Unlock() - for _, f := range tx.onCommit { - f(err) + hooks := append([]CommitHook(nil), tx.onCommit...) + tx.mu.Unlock() + for i := len(hooks) - 1; i >= 0; i-- { + fn = hooks[i](fn) } - return err + return fn.Commit(tx.ctx, tx) } -// OnCommit adds a function to call on commit. -func (tx *Tx) OnCommit(f func(error)) { +// OnCommit adds a hook to call on commit. +func (tx *Tx) OnCommit(f CommitHook) { tx.mu.Lock() defer tx.mu.Unlock() tx.onCommit = append(tx.onCommit, f) } -// Rollback rollbacks the transaction. -func (tx *Tx) Rollback() error { - err := tx.config.driver.(*txDriver).tx.Rollback() - tx.mu.Lock() - defer tx.mu.Unlock() - for _, f := range tx.onRollback { - f(err) +type ( + // Rollbacker is the interface that wraps the Rollbacker method. + Rollbacker interface { + Rollback(context.Context, *Tx) error } - return err + + // The RollbackFunc type is an adapter to allow the use of ordinary + // function as a Rollbacker. If f is a function with the appropriate + // signature, RollbackFunc(f) is a Rollbacker that calls f. + RollbackFunc func(context.Context, *Tx) error + + // RollbackHook defines the "rollback middleware". A function that gets a Rollbacker + // and returns a Rollbacker. For example: + // + // hook := func(next ent.Rollbacker) ent.Rollbacker { + // return ent.RollbackFunc(func(context.Context, tx *ent.Tx) error { + // // Do some stuff before. + // if err := next.Rollback(ctx, tx); err != nil { + // return err + // } + // // Do some stuff after. + // return nil + // }) + // } + // + RollbackHook func(Rollbacker) Rollbacker +) + +// Rollback calls f(ctx, m). +func (f RollbackFunc) Rollback(ctx context.Context, tx *Tx) error { + return f(ctx, tx) } -// OnRollback adds a function to call on rollback. -func (tx *Tx) OnRollback(f func(error)) { +// Rollback rollbacks the transaction. +func (tx *Tx) Rollback() error { + txDriver := tx.config.driver.(*txDriver) + var fn Rollbacker = RollbackFunc(func(context.Context, *Tx) error { + return txDriver.tx.Rollback() + }) + tx.mu.Lock() + hooks := append([]RollbackHook(nil), tx.onRollback...) + tx.mu.Unlock() + for i := len(hooks) - 1; i >= 0; i-- { + fn = hooks[i](fn) + } + return fn.Rollback(tx.ctx, tx) +} + +// OnRollback adds a hook to call on rollback. +func (tx *Tx) OnRollback(f RollbackHook) { tx.mu.Lock() defer tx.mu.Unlock() tx.onRollback = append(tx.onRollback, f) diff --git a/examples/edgeindex/ent/client.go b/examples/edgeindex/ent/client.go index 38a5ad0a0..3358be964 100644 --- a/examples/edgeindex/ent/client.go +++ b/examples/edgeindex/ent/client.go @@ -63,7 +63,8 @@ func Open(driverName, dataSourceName string, options ...Option) (*Client, error) } } -// Tx returns a new transactional client. +// Tx returns a new transactional client. The provided context +// is used until the transaction is committed or rolled back. func (c *Client) Tx(ctx context.Context) (*Tx, error) { if _, ok := c.driver.(*txDriver); ok { return nil, fmt.Errorf("ent: cannot start a transaction within a transaction") @@ -74,6 +75,7 @@ func (c *Client) Tx(ctx context.Context) (*Tx, error) { } cfg := config{driver: tx, log: c.log, debug: c.debug, hooks: c.hooks} return &Tx{ + ctx: ctx, config: cfg, City: NewCityClient(cfg), Street: NewStreetClient(cfg), diff --git a/examples/edgeindex/ent/tx.go b/examples/edgeindex/ent/tx.go index 2ba1ce928..9ca768657 100644 --- a/examples/edgeindex/ent/tx.go +++ b/examples/edgeindex/ent/tx.go @@ -27,41 +27,119 @@ type Tx struct { // completion callbacks. mu sync.Mutex - onCommit []func(error) - onRollback []func(error) + onCommit []CommitHook + onRollback []RollbackHook + + // ctx lives for the life of the transaction. It is + // the same context used by the underlying connection. + ctx context.Context +} + +type ( + // Committer is the interface that wraps the Committer method. + Committer interface { + Commit(context.Context, *Tx) error + } + + // The CommitFunc type is an adapter to allow the use of ordinary + // function as a Committer. If f is a function with the appropriate + // signature, CommitFunc(f) is a Committer that calls f. + CommitFunc func(context.Context, *Tx) error + + // CommitHook defines the "commit middleware". A function that gets a Committer + // and returns a Committer. For example: + // + // hook := func(next ent.Committer) ent.Committer { + // return ent.CommitFunc(func(context.Context, tx *ent.Tx) error { + // // Do some stuff before. + // if err := next.Commit(ctx, tx); err != nil { + // return err + // } + // // Do some stuff after. + // return nil + // }) + // } + // + CommitHook func(Committer) Committer +) + +// Commit calls f(ctx, m). +func (f CommitFunc) Commit(ctx context.Context, tx *Tx) error { + return f(ctx, tx) } // Commit commits the transaction. func (tx *Tx) Commit() error { - err := tx.config.driver.(*txDriver).tx.Commit() + txDriver := tx.config.driver.(*txDriver) + var fn Committer = CommitFunc(func(context.Context, *Tx) error { + return txDriver.tx.Commit() + }) tx.mu.Lock() - defer tx.mu.Unlock() - for _, f := range tx.onCommit { - f(err) + hooks := append([]CommitHook(nil), tx.onCommit...) + tx.mu.Unlock() + for i := len(hooks) - 1; i >= 0; i-- { + fn = hooks[i](fn) } - return err + return fn.Commit(tx.ctx, tx) } -// OnCommit adds a function to call on commit. -func (tx *Tx) OnCommit(f func(error)) { +// OnCommit adds a hook to call on commit. +func (tx *Tx) OnCommit(f CommitHook) { tx.mu.Lock() defer tx.mu.Unlock() tx.onCommit = append(tx.onCommit, f) } -// Rollback rollbacks the transaction. -func (tx *Tx) Rollback() error { - err := tx.config.driver.(*txDriver).tx.Rollback() - tx.mu.Lock() - defer tx.mu.Unlock() - for _, f := range tx.onRollback { - f(err) +type ( + // Rollbacker is the interface that wraps the Rollbacker method. + Rollbacker interface { + Rollback(context.Context, *Tx) error } - return err + + // The RollbackFunc type is an adapter to allow the use of ordinary + // function as a Rollbacker. If f is a function with the appropriate + // signature, RollbackFunc(f) is a Rollbacker that calls f. + RollbackFunc func(context.Context, *Tx) error + + // RollbackHook defines the "rollback middleware". A function that gets a Rollbacker + // and returns a Rollbacker. For example: + // + // hook := func(next ent.Rollbacker) ent.Rollbacker { + // return ent.RollbackFunc(func(context.Context, tx *ent.Tx) error { + // // Do some stuff before. + // if err := next.Rollback(ctx, tx); err != nil { + // return err + // } + // // Do some stuff after. + // return nil + // }) + // } + // + RollbackHook func(Rollbacker) Rollbacker +) + +// Rollback calls f(ctx, m). +func (f RollbackFunc) Rollback(ctx context.Context, tx *Tx) error { + return f(ctx, tx) } -// OnRollback adds a function to call on rollback. -func (tx *Tx) OnRollback(f func(error)) { +// Rollback rollbacks the transaction. +func (tx *Tx) Rollback() error { + txDriver := tx.config.driver.(*txDriver) + var fn Rollbacker = RollbackFunc(func(context.Context, *Tx) error { + return txDriver.tx.Rollback() + }) + tx.mu.Lock() + hooks := append([]RollbackHook(nil), tx.onRollback...) + tx.mu.Unlock() + for i := len(hooks) - 1; i >= 0; i-- { + fn = hooks[i](fn) + } + return fn.Rollback(tx.ctx, tx) +} + +// OnRollback adds a hook to call on rollback. +func (tx *Tx) OnRollback(f RollbackHook) { tx.mu.Lock() defer tx.mu.Unlock() tx.onRollback = append(tx.onRollback, f) diff --git a/examples/entcpkg/ent/client.go b/examples/entcpkg/ent/client.go index d29536d39..781345ae1 100644 --- a/examples/entcpkg/ent/client.go +++ b/examples/entcpkg/ent/client.go @@ -58,7 +58,8 @@ func Open(driverName, dataSourceName string, options ...Option) (*Client, error) } } -// Tx returns a new transactional client. +// Tx returns a new transactional client. The provided context +// is used until the transaction is committed or rolled back. func (c *Client) Tx(ctx context.Context) (*Tx, error) { if _, ok := c.driver.(*txDriver); ok { return nil, fmt.Errorf("ent: cannot start a transaction within a transaction") @@ -69,6 +70,7 @@ func (c *Client) Tx(ctx context.Context) (*Tx, error) { } cfg := config{driver: tx, log: c.log, debug: c.debug, hooks: c.hooks} return &Tx{ + ctx: ctx, config: cfg, User: NewUserClient(cfg), }, nil diff --git a/examples/entcpkg/ent/tx.go b/examples/entcpkg/ent/tx.go index 62082b404..bd5cb64a3 100644 --- a/examples/entcpkg/ent/tx.go +++ b/examples/entcpkg/ent/tx.go @@ -25,41 +25,119 @@ type Tx struct { // completion callbacks. mu sync.Mutex - onCommit []func(error) - onRollback []func(error) + onCommit []CommitHook + onRollback []RollbackHook + + // ctx lives for the life of the transaction. It is + // the same context used by the underlying connection. + ctx context.Context +} + +type ( + // Committer is the interface that wraps the Committer method. + Committer interface { + Commit(context.Context, *Tx) error + } + + // The CommitFunc type is an adapter to allow the use of ordinary + // function as a Committer. If f is a function with the appropriate + // signature, CommitFunc(f) is a Committer that calls f. + CommitFunc func(context.Context, *Tx) error + + // CommitHook defines the "commit middleware". A function that gets a Committer + // and returns a Committer. For example: + // + // hook := func(next ent.Committer) ent.Committer { + // return ent.CommitFunc(func(context.Context, tx *ent.Tx) error { + // // Do some stuff before. + // if err := next.Commit(ctx, tx); err != nil { + // return err + // } + // // Do some stuff after. + // return nil + // }) + // } + // + CommitHook func(Committer) Committer +) + +// Commit calls f(ctx, m). +func (f CommitFunc) Commit(ctx context.Context, tx *Tx) error { + return f(ctx, tx) } // Commit commits the transaction. func (tx *Tx) Commit() error { - err := tx.config.driver.(*txDriver).tx.Commit() + txDriver := tx.config.driver.(*txDriver) + var fn Committer = CommitFunc(func(context.Context, *Tx) error { + return txDriver.tx.Commit() + }) tx.mu.Lock() - defer tx.mu.Unlock() - for _, f := range tx.onCommit { - f(err) + hooks := append([]CommitHook(nil), tx.onCommit...) + tx.mu.Unlock() + for i := len(hooks) - 1; i >= 0; i-- { + fn = hooks[i](fn) } - return err + return fn.Commit(tx.ctx, tx) } -// OnCommit adds a function to call on commit. -func (tx *Tx) OnCommit(f func(error)) { +// OnCommit adds a hook to call on commit. +func (tx *Tx) OnCommit(f CommitHook) { tx.mu.Lock() defer tx.mu.Unlock() tx.onCommit = append(tx.onCommit, f) } -// Rollback rollbacks the transaction. -func (tx *Tx) Rollback() error { - err := tx.config.driver.(*txDriver).tx.Rollback() - tx.mu.Lock() - defer tx.mu.Unlock() - for _, f := range tx.onRollback { - f(err) +type ( + // Rollbacker is the interface that wraps the Rollbacker method. + Rollbacker interface { + Rollback(context.Context, *Tx) error } - return err + + // The RollbackFunc type is an adapter to allow the use of ordinary + // function as a Rollbacker. If f is a function with the appropriate + // signature, RollbackFunc(f) is a Rollbacker that calls f. + RollbackFunc func(context.Context, *Tx) error + + // RollbackHook defines the "rollback middleware". A function that gets a Rollbacker + // and returns a Rollbacker. For example: + // + // hook := func(next ent.Rollbacker) ent.Rollbacker { + // return ent.RollbackFunc(func(context.Context, tx *ent.Tx) error { + // // Do some stuff before. + // if err := next.Rollback(ctx, tx); err != nil { + // return err + // } + // // Do some stuff after. + // return nil + // }) + // } + // + RollbackHook func(Rollbacker) Rollbacker +) + +// Rollback calls f(ctx, m). +func (f RollbackFunc) Rollback(ctx context.Context, tx *Tx) error { + return f(ctx, tx) } -// OnRollback adds a function to call on rollback. -func (tx *Tx) OnRollback(f func(error)) { +// Rollback rollbacks the transaction. +func (tx *Tx) Rollback() error { + txDriver := tx.config.driver.(*txDriver) + var fn Rollbacker = RollbackFunc(func(context.Context, *Tx) error { + return txDriver.tx.Rollback() + }) + tx.mu.Lock() + hooks := append([]RollbackHook(nil), tx.onRollback...) + tx.mu.Unlock() + for i := len(hooks) - 1; i >= 0; i-- { + fn = hooks[i](fn) + } + return fn.Rollback(tx.ctx, tx) +} + +// OnRollback adds a hook to call on rollback. +func (tx *Tx) OnRollback(f RollbackHook) { tx.mu.Lock() defer tx.mu.Unlock() tx.onRollback = append(tx.onRollback, f) diff --git a/examples/m2m2types/ent/client.go b/examples/m2m2types/ent/client.go index 47daac09a..cc128c477 100644 --- a/examples/m2m2types/ent/client.go +++ b/examples/m2m2types/ent/client.go @@ -63,7 +63,8 @@ func Open(driverName, dataSourceName string, options ...Option) (*Client, error) } } -// Tx returns a new transactional client. +// Tx returns a new transactional client. The provided context +// is used until the transaction is committed or rolled back. func (c *Client) Tx(ctx context.Context) (*Tx, error) { if _, ok := c.driver.(*txDriver); ok { return nil, fmt.Errorf("ent: cannot start a transaction within a transaction") @@ -74,6 +75,7 @@ func (c *Client) Tx(ctx context.Context) (*Tx, error) { } cfg := config{driver: tx, log: c.log, debug: c.debug, hooks: c.hooks} return &Tx{ + ctx: ctx, config: cfg, Group: NewGroupClient(cfg), User: NewUserClient(cfg), diff --git a/examples/m2m2types/ent/tx.go b/examples/m2m2types/ent/tx.go index caa52a7b9..f25a925c8 100644 --- a/examples/m2m2types/ent/tx.go +++ b/examples/m2m2types/ent/tx.go @@ -27,41 +27,119 @@ type Tx struct { // completion callbacks. mu sync.Mutex - onCommit []func(error) - onRollback []func(error) + onCommit []CommitHook + onRollback []RollbackHook + + // ctx lives for the life of the transaction. It is + // the same context used by the underlying connection. + ctx context.Context +} + +type ( + // Committer is the interface that wraps the Committer method. + Committer interface { + Commit(context.Context, *Tx) error + } + + // The CommitFunc type is an adapter to allow the use of ordinary + // function as a Committer. If f is a function with the appropriate + // signature, CommitFunc(f) is a Committer that calls f. + CommitFunc func(context.Context, *Tx) error + + // CommitHook defines the "commit middleware". A function that gets a Committer + // and returns a Committer. For example: + // + // hook := func(next ent.Committer) ent.Committer { + // return ent.CommitFunc(func(context.Context, tx *ent.Tx) error { + // // Do some stuff before. + // if err := next.Commit(ctx, tx); err != nil { + // return err + // } + // // Do some stuff after. + // return nil + // }) + // } + // + CommitHook func(Committer) Committer +) + +// Commit calls f(ctx, m). +func (f CommitFunc) Commit(ctx context.Context, tx *Tx) error { + return f(ctx, tx) } // Commit commits the transaction. func (tx *Tx) Commit() error { - err := tx.config.driver.(*txDriver).tx.Commit() + txDriver := tx.config.driver.(*txDriver) + var fn Committer = CommitFunc(func(context.Context, *Tx) error { + return txDriver.tx.Commit() + }) tx.mu.Lock() - defer tx.mu.Unlock() - for _, f := range tx.onCommit { - f(err) + hooks := append([]CommitHook(nil), tx.onCommit...) + tx.mu.Unlock() + for i := len(hooks) - 1; i >= 0; i-- { + fn = hooks[i](fn) } - return err + return fn.Commit(tx.ctx, tx) } -// OnCommit adds a function to call on commit. -func (tx *Tx) OnCommit(f func(error)) { +// OnCommit adds a hook to call on commit. +func (tx *Tx) OnCommit(f CommitHook) { tx.mu.Lock() defer tx.mu.Unlock() tx.onCommit = append(tx.onCommit, f) } -// Rollback rollbacks the transaction. -func (tx *Tx) Rollback() error { - err := tx.config.driver.(*txDriver).tx.Rollback() - tx.mu.Lock() - defer tx.mu.Unlock() - for _, f := range tx.onRollback { - f(err) +type ( + // Rollbacker is the interface that wraps the Rollbacker method. + Rollbacker interface { + Rollback(context.Context, *Tx) error } - return err + + // The RollbackFunc type is an adapter to allow the use of ordinary + // function as a Rollbacker. If f is a function with the appropriate + // signature, RollbackFunc(f) is a Rollbacker that calls f. + RollbackFunc func(context.Context, *Tx) error + + // RollbackHook defines the "rollback middleware". A function that gets a Rollbacker + // and returns a Rollbacker. For example: + // + // hook := func(next ent.Rollbacker) ent.Rollbacker { + // return ent.RollbackFunc(func(context.Context, tx *ent.Tx) error { + // // Do some stuff before. + // if err := next.Rollback(ctx, tx); err != nil { + // return err + // } + // // Do some stuff after. + // return nil + // }) + // } + // + RollbackHook func(Rollbacker) Rollbacker +) + +// Rollback calls f(ctx, m). +func (f RollbackFunc) Rollback(ctx context.Context, tx *Tx) error { + return f(ctx, tx) } -// OnRollback adds a function to call on rollback. -func (tx *Tx) OnRollback(f func(error)) { +// Rollback rollbacks the transaction. +func (tx *Tx) Rollback() error { + txDriver := tx.config.driver.(*txDriver) + var fn Rollbacker = RollbackFunc(func(context.Context, *Tx) error { + return txDriver.tx.Rollback() + }) + tx.mu.Lock() + hooks := append([]RollbackHook(nil), tx.onRollback...) + tx.mu.Unlock() + for i := len(hooks) - 1; i >= 0; i-- { + fn = hooks[i](fn) + } + return fn.Rollback(tx.ctx, tx) +} + +// OnRollback adds a hook to call on rollback. +func (tx *Tx) OnRollback(f RollbackHook) { tx.mu.Lock() defer tx.mu.Unlock() tx.onRollback = append(tx.onRollback, f) diff --git a/examples/m2mbidi/ent/client.go b/examples/m2mbidi/ent/client.go index 9f9f3a36a..b85222be2 100644 --- a/examples/m2mbidi/ent/client.go +++ b/examples/m2mbidi/ent/client.go @@ -59,7 +59,8 @@ func Open(driverName, dataSourceName string, options ...Option) (*Client, error) } } -// Tx returns a new transactional client. +// Tx returns a new transactional client. The provided context +// is used until the transaction is committed or rolled back. func (c *Client) Tx(ctx context.Context) (*Tx, error) { if _, ok := c.driver.(*txDriver); ok { return nil, fmt.Errorf("ent: cannot start a transaction within a transaction") @@ -70,6 +71,7 @@ func (c *Client) Tx(ctx context.Context) (*Tx, error) { } cfg := config{driver: tx, log: c.log, debug: c.debug, hooks: c.hooks} return &Tx{ + ctx: ctx, config: cfg, User: NewUserClient(cfg), }, nil diff --git a/examples/m2mbidi/ent/tx.go b/examples/m2mbidi/ent/tx.go index 62082b404..bd5cb64a3 100644 --- a/examples/m2mbidi/ent/tx.go +++ b/examples/m2mbidi/ent/tx.go @@ -25,41 +25,119 @@ type Tx struct { // completion callbacks. mu sync.Mutex - onCommit []func(error) - onRollback []func(error) + onCommit []CommitHook + onRollback []RollbackHook + + // ctx lives for the life of the transaction. It is + // the same context used by the underlying connection. + ctx context.Context +} + +type ( + // Committer is the interface that wraps the Committer method. + Committer interface { + Commit(context.Context, *Tx) error + } + + // The CommitFunc type is an adapter to allow the use of ordinary + // function as a Committer. If f is a function with the appropriate + // signature, CommitFunc(f) is a Committer that calls f. + CommitFunc func(context.Context, *Tx) error + + // CommitHook defines the "commit middleware". A function that gets a Committer + // and returns a Committer. For example: + // + // hook := func(next ent.Committer) ent.Committer { + // return ent.CommitFunc(func(context.Context, tx *ent.Tx) error { + // // Do some stuff before. + // if err := next.Commit(ctx, tx); err != nil { + // return err + // } + // // Do some stuff after. + // return nil + // }) + // } + // + CommitHook func(Committer) Committer +) + +// Commit calls f(ctx, m). +func (f CommitFunc) Commit(ctx context.Context, tx *Tx) error { + return f(ctx, tx) } // Commit commits the transaction. func (tx *Tx) Commit() error { - err := tx.config.driver.(*txDriver).tx.Commit() + txDriver := tx.config.driver.(*txDriver) + var fn Committer = CommitFunc(func(context.Context, *Tx) error { + return txDriver.tx.Commit() + }) tx.mu.Lock() - defer tx.mu.Unlock() - for _, f := range tx.onCommit { - f(err) + hooks := append([]CommitHook(nil), tx.onCommit...) + tx.mu.Unlock() + for i := len(hooks) - 1; i >= 0; i-- { + fn = hooks[i](fn) } - return err + return fn.Commit(tx.ctx, tx) } -// OnCommit adds a function to call on commit. -func (tx *Tx) OnCommit(f func(error)) { +// OnCommit adds a hook to call on commit. +func (tx *Tx) OnCommit(f CommitHook) { tx.mu.Lock() defer tx.mu.Unlock() tx.onCommit = append(tx.onCommit, f) } -// Rollback rollbacks the transaction. -func (tx *Tx) Rollback() error { - err := tx.config.driver.(*txDriver).tx.Rollback() - tx.mu.Lock() - defer tx.mu.Unlock() - for _, f := range tx.onRollback { - f(err) +type ( + // Rollbacker is the interface that wraps the Rollbacker method. + Rollbacker interface { + Rollback(context.Context, *Tx) error } - return err + + // The RollbackFunc type is an adapter to allow the use of ordinary + // function as a Rollbacker. If f is a function with the appropriate + // signature, RollbackFunc(f) is a Rollbacker that calls f. + RollbackFunc func(context.Context, *Tx) error + + // RollbackHook defines the "rollback middleware". A function that gets a Rollbacker + // and returns a Rollbacker. For example: + // + // hook := func(next ent.Rollbacker) ent.Rollbacker { + // return ent.RollbackFunc(func(context.Context, tx *ent.Tx) error { + // // Do some stuff before. + // if err := next.Rollback(ctx, tx); err != nil { + // return err + // } + // // Do some stuff after. + // return nil + // }) + // } + // + RollbackHook func(Rollbacker) Rollbacker +) + +// Rollback calls f(ctx, m). +func (f RollbackFunc) Rollback(ctx context.Context, tx *Tx) error { + return f(ctx, tx) } -// OnRollback adds a function to call on rollback. -func (tx *Tx) OnRollback(f func(error)) { +// Rollback rollbacks the transaction. +func (tx *Tx) Rollback() error { + txDriver := tx.config.driver.(*txDriver) + var fn Rollbacker = RollbackFunc(func(context.Context, *Tx) error { + return txDriver.tx.Rollback() + }) + tx.mu.Lock() + hooks := append([]RollbackHook(nil), tx.onRollback...) + tx.mu.Unlock() + for i := len(hooks) - 1; i >= 0; i-- { + fn = hooks[i](fn) + } + return fn.Rollback(tx.ctx, tx) +} + +// OnRollback adds a hook to call on rollback. +func (tx *Tx) OnRollback(f RollbackHook) { tx.mu.Lock() defer tx.mu.Unlock() tx.onRollback = append(tx.onRollback, f) diff --git a/examples/m2mrecur/ent/client.go b/examples/m2mrecur/ent/client.go index 1392991a6..48c4ce7d6 100644 --- a/examples/m2mrecur/ent/client.go +++ b/examples/m2mrecur/ent/client.go @@ -59,7 +59,8 @@ func Open(driverName, dataSourceName string, options ...Option) (*Client, error) } } -// Tx returns a new transactional client. +// Tx returns a new transactional client. The provided context +// is used until the transaction is committed or rolled back. func (c *Client) Tx(ctx context.Context) (*Tx, error) { if _, ok := c.driver.(*txDriver); ok { return nil, fmt.Errorf("ent: cannot start a transaction within a transaction") @@ -70,6 +71,7 @@ func (c *Client) Tx(ctx context.Context) (*Tx, error) { } cfg := config{driver: tx, log: c.log, debug: c.debug, hooks: c.hooks} return &Tx{ + ctx: ctx, config: cfg, User: NewUserClient(cfg), }, nil diff --git a/examples/m2mrecur/ent/tx.go b/examples/m2mrecur/ent/tx.go index 62082b404..bd5cb64a3 100644 --- a/examples/m2mrecur/ent/tx.go +++ b/examples/m2mrecur/ent/tx.go @@ -25,41 +25,119 @@ type Tx struct { // completion callbacks. mu sync.Mutex - onCommit []func(error) - onRollback []func(error) + onCommit []CommitHook + onRollback []RollbackHook + + // ctx lives for the life of the transaction. It is + // the same context used by the underlying connection. + ctx context.Context +} + +type ( + // Committer is the interface that wraps the Committer method. + Committer interface { + Commit(context.Context, *Tx) error + } + + // The CommitFunc type is an adapter to allow the use of ordinary + // function as a Committer. If f is a function with the appropriate + // signature, CommitFunc(f) is a Committer that calls f. + CommitFunc func(context.Context, *Tx) error + + // CommitHook defines the "commit middleware". A function that gets a Committer + // and returns a Committer. For example: + // + // hook := func(next ent.Committer) ent.Committer { + // return ent.CommitFunc(func(context.Context, tx *ent.Tx) error { + // // Do some stuff before. + // if err := next.Commit(ctx, tx); err != nil { + // return err + // } + // // Do some stuff after. + // return nil + // }) + // } + // + CommitHook func(Committer) Committer +) + +// Commit calls f(ctx, m). +func (f CommitFunc) Commit(ctx context.Context, tx *Tx) error { + return f(ctx, tx) } // Commit commits the transaction. func (tx *Tx) Commit() error { - err := tx.config.driver.(*txDriver).tx.Commit() + txDriver := tx.config.driver.(*txDriver) + var fn Committer = CommitFunc(func(context.Context, *Tx) error { + return txDriver.tx.Commit() + }) tx.mu.Lock() - defer tx.mu.Unlock() - for _, f := range tx.onCommit { - f(err) + hooks := append([]CommitHook(nil), tx.onCommit...) + tx.mu.Unlock() + for i := len(hooks) - 1; i >= 0; i-- { + fn = hooks[i](fn) } - return err + return fn.Commit(tx.ctx, tx) } -// OnCommit adds a function to call on commit. -func (tx *Tx) OnCommit(f func(error)) { +// OnCommit adds a hook to call on commit. +func (tx *Tx) OnCommit(f CommitHook) { tx.mu.Lock() defer tx.mu.Unlock() tx.onCommit = append(tx.onCommit, f) } -// Rollback rollbacks the transaction. -func (tx *Tx) Rollback() error { - err := tx.config.driver.(*txDriver).tx.Rollback() - tx.mu.Lock() - defer tx.mu.Unlock() - for _, f := range tx.onRollback { - f(err) +type ( + // Rollbacker is the interface that wraps the Rollbacker method. + Rollbacker interface { + Rollback(context.Context, *Tx) error } - return err + + // The RollbackFunc type is an adapter to allow the use of ordinary + // function as a Rollbacker. If f is a function with the appropriate + // signature, RollbackFunc(f) is a Rollbacker that calls f. + RollbackFunc func(context.Context, *Tx) error + + // RollbackHook defines the "rollback middleware". A function that gets a Rollbacker + // and returns a Rollbacker. For example: + // + // hook := func(next ent.Rollbacker) ent.Rollbacker { + // return ent.RollbackFunc(func(context.Context, tx *ent.Tx) error { + // // Do some stuff before. + // if err := next.Rollback(ctx, tx); err != nil { + // return err + // } + // // Do some stuff after. + // return nil + // }) + // } + // + RollbackHook func(Rollbacker) Rollbacker +) + +// Rollback calls f(ctx, m). +func (f RollbackFunc) Rollback(ctx context.Context, tx *Tx) error { + return f(ctx, tx) } -// OnRollback adds a function to call on rollback. -func (tx *Tx) OnRollback(f func(error)) { +// Rollback rollbacks the transaction. +func (tx *Tx) Rollback() error { + txDriver := tx.config.driver.(*txDriver) + var fn Rollbacker = RollbackFunc(func(context.Context, *Tx) error { + return txDriver.tx.Rollback() + }) + tx.mu.Lock() + hooks := append([]RollbackHook(nil), tx.onRollback...) + tx.mu.Unlock() + for i := len(hooks) - 1; i >= 0; i-- { + fn = hooks[i](fn) + } + return fn.Rollback(tx.ctx, tx) +} + +// OnRollback adds a hook to call on rollback. +func (tx *Tx) OnRollback(f RollbackHook) { tx.mu.Lock() defer tx.mu.Unlock() tx.onRollback = append(tx.onRollback, f) diff --git a/examples/o2m2types/ent/client.go b/examples/o2m2types/ent/client.go index 69a8383f5..5cbed52c8 100644 --- a/examples/o2m2types/ent/client.go +++ b/examples/o2m2types/ent/client.go @@ -63,7 +63,8 @@ func Open(driverName, dataSourceName string, options ...Option) (*Client, error) } } -// Tx returns a new transactional client. +// Tx returns a new transactional client. The provided context +// is used until the transaction is committed or rolled back. func (c *Client) Tx(ctx context.Context) (*Tx, error) { if _, ok := c.driver.(*txDriver); ok { return nil, fmt.Errorf("ent: cannot start a transaction within a transaction") @@ -74,6 +75,7 @@ func (c *Client) Tx(ctx context.Context) (*Tx, error) { } cfg := config{driver: tx, log: c.log, debug: c.debug, hooks: c.hooks} return &Tx{ + ctx: ctx, config: cfg, Pet: NewPetClient(cfg), User: NewUserClient(cfg), diff --git a/examples/o2m2types/ent/tx.go b/examples/o2m2types/ent/tx.go index c829488d1..b959e1f19 100644 --- a/examples/o2m2types/ent/tx.go +++ b/examples/o2m2types/ent/tx.go @@ -27,41 +27,119 @@ type Tx struct { // completion callbacks. mu sync.Mutex - onCommit []func(error) - onRollback []func(error) + onCommit []CommitHook + onRollback []RollbackHook + + // ctx lives for the life of the transaction. It is + // the same context used by the underlying connection. + ctx context.Context +} + +type ( + // Committer is the interface that wraps the Committer method. + Committer interface { + Commit(context.Context, *Tx) error + } + + // The CommitFunc type is an adapter to allow the use of ordinary + // function as a Committer. If f is a function with the appropriate + // signature, CommitFunc(f) is a Committer that calls f. + CommitFunc func(context.Context, *Tx) error + + // CommitHook defines the "commit middleware". A function that gets a Committer + // and returns a Committer. For example: + // + // hook := func(next ent.Committer) ent.Committer { + // return ent.CommitFunc(func(context.Context, tx *ent.Tx) error { + // // Do some stuff before. + // if err := next.Commit(ctx, tx); err != nil { + // return err + // } + // // Do some stuff after. + // return nil + // }) + // } + // + CommitHook func(Committer) Committer +) + +// Commit calls f(ctx, m). +func (f CommitFunc) Commit(ctx context.Context, tx *Tx) error { + return f(ctx, tx) } // Commit commits the transaction. func (tx *Tx) Commit() error { - err := tx.config.driver.(*txDriver).tx.Commit() + txDriver := tx.config.driver.(*txDriver) + var fn Committer = CommitFunc(func(context.Context, *Tx) error { + return txDriver.tx.Commit() + }) tx.mu.Lock() - defer tx.mu.Unlock() - for _, f := range tx.onCommit { - f(err) + hooks := append([]CommitHook(nil), tx.onCommit...) + tx.mu.Unlock() + for i := len(hooks) - 1; i >= 0; i-- { + fn = hooks[i](fn) } - return err + return fn.Commit(tx.ctx, tx) } -// OnCommit adds a function to call on commit. -func (tx *Tx) OnCommit(f func(error)) { +// OnCommit adds a hook to call on commit. +func (tx *Tx) OnCommit(f CommitHook) { tx.mu.Lock() defer tx.mu.Unlock() tx.onCommit = append(tx.onCommit, f) } -// Rollback rollbacks the transaction. -func (tx *Tx) Rollback() error { - err := tx.config.driver.(*txDriver).tx.Rollback() - tx.mu.Lock() - defer tx.mu.Unlock() - for _, f := range tx.onRollback { - f(err) +type ( + // Rollbacker is the interface that wraps the Rollbacker method. + Rollbacker interface { + Rollback(context.Context, *Tx) error } - return err + + // The RollbackFunc type is an adapter to allow the use of ordinary + // function as a Rollbacker. If f is a function with the appropriate + // signature, RollbackFunc(f) is a Rollbacker that calls f. + RollbackFunc func(context.Context, *Tx) error + + // RollbackHook defines the "rollback middleware". A function that gets a Rollbacker + // and returns a Rollbacker. For example: + // + // hook := func(next ent.Rollbacker) ent.Rollbacker { + // return ent.RollbackFunc(func(context.Context, tx *ent.Tx) error { + // // Do some stuff before. + // if err := next.Rollback(ctx, tx); err != nil { + // return err + // } + // // Do some stuff after. + // return nil + // }) + // } + // + RollbackHook func(Rollbacker) Rollbacker +) + +// Rollback calls f(ctx, m). +func (f RollbackFunc) Rollback(ctx context.Context, tx *Tx) error { + return f(ctx, tx) } -// OnRollback adds a function to call on rollback. -func (tx *Tx) OnRollback(f func(error)) { +// Rollback rollbacks the transaction. +func (tx *Tx) Rollback() error { + txDriver := tx.config.driver.(*txDriver) + var fn Rollbacker = RollbackFunc(func(context.Context, *Tx) error { + return txDriver.tx.Rollback() + }) + tx.mu.Lock() + hooks := append([]RollbackHook(nil), tx.onRollback...) + tx.mu.Unlock() + for i := len(hooks) - 1; i >= 0; i-- { + fn = hooks[i](fn) + } + return fn.Rollback(tx.ctx, tx) +} + +// OnRollback adds a hook to call on rollback. +func (tx *Tx) OnRollback(f RollbackHook) { tx.mu.Lock() defer tx.mu.Unlock() tx.onRollback = append(tx.onRollback, f) diff --git a/examples/o2mrecur/ent/client.go b/examples/o2mrecur/ent/client.go index 241bead55..9a787290a 100644 --- a/examples/o2mrecur/ent/client.go +++ b/examples/o2mrecur/ent/client.go @@ -59,7 +59,8 @@ func Open(driverName, dataSourceName string, options ...Option) (*Client, error) } } -// Tx returns a new transactional client. +// Tx returns a new transactional client. The provided context +// is used until the transaction is committed or rolled back. func (c *Client) Tx(ctx context.Context) (*Tx, error) { if _, ok := c.driver.(*txDriver); ok { return nil, fmt.Errorf("ent: cannot start a transaction within a transaction") @@ -70,6 +71,7 @@ func (c *Client) Tx(ctx context.Context) (*Tx, error) { } cfg := config{driver: tx, log: c.log, debug: c.debug, hooks: c.hooks} return &Tx{ + ctx: ctx, config: cfg, Node: NewNodeClient(cfg), }, nil diff --git a/examples/o2mrecur/ent/tx.go b/examples/o2mrecur/ent/tx.go index c45a1d2da..004c3855c 100644 --- a/examples/o2mrecur/ent/tx.go +++ b/examples/o2mrecur/ent/tx.go @@ -25,41 +25,119 @@ type Tx struct { // completion callbacks. mu sync.Mutex - onCommit []func(error) - onRollback []func(error) + onCommit []CommitHook + onRollback []RollbackHook + + // ctx lives for the life of the transaction. It is + // the same context used by the underlying connection. + ctx context.Context +} + +type ( + // Committer is the interface that wraps the Committer method. + Committer interface { + Commit(context.Context, *Tx) error + } + + // The CommitFunc type is an adapter to allow the use of ordinary + // function as a Committer. If f is a function with the appropriate + // signature, CommitFunc(f) is a Committer that calls f. + CommitFunc func(context.Context, *Tx) error + + // CommitHook defines the "commit middleware". A function that gets a Committer + // and returns a Committer. For example: + // + // hook := func(next ent.Committer) ent.Committer { + // return ent.CommitFunc(func(context.Context, tx *ent.Tx) error { + // // Do some stuff before. + // if err := next.Commit(ctx, tx); err != nil { + // return err + // } + // // Do some stuff after. + // return nil + // }) + // } + // + CommitHook func(Committer) Committer +) + +// Commit calls f(ctx, m). +func (f CommitFunc) Commit(ctx context.Context, tx *Tx) error { + return f(ctx, tx) } // Commit commits the transaction. func (tx *Tx) Commit() error { - err := tx.config.driver.(*txDriver).tx.Commit() + txDriver := tx.config.driver.(*txDriver) + var fn Committer = CommitFunc(func(context.Context, *Tx) error { + return txDriver.tx.Commit() + }) tx.mu.Lock() - defer tx.mu.Unlock() - for _, f := range tx.onCommit { - f(err) + hooks := append([]CommitHook(nil), tx.onCommit...) + tx.mu.Unlock() + for i := len(hooks) - 1; i >= 0; i-- { + fn = hooks[i](fn) } - return err + return fn.Commit(tx.ctx, tx) } -// OnCommit adds a function to call on commit. -func (tx *Tx) OnCommit(f func(error)) { +// OnCommit adds a hook to call on commit. +func (tx *Tx) OnCommit(f CommitHook) { tx.mu.Lock() defer tx.mu.Unlock() tx.onCommit = append(tx.onCommit, f) } -// Rollback rollbacks the transaction. -func (tx *Tx) Rollback() error { - err := tx.config.driver.(*txDriver).tx.Rollback() - tx.mu.Lock() - defer tx.mu.Unlock() - for _, f := range tx.onRollback { - f(err) +type ( + // Rollbacker is the interface that wraps the Rollbacker method. + Rollbacker interface { + Rollback(context.Context, *Tx) error } - return err + + // The RollbackFunc type is an adapter to allow the use of ordinary + // function as a Rollbacker. If f is a function with the appropriate + // signature, RollbackFunc(f) is a Rollbacker that calls f. + RollbackFunc func(context.Context, *Tx) error + + // RollbackHook defines the "rollback middleware". A function that gets a Rollbacker + // and returns a Rollbacker. For example: + // + // hook := func(next ent.Rollbacker) ent.Rollbacker { + // return ent.RollbackFunc(func(context.Context, tx *ent.Tx) error { + // // Do some stuff before. + // if err := next.Rollback(ctx, tx); err != nil { + // return err + // } + // // Do some stuff after. + // return nil + // }) + // } + // + RollbackHook func(Rollbacker) Rollbacker +) + +// Rollback calls f(ctx, m). +func (f RollbackFunc) Rollback(ctx context.Context, tx *Tx) error { + return f(ctx, tx) } -// OnRollback adds a function to call on rollback. -func (tx *Tx) OnRollback(f func(error)) { +// Rollback rollbacks the transaction. +func (tx *Tx) Rollback() error { + txDriver := tx.config.driver.(*txDriver) + var fn Rollbacker = RollbackFunc(func(context.Context, *Tx) error { + return txDriver.tx.Rollback() + }) + tx.mu.Lock() + hooks := append([]RollbackHook(nil), tx.onRollback...) + tx.mu.Unlock() + for i := len(hooks) - 1; i >= 0; i-- { + fn = hooks[i](fn) + } + return fn.Rollback(tx.ctx, tx) +} + +// OnRollback adds a hook to call on rollback. +func (tx *Tx) OnRollback(f RollbackHook) { tx.mu.Lock() defer tx.mu.Unlock() tx.onRollback = append(tx.onRollback, f) diff --git a/examples/o2o2types/ent/client.go b/examples/o2o2types/ent/client.go index 1be1b71b0..7d5348608 100644 --- a/examples/o2o2types/ent/client.go +++ b/examples/o2o2types/ent/client.go @@ -63,7 +63,8 @@ func Open(driverName, dataSourceName string, options ...Option) (*Client, error) } } -// Tx returns a new transactional client. +// Tx returns a new transactional client. The provided context +// is used until the transaction is committed or rolled back. func (c *Client) Tx(ctx context.Context) (*Tx, error) { if _, ok := c.driver.(*txDriver); ok { return nil, fmt.Errorf("ent: cannot start a transaction within a transaction") @@ -74,6 +75,7 @@ func (c *Client) Tx(ctx context.Context) (*Tx, error) { } cfg := config{driver: tx, log: c.log, debug: c.debug, hooks: c.hooks} return &Tx{ + ctx: ctx, config: cfg, Card: NewCardClient(cfg), User: NewUserClient(cfg), diff --git a/examples/o2o2types/ent/tx.go b/examples/o2o2types/ent/tx.go index cf4d56e06..8661f18f2 100644 --- a/examples/o2o2types/ent/tx.go +++ b/examples/o2o2types/ent/tx.go @@ -27,41 +27,119 @@ type Tx struct { // completion callbacks. mu sync.Mutex - onCommit []func(error) - onRollback []func(error) + onCommit []CommitHook + onRollback []RollbackHook + + // ctx lives for the life of the transaction. It is + // the same context used by the underlying connection. + ctx context.Context +} + +type ( + // Committer is the interface that wraps the Committer method. + Committer interface { + Commit(context.Context, *Tx) error + } + + // The CommitFunc type is an adapter to allow the use of ordinary + // function as a Committer. If f is a function with the appropriate + // signature, CommitFunc(f) is a Committer that calls f. + CommitFunc func(context.Context, *Tx) error + + // CommitHook defines the "commit middleware". A function that gets a Committer + // and returns a Committer. For example: + // + // hook := func(next ent.Committer) ent.Committer { + // return ent.CommitFunc(func(context.Context, tx *ent.Tx) error { + // // Do some stuff before. + // if err := next.Commit(ctx, tx); err != nil { + // return err + // } + // // Do some stuff after. + // return nil + // }) + // } + // + CommitHook func(Committer) Committer +) + +// Commit calls f(ctx, m). +func (f CommitFunc) Commit(ctx context.Context, tx *Tx) error { + return f(ctx, tx) } // Commit commits the transaction. func (tx *Tx) Commit() error { - err := tx.config.driver.(*txDriver).tx.Commit() + txDriver := tx.config.driver.(*txDriver) + var fn Committer = CommitFunc(func(context.Context, *Tx) error { + return txDriver.tx.Commit() + }) tx.mu.Lock() - defer tx.mu.Unlock() - for _, f := range tx.onCommit { - f(err) + hooks := append([]CommitHook(nil), tx.onCommit...) + tx.mu.Unlock() + for i := len(hooks) - 1; i >= 0; i-- { + fn = hooks[i](fn) } - return err + return fn.Commit(tx.ctx, tx) } -// OnCommit adds a function to call on commit. -func (tx *Tx) OnCommit(f func(error)) { +// OnCommit adds a hook to call on commit. +func (tx *Tx) OnCommit(f CommitHook) { tx.mu.Lock() defer tx.mu.Unlock() tx.onCommit = append(tx.onCommit, f) } -// Rollback rollbacks the transaction. -func (tx *Tx) Rollback() error { - err := tx.config.driver.(*txDriver).tx.Rollback() - tx.mu.Lock() - defer tx.mu.Unlock() - for _, f := range tx.onRollback { - f(err) +type ( + // Rollbacker is the interface that wraps the Rollbacker method. + Rollbacker interface { + Rollback(context.Context, *Tx) error } - return err + + // The RollbackFunc type is an adapter to allow the use of ordinary + // function as a Rollbacker. If f is a function with the appropriate + // signature, RollbackFunc(f) is a Rollbacker that calls f. + RollbackFunc func(context.Context, *Tx) error + + // RollbackHook defines the "rollback middleware". A function that gets a Rollbacker + // and returns a Rollbacker. For example: + // + // hook := func(next ent.Rollbacker) ent.Rollbacker { + // return ent.RollbackFunc(func(context.Context, tx *ent.Tx) error { + // // Do some stuff before. + // if err := next.Rollback(ctx, tx); err != nil { + // return err + // } + // // Do some stuff after. + // return nil + // }) + // } + // + RollbackHook func(Rollbacker) Rollbacker +) + +// Rollback calls f(ctx, m). +func (f RollbackFunc) Rollback(ctx context.Context, tx *Tx) error { + return f(ctx, tx) } -// OnRollback adds a function to call on rollback. -func (tx *Tx) OnRollback(f func(error)) { +// Rollback rollbacks the transaction. +func (tx *Tx) Rollback() error { + txDriver := tx.config.driver.(*txDriver) + var fn Rollbacker = RollbackFunc(func(context.Context, *Tx) error { + return txDriver.tx.Rollback() + }) + tx.mu.Lock() + hooks := append([]RollbackHook(nil), tx.onRollback...) + tx.mu.Unlock() + for i := len(hooks) - 1; i >= 0; i-- { + fn = hooks[i](fn) + } + return fn.Rollback(tx.ctx, tx) +} + +// OnRollback adds a hook to call on rollback. +func (tx *Tx) OnRollback(f RollbackHook) { tx.mu.Lock() defer tx.mu.Unlock() tx.onRollback = append(tx.onRollback, f) diff --git a/examples/o2obidi/ent/client.go b/examples/o2obidi/ent/client.go index e8a9c2cac..87d6c010a 100644 --- a/examples/o2obidi/ent/client.go +++ b/examples/o2obidi/ent/client.go @@ -59,7 +59,8 @@ func Open(driverName, dataSourceName string, options ...Option) (*Client, error) } } -// Tx returns a new transactional client. +// Tx returns a new transactional client. The provided context +// is used until the transaction is committed or rolled back. func (c *Client) Tx(ctx context.Context) (*Tx, error) { if _, ok := c.driver.(*txDriver); ok { return nil, fmt.Errorf("ent: cannot start a transaction within a transaction") @@ -70,6 +71,7 @@ func (c *Client) Tx(ctx context.Context) (*Tx, error) { } cfg := config{driver: tx, log: c.log, debug: c.debug, hooks: c.hooks} return &Tx{ + ctx: ctx, config: cfg, User: NewUserClient(cfg), }, nil diff --git a/examples/o2obidi/ent/tx.go b/examples/o2obidi/ent/tx.go index 62082b404..bd5cb64a3 100644 --- a/examples/o2obidi/ent/tx.go +++ b/examples/o2obidi/ent/tx.go @@ -25,41 +25,119 @@ type Tx struct { // completion callbacks. mu sync.Mutex - onCommit []func(error) - onRollback []func(error) + onCommit []CommitHook + onRollback []RollbackHook + + // ctx lives for the life of the transaction. It is + // the same context used by the underlying connection. + ctx context.Context +} + +type ( + // Committer is the interface that wraps the Committer method. + Committer interface { + Commit(context.Context, *Tx) error + } + + // The CommitFunc type is an adapter to allow the use of ordinary + // function as a Committer. If f is a function with the appropriate + // signature, CommitFunc(f) is a Committer that calls f. + CommitFunc func(context.Context, *Tx) error + + // CommitHook defines the "commit middleware". A function that gets a Committer + // and returns a Committer. For example: + // + // hook := func(next ent.Committer) ent.Committer { + // return ent.CommitFunc(func(context.Context, tx *ent.Tx) error { + // // Do some stuff before. + // if err := next.Commit(ctx, tx); err != nil { + // return err + // } + // // Do some stuff after. + // return nil + // }) + // } + // + CommitHook func(Committer) Committer +) + +// Commit calls f(ctx, m). +func (f CommitFunc) Commit(ctx context.Context, tx *Tx) error { + return f(ctx, tx) } // Commit commits the transaction. func (tx *Tx) Commit() error { - err := tx.config.driver.(*txDriver).tx.Commit() + txDriver := tx.config.driver.(*txDriver) + var fn Committer = CommitFunc(func(context.Context, *Tx) error { + return txDriver.tx.Commit() + }) tx.mu.Lock() - defer tx.mu.Unlock() - for _, f := range tx.onCommit { - f(err) + hooks := append([]CommitHook(nil), tx.onCommit...) + tx.mu.Unlock() + for i := len(hooks) - 1; i >= 0; i-- { + fn = hooks[i](fn) } - return err + return fn.Commit(tx.ctx, tx) } -// OnCommit adds a function to call on commit. -func (tx *Tx) OnCommit(f func(error)) { +// OnCommit adds a hook to call on commit. +func (tx *Tx) OnCommit(f CommitHook) { tx.mu.Lock() defer tx.mu.Unlock() tx.onCommit = append(tx.onCommit, f) } -// Rollback rollbacks the transaction. -func (tx *Tx) Rollback() error { - err := tx.config.driver.(*txDriver).tx.Rollback() - tx.mu.Lock() - defer tx.mu.Unlock() - for _, f := range tx.onRollback { - f(err) +type ( + // Rollbacker is the interface that wraps the Rollbacker method. + Rollbacker interface { + Rollback(context.Context, *Tx) error } - return err + + // The RollbackFunc type is an adapter to allow the use of ordinary + // function as a Rollbacker. If f is a function with the appropriate + // signature, RollbackFunc(f) is a Rollbacker that calls f. + RollbackFunc func(context.Context, *Tx) error + + // RollbackHook defines the "rollback middleware". A function that gets a Rollbacker + // and returns a Rollbacker. For example: + // + // hook := func(next ent.Rollbacker) ent.Rollbacker { + // return ent.RollbackFunc(func(context.Context, tx *ent.Tx) error { + // // Do some stuff before. + // if err := next.Rollback(ctx, tx); err != nil { + // return err + // } + // // Do some stuff after. + // return nil + // }) + // } + // + RollbackHook func(Rollbacker) Rollbacker +) + +// Rollback calls f(ctx, m). +func (f RollbackFunc) Rollback(ctx context.Context, tx *Tx) error { + return f(ctx, tx) } -// OnRollback adds a function to call on rollback. -func (tx *Tx) OnRollback(f func(error)) { +// Rollback rollbacks the transaction. +func (tx *Tx) Rollback() error { + txDriver := tx.config.driver.(*txDriver) + var fn Rollbacker = RollbackFunc(func(context.Context, *Tx) error { + return txDriver.tx.Rollback() + }) + tx.mu.Lock() + hooks := append([]RollbackHook(nil), tx.onRollback...) + tx.mu.Unlock() + for i := len(hooks) - 1; i >= 0; i-- { + fn = hooks[i](fn) + } + return fn.Rollback(tx.ctx, tx) +} + +// OnRollback adds a hook to call on rollback. +func (tx *Tx) OnRollback(f RollbackHook) { tx.mu.Lock() defer tx.mu.Unlock() tx.onRollback = append(tx.onRollback, f) diff --git a/examples/o2orecur/ent/client.go b/examples/o2orecur/ent/client.go index 67ad1892f..d8dd35fbf 100644 --- a/examples/o2orecur/ent/client.go +++ b/examples/o2orecur/ent/client.go @@ -59,7 +59,8 @@ func Open(driverName, dataSourceName string, options ...Option) (*Client, error) } } -// Tx returns a new transactional client. +// Tx returns a new transactional client. The provided context +// is used until the transaction is committed or rolled back. func (c *Client) Tx(ctx context.Context) (*Tx, error) { if _, ok := c.driver.(*txDriver); ok { return nil, fmt.Errorf("ent: cannot start a transaction within a transaction") @@ -70,6 +71,7 @@ func (c *Client) Tx(ctx context.Context) (*Tx, error) { } cfg := config{driver: tx, log: c.log, debug: c.debug, hooks: c.hooks} return &Tx{ + ctx: ctx, config: cfg, Node: NewNodeClient(cfg), }, nil diff --git a/examples/o2orecur/ent/tx.go b/examples/o2orecur/ent/tx.go index c45a1d2da..004c3855c 100644 --- a/examples/o2orecur/ent/tx.go +++ b/examples/o2orecur/ent/tx.go @@ -25,41 +25,119 @@ type Tx struct { // completion callbacks. mu sync.Mutex - onCommit []func(error) - onRollback []func(error) + onCommit []CommitHook + onRollback []RollbackHook + + // ctx lives for the life of the transaction. It is + // the same context used by the underlying connection. + ctx context.Context +} + +type ( + // Committer is the interface that wraps the Committer method. + Committer interface { + Commit(context.Context, *Tx) error + } + + // The CommitFunc type is an adapter to allow the use of ordinary + // function as a Committer. If f is a function with the appropriate + // signature, CommitFunc(f) is a Committer that calls f. + CommitFunc func(context.Context, *Tx) error + + // CommitHook defines the "commit middleware". A function that gets a Committer + // and returns a Committer. For example: + // + // hook := func(next ent.Committer) ent.Committer { + // return ent.CommitFunc(func(context.Context, tx *ent.Tx) error { + // // Do some stuff before. + // if err := next.Commit(ctx, tx); err != nil { + // return err + // } + // // Do some stuff after. + // return nil + // }) + // } + // + CommitHook func(Committer) Committer +) + +// Commit calls f(ctx, m). +func (f CommitFunc) Commit(ctx context.Context, tx *Tx) error { + return f(ctx, tx) } // Commit commits the transaction. func (tx *Tx) Commit() error { - err := tx.config.driver.(*txDriver).tx.Commit() + txDriver := tx.config.driver.(*txDriver) + var fn Committer = CommitFunc(func(context.Context, *Tx) error { + return txDriver.tx.Commit() + }) tx.mu.Lock() - defer tx.mu.Unlock() - for _, f := range tx.onCommit { - f(err) + hooks := append([]CommitHook(nil), tx.onCommit...) + tx.mu.Unlock() + for i := len(hooks) - 1; i >= 0; i-- { + fn = hooks[i](fn) } - return err + return fn.Commit(tx.ctx, tx) } -// OnCommit adds a function to call on commit. -func (tx *Tx) OnCommit(f func(error)) { +// OnCommit adds a hook to call on commit. +func (tx *Tx) OnCommit(f CommitHook) { tx.mu.Lock() defer tx.mu.Unlock() tx.onCommit = append(tx.onCommit, f) } -// Rollback rollbacks the transaction. -func (tx *Tx) Rollback() error { - err := tx.config.driver.(*txDriver).tx.Rollback() - tx.mu.Lock() - defer tx.mu.Unlock() - for _, f := range tx.onRollback { - f(err) +type ( + // Rollbacker is the interface that wraps the Rollbacker method. + Rollbacker interface { + Rollback(context.Context, *Tx) error } - return err + + // The RollbackFunc type is an adapter to allow the use of ordinary + // function as a Rollbacker. If f is a function with the appropriate + // signature, RollbackFunc(f) is a Rollbacker that calls f. + RollbackFunc func(context.Context, *Tx) error + + // RollbackHook defines the "rollback middleware". A function that gets a Rollbacker + // and returns a Rollbacker. For example: + // + // hook := func(next ent.Rollbacker) ent.Rollbacker { + // return ent.RollbackFunc(func(context.Context, tx *ent.Tx) error { + // // Do some stuff before. + // if err := next.Rollback(ctx, tx); err != nil { + // return err + // } + // // Do some stuff after. + // return nil + // }) + // } + // + RollbackHook func(Rollbacker) Rollbacker +) + +// Rollback calls f(ctx, m). +func (f RollbackFunc) Rollback(ctx context.Context, tx *Tx) error { + return f(ctx, tx) } -// OnRollback adds a function to call on rollback. -func (tx *Tx) OnRollback(f func(error)) { +// Rollback rollbacks the transaction. +func (tx *Tx) Rollback() error { + txDriver := tx.config.driver.(*txDriver) + var fn Rollbacker = RollbackFunc(func(context.Context, *Tx) error { + return txDriver.tx.Rollback() + }) + tx.mu.Lock() + hooks := append([]RollbackHook(nil), tx.onRollback...) + tx.mu.Unlock() + for i := len(hooks) - 1; i >= 0; i-- { + fn = hooks[i](fn) + } + return fn.Rollback(tx.ctx, tx) +} + +// OnRollback adds a hook to call on rollback. +func (tx *Tx) OnRollback(f RollbackHook) { tx.mu.Lock() defer tx.mu.Unlock() tx.onRollback = append(tx.onRollback, f) diff --git a/examples/start/ent/client.go b/examples/start/ent/client.go index 740885d82..239d013f4 100644 --- a/examples/start/ent/client.go +++ b/examples/start/ent/client.go @@ -67,7 +67,8 @@ func Open(driverName, dataSourceName string, options ...Option) (*Client, error) } } -// Tx returns a new transactional client. +// Tx returns a new transactional client. The provided context +// is used until the transaction is committed or rolled back. func (c *Client) Tx(ctx context.Context) (*Tx, error) { if _, ok := c.driver.(*txDriver); ok { return nil, fmt.Errorf("ent: cannot start a transaction within a transaction") @@ -78,6 +79,7 @@ func (c *Client) Tx(ctx context.Context) (*Tx, error) { } cfg := config{driver: tx, log: c.log, debug: c.debug, hooks: c.hooks} return &Tx{ + ctx: ctx, config: cfg, Car: NewCarClient(cfg), Group: NewGroupClient(cfg), diff --git a/examples/start/ent/tx.go b/examples/start/ent/tx.go index f3bde1012..811607284 100644 --- a/examples/start/ent/tx.go +++ b/examples/start/ent/tx.go @@ -29,41 +29,119 @@ type Tx struct { // completion callbacks. mu sync.Mutex - onCommit []func(error) - onRollback []func(error) + onCommit []CommitHook + onRollback []RollbackHook + + // ctx lives for the life of the transaction. It is + // the same context used by the underlying connection. + ctx context.Context +} + +type ( + // Committer is the interface that wraps the Committer method. + Committer interface { + Commit(context.Context, *Tx) error + } + + // The CommitFunc type is an adapter to allow the use of ordinary + // function as a Committer. If f is a function with the appropriate + // signature, CommitFunc(f) is a Committer that calls f. + CommitFunc func(context.Context, *Tx) error + + // CommitHook defines the "commit middleware". A function that gets a Committer + // and returns a Committer. For example: + // + // hook := func(next ent.Committer) ent.Committer { + // return ent.CommitFunc(func(context.Context, tx *ent.Tx) error { + // // Do some stuff before. + // if err := next.Commit(ctx, tx); err != nil { + // return err + // } + // // Do some stuff after. + // return nil + // }) + // } + // + CommitHook func(Committer) Committer +) + +// Commit calls f(ctx, m). +func (f CommitFunc) Commit(ctx context.Context, tx *Tx) error { + return f(ctx, tx) } // Commit commits the transaction. func (tx *Tx) Commit() error { - err := tx.config.driver.(*txDriver).tx.Commit() + txDriver := tx.config.driver.(*txDriver) + var fn Committer = CommitFunc(func(context.Context, *Tx) error { + return txDriver.tx.Commit() + }) tx.mu.Lock() - defer tx.mu.Unlock() - for _, f := range tx.onCommit { - f(err) + hooks := append([]CommitHook(nil), tx.onCommit...) + tx.mu.Unlock() + for i := len(hooks) - 1; i >= 0; i-- { + fn = hooks[i](fn) } - return err + return fn.Commit(tx.ctx, tx) } -// OnCommit adds a function to call on commit. -func (tx *Tx) OnCommit(f func(error)) { +// OnCommit adds a hook to call on commit. +func (tx *Tx) OnCommit(f CommitHook) { tx.mu.Lock() defer tx.mu.Unlock() tx.onCommit = append(tx.onCommit, f) } -// Rollback rollbacks the transaction. -func (tx *Tx) Rollback() error { - err := tx.config.driver.(*txDriver).tx.Rollback() - tx.mu.Lock() - defer tx.mu.Unlock() - for _, f := range tx.onRollback { - f(err) +type ( + // Rollbacker is the interface that wraps the Rollbacker method. + Rollbacker interface { + Rollback(context.Context, *Tx) error } - return err + + // The RollbackFunc type is an adapter to allow the use of ordinary + // function as a Rollbacker. If f is a function with the appropriate + // signature, RollbackFunc(f) is a Rollbacker that calls f. + RollbackFunc func(context.Context, *Tx) error + + // RollbackHook defines the "rollback middleware". A function that gets a Rollbacker + // and returns a Rollbacker. For example: + // + // hook := func(next ent.Rollbacker) ent.Rollbacker { + // return ent.RollbackFunc(func(context.Context, tx *ent.Tx) error { + // // Do some stuff before. + // if err := next.Rollback(ctx, tx); err != nil { + // return err + // } + // // Do some stuff after. + // return nil + // }) + // } + // + RollbackHook func(Rollbacker) Rollbacker +) + +// Rollback calls f(ctx, m). +func (f RollbackFunc) Rollback(ctx context.Context, tx *Tx) error { + return f(ctx, tx) } -// OnRollback adds a function to call on rollback. -func (tx *Tx) OnRollback(f func(error)) { +// Rollback rollbacks the transaction. +func (tx *Tx) Rollback() error { + txDriver := tx.config.driver.(*txDriver) + var fn Rollbacker = RollbackFunc(func(context.Context, *Tx) error { + return txDriver.tx.Rollback() + }) + tx.mu.Lock() + hooks := append([]RollbackHook(nil), tx.onRollback...) + tx.mu.Unlock() + for i := len(hooks) - 1; i >= 0; i-- { + fn = hooks[i](fn) + } + return fn.Rollback(tx.ctx, tx) +} + +// OnRollback adds a hook to call on rollback. +func (tx *Tx) OnRollback(f RollbackHook) { tx.mu.Lock() defer tx.mu.Unlock() tx.onRollback = append(tx.onRollback, f) diff --git a/examples/traversal/ent/client.go b/examples/traversal/ent/client.go index 5365c2b5e..a8d35b643 100644 --- a/examples/traversal/ent/client.go +++ b/examples/traversal/ent/client.go @@ -67,7 +67,8 @@ func Open(driverName, dataSourceName string, options ...Option) (*Client, error) } } -// Tx returns a new transactional client. +// Tx returns a new transactional client. The provided context +// is used until the transaction is committed or rolled back. func (c *Client) Tx(ctx context.Context) (*Tx, error) { if _, ok := c.driver.(*txDriver); ok { return nil, fmt.Errorf("ent: cannot start a transaction within a transaction") @@ -78,6 +79,7 @@ func (c *Client) Tx(ctx context.Context) (*Tx, error) { } cfg := config{driver: tx, log: c.log, debug: c.debug, hooks: c.hooks} return &Tx{ + ctx: ctx, config: cfg, Group: NewGroupClient(cfg), Pet: NewPetClient(cfg), diff --git a/examples/traversal/ent/tx.go b/examples/traversal/ent/tx.go index be5bd0e86..2b63ca443 100644 --- a/examples/traversal/ent/tx.go +++ b/examples/traversal/ent/tx.go @@ -29,41 +29,119 @@ type Tx struct { // completion callbacks. mu sync.Mutex - onCommit []func(error) - onRollback []func(error) + onCommit []CommitHook + onRollback []RollbackHook + + // ctx lives for the life of the transaction. It is + // the same context used by the underlying connection. + ctx context.Context +} + +type ( + // Committer is the interface that wraps the Committer method. + Committer interface { + Commit(context.Context, *Tx) error + } + + // The CommitFunc type is an adapter to allow the use of ordinary + // function as a Committer. If f is a function with the appropriate + // signature, CommitFunc(f) is a Committer that calls f. + CommitFunc func(context.Context, *Tx) error + + // CommitHook defines the "commit middleware". A function that gets a Committer + // and returns a Committer. For example: + // + // hook := func(next ent.Committer) ent.Committer { + // return ent.CommitFunc(func(context.Context, tx *ent.Tx) error { + // // Do some stuff before. + // if err := next.Commit(ctx, tx); err != nil { + // return err + // } + // // Do some stuff after. + // return nil + // }) + // } + // + CommitHook func(Committer) Committer +) + +// Commit calls f(ctx, m). +func (f CommitFunc) Commit(ctx context.Context, tx *Tx) error { + return f(ctx, tx) } // Commit commits the transaction. func (tx *Tx) Commit() error { - err := tx.config.driver.(*txDriver).tx.Commit() + txDriver := tx.config.driver.(*txDriver) + var fn Committer = CommitFunc(func(context.Context, *Tx) error { + return txDriver.tx.Commit() + }) tx.mu.Lock() - defer tx.mu.Unlock() - for _, f := range tx.onCommit { - f(err) + hooks := append([]CommitHook(nil), tx.onCommit...) + tx.mu.Unlock() + for i := len(hooks) - 1; i >= 0; i-- { + fn = hooks[i](fn) } - return err + return fn.Commit(tx.ctx, tx) } -// OnCommit adds a function to call on commit. -func (tx *Tx) OnCommit(f func(error)) { +// OnCommit adds a hook to call on commit. +func (tx *Tx) OnCommit(f CommitHook) { tx.mu.Lock() defer tx.mu.Unlock() tx.onCommit = append(tx.onCommit, f) } -// Rollback rollbacks the transaction. -func (tx *Tx) Rollback() error { - err := tx.config.driver.(*txDriver).tx.Rollback() - tx.mu.Lock() - defer tx.mu.Unlock() - for _, f := range tx.onRollback { - f(err) +type ( + // Rollbacker is the interface that wraps the Rollbacker method. + Rollbacker interface { + Rollback(context.Context, *Tx) error } - return err + + // The RollbackFunc type is an adapter to allow the use of ordinary + // function as a Rollbacker. If f is a function with the appropriate + // signature, RollbackFunc(f) is a Rollbacker that calls f. + RollbackFunc func(context.Context, *Tx) error + + // RollbackHook defines the "rollback middleware". A function that gets a Rollbacker + // and returns a Rollbacker. For example: + // + // hook := func(next ent.Rollbacker) ent.Rollbacker { + // return ent.RollbackFunc(func(context.Context, tx *ent.Tx) error { + // // Do some stuff before. + // if err := next.Rollback(ctx, tx); err != nil { + // return err + // } + // // Do some stuff after. + // return nil + // }) + // } + // + RollbackHook func(Rollbacker) Rollbacker +) + +// Rollback calls f(ctx, m). +func (f RollbackFunc) Rollback(ctx context.Context, tx *Tx) error { + return f(ctx, tx) } -// OnRollback adds a function to call on rollback. -func (tx *Tx) OnRollback(f func(error)) { +// Rollback rollbacks the transaction. +func (tx *Tx) Rollback() error { + txDriver := tx.config.driver.(*txDriver) + var fn Rollbacker = RollbackFunc(func(context.Context, *Tx) error { + return txDriver.tx.Rollback() + }) + tx.mu.Lock() + hooks := append([]RollbackHook(nil), tx.onRollback...) + tx.mu.Unlock() + for i := len(hooks) - 1; i >= 0; i-- { + fn = hooks[i](fn) + } + return fn.Rollback(tx.ctx, tx) +} + +// OnRollback adds a hook to call on rollback. +func (tx *Tx) OnRollback(f RollbackHook) { tx.mu.Lock() defer tx.mu.Unlock() tx.onRollback = append(tx.onRollback, f)