From 37de2bd3f64e9ae3ae326a31610b92bbf3dc93a1 Mon Sep 17 00:00:00 2001 From: Ariel Mashraki <7413593+a8m@users.noreply.github.com> Date: Tue, 22 Jun 2021 23:26:32 +0300 Subject: [PATCH] entc/gen: add support for user-defined ids in gremlin (#1651) --- dialect/gremlin/graph/dsl/dsl.go | 11 ++++++++++ entc/gen/internal/bindata.go | 4 ++-- entc/gen/template/dialect/gremlin/create.tmpl | 5 +++++ entc/integration/ent/client.go | 8 +++---- entc/integration/ent/entql.go | 6 +++--- entc/integration/ent/item.go | 12 +++++------ entc/integration/ent/item/item.go | 5 +++++ entc/integration/ent/item/where.go | 18 ++++++++-------- entc/integration/ent/item_create.go | 21 ++++++++++++++----- entc/integration/ent/item_delete.go | 2 +- entc/integration/ent/item_query.go | 20 +++++++++--------- entc/integration/ent/item_update.go | 4 ++-- entc/integration/ent/migrate/schema.go | 2 +- entc/integration/ent/mutation.go | 12 ++++++++--- entc/integration/ent/runtime.go | 7 +++++++ entc/integration/ent/schema/item.go | 6 +++++- entc/integration/gremlin/ent/item/item.go | 5 +++++ entc/integration/gremlin/ent/item_create.go | 14 +++++++++++++ entc/integration/gremlin/ent/mutation.go | 6 ++++++ entc/integration/gremlin/ent/runtime.go | 7 +++++++ entc/integration/gremlin/gremlin_test.go | 4 ++++ 21 files changed, 132 insertions(+), 47 deletions(-) diff --git a/dialect/gremlin/graph/dsl/dsl.go b/dialect/gremlin/graph/dsl/dsl.go index 6dd51cfe3..0c0901d65 100644 --- a/dialect/gremlin/graph/dsl/dsl.go +++ b/dialect/gremlin/graph/dsl/dsl.go @@ -122,6 +122,17 @@ const ( // Code implements the Node interface. func (c Cardinality) Code() (string, []interface{}) { return string(c), nil } +// Keyword defines a Gremlin keyword. +type Keyword string + +// Keyword options. +const ( + ID Keyword = "id" +) + +// Code implements the Node interface. +func (k Keyword) Code() (string, []interface{}) { return string(k), nil } + // Order of vertex properties. type Order string diff --git a/entc/gen/internal/bindata.go b/entc/gen/internal/bindata.go index 791e631e8..214baedc6 100644 --- a/entc/gen/internal/bindata.go +++ b/entc/gen/internal/bindata.go @@ -372,7 +372,7 @@ func templateDialectGremlinByTmpl() (*asset, error) { return a, nil } -var _templateDialectGremlinCreateTmpl = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xa4\x56\x4d\x6f\xdb\x38\x13\x3e\x4b\xbf\x62\x5e\xc3\x2d\xa4\x40\xa5\xdd\xde\x5e\x2f\x7c\xc8\xba\xee\xd6\x40\x9b\x7e\x24\x9b\x4b\x51\x14\x0c\x39\x92\x89\xc8\xa4\x4a\x52\x6e\x0c\x43\xff\x7d\x31\x94\x6c\xcb\x8e\xb3\xbb\xdd\x1e\x82\x48\x9c\xaf\x67\x9e\x79\x86\xf2\x76\x3b\xba\x88\x67\xa6\xda\x58\x55\x2c\x3d\xbc\x1a\xbf\xfc\xff\x8b\xca\xa2\x43\xed\xe1\x0d\x17\x78\x67\xcc\x3d\x2c\xb4\x60\x70\x59\x96\x10\x9c\x1c\x90\xdd\xae\x51\xb2\xf8\x66\xa9\x1c\x38\x53\x5b\x81\x20\x8c\x44\x50\x0e\x4a\x25\x50\x3b\x94\x50\x6b\x89\x16\xfc\x12\xe1\xb2\xe2\x62\x89\xf0\x8a\x8d\x77\x56\xc8\x4d\xad\x65\xac\x74\xb0\xbf\x5b\xcc\xe6\x57\xd7\x73\xc8\x55\x89\xd0\x9d\x59\x63\x3c\x48\x65\x51\x78\x63\x37\x60\x72\xf0\xbd\x62\xde\x22\xb2\xf8\x62\xd4\x34\x71\x4c\x3d\x40\x61\xfc\xa6\xc2\x09\xa0\xf6\x85\x61\xca\x8c\x50\x7b\xfa\x13\xa3\x02\x35\x23\xdb\xb5\x30\x15\xc2\x2e\x04\x24\xe6\x4a\x23\x0c\xa4\xe2\x25\x0a\x3f\x2a\x2c\xae\x4a\xa5\x47\xc2\x22\xf7\x38\x80\xa6\x21\xaf\xe1\x5d\xad\x4a\x6a\x63\x32\x85\x8a\x3b\xc1\x4b\x18\xb2\x90\x89\xfd\xde\x59\x3a\x47\x8b\x02\xd5\xba\xf5\xdc\x3f\xef\xc3\x3b\xa7\x55\xed\xb9\x57\x46\x87\x74\x56\x69\xdf\x8b\x1b\xb0\x9d\x75\x00\xe4\x1f\xe7\xb5\x16\x90\x1c\xe5\x6e\x1a\xb8\xe8\xa3\x6a\x9a\x14\x3a\xe0\xd7\x7c\x8d\x89\xf0\x0f\x20\x8c\xf6\xf8\xe0\xd9\xac\xfd\x9f\x42\x12\x42\xd8\x15\x5f\x21\x34\x4d\x06\x68\xad\xb1\x29\x6c\xe3\xc8\xa2\x23\x24\xcf\xbb\x14\xec\x33\xba\xca\x68\x87\xdb\x26\x8e\xbe\xd7\x68\x37\x19\xdc\x29\x2d\x95\x2e\x82\xdf\x09\x14\xd6\x85\x25\x29\xfb\x44\xce\x49\x1a\x47\x2a\xa7\xf4\xe7\x9c\xa5\xa5\x27\x36\x7f\x40\x41\x30\x33\x38\x29\x90\x91\xac\xd2\xdf\x42\xf8\xff\xa6\xa0\x55\x49\x08\x23\x8b\xbe\xb6\x9a\x5e\x03\xf0\x38\x6a\x76\x45\x32\x30\xf7\x54\x48\xb9\x99\xd1\xce\x73\xed\xe7\xd4\x58\xd2\xa6\x31\xf7\x4f\x86\x07\x36\x3e\xf7\x28\x25\x0a\xfa\x14\x6d\x85\xd1\xb9\x2a\x26\x8f\x7a\x68\xcf\x9b\xd3\x36\xfb\xc9\xd8\x1b\x6b\x56\x3b\x1e\x93\x7f\xdd\x52\x77\x76\x9a\x2d\x23\xaf\xf8\xa7\xb5\x90\xa4\x70\x21\x5d\xc9\x6e\x2c\x5f\xa3\x75\x3c\xd4\xdd\x6e\x5f\xc0\x0f\xe5\x97\xc0\xae\xea\x55\xa0\xcc\x72\x52\x60\xd3\xc4\x51\x44\x0b\x42\xca\xd9\x1d\x3a\x6f\x6b\xe1\x03\xdc\xa8\xb2\x28\x4f\xf3\x8d\x46\x7d\x6f\xf2\x50\x82\x7b\x64\xe4\xef\xd1\xf9\x33\xfe\xe1\x78\xc5\xbd\x58\xa2\x03\xae\x25\x28\xef\xda\x24\x5c\x7b\x0a\x24\x1c\x87\xa4\x41\x71\x2b\x7e\x8f\xc9\x97\xaf\x17\x87\xe3\x0c\xc6\x19\xd1\xc4\xa8\xdf\xb6\x29\xd4\x32\x34\xb1\xa6\x88\x82\x5d\x4a\x79\x1b\x98\x62\x1f\xb9\xb8\xe7\x05\x4d\x94\xbd\xe3\x77\x58\x76\xfe\x96\xeb\x02\x61\x98\x93\xfb\x90\xbd\xef\x96\xee\x8d\xc2\x52\xba\x96\x0d\x95\xc3\x9a\x97\x35\xee\x24\x76\xb4\xba\x4d\xc3\xe8\x3d\xdf\x47\xfe\x81\x44\x62\x72\x10\x5d\xa8\xa2\x72\xf2\xf9\x53\xab\xef\x35\xb6\x59\x8f\xbb\x9b\x02\xaf\x2a\xd4\x32\xe9\x1d\x66\xf0\xfc\xf0\x16\x32\xb5\xec\x4f\xa0\x60\xb7\x49\xca\xde\x72\x77\xbe\xb3\x0c\x4e\x8f\x5b\x88\xbb\xcd\x08\x52\x0a\x2d\xa5\x6c\x66\x6a\xed\x93\x34\x6b\xd3\xd3\x54\x26\xf0\xed\x1b\x5b\xb8\xa4\x62\x57\xf3\x4f\xc9\x38\x4d\xf7\x71\xc9\x15\xfe\x98\x5b\xdb\x76\x11\x18\xfa\xe5\xfa\x5d\x61\x1a\x5e\x74\x34\xbe\x28\x5a\xb3\x8f\xd6\x54\x68\xfd\x26\x21\xf5\x5c\x2b\x5d\x94\xf8\x13\xa9\x5b\x0d\xf5\x73\xf6\xc6\x8d\xed\xb8\xe7\xb2\xc0\x6e\xca\x64\x1c\xb6\x5f\x98\xee\x4a\x1e\x2c\xf4\xa0\x67\xd3\x74\x1d\xec\x6e\xea\x1c\x06\xcf\x1c\x7b\xe6\x06\x3d\x30\x43\x6c\xdb\xef\x61\x89\xa3\x28\x37\x16\xbe\x65\xa0\x64\xf8\x14\x84\xea\xe7\x04\x84\xec\x3a\x2c\x59\xa0\x15\x9a\x66\xf1\xda\x25\xe9\xb1\x7e\x90\x2d\xdc\x42\xd3\x0a\xed\x25\x74\x02\x7a\x0a\x83\x0f\xb5\x1f\x1c\x59\x03\xec\xc7\xa8\x91\xdd\x6c\x2a\xfc\x07\xec\x34\x84\x4b\x29\xe7\x61\xcc\xba\xbd\x0e\xd3\x70\xa7\x25\x24\x41\x25\xd3\x94\x2d\xf4\x6d\x72\x98\x5e\x79\xc0\x76\x2e\xf4\xc6\x1c\x02\x3f\xd4\xbe\x1f\xb9\x9f\xfb\xa1\xdd\xb7\xdc\x9d\xde\x4c\xbf\xb6\x35\xf3\x76\x6b\x42\xa7\xc7\xc0\xe8\xe5\xc0\x63\xd3\x74\xfb\xb5\x78\x4d\x58\xff\xfb\x92\x90\xbe\xfe\x6e\x47\xf4\xee\x2b\x4c\x8c\x3c\xb9\x0a\x67\x64\xfc\xe4\xd5\xad\x72\x28\x51\xf7\x09\x49\x61\x3a\x85\x71\x2b\xa5\xee\xc3\xb2\x66\xb7\xb4\x22\xef\x79\x95\x78\xbb\x5f\x95\xc8\x87\x6f\x58\x2f\xf4\xcb\xf8\x2b\x23\xee\xd8\xcc\xf0\x12\x9d\xc0\xe4\xc4\x48\x54\x64\x8f\xd2\xa5\x07\xd9\x0b\x7b\x90\x7d\x3f\xf6\xe5\xe4\x6b\x8b\xc8\x5b\x98\x82\xb0\xa7\x65\x6c\x97\xda\xdb\x1d\xb8\x0e\xba\xb7\xf1\x89\xd2\x9e\xec\xa9\xc7\x59\xf8\xbd\xd5\x3d\xff\x15\x00\x00\xff\xff\x44\xdd\x7c\x50\xe5\x0a\x00\x00") +var _templateDialectGremlinCreateTmpl = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xa4\x56\x4d\x6f\xdb\x38\x13\x3e\x4b\xbf\x62\x5e\xc3\x2d\xa4\x40\xa5\xd3\xde\xde\x2c\x7c\xc8\x26\xee\x56\x40\x9b\x7e\x24\xcd\xa5\x28\x0a\x86\x1c\x29\x44\x64\x52\x25\x29\xb7\x81\xa1\xff\xbe\x18\x4a\xb2\x65\xc7\xc9\x6e\xb7\x87\x20\x12\xe7\xeb\x99\x67\x9e\x11\xbd\x5e\xcf\x8e\xe2\x33\x53\xdf\x5b\x55\xde\x7a\x78\x75\xfc\xf2\xff\x2f\x6a\x8b\x0e\xb5\x87\xd7\x5c\xe0\x8d\x31\x77\x90\x6b\xc1\xe0\xb4\xaa\x20\x38\x39\x20\xbb\x5d\xa1\x64\xf1\xd5\xad\x72\xe0\x4c\x63\x05\x82\x30\x12\x41\x39\xa8\x94\x40\xed\x50\x42\xa3\x25\x5a\xf0\xb7\x08\xa7\x35\x17\xb7\x08\xaf\xd8\xf1\x60\x85\xc2\x34\x5a\xc6\x4a\x07\xfb\xdb\xfc\x6c\x71\x71\xb9\x80\x42\x55\x08\xfd\x99\x35\xc6\x83\x54\x16\x85\x37\xf6\x1e\x4c\x01\x7e\x54\xcc\x5b\x44\x16\x1f\xcd\xda\x36\x8e\xa9\x07\x28\x8d\xbf\xaf\xf1\x04\x50\xfb\xd2\x30\x65\x66\xa8\x3d\xfd\x89\x59\x89\x9a\x91\xed\x52\x98\x1a\x61\x08\x01\x89\x85\xd2\x08\x13\xa9\x78\x85\xc2\xcf\x4a\x8b\xcb\x4a\xe9\x99\xb0\xc8\x3d\x4e\xa0\x6d\xc9\x6b\x7a\xd3\xa8\x8a\xda\x38\x99\x43\xcd\x9d\xe0\x15\x4c\x59\xc8\xc4\xfe\xec\x2d\xbd\xa3\x45\x81\x6a\xd5\x79\x6e\x9e\x37\xe1\xbd\xd3\xb2\xf1\xdc\x2b\xa3\x43\x3a\xab\xb4\x1f\xc5\x4d\xd8\x60\x9d\x00\xf9\xc7\x45\xa3\x05\x24\x3b\xb9\xdb\x16\x8e\xc6\xa8\xda\x36\x85\x1e\xf8\x25\x5f\x61\x22\xfc\x4f\x10\x46\x7b\xfc\xe9\xd9\x59\xf7\x3f\x85\x24\x84\xb0\x0b\xbe\x44\x68\xdb\x0c\xd0\x5a\x63\x53\x58\xc7\x91\x45\x47\x48\x9e\xf7\x29\xd8\x27\x74\xb5\xd1\x0e\xd7\x6d\x1c\x7d\x6f\xd0\xde\x67\x70\xa3\xb4\x54\xba\x0c\x7e\x7b\x50\x58\x1f\x96\xa4\xec\x23\x39\x27\x69\x1c\xa9\x82\xd2\x1f\x72\x96\x96\x9e\xd8\xe2\x27\x0a\x82\x99\xc1\x5e\x81\x8c\x64\x95\xfe\x11\xc2\xff\x37\x07\xad\x2a\x42\x18\x59\xf4\x8d\xd5\xf4\x1a\x80\xc7\x51\x3b\x14\xc9\xc0\xdc\x51\x21\xe5\xce\x8c\x76\x9e\x6b\xbf\xa0\xc6\x92\x2e\x8d\xb9\x7b\x34\x3c\xb0\xf1\x69\x44\x29\x51\x30\xa6\x68\x2d\x8c\x2e\x54\x79\xf2\xa0\x87\xee\xbc\xdd\x6f\x73\x9c\x8c\xbd\xb6\x66\x39\xf0\x98\xfc\xeb\x96\xfa\xb3\xfd\x6c\x19\x79\xc5\xbf\xac\x85\x24\x85\x23\xe9\x2a\x76\x65\xf9\x0a\xad\xe3\xa1\xee\x7a\xfd\x02\x7e\x28\x7f\x0b\xec\xa2\x59\x06\xca\x2c\x27\x05\xb6\x6d\x1c\x45\xb4\x20\xa4\x9c\xe1\xd0\x79\xdb\x08\x1f\xe0\x46\xb5\x45\xb9\x9f\x6f\x36\x1b\x7b\x93\x87\x12\xdc\x23\x23\x7f\x8f\xce\x1f\xf0\x0f\xc7\x4b\xee\xc5\x2d\x3a\xe0\x5a\x82\xf2\xae\x4b\xc2\xb5\xa7\x40\xc2\xb1\x4d\x1a\x14\xb7\xe4\x77\x98\x7c\xf9\x7a\xb4\x3d\xce\xe0\x38\x23\x9a\x18\xf5\xdb\x35\x85\x5a\x86\x26\x56\x14\x51\xb2\x53\x29\xaf\x03\x53\xec\x03\x17\x77\xbc\xa4\x89\xb2\xb7\xfc\x06\xab\xde\x5f\x15\x30\x65\xf9\x39\xfb\xec\xd0\x9e\x87\xaf\x40\x17\x4f\x53\x55\x72\x90\xd5\xce\xba\xb6\x2d\x0b\x19\xf3\x73\xf6\xae\x3f\xfb\x0b\x89\xba\x64\x2b\xb5\x68\xc5\x3e\x58\x53\xa3\xf5\xf7\x09\x35\x9f\x9f\x67\xa0\x64\xda\x35\x36\xc6\x49\xcf\x96\xeb\x12\x61\x5a\x50\xa5\xe9\x26\xe7\x6b\x85\x95\x74\x1b\x30\x2b\x5e\x35\xf8\x14\x9e\xe2\x09\x34\x43\xa7\x05\xfb\xac\xd5\xf7\x06\xbb\xac\xbb\x0c\xcf\x81\xd7\x35\x6a\x99\x8c\x0e\x33\x78\xbe\x7d\x0b\x99\x3a\x05\x9c\x40\xc9\xae\x93\x94\xbd\xe1\xee\x30\xbb\x19\xec\x1f\x77\x10\x87\xed\x0c\x72\x0e\x2d\xa5\xec\xcc\x34\xda\x27\x69\xd6\xa5\x27\x65\x9c\xc0\xb7\x6f\x2c\x77\x49\xcd\x2e\x16\x1f\x93\xe3\x34\xdd\xc4\x25\x17\xf8\x63\x61\x6d\xd7\x45\x60\xe8\xb7\xeb\xf7\x85\x49\x40\xd1\xce\x68\x1e\x0c\xf1\x52\xe9\xb2\xc2\x5f\x48\xfd\xe4\xb8\xb1\x1b\xf7\x42\x96\xd8\x4f\x99\x8c\xd3\xee\x96\xeb\xaf\x85\x49\xae\x27\x23\x9b\xa6\x4f\xd2\x70\x5b\x14\x30\x79\xe6\xd8\x33\x37\x19\x81\x99\x62\xd7\xfe\x08\x4b\x1c\x45\x85\xb1\xf0\x8d\xf4\x17\xae\xa3\x50\xfd\x90\x80\x90\x5d\x86\x45\x0f\xb4\x42\xdb\xe6\xe7\x2e\x49\x77\xf5\x83\x2c\x77\xb9\xa6\x35\xde\x48\x68\x0f\xf4\x1c\x26\xef\x1b\x3f\xd9\xb1\x06\xd8\x0f\x51\x23\xbb\xba\xaf\xf1\x1f\xb0\xd3\x10\x4e\xa5\x5c\x84\x31\xeb\xee\x93\x9c\x86\xef\x6a\x42\x12\x54\x32\x4d\x59\xae\xaf\x93\xed\xf4\xaa\x2d\xb6\x43\xa1\x57\x66\x1b\xf8\xbe\xf1\xe3\xc8\xcd\xdc\xb7\xed\xbe\xe1\x6e\xff\xeb\xf8\x7b\x5b\xb3\xe8\xb6\x26\x74\xba\x0b\x8c\x5e\xb6\x3c\xb6\x6d\xbf\x5f\xf9\x39\x61\xfd\xef\x4b\x42\xfa\x7a\x6a\x47\xf4\xf0\x4b\x80\x18\x79\x74\x15\x0e\xc8\xf8\xd1\xeb\x43\x15\x50\xa1\x1e\x13\x92\xc2\x7c\x0e\xc7\x9d\x94\xfa\xcb\x6d\xc5\xae\x69\x45\xde\xf1\x3a\xf1\x76\xb3\x2a\x91\x0f\xf7\xe8\x28\xf4\xcb\xf1\x57\x46\xdc\xb1\x33\xc3\x2b\x74\x02\x93\x3d\x23\x51\x91\x3d\x48\x97\x6e\x65\x2f\xec\x56\xf6\xe3\xd8\x97\x27\x5f\x3b\x44\xde\xc2\x1c\x84\xdd\x2f\x63\xfb\xd4\xde\x0e\xe0\x7a\xe8\xde\xc6\x7b\x4a\x7b\xb4\xa7\x11\x67\xe1\x37\x5f\xff\xfc\x77\x00\x00\x00\xff\xff\x7c\x04\xb0\x35\x69\x0b\x00\x00") func templateDialectGremlinCreateTmplBytes() ([]byte, error) { return bindataRead( @@ -387,7 +387,7 @@ func templateDialectGremlinCreateTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "template/dialect/gremlin/create.tmpl", size: 2789, mode: os.FileMode(420), modTime: time.Unix(1, 0)} + info := bindataFileInfo{name: "template/dialect/gremlin/create.tmpl", size: 2921, mode: os.FileMode(420), modTime: time.Unix(1, 0)} a := &asset{bytes: bytes, info: info} return a, nil } diff --git a/entc/gen/template/dialect/gremlin/create.tmpl b/entc/gen/template/dialect/gremlin/create.tmpl index 79cb29e24..83bb3fe54 100644 --- a/entc/gen/template/dialect/gremlin/create.tmpl +++ b/entc/gen/template/dialect/gremlin/create.tmpl @@ -36,6 +36,11 @@ func ({{ $receiver }} *{{ $builder }}) gremlin() *dsl.Traversal { constraints := make([]*constraint, 0, {{ . }}) {{- end }} v := g.AddV({{ $.Package }}.Label) + {{- if $.ID.UserDefined }} + if id, ok := {{ $mutation }}.{{ $.ID.MutationGet }}(); ok { + v.Property(dsl.ID, id) + } + {{- end }} {{- range $f := $.MutationFields }} if value, ok := {{ $mutation }}.{{ $f.MutationGet }}(); ok { {{- if $f.Unique }} diff --git a/entc/integration/ent/client.go b/entc/integration/ent/client.go index 0799ecdce..9ed61c3d3 100644 --- a/entc/integration/ent/client.go +++ b/entc/integration/ent/client.go @@ -1154,7 +1154,7 @@ func (c *ItemClient) UpdateOne(i *Item) *ItemUpdateOne { } // UpdateOneID returns an update builder for the given id. -func (c *ItemClient) UpdateOneID(id int) *ItemUpdateOne { +func (c *ItemClient) UpdateOneID(id string) *ItemUpdateOne { mutation := newItemMutation(c.config, OpUpdateOne, withItemID(id)) return &ItemUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation} } @@ -1171,7 +1171,7 @@ func (c *ItemClient) DeleteOne(i *Item) *ItemDeleteOne { } // DeleteOneID returns a delete builder for the given id. -func (c *ItemClient) DeleteOneID(id int) *ItemDeleteOne { +func (c *ItemClient) DeleteOneID(id string) *ItemDeleteOne { builder := c.Delete().Where(item.ID(id)) builder.mutation.id = &id builder.mutation.op = OpDeleteOne @@ -1186,12 +1186,12 @@ func (c *ItemClient) Query() *ItemQuery { } // Get returns a Item entity by its id. -func (c *ItemClient) Get(ctx context.Context, id int) (*Item, error) { +func (c *ItemClient) Get(ctx context.Context, id string) (*Item, error) { return c.Query().Where(item.ID(id)).Only(ctx) } // GetX is like Get, but panics if an error occurs. -func (c *ItemClient) GetX(ctx context.Context, id int) *Item { +func (c *ItemClient) GetX(ctx context.Context, id string) *Item { obj, err := c.Get(ctx, id) if err != nil { panic(err) diff --git a/entc/integration/ent/entql.go b/entc/integration/ent/entql.go index 64eb7d706..45da4647b 100644 --- a/entc/integration/ent/entql.go +++ b/entc/integration/ent/entql.go @@ -221,7 +221,7 @@ var schemaGraph = func() *sqlgraph.Schema { Table: item.Table, Columns: item.Columns, ID: &sqlgraph.FieldSpec{ - Type: field.TypeInt, + Type: field.TypeString, Column: item.FieldID, }, }, @@ -1545,8 +1545,8 @@ func (f *ItemFilter) Where(p entql.P) { }) } -// WhereID applies the entql int predicate on the id field. -func (f *ItemFilter) WhereID(p entql.IntP) { +// WhereID applies the entql string predicate on the id field. +func (f *ItemFilter) WhereID(p entql.StringP) { f.Where(p.Field(item.FieldID)) } diff --git a/entc/integration/ent/item.go b/entc/integration/ent/item.go index 34503185d..3ee5d1201 100644 --- a/entc/integration/ent/item.go +++ b/entc/integration/ent/item.go @@ -18,7 +18,7 @@ import ( type Item struct { config // ID of the ent. - ID int `json:"id,omitempty"` + ID string `json:"id,omitempty"` } // scanValues returns the types for scanning values from sql.Rows. @@ -27,7 +27,7 @@ func (*Item) scanValues(columns []string) ([]interface{}, error) { for i := range columns { switch columns[i] { case item.FieldID: - values[i] = new(sql.NullInt64) + values[i] = new(sql.NullString) default: return nil, fmt.Errorf("unexpected column %q for type Item", columns[i]) } @@ -44,11 +44,11 @@ func (i *Item) assignValues(columns []string, values []interface{}) error { for j := range columns { switch columns[j] { case item.FieldID: - value, ok := values[j].(*sql.NullInt64) - if !ok { - return fmt.Errorf("unexpected type %T for field id", value) + if value, ok := values[j].(*sql.NullString); !ok { + return fmt.Errorf("unexpected type %T for field id", values[j]) + } else if value.Valid { + i.ID = value.String } - i.ID = int(value.Int64) } } return nil diff --git a/entc/integration/ent/item/item.go b/entc/integration/ent/item/item.go index 6caeb6d51..8615d76df 100644 --- a/entc/integration/ent/item/item.go +++ b/entc/integration/ent/item/item.go @@ -30,4 +30,9 @@ func ValidColumn(column string) bool { return false } +var ( + // IDValidator is a validator for the "id" field. It is called by the builders before save. + IDValidator func(string) error +) + // comment from another template. diff --git a/entc/integration/ent/item/where.go b/entc/integration/ent/item/where.go index 67200e48d..63f4d0fdb 100644 --- a/entc/integration/ent/item/where.go +++ b/entc/integration/ent/item/where.go @@ -12,28 +12,28 @@ import ( ) // ID filters vertices based on their ID field. -func ID(id int) predicate.Item { +func ID(id string) predicate.Item { return predicate.Item(func(s *sql.Selector) { s.Where(sql.EQ(s.C(FieldID), id)) }) } // IDEQ applies the EQ predicate on the ID field. -func IDEQ(id int) predicate.Item { +func IDEQ(id string) predicate.Item { return predicate.Item(func(s *sql.Selector) { s.Where(sql.EQ(s.C(FieldID), id)) }) } // IDNEQ applies the NEQ predicate on the ID field. -func IDNEQ(id int) predicate.Item { +func IDNEQ(id string) predicate.Item { return predicate.Item(func(s *sql.Selector) { s.Where(sql.NEQ(s.C(FieldID), id)) }) } // IDIn applies the In predicate on the ID field. -func IDIn(ids ...int) predicate.Item { +func IDIn(ids ...string) predicate.Item { return predicate.Item(func(s *sql.Selector) { // if not arguments were provided, append the FALSE constants, // since we can't apply "IN ()". This will make this predicate falsy. @@ -50,7 +50,7 @@ func IDIn(ids ...int) predicate.Item { } // IDNotIn applies the NotIn predicate on the ID field. -func IDNotIn(ids ...int) predicate.Item { +func IDNotIn(ids ...string) predicate.Item { return predicate.Item(func(s *sql.Selector) { // if not arguments were provided, append the FALSE constants, // since we can't apply "IN ()". This will make this predicate falsy. @@ -67,28 +67,28 @@ func IDNotIn(ids ...int) predicate.Item { } // IDGT applies the GT predicate on the ID field. -func IDGT(id int) predicate.Item { +func IDGT(id string) predicate.Item { return predicate.Item(func(s *sql.Selector) { s.Where(sql.GT(s.C(FieldID), id)) }) } // IDGTE applies the GTE predicate on the ID field. -func IDGTE(id int) predicate.Item { +func IDGTE(id string) predicate.Item { return predicate.Item(func(s *sql.Selector) { s.Where(sql.GTE(s.C(FieldID), id)) }) } // IDLT applies the LT predicate on the ID field. -func IDLT(id int) predicate.Item { +func IDLT(id string) predicate.Item { return predicate.Item(func(s *sql.Selector) { s.Where(sql.LT(s.C(FieldID), id)) }) } // IDLTE applies the LTE predicate on the ID field. -func IDLTE(id int) predicate.Item { +func IDLTE(id string) predicate.Item { return predicate.Item(func(s *sql.Selector) { s.Where(sql.LTE(s.C(FieldID), id)) }) diff --git a/entc/integration/ent/item_create.go b/entc/integration/ent/item_create.go index c6a684ae3..e50995475 100644 --- a/entc/integration/ent/item_create.go +++ b/entc/integration/ent/item_create.go @@ -22,6 +22,12 @@ type ItemCreate struct { hooks []Hook } +// SetID sets the "id" field. +func (ic *ItemCreate) SetID(s string) *ItemCreate { + ic.mutation.SetID(s) + return ic +} + // Mutation returns the ItemMutation object of the builder. func (ic *ItemCreate) Mutation() *ItemMutation { return ic.mutation @@ -76,6 +82,11 @@ func (ic *ItemCreate) SaveX(ctx context.Context) *Item { // check runs all checks and user-defined validators on the builder. func (ic *ItemCreate) check() error { + if v, ok := ic.mutation.ID(); ok { + if err := item.IDValidator(v); err != nil { + return &ValidationError{Name: "id", err: fmt.Errorf("ent: validator failed for field \"id\": %w", err)} + } + } return nil } @@ -87,8 +98,6 @@ func (ic *ItemCreate) sqlSave(ctx context.Context) (*Item, error) { } return nil, err } - id := _spec.ID.Value.(int64) - _node.ID = int(id) return _node, nil } @@ -98,11 +107,15 @@ func (ic *ItemCreate) createSpec() (*Item, *sqlgraph.CreateSpec) { _spec = &sqlgraph.CreateSpec{ Table: item.Table, ID: &sqlgraph.FieldSpec{ - Type: field.TypeInt, + Type: field.TypeString, Column: item.FieldID, }, } ) + if id, ok := ic.mutation.ID(); ok { + _node.ID = id + _spec.ID.Value = id + } return _node, _spec } @@ -146,8 +159,6 @@ func (icb *ItemCreateBulk) Save(ctx context.Context) ([]*Item, error) { } mutation.id = &nodes[i].ID mutation.done = true - id := specs[i].ID.Value.(int64) - nodes[i].ID = int(id) return nodes[i], nil }) for i := len(builder.hooks) - 1; i >= 0; i-- { diff --git a/entc/integration/ent/item_delete.go b/entc/integration/ent/item_delete.go index 596a024ee..b13df169b 100644 --- a/entc/integration/ent/item_delete.go +++ b/entc/integration/ent/item_delete.go @@ -73,7 +73,7 @@ func (id *ItemDelete) sqlExec(ctx context.Context) (int, error) { Node: &sqlgraph.NodeSpec{ Table: item.Table, ID: &sqlgraph.FieldSpec{ - Type: field.TypeInt, + Type: field.TypeString, Column: item.FieldID, }, }, diff --git a/entc/integration/ent/item_query.go b/entc/integration/ent/item_query.go index 69719c3ef..7500e7f43 100644 --- a/entc/integration/ent/item_query.go +++ b/entc/integration/ent/item_query.go @@ -88,8 +88,8 @@ func (iq *ItemQuery) FirstX(ctx context.Context) *Item { // FirstID returns the first Item ID from the query. // Returns a *NotFoundError when no Item ID was found. -func (iq *ItemQuery) FirstID(ctx context.Context) (id int, err error) { - var ids []int +func (iq *ItemQuery) FirstID(ctx context.Context) (id string, err error) { + var ids []string if ids, err = iq.Limit(1).IDs(ctx); err != nil { return } @@ -101,7 +101,7 @@ func (iq *ItemQuery) FirstID(ctx context.Context) (id int, err error) { } // FirstIDX is like FirstID, but panics if an error occurs. -func (iq *ItemQuery) FirstIDX(ctx context.Context) int { +func (iq *ItemQuery) FirstIDX(ctx context.Context) string { id, err := iq.FirstID(ctx) if err != nil && !IsNotFound(err) { panic(err) @@ -139,8 +139,8 @@ func (iq *ItemQuery) OnlyX(ctx context.Context) *Item { // OnlyID is like Only, but returns the only Item ID in the query. // Returns a *NotSingularError when exactly one Item ID is not found. // Returns a *NotFoundError when no entities are found. -func (iq *ItemQuery) OnlyID(ctx context.Context) (id int, err error) { - var ids []int +func (iq *ItemQuery) OnlyID(ctx context.Context) (id string, err error) { + var ids []string if ids, err = iq.Limit(2).IDs(ctx); err != nil { return } @@ -156,7 +156,7 @@ func (iq *ItemQuery) OnlyID(ctx context.Context) (id int, err error) { } // OnlyIDX is like OnlyID, but panics if an error occurs. -func (iq *ItemQuery) OnlyIDX(ctx context.Context) int { +func (iq *ItemQuery) OnlyIDX(ctx context.Context) string { id, err := iq.OnlyID(ctx) if err != nil { panic(err) @@ -182,8 +182,8 @@ func (iq *ItemQuery) AllX(ctx context.Context) []*Item { } // IDs executes the query and returns a list of Item IDs. -func (iq *ItemQuery) IDs(ctx context.Context) ([]int, error) { - var ids []int +func (iq *ItemQuery) IDs(ctx context.Context) ([]string, error) { + var ids []string if err := iq.Select(item.FieldID).Scan(ctx, &ids); err != nil { return nil, err } @@ -191,7 +191,7 @@ func (iq *ItemQuery) IDs(ctx context.Context) ([]int, error) { } // IDsX is like IDs, but panics if an error occurs. -func (iq *ItemQuery) IDsX(ctx context.Context) []int { +func (iq *ItemQuery) IDsX(ctx context.Context) []string { ids, err := iq.IDs(ctx) if err != nil { panic(err) @@ -333,7 +333,7 @@ func (iq *ItemQuery) querySpec() *sqlgraph.QuerySpec { Table: item.Table, Columns: item.Columns, ID: &sqlgraph.FieldSpec{ - Type: field.TypeInt, + Type: field.TypeString, Column: item.FieldID, }, }, diff --git a/entc/integration/ent/item_update.go b/entc/integration/ent/item_update.go index 334862b09..5dbd8728c 100644 --- a/entc/integration/ent/item_update.go +++ b/entc/integration/ent/item_update.go @@ -92,7 +92,7 @@ func (iu *ItemUpdate) sqlSave(ctx context.Context) (n int, err error) { Table: item.Table, Columns: item.Columns, ID: &sqlgraph.FieldSpec{ - Type: field.TypeInt, + Type: field.TypeString, Column: item.FieldID, }, }, @@ -192,7 +192,7 @@ func (iuo *ItemUpdateOne) sqlSave(ctx context.Context) (_node *Item, err error) Table: item.Table, Columns: item.Columns, ID: &sqlgraph.FieldSpec{ - Type: field.TypeInt, + Type: field.TypeString, Column: item.FieldID, }, }, diff --git a/entc/integration/ent/migrate/schema.go b/entc/integration/ent/migrate/schema.go index ddc38e3e6..eb201b13a 100644 --- a/entc/integration/ent/migrate/schema.go +++ b/entc/integration/ent/migrate/schema.go @@ -270,7 +270,7 @@ var ( } // ItemsColumns holds the columns for the "items" table. ItemsColumns = []*schema.Column{ - {Name: "id", Type: field.TypeInt, Increment: true}, + {Name: "id", Type: field.TypeString, Size: 64}, } // ItemsTable holds the schema information for the "items" table. ItemsTable = &schema.Table{ diff --git a/entc/integration/ent/mutation.go b/entc/integration/ent/mutation.go index f70fa724d..694a96d82 100644 --- a/entc/integration/ent/mutation.go +++ b/entc/integration/ent/mutation.go @@ -9481,7 +9481,7 @@ type ItemMutation struct { config op Op typ string - id *int + id *string clearedFields map[string]struct{} done bool oldValue func(context.Context) (*Item, error) @@ -9508,7 +9508,7 @@ func newItemMutation(c config, op Op, opts ...itemOption) *ItemMutation { } // withItemID sets the ID field of the mutation. -func withItemID(id int) itemOption { +func withItemID(id string) itemOption { return func(m *ItemMutation) { var ( err error @@ -9558,9 +9558,15 @@ func (m ItemMutation) Tx() (*Tx, error) { return tx, nil } +// SetID sets the value of the id field. Note that this +// operation is only accepted on creation of Item entities. +func (m *ItemMutation) SetID(id string) { + m.id = &id +} + // ID returns the ID value in the mutation. Note that the ID is only available // if it was provided to the builder or after it was returned from the database. -func (m *ItemMutation) ID() (id int, exists bool) { +func (m *ItemMutation) ID() (id string, exists bool) { if m.id == nil { return } diff --git a/entc/integration/ent/runtime.go b/entc/integration/ent/runtime.go index c87907ae6..ba2490829 100644 --- a/entc/integration/ent/runtime.go +++ b/entc/integration/ent/runtime.go @@ -17,6 +17,7 @@ import ( "entgo.io/ent/entc/integration/ent/file" "entgo.io/ent/entc/integration/ent/group" "entgo.io/ent/entc/integration/ent/groupinfo" + "entgo.io/ent/entc/integration/ent/item" "entgo.io/ent/entc/integration/ent/pet" "entgo.io/ent/entc/integration/ent/schema" "entgo.io/ent/entc/integration/ent/task" @@ -164,6 +165,12 @@ func init() { groupinfoDescMaxUsers := groupinfoFields[1].Descriptor() // groupinfo.DefaultMaxUsers holds the default value on creation for the max_users field. groupinfo.DefaultMaxUsers = groupinfoDescMaxUsers.Default.(int) + itemFields := schema.Item{}.Fields() + _ = itemFields + // itemDescID is the schema descriptor for id field. + itemDescID := itemFields[0].Descriptor() + // item.IDValidator is a validator for the "id" field. It is called by the builders before save. + item.IDValidator = itemDescID.Validators[0].(func(string) error) petFields := schema.Pet{}.Fields() _ = petFields // petDescAge is the schema descriptor for age field. diff --git a/entc/integration/ent/schema/item.go b/entc/integration/ent/schema/item.go index 5b0540fb7..0b2e7d8a2 100644 --- a/entc/integration/ent/schema/item.go +++ b/entc/integration/ent/schema/item.go @@ -6,6 +6,7 @@ package schema import ( "entgo.io/ent" + "entgo.io/ent/schema/field" ) // Item holds the schema definition for the Item entity. @@ -15,7 +16,10 @@ type Item struct { // Fields of the Item. func (Item) Fields() []ent.Field { - return nil + return []ent.Field{ + field.String("id"). + MaxLen(64), + } } // Edges of the Item. diff --git a/entc/integration/gremlin/ent/item/item.go b/entc/integration/gremlin/ent/item/item.go index 3c827624f..1a8bbc728 100644 --- a/entc/integration/gremlin/ent/item/item.go +++ b/entc/integration/gremlin/ent/item/item.go @@ -13,4 +13,9 @@ const ( FieldID = "id" ) +var ( + // IDValidator is a validator for the "id" field. It is called by the builders before save. + IDValidator func(string) error +) + // comment from another template. diff --git a/entc/integration/gremlin/ent/item_create.go b/entc/integration/gremlin/ent/item_create.go index e3816bf3b..7c027363e 100644 --- a/entc/integration/gremlin/ent/item_create.go +++ b/entc/integration/gremlin/ent/item_create.go @@ -23,6 +23,12 @@ type ItemCreate struct { hooks []Hook } +// SetID sets the "id" field. +func (ic *ItemCreate) SetID(s string) *ItemCreate { + ic.mutation.SetID(s) + return ic +} + // Mutation returns the ItemMutation object of the builder. func (ic *ItemCreate) Mutation() *ItemMutation { return ic.mutation @@ -77,6 +83,11 @@ func (ic *ItemCreate) SaveX(ctx context.Context) *Item { // check runs all checks and user-defined validators on the builder. func (ic *ItemCreate) check() error { + if v, ok := ic.mutation.ID(); ok { + if err := item.IDValidator(v); err != nil { + return &ValidationError{Name: "id", err: fmt.Errorf("ent: validator failed for field \"id\": %w", err)} + } + } return nil } @@ -98,6 +109,9 @@ func (ic *ItemCreate) gremlinSave(ctx context.Context) (*Item, error) { func (ic *ItemCreate) gremlin() *dsl.Traversal { v := g.AddV(item.Label) + if id, ok := ic.mutation.ID(); ok { + v.Property(dsl.ID, id) + } return v.ValueMap(true) } diff --git a/entc/integration/gremlin/ent/mutation.go b/entc/integration/gremlin/ent/mutation.go index 466b43cc4..8cbfbdc63 100644 --- a/entc/integration/gremlin/ent/mutation.go +++ b/entc/integration/gremlin/ent/mutation.go @@ -9558,6 +9558,12 @@ func (m ItemMutation) Tx() (*Tx, error) { return tx, nil } +// SetID sets the value of the id field. Note that this +// operation is only accepted on creation of Item entities. +func (m *ItemMutation) SetID(id string) { + m.id = &id +} + // ID returns the ID value in the mutation. Note that the ID is only available // if it was provided to the builder or after it was returned from the database. func (m *ItemMutation) ID() (id string, exists bool) { diff --git a/entc/integration/gremlin/ent/runtime.go b/entc/integration/gremlin/ent/runtime.go index 54f099381..758802d67 100644 --- a/entc/integration/gremlin/ent/runtime.go +++ b/entc/integration/gremlin/ent/runtime.go @@ -18,6 +18,7 @@ import ( "entgo.io/ent/entc/integration/gremlin/ent/file" "entgo.io/ent/entc/integration/gremlin/ent/group" "entgo.io/ent/entc/integration/gremlin/ent/groupinfo" + "entgo.io/ent/entc/integration/gremlin/ent/item" "entgo.io/ent/entc/integration/gremlin/ent/pet" "entgo.io/ent/entc/integration/gremlin/ent/task" "entgo.io/ent/entc/integration/gremlin/ent/user" @@ -164,6 +165,12 @@ func init() { groupinfoDescMaxUsers := groupinfoFields[1].Descriptor() // groupinfo.DefaultMaxUsers holds the default value on creation for the max_users field. groupinfo.DefaultMaxUsers = groupinfoDescMaxUsers.Default.(int) + itemFields := schema.Item{}.Fields() + _ = itemFields + // itemDescID is the schema descriptor for id field. + itemDescID := itemFields[0].Descriptor() + // item.IDValidator is a validator for the "id" field. It is called by the builders before save. + item.IDValidator = itemDescID.Validators[0].(func(string) error) petFields := schema.Pet{}.Fields() _ = petFields // petDescAge is the schema descriptor for age field. diff --git a/entc/integration/gremlin/gremlin_test.go b/entc/integration/gremlin/gremlin_test.go index 2890a71f8..29809c26f 100644 --- a/entc/integration/gremlin/gremlin_test.go +++ b/entc/integration/gremlin/gremlin_test.go @@ -25,6 +25,7 @@ import ( "entgo.io/ent/entc/integration/gremlin/ent/pet" "entgo.io/ent/entc/integration/gremlin/ent/user" + "github.com/google/uuid" "github.com/stretchr/testify/require" ) @@ -139,6 +140,9 @@ func Sanity(t *testing.T, client *ent.Client) { usr = client.User.UpdateOne(usr).SetName("baz").AddGroups(grp).SaveX(ctx) require.Equal("baz", usr.Name) require.NotEmpty(usr.QueryGroups().AllX(ctx)) + id := uuid.NewString() + it := client.Item.Create().SetID(id).SaveX(ctx) + require.Equal(id, it.ID) // grouping. var v []struct {