Skip to content

Commit

Permalink
Fix linter + CR suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
saturn4er committed Feb 1, 2019
1 parent de1208c commit b313ae6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 19 deletions.
2 changes: 1 addition & 1 deletion generator/plugins/dataloader/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type DataLoadersConfig struct {

type ProviderConfig struct {
Name string `mapstructure:"name"`
WaitDuration time.Duration `mapstructure:"wait_duration_ms"`
WaitDuration time.Duration `mapstructure:"wait_duration"`
Slice bool
}

Expand Down
48 changes: 31 additions & 17 deletions generator/plugins/proto2gql/dataloaders.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,21 @@ func (g Proto2GraphQL) registerMethodDataLoader(name string, cfg DataLoaderConfi

var fetchCode func(importer *importer.Importer) string
if cfg.Type == DataLoaderType1ToN {
fetchCode = g.oneToNDataLoaderFetchCode(file, cfg, method)
fetchCode, err = g.oneToNDataLoaderFetchCode(file, cfg, method)
if err != nil {
return errors.Wrap(err, "failed to resolve 1-N data loader fetch code")
}
responseCopy := responseGoType
responseGoType = graphql.GoType{
Kind: reflect.Slice,
ElemType: &responseCopy,
}
dataLoaderOutType = graphql.GqlListTypeResolver(dataLoaderOutType)
} else {
fetchCode = g.oneToOneDataLoaderFetchCode(file, cfg, method)
fetchCode, err = g.oneToOneDataLoaderFetchCode(file, cfg, method)
if err != nil {
return errors.Wrap(err, "failed to resolve 1-1 data loader fetch code")
}
}

matchFieldGoType, err := g.goTypeByParserType(matchField.GetType())
Expand Down Expand Up @@ -158,7 +164,16 @@ func (g Proto2GraphQL) validateDataLoader(cfg DataLoaderConfig, method *parser.M
return nil
}

func (g Proto2GraphQL) oneToOneDataLoaderFetchCode(file *parsedFile, cfg DataLoaderConfig, method *parser.Method) func(importer *importer.Importer) string {
func (g Proto2GraphQL) oneToOneDataLoaderFetchCode(file *parsedFile, cfg DataLoaderConfig, method *parser.Method) (func(importer *importer.Importer) string, error) {
resultField, err := messageFieldByPath(method.OutputMessage, cfg.ResultField)
if err != nil {
return nil, errors.Wrap(err, "failed to get result message field by path")
}
responseGoType, err := g.goTypeByParserType(resultField.GetType())
if err != nil {
return nil, errors.Wrap(err, "failed to resolve go type by result field parser type")
}

return func(importer *importer.Importer) string {
filledRequest, err := g.getMessageWithFilledField(file, importer, method.InputMessage, strings.Split(cfg.RequestField, "."), "keys")
if err != nil {
Expand All @@ -167,11 +182,6 @@ func (g Proto2GraphQL) oneToOneDataLoaderFetchCode(file *parsedFile, cfg DataLoa

responseFieldAccessor := buildFieldAccessorByFieldPath(cfg.ResultField)
matchFieldAccessor := buildFieldAccessorByFieldPath(cfg.MatchField)
responseField, err := messageFieldByPath(method.OutputMessage, cfg.ResultField)
if err != nil {
panic("failed to access output message field by path")
}
responseGoType, err := g.goTypeByParserType(responseField.GetType())

return `response, err := client.` + method.Name + `(ctx, ` + filledRequest + `)
if err != nil{
Expand All @@ -188,10 +198,19 @@ func (g Proto2GraphQL) oneToOneDataLoaderFetchCode(file *parsedFile, cfg DataLoa
}
return result, nil`
}
}, nil
}

func (g Proto2GraphQL) oneToNDataLoaderFetchCode(file *parsedFile, cfg DataLoaderConfig, method *parser.Method) func(importer *importer.Importer) string {
func (g Proto2GraphQL) oneToNDataLoaderFetchCode(file *parsedFile, cfg DataLoaderConfig, method *parser.Method) (func(importer *importer.Importer) string, error) {
resultField, err := messageFieldByPath(method.OutputMessage, cfg.ResultField)
if err != nil {
return nil, errors.Wrap(err, "failed to get result message field by path")
}
responseGoType, err := g.goTypeByParserType(resultField.GetType())
if err != nil {
return nil, errors.Wrap(err, "failed to resolve go type by result field parser type")
}

return func(importer *importer.Importer) string {
filledRequest, err := g.getMessageWithFilledField(file, importer, method.InputMessage, strings.Split(cfg.RequestField, "."), "keys")
if err != nil {
Expand All @@ -200,11 +219,6 @@ func (g Proto2GraphQL) oneToNDataLoaderFetchCode(file *parsedFile, cfg DataLoade

responseFieldAccessor := buildFieldAccessorByFieldPath(cfg.ResultField)
matchFieldAccessor := buildFieldAccessorByFieldPath(cfg.MatchField)
responseField, err := messageFieldByPath(method.OutputMessage, cfg.ResultField)
if err != nil {
panic("failed to access output message field by path")
}
responseGoType, err := g.goTypeByParserType(responseField.GetType())

return `response, err := client.` + method.Name + `(ctx, ` + filledRequest + `)
if err != nil{
Expand All @@ -220,7 +234,7 @@ func (g Proto2GraphQL) oneToNDataLoaderFetchCode(file *parsedFile, cfg DataLoade
}
return result, nil`
}
}, nil
}

func messageFieldByPath(message *parser.Message, path string) (parser.Field, error) {
Expand Down Expand Up @@ -266,7 +280,7 @@ func (g Proto2GraphQL) getMessageWithFilledField(file *parsedFile, importer *imp

field, ok := msg.GetFieldByName(pathParts[0])
if !ok {
return "", errors.New("can't find field ") // TOOD
return "", errors.New("can't find field ")
}
if len(pathParts) == 1 {
typ, err := g.goTypeByParserType(msg)
Expand Down
2 changes: 1 addition & 1 deletion generator/plugins/proto2gql/messages_outputs.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (g *Proto2GraphQL) dataLoaderFields(configs []dataloader.FieldConfig, msg *
}

field := &graphql.DataLoaderField{
Name: cfg.FieldName,
Name: cfg.FieldName,
NormalizedParentKeyFieldName: camelCase(cfg.KeyFieldName),
ParentKeyFieldName: cfg.KeyFieldName,
KeyFieldSlice: normalKeyField.Repeated,
Expand Down

0 comments on commit b313ae6

Please sign in to comment.