From 3f5c50501ec34aca4f0e06a6caed4f535f69198a Mon Sep 17 00:00:00 2001 From: saturn4er Date: Fri, 1 Feb 2019 12:16:55 +0200 Subject: [PATCH] Move DataLoaderConfig to swagger2gql plugin. Dataloader model now receive name and WaitDuration instead of Config --- generator/plugins/dataloader/config.go | 8 -------- .../plugins/dataloader/data_loader_generator.go | 8 +++++--- generator/plugins/dataloader/dataloader.go | 5 ++++- generator/plugins/dataloader/plugin.go | 2 +- generator/plugins/dataloader/templates.go | 4 ++-- .../dataloader/templates/loaders_body.gohtml | 8 ++++---- generator/plugins/proto2gql/dataloaders.go | 8 +++----- generator/plugins/swagger2gql/config.go | 14 +++++++++++--- generator/plugins/swagger2gql/plugin.go | 3 ++- generator/plugins/swagger2gql/services.go | 3 ++- 10 files changed, 34 insertions(+), 29 deletions(-) diff --git a/generator/plugins/dataloader/config.go b/generator/plugins/dataloader/config.go index 3511a47..cf11a25 100644 --- a/generator/plugins/dataloader/config.go +++ b/generator/plugins/dataloader/config.go @@ -1,17 +1,9 @@ package dataloader -import "time" - type DataLoadersConfig struct { OutputPath string `mapstructure:"output_path"` } -type ProviderConfig struct { - Name string `mapstructure:"name"` - WaitDuration time.Duration `mapstructure:"wait_duration"` - Slice bool -} - type FieldConfig struct { FieldName string `mapstructure:"field_name"` KeyFieldName string `mapstructure:"key_field_name"` diff --git a/generator/plugins/dataloader/data_loader_generator.go b/generator/plugins/dataloader/data_loader_generator.go index 23d563b..f5fb5b3 100644 --- a/generator/plugins/dataloader/data_loader_generator.go +++ b/generator/plugins/dataloader/data_loader_generator.go @@ -34,7 +34,8 @@ type Loader struct { RequestGoType graphql.GoType ResponseGoType graphql.GoType OutputGraphqlType graphql.TypeResolver - Config ProviderConfig + Name string + WaitDuration time.Duration } type LoaderGenerator struct { @@ -57,7 +58,7 @@ func (p *LoaderGenerator) GenerateDataLoaders() error { for _, dataLoader := range p.dataLoader.Loaders { if err := p.generateLoaders(dataLoader.InputGoType, dataLoader.OutputGoType, dataLoader.Slice); err != nil { - return errors.Wrapf(err, "failed to generate %s data loader", dataLoader.Config.Name) + return errors.Wrapf(err, "failed to generate %s data loader", dataLoader.Name) } } @@ -167,7 +168,8 @@ func (p *LoaderGenerator) generateBody() ([]byte, error) { FetchCode: dataLoaderModel.FetchCode(p.importer), RequestGoType: requestGoType, ResponseGoType: responseGoType, - Config: dataLoaderModel.Config, + Name: dataLoaderModel.Name, + WaitDuration: dataLoaderModel.WaitDuration, }) } diff --git a/generator/plugins/dataloader/dataloader.go b/generator/plugins/dataloader/dataloader.go index b6f257d..d4c9435 100644 --- a/generator/plugins/dataloader/dataloader.go +++ b/generator/plugins/dataloader/dataloader.go @@ -1,6 +1,8 @@ package dataloader import ( + "time" + "github.com/pkg/errors" "github.com/EGT-Ukraine/go2gql/generator/plugins/graphql" @@ -19,13 +21,14 @@ type Service struct { } type LoaderModel struct { + Name string + WaitDuration time.Duration Service *Service Method *graphql.Method InputGoType graphql.GoType OutputGoType graphql.GoType OutputGraphqlType graphql.TypeResolver FetchCode func(importer *importer.Importer) string - Config ProviderConfig Slice bool } diff --git a/generator/plugins/dataloader/plugin.go b/generator/plugins/dataloader/plugin.go index 81f400d..35844f6 100644 --- a/generator/plugins/dataloader/plugin.go +++ b/generator/plugins/dataloader/plugin.go @@ -25,7 +25,7 @@ type Plugin struct { } func (p *Plugin) AddLoader(loader LoaderModel) { - p.loaders[loader.Config.Name] = loader + p.loaders[loader.Name] = loader } func (p *Plugin) Prepare() error { diff --git a/generator/plugins/dataloader/templates.go b/generator/plugins/dataloader/templates.go index 90c4ec5..350e837 100644 --- a/generator/plugins/dataloader/templates.go +++ b/generator/plugins/dataloader/templates.go @@ -70,7 +70,7 @@ func (fi bindataFileInfo) Sys() interface{} { return nil } -var _templatesLoaders_bodyGohtml = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xac\x54\xc1\x8e\xda\x30\x10\x3d\xdb\x5f\x31\x87\x55\x45\x10\x24\x52\x8f\x48\x5c\xca\x76\x51\xd5\xaa\x87\xee\xb6\x7b\x58\xed\xc1\x4d\x86\x60\xad\xb1\x59\x67\x42\x41\x56\xfe\xbd\x72\xec\x40\x60\xb3\xe2\xb2\x27\x84\xe7\x79\xe6\xcd\x7b\xcf\x71\x6e\x0a\xd9\xb8\x34\x74\xd8\xe2\x0c\x4a\x49\xeb\xfa\x6f\x9a\x9b\x4d\xf6\x75\xf9\x30\xfd\xfd\x62\x85\xd4\x98\x95\xe6\x73\xf9\xaa\xb2\x12\x35\x5a\x41\xc6\x66\x5b\x55\x97\x52\x57\x59\x21\x48\x28\x23\x0a\xb4\xe9\x8f\xf6\xa7\xfa\x62\x8a\xc3\xc2\x68\xc2\x3d\x8d\x33\x98\x36\x0d\xe7\xbe\x35\x84\xf2\x42\x49\xd4\x54\x81\xd4\x84\x76\x25\x72\x04\xc7\x99\x73\x56\xe8\x12\xe1\xa6\x42\xbb\x93\x39\xc2\x6c\x0e\x37\xe9\x7d\xf8\x53\xb5\x3d\x18\x5b\x22\x39\xd7\x21\xd2\x9f\x62\x83\x4d\x13\xba\x8d\x12\x70\xae\x34\x0f\x7e\xca\x11\xb0\x10\x4a\x7d\xeb\x86\xf8\x06\xce\xa1\x2e\xda\x5e\x1d\xa3\x5b\x41\x22\x92\x86\x8a\x6c\x9d\xd3\x19\x99\xb0\x56\xe0\xd2\xc1\x02\x15\xe7\x62\x31\x5d\x18\xbd\x92\x65\x64\x13\x40\x70\xaa\x86\x03\xcf\xab\x0f\x18\xa2\x52\x9c\xa8\x44\xed\xbe\xe3\xa1\x5d\x28\x10\x73\x0d\xe7\x3b\x61\x87\x71\x30\x7f\xff\xbe\xbf\xb8\xaa\x75\x0e\x4b\xa4\x58\x79\x94\xb4\x8e\xd8\x51\x4e\x7b\xc8\xc3\x71\x1a\xcb\x13\x10\x5b\xd9\xd9\x74\x66\x5a\x72\x09\xf5\x72\xf5\x26\x7b\xa9\x3e\xf5\x44\xfd\x00\x31\x67\x90\x5b\x14\x84\xef\x80\x3c\xff\x3e\xdf\x34\x84\x24\x42\xef\x07\xb3\x92\x4c\xfa\x06\xb0\x86\x73\x66\x91\x6a\xab\x8f\xdb\x79\x81\xfe\x08\x55\x63\x68\x3f\xa8\xed\xd9\x71\xe2\x7d\xbc\xba\x6a\x6b\xc3\xd5\x75\xde\xda\x91\xb7\xc4\x7b\x11\xbf\x58\xef\x22\xe9\xc9\xd5\x00\x7a\xdb\xe2\xca\xd7\xa0\x8e\x33\xb6\x42\xca\xd7\x33\xf0\xf4\x47\x2f\x78\xa8\xde\x32\xf9\x85\xaf\x35\x56\xb4\x6c\x4f\x3d\x83\xd1\xd3\xf3\x00\xa8\xda\x1a\x5d\x61\x87\x9a\xc0\xd3\x33\x5a\x6b\x6c\xe2\xf9\xf4\x73\x70\xe7\x07\x2e\x4c\xd1\x3e\x5b\xd6\x4c\x38\x63\xff\x84\xa4\x19\x38\x57\xd4\x56\x90\x34\x1a\x2e\xf4\x7b\x14\x92\x6e\x63\xad\xf1\x37\xfc\xd3\x3a\xb9\x7c\x7c\x02\xbd\x74\xde\x59\xb3\x89\x1a\x0f\xe9\x9e\xc0\xb8\xff\x7d\x70\x9c\xed\x84\xf2\xa6\xe6\xb4\x4f\x43\x3a\x06\x83\x91\x70\xce\xe4\x0a\x3c\x78\x3e\x07\x2d\x55\xbb\x5d\x94\x5b\x4b\x75\x96\xb8\x9d\x50\xe9\xa8\x3f\xc7\x27\xe9\x7f\x00\x00\x00\xff\xff\x50\xe8\xec\xa9\x91\x05\x00\x00") +var _templatesLoaders_bodyGohtml = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xa4\x53\xc1\x6e\xda\x40\x10\x3d\xef\x7e\xc5\x1c\xa2\x0a\x23\xb0\xa5\x1e\x91\xb8\x94\x34\xa8\x6a\xd5\x43\x93\x36\x87\x28\x87\xad\x3d\x98\x55\x96\x5d\xb2\x1e\x53\xd0\xca\xff\x5e\xad\x77\x0d\x86\x38\xe2\x90\x13\x62\xe6\x79\xe6\xbd\x37\x6f\x9d\x9b\x42\x36\x2e\x0d\x1d\xb6\x38\x83\x52\xd2\xba\xfe\x9b\xe6\x66\x93\x7d\x5d\x3e\x4c\x7f\xbf\x58\x21\x35\x66\xa5\xf9\x5c\xbe\xaa\xac\x44\x8d\x56\x90\xb1\xd9\x56\xd5\xa5\xd4\x55\x56\x08\x12\xca\x88\x02\x6d\xfa\xa3\xfd\xa9\xbe\x98\xe2\xb0\x30\x9a\x70\x4f\xe3\x0c\xa6\x4d\xc3\xb9\x1f\x0d\xa1\xbd\x50\x12\x35\x55\x20\x35\xa1\x5d\x89\x1c\xc1\x71\xe6\x9c\x15\xba\x44\xb8\xa9\xd0\xee\x64\x8e\x30\x9b\xc3\x4d\x7a\x1f\xfe\x54\xed\x0c\xc6\x96\x48\xce\x75\x88\xf4\xa7\xd8\x60\xd3\x84\x69\xa3\x04\x9c\x2b\xcd\x83\xdf\x72\x04\x2c\x84\x52\xdf\xba\x25\x7e\x80\x73\xa8\x8b\x76\x56\xc7\xe8\x56\x90\x88\xa4\xa1\x22\x5b\xe7\x74\x46\x26\xc8\x0a\x5c\x3a\x58\xa0\xe2\x5c\x6c\x46\x1a\xa1\x0b\xa7\x72\x28\x78\x42\x7d\xc0\x10\x87\xe2\xc4\x21\x9a\xf6\x1d\x0f\xad\x92\xc0\xc8\x35\x9c\xef\x84\x1d\xc6\xc1\xfc\xfd\xef\xfd\x87\xab\x5a\xe7\xb0\x44\x8a\x9d\x47\x49\xeb\x88\x1d\xe5\xb4\x87\x3c\x94\xd3\xd8\x9e\x80\xd8\xca\xee\x3e\x67\xd7\x4a\x2e\xa1\xde\xa7\xde\x66\xef\xd1\xa7\x9e\x9b\x1f\x71\x71\x06\xb9\x45\x41\x78\xd9\xf5\x8c\xfb\x0c\xd3\x90\x87\x88\xb9\x1f\x8c\x45\x32\xe9\x5b\xce\x1a\xce\x99\x45\xaa\xad\x3e\xea\xf1\x96\xfc\x11\xaa\xc6\x30\x7e\xd0\xcd\xb3\x72\xe2\x2f\x77\x55\x5c\x6b\xfc\xfb\x3a\xde\x3a\x9f\xb7\x8c\x7b\x31\xbe\xd0\x75\x91\xe6\xe4\x6a\xd6\xfc\x85\xa2\xd6\x6b\x50\xc7\x19\x5b\x21\xe5\xeb\x19\x78\xde\xa3\x17\x3c\x54\x6f\x99\xfc\xc2\xd7\x1a\x2b\x5a\xb6\x55\xcf\x60\xf4\xf4\x3c\x00\xaa\xb6\x46\x57\xd8\xa1\x26\xf0\xf4\x8c\xd6\x1a\x9b\x78\x3e\xfd\x93\xdf\xf9\x85\x0b\x53\xb4\x4f\x93\x35\x13\xce\xd8\x3f\x21\x69\x06\xce\x15\xb5\x15\x24\x8d\x3e\x4e\x7d\x14\x92\x6e\x63\xb1\xf1\x50\xff\x7c\x4e\x77\x3d\xc6\xbc\x97\xc0\x3b\x6b\x36\xd1\xdc\x21\xc3\x13\x18\xf7\x1f\xbf\xe3\x6c\x27\x94\x3f\x63\x4e\xfb\x34\xe4\x61\x30\x0a\x09\xe7\x4c\xae\xc0\x83\xe7\x73\xd0\x52\xb5\xb2\xa2\xcf\x5a\xaa\xb3\x8c\xed\x84\x4a\x47\xfd\x3d\x3e\x3b\xff\x03\x00\x00\xff\xff\x52\x5c\x78\x4e\x6e\x05\x00\x00") func templatesLoaders_bodyGohtmlBytes() ([]byte, error) { return bindataRead( @@ -85,7 +85,7 @@ func templatesLoaders_bodyGohtml() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "templates/loaders_body.gohtml", size: 1425, mode: os.FileMode(420), modTime: time.Unix(1548953658, 0)} + info := bindataFileInfo{name: "templates/loaders_body.gohtml", size: 1390, mode: os.FileMode(420), modTime: time.Unix(1549016074, 0)} a := &asset{bytes: bytes, info: info} return a, nil } diff --git a/generator/plugins/dataloader/templates/loaders_body.gohtml b/generator/plugins/dataloader/templates/loaders_body.gohtml index 8f1c994..0426769 100644 --- a/generator/plugins/dataloader/templates/loaders_body.gohtml +++ b/generator/plugins/dataloader/templates/loaders_body.gohtml @@ -8,7 +8,7 @@ type LoaderClients interface { type DataLoaders struct { {{range $loader := $.Loaders -}} - {{$loader.Config.Name}}Loader {{$loader.LoaderTypeName}}Loader + {{$loader.Name}}Loader {{$loader.LoaderTypeName}}Loader {{end -}} } @@ -19,7 +19,7 @@ var dataLoadersContextKey = dataLoadersContextKeyType{} func GetContextWithLoaders(ctx context.Context, apiClients LoaderClients) context.Context { dataLoaders := &DataLoaders{ {{range $loader := $.Loaders -}} - {{$loader.Config.Name}}Loader: create{{$loader.Config.Name}}(ctx, apiClients.Get{{$loader.Service.Name}}Client()), + {{$loader.Name}}Loader: create{{$loader.Name}}(ctx, apiClients.Get{{$loader.Service.Name}}Client()), {{end -}} } @@ -27,12 +27,12 @@ func GetContextWithLoaders(ctx context.Context, apiClients LoaderClients) contex } {{range $loader := $.Loaders -}} -func create{{$loader.Config.Name}}(ctx context.Context, client {{goType $loader.Service.CallInterface}}) {{$loader.LoaderTypeName}}Loader { +func create{{$loader.Name}}(ctx context.Context, client {{goType $loader.Service.CallInterface}}) {{$loader.LoaderTypeName}}Loader { return {{$loader.LoaderTypeName}}Loader{ fetch: func(keys {{goType $loader.RequestGoType}}) ([]{{goType $loader.ResponseGoType}}, []error) { {{$loader.FetchCode}} }, - wait: {{duration $loader.Config.WaitDuration}}, + wait: {{duration $loader.WaitDuration}}, } } {{end -}} diff --git a/generator/plugins/proto2gql/dataloaders.go b/generator/plugins/proto2gql/dataloaders.go index 1d3280e..b4f703c 100644 --- a/generator/plugins/proto2gql/dataloaders.go +++ b/generator/plugins/proto2gql/dataloaders.go @@ -90,11 +90,9 @@ func (g Proto2GraphQL) registerMethodDataLoader(name string, cfg DataLoaderConfi }, OutputGoType: responseGoType, OutputGraphqlType: dataLoaderOutType, - Config: dataloader.ProviderConfig{ - Name: name, - WaitDuration: cfg.WaitDuration, - }, - Slice: cfg.Type == DataLoaderType1ToN, + Name: name, + WaitDuration: cfg.WaitDuration, + Slice: cfg.Type == DataLoaderType1ToN, } g.DataLoaderPlugin.AddLoader(dataLoaderProvider) diff --git a/generator/plugins/swagger2gql/config.go b/generator/plugins/swagger2gql/config.go index 305046b..53cd751 100644 --- a/generator/plugins/swagger2gql/config.go +++ b/generator/plugins/swagger2gql/config.go @@ -2,6 +2,7 @@ package swagger2gql import ( "regexp" + "time" "github.com/pkg/errors" @@ -18,10 +19,17 @@ type ObjectConfig struct { } type MethodConfig struct { - Alias string `mapstructure:"alias"` - RequestType string `mapstructure:"request_type"` // QUERY | MUTATION - DataLoaderProvider dataloader.ProviderConfig `mapstructure:"data_loader_provider"` + Alias string `mapstructure:"alias"` + RequestType string `mapstructure:"request_type"` // QUERY | MUTATION + DataLoaderProvider ProviderConfig `mapstructure:"data_loader_provider"` } + +type ProviderConfig struct { + Name string `mapstructure:"name"` + WaitDuration time.Duration `mapstructure:"wait_duration"` + Slice bool +} + type TagConfig struct { ClientGoPackage string `mapstructure:"client_go_package"` ServiceName string `mapstructure:"service_name"` diff --git a/generator/plugins/swagger2gql/plugin.go b/generator/plugins/swagger2gql/plugin.go index 96927f5..b2f19d6 100644 --- a/generator/plugins/swagger2gql/plugin.go +++ b/generator/plugins/swagger2gql/plugin.go @@ -11,6 +11,7 @@ import ( "github.com/EGT-Ukraine/go2gql/generator" "github.com/EGT-Ukraine/go2gql/generator/plugins/dataloader" "github.com/EGT-Ukraine/go2gql/generator/plugins/graphql" + "github.com/EGT-Ukraine/go2gql/generator/plugins/graphql/lib/pluginconfig" "github.com/EGT-Ukraine/go2gql/generator/plugins/swagger2gql/parser" ) @@ -60,7 +61,7 @@ func (p *Plugin) Init(config *generator.GenerateConfig, plugins []generator.Plug func (p *Plugin) parseImports() error { for _, pluginsConfigsImports := range p.generateConfig.PluginsConfigsImports { configs := new([]*SwaggerFileConfig) - if err := mapstructure.Decode(pluginsConfigsImports.PluginsConfigs[PluginImportConfigKey], configs); err != nil { + if err := pluginconfig.Decode(pluginsConfigsImports.PluginsConfigs[PluginImportConfigKey], configs); err != nil { return errors.Wrap(err, "failed to decode config") } diff --git a/generator/plugins/swagger2gql/services.go b/generator/plugins/swagger2gql/services.go index 4318162..3388075 100644 --- a/generator/plugins/swagger2gql/services.go +++ b/generator/plugins/swagger2gql/services.go @@ -160,6 +160,8 @@ func (p *Plugin) addDataLoaderProvider( } dataLoaderProvider := dataloader.LoaderModel{ + Name: dataLoaderProviderConfig.Name, + WaitDuration: dataLoaderProviderConfig.WaitDuration, Service: &dataloader.Service{ Name: p.tagName(tag, &tagCfg), CallInterface: p.serviceCallInterface(&tagCfg), @@ -193,7 +195,6 @@ func (p *Plugin) addDataLoaderProvider( InputGoType: inputArgumentGoType, OutputGoType: responseGoType, OutputGraphqlType: dataLoaderOutType, - Config: dataLoaderProviderConfig, Slice: dataLoaderProviderConfig.Slice, }