From cdaff52e6ac799b01e71123267803b5928e94af6 Mon Sep 17 00:00:00 2001 From: Sarah Funkhouser <147884153+golanglemonade@users.noreply.github.com> Date: Wed, 4 Sep 2024 12:38:58 -0600 Subject: [PATCH 1/3] feat: add search functions and resolver generation Signed-off-by: Sarah Funkhouser <147884153+golanglemonade@users.noreply.github.com> --- go.mod | 51 ++++++++-- go.sum | 138 ++++++++++++++++++++++++-- searchgen/doc.go | 2 + searchgen/search.go | 152 +++++++++++++++++++++++++++++ searchgen/templates/helpers.gotpl | 63 ++++++++++++ searchgen/templates/resolver.gotpl | 68 +++++++++++++ 6 files changed, 458 insertions(+), 16 deletions(-) create mode 100644 searchgen/doc.go create mode 100644 searchgen/search.go create mode 100644 searchgen/templates/helpers.gotpl create mode 100644 searchgen/templates/resolver.gotpl diff --git a/go.mod b/go.mod index d54ab15..4eb5d48 100644 --- a/go.mod +++ b/go.mod @@ -3,22 +3,61 @@ module github.com/theopenlane/gqlgen-plugins go 1.23.0 require ( + entgo.io/contrib v0.6.0 + entgo.io/ent v0.14.1 github.com/99designs/gqlgen v0.17.49 github.com/gertd/go-pluralize v0.2.1 github.com/stoewer/go-strcase v1.3.0 github.com/stretchr/testify v1.9.0 + github.com/theopenlane/entx v0.1.5 github.com/vektah/gqlparser/v2 v2.5.16 ) require ( + ariga.io/atlas v0.26.1 // indirect + github.com/XSAM/otelsql v0.33.0 // indirect + github.com/agext/levenshtein v1.2.3 // indirect github.com/agnivade/levenshtein v1.1.1 // indirect - github.com/davecgh/go-spew v1.1.1 // indirect + github.com/antlr4-go/antlr/v4 v4.13.1 // indirect + github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect + github.com/coder/websocket v1.8.12 // indirect + github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect + github.com/dustin/go-humanize v1.0.1 // indirect + github.com/go-logr/logr v1.4.2 // indirect + github.com/go-logr/stdr v1.2.2 // indirect + github.com/go-openapi/inflect v0.21.0 // indirect + github.com/google/go-cmp v0.6.0 // indirect github.com/google/uuid v1.6.0 // indirect - github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect + github.com/hashicorp/hcl/v2 v2.22.0 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/mitchellh/go-wordwrap v1.0.1 // indirect + github.com/ncruces/go-strftime v0.1.9 // indirect + github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect + github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect github.com/sosodev/duration v1.3.1 // indirect - golang.org/x/mod v0.18.0 // indirect - golang.org/x/sync v0.7.0 // indirect - golang.org/x/text v0.16.0 // indirect - golang.org/x/tools v0.22.0 // indirect + github.com/theopenlane/utils v0.1.5 // indirect + github.com/tursodatabase/libsql-client-go v0.0.0-20240902231107-85af5b9d094d // indirect + github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect + github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect + github.com/zclconf/go-cty v1.15.0 // indirect + go.opentelemetry.io/otel v1.29.0 // indirect + go.opentelemetry.io/otel/metric v1.29.0 // indirect + go.opentelemetry.io/otel/trace v1.29.0 // indirect + go.uber.org/multierr v1.11.0 // indirect + go.uber.org/zap v1.27.0 // indirect + golang.org/x/exp v0.0.0-20240823005443-9b4947da3948 // indirect + golang.org/x/mod v0.20.0 // indirect + golang.org/x/sync v0.8.0 // indirect + golang.org/x/sys v0.24.0 // indirect + golang.org/x/text v0.17.0 // indirect + golang.org/x/tools v0.24.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect + modernc.org/gc/v3 v3.0.0-20240801135723-a856999a2e4a // indirect + modernc.org/libc v1.60.0 // indirect + modernc.org/mathutil v1.6.0 // indirect + modernc.org/memory v1.8.0 // indirect + modernc.org/sqlite v1.32.0 // indirect + modernc.org/strutil v1.2.0 // indirect + modernc.org/token v1.1.0 // indirect ) diff --git a/go.sum b/go.sum index 0347386..964396d 100644 --- a/go.sum +++ b/go.sum @@ -1,22 +1,77 @@ +ariga.io/atlas v0.26.1 h1:UwLn9sXgcuoo9/A3sxXhDqnOImXUcaYb2JqVP0FQciw= +ariga.io/atlas v0.26.1/go.mod h1:KPLc7Zj+nzoXfWshrcY1RwlOh94dsATQEy4UPrF2RkM= +entgo.io/contrib v0.6.0 h1:xfo4TbJE7sJZWx7BV7YrpSz7IPFvS8MzL3fnfzZjKvQ= +entgo.io/contrib v0.6.0/go.mod h1:3qWIseJ/9Wx2Hu5zVh15FDzv7d/UvKNcYKdViywWCQg= +entgo.io/ent v0.14.1 h1:fUERL506Pqr92EPHJqr8EYxbPioflJo6PudkrEA8a/s= +entgo.io/ent v0.14.1/go.mod h1:MH6XLG0KXpkcDQhKiHfANZSzR55TJyPL5IGNpI8wpco= github.com/99designs/gqlgen v0.17.49 h1:b3hNGexHd33fBSAd4NDT/c3NCcQzcAVkknhN9ym36YQ= github.com/99designs/gqlgen v0.17.49/go.mod h1:tC8YFVZMed81x7UJ7ORUwXF4Kn6SXuucFqQBhN8+BU0= +github.com/DATA-DOG/go-sqlmock v1.5.0 h1:Shsta01QNfFxHCfpW6YH2STWB0MudeXXEWMr20OEh60= +github.com/DATA-DOG/go-sqlmock v1.5.0/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM= +github.com/XSAM/otelsql v0.33.0 h1:8ZgVGFMG78Gd7BcCkxZ+lBTybWrnOtQv5sn4sLWb0+w= +github.com/XSAM/otelsql v0.33.0/go.mod h1:TIaqdCA0m+GP0TJ4axwMSLunVfMFsxf1x1UU8MlUvAY= +github.com/agext/levenshtein v1.2.3 h1:YB2fHEn0UJagG8T1rrWknE3ZQzWM06O8AMAatNn7lmo= +github.com/agext/levenshtein v1.2.3/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558= github.com/agnivade/levenshtein v1.1.1 h1:QY8M92nrzkmr798gCo3kmMyqXFzdQVpxLlGPRBij0P8= github.com/agnivade/levenshtein v1.1.1/go.mod h1:veldBMzWxcCG2ZvUTKD2kJNRdCk5hVbJomOvKkmgYbo= github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883 h1:bvNMNQO63//z+xNgfBlViaCIJKLlCJ6/fmUseuG0wVQ= github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883/go.mod h1:rCTlJbsFo29Kk6CurOXKm700vrz8f0KW0JNfpkRJY/8= +github.com/antlr4-go/antlr/v4 v4.13.1 h1:SqQKkuVZ+zWkMMNkjy5FZe5mr5WURWnlpmOuzYWrPrQ= +github.com/antlr4-go/antlr/v4 v4.13.1/go.mod h1:GKmUxMtwp6ZgGwZSva4eWPC5mS6vUAmOABFgjdkM7Nw= +github.com/apparentlymart/go-textseg/v15 v15.0.0 h1:uYvfpb3DyLSCGWnctWKGj857c6ew1u1fNQOlOtuGxQY= +github.com/apparentlymart/go-textseg/v15 v15.0.0/go.mod h1:K8XmNZdhEBkdlyDdvbmmsvpAG721bKi0joRfFdHIWJ4= github.com/arbovm/levenshtein v0.0.0-20160628152529-48b4e1c0c4d0 h1:jfIu9sQUG6Ig+0+Ap1h4unLjW6YQJpKZVmUzxsD4E/Q= github.com/arbovm/levenshtein v0.0.0-20160628152529-48b4e1c0c4d0/go.mod h1:t2tdKJDJF9BV14lnkjHmOQgcvEKgtqs5a1N3LNdJhGE= +github.com/brianvoe/gofakeit/v7 v7.0.4 h1:Mkxwz9jYg8Ad8NvT9HA27pCMZGFQo08MK6jD0QTKEww= +github.com/brianvoe/gofakeit/v7 v7.0.4/go.mod h1:QXuPeBw164PJCzCUZVmgpgHJ3Llj49jSLVkKPMtxtxA= +github.com/coder/websocket v1.8.12 h1:5bUXkEPPIbewrnkU8LTCLVaxi4N4J8ahufH2vlo4NAo= +github.com/coder/websocket v1.8.12/go.mod h1:LNVeNrXQZfe5qhS9ALED3uA+l5pPqvwXg3CKoDBB2gs= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dgryski/trifles v0.0.0-20200323201526-dd97f9abfb48 h1:fRzb/w+pyskVMQ+UbP35JkH8yB7MYb4q/qhBarqZE6g= github.com/dgryski/trifles v0.0.0-20200323201526-dd97f9abfb48/go.mod h1:if7Fbed8SFyPtHLHbg49SI7NAdJiC5WIA09pe59rfAA= +github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= +github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= github.com/gertd/go-pluralize v0.2.1 h1:M3uASbVjMnTsPb0PNqg+E/24Vwigyo/tvyMTtAlLgiA= github.com/gertd/go-pluralize v0.2.1/go.mod h1:rbYaKDbsXxmRfr8uygAEKhOWsjyrrqrkHVpZvoOp8zk= +github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY= +github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= +github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= +github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= +github.com/go-openapi/inflect v0.21.0 h1:FoBjBTQEcbg2cJUWX6uwL9OyIW8eqc9k4KhN4lfbeYk= +github.com/go-openapi/inflect v0.21.0/go.mod h1:INezMuUu7SJQc2AyR3WO0DqqYUJSj8Kb4hBd7WtjlAw= +github.com/go-test/deep v1.0.3 h1:ZrJSEWsXzPOxaZnFteGEfooLba+ju3FYIbOrS+rQd68= +github.com/go-test/deep v1.0.3/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/pprof v0.0.0-20240409012703-83162a5b38cd h1:gbpYu9NMq8jhDVbvlGkMFWCjLFlqqEZjEmObmhUy6Vo= +github.com/google/pprof v0.0.0-20240409012703-83162a5b38cd/go.mod h1:kf6iHlnVGwgKolg33glAes7Yg/8iWP8ukqeldJSO7jw= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= +github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k= +github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM= +github.com/hashicorp/hcl/v2 v2.22.0 h1:hkZ3nCtqeJsDhPRFz5EA9iwcG1hNWGePOTw6oyul12M= +github.com/hashicorp/hcl/v2 v2.22.0/go.mod h1:62ZYHrXgPoX8xBnzl8QzbWq4dyDsDtfCRgIq1rbJEvA= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU= +github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= +github.com/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQflz0v0= +github.com/mitchellh/go-wordwrap v1.0.1/go.mod h1:R62XHJLzvMFRBbcrT7m7WgmE1eOyTSsCt+hzestvNj0= +github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= +github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= +github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4= +github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE= +github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= github.com/sergi/go-diff v1.3.1 h1:xkr+Oxo4BOQKmkn/B9eMK0g5Kg/983T9DqqPHwYqD+8= github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NFbPK1I= github.com/sosodev/duration v1.3.1 h1:qtHBDMQ6lvMQsL15g4aopM4HEfOaYuhWBw3NPTtlqq4= @@ -26,21 +81,58 @@ github.com/stoewer/go-strcase v1.3.0/go.mod h1:fAH5hQ5pehh+j3nZfvwdk2RgEgQjAoM8w github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY= +github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/theopenlane/entx v0.1.5 h1:qEJxAmsrGP1rD+PeDx5N3ThRmKWlTpRcYhA9rMfwo/k= +github.com/theopenlane/entx v0.1.5/go.mod h1:KRCgcNoZmHdljODipqHX92/MG90da6W7sNKiptuC/p4= +github.com/theopenlane/utils v0.1.5 h1:4DRieQmsBF87n4lPjEkTt6s4iVRQaCGYlk2+C05lt3o= +github.com/theopenlane/utils v0.1.5/go.mod h1:LWJzG9FfklsLlqWx/VdmfBMuNk700cWqHAwQL0299FM= +github.com/tursodatabase/libsql-client-go v0.0.0-20240902231107-85af5b9d094d h1:dOMI4+zEbDI37KGb0TI44GUAwxHF9cMsIoDTJ7UmgfU= +github.com/tursodatabase/libsql-client-go v0.0.0-20240902231107-85af5b9d094d/go.mod h1:l8xTsYB90uaVdMHXMCxKKLSgw5wLYBwBKKefNIUnm9s= github.com/vektah/gqlparser/v2 v2.5.16 h1:1gcmLTvs3JLKXckwCwlUagVn/IlV2bwqle0vJ0vy5p8= github.com/vektah/gqlparser/v2 v2.5.16/go.mod h1:1lz1OeCqgQbQepsGxPVywrjdBHW2T08PUS3pJqepRww= -golang.org/x/mod v0.18.0 h1:5+9lSbEzPSdWkH32vYPBwEpX8KwDbM52Ud9xBUvNlb0= -golang.org/x/mod v0.18.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= -golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= -golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= -golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4= -golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI= -golang.org/x/tools v0.22.0 h1:gqSGLZqv+AI9lIQzniJ0nZDRG5GBPsSi+DRNHWNz6yA= -golang.org/x/tools v0.22.0/go.mod h1:aCwcsjqvq7Yqt6TNyX7QMU2enbQ/Gt0bo6krSeEri+c= +github.com/vmihailenco/msgpack/v5 v5.4.1 h1:cQriyiUvjTwOHg8QZaPihLWeRAAVoCpE00IUPn0Bjt8= +github.com/vmihailenco/msgpack/v5 v5.4.1/go.mod h1:GaZTsDaehaPpQVyxrf5mtQlH+pc21PIudVV/E3rRQok= +github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g= +github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds= +github.com/zclconf/go-cty v1.15.0 h1:tTCRWxsexYUmtt/wVxgDClUe+uQusuI443uL6e+5sXQ= +github.com/zclconf/go-cty v1.15.0/go.mod h1:VvMs5i0vgZdhYawQNq5kePSpLAoz8u1xvZgrPIxfnZE= +github.com/zclconf/go-cty-debug v0.0.0-20240509010212-0d6042c53940 h1:4r45xpDWB6ZMSMNJFMOjqrGHynW3DIBuR2H9j0ug+Mo= +github.com/zclconf/go-cty-debug v0.0.0-20240509010212-0d6042c53940/go.mod h1:CmBdvvj3nqzfzJ6nTCIwDTPZ56aVGvDrmztiO5g3qrM= +go.opentelemetry.io/otel v1.29.0 h1:PdomN/Al4q/lN6iBJEN3AwPvUiHPMlt93c8bqTG5Llw= +go.opentelemetry.io/otel v1.29.0/go.mod h1:N/WtXPs1CNCUEx+Agz5uouwCba+i+bJGFicT8SR4NP8= +go.opentelemetry.io/otel/metric v1.29.0 h1:vPf/HFWTNkPu1aYeIsc98l4ktOQaL6LeSoeV2g+8YLc= +go.opentelemetry.io/otel/metric v1.29.0/go.mod h1:auu/QWieFVWx+DmQOUMgj0F8LHWdgalxXqvp7BII/W8= +go.opentelemetry.io/otel/sdk v1.29.0 h1:vkqKjk7gwhS8VaWb0POZKmIEDimRCMsopNYnriHyryo= +go.opentelemetry.io/otel/sdk v1.29.0/go.mod h1:pM8Dx5WKnvxLCb+8lG1PRNIDxu9g9b9g59Qr7hfAAok= +go.opentelemetry.io/otel/sdk/metric v1.29.0 h1:K2CfmJohnRgvZ9UAj2/FhIf/okdWcNdBwe1m8xFXiSY= +go.opentelemetry.io/otel/sdk/metric v1.29.0/go.mod h1:6zZLdCl2fkauYoZIOn/soQIDSWFmNSRcICarHfuhNJQ= +go.opentelemetry.io/otel/trace v1.29.0 h1:J/8ZNK4XgR7a21DZUAsbF8pZ5Jcw1VhACmnYt39JTi4= +go.opentelemetry.io/otel/trace v1.29.0/go.mod h1:eHl3w0sp3paPkYstJOmAimxhiFXPg+MMTlEh3nsQgWQ= +go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= +go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= +go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= +go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= +go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8= +go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E= +golang.org/x/exp v0.0.0-20240823005443-9b4947da3948 h1:kx6Ds3MlpiUHKj7syVnbp57++8WpuKPcR5yjLBjvLEA= +golang.org/x/exp v0.0.0-20240823005443-9b4947da3948/go.mod h1:akd2r19cwCdwSwWeIdzYQGa/EZZyqcOdwWiwj5L5eKQ= +golang.org/x/mod v0.20.0 h1:utOm6MM3R3dnawAiJgn0y+xvuYRsm1RKM/4giyfDgV0= +golang.org/x/mod v0.20.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= +golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= +golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg= +golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc= +golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= +golang.org/x/tools v0.24.0 h1:J1shsA93PJUEVaUSaay7UXAyE8aimq3GW0pjlolpa24= +golang.org/x/tools v0.24.0/go.mod h1:YhNqVBIfWHdzvTLs0d8LCuMhkKUgSUKldakyV7W/WDQ= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= @@ -48,3 +140,29 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +modernc.org/cc/v4 v4.21.4 h1:3Be/Rdo1fpr8GrQ7IVw9OHtplU4gWbb+wNgeoBMmGLQ= +modernc.org/cc/v4 v4.21.4/go.mod h1:HM7VJTZbUCR3rV8EYBi9wxnJ0ZBRiGE5OeGXNA0IsLQ= +modernc.org/ccgo/v4 v4.21.0 h1:kKPI3dF7RIag8YcToh5ZwDcVMIv6VGa0ED5cvh0LMW4= +modernc.org/ccgo/v4 v4.21.0/go.mod h1:h6kt6H/A2+ew/3MW/p6KEoQmrq/i3pr0J/SiwiaF/g0= +modernc.org/fileutil v1.3.0 h1:gQ5SIzK3H9kdfai/5x41oQiKValumqNTDXMvKo62HvE= +modernc.org/fileutil v1.3.0/go.mod h1:XatxS8fZi3pS8/hKG2GH/ArUogfxjpEKs3Ku3aK4JyQ= +modernc.org/gc/v2 v2.5.0 h1:bJ9ChznK1L1mUtAQtxi0wi5AtAs5jQuw4PrPHO5pb6M= +modernc.org/gc/v2 v2.5.0/go.mod h1:wzN5dK1AzVGoH6XOzc3YZ+ey/jPgYHLuVckd62P0GYU= +modernc.org/gc/v3 v3.0.0-20240801135723-a856999a2e4a h1:CfbpOLEo2IwNzJdMvE8aiRbPMxoTpgAJeyePh0SmO8M= +modernc.org/gc/v3 v3.0.0-20240801135723-a856999a2e4a/go.mod h1:Qz0X07sNOR1jWYCrJMEnbW/X55x206Q7Vt4mz6/wHp4= +modernc.org/libc v1.60.0 h1:XeRF1gXky7JE5E8IErtYAdKj+ykZPdYUsgJNQ8RFWIA= +modernc.org/libc v1.60.0/go.mod h1:xJuobKuNxKH3RUatS7GjR+suWj+5c2K7bi4m/S5arOY= +modernc.org/mathutil v1.6.0 h1:fRe9+AmYlaej+64JsEEhoWuAYBkOtQiMEU7n/XgfYi4= +modernc.org/mathutil v1.6.0/go.mod h1:Ui5Q9q1TR2gFm0AQRqQUaBWFLAhQpCwNcuhBOSedWPo= +modernc.org/memory v1.8.0 h1:IqGTL6eFMaDZZhEWwcREgeMXYwmW83LYW8cROZYkg+E= +modernc.org/memory v1.8.0/go.mod h1:XPZ936zp5OMKGWPqbD3JShgd/ZoQ7899TUuQqxY+peU= +modernc.org/opt v0.1.3 h1:3XOZf2yznlhC+ibLltsDGzABUGVx8J6pnFMS3E4dcq4= +modernc.org/opt v0.1.3/go.mod h1:WdSiB5evDcignE70guQKxYUl14mgWtbClRi5wmkkTX0= +modernc.org/sortutil v1.2.0 h1:jQiD3PfS2REGJNzNCMMaLSp/wdMNieTbKX920Cqdgqc= +modernc.org/sortutil v1.2.0/go.mod h1:TKU2s7kJMf1AE84OoiGppNHJwvB753OYfNl2WRb++Ss= +modernc.org/sqlite v1.32.0 h1:6BM4uGza7bWypsw4fdLRsLxut6bHe4c58VeqjRgST8s= +modernc.org/sqlite v1.32.0/go.mod h1:UqoylwmTb9F+IqXERT8bW9zzOWN8qwAIcLdzeBZs4hA= +modernc.org/strutil v1.2.0 h1:agBi9dp1I+eOnxXeiZawM8F4LawKv4NzGWSaLfyeNZA= +modernc.org/strutil v1.2.0/go.mod h1:/mdcBmfOibveCTBxUl5B5l6W+TTH1FXPLHZE6bTosX0= +modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y= +modernc.org/token v1.1.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM= diff --git a/searchgen/doc.go b/searchgen/doc.go new file mode 100644 index 0000000..0615d08 --- /dev/null +++ b/searchgen/doc.go @@ -0,0 +1,2 @@ +// Package searchgen provides the the ability to generate a search resolver +package searchgen diff --git a/searchgen/search.go b/searchgen/search.go new file mode 100644 index 0000000..d749f4a --- /dev/null +++ b/searchgen/search.go @@ -0,0 +1,152 @@ +package searchgen + +import ( + "cmp" + _ "embed" + "fmt" + "slices" + "strings" + "text/template" + + "entgo.io/contrib/entgql" + "entgo.io/ent/entc" + "entgo.io/ent/entc/gen" + "github.com/99designs/gqlgen/codegen" + "github.com/99designs/gqlgen/codegen/templates" + "github.com/gertd/go-pluralize" + "github.com/theopenlane/entx/genhooks" +) + +const ( + relativeSchemaPath = "./internal/ent/schema" +) + +var SearchDirective = entgql.NewDirective("search") + +//go:embed templates/helpers.gotpl +var helperTemplate string + +//go:embed templates/resolver.gotpl +var resolverTemplate string + +// SearchPlugin is a gqlgen plugin to generate search functions +type SearchPlugin struct{} + +// Name returns the name of the plugin +// This name must match the upstream resolvergen to replace during code generation +func (r SearchPlugin) Name() string { + return "searchgen" +} + +// NewSearchPlugin returns a new search plugin +func New() *SearchPlugin { + return &SearchPlugin{} +} + +// SearchResolverBuild is a struct to hold the objects for the bulk resolver +type SearchResolverBuild struct { + // Name of the search type + Name string + // Objects is a list of objects to generate bulk resolvers for + Objects []Object +} + +// Object is a struct to hold the object name for the bulk resolver +type Object struct { + // Name of the object + Name string + // Fields of the object that are searchable + Fields []genhooks.Field + // AdminFields of the object that are searchable + AdminFields []genhooks.Field +} + +// GenerateCode implements api.CodeGenerator to generate the search resolver and it's helper functions +func (r SearchPlugin) GenerateCode(data *codegen.Data) error { + inputData, err := getInputData(data) + if err != nil { + return err + } + + // generate the search helper + if err := genSearchHelper(data, inputData); err != nil { + return err + } + + // generate the search resolver + inputData.Name = "Global" + if err := genSearchResolver(data, inputData, "search"); err != nil { + return err + } + + // generate the admin search resolver + inputData.Name = "Admin" + + return genSearchResolver(data, inputData, "adminsearch") +} + +func getInputData(data *codegen.Data) (SearchResolverBuild, error) { + inputData := SearchResolverBuild{ + Objects: []Object{}, + } + + graph, err := entc.LoadGraph(relativeSchemaPath, &gen.Config{}) + if err != nil { + return inputData, err + } + + for _, f := range data.Schema.Types { + // Add the search fields + if strings.Contains(f.Name, "Search") && !strings.Contains(f.Name, "GlobalSearch") { + schemaName := strings.TrimSuffix(f.Name, "SearchResult") + fields, adminFields := genhooks.GetSearchableFields(schemaName, graph) + + if len(fields) > 0 { + inputData.Objects = append(inputData.Objects, Object{ + Name: schemaName, + Fields: fields, // add the fields that are being searched + AdminFields: adminFields, // add the admin fields that are being searched + }) + } + } + } + + // sort objects by name so we have consistent output + slices.SortFunc(inputData.Objects, func(a, b Object) int { + return cmp.Compare(a.Name, b.Name) + }) + + return inputData, nil +} + +// genSearchHelper generates the search helper functions +func genSearchHelper(data *codegen.Data, inputData SearchResolverBuild) error { + return templates.Render(templates.Options{ + PackageName: data.Config.Resolver.Package, // use the resolver package + Filename: data.Config.Resolver.Dir() + "/search.go", // write to the resolver directory + FileNotice: `// THIS CODE IS REGENERATED BY github.com/theopenlane/gqlgen-plugins. DO NOT EDIT.`, + Data: inputData, + Funcs: template.FuncMap{ + "toLower": strings.ToLower, + "toPlural": pluralize.NewClient().Plural, + }, + Packages: data.Config.Packages, + Template: helperTemplate, + }) +} + +// genSearchResolver generates the search resolver functions +func genSearchResolver(data *codegen.Data, inputData SearchResolverBuild, resolverName string) error { + return templates.Render(templates.Options{ + PackageName: data.Config.Resolver.Package, // use the resolver package + Filename: data.Config.Resolver.Dir() + fmt.Sprintf("/%s.resolvers.go", resolverName), // write to the resolver directory + FileNotice: `// THIS CODE IS REGENERATED BY github.com/theopenlane/gqlgen-plugins. DO NOT EDIT.`, + Data: inputData, + Funcs: template.FuncMap{ + "toLower": strings.ToLower, + "toPlural": pluralize.NewClient().Plural, + }, + Packages: data.Config.Packages, + Template: resolverTemplate, + }) +} diff --git a/searchgen/templates/helpers.gotpl b/searchgen/templates/helpers.gotpl new file mode 100644 index 0000000..9340f2c --- /dev/null +++ b/searchgen/templates/helpers.gotpl @@ -0,0 +1,63 @@ +{{reserveImport "context" }} +{{reserveImport "time" }} + +{{reserveImport "github.com/theopenlane/core/internal/ent/generated" }} +{{reserveImport "entgo.io/ent/dialect/sql"}} +{{reserveImport "entgo.io/ent/dialect/sql/sqljson"}} + +import ( +{{- range $object := $.Objects }} + "github.com/theopenlane/core/internal/ent/generated/{{ $object.Name | toLower }}" +{{- end }} +) + +var ( + maxSearchTime = time.Duration(30 * time.Second) +) + +// searchResult is a generic struct to hold the result of a search operation +type searchResult[T any] struct { + result T + err error +} + +{{/* For each schema */}} +{{- range $object := $.Objects }} +// search{{ $object.Name }} searches for {{ $object.Name }} based on the query string looking for matches +func search{{ $object.Name | toPlural }}(ctx context.Context, query string) ([]*generated.{{ $object.Name }}, error) { + return withTransactionalMutation(ctx).{{ $object.Name }}.Query().Where( + {{ $object.Name | toLower }}.Or( + {{- range $field := $object.Fields }} + {{- if eq $field.Type "json.RawMessage" }} + func(s *sql.Selector) { + s.Where( + sqljson.ValueContains({{ $object.Name | toLower }}.Field{{ $field.Name }}, query), // search by {{ $field.Name }} + ) + }, + {{- else }} + {{ $object.Name | toLower }}.{{ $field.Name }}ContainsFold(query), // search by {{ $field.Name }} + {{- end }} + {{- end }} + ), + ).All(ctx) +} + +// search{{ $object.Name }} searches for {{ $object.Name }} based on the query string looking for matches +func adminSearch{{ $object.Name | toPlural }}(ctx context.Context, query string) ([]*generated.{{ $object.Name }}, error) { + return withTransactionalMutation(ctx).{{ $object.Name }}.Query().Where( + {{ $object.Name | toLower }}.Or( + {{- range $field := $object.AdminFields }} + {{- if eq $field.Type "json.RawMessage" }} + func(s *sql.Selector) { + s.Where( + sqljson.ValueContains({{ $object.Name | toLower }}.Field{{ $field.Name }}, query), // search by {{ $field.Name }} + ) + }, + {{- else }} + {{ $object.Name | toLower }}.{{ $field.Name }}ContainsFold(query), // search by {{ $field.Name }} + {{- end }} + {{- end }} + ), + ).All(ctx) +} +{{ end }} diff --git a/searchgen/templates/resolver.gotpl b/searchgen/templates/resolver.gotpl new file mode 100644 index 0000000..5f3f064 --- /dev/null +++ b/searchgen/templates/resolver.gotpl @@ -0,0 +1,68 @@ +{{reserveImport "context" }} + +{{reserveImport "github.com/theopenlane/core/internal/ent/generated" }} + +// Search is the resolver for the search field. +{{- if eq $.Name "Global" }} +func (r *queryResolver) Search(ctx context.Context, query string) (*SearchResultConnection, error) { +{{- else }} +func (r *queryResolver) {{ $.Name }}Search(ctx context.Context, query string) (*SearchResultConnection, error) { +{{- end }} + var ( + errors []error + {{- range $object := $.Objects }} + {{ $object.Name | toLower }}Results []*generated.{{ $object.Name }} + {{- end }} + ) + + r.withPool().SubmitMultipleAndWait([]func(){ + {{- range $object := $.Objects }} + func() { + var err error + {{ $object.Name | toLower }}Results, err = search{{ $object.Name | toPlural }}(ctx, query) + if err != nil { + errors = append(errors, err) + } + }, + {{- end }} + }) + + // Check all errors and return a single error if any of the searches failed + if len(errors) > 0 { + r.logger.Errorw("search failed", "errors", errors) + + return nil, ErrSearchFailed + } + + // return the results + return &SearchResultConnection{ + Nodes: []SearchResult{ + {{- range $object := $.Objects }} + {{ $object.Name }}SearchResult{ + {{ $object.Name | toPlural }}: {{ $object.Name | toLower }}Results, + }, + {{- end }} + }, + }, nil +} + +{{- range $object := $.Objects }} + +{{- if eq $.Name "Global" }} +func (r *queryResolver) {{ $object.Name }}Search(ctx context.Context, query string) (*{{ $object.Name }}SearchResult, error) { + {{ $object.Name | toLower }}Results, err := search{{ $object.Name | toPlural }}(ctx, query) +{{- else }} +func (r *queryResolver) Admin{{ $object.Name }}Search(ctx context.Context, query string) (*{{ $object.Name }}SearchResult, error) { + {{ $object.Name | toLower }}Results, err := adminSearch{{ $object.Name | toPlural }}(ctx, query) +{{- end}} + + if err != nil { + return nil, ErrSearchFailed + } + + // return the results + return &{{ $object.Name }}SearchResult{ + {{ $object.Name | toPlural }}: {{ $object.Name | toLower }}Results, + }, nil +} +{{- end }} \ No newline at end of file From 09759ac79034031f5baf5808368c9b9fab998f10 Mon Sep 17 00:00:00 2001 From: Sarah Funkhouser <147884153+golanglemonade@users.noreply.github.com> Date: Wed, 4 Sep 2024 12:42:10 -0600 Subject: [PATCH 2/3] update buildkite pipeline Signed-off-by: Sarah Funkhouser <147884153+golanglemonade@users.noreply.github.com> --- .buildkite/pipeline.yaml | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/.buildkite/pipeline.yaml b/.buildkite/pipeline.yaml index 0c9c60c..4db124f 100644 --- a/.buildkite/pipeline.yaml +++ b/.buildkite/pipeline.yaml @@ -3,28 +3,32 @@ env: IMAGE_REPO: ghcr.io/theopenlane/${APP_NAME} SONAR_HOST: "https://sonarcloud.io" steps: - - group: ":test_tube: tests" + - group: ":test_tube: Tests" key: "tests" steps: - label: ":golangci-lint: lint :lint-roller:" - cancel_on_build_failing: true key: "lint" + cancel_on_build_failing: true plugins: - docker#v5.11.0: - image: "registry.hub.docker.com/golangci/golangci-lint:latest-alpine" - command: ["golangci-lint", "run", "-v"] + image: "ghcr.io/theopenlane/build-image:latest" always-pull: true + command: ["task", "go:lint"] environment: - "GOTOOLCHAIN=auto" - label: ":golang: go test" key: "go_test" + cancel_on_build_failing: true plugins: - docker#v5.11.0: - image: golang:1.23.0 - command: ["go", "test", "-coverprofile=coverage.out", "./..."] + image: "ghcr.io/theopenlane/build-image:latest" + always-pull: true + command: ["task", "go:test:cover"] + environment: + - "GOTOOLCHAIN=auto" artifact_paths: ["coverage.out"] - group: ":closed_lock_with_key: Security Checks" - depends_on: "tests" + depends_on: "go_test" key: "security" steps: - label: ":closed_lock_with_key: gosec" @@ -33,6 +37,7 @@ steps: - docker#v5.11.0: image: "registry.hub.docker.com/securego/gosec:2.20.0" command: ["-no-fail", "-exclude-generated", "-fmt sonarqube", "-out", "results.txt", "./..."] + always-pull: true environment: - "GOTOOLCHAIN=auto" artifact_paths: ["results.txt"] @@ -47,7 +52,7 @@ steps: download: "coverage.out" step: "go_test" - docker#v5.11.0: - image: "sonarsource/sonar-scanner-cli:5" + image: "sonarsource/sonar-scanner-cli:11" environment: - "SONAR_TOKEN" - "SONAR_HOST_URL=$SONAR_HOST" @@ -63,7 +68,7 @@ steps: download: coverage.out step: "go_test" - docker#v5.11.0: - image: "sonarsource/sonar-scanner-cli:5" + image: "sonarsource/sonar-scanner-cli:11" environment: - "SONAR_TOKEN" - "SONAR_HOST_URL=$SONAR_HOST" From fbe28b0c8cb07fc9e92e6e36a3ec4d3237bafad3 Mon Sep 17 00:00:00 2001 From: Sarah Funkhouser <147884153+golanglemonade@users.noreply.github.com> Date: Wed, 4 Sep 2024 12:43:35 -0600 Subject: [PATCH 3/3] go:test:cover task Signed-off-by: Sarah Funkhouser <147884153+golanglemonade@users.noreply.github.com> --- Taskfile.yaml | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Taskfile.yaml b/Taskfile.yaml index 8fc5c91..4e61eb9 100644 --- a/Taskfile.yaml +++ b/Taskfile.yaml @@ -21,6 +21,19 @@ tasks: cmds: - go test -v ./... + go:test:cover: + desc: runs and outputs results of created go tests with coverage + aliases: [cover] + cmds: + - go test -v ./... -coverprofile=coverage.out + + go:test:coverout: + desc: runs and outputs results of created go tests with coverage + aliases: [cover] + cmds: + - task: go:test:cover + - go tool cover -html=coverage.out + go:tidy: desc: runs go mod tidy on the backend aliases: [tidy]