diff --git a/entc/gen/internal/bindata.go b/entc/gen/internal/bindata.go index 5c11a6c75..da4fc09d2 100644 --- a/entc/gen/internal/bindata.go +++ b/entc/gen/internal/bindata.go @@ -926,7 +926,7 @@ func templateHeaderTmpl() (*asset, error) { return a, nil } -var _templateHookTmpl = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xe4\x56\x51\x6f\xdc\x36\x0c\x7e\xb6\x7f\x05\x67\x5c\x00\xbb\x73\xe4\xb4\x6f\x5b\x91\x01\x45\x96\x62\x05\xba\x64\xd8\xb2\xbd\x14\x7d\x50\x6d\xfa\xac\x9d\x2c\x19\x92\x9c\xbb\x83\xe7\xff\x3e\x50\xb2\xef\xdc\x78\xe8\x82\x62\x6f\x7b\x69\x73\x24\xc5\x8f\xfa\xf8\x91\xf2\x30\x14\x2f\xe2\x1b\xdd\x1d\x8d\xd8\x36\x0e\x5e\x5d\xbd\xfc\xee\xb2\x33\x68\x51\x39\x78\xcb\x4b\xfc\xa4\xf5\x0e\xde\xa9\x92\xc1\x1b\x29\xc1\x07\x59\x20\xbf\x79\xc4\x8a\xc5\x0f\x8d\xb0\x60\x75\x6f\x4a\x84\x52\x57\x08\xc2\x82\x14\x25\x2a\x8b\x15\xf4\xaa\x42\x03\xae\x41\x78\xd3\xf1\xb2\x41\x78\xc5\xae\x66\x2f\xd4\xba\x57\x55\x2c\x94\xf7\xbf\x7f\x77\x73\x7b\xf7\xdb\x2d\xd4\x42\x22\x4c\x36\xa3\xb5\x83\x4a\x18\x2c\x9d\x36\x47\xd0\x35\xb8\x05\x98\x33\x88\x2c\x7e\x51\x8c\x63\x1c\x0f\x03\x54\x58\x0b\x85\x90\x34\x5a\xef\x12\x98\x8c\x7b\xe1\x1a\xc0\x83\x43\x55\xc1\x06\x92\x5f\x78\xb9\xe3\x5b\x4c\x16\x51\xd1\x30\x80\xc3\xb6\x93\xdc\xd1\x61\xe4\x15\x9a\x04\x18\xb9\x86\x01\xe8\x1c\xa5\x12\x6d\xa7\x8d\x83\x64\x18\x60\xc3\x6e\xb4\xaa\xc5\x96\x4d\xc9\x60\x1c\x13\x8f\xb5\xe9\x76\x5b\xf8\xfe\x1a\x3e\x71\x8b\xff\x14\xe5\x83\x0c\x57\x5b\x84\x8d\xa2\xc0\x0d\xbb\xd3\x15\xda\xb9\x8a\x8d\xe2\x2d\x92\xbd\x33\x42\x39\xd8\x28\x76\x47\x86\xe4\x6d\xaf\xca\x53\xa9\x1b\x77\xec\xce\x41\x35\x24\x2f\x2e\x2c\xbb\xb0\x49\x40\xdf\x28\xf6\x73\xef\xb8\x13\x5a\xf9\xb3\x04\x1a\x15\x05\x3c\x34\x08\x27\x84\x71\x04\x9f\x44\x58\xe0\x0a\x78\xc5\x3b\x47\x3d\xd2\xc0\xa5\xd4\x7b\x4f\x7c\x6f\x91\xd8\xd6\xa6\x12\x8a\x9b\xa3\xcf\x51\xf7\xaa\xa4\xc4\xc0\x6d\xc8\xc5\x26\x08\x68\x09\x52\x1b\x16\x47\x3e\xef\x12\x88\x0e\xa5\xa5\x56\x0e\x0f\x8e\x18\xa1\xff\x73\x38\xdd\x63\x1c\x33\x48\x67\xea\xc6\x91\xfd\xc1\x65\x8f\x39\xa0\x31\xda\x64\xa1\x74\x7f\x1f\x84\x92\x4b\x69\xa1\x4e\x4b\x77\xc8\xa1\xcd\x58\x1c\x51\x6a\x48\xeb\x25\x5c\x36\x45\x53\x14\xac\x50\x5b\x58\x20\xcd\x34\x7d\x01\x1f\x86\x38\x8a\xda\xc7\x1c\xf4\x8e\x08\x6f\x59\xba\xac\x3b\x8e\x22\x51\xc3\x37\x7a\xe7\xc3\x22\x83\xae\x37\x0a\x94\x90\x39\xd4\xad\x63\xb7\x94\xa2\x4e\x93\x5e\xe1\xa1\xc3\xd2\x61\x15\x68\x22\x02\x7d\x8a\x8b\x07\x06\xc1\xb5\xa4\x23\xa1\xcb\xc5\x51\x34\xc6\xa7\x94\xf3\x9d\x1f\xb3\x38\xfa\x4c\x93\x45\x01\xf7\x0a\xf0\x80\x65\xef\xd0\xfa\xbe\x6d\xc5\x23\x2a\x20\x6d\x83\x56\x72\x9a\x98\xd9\xac\x3b\x34\xbe\x00\x16\x17\x45\x5c\x14\x11\xc5\xb1\x7b\x95\xbe\xd7\xdb\x7c\xc9\xcd\x8f\x28\xd1\xe1\x5f\x0b\xcb\x8d\x41\xee\x30\xa3\x73\x9e\xf6\x7b\x95\x36\xbb\xe5\x91\x9f\xb4\xde\xe5\xa0\xbb\xa5\xed\xbe\xcb\x9e\x86\x10\x57\xf3\xb5\x48\x19\x0a\x0f\x6e\xd5\x15\x4f\xfd\xca\xe6\x69\x9e\xce\x3e\xf5\x22\x8d\x48\x1a\xb4\xf6\x9f\x75\x9e\xda\xdb\xb2\xfb\x2e\xcd\xd8\x3b\x9b\xea\x6e\xb2\xce\x35\x34\x3b\x5f\x7d\xc6\xce\x92\x9b\x7a\xe7\x9b\x77\x12\x04\x15\xb2\x0a\x19\x7d\x2f\x43\x0f\x7f\xc5\x3f\x49\x04\x21\xde\x02\x0f\xed\x73\x0d\x27\x1b\xb9\x2c\x0d\xe6\xb9\x7b\x36\xf8\x5a\xee\xca\x06\x74\x37\x37\x33\x8c\xc3\x43\x06\xc4\xb3\x4d\x33\xf8\xf0\x71\x4d\x7e\x51\x9c\x0a\x5b\xb9\x83\x37\x0a\xe5\xa4\x5f\x94\xc3\xef\x5d\x45\x72\xc8\xfd\x89\x91\xfe\x1d\x4f\xd2\x98\xce\xff\x9f\xa4\xb0\x1a\xfa\x0b\x7b\x6e\x17\xad\x59\xa5\x5d\x58\xae\x58\xd1\x80\xfb\x4c\x5f\xa1\x94\x9b\x86\x0b\x05\xdc\x2b\x82\x84\x22\x85\x75\x34\xe2\x24\x18\xda\xe5\x15\x61\x61\x5d\x63\xe9\xc4\x23\xca\x23\x88\x96\x76\xce\x27\x89\x2c\x2c\x0b\x7a\xa1\xfd\x24\x57\x39\x08\x07\x7b\x21\x25\x70\xb9\xe7\x47\x0b\x8d\x96\x95\xdf\x15\x96\x56\xa9\xc5\x45\xe2\xe9\x31\xf6\x0e\x6d\x2a\x34\x2c\xf6\xdb\x2a\x94\x63\x9d\xe9\x69\x87\xc5\x51\x88\x5e\xe9\x6a\x2a\xfe\x0e\xf7\xe1\x40\xa8\x80\xea\x57\xb8\x87\xd2\xdb\x66\x2c\x16\x34\x34\xc7\xa6\x21\x25\x63\xec\x49\xce\x6c\x02\x3f\x6b\xc8\xff\x1e\x78\xd7\xa1\xaa\xd2\x55\x0d\xa9\x12\x32\xcb\x27\x0c\xc6\xb2\x99\x51\x2f\x44\x5f\x42\xd8\x9f\x6b\x46\xe7\xb1\x24\x6f\x2d\x14\x97\xde\x37\xd5\x99\x96\x01\x37\x0c\x5d\xfa\xaf\x1a\x6f\x67\xfd\x3e\x5f\xe6\xb5\x36\x20\xe8\xf9\x91\xa8\xd2\x92\xf9\xc2\x32\xb8\x84\x97\xaf\x41\xc0\x0f\xd7\x70\xf5\x1a\xc4\xe5\x65\xd0\xe3\x9c\xfe\x1a\xa6\xc0\x0f\xe2\xe3\x8c\xf9\xe4\x51\x99\xac\x67\x6d\xbd\xf1\xcc\x4d\x9f\x4a\xd4\x1c\xcf\x4a\x0e\xbc\xaa\x84\xda\x06\x01\x74\x58\x8a\x5a\x60\xe5\x29\xa0\x43\x7c\x62\x8d\x13\x6b\x0a\x4f\x52\x39\xbd\x74\xb5\xd4\xfb\x15\x57\x01\xea\x39\xbd\x55\xb8\xf7\xdb\xcc\x3f\xbf\x7c\x87\xeb\xc6\xe6\x70\x95\x7f\x46\xcd\xb7\xf4\x23\xfc\x99\x2d\x12\x5c\xc3\x24\x8d\xd9\x92\xcf\x1c\x91\x1c\xbe\x1c\xb8\x08\xfb\x4c\x6c\x73\xc4\x4c\xe1\x6d\xf8\xcc\x7c\x1e\x85\xde\xf7\xf5\x1c\x06\xac\x34\x4c\xcf\x64\x7b\x3a\x12\x25\x9b\x98\xf6\x51\x8b\xdb\x86\x2f\xd1\xe9\x33\xe2\xef\x00\x00\x00\xff\xff\xe7\x7b\xf9\x06\xfe\x0b\x00\x00") +var _templateHookTmpl = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xac\x56\x5f\x6f\xdb\xb6\x17\x7d\x96\x3e\xc5\xfd\x19\x0e\x20\xf5\xa7\xd0\x69\xdf\xb6\x22\x03\x8a\x2c\xc5\x0a\x74\xc9\xb0\xa5\x7b\x29\xba\x81\x95\xae\x2c\xce\x14\x29\x90\x74\x1c\x43\xd3\x77\x1f\x2e\x49\xd9\x8a\xb5\x75\x41\x97\x97\x36\xbe\xff\x79\xce\xe1\x15\xfb\x7e\xf5\x22\xbd\xd2\xdd\xde\x88\x75\xe3\xe0\xd5\xc5\xcb\x6f\xce\x3b\x83\x16\x95\x83\xb7\xbc\xc4\xcf\x5a\x6f\xe0\x9d\x2a\x19\xbc\x91\x12\x7c\x90\x05\xf2\x9b\x7b\xac\x58\x7a\xd7\x08\x0b\x56\x6f\x4d\x89\x50\xea\x0a\x41\x58\x90\xa2\x44\x65\xb1\x82\xad\xaa\xd0\x80\x6b\x10\xde\x74\xbc\x6c\x10\x5e\xb1\x8b\xd1\x0b\xb5\xde\xaa\x2a\x15\xca\xfb\xdf\xbf\xbb\xba\xbe\xf9\xe5\x1a\x6a\x21\x11\xa2\xcd\x68\xed\xa0\x12\x06\x4b\xa7\xcd\x1e\x74\x0d\x6e\xd2\xcc\x19\x44\x96\xbe\x58\x0d\x43\x9a\xf6\x3d\x54\x58\x0b\x85\xb0\x68\xb4\xde\x2c\x20\x1a\x77\xc2\x35\x80\x0f\x0e\x55\x05\x4b\x58\xfc\xc4\xcb\x0d\x5f\xe3\x62\x12\x95\xf4\x3d\x38\x6c\x3b\xc9\x1d\x25\x23\xaf\xd0\x2c\x80\x91\xab\xef\x81\xf2\xa8\x94\x68\x3b\x6d\x1c\x2c\xfa\x1e\x96\xec\x4a\xab\x5a\xac\x59\x2c\x06\xc3\xb0\xf0\xbd\x96\xdd\x66\x0d\xdf\x5e\xc2\x67\x6e\xf1\xef\xa2\x7c\x90\xe1\x6a\x8d\xb0\x54\x14\xb8\x64\x37\xba\x42\x3b\x4e\xb1\x54\xbc\x45\xb2\x77\x46\x28\x07\x4b\xc5\x6e\xc8\xb0\x78\xbb\x55\xe5\x61\xd4\xa5\xdb\x77\xc7\xa0\x1a\x16\x2f\xce\x2c\x3b\xb3\x8b\xd0\x7d\xa9\xd8\x8f\x5b\xc7\x9d\xd0\xca\xe7\x52\xd3\x64\xb5\x82\xbb\x06\xe1\xd0\x61\x18\xc0\x17\x11\x16\xb8\x02\x5e\xf1\xce\x11\x47\x1a\xb8\x94\x7a\xe7\x81\xdf\x5a\x24\xb4\xb5\xa9\x84\xe2\x66\xef\x6b\xd4\x5b\x55\x52\x61\xe0\x36\xd4\x62\xb1\x05\xb4\xd4\x52\x1b\x96\x26\xbe\xee\xb4\x11\x25\x65\xa5\x56\x0e\x1f\x1c\x21\x42\xff\x17\x70\x38\xc7\x30\xe4\x90\x8d\xd0\x0d\x03\xfb\x95\xcb\x2d\x16\x80\xc6\x68\x93\x87\xd1\xfd\x79\x10\x4a\x2e\xa5\x85\x3a\x2b\xdd\x43\x01\x6d\xce\xd2\x84\x4a\x43\x56\x4f\xdb\xe5\x31\x9a\xa2\x60\xd6\xb5\x85\x49\xa7\x11\xa6\x2f\xf4\x87\x3e\x4d\x92\xf6\xbe\x00\xbd\x21\xc0\x5b\x96\x4d\xe7\x4e\x93\x44\xd4\xf0\x3f\xbd\xf1\x61\x89\x41\xb7\x35\x0a\x94\x90\x05\xd4\xad\x63\xd7\x54\xa2\xce\x16\x5b\x85\x0f\x1d\x96\x0e\xab\x00\x13\x01\xe8\x4b\x9c\xdd\x31\x08\xae\x29\x1c\x0b\x3a\x5c\x9a\x24\x43\x7a\x28\x39\x9e\xf9\x3e\x4f\x93\x47\x9a\x5c\xad\xe0\x56\x01\x3e\x60\xb9\x75\x68\x3d\x6f\x6b\x71\x8f\x0a\x48\xdb\xa0\x95\xdc\x43\xad\xcd\xc4\xae\x3b\x34\x7e\x02\x96\xae\x56\xe9\x6a\x95\x50\x20\xbb\x55\xd9\x7b\xbd\x2e\xa6\xe0\x7c\x8f\x12\x1d\xfe\x39\xb1\x5c\x19\xe4\x0e\x73\xca\xf3\xb8\xdf\xaa\xac\xd9\x4c\x53\x7e\xd0\x7a\x53\x80\xee\xa6\xb6\xdb\x2e\x3f\x0d\x21\xb0\xc6\x73\x91\x34\x14\x3e\xb8\x19\x2d\x1e\xfb\x99\xcd\xe3\x1c\x73\x4f\xbd\x48\x77\x24\x0b\x62\x7b\x36\xea\x89\xdf\x96\xdd\x76\x59\xce\xde\xd9\x4c\x77\xd1\x3a\xce\xd0\x6c\xfc\xf4\x39\x3b\x6a\x2e\x92\xe7\xd9\x3b\x28\x82\x06\x99\x85\x0c\x9e\xcc\x40\xe2\x07\x25\xd1\x5a\xb0\x1b\xd1\x7d\x3d\x8b\xa1\xc8\x8c\xc9\x0f\x5d\xc5\x1f\x33\x19\x2c\xb7\x6a\x42\x66\xcc\xfd\xaf\x84\x7a\x4d\x14\xf0\x9b\xee\xf2\x78\xb2\x9f\xf1\x0f\xd2\x77\xf0\x5b\xe0\xe1\x4c\xae\xe1\x64\x23\x97\xa5\x9d\x73\x3c\x91\x0d\xbe\x96\xbb\xb2\x01\xdd\x8d\x07\x0c\x37\xfd\x2e\x07\x6a\x68\xb3\x1c\x3e\x7e\x9a\x4f\xb1\x5a\x1d\x20\x9f\xb9\x83\x37\x09\xe3\x64\x5f\x14\x7a\x80\x27\x2f\x7c\xc6\x40\xff\x0e\x07\x9c\x62\xfe\x93\x30\x69\xfc\xce\xf0\x8a\x7c\x56\x79\xff\xfe\x5c\xe2\xfe\xa7\x85\x75\x66\x8f\x7c\xd0\x27\x42\x69\x17\x3e\x0c\x58\xd1\x72\xf2\xf7\xe1\xa8\xe0\x13\xee\x8f\xd4\x5f\x35\x5c\x28\xe0\x9e\x62\x62\x5e\x0a\xeb\xe8\x93\x42\x0a\xa0\xef\x4e\x45\xb5\xb1\xae\xb1\x74\xe2\x1e\xe5\x1e\x44\x4b\xfb\xf1\xb3\x44\x16\x16\x1b\xbd\x26\xfc\xd2\xa9\x0a\x10\x0e\x76\x42\x4a\xe0\x72\xc7\xf7\x16\x1a\x2d\x2b\x7f\x21\x2c\xad\x7d\x8b\x93\xc2\xf1\xe1\xe0\x1d\xda\x54\x68\x58\xea\x37\x6b\x18\xc7\x3a\xb3\xa5\x7d\x9b\x26\x21\x7a\x26\x94\x38\xfc\x0d\xee\x42\x42\x98\x80\xe6\x57\xb8\x83\xd2\xdb\xc6\x5e\x2c\x88\x62\x8c\xcd\x42\x49\xc6\xd8\x49\xcd\x3c\x36\x3f\x5e\x14\xff\xbb\xe7\x5d\x87\xaa\xca\x66\x33\x64\x4a\xc8\xbc\x88\x3d\x18\xcb\xc7\x35\xe1\x95\xe5\x47\x08\x5b\x62\x8e\xe8\x78\xcf\xc8\x5b\x0b\xc5\xa5\xf7\xc5\x39\xb3\x32\xf4\x0d\xb7\x28\xfb\xd7\xcd\xdc\x8e\xb2\x7c\xba\x7a\x69\x4f\x09\x92\xbd\x44\x95\x95\xcc\x0f\x96\xc3\x39\xbc\x7c\x0d\x02\xbe\xbb\x84\x8b\xd7\x20\xce\xcf\x83\xfc\xc6\xf2\x97\x10\x03\x3f\x8a\x4f\x63\xcf\x93\x0f\x60\xb4\x1e\x17\xe6\x1b\x8f\x5c\x7c\xd6\x11\x39\x1e\x95\x02\x78\x55\x09\xb5\x0e\x02\xe8\xb0\x14\xb5\xc0\xca\x43\x40\x49\x3c\xa2\xc6\x09\x35\x85\x07\xa9\x1c\xbe\xca\xb5\xd4\xbb\x19\x56\xa1\xd5\x53\xb8\x55\xb8\xf3\xeb\xc9\x3f\x15\xf8\x06\xe7\xc4\x16\x70\x51\x3c\x82\xe6\xff\xf4\x23\xfc\x99\x4f\x0a\x5c\x42\x94\xc6\x68\x29\x46\x8c\x48\x0e\x5f\x0e\x9c\x84\x3d\x12\xdb\x18\x31\x42\x78\x1d\x9e\xc4\x4f\x83\xd0\xfb\xbe\x1e\xc3\xd0\x2b\x0b\xb7\x27\xda\x4e\xaf\x44\xc9\x22\xd2\x3e\x6a\x72\xda\xf0\x6a\x8e\x4f\x9e\xbf\x02\x00\x00\xff\xff\x0f\x28\xb5\x6b\xaa\x0c\x00\x00") func templateHookTmplBytes() ([]byte, error) { return bindataRead( @@ -941,7 +941,7 @@ func templateHookTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "template/hook.tmpl", size: 3070, mode: os.FileMode(420), modTime: time.Unix(1, 0)} + info := bindataFileInfo{name: "template/hook.tmpl", size: 3242, mode: os.FileMode(420), modTime: time.Unix(1, 0)} a := &asset{bytes: bytes, info: info} return a, nil } diff --git a/entc/gen/template/hook.tmpl b/entc/gen/template/hook.tmpl index 401dc8c6c..2485adb54 100644 --- a/entc/gen/template/hook.tmpl +++ b/entc/gen/template/hook.tmpl @@ -32,7 +32,7 @@ import "{{ $.Config.Package }}" } {{ end }} -// On executes the given hook only of the given operation. +// On executes the given hook only for the given operation. // // hook.On(Log, {{ $pkg }}.Delete|{{ $pkg }}.Create) // @@ -47,6 +47,14 @@ func On(hk {{ $pkg }}.Hook, op {{ $pkg }}.Op) {{ $pkg }}.Hook { } } +// Unless skips the given hook only for the given operation. +// +// hook.Unless(Log, {{ $pkg }}.Update|{{ $pkg }}.UpdateOne) +// +func Unless(hk {{ $pkg }}.Hook, op {{ $pkg }}.Op) {{ $pkg }}.Hook { + return On(hk, ^op) +} + // Reject returns a hook that rejects all operations that match op. // // func (T) Hooks() []{{ $pkg }}.Hook { @@ -56,14 +64,12 @@ func On(hk {{ $pkg }}.Hook, op {{ $pkg }}.Op) {{ $pkg }}.Hook { // } // func Reject(op {{ $pkg }}.Op) {{ $pkg }}.Hook { - return func(next {{ $pkg }}.Mutator) {{ $pkg }}.Mutator { - return {{ $pkg }}.MutateFunc(func(ctx context.Context, m {{ $pkg }}.Mutation) ({{ $pkg }}.Value, error) { - if m.Op().Is(op) { - return nil, fmt.Errorf("%s operation is not allowed", m.Op()) - } - return next.Mutate(ctx, m) + hk := func({{ $pkg }}.Mutator) {{ $pkg }}.Mutator { + return {{ $pkg }}.MutateFunc(func(_ context.Context, m {{ $pkg }}.Mutation) ({{ $pkg }}.Value, error) { + return nil, fmt.Errorf("%s operation is not allowed", m.Op()) }) } + return On(hk, op) } // Chain acts as a list of hooks and is effectively immutable. diff --git a/entc/integration/config/ent/hook/hook.go b/entc/integration/config/ent/hook/hook.go index ba08c4a77..eed0b043b 100644 --- a/entc/integration/config/ent/hook/hook.go +++ b/entc/integration/config/ent/hook/hook.go @@ -26,7 +26,7 @@ func (f UserFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) return f(ctx, mv) } -// On executes the given hook only of the given operation. +// On executes the given hook only for the given operation. // // hook.On(Log, ent.Delete|ent.Create) // @@ -41,6 +41,14 @@ func On(hk ent.Hook, op ent.Op) ent.Hook { } } +// Unless skips the given hook only for the given operation. +// +// hook.Unless(Log, ent.Update|ent.UpdateOne) +// +func Unless(hk ent.Hook, op ent.Op) ent.Hook { + return On(hk, ^op) +} + // Reject returns a hook that rejects all operations that match op. // // func (T) Hooks() []ent.Hook { @@ -50,14 +58,12 @@ func On(hk ent.Hook, op ent.Op) ent.Hook { // } // func Reject(op ent.Op) ent.Hook { - return func(next ent.Mutator) ent.Mutator { - return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) { - if m.Op().Is(op) { - return nil, fmt.Errorf("%s operation is not allowed", m.Op()) - } - return next.Mutate(ctx, m) + hk := func(ent.Mutator) ent.Mutator { + return ent.MutateFunc(func(_ context.Context, m ent.Mutation) (ent.Value, error) { + return nil, fmt.Errorf("%s operation is not allowed", m.Op()) }) } + return On(hk, op) } // Chain acts as a list of hooks and is effectively immutable. diff --git a/entc/integration/customid/ent/hook/hook.go b/entc/integration/customid/ent/hook/hook.go index 90143102d..66db79889 100644 --- a/entc/integration/customid/ent/hook/hook.go +++ b/entc/integration/customid/ent/hook/hook.go @@ -78,7 +78,7 @@ func (f UserFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) return f(ctx, mv) } -// On executes the given hook only of the given operation. +// On executes the given hook only for the given operation. // // hook.On(Log, ent.Delete|ent.Create) // @@ -93,6 +93,14 @@ func On(hk ent.Hook, op ent.Op) ent.Hook { } } +// Unless skips the given hook only for the given operation. +// +// hook.Unless(Log, ent.Update|ent.UpdateOne) +// +func Unless(hk ent.Hook, op ent.Op) ent.Hook { + return On(hk, ^op) +} + // Reject returns a hook that rejects all operations that match op. // // func (T) Hooks() []ent.Hook { @@ -102,14 +110,12 @@ func On(hk ent.Hook, op ent.Op) ent.Hook { // } // func Reject(op ent.Op) ent.Hook { - return func(next ent.Mutator) ent.Mutator { - return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) { - if m.Op().Is(op) { - return nil, fmt.Errorf("%s operation is not allowed", m.Op()) - } - return next.Mutate(ctx, m) + hk := func(ent.Mutator) ent.Mutator { + return ent.MutateFunc(func(_ context.Context, m ent.Mutation) (ent.Value, error) { + return nil, fmt.Errorf("%s operation is not allowed", m.Op()) }) } + return On(hk, op) } // Chain acts as a list of hooks and is effectively immutable. diff --git a/entc/integration/ent/hook/hook.go b/entc/integration/ent/hook/hook.go index ca7ec7da0..33c3fd18c 100644 --- a/entc/integration/ent/hook/hook.go +++ b/entc/integration/ent/hook/hook.go @@ -169,7 +169,7 @@ func (f UserFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) return f(ctx, mv) } -// On executes the given hook only of the given operation. +// On executes the given hook only for the given operation. // // hook.On(Log, ent.Delete|ent.Create) // @@ -184,6 +184,14 @@ func On(hk ent.Hook, op ent.Op) ent.Hook { } } +// Unless skips the given hook only for the given operation. +// +// hook.Unless(Log, ent.Update|ent.UpdateOne) +// +func Unless(hk ent.Hook, op ent.Op) ent.Hook { + return On(hk, ^op) +} + // Reject returns a hook that rejects all operations that match op. // // func (T) Hooks() []ent.Hook { @@ -193,14 +201,12 @@ func On(hk ent.Hook, op ent.Op) ent.Hook { // } // func Reject(op ent.Op) ent.Hook { - return func(next ent.Mutator) ent.Mutator { - return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) { - if m.Op().Is(op) { - return nil, fmt.Errorf("%s operation is not allowed", m.Op()) - } - return next.Mutate(ctx, m) + hk := func(ent.Mutator) ent.Mutator { + return ent.MutateFunc(func(_ context.Context, m ent.Mutation) (ent.Value, error) { + return nil, fmt.Errorf("%s operation is not allowed", m.Op()) }) } + return On(hk, op) } // Chain acts as a list of hooks and is effectively immutable. diff --git a/entc/integration/gremlin/ent/hook/hook.go b/entc/integration/gremlin/ent/hook/hook.go index a432a4771..8ea324c46 100644 --- a/entc/integration/gremlin/ent/hook/hook.go +++ b/entc/integration/gremlin/ent/hook/hook.go @@ -169,7 +169,7 @@ func (f UserFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) return f(ctx, mv) } -// On executes the given hook only of the given operation. +// On executes the given hook only for the given operation. // // hook.On(Log, ent.Delete|ent.Create) // @@ -184,6 +184,14 @@ func On(hk ent.Hook, op ent.Op) ent.Hook { } } +// Unless skips the given hook only for the given operation. +// +// hook.Unless(Log, ent.Update|ent.UpdateOne) +// +func Unless(hk ent.Hook, op ent.Op) ent.Hook { + return On(hk, ^op) +} + // Reject returns a hook that rejects all operations that match op. // // func (T) Hooks() []ent.Hook { @@ -193,14 +201,12 @@ func On(hk ent.Hook, op ent.Op) ent.Hook { // } // func Reject(op ent.Op) ent.Hook { - return func(next ent.Mutator) ent.Mutator { - return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) { - if m.Op().Is(op) { - return nil, fmt.Errorf("%s operation is not allowed", m.Op()) - } - return next.Mutate(ctx, m) + hk := func(ent.Mutator) ent.Mutator { + return ent.MutateFunc(func(_ context.Context, m ent.Mutation) (ent.Value, error) { + return nil, fmt.Errorf("%s operation is not allowed", m.Op()) }) } + return On(hk, op) } // Chain acts as a list of hooks and is effectively immutable. diff --git a/entc/integration/hooks/ent/hook/hook.go b/entc/integration/hooks/ent/hook/hook.go index aece904be..405555309 100644 --- a/entc/integration/hooks/ent/hook/hook.go +++ b/entc/integration/hooks/ent/hook/hook.go @@ -39,7 +39,7 @@ func (f UserFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) return f(ctx, mv) } -// On executes the given hook only of the given operation. +// On executes the given hook only for the given operation. // // hook.On(Log, ent.Delete|ent.Create) // @@ -54,6 +54,14 @@ func On(hk ent.Hook, op ent.Op) ent.Hook { } } +// Unless skips the given hook only for the given operation. +// +// hook.Unless(Log, ent.Update|ent.UpdateOne) +// +func Unless(hk ent.Hook, op ent.Op) ent.Hook { + return On(hk, ^op) +} + // Reject returns a hook that rejects all operations that match op. // // func (T) Hooks() []ent.Hook { @@ -63,14 +71,12 @@ func On(hk ent.Hook, op ent.Op) ent.Hook { // } // func Reject(op ent.Op) ent.Hook { - return func(next ent.Mutator) ent.Mutator { - return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) { - if m.Op().Is(op) { - return nil, fmt.Errorf("%s operation is not allowed", m.Op()) - } - return next.Mutate(ctx, m) + hk := func(ent.Mutator) ent.Mutator { + return ent.MutateFunc(func(_ context.Context, m ent.Mutation) (ent.Value, error) { + return nil, fmt.Errorf("%s operation is not allowed", m.Op()) }) } + return On(hk, op) } // Chain acts as a list of hooks and is effectively immutable. diff --git a/entc/integration/hooks/hooks_test.go b/entc/integration/hooks/hooks_test.go index 602872d5a..d0c752f50 100644 --- a/entc/integration/hooks/hooks_test.go +++ b/entc/integration/hooks/hooks_test.go @@ -207,7 +207,7 @@ func TestOldValues(t *testing.T) { }) }, ent.OpUpdateOne)) // A generic hook (executed on all types). - client.Use(hook.On(func(next ent.Mutator) ent.Mutator { + client.Use(hook.Unless(func(next ent.Mutator) ent.Mutator { return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) { namer, ok := m.(interface { OldName(context.Context) (string, error) @@ -230,7 +230,7 @@ func TestOldValues(t *testing.T) { require.NoError(t, err) return value, nil }) - }, ent.OpUpdateOne)) + }, ^ent.OpUpdateOne)) a8m := client.User.Create().SetName("a8m").SaveX(ctx) require.Equal(t, "a8m", a8m.Name) _, err := client.User.UpdateOne(a8m).SetName("Ariel").SetVersion(a8m.Version).Save(ctx) diff --git a/entc/integration/idtype/ent/hook/hook.go b/entc/integration/idtype/ent/hook/hook.go index 206985fb0..9ab6eefa3 100644 --- a/entc/integration/idtype/ent/hook/hook.go +++ b/entc/integration/idtype/ent/hook/hook.go @@ -26,7 +26,7 @@ func (f UserFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) return f(ctx, mv) } -// On executes the given hook only of the given operation. +// On executes the given hook only for the given operation. // // hook.On(Log, ent.Delete|ent.Create) // @@ -41,6 +41,14 @@ func On(hk ent.Hook, op ent.Op) ent.Hook { } } +// Unless skips the given hook only for the given operation. +// +// hook.Unless(Log, ent.Update|ent.UpdateOne) +// +func Unless(hk ent.Hook, op ent.Op) ent.Hook { + return On(hk, ^op) +} + // Reject returns a hook that rejects all operations that match op. // // func (T) Hooks() []ent.Hook { @@ -50,14 +58,12 @@ func On(hk ent.Hook, op ent.Op) ent.Hook { // } // func Reject(op ent.Op) ent.Hook { - return func(next ent.Mutator) ent.Mutator { - return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) { - if m.Op().Is(op) { - return nil, fmt.Errorf("%s operation is not allowed", m.Op()) - } - return next.Mutate(ctx, m) + hk := func(ent.Mutator) ent.Mutator { + return ent.MutateFunc(func(_ context.Context, m ent.Mutation) (ent.Value, error) { + return nil, fmt.Errorf("%s operation is not allowed", m.Op()) }) } + return On(hk, op) } // Chain acts as a list of hooks and is effectively immutable. diff --git a/entc/integration/json/ent/hook/hook.go b/entc/integration/json/ent/hook/hook.go index 578093404..25dd25e05 100644 --- a/entc/integration/json/ent/hook/hook.go +++ b/entc/integration/json/ent/hook/hook.go @@ -26,7 +26,7 @@ func (f UserFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) return f(ctx, mv) } -// On executes the given hook only of the given operation. +// On executes the given hook only for the given operation. // // hook.On(Log, ent.Delete|ent.Create) // @@ -41,6 +41,14 @@ func On(hk ent.Hook, op ent.Op) ent.Hook { } } +// Unless skips the given hook only for the given operation. +// +// hook.Unless(Log, ent.Update|ent.UpdateOne) +// +func Unless(hk ent.Hook, op ent.Op) ent.Hook { + return On(hk, ^op) +} + // Reject returns a hook that rejects all operations that match op. // // func (T) Hooks() []ent.Hook { @@ -50,14 +58,12 @@ func On(hk ent.Hook, op ent.Op) ent.Hook { // } // func Reject(op ent.Op) ent.Hook { - return func(next ent.Mutator) ent.Mutator { - return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) { - if m.Op().Is(op) { - return nil, fmt.Errorf("%s operation is not allowed", m.Op()) - } - return next.Mutate(ctx, m) + hk := func(ent.Mutator) ent.Mutator { + return ent.MutateFunc(func(_ context.Context, m ent.Mutation) (ent.Value, error) { + return nil, fmt.Errorf("%s operation is not allowed", m.Op()) }) } + return On(hk, op) } // Chain acts as a list of hooks and is effectively immutable. diff --git a/entc/integration/migrate/entv1/hook/hook.go b/entc/integration/migrate/entv1/hook/hook.go index cbe48f857..f08b5c0df 100644 --- a/entc/integration/migrate/entv1/hook/hook.go +++ b/entc/integration/migrate/entv1/hook/hook.go @@ -39,7 +39,7 @@ func (f UserFunc) Mutate(ctx context.Context, m entv1.Mutation) (entv1.Value, er return f(ctx, mv) } -// On executes the given hook only of the given operation. +// On executes the given hook only for the given operation. // // hook.On(Log, entv1.Delete|entv1.Create) // @@ -54,6 +54,14 @@ func On(hk entv1.Hook, op entv1.Op) entv1.Hook { } } +// Unless skips the given hook only for the given operation. +// +// hook.Unless(Log, entv1.Update|entv1.UpdateOne) +// +func Unless(hk entv1.Hook, op entv1.Op) entv1.Hook { + return On(hk, ^op) +} + // Reject returns a hook that rejects all operations that match op. // // func (T) Hooks() []entv1.Hook { @@ -63,14 +71,12 @@ func On(hk entv1.Hook, op entv1.Op) entv1.Hook { // } // func Reject(op entv1.Op) entv1.Hook { - return func(next entv1.Mutator) entv1.Mutator { - return entv1.MutateFunc(func(ctx context.Context, m entv1.Mutation) (entv1.Value, error) { - if m.Op().Is(op) { - return nil, fmt.Errorf("%s operation is not allowed", m.Op()) - } - return next.Mutate(ctx, m) + hk := func(entv1.Mutator) entv1.Mutator { + return entv1.MutateFunc(func(_ context.Context, m entv1.Mutation) (entv1.Value, error) { + return nil, fmt.Errorf("%s operation is not allowed", m.Op()) }) } + return On(hk, op) } // Chain acts as a list of hooks and is effectively immutable. diff --git a/entc/integration/migrate/entv2/hook/hook.go b/entc/integration/migrate/entv2/hook/hook.go index 8e0aed23b..dc33981ec 100644 --- a/entc/integration/migrate/entv2/hook/hook.go +++ b/entc/integration/migrate/entv2/hook/hook.go @@ -65,7 +65,7 @@ func (f UserFunc) Mutate(ctx context.Context, m entv2.Mutation) (entv2.Value, er return f(ctx, mv) } -// On executes the given hook only of the given operation. +// On executes the given hook only for the given operation. // // hook.On(Log, entv2.Delete|entv2.Create) // @@ -80,6 +80,14 @@ func On(hk entv2.Hook, op entv2.Op) entv2.Hook { } } +// Unless skips the given hook only for the given operation. +// +// hook.Unless(Log, entv2.Update|entv2.UpdateOne) +// +func Unless(hk entv2.Hook, op entv2.Op) entv2.Hook { + return On(hk, ^op) +} + // Reject returns a hook that rejects all operations that match op. // // func (T) Hooks() []entv2.Hook { @@ -89,14 +97,12 @@ func On(hk entv2.Hook, op entv2.Op) entv2.Hook { // } // func Reject(op entv2.Op) entv2.Hook { - return func(next entv2.Mutator) entv2.Mutator { - return entv2.MutateFunc(func(ctx context.Context, m entv2.Mutation) (entv2.Value, error) { - if m.Op().Is(op) { - return nil, fmt.Errorf("%s operation is not allowed", m.Op()) - } - return next.Mutate(ctx, m) + hk := func(entv2.Mutator) entv2.Mutator { + return entv2.MutateFunc(func(_ context.Context, m entv2.Mutation) (entv2.Value, error) { + return nil, fmt.Errorf("%s operation is not allowed", m.Op()) }) } + return On(hk, op) } // Chain acts as a list of hooks and is effectively immutable. diff --git a/entc/integration/privacy/ent/hook/hook.go b/entc/integration/privacy/ent/hook/hook.go index d97436063..d3b5a058f 100644 --- a/entc/integration/privacy/ent/hook/hook.go +++ b/entc/integration/privacy/ent/hook/hook.go @@ -39,7 +39,7 @@ func (f PlanetFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, erro return f(ctx, mv) } -// On executes the given hook only of the given operation. +// On executes the given hook only for the given operation. // // hook.On(Log, ent.Delete|ent.Create) // @@ -54,6 +54,14 @@ func On(hk ent.Hook, op ent.Op) ent.Hook { } } +// Unless skips the given hook only for the given operation. +// +// hook.Unless(Log, ent.Update|ent.UpdateOne) +// +func Unless(hk ent.Hook, op ent.Op) ent.Hook { + return On(hk, ^op) +} + // Reject returns a hook that rejects all operations that match op. // // func (T) Hooks() []ent.Hook { @@ -63,14 +71,12 @@ func On(hk ent.Hook, op ent.Op) ent.Hook { // } // func Reject(op ent.Op) ent.Hook { - return func(next ent.Mutator) ent.Mutator { - return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) { - if m.Op().Is(op) { - return nil, fmt.Errorf("%s operation is not allowed", m.Op()) - } - return next.Mutate(ctx, m) + hk := func(ent.Mutator) ent.Mutator { + return ent.MutateFunc(func(_ context.Context, m ent.Mutation) (ent.Value, error) { + return nil, fmt.Errorf("%s operation is not allowed", m.Op()) }) } + return On(hk, op) } // Chain acts as a list of hooks and is effectively immutable. diff --git a/entc/integration/template/ent/hook/hook.go b/entc/integration/template/ent/hook/hook.go index 754ebf693..e39b0910d 100644 --- a/entc/integration/template/ent/hook/hook.go +++ b/entc/integration/template/ent/hook/hook.go @@ -52,7 +52,7 @@ func (f UserFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) return f(ctx, mv) } -// On executes the given hook only of the given operation. +// On executes the given hook only for the given operation. // // hook.On(Log, ent.Delete|ent.Create) // @@ -67,6 +67,14 @@ func On(hk ent.Hook, op ent.Op) ent.Hook { } } +// Unless skips the given hook only for the given operation. +// +// hook.Unless(Log, ent.Update|ent.UpdateOne) +// +func Unless(hk ent.Hook, op ent.Op) ent.Hook { + return On(hk, ^op) +} + // Reject returns a hook that rejects all operations that match op. // // func (T) Hooks() []ent.Hook { @@ -76,14 +84,12 @@ func On(hk ent.Hook, op ent.Op) ent.Hook { // } // func Reject(op ent.Op) ent.Hook { - return func(next ent.Mutator) ent.Mutator { - return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) { - if m.Op().Is(op) { - return nil, fmt.Errorf("%s operation is not allowed", m.Op()) - } - return next.Mutate(ctx, m) + hk := func(ent.Mutator) ent.Mutator { + return ent.MutateFunc(func(_ context.Context, m ent.Mutation) (ent.Value, error) { + return nil, fmt.Errorf("%s operation is not allowed", m.Op()) }) } + return On(hk, op) } // Chain acts as a list of hooks and is effectively immutable. diff --git a/examples/edgeindex/ent/hook/hook.go b/examples/edgeindex/ent/hook/hook.go index b30669da7..1136dd599 100644 --- a/examples/edgeindex/ent/hook/hook.go +++ b/examples/edgeindex/ent/hook/hook.go @@ -39,7 +39,7 @@ func (f StreetFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, erro return f(ctx, mv) } -// On executes the given hook only of the given operation. +// On executes the given hook only for the given operation. // // hook.On(Log, ent.Delete|ent.Create) // @@ -54,6 +54,14 @@ func On(hk ent.Hook, op ent.Op) ent.Hook { } } +// Unless skips the given hook only for the given operation. +// +// hook.Unless(Log, ent.Update|ent.UpdateOne) +// +func Unless(hk ent.Hook, op ent.Op) ent.Hook { + return On(hk, ^op) +} + // Reject returns a hook that rejects all operations that match op. // // func (T) Hooks() []ent.Hook { @@ -63,14 +71,12 @@ func On(hk ent.Hook, op ent.Op) ent.Hook { // } // func Reject(op ent.Op) ent.Hook { - return func(next ent.Mutator) ent.Mutator { - return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) { - if m.Op().Is(op) { - return nil, fmt.Errorf("%s operation is not allowed", m.Op()) - } - return next.Mutate(ctx, m) + hk := func(ent.Mutator) ent.Mutator { + return ent.MutateFunc(func(_ context.Context, m ent.Mutation) (ent.Value, error) { + return nil, fmt.Errorf("%s operation is not allowed", m.Op()) }) } + return On(hk, op) } // Chain acts as a list of hooks and is effectively immutable. diff --git a/examples/entcpkg/ent/hook/hook.go b/examples/entcpkg/ent/hook/hook.go index f3ed3e1fa..9cea8ed23 100644 --- a/examples/entcpkg/ent/hook/hook.go +++ b/examples/entcpkg/ent/hook/hook.go @@ -26,7 +26,7 @@ func (f UserFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) return f(ctx, mv) } -// On executes the given hook only of the given operation. +// On executes the given hook only for the given operation. // // hook.On(Log, ent.Delete|ent.Create) // @@ -41,6 +41,14 @@ func On(hk ent.Hook, op ent.Op) ent.Hook { } } +// Unless skips the given hook only for the given operation. +// +// hook.Unless(Log, ent.Update|ent.UpdateOne) +// +func Unless(hk ent.Hook, op ent.Op) ent.Hook { + return On(hk, ^op) +} + // Reject returns a hook that rejects all operations that match op. // // func (T) Hooks() []ent.Hook { @@ -50,14 +58,12 @@ func On(hk ent.Hook, op ent.Op) ent.Hook { // } // func Reject(op ent.Op) ent.Hook { - return func(next ent.Mutator) ent.Mutator { - return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) { - if m.Op().Is(op) { - return nil, fmt.Errorf("%s operation is not allowed", m.Op()) - } - return next.Mutate(ctx, m) + hk := func(ent.Mutator) ent.Mutator { + return ent.MutateFunc(func(_ context.Context, m ent.Mutation) (ent.Value, error) { + return nil, fmt.Errorf("%s operation is not allowed", m.Op()) }) } + return On(hk, op) } // Chain acts as a list of hooks and is effectively immutable. diff --git a/examples/m2m2types/ent/hook/hook.go b/examples/m2m2types/ent/hook/hook.go index d9b2d9db3..dc038dd5d 100644 --- a/examples/m2m2types/ent/hook/hook.go +++ b/examples/m2m2types/ent/hook/hook.go @@ -39,7 +39,7 @@ func (f UserFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) return f(ctx, mv) } -// On executes the given hook only of the given operation. +// On executes the given hook only for the given operation. // // hook.On(Log, ent.Delete|ent.Create) // @@ -54,6 +54,14 @@ func On(hk ent.Hook, op ent.Op) ent.Hook { } } +// Unless skips the given hook only for the given operation. +// +// hook.Unless(Log, ent.Update|ent.UpdateOne) +// +func Unless(hk ent.Hook, op ent.Op) ent.Hook { + return On(hk, ^op) +} + // Reject returns a hook that rejects all operations that match op. // // func (T) Hooks() []ent.Hook { @@ -63,14 +71,12 @@ func On(hk ent.Hook, op ent.Op) ent.Hook { // } // func Reject(op ent.Op) ent.Hook { - return func(next ent.Mutator) ent.Mutator { - return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) { - if m.Op().Is(op) { - return nil, fmt.Errorf("%s operation is not allowed", m.Op()) - } - return next.Mutate(ctx, m) + hk := func(ent.Mutator) ent.Mutator { + return ent.MutateFunc(func(_ context.Context, m ent.Mutation) (ent.Value, error) { + return nil, fmt.Errorf("%s operation is not allowed", m.Op()) }) } + return On(hk, op) } // Chain acts as a list of hooks and is effectively immutable. diff --git a/examples/m2mbidi/ent/hook/hook.go b/examples/m2mbidi/ent/hook/hook.go index de31a68db..b2532e01e 100644 --- a/examples/m2mbidi/ent/hook/hook.go +++ b/examples/m2mbidi/ent/hook/hook.go @@ -26,7 +26,7 @@ func (f UserFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) return f(ctx, mv) } -// On executes the given hook only of the given operation. +// On executes the given hook only for the given operation. // // hook.On(Log, ent.Delete|ent.Create) // @@ -41,6 +41,14 @@ func On(hk ent.Hook, op ent.Op) ent.Hook { } } +// Unless skips the given hook only for the given operation. +// +// hook.Unless(Log, ent.Update|ent.UpdateOne) +// +func Unless(hk ent.Hook, op ent.Op) ent.Hook { + return On(hk, ^op) +} + // Reject returns a hook that rejects all operations that match op. // // func (T) Hooks() []ent.Hook { @@ -50,14 +58,12 @@ func On(hk ent.Hook, op ent.Op) ent.Hook { // } // func Reject(op ent.Op) ent.Hook { - return func(next ent.Mutator) ent.Mutator { - return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) { - if m.Op().Is(op) { - return nil, fmt.Errorf("%s operation is not allowed", m.Op()) - } - return next.Mutate(ctx, m) + hk := func(ent.Mutator) ent.Mutator { + return ent.MutateFunc(func(_ context.Context, m ent.Mutation) (ent.Value, error) { + return nil, fmt.Errorf("%s operation is not allowed", m.Op()) }) } + return On(hk, op) } // Chain acts as a list of hooks and is effectively immutable. diff --git a/examples/m2mrecur/ent/hook/hook.go b/examples/m2mrecur/ent/hook/hook.go index 680d080fd..dda5f5337 100644 --- a/examples/m2mrecur/ent/hook/hook.go +++ b/examples/m2mrecur/ent/hook/hook.go @@ -26,7 +26,7 @@ func (f UserFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) return f(ctx, mv) } -// On executes the given hook only of the given operation. +// On executes the given hook only for the given operation. // // hook.On(Log, ent.Delete|ent.Create) // @@ -41,6 +41,14 @@ func On(hk ent.Hook, op ent.Op) ent.Hook { } } +// Unless skips the given hook only for the given operation. +// +// hook.Unless(Log, ent.Update|ent.UpdateOne) +// +func Unless(hk ent.Hook, op ent.Op) ent.Hook { + return On(hk, ^op) +} + // Reject returns a hook that rejects all operations that match op. // // func (T) Hooks() []ent.Hook { @@ -50,14 +58,12 @@ func On(hk ent.Hook, op ent.Op) ent.Hook { // } // func Reject(op ent.Op) ent.Hook { - return func(next ent.Mutator) ent.Mutator { - return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) { - if m.Op().Is(op) { - return nil, fmt.Errorf("%s operation is not allowed", m.Op()) - } - return next.Mutate(ctx, m) + hk := func(ent.Mutator) ent.Mutator { + return ent.MutateFunc(func(_ context.Context, m ent.Mutation) (ent.Value, error) { + return nil, fmt.Errorf("%s operation is not allowed", m.Op()) }) } + return On(hk, op) } // Chain acts as a list of hooks and is effectively immutable. diff --git a/examples/o2m2types/ent/hook/hook.go b/examples/o2m2types/ent/hook/hook.go index 6f0fed55c..2bb5239b6 100644 --- a/examples/o2m2types/ent/hook/hook.go +++ b/examples/o2m2types/ent/hook/hook.go @@ -39,7 +39,7 @@ func (f UserFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) return f(ctx, mv) } -// On executes the given hook only of the given operation. +// On executes the given hook only for the given operation. // // hook.On(Log, ent.Delete|ent.Create) // @@ -54,6 +54,14 @@ func On(hk ent.Hook, op ent.Op) ent.Hook { } } +// Unless skips the given hook only for the given operation. +// +// hook.Unless(Log, ent.Update|ent.UpdateOne) +// +func Unless(hk ent.Hook, op ent.Op) ent.Hook { + return On(hk, ^op) +} + // Reject returns a hook that rejects all operations that match op. // // func (T) Hooks() []ent.Hook { @@ -63,14 +71,12 @@ func On(hk ent.Hook, op ent.Op) ent.Hook { // } // func Reject(op ent.Op) ent.Hook { - return func(next ent.Mutator) ent.Mutator { - return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) { - if m.Op().Is(op) { - return nil, fmt.Errorf("%s operation is not allowed", m.Op()) - } - return next.Mutate(ctx, m) + hk := func(ent.Mutator) ent.Mutator { + return ent.MutateFunc(func(_ context.Context, m ent.Mutation) (ent.Value, error) { + return nil, fmt.Errorf("%s operation is not allowed", m.Op()) }) } + return On(hk, op) } // Chain acts as a list of hooks and is effectively immutable. diff --git a/examples/o2mrecur/ent/hook/hook.go b/examples/o2mrecur/ent/hook/hook.go index 1de2cfed3..385d36d16 100644 --- a/examples/o2mrecur/ent/hook/hook.go +++ b/examples/o2mrecur/ent/hook/hook.go @@ -26,7 +26,7 @@ func (f NodeFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) return f(ctx, mv) } -// On executes the given hook only of the given operation. +// On executes the given hook only for the given operation. // // hook.On(Log, ent.Delete|ent.Create) // @@ -41,6 +41,14 @@ func On(hk ent.Hook, op ent.Op) ent.Hook { } } +// Unless skips the given hook only for the given operation. +// +// hook.Unless(Log, ent.Update|ent.UpdateOne) +// +func Unless(hk ent.Hook, op ent.Op) ent.Hook { + return On(hk, ^op) +} + // Reject returns a hook that rejects all operations that match op. // // func (T) Hooks() []ent.Hook { @@ -50,14 +58,12 @@ func On(hk ent.Hook, op ent.Op) ent.Hook { // } // func Reject(op ent.Op) ent.Hook { - return func(next ent.Mutator) ent.Mutator { - return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) { - if m.Op().Is(op) { - return nil, fmt.Errorf("%s operation is not allowed", m.Op()) - } - return next.Mutate(ctx, m) + hk := func(ent.Mutator) ent.Mutator { + return ent.MutateFunc(func(_ context.Context, m ent.Mutation) (ent.Value, error) { + return nil, fmt.Errorf("%s operation is not allowed", m.Op()) }) } + return On(hk, op) } // Chain acts as a list of hooks and is effectively immutable. diff --git a/examples/o2o2types/ent/hook/hook.go b/examples/o2o2types/ent/hook/hook.go index 9895007b0..71726a0f7 100644 --- a/examples/o2o2types/ent/hook/hook.go +++ b/examples/o2o2types/ent/hook/hook.go @@ -39,7 +39,7 @@ func (f UserFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) return f(ctx, mv) } -// On executes the given hook only of the given operation. +// On executes the given hook only for the given operation. // // hook.On(Log, ent.Delete|ent.Create) // @@ -54,6 +54,14 @@ func On(hk ent.Hook, op ent.Op) ent.Hook { } } +// Unless skips the given hook only for the given operation. +// +// hook.Unless(Log, ent.Update|ent.UpdateOne) +// +func Unless(hk ent.Hook, op ent.Op) ent.Hook { + return On(hk, ^op) +} + // Reject returns a hook that rejects all operations that match op. // // func (T) Hooks() []ent.Hook { @@ -63,14 +71,12 @@ func On(hk ent.Hook, op ent.Op) ent.Hook { // } // func Reject(op ent.Op) ent.Hook { - return func(next ent.Mutator) ent.Mutator { - return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) { - if m.Op().Is(op) { - return nil, fmt.Errorf("%s operation is not allowed", m.Op()) - } - return next.Mutate(ctx, m) + hk := func(ent.Mutator) ent.Mutator { + return ent.MutateFunc(func(_ context.Context, m ent.Mutation) (ent.Value, error) { + return nil, fmt.Errorf("%s operation is not allowed", m.Op()) }) } + return On(hk, op) } // Chain acts as a list of hooks and is effectively immutable. diff --git a/examples/o2obidi/ent/hook/hook.go b/examples/o2obidi/ent/hook/hook.go index f5ee0d546..b0c9a4823 100644 --- a/examples/o2obidi/ent/hook/hook.go +++ b/examples/o2obidi/ent/hook/hook.go @@ -26,7 +26,7 @@ func (f UserFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) return f(ctx, mv) } -// On executes the given hook only of the given operation. +// On executes the given hook only for the given operation. // // hook.On(Log, ent.Delete|ent.Create) // @@ -41,6 +41,14 @@ func On(hk ent.Hook, op ent.Op) ent.Hook { } } +// Unless skips the given hook only for the given operation. +// +// hook.Unless(Log, ent.Update|ent.UpdateOne) +// +func Unless(hk ent.Hook, op ent.Op) ent.Hook { + return On(hk, ^op) +} + // Reject returns a hook that rejects all operations that match op. // // func (T) Hooks() []ent.Hook { @@ -50,14 +58,12 @@ func On(hk ent.Hook, op ent.Op) ent.Hook { // } // func Reject(op ent.Op) ent.Hook { - return func(next ent.Mutator) ent.Mutator { - return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) { - if m.Op().Is(op) { - return nil, fmt.Errorf("%s operation is not allowed", m.Op()) - } - return next.Mutate(ctx, m) + hk := func(ent.Mutator) ent.Mutator { + return ent.MutateFunc(func(_ context.Context, m ent.Mutation) (ent.Value, error) { + return nil, fmt.Errorf("%s operation is not allowed", m.Op()) }) } + return On(hk, op) } // Chain acts as a list of hooks and is effectively immutable. diff --git a/examples/o2orecur/ent/hook/hook.go b/examples/o2orecur/ent/hook/hook.go index cfb85c573..9a1287233 100644 --- a/examples/o2orecur/ent/hook/hook.go +++ b/examples/o2orecur/ent/hook/hook.go @@ -26,7 +26,7 @@ func (f NodeFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) return f(ctx, mv) } -// On executes the given hook only of the given operation. +// On executes the given hook only for the given operation. // // hook.On(Log, ent.Delete|ent.Create) // @@ -41,6 +41,14 @@ func On(hk ent.Hook, op ent.Op) ent.Hook { } } +// Unless skips the given hook only for the given operation. +// +// hook.Unless(Log, ent.Update|ent.UpdateOne) +// +func Unless(hk ent.Hook, op ent.Op) ent.Hook { + return On(hk, ^op) +} + // Reject returns a hook that rejects all operations that match op. // // func (T) Hooks() []ent.Hook { @@ -50,14 +58,12 @@ func On(hk ent.Hook, op ent.Op) ent.Hook { // } // func Reject(op ent.Op) ent.Hook { - return func(next ent.Mutator) ent.Mutator { - return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) { - if m.Op().Is(op) { - return nil, fmt.Errorf("%s operation is not allowed", m.Op()) - } - return next.Mutate(ctx, m) + hk := func(ent.Mutator) ent.Mutator { + return ent.MutateFunc(func(_ context.Context, m ent.Mutation) (ent.Value, error) { + return nil, fmt.Errorf("%s operation is not allowed", m.Op()) }) } + return On(hk, op) } // Chain acts as a list of hooks and is effectively immutable. diff --git a/examples/start/ent/hook/hook.go b/examples/start/ent/hook/hook.go index ce6ef6fe7..6aca7c457 100644 --- a/examples/start/ent/hook/hook.go +++ b/examples/start/ent/hook/hook.go @@ -52,7 +52,7 @@ func (f UserFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) return f(ctx, mv) } -// On executes the given hook only of the given operation. +// On executes the given hook only for the given operation. // // hook.On(Log, ent.Delete|ent.Create) // @@ -67,6 +67,14 @@ func On(hk ent.Hook, op ent.Op) ent.Hook { } } +// Unless skips the given hook only for the given operation. +// +// hook.Unless(Log, ent.Update|ent.UpdateOne) +// +func Unless(hk ent.Hook, op ent.Op) ent.Hook { + return On(hk, ^op) +} + // Reject returns a hook that rejects all operations that match op. // // func (T) Hooks() []ent.Hook { @@ -76,14 +84,12 @@ func On(hk ent.Hook, op ent.Op) ent.Hook { // } // func Reject(op ent.Op) ent.Hook { - return func(next ent.Mutator) ent.Mutator { - return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) { - if m.Op().Is(op) { - return nil, fmt.Errorf("%s operation is not allowed", m.Op()) - } - return next.Mutate(ctx, m) + hk := func(ent.Mutator) ent.Mutator { + return ent.MutateFunc(func(_ context.Context, m ent.Mutation) (ent.Value, error) { + return nil, fmt.Errorf("%s operation is not allowed", m.Op()) }) } + return On(hk, op) } // Chain acts as a list of hooks and is effectively immutable. diff --git a/examples/traversal/ent/hook/hook.go b/examples/traversal/ent/hook/hook.go index d45e402bf..dd55a2349 100644 --- a/examples/traversal/ent/hook/hook.go +++ b/examples/traversal/ent/hook/hook.go @@ -52,7 +52,7 @@ func (f UserFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) return f(ctx, mv) } -// On executes the given hook only of the given operation. +// On executes the given hook only for the given operation. // // hook.On(Log, ent.Delete|ent.Create) // @@ -67,6 +67,14 @@ func On(hk ent.Hook, op ent.Op) ent.Hook { } } +// Unless skips the given hook only for the given operation. +// +// hook.Unless(Log, ent.Update|ent.UpdateOne) +// +func Unless(hk ent.Hook, op ent.Op) ent.Hook { + return On(hk, ^op) +} + // Reject returns a hook that rejects all operations that match op. // // func (T) Hooks() []ent.Hook { @@ -76,14 +84,12 @@ func On(hk ent.Hook, op ent.Op) ent.Hook { // } // func Reject(op ent.Op) ent.Hook { - return func(next ent.Mutator) ent.Mutator { - return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) { - if m.Op().Is(op) { - return nil, fmt.Errorf("%s operation is not allowed", m.Op()) - } - return next.Mutate(ctx, m) + hk := func(ent.Mutator) ent.Mutator { + return ent.MutateFunc(func(_ context.Context, m ent.Mutation) (ent.Value, error) { + return nil, fmt.Errorf("%s operation is not allowed", m.Op()) }) } + return On(hk, op) } // Chain acts as a list of hooks and is effectively immutable. diff --git a/go.mod b/go.mod index 44ba5f7da..a4f63359f 100644 --- a/go.mod +++ b/go.mod @@ -20,11 +20,12 @@ require ( github.com/modern-go/reflect2 v1.0.1 github.com/olekukonko/tablewriter v0.0.4 github.com/pkg/errors v0.9.1 - github.com/spf13/cobra v0.0.7 + github.com/spf13/cobra v1.0.0 github.com/spf13/pflag v1.0.5 // indirect github.com/stretchr/objx v0.2.0 // indirect github.com/stretchr/testify v1.4.0 go.opencensus.io v0.22.3 + golang.org/x/mod v0.3.0 // indirect golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a - golang.org/x/tools v0.0.0-20200329025819-fd4102a86c65 + golang.org/x/tools v0.0.0-20200615222825-6aa8f57aacd9 ) diff --git a/go.sum b/go.sum index 4120f6e03..11548ecce 100644 --- a/go.sum +++ b/go.sum @@ -124,8 +124,8 @@ github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4k github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= -github.com/spf13/cobra v0.0.7 h1:FfTH+vuMXOas8jmfb5/M7dzEYx7LpcLb7a0LPe34uOU= -github.com/spf13/cobra v0.0.7/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE= +github.com/spf13/cobra v1.0.0 h1:6m/oheQuQ13N9ks4hubMG6BnvwOeaJrqSPLahSnczz8= +github.com/spf13/cobra v1.0.0/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE= github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo= github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg= github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= @@ -144,7 +144,7 @@ github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1 github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc= github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU= github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= -github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU= go.opencensus.io v0.22.3 h1:8sGtKOrtQqkN1bp2AtX+misvLIlOmsEsNd+9NIcPEm8= go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= @@ -161,6 +161,8 @@ golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHl golang.org/x/lint v0.0.0-20190409202823-959b441ac422/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/mod v0.2.0 h1:KU7oHjnv3XNWfa5COkzUifxZmxp1TyI7ImMXqFxLwvQ= golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.3.0 h1:RM4zey1++hCTbCVQfnWeKs9/IEsaBLA8vTkd0WVtmH4= +golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -198,8 +200,8 @@ golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGm golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.0.0-20200329025819-fd4102a86c65 h1:1KSbntBked74wYsKq0jzXYy7ZwcjAUtrl7EmPE97Iiw= -golang.org/x/tools v0.0.0-20200329025819-fd4102a86c65/go.mod h1:Sl4aGygMT6LrqrWclx+PTx3U+LnKx/seiNR+3G19Ar8= +golang.org/x/tools v0.0.0-20200615222825-6aa8f57aacd9 h1:cwgUY+1ja2qxWb2dyaCoixaA66WGWmrijSlxaM+JM/g= +golang.org/x/tools v0.0.0-20200615222825-6aa8f57aacd9/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7 h1:9zdDQZ7Thm29KFXgAX/+yaf3eVbP7djjWp/dXAppNCc= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898 h1:/atklqdjdhuosWIl6AIbOeHJjicWYPqR9bpxqxYG2pA=