From 61b8ccea4aadbc7c83a4b4ccd6149f2ac49d525a Mon Sep 17 00:00:00 2001 From: Ariel Mashraki Date: Sun, 8 Sep 2019 08:03:43 -0700 Subject: [PATCH] entc/load: enable adding additional fields to the generated entities Summary: In some cases (like xwf auth service), you want to add additional fields to the generated model. Reviewed By: alexsn Differential Revision: D17223674 fbshipit-source-id: fb4be71b388c6bd107e6bac242133c237fe25599 --- entc/gen/bindata.go | 10 ++-- entc/gen/template/ent.tmpl | 6 +++ entc/gen/template/import.tmpl | 5 +- entc/gen/type.go | 15 ++++-- entc/integration/ent/card.go | 5 ++ entc/integration/ent/schema/card.go | 6 +++ entc/load/bindata.go | 4 +- entc/load/load.go | 82 ++++++++++++++++++++++++++--- entc/load/load_test.go | 10 ++++ entc/load/schema.go | 19 +++++-- entc/load/testdata/valid/schema.go | 10 ++++ 11 files changed, 149 insertions(+), 23 deletions(-) diff --git a/entc/gen/bindata.go b/entc/gen/bindata.go index 899dec4b0..acaaeeccf 100644 --- a/entc/gen/bindata.go +++ b/entc/gen/bindata.go @@ -726,7 +726,7 @@ func templateDialectSqlUpdateTmpl() (*asset, error) { return a, nil } -var _templateEntTmpl = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x9c\x56\x5f\x6f\xdb\x38\x12\x7f\x96\x3e\xc5\x54\x70\x0f\x56\xe0\xca\xbd\xbe\x5d\x0f\x79\xe8\x25\x2d\x60\xe0\x90\xfd\x93\x14\xfb\x50\x14\x2d\x2d\x8e\x2c\x6e\x28\x52\x25\x29\x27\x86\xa0\xef\xbe\x18\x52\x52\x24\xdb\xdb\xa6\xfb\x64\x99\x1c\xce\xfc\xe6\x37\x7f\xdb\x76\x7d\x11\x5f\xe9\xfa\x60\xc4\xae\x74\xf0\xe6\xf5\xbf\xff\xf3\xaa\x36\x68\x51\x39\xf8\xc0\x72\xdc\x6a\x7d\x0f\x1b\x95\x67\xf0\x4e\x4a\xf0\x42\x16\xe8\xde\xec\x91\x67\xf1\x5d\x29\x2c\x58\xdd\x98\x1c\x21\xd7\x1c\x41\x58\x90\x22\x47\x65\x91\x43\xa3\x38\x1a\x70\x25\xc2\xbb\x9a\xe5\x25\xc2\x9b\xec\xf5\x70\x0b\x85\x6e\x14\x8f\x85\xf2\xf7\xff\xdf\x5c\xbd\xbf\xb9\x7d\x0f\x85\x90\x08\xfd\x99\xd1\xda\x01\x17\x06\x73\xa7\xcd\x01\x74\x01\x6e\x62\xcc\x19\xc4\x2c\xbe\x58\x77\x5d\x1c\xb7\x2d\x70\x2c\x84\x42\x48\x2a\xcd\x51\x26\xd0\x9f\x2e\xea\xfb\x1d\xbc\xbd\x84\x2d\xb3\x08\x8b\xec\x4a\xab\x42\xec\xb2\x5f\x59\x7e\xcf\x76\x48\x42\x6d\x0b\x0e\xab\x5a\x32\x87\x90\x94\xc8\x38\x9a\x04\x16\xc3\xf3\xa7\x2b\x51\xd5\xda\xb8\xe1\x6a\xbd\x06\x52\x9e\xdd\xb0\x8a\xb4\x90\xcf\x04\xd8\xdb\x06\x54\x4e\xb8\x03\x14\x3a\x78\x3e\x13\xb4\x79\x89\x15\xcb\x62\x77\xa8\x8f\x6f\x9c\x69\x72\x07\x6d\x1c\xe5\x1e\x64\x1c\xad\xd7\xb0\xb9\x0e\x5e\x23\x29\xcd\xe2\x68\x73\x1d\x1e\x6d\xae\xb3\x3b\xd2\xd0\x75\xf0\xf5\x4f\xab\xd5\xdb\x44\xf0\x95\xae\x04\xe1\x75\x87\xe4\x6b\x1c\xb5\x2d\x18\xa6\x76\x08\x8b\x2f\x2b\x58\x14\xc4\xc1\x22\xfb\x20\x50\x72\x0b\xaf\xba\x2e\x8e\xa2\xe0\x43\xcd\x6c\xce\x24\x2c\x8a\x11\x48\xa9\x49\x86\x6c\xee\x99\x6c\x70\x00\x90\x90\xe1\x51\x2a\x81\x82\x74\x65\x31\x00\x40\x74\x56\x4f\xdb\x82\x28\xfc\x81\x90\x92\x6d\x25\x1d\x5e\xb4\x2d\xa0\xe2\xd0\x75\x41\xdb\xe8\x44\xf8\x7b\xeb\x39\xb8\x63\x3b\xe8\xba\xe0\x03\x09\x7b\xb8\x73\x7f\x30\xf8\xf3\x9e\xef\x70\x70\x87\xd2\x18\xc4\x4e\x69\x83\xb0\x43\x85\x86\x39\xa1\x76\x80\x7c\x87\x01\xab\x05\x9f\x2b\x24\xf9\x0a\x1e\x84\x2b\x61\x81\x13\x8b\x41\xcb\x11\x2b\xf8\x23\x56\x08\xf7\x93\x10\x19\xcb\xe0\x6e\x14\xb2\xe8\xc0\x69\x50\x42\xae\x80\x29\x0e\xb6\xd4\x8d\xe4\xb0\x45\x68\x6a\xce\x1c\x72\xa8\x98\x6a\x98\x94\x87\x8c\x6c\x9f\x35\x1c\x68\x54\xda\xd1\xe1\x47\x25\xbe\x35\x74\xfc\xe9\xf3\xc8\xe4\x45\xc0\x40\x54\x8e\x8f\xbe\x86\xb3\x23\x3e\x8f\x09\x1d\xbe\x43\xae\x4f\xe8\xb5\x4e\x1b\xaa\x0f\x4f\xf2\x6d\xff\xa7\x7f\xb4\x70\x55\x2d\xe9\xa6\x36\x42\xb9\x02\x12\x2e\x98\xc4\xdc\xad\x5f\xda\x35\x47\xea\x00\x6b\xad\x30\x79\x52\xd2\xbf\x7b\x1c\x6b\x29\x68\x58\xf4\xd5\x17\x9c\x08\xc5\x6a\x30\x47\xb1\x47\x13\x0c\xff\x3e\xfc\xeb\x4e\x00\xce\xe2\x3f\x00\x2b\x1a\x95\x8f\xc0\x20\xf9\xad\x41\x73\x48\x60\x39\xa7\x34\xf5\xe2\x7d\xfd\xfa\x17\x5d\x07\xdf\x1a\x34\x02\xed\xdf\x44\x74\x1a\xeb\xe1\x22\x8b\x23\xff\x78\x39\x83\xdd\x75\x70\x31\x95\x4a\xa7\x56\x96\x29\x1c\x87\xaa\xeb\x3c\x48\x2a\xfa\xc8\xa0\x6b\x8c\x82\xe5\xbf\xa6\x0a\xae\xa4\x40\xe5\x5a\x38\xb2\x92\x85\x16\xd1\xa5\xd9\x54\xff\x91\x50\x1a\x47\x33\x82\xd7\x6b\xf8\xe8\xb3\x0e\x82\x29\x0b\x0c\xb6\x8d\x90\xd4\xa1\xa9\x57\xf9\x94\xa4\x92\xf1\x4d\x76\xee\xec\x7a\x0d\x37\xda\x21\xb8\x92\xb9\x15\x1c\x74\x03\x0a\x91\x53\x6e\xe7\x4c\xca\xb9\xf0\x47\xf5\x60\x58\xbd\x4c\x61\x8b\x05\x15\x23\x49\x8c\x6a\x2b\x74\xa5\xe6\x2b\x4a\xe9\x13\x33\x64\xe5\x81\xd9\x1e\x1e\x72\x28\x8c\xae\x80\x81\x33\x4c\x59\x96\x3b\xa1\x55\x28\x23\x0a\xc6\xe4\xd0\x3f\xca\x75\x55\x09\x47\x25\xa5\x0d\x18\x2d\x25\x72\xd8\xb2\xfc\x3e\x8b\x9f\x15\xa7\xc0\xcc\x10\xa2\xe1\x3c\x9c\xfe\xa2\x90\x22\xf4\xcf\x02\x34\xaa\x38\x0d\x4f\x1f\x13\x4f\x17\x34\xfe\xc7\x0e\xbd\x9e\x06\x08\x91\xfd\x23\x42\x80\x15\x0e\x0d\x88\x20\x98\x4b\x6d\x91\xaf\x48\xad\xd5\xe1\x3d\x85\x47\xe1\xa3\x1b\x73\xfc\x41\x48\x49\x1d\x08\x1f\x31\x6f\x88\x2f\x57\x1a\xdd\xec\x4a\x6f\x99\x1b\x8f\xee\xa1\x14\x79\x09\xb9\x41\x16\x04\x66\x74\x3f\x97\xd1\x21\x0d\x66\xe7\x44\xa4\x7b\x5c\x81\xbe\xa7\x4a\x3d\xcf\x5a\x16\x50\x64\xcb\x0b\xf7\x78\xed\x3f\xd3\x38\x12\x05\xbc\xd0\xf7\xbe\x52\x6a\xa6\x44\xbe\x4c\x86\xe9\xde\x75\x6f\x4f\x86\x31\xb5\xcb\x19\x4f\x6c\x18\xcb\x89\x2f\x8b\xe8\xbb\x96\xe1\x12\xdc\x63\xc6\xcd\x7e\x0c\xfa\x91\x78\x1f\xba\x5b\x67\x28\xb1\x45\x55\x4b\xac\x50\xb9\x10\xbd\xa2\x72\x59\xb8\x41\xf3\x4c\xae\x82\xf8\x32\xa5\x25\x80\x34\xb6\x71\xb4\x6d\xfc\xcc\xde\x1e\x1c\xda\xec\x06\x1f\xfe\xd7\x14\x05\x9a\xa5\x12\x32\xf5\x97\xd9\x1f\x46\x38\xec\x1f\x26\x53\x75\xcb\xe4\x8c\x84\x07\x15\x3a\xf6\x32\x11\xfc\xf2\xe5\x3e\x59\x9d\xd0\xbf\xb9\x4e\xd3\xd8\x8f\xc7\xbe\xd5\x8a\x93\xd5\x61\x1c\xa0\x27\xa3\x9d\x06\x98\x28\x60\x7f\x2e\xae\xe7\xf6\x83\xff\xc2\x1e\x5e\x5c\xd2\x6c\xf4\x41\x8d\xbe\x0f\x39\x80\x1d\x1f\x07\xfc\x17\x7b\xc2\x1b\x45\x03\x26\x94\x76\x40\xf2\xf3\xca\x9e\x83\xd9\x9b\xf3\x96\x42\x4f\x9d\x7d\x9f\x04\x25\xa5\x40\xf4\x09\x44\x97\x43\x94\xc3\xbc\x0d\x14\xd2\x26\xb7\xb1\x7d\x22\x85\x16\x28\xf8\xd8\xa0\x29\x9d\x68\x96\x19\xec\x17\x72\xe6\x6b\xbe\x9f\x47\x9b\xeb\x61\x09\x7b\x56\x92\x09\xbe\x4c\xbd\xb6\x36\x8e\x04\x5f\xc1\x17\x8a\x94\x75\x26\xd7\x6a\x9f\xbd\x73\x5a\x1c\x2b\xa0\x7c\x18\x1d\x10\x3c\xee\xe2\x89\xbb\x7e\x5e\x5b\x5a\xe5\xfd\xc8\x95\x8d\x21\xa6\xa6\xad\xfc\x49\x20\xd4\x24\x83\x9a\x19\xeb\xb3\x25\x1c\xeb\xe2\x68\xca\x8c\x4b\xf1\xf8\xec\xd3\xe7\x99\x13\x3f\xb3\xa7\xf8\x0d\x0f\x1f\x1d\xe1\x5d\x40\x72\x4b\x2a\x93\x27\xd5\x61\x1b\x7a\xc6\x32\x53\x31\x75\x38\xda\x66\xce\xad\x33\x19\x4c\x96\xaa\xf9\x66\x73\x3e\x3a\x53\x3f\x53\x08\xfd\x67\x99\x17\xbb\xfe\x33\xa5\x30\xd1\x58\x16\x04\x2f\xf8\x7c\xa2\xa2\x77\x62\x72\xf6\x49\x7c\xee\x7b\x19\x5c\x42\x5e\xec\xa8\xd9\x4d\xc1\xfc\x15\x00\x00\xff\xff\x0f\x72\xac\xe1\xed\x0d\x00\x00") +var _templateEntTmpl = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x9c\x57\x4d\x6f\xdb\x3c\x12\x3e\x4b\xbf\x62\x2a\xb8\x0b\x2b\x70\xe4\x6e\x6f\xdb\x45\x0e\xdd\xa4\x05\x0c\x2c\xb2\x1f\x49\xb1\x87\xa2\x68\x69\x71\x64\x71\x43\x91\x2a\x49\x39\x31\x04\xfd\xf7\x17\x43\x4a\x8a\x64\xbb\x6d\xfa\x9e\x6c\x93\xc3\x99\x67\x9e\xf9\x74\xdb\xae\x2f\xe2\x6b\x5d\x1f\x8c\xd8\x95\x0e\xde\xbe\xf9\xeb\xdf\x2e\x6b\x83\x16\x95\x83\x8f\x2c\xc7\xad\xd6\x0f\xb0\x51\x79\x06\xef\xa5\x04\x2f\x64\x81\xee\xcd\x1e\x79\x16\xdf\x97\xc2\x82\xd5\x8d\xc9\x11\x72\xcd\x11\x84\x05\x29\x72\x54\x16\x39\x34\x8a\xa3\x01\x57\x22\xbc\xaf\x59\x5e\x22\xbc\xcd\xde\x0c\xb7\x50\xe8\x46\xf1\x58\x28\x7f\xff\xcf\xcd\xf5\x87\xdb\xbb\x0f\x50\x08\x89\xd0\x9f\x19\xad\x1d\x70\x61\x30\x77\xda\x1c\x40\x17\xe0\x26\xc6\x9c\x41\xcc\xe2\x8b\x75\xd7\xc5\x71\xdb\x02\xc7\x42\x28\x84\xa4\xd2\x1c\x65\x02\xfd\xe9\xa2\x7e\xd8\xc1\xbb\x2b\xd8\x32\x8b\xb0\xc8\xae\xb5\x2a\xc4\x2e\xfb\x37\xcb\x1f\xd8\x0e\x49\xa8\x6d\xc1\x61\x55\x4b\xe6\x10\x92\x12\x19\x47\x93\xc0\x62\x78\xfe\x7c\x25\xaa\x5a\x1b\x37\x5c\xad\xd7\x40\xca\xb3\x5b\x56\x91\x16\xf2\x99\x00\x7b\xdb\x80\xca\x09\x77\x80\x42\x07\xcf\x67\x82\x36\x2f\xb1\x62\x59\xec\x0e\xf5\xf1\x8d\x33\x4d\xee\xa0\x8d\xa3\xdc\x83\x8c\xa3\xf5\x1a\x36\x37\xc1\x6b\x24\xa5\x59\x1c\x6d\x6e\xc2\xa3\xcd\x4d\x76\x4f\x1a\xba\x0e\xbe\xfd\xdf\x6a\xf5\x2e\x11\x7c\xa5\x2b\x41\x78\xdd\x21\xf9\x16\x47\x6d\x0b\x86\xa9\x1d\xc2\xe2\xeb\x0a\x16\x05\x71\xb0\xc8\x3e\x0a\x94\xdc\xc2\x65\xd7\xc5\x51\x14\x7c\xa8\x99\xcd\x99\x84\x45\x31\x02\x29\x35\xc9\x90\xcd\x3d\x93\x0d\x0e\x00\x12\x32\x3c\x4a\x25\x50\x90\xae\x2c\x06\x00\x88\xce\xea\x69\x5b\x10\x85\x3f\x10\x52\xb2\xad\xa4\xc3\x8b\xb6\x05\x54\x1c\xba\x2e\x68\x1b\x9d\x08\x3f\xef\x3c\x07\xf7\x6c\x07\x5d\x17\x7c\x20\x61\x0f\x77\xee\x0f\x06\x7f\x3e\xf0\x1d\x0e\xee\x50\x1a\x83\xd8\x29\x6d\x10\x76\xa8\xd0\x30\x27\xd4\x0e\x90\xef\x30\x60\xb5\xe0\x73\x85\x24\x2f\xe1\x51\xb8\x12\x16\x38\xb1\x18\xb4\x1c\xb1\x82\xbf\x62\x85\x70\x3f\x0b\x91\xb1\x0c\xee\x47\x21\x8b\x0e\x9c\x06\x25\xe4\x0a\x98\xe2\x60\x4b\xdd\x48\x0e\x5b\x84\xa6\xe6\xcc\x21\x87\x8a\xa9\x86\x49\x79\xc8\xc8\xf6\x59\xc3\x81\x46\xa5\x1d\x1d\x7e\x52\xe2\x7b\x43\xc7\x9f\xbf\x8c\x4c\x5e\x04\x0c\x44\xe5\xf8\xe8\x5b\x38\x3b\xe2\xf3\x98\xd0\xe9\xf7\x40\x48\xff\xe2\x38\x4f\x18\xe7\xc2\x09\xad\x98\x1c\xd2\xb4\x67\x34\x14\x1d\x1f\x0a\x76\xc8\xee\xe8\x7c\xfa\x9d\x51\x1e\xcd\xb2\x0a\xe6\x59\x31\xc2\x2a\xb2\x3e\x42\xe4\x57\x06\xe1\x33\x78\x3f\x15\xba\xd6\x55\x45\x5d\xeb\xb2\xeb\x42\x18\xb3\x90\x68\x41\xf2\xc7\xfe\x87\x5a\x9f\xe0\xb5\x4e\x1b\xea\x0f\x3d\xea\xf0\xa3\x7f\xb4\x70\x55\x2d\xe9\xa6\x36\x42\xb9\x02\x12\x2e\x98\xc4\xdc\xad\x5f\xdb\x35\x47\xea\x80\x6b\xad\x30\x79\x56\xd2\xbf\x7b\x1a\x7b\x49\xd0\xb0\xe8\xbb\x4f\x0f\xce\x37\x2b\x83\x39\x8a\x3d\x9a\x60\xf8\xbf\xc3\xaf\xee\x04\xe0\x2c\xff\x07\x60\x45\xa3\xf2\x11\x18\x24\xff\x69\xd0\x1c\x12\x58\xce\x53\x2a\xf5\xe2\x7d\xff\xf2\x2f\xba\x0e\xbe\x37\x68\x04\xda\x1f\x64\xf4\x34\xd7\x87\x8b\x2c\x8e\xfc\xe3\xe5\x0c\x76\xd7\xc1\xc5\x54\x2a\x9d\x5a\x59\xa6\x70\x9c\xaa\x5d\xe7\x41\x52\xd3\x8b\x0c\xba\xc6\x28\x58\xfe\x65\xaa\xe0\x5a\x0a\x54\xae\x85\x23\x2b\x59\x68\x91\x5d\x9a\x4d\xf5\x1f\x09\xa5\x71\x34\x23\x78\xbd\x86\x4f\xbe\xea\x20\x98\xb2\xc0\x60\xdb\x08\x49\x13\x8a\x7a\xb5\x2f\x49\x6a\x19\x7e\xc8\xcc\x9d\x5d\xaf\xe1\x56\x3b\x04\x57\x32\xb7\x82\x83\x6e\x40\x21\x72\xaa\xed\x9c\x49\x39\x17\xfe\xa4\x1e\x0d\xab\x97\x29\x6c\xb1\xa0\x66\x44\x12\xa3\xda\x0a\x5d\xa9\xf9\x8a\x4a\xfa\xc4\x0c\x59\x79\x64\xb6\x87\x87\x1c\x0a\xa3\x2b\x60\xe0\x0c\x53\x96\xe5\x54\x80\xa1\x8d\x50\x30\x26\x87\xfe\x51\xae\xab\x4a\x38\x6a\x29\xda\x80\xd1\x52\x22\x87\x2d\xcb\x1f\xb2\xf8\x45\x71\x0a\xcc\x0c\x21\x1a\xce\xc3\xe9\xbf\x14\x52\x84\xfe\x5c\x80\x46\x15\xa7\xe1\xe9\x63\xe2\xe9\x82\xc6\x7f\xd8\x61\xd6\xd1\x00\x25\xb2\x7f\x45\x08\xb0\xc2\xa1\x01\x11\x04\x73\xa9\x2d\xf2\x15\xa9\xb5\x3a\xbc\xa7\xf0\x28\x7c\x72\x63\x8e\x3f\x0a\x29\xa9\x03\xe3\x13\xe6\x0d\xf1\xe5\x4a\xa3\x9b\x5d\xe9\x2d\x73\xe3\xd1\x3d\x96\x22\x2f\x21\x37\xc8\x82\xc0\x8c\xee\x97\x32\x3a\xa4\xc1\xec\x9c\x88\x74\x4f\x2b\xd0\x0f\x54\xa9\xe7\x59\xcb\x02\x8a\x6c\x79\xe1\x9e\x6e\xfc\xd7\x34\x8e\x44\x01\xaf\xf4\x83\xaf\x94\x9a\x29\x91\x2f\x93\x61\xbb\xe9\xba\x77\x27\xcb\x08\x8d\x8b\x19\x4f\x6c\x58\x4b\x12\x5f\x16\xd1\x4f\x2d\xc3\x15\xb8\xa7\x8c\x9b\xfd\x18\xf4\x23\xf1\x3e\x74\x77\xce\x50\x62\x8b\xaa\x96\x48\x8d\x37\x44\xaf\xa8\x5c\x16\x6e\xd0\xbc\x90\xab\x20\xbe\x4c\x69\xba\x90\xc6\x36\x8e\xb6\x8d\x1f\x1a\xdb\x83\x43\x9b\xdd\xe2\xe3\x3f\x9a\xa2\x40\xb3\x54\x42\xa6\xfe\x32\xfb\x9f\x11\x0e\xfb\x87\xc9\x54\xdd\x32\x39\x23\xe1\x41\x85\x8e\xbd\x4c\x04\xbf\x7a\xbd\x4f\x56\x27\xf4\x6f\x6e\xd2\x34\xf6\xeb\x41\xdf\x6a\xc5\xc9\xea\x34\x2e\x10\x27\xab\x0d\x0d\x33\x51\xc0\xfe\x5c\x5c\xcf\xed\x47\x7f\x87\x3d\xbc\xba\xa2\xdd\xc0\x07\x35\xfa\x39\xe4\x15\xcc\x06\x65\xc0\x7f\xb1\x27\xbc\x51\x34\x60\x42\x69\x07\x24\xbf\xaf\xec\x25\x98\xbd\x39\x6f\xa9\x9f\xa8\xd3\xef\x27\x41\x49\x29\x10\x7d\x02\xd1\xe5\x10\xe5\x30\x6f\x03\x85\xb4\xc9\x6e\x6c\x9f\x48\xa1\x05\x0a\x3e\x36\x68\x4a\x27\x9a\x65\x06\xfb\x3f\x24\xcc\xd7\x7c\x3f\x8f\x36\x37\xc3\x12\xfa\xa2\x24\x13\x7c\x99\x7a\x6d\x6d\x1c\x09\xbe\x82\xaf\x14\x29\xeb\x4c\xae\xd5\x3e\x7b\xef\xb4\x38\x56\x40\xf9\x30\x3a\x20\x78\xdc\xc5\x13\x77\xfd\xbc\xb6\xf4\x57\xc6\x8f\x5c\xd9\x18\x62\x6a\xda\xca\x9f\x05\x42\x4d\x32\xa8\x99\xb1\x3e\x5b\xc2\xb1\x2e\x8e\xa6\xcc\xf8\xa7\x60\x7c\xf6\xf9\xcb\xcc\x89\xdf\xd9\x53\xfc\x52\x84\x4f\x8e\xf0\x2e\x20\xb9\x23\x95\xc9\xb3\xea\xb0\x0d\xbd\x60\x99\xa9\x98\x3a\x1c\x6d\x33\xe7\xd6\x99\x0c\x26\x4b\xd5\x7c\xb3\x39\x1f\x9d\xa9\x9f\x29\x84\xfe\xb3\xcc\x8b\x5d\xff\x35\xa5\x30\xd1\x58\x16\x04\x2f\xf8\x7c\xa2\xa2\x77\x62\x72\xf6\x59\x7c\xe9\x7b\x19\x5c\x41\x5e\xec\xa8\xd9\x4d\xc1\xfc\x11\x00\x00\xff\xff\xb1\xd7\xce\xa8\xed\x0e\x00\x00") func templateEntTmplBytes() ([]byte, error) { return bindataRead( @@ -741,7 +741,7 @@ func templateEntTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "template/ent.tmpl", size: 3565, mode: os.FileMode(420), modTime: time.Unix(1567935214, 0)} + info := bindataFileInfo{name: "template/ent.tmpl", size: 3821, mode: os.FileMode(420), modTime: time.Unix(1567952289, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -786,7 +786,7 @@ func templateHeaderTmpl() (*asset, error) { return a, nil } -var _templateImportTmpl = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x84\x92\x41\x6b\x9d\x40\x14\x85\xd7\xce\xaf\x38\xc8\x5b\x24\xa1\xd1\x34\xbb\x16\xba\x08\x8f\x14\x1e\x94\x52\x78\xdd\x97\x79\xe3\x55\x87\xe8\x5c\x7b\xe7\x9a\x36\x0c\xfe\xf7\x32\xc6\xf4\xa5\x14\xda\x95\x67\xee\xfd\xd4\x73\x8e\xa6\x54\x5f\x99\x3d\x4f\x4f\xe2\xbb\x5e\x71\x7b\xf3\xf6\xdd\xf5\x24\x14\x29\x28\x3e\x5a\x47\x27\xe6\x07\x1c\x82\xab\x70\x37\x0c\x58\xa1\x88\xbc\x97\x47\x6a\x2a\xf3\xb5\xf7\x11\x91\x67\x71\x04\xc7\x0d\xc1\x47\x0c\xde\x51\x88\xd4\x60\x0e\x0d\x09\xb4\x27\xdc\x4d\xd6\xf5\x84\xdb\xea\xe6\x65\x8b\x96\xe7\xd0\x18\x1f\xd6\xfd\xa7\xc3\xfe\xfe\xf3\xf1\x1e\xad\x1f\x08\xdb\x4c\x98\x15\x8d\x17\x72\xca\xf2\x04\x6e\xa1\xaf\x5e\xa6\x42\x54\x99\xab\x7a\x59\x8c\x49\x09\x0d\xb5\x3e\x10\x4a\x3f\x4e\x2c\x5a\x62\x59\xcc\xb3\xc4\x85\x29\xca\x76\xd4\xd2\x14\xa5\xe3\xa0\xf4\x73\x95\x24\xc2\x12\xb3\x1a\xad\xf6\xf9\x1a\x55\x1c\x87\xc7\x4d\xfa\xd0\xad\x5b\xf5\x23\x95\xc6\x14\xb9\x26\xf8\x2e\xb0\x10\x3a\x0a\x24\xea\x43\x07\x0e\xe8\xc4\x4e\x3d\x94\xc6\x69\xb0\x4a\x11\xab\xa3\x22\x25\xf8\x16\x81\x15\x17\xf4\x1d\xbb\x6a\xcf\xa1\xf5\x5d\xf5\xc5\xba\x07\xdb\x11\x76\x2f\xea\x32\x1b\x2d\x8a\x32\xa5\xbf\xa1\x65\xa9\x27\xa1\xc6\x3b\xab\x54\xfe\x03\x5a\xc7\xe7\x73\x46\x53\xc2\x0f\xaf\xfd\x19\x3f\xba\x9e\x46\x0b\x2c\x0b\xf2\x63\xaa\xcc\x21\x25\x50\x68\xb0\xf9\xdd\xa4\x29\xca\xce\x6b\x3f\x9f\x2a\xc7\x63\xdd\x6e\x7f\x80\x0f\x6e\x3e\x59\x65\xa9\x29\xac\xfd\xfd\x8f\xa9\x1b\x6f\x07\x72\x99\x4d\x09\x62\x43\x4e\xfd\xed\x0d\x76\x51\x59\xb2\xd1\xf7\x1f\xb0\xab\x8e\xdb\xe1\x7a\x6d\xe1\x4f\x70\xfb\x7c\x99\xdb\xee\xa9\x0e\xeb\x28\x6e\xf8\x73\x21\x1b\xf6\x3b\x77\x8e\x71\xfd\x2a\x52\xd6\x97\xe6\x9c\xef\x57\x00\x00\x00\xff\xff\xaf\xd4\x36\xb3\xf0\x02\x00\x00") +var _templateImportTmpl = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x84\x92\x41\x6b\xdc\x3e\x10\xc5\xcf\xd6\xa7\x78\x98\x3d\x24\xe1\xbf\x76\xfe\xb9\xb5\xd0\x43\x08\x09\x04\x4a\x09\x6c\xef\x45\x2b\x8f\xec\x61\x6d\xc9\x95\xc6\x69\x83\xf0\x77\x2f\xf2\x7a\xb3\x6d\x5a\xda\x93\x9f\x66\x7e\x1e\xcf\x7b\x56\x4a\xf5\x95\xba\xf3\xe3\x4b\xe0\xb6\x13\xdc\x5c\xff\xff\x6e\x3b\x06\x8a\xe4\x04\x0f\xda\xd0\xde\xfb\x03\x1e\x9d\xa9\x70\xdb\xf7\x58\xa0\x88\xdc\x0f\xcf\xd4\x54\xea\x73\xc7\x11\xd1\x4f\xc1\x10\x8c\x6f\x08\x1c\xd1\xb3\x21\x17\xa9\xc1\xe4\x1a\x0a\x90\x8e\x70\x3b\x6a\xd3\x11\x6e\xaa\xeb\x53\x17\xd6\x4f\xae\x51\xec\x96\xfe\xc7\xc7\xbb\xfb\x4f\xbb\x7b\x58\xee\x09\x6b\x2d\x78\x2f\x68\x38\x90\x11\x1f\x5e\xe0\x2d\xe4\xa7\x8f\x49\x20\xaa\xd4\x55\x3d\xcf\x4a\xa5\x84\x86\x2c\x3b\x42\xc9\xc3\xe8\x83\x94\x98\x67\x75\x94\xb8\x50\x45\x69\x07\x29\x55\x51\x1a\xef\x84\xbe\x2f\x92\x42\xf0\x21\x66\x35\x68\xe9\xf2\x33\x4a\x30\xde\x3d\xaf\x92\x5d\xbb\x74\x85\x07\x2a\x95\x2a\x72\x4c\xe0\xd6\xf9\x40\x68\xc9\x51\x10\x76\x2d\xbc\x43\x1b\xf4\xd8\x21\x8e\x64\xd8\xb2\x35\x10\x1a\xc6\x5e\x0b\x45\x2c\xbb\x15\x29\x81\x2d\x9c\x17\x5c\xd0\x57\x6c\xaa\x3b\xef\x2c\xb7\xd5\x93\x36\x07\xdd\x12\x36\x27\x75\x99\x57\x2e\x8a\x32\xa5\xdf\xa1\x79\xae\xc7\x40\x0d\x1b\x2d\x54\xfe\x05\x5a\xca\xe7\x73\x46\x53\xc2\x37\x96\xee\x8c\xef\x4c\x47\x83\x06\xe6\x19\x79\x4c\x95\x39\xa4\x04\x72\xcd\x71\x83\x94\x10\xb4\xcb\xab\x7d\xf9\x0f\x1b\x8b\xf7\x1f\xb0\xa9\x76\x12\x26\x23\x0f\x4c\x7d\x13\xb1\x5d\xb8\xf3\x68\x5b\x3d\x1d\xda\x27\x2d\x5d\x9e\x75\x9a\xf9\x66\x64\xd6\xdb\x35\x8f\xb5\xae\x8a\xb2\x65\xe9\xa6\x7d\x65\xfc\x50\xdb\xf5\xae\xb1\x33\xd3\x5e\x8b\x0f\x35\xb9\xe5\x4f\xfd\x8b\xa9\x1b\xd6\x3d\x99\xcc\xfe\xba\x7a\x14\x1f\x72\x10\xab\x81\xe3\x61\xfb\x07\x8f\xeb\x45\xc9\xdc\xfa\x4e\xf5\xb8\x94\x5e\xad\x2e\x81\xaf\xd8\x6b\xae\x6f\x2d\x65\x7d\xa9\xce\xfe\x7e\x04\x00\x00\xff\xff\x3b\xd4\xf1\x71\x5a\x03\x00\x00") func templateImportTmplBytes() ([]byte, error) { return bindataRead( @@ -801,7 +801,7 @@ func templateImportTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "template/import.tmpl", size: 752, mode: os.FileMode(420), modTime: time.Unix(1567935214, 0)} + info := bindataFileInfo{name: "template/import.tmpl", size: 858, mode: os.FileMode(420), modTime: time.Unix(1567952289, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -841,7 +841,7 @@ func templateMigrateMigrateTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "template/migrate/migrate.tmpl", size: 2450, mode: os.FileMode(420), modTime: time.Unix(1567946944, 0)} + info := bindataFileInfo{name: "template/migrate/migrate.tmpl", size: 2450, mode: os.FileMode(420), modTime: time.Unix(1567952288, 0)} a := &asset{bytes: bytes, info: info} return a, nil } diff --git a/entc/gen/template/ent.tmpl b/entc/gen/template/ent.tmpl index 4e5ba53ed..60934c238 100644 --- a/entc/gen/template/ent.tmpl +++ b/entc/gen/template/ent.tmpl @@ -27,6 +27,12 @@ type {{ $.Name }} struct { {{ pascal $e.Name }} {{ if not $e.Unique }}[]{{ end }}*{{ $e.Type.Name }} `{{ $e.StructTag }}` {{ end -}} {{ end -}} + {{ with $.StructFields -}} + // additional struct fields defined in the schema. + {{ range $_, $f := $.StructFields -}} + {{ $f.Name }} {{ $f.Type }} {{ with $f.Tag -}}`{{ . }}`{{ end }} {{ with $f.Comment -}}// {{ . }}{{ end }} + {{ end -}} + {{ end -}} } {{ range $_, $storage := $.Storage }} diff --git a/entc/gen/template/import.tmpl b/entc/gen/template/import.tmpl index 23765baa5..db2a2178d 100644 --- a/entc/gen/template/import.tmpl +++ b/entc/gen/template/import.tmpl @@ -14,11 +14,14 @@ import ( "strings" "time" - {{/* ignore generting on graph templates */}} + {{/* ignore generting on graph specififc templates */}} {{ if not (eq $.Config.Package $.Package) }} "{{ $.Config.Package }}/predicate" "{{ $.Config.Package }}/{{ $.Package }}" {{ with $.Config.Schema }} "{{ . }}" {{ end }} + {{ range $_, $f := $.StructFields -}} + {{ with $f.PkgPath }}"{{ . }}"{{ end }} + {{ end -}} {{ end }} "github.com/facebookincubator/ent" diff --git a/entc/gen/type.go b/entc/gen/type.go index a113ea405..9f3955631 100644 --- a/entc/gen/type.go +++ b/entc/gen/type.go @@ -20,7 +20,8 @@ import ( ) type ( - // Type represents one node/type in the graph, its relations and the information it holds. + // Type represents one node-type in the graph, its relations and + // the information it holds. Type struct { Config // Name holds the type/ent name. @@ -35,6 +36,9 @@ type ( Edges []*Edge // Indexes are the configured indexes for this type. Indexes []*Index + // StructFields are additional struct fields to be added to + // the generated entity. + StructFields []*load.StructField } // Field holds the information of a type field used for the templates. @@ -84,8 +88,8 @@ type ( // Relation holds the relation info of an edge. Rel Relation // SelfRef indicates if this edge is a self-reference to the same - // type with the same name. For example, a User type have one of - // following edges: + // type with the same name (symmetric relation). For example, a User + // type have one of following edges: // // edge.To("friends", User.Type) // many 2 many. // edge.To("spouse", User.Type).Unique() // one 2 one. @@ -129,8 +133,9 @@ func NewType(c Config, schema *load.Schema) (*Type, error) { Type: c.IDType, StructTag: `json:"id,omitempty"`, }, - Fields: make([]*Field, len(schema.Fields)), - fields: make(map[string]*Field, len(schema.Fields)), + Fields: make([]*Field, len(schema.Fields)), + fields: make(map[string]*Field, len(schema.Fields)), + StructFields: schema.StructFields, } for i, f := range schema.Fields { switch { diff --git a/entc/integration/ent/card.go b/entc/integration/ent/card.go index 1408f1e87..95c9546ed 100644 --- a/entc/integration/ent/card.go +++ b/entc/integration/ent/card.go @@ -12,6 +12,8 @@ import ( "strconv" "time" + "log" + "github.com/facebookincubator/ent/dialect/gremlin" "github.com/facebookincubator/ent/dialect/sql" ) @@ -27,6 +29,9 @@ type Card struct { CreatedAt time.Time `json:"created_at,omitempty"` // UpdatedAt holds the value of the "updated_at" field. UpdatedAt time.Time `json:"updated_at,omitempty"` + // additional struct fields defined in the schema. + RequestID string // RequestID. + Logger *log.Logger // Logger. } // FromRows scans the sql response data into Card. diff --git a/entc/integration/ent/schema/card.go b/entc/integration/ent/schema/card.go index 2785110fd..fa0c45a33 100644 --- a/entc/integration/ent/schema/card.go +++ b/entc/integration/ent/schema/card.go @@ -5,6 +5,7 @@ package schema import ( + "log" "time" "github.com/facebookincubator/ent" @@ -15,6 +16,11 @@ import ( // Card holds the schema definition for the CreditCard entity. type Card struct { ent.Schema + + // additional fields to be added to the schema. + internal int // internal. + RequestID string // RequestID. + Logger *log.Logger // Logger. } // Fields of the Comment. diff --git a/entc/load/bindata.go b/entc/load/bindata.go index baaa3a055..e33f370dc 100644 --- a/entc/load/bindata.go +++ b/entc/load/bindata.go @@ -87,7 +87,7 @@ func templateMainTmpl() (*asset, error) { return a, nil } -var _schemaGo = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xb4\x57\xdb\x6e\xdb\x3c\x12\xbe\x96\x9e\x62\x6a\xa0\x81\x54\x78\xe5\x6e\xef\xd6\x85\x2f\x8a\x36\x05\xb2\x87\x74\xd1\x74\xf7\xa6\x28\x5a\x5a\x1c\xda\x6c\x25\x52\x25\x69\xa7\x69\x90\x77\x5f\x0c\x0f\x3a\xd8\x4e\xf6\xff\x5b\x24\x40\x60\x69\x4e\xe4\x7c\xf3\x71\x86\x5a\x2c\xe0\xb5\xee\x6e\x8c\xdc\x6c\x1d\xbc\x78\xfe\xd7\xbf\xfd\xa5\x33\x68\x51\x39\x78\xcb\x6a\x5c\x6b\xfd\x0d\x2e\x54\x5d\xc1\xab\xa6\x01\x6f\x64\x81\xf4\x66\x8f\xbc\xca\x17\x0b\xf8\xb0\x95\x16\xac\xde\x99\x1a\xa1\xd6\x1c\x41\x5a\x68\x64\x8d\xca\x22\x87\x9d\xe2\x68\xc0\x6d\x11\x5e\x75\xac\xde\x22\xbc\xa8\x9e\x27\x2d\x08\xbd\x53\x9c\x42\x48\xe5\x4d\xfe\x79\xf1\xfa\xfc\xf2\xea\x1c\x84\x6c\x30\xc9\x8c\xd6\x0e\xb8\x34\x58\x3b\x6d\x6e\x40\x0b\x70\xa3\xf5\x9c\x41\xac\xf2\xbc\x63\xf5\x37\xb6\x41\x68\x34\xe3\x79\x2e\xdb\x4e\x1b\x07\x45\x9e\xcd\x50\xd5\x9a\x4b\xb5\x59\x7c\xb5\x5a\xcd\xf2\x6c\x26\x5a\x47\x3f\x06\x45\x83\xb5\x9b\xe5\x79\x36\xdb\x48\xb7\xdd\xad\xab\x5a\xb7\x0b\x11\x13\x96\xaa\xde\xad\x99\xd3\x66\x81\xca\x2d\x6c\xbd\xc5\x96\x2d\x90\x6f\xf0\x0f\x39\xcc\xfe\x44\x50\x21\xb1\xe1\xb3\xbc\xcc\x09\x86\x2b\x2f\x03\x83\xb1\x00\x16\x98\x02\x54\xae\x8a\x0a\xb7\x65\x0e\xae\x99\xf5\x79\x22\x07\x61\x74\x0b\x0c\x6a\xdd\x76\x8d\x24\xb0\x2d\x1a\x88\x58\x54\xb9\xbb\xe9\x30\x85\xb4\xce\xec\x6a\x07\xb7\x79\x76\xc9\x5a\x04\x00\x92\x48\xb5\x01\x80\x2f\x04\xcd\x72\xa6\x58\x8b\x73\xdd\x4a\x87\x6d\xe7\x6e\x66\x5f\xf2\xec\x9c\x6f\xd0\x02\xc0\xc7\x4f\xcf\xe8\xb1\xb7\x24\x1c\xec\xd4\xf4\x2d\x65\x61\xbd\xa9\x7f\x4c\xa6\x3e\xbb\x03\xdb\x0b\xc5\xf1\x07\x5a\xb2\xf5\x8f\xc9\x56\x06\xf9\xc4\xf8\xce\xc3\x12\x42\x1e\xa3\x12\xe4\xbf\x00\x4a\x70\x3c\xc6\x24\xfc\xf5\xc8\x3c\x80\xcd\x07\x0a\xd3\xff\xf9\x34\x2b\x2f\x8b\x1e\xb4\xcc\x81\x07\xdb\xc0\x43\x6b\x38\xb6\x99\x3a\x5c\xc9\x9f\xa3\x25\x9e\x49\xe5\xe2\x63\x74\xb0\xf2\xe7\xc1\x12\xff\x51\xf2\xfb\xae\xf7\x59\x6b\xdd\x4c\x3d\x76\x5e\x3f\xf5\xb9\x94\x4d\xc3\xd6\x0d\xde\xe7\xa3\xa2\x7e\xea\xf5\xae\x73\x52\x2b\xd6\xdc\xe7\xa5\xa3\x7e\xea\xf5\x06\x05\xdb\x35\xee\xde\xfd\xf1\xa0\x3f\x48\xaa\xe3\xcc\x61\x72\x3d\x91\x94\xd7\x7f\x3e\xe9\x7b\xd1\xb6\x3b\xd7\x67\x77\xec\x2b\x93\x7e\xea\xf6\x5f\xd6\x48\x4e\x07\x95\xd8\x0f\x03\xf0\xc9\x6d\xdf\xeb\x4f\x90\xd5\x1f\x95\x63\xae\x7a\xf1\x2f\x50\xd5\xfb\x9d\x60\x6a\x24\xd0\xff\x27\xe8\xd4\xf0\x01\x5e\x1e\x18\x1e\xd2\xf1\x3d\x8a\xb0\xf8\xd4\xce\xa0\xf8\x7c\xbc\xfa\x7b\x14\x91\xb6\x93\xce\x61\x50\xdc\xc3\xd8\x58\x9b\x07\x88\x7a\xa1\xf6\x68\x2c\x1e\x9a\xca\x20\x3e\x5c\xfe\xfb\x4e\x1a\xe4\x07\xb6\x26\x8a\x4f\x54\x2d\x74\xa2\xe3\xb2\x05\xf9\x2f\xd4\x2d\x38\x0e\x85\x8b\x99\xf6\x1c\x7c\x20\xd3\xd8\x77\x3f\x7e\x9a\x22\x7d\x7f\xdb\x3d\xb4\x3c\xd1\x75\x43\x96\x97\x78\xed\xeb\x51\x1b\x64\x0e\x7d\x92\x31\x23\x0a\x1e\xd2\xf2\x4f\x1c\x6d\x6d\x64\xe7\xb4\xa9\x72\xb1\x53\x75\xf2\x2c\x90\xc3\x33\xb2\xa8\xde\xf4\x16\x65\x2c\xf2\x6d\x9e\x29\x84\xe5\x0a\xce\xe8\xf5\x36\xcf\x88\x5a\xcb\x40\x03\xe4\xd5\x07\xb6\x99\x93\xec\xa6\xc3\x65\x2f\x23\x36\xe6\x99\x67\x75\x2f\xa4\x17\x12\x06\xc4\x96\x41\x18\x5e\x48\x1c\x79\xb0\xf4\xe2\xf8\x42\xf2\x54\xf3\x25\xc9\xd3\x4b\x50\x88\x18\xdf\x2b\x44\x8c\x7f\x97\x67\x52\x80\x41\x41\x5b\x0e\x9a\x97\xfe\xf5\xc9\x0a\x94\x6c\x28\x9d\x4c\x21\x89\x61\xd5\xa7\x6f\x50\x94\xde\xd5\xa0\xdb\x19\x05\x0a\x23\xb2\xff\x62\xc6\x6e\x59\x13\xa7\xad\xbf\x75\xa0\xbf\xbe\x8c\xa6\xb7\x54\x0e\x0d\x5d\x06\xe8\x49\x03\x83\xbf\x5f\xbd\xbb\x24\x67\x4f\xaf\x9a\x29\x58\x13\xf2\xe4\xca\x83\x09\x05\x88\xce\x7a\xfd\x15\x6b\x17\x7f\x62\x51\x26\x8b\x16\x36\xad\x4d\xac\x8d\x2b\x95\x50\xac\xe1\xe3\xa7\xf5\x8d\xc3\x39\xa0\x31\xf4\x4f\x15\xbb\xcd\x33\xeb\x4b\x15\x7c\x6f\x03\x40\x52\x85\x7b\x56\x11\x6f\x47\xbe\x3e\xef\x44\x8c\x5c\x96\xbe\x34\x45\x79\x97\x67\x91\x61\x3e\xe4\x72\x05\x96\x09\x0c\x5c\x4c\xb6\x1e\x5c\xd2\x8e\xd0\x4c\x98\xc9\x66\x0e\xa2\x75\xd5\x39\xed\x45\x14\xb3\xb8\xf1\xa7\xdf\x97\xf0\x74\x3f\x9b\x83\x0d\x14\x20\xf7\x00\xb6\xd0\x06\x3e\xcf\xc1\x57\xca\x30\x45\x4c\x0d\xc4\xa7\xa8\x82\x93\x58\x8c\x08\x59\x94\x79\x96\x59\x6f\x7d\xe6\x77\x45\x66\x23\x8e\x85\x91\x3d\x10\x6d\xc4\xc9\xa4\x4a\xc4\x1c\x51\xb8\x57\x05\x1e\x8f\xe8\x99\x34\x03\x47\xfb\xa9\xba\x1c\x16\x4b\x73\x94\xd4\x69\x7c\x0e\xea\x24\xf1\xea\x7e\x6c\x2d\x93\xba\x97\x78\xfd\x30\x9f\xbc\x41\x83\xaa\x10\xbc\x1a\xa4\xa5\xb7\x8a\x23\x73\x39\x6c\x30\x0d\xd1\x50\x93\x90\xc5\x78\xba\x2e\x7d\x16\x93\x79\x3b\x98\xde\xe5\x19\xd5\x54\xf0\xca\x5f\x4c\x9e\xac\xe0\xb9\xc7\x3f\xb3\x22\x48\x56\x70\x16\x95\xd1\xda\x56\xb1\x3f\xad\x80\x75\x1d\x2a\x5e\x24\xc9\x1c\x6c\x3c\x47\xa1\xa9\x8d\x79\xe4\xbb\xdf\x63\xd2\x08\x07\x1a\xf9\xd5\x7d\x50\x5b\x85\xae\x3b\xda\xea\x79\xd8\x5a\xdf\xf9\x26\x0c\x2b\x43\xc8\x74\x67\x1d\x27\x10\xef\xb7\x8f\x99\x82\xe4\x3f\x86\x24\xe2\x1e\x7c\xe0\xa8\x90\xfc\xc7\xd1\x79\xa8\xd2\xbd\x7b\x94\xe2\x45\xda\xfe\x99\x7f\xf2\xe5\xf4\x69\x2f\xc1\xc7\x08\x10\x90\x34\xd4\x6d\xe9\xa5\xb1\x86\xe3\x43\x40\xe2\x81\xfe\x77\x93\x1e\x49\x33\xa9\x8a\xad\xaa\xb0\x65\x6c\x98\x43\xcb\x80\x6b\xc3\x3a\xeb\x7b\x5d\xc8\x3f\xd1\xa6\x45\xb7\xd5\x1c\xae\xa5\xdb\x82\xc1\x5a\xef\xe9\x43\x52\x03\x2a\xbb\x33\x08\x4a\x43\xc7\x94\xac\x2d\x7d\x28\xb6\x21\xbc\x54\x9b\xd8\x1a\x8f\x3a\xd2\x51\x5f\x14\x69\x76\xf6\x5f\x12\x87\x1d\x92\xa3\x40\x03\x14\xae\x28\x03\xba\x02\xf6\x1e\xf7\xb0\x99\xa2\x7c\x09\xfb\x71\x59\x33\xf2\x5f\x9d\xa8\x68\xca\x28\x6c\x38\x16\x77\x4f\x65\x89\x9d\x14\x7c\x90\x70\x6e\xee\xa8\x5e\x11\xbb\x89\x7b\x51\xce\xbd\xd5\x00\x60\xe0\xec\x11\x7e\x41\xfc\xbb\xf0\x8d\x0f\xe2\x11\x7a\xe1\xe4\x04\xf0\xc8\xf0\x11\xb1\x0b\xd9\x9c\x80\x0e\xe3\x89\x7d\x08\xb9\x90\xc4\x11\x70\xe9\x2c\x1c\x41\x97\x14\xbf\x0b\xde\xb4\x09\x1c\xc1\x27\xfb\x6f\xe0\xfe\x92\xf9\x88\x08\xa6\xa4\x4e\x60\x28\xfb\xa6\xf0\x10\x8a\x29\x9b\x01\x47\x9f\x68\x7f\x5f\x70\x30\xbe\x31\x94\x93\x37\xda\x1b\xb5\x2d\x57\xfd\x43\x2a\x5e\x94\xb0\x5a\xf5\xfa\x7f\x3b\xe3\xb7\xee\x60\x05\xae\x3a\x6f\xb0\x2d\x26\xad\xc3\xe5\x77\xf9\xff\x02\x00\x00\xff\xff\x63\x87\x08\x63\x9e\x12\x00\x00") +var _schemaGo = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xb4\x57\x4b\x6f\xdb\x38\x10\x3e\x4b\xbf\x62\x6a\xa0\x81\x14\x78\xe5\x6e\x6f\xeb\xc2\x87\xa2\x4d\x81\xec\x23\x2d\x9a\xee\x5e\x82\x20\xa5\xc5\xa1\xcd\x46\x22\x55\x92\x76\x92\x06\xf9\xef\x0b\xbe\xf4\xb0\xec\xa0\xdb\x6e\x02\x04\x36\xe7\x45\xce\x37\x1f\x87\xe3\xd9\x0c\xde\xc8\xe6\x4e\xf1\xd5\xda\xc0\xcb\x17\xbf\xfe\xf6\x4b\xa3\x50\xa3\x30\xf0\x8e\x94\xb8\x94\xf2\x1a\x4e\x45\x59\xc0\xeb\xaa\x02\x67\xa4\xc1\xea\xd5\x16\x69\x91\xce\x66\xf0\x69\xcd\x35\x68\xb9\x51\x25\x42\x29\x29\x02\xd7\x50\xf1\x12\x85\x46\x0a\x1b\x41\x51\x81\x59\x23\xbc\x6e\x48\xb9\x46\x78\x59\xbc\x88\x5a\x60\x72\x23\xa8\x0d\xc1\x85\x33\xf9\xf3\xf4\xcd\xc9\xd9\xf9\x09\x30\x5e\x61\x94\x29\x29\x0d\x50\xae\xb0\x34\x52\xdd\x81\x64\x60\x7a\xfb\x19\x85\x58\xa4\x69\x43\xca\x6b\xb2\x42\xa8\x24\xa1\x69\xca\xeb\x46\x2a\x03\x59\x9a\x4c\x50\x94\x92\x72\xb1\x9a\x7d\xd1\x52\x4c\xd2\x64\xc2\x6a\x63\x3f\x14\xb2\x0a\x4b\x33\x49\xd3\x64\xb2\xe2\x66\xbd\x59\x16\xa5\xac\x67\x2c\x24\xcc\x45\xb9\x59\x12\x23\xd5\x0c\x85\x99\xe9\x72\x8d\x35\x99\x21\x5d\xe1\x77\x39\x4c\xfe\x43\x50\xc6\xb1\xa2\x93\x34\x4f\x2d\x0c\xe7\x4e\x06\x0a\x43\x01\x34\x10\x01\x28\x4c\x11\x14\x66\x4d\x0c\xdc\x10\xed\xf2\x44\x0a\x4c\xc9\x1a\x08\x94\xb2\x6e\x2a\x6e\xc1\xd6\xa8\x20\x60\x51\xa4\xe6\xae\xc1\x18\x52\x1b\xb5\x29\x0d\xdc\xa7\xc9\x19\xa9\x11\xe2\x9f\x36\x8a\x8b\x55\xbb\xfc\x6c\x51\x9a\x4f\x04\xa9\x71\x2a\x6b\x6e\xb0\x6e\xcc\xdd\xe4\x73\x9a\x9c\xd0\x15\xea\x68\x75\x71\x79\x6c\xd7\x3b\x4e\x16\x1d\x3d\xf4\x7a\x67\x73\xd3\x9d\x97\x5b\x0f\xbd\x5c\xfa\x3b\x6e\xa7\x82\xe2\x6d\xdc\xee\xe2\xf2\xd8\xad\x87\x6e\xdc\x9b\x0c\xfd\xce\x5d\x8e\x61\xd3\x8b\xcb\xe3\xde\x3a\xfa\x79\x18\xae\xf6\xec\xfa\xe0\x0a\xe0\x8d\xc7\xf8\x7b\xf9\x0f\xc0\xef\x1d\x0f\xa0\xdf\x87\xff\x20\xf4\x9f\x6c\x98\xf6\xcf\x9d\xbc\x70\xb2\xe0\x61\xb7\xd9\xf1\x20\x5d\x45\xf7\xed\x61\xc8\x6a\x07\x38\xfe\xad\xb7\xc5\x31\x17\x66\x88\xb6\xe6\xdf\x76\xb6\xf8\x5b\xf0\xaf\x9b\xd6\x67\x29\x65\x35\xf4\xd8\x38\xfd\xd0\xe7\x8c\x57\x15\x59\x56\x78\xc8\x47\x04\xfd\xd0\xeb\x7d\x63\xb8\x14\xa4\x3a\xe4\x25\x83\x7e\xe8\xf5\x16\x19\xd9\x54\xe6\xe0\xf9\xa8\xd7\xef\x24\xd5\x50\x62\x30\xba\xee\x49\xca\xe9\xaf\xf6\xfa\x9e\xd6\xf5\xc6\xb4\xd9\x8d\x7d\x79\xd4\x0f\xdd\xfe\x21\x15\xa7\xb6\x25\x38\xb6\x77\xc0\x47\xb7\x6d\xab\xdf\x43\xd6\x3e\xbf\x77\x28\x7b\x6b\x50\x59\xd0\x02\xf1\x1c\x6b\x80\x22\xe3\x02\x69\x6c\xac\xbe\x01\xc5\x36\xd1\x8b\xd5\xb1\xb5\x65\x52\x20\xd1\x21\x02\xb5\xac\x1e\xda\x3d\x42\xe6\x9d\x80\x23\x0e\xbf\x91\x75\x6d\x5f\xa0\x1d\xc3\xd2\x8b\x87\xb6\x1f\xae\x57\x1f\x88\x59\xef\xda\x36\xd7\xab\xab\x86\x98\xf5\x4e\x27\xab\x97\x48\xed\xe5\x0d\x35\x8a\xdd\x2b\x88\xf7\xc0\xec\x7a\xdd\xb8\x25\x38\xf1\x0f\x74\x04\xe7\xb7\xa7\x21\xfc\x6f\xd0\x7d\x6f\xd1\x3e\x22\xf3\x9b\x0f\xed\x14\xb2\xab\xf1\xee\x1f\x91\x85\xee\xe0\x5b\x7f\x67\x7c\xa0\x31\x0c\xe1\xdd\xd7\x0f\x4e\xc5\x16\x95\xc6\x5d\x53\xee\xc5\xbb\xdb\x7f\xdd\x70\x35\xaa\x9a\x0a\xe2\x3d\x55\xf3\x8f\xc6\xb8\x6c\x5e\xfe\x03\x75\xf3\x8e\x5d\xe1\x42\xa6\xed\x55\x7f\x24\xd3\xf0\x7a\x5e\x5c\x0e\x91\x3e\xfc\x62\xee\x5a\x1e\x7c\xaf\xce\xf0\xc6\xd5\xa3\x54\x48\x0c\xba\x24\x43\x46\x36\xb8\x4f\xcb\x7d\xa3\xa8\x4b\xc5\x1b\x23\x55\x91\xb2\x8d\x28\xa3\x67\x86\x14\x8e\xad\x45\xf1\xb6\xb5\xc8\x43\x91\xef\xd3\x44\x20\xcc\x17\x70\x64\x97\xf7\x69\x62\xa9\x35\xf7\x34\x40\x5a\x7c\x22\xab\xa9\x95\xdd\x35\x38\x6f\x65\x96\x8d\x69\xe2\x58\xdd\x0a\xed\xc2\x0a\x3d\x62\x73\x2f\xf4\x0b\x2b\x0e\x3c\x98\x3b\x71\x58\x58\x79\xac\xf9\xdc\xca\xe3\xc2\x2b\x58\x88\xef\x14\x2c\xc4\x7f\x48\x13\xce\x40\x21\xb3\x47\xf6\x9a\x57\x6e\xf9\x6c\x01\x82\x57\x36\x9d\x44\xa0\x15\xc3\xa2\x4d\x5f\x21\xcb\x9d\xab\x42\xb3\x51\x02\x04\x06\x64\xff\x22\x4a\xaf\x49\x15\xc6\x27\x37\x46\xa2\x6b\x9b\xbd\x71\x8c\x0b\x83\xca\x4e\x77\xf6\x9b\x04\x02\xbf\x9f\xbf\x3f\xb3\xce\x8e\x5e\x25\x11\xb0\xb4\xc8\x5b\x57\xea\x4d\x6c\x80\xe0\x2c\x97\x5f\xb0\x34\xe1\x23\x14\x65\xb0\x69\xa6\xe3\xde\x96\xb5\x61\xa7\x1c\xb2\x25\x5c\x5c\x2e\xef\x0c\x4e\x01\x95\xb2\xff\xb6\x62\xf7\x69\xa2\x5d\xa9\xbc\xef\xbd\x07\x88\x0b\x3f\x38\x67\x61\xdc\x75\xf5\x79\xcf\x42\xe4\x3c\x77\xa5\xc9\xf2\x87\x34\x09\x0c\x73\x21\xe7\x0b\xd0\x84\xa1\xe7\x62\xb4\x75\xe0\x5a\x6d\x0f\xcd\x88\x19\xaf\xa6\xc0\x6a\x53\x9c\xd8\xb3\xb0\x6c\x12\x0e\xfe\xfc\xeb\x1c\x9e\x6f\x27\x53\xd0\x9e\x02\xd6\xdd\x83\xcd\xa4\x82\xab\x29\xb8\x4a\x29\x22\x2c\x53\x3d\xf1\x6d\x54\x46\xad\x98\xf5\x08\x99\xe5\x69\x92\x68\x67\x7d\xe4\x4e\x65\xcd\x7a\x1c\xf3\x93\x51\x47\xb4\x1e\x27\xa3\x2a\x12\xb3\x47\xe1\x56\xe5\x79\xdc\xa3\x67\xd4\x74\x1c\x6d\x87\x97\x79\xb7\x59\x1c\x57\xac\x3a\x4e\x29\x9d\x3a\x4a\x9c\xba\x9d\x0e\xe6\x51\xdd\x4a\x9c\xbe\x1b\x03\x9c\x41\x85\x22\x63\xb4\xe8\xa4\xb9\xb3\x0a\x93\xc9\xbc\x3b\x60\x9c\x55\x7c\x4d\x7c\x16\xfd\x21\x66\xee\xb2\x18\x8c\x35\x9d\xe9\x43\x9a\xd8\x9a\x32\x5a\xb8\xf9\xef\xd9\x02\x5e\x38\xfc\x13\xcd\xbc\x64\x01\x47\x41\x19\xac\x75\x11\xfa\xd3\x02\x48\xd3\xa0\xa0\x59\x94\x4c\x41\x87\x7b\xe4\x9b\x5a\x9f\x47\xae\xfb\x3d\x25\x8d\xb0\xa3\x91\xdb\xdd\x05\xd5\x85\xef\xba\xbd\xa3\x9e\xf8\xa3\xb5\x9d\x6f\xc0\xb0\xdc\x87\x8c\x3f\x2c\xfa\x09\x84\xdf\x23\x4f\x99\x02\xa7\xb7\x5d\x12\xe1\x0c\x2e\x70\x50\x70\x7a\x3b\xba\x0f\x45\xfc\x9d\xd4\x4b\xf1\x34\x1e\xff\xc8\x7d\x73\xe5\x74\x69\xcf\xc1\xc5\xf0\x10\x58\xa9\xaf\xdb\xdc\x49\x43\x0d\xfb\x97\xc0\x8a\x3b\xfa\x3f\x0c\x7a\xa4\x7d\x93\x8a\xd0\xaa\x32\x9d\x87\x86\xd9\xb5\x0c\xb8\x51\xa4\xd1\xfd\x19\x33\xc8\x6b\x34\x6b\x49\xe1\x86\x9b\x35\x28\x2c\xe5\x16\x15\x18\x09\x28\xf4\x46\x21\x08\x09\x0d\x11\xbc\xd4\x76\x40\xad\x7d\x78\x2e\x56\xa1\x35\x8e\x3a\xd2\xa8\x2f\xb2\xf8\x76\xb6\x3f\xd8\x76\x3b\x24\x45\x86\x0a\x6c\xb8\x2c\xf7\xe8\x32\xd8\x3a\xdc\xfd\x61\xb2\xfc\x15\x6c\xfb\x65\x4d\xac\xff\x62\x4f\x45\x63\x46\xfe\xc0\xa1\xb8\x5b\x5b\x96\xd0\x49\xc1\x05\xf1\xf7\xe6\xc1\xd6\x2b\x60\x37\x70\xcf\xf2\xa9\xb3\xea\x00\xf4\x9c\x1d\xe1\xe7\xc5\x3f\x0b\x5f\xff\x22\x8e\xd0\xf3\x37\xc7\x83\x67\x0d\x9f\x10\x3b\x9f\xcd\x1e\xe8\x30\xdc\xd8\xc7\x90\xf3\x49\x8c\x80\x8b\x77\x61\x04\x5d\x54\xfc\x2c\x78\xc3\x26\x30\x82\x2f\xde\x59\x0f\xa0\x33\x7e\x42\x04\x63\x52\x7b\x30\xe4\x6d\x53\x78\x0c\xc5\x98\x4d\x87\xa3\x4b\xb4\x9d\x17\x0c\xf4\x27\x86\x7c\xb0\xb2\x67\xb3\x6d\xcb\x14\x7f\x70\x41\xb3\x1c\x16\x8b\x56\xff\xc1\x28\x77\x74\x03\x0b\x30\xc5\x49\x85\x75\x36\x68\x1d\x26\x7d\x48\xff\x0d\x00\x00\xff\xff\xb1\x82\x4a\x61\x6f\x14\x00\x00") func schemaGoBytes() ([]byte, error) { return bindataRead( @@ -102,7 +102,7 @@ func schemaGo() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "schema.go", size: 4766, mode: os.FileMode(420), modTime: time.Unix(1567935214, 0)} + info := bindataFileInfo{name: "schema.go", size: 5231, mode: os.FileMode(420), modTime: time.Unix(1567952289, 0)} a := &asset{bytes: bytes, info: info} return a, nil } diff --git a/entc/load/load.go b/entc/load/load.go index f139b3a93..87808ad60 100644 --- a/entc/load/load.go +++ b/entc/load/load.go @@ -47,6 +47,8 @@ type Config struct { // Names are the schema names to run the code generation on. // Empty means all schemas in the directory. Names []string + // schema types and their exported struct fields. + fields map[string][]*StructField } // Build loads the schemas package and build the Go plugin with this info. @@ -85,31 +87,43 @@ func (c *Config) Load() (*SchemaSpec, error) { if err := json.Unmarshal([]byte(line), schema); err != nil { return nil, errors.WithMessagef(err, "unmarshal schema %s", line) } + schema.StructFields = c.fields[schema.Name] spec.Schemas = append(spec.Schemas, schema) } return spec, nil } +// entInterface represents the the ent.Interface type. +var entInterface = reflect.TypeOf(struct{ ent.Interface }{}).Field(0).Type + // load loads the schemas info. func (c *Config) load() (string, error) { - // get the ent package info statically instead of dealing with string constants - // in the code, since import is handled by goimports and renaming should be easy. - entface := reflect.TypeOf(struct{ ent.Interface }{}).Field(0).Type - pkgs, err := packages.Load(&packages.Config{Mode: packages.LoadSyntax}, c.Path, entface.PkgPath()) + pkgs, err := packages.Load(&packages.Config{Mode: packages.LoadSyntax}, c.Path, entInterface.PkgPath()) if err != nil { return "", err } entPkg, pkg := pkgs[0], pkgs[1] - if pkgs[0].PkgPath != entface.PkgPath() { + if pkgs[0].PkgPath != entInterface.PkgPath() { entPkg, pkg = pkgs[1], pkgs[0] } names := make([]string, 0) - iface := entPkg.Types.Scope().Lookup(entface.Name()).Type().Underlying().(*types.Interface) + iface := entPkg.Types.Scope().Lookup(entInterface.Name()).Type().Underlying().(*types.Interface) for k, v := range pkg.TypesInfo.Defs { typ, ok := v.(*types.TypeName) if !ok || !k.IsExported() || !types.Implements(typ.Type(), iface) { continue } + spec, ok := k.Obj.Decl.(*ast.TypeSpec) + if !ok { + return "", fmt.Errorf("invalid declaration %T for %s", k.Obj.Decl, k.Name) + } + specType, ok := spec.Type.(*ast.StructType) + if !ok { + return "", fmt.Errorf("invalid spec type %T for %s", spec.Type, k.Name) + } + if err := c.structFields(k.Name, v, specType); err != nil { + return "", err + } names = append(names, k.Name) } if len(c.Names) == 0 { @@ -119,6 +133,62 @@ func (c *Config) load() (string, error) { return pkg.PkgPath, err } +// structFields loads schema type fields if exist. +func (c *Config) structFields(name string, obj types.Object, spec *ast.StructType) (err error) { + typ, ok := obj.(*types.TypeName) + if !ok { + return + } + st, ok := typ.Type().Underlying().(*types.Struct) + if !ok { + return + } + if c.fields == nil { + c.fields = make(map[string][]*StructField) + } + for i := 0; i < st.NumFields(); i++ { + f := st.Field(i) + // skip non-exported fields, because they + // cannot be used outside the package. + if !f.Exported() { + continue + } + sf := &StructField{ + Tag: st.Tag(i), + Name: f.Name(), + Type: f.Type().String(), + Embedded: f.Embedded(), + Comment: strings.TrimSpace(spec.Fields.List[i].Comment.Text()), + } + switch typ := indirectType(f.Type()).(type) { + case *types.Named: + sf.PkgPath = typ.Obj().Pkg().Path() + // skip fields used for schema definition. + if sf.PkgPath == entInterface.PkgPath() { + continue + } + c.fields[name] = append(c.fields[name], sf) + default: + if f.Embedded() { + return fmt.Errorf("field %s for schema %q cannot be embbeded", f.Type(), name) + } + c.fields[name] = append(c.fields[name], sf) + } + } + return +} + +// indirectType returns the type at the end of indirection. +func indirectType(typ types.Type) types.Type { + for { + ptr, ok := typ.(*types.Pointer) + if !ok { + return typ + } + typ = ptr.Elem() + } +} + //go:generate go-bindata -pkg=load ./template/... schema.go var buildTmpl = templates() diff --git a/entc/load/load_test.go b/entc/load/load_test.go index f459b2daa..8598500a6 100644 --- a/entc/load/load_test.go +++ b/entc/load/load_test.go @@ -18,6 +18,16 @@ func TestLoad(t *testing.T) { require.NoError(t, err) require.Len(t, spec.Schemas, 3) require.Equal(t, "github.com/facebookincubator/ent/entc/load/testdata/valid", spec.PkgPath) + + require.Equal(t, "Group", spec.Schemas[0].Name, "ordered alphabetically") + require.Equal(t, "Tag", spec.Schemas[1].Name) + require.Equal(t, "User", spec.Schemas[2].Name) + + require.Len(t, spec.Schemas[2].StructFields, 3) + fields := spec.Schemas[2].StructFields + require.Equal(t, &StructField{Tag: "json:\"tenant,omitempty\"", Name: "Tenant", Type: "string"}, fields[0]) + require.Equal(t, &StructField{Name: "Logger", Type: "*log.Logger", PkgPath: "log", Comment: "Comment."}, fields[1]) + require.Equal(t, &StructField{Name: "Mutex", Type: "sync.Mutex", PkgPath: "sync", Embedded: true}, fields[2]) } func TestLoadWrongPath(t *testing.T) { diff --git a/entc/load/schema.go b/entc/load/schema.go index 497c14ab6..dd93c5a38 100644 --- a/entc/load/schema.go +++ b/entc/load/schema.go @@ -17,10 +17,11 @@ import ( // Schema represents an ent.Schema that was loaded from a complied user package. type Schema struct { - Name string `json:"name,omitempty"` - Edges []*Edge `json:"edges,omitempty"` - Fields []*Field `json:"fields,omitempty"` - Indexes []*Index `json:"indexes,omitempty"` + Name string `json:"name,omitempty"` + Edges []*Edge `json:"edges,omitempty"` + Fields []*Field `json:"fields,omitempty"` + Indexes []*Index `json:"indexes,omitempty"` + StructFields []*StructField `json:"struct_fields,omitempty"` } // Field represents an ent.Field that was loaded from a complied user package. @@ -38,6 +39,16 @@ type Field struct { Validators int `json:"validators,omitempty"` } +// StructField represents an external struct field defined in the schema. +type StructField struct { + Tag string `json:"tag,omitempty"` + Name string `json:"name,omitempty"` + Type string `json:"type,omitempty"` + Comment string `json:"comment,omitempty"` + PkgPath string `json:"pkg_path,omitempty"` + Embedded bool `json:"embedded,omitempty"` +} + // Edge represents an ent.Edge that was loaded from a complied user package. type Edge struct { Name string `json:"name,omitempty"` diff --git a/entc/load/testdata/valid/schema.go b/entc/load/testdata/valid/schema.go index a66d3b3b9..5c5c832a2 100644 --- a/entc/load/testdata/valid/schema.go +++ b/entc/load/testdata/valid/schema.go @@ -5,6 +5,9 @@ package valid import ( + "log" + "sync" + "github.com/facebookincubator/ent" "github.com/facebookincubator/ent/schema/field" ) @@ -12,6 +15,13 @@ import ( // User holds the user schema. type User struct { ent.Schema + + // add additional struct-only + // fields to the generated model. + Tenant string `json:"tenant,omitempty"` + Logger *log.Logger // Comment. + sync.Mutex + Ignored ent.Interface // Ignored comment. } func (User) Fields() []ent.Field {