Skip to content

Commit

Permalink
Merge 14070e4 into 19a280b
Browse files Browse the repository at this point in the history
  • Loading branch information
damour committed Jan 24, 2019
2 parents 19a280b + 14070e4 commit 6fe483b
Show file tree
Hide file tree
Showing 51 changed files with 211 additions and 228 deletions.
22 changes: 22 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
linters:
enable-all: true
disable:
- gochecknoglobals
- scopelint
- prealloc
- maligned
- unparam
- megacheck
- nakedret

linters-settings:
goimports:
local-prefixes: github.com/EGT-Ukraine/go2gql
gocyclo:
min-complexity: 20 # todo: decrease
lll:
line-length: 180 # todo: decrease
golint:
min-confidence: 0
dupl:
threshold: 260 # todo: decrease
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ install:
- curl -L https://github.com/google/protobuf/releases/download/v3.6.1/protoc-3.6.1-linux-x86_64.zip -o /tmp/protoc.zip
- unzip /tmp/protoc.zip -d "$HOME"/protoc
- mkdir -p "$HOME"/src && ln -s "$HOME"/protoc "$HOME"/src/protobuf
- curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(go env GOPATH)/bin v1.13.1

env:
- PATH=$HOME/protoc/bin:$PATH

script:
- make test
- make lint
- goveralls -race -service=travis-ci -repotoken $COVERALLS_TOKEN
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ test:
$(MAKE) -C tests
go test ./...

lint:
golangci-lint run -v

.PHONY: install
2 changes: 1 addition & 1 deletion api/multipart_file/file.go → api/multipartfile/file.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package multipart_file
package multipartfile

import (
"mime/multipart"
Expand Down
28 changes: 14 additions & 14 deletions api/scalars/scalars.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/graphql-go/graphql/language/ast"
"github.com/graphql-go/graphql/language/kinds"

"github.com/EGT-Ukraine/go2gql/api/multipart_file"
"github.com/EGT-Ukraine/go2gql/api/multipartfile"
)

var GraphQLInt64Scalar = graphql.NewScalar(graphql.ScalarConfig{
Expand All @@ -23,7 +23,7 @@ var GraphQLInt64Scalar = graphql.NewScalar(graphql.ScalarConfig{
if val == nil {
return nil
}
return int64(*val)
return *val
case int32:
return int64(val)
case *int32:
Expand Down Expand Up @@ -88,7 +88,7 @@ var GraphQLInt32Scalar = graphql.NewScalar(graphql.ScalarConfig{
if val == nil {
return nil
}
return int32(*val)
return *val
case int:
return int32(val)
case *int:
Expand All @@ -114,7 +114,7 @@ var GraphQLInt32Scalar = graphql.NewScalar(graphql.ScalarConfig{
if val == nil {
return nil
}
return int32(*val)
return *val
case int64:
return int32(val)
case *int64:
Expand Down Expand Up @@ -156,7 +156,7 @@ var GraphQLUInt64Scalar = graphql.NewScalar(graphql.ScalarConfig{
if val == nil {
return nil
}
return uint64(*val)
return *val
case uint32:
return uint64(val)
case *uint32:
Expand Down Expand Up @@ -221,14 +221,14 @@ var GraphQLUInt32Scalar = graphql.NewScalar(graphql.ScalarConfig{
if val == nil {
return nil
}
return uint32(*val)
return *val
case uint:
return uint32(val)
case *uint:
if val == nil {
return nil
}
return uint(*val)
return *val
}

return nil
Expand Down Expand Up @@ -330,7 +330,7 @@ var GraphQLFloat64Scalar = graphql.NewScalar(graphql.ScalarConfig{
if err != nil {
return nil
}
return float64(value)
return value
case *string:
if val == nil {
return nil
Expand All @@ -339,7 +339,7 @@ var GraphQLFloat64Scalar = graphql.NewScalar(graphql.ScalarConfig{
if err != nil {
return nil
}
return float64(value)
return value
case float32:
return float64(val)
case *float32:
Expand All @@ -366,7 +366,7 @@ var GraphQLFloat64Scalar = graphql.NewScalar(graphql.ScalarConfig{
if err != nil {
return nil
}
return float64(val)
return val
}

return nil
Expand Down Expand Up @@ -426,19 +426,19 @@ var MultipartFile = graphql.NewScalar(graphql.ScalarConfig{
Description: "The `Upload` scalar type represents no data.",
Serialize: func(value interface{}) interface{} {
switch t := value.(type) {
case multipart_file.MultipartFile:
case multipartfile.MultipartFile:
return t.Header.Filename
case *multipart_file.MultipartFile:
case *multipartfile.MultipartFile:
return t.Header.Filename
}

return nil
},
ParseValue: func(value interface{}) interface{} {
switch t := value.(type) {
case multipart_file.MultipartFile:
case multipartfile.MultipartFile:
return &t
case *multipart_file.MultipartFile:
case *multipartfile.MultipartFile:
return t
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/go2gql/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"github.com/pkg/errors"
"github.com/urfave/cli"
"gopkg.in/yaml.v2"
yaml "gopkg.in/yaml.v2"

"github.com/EGT-Ukraine/go2gql/generator"
)
Expand Down
2 changes: 1 addition & 1 deletion cmd/go2gql/plugins_with_external.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func Plugins(c *cli.Context) []generator.Plugin {
return res
}

func init() {
func init() { //nolint:gochecknoinits
appFlags = append(appFlags, cli.StringFlag{
Name: "plugins, p",
Usage: "Plugins directory",
Expand Down
15 changes: 12 additions & 3 deletions example/simple_plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,18 @@ func (p plugin) Generate() error {
if len(service.QueryMethods) == 0 {
continue
}
file.WriteString(service.Name + ":\n")
_, err := file.WriteString(service.Name + ":\n")

if err != nil {
return errors.Wrap(err, "failed to write to file")
}

for _, method := range service.QueryMethods {
file.WriteString(" " + method.Name + ":\n")
_, err := file.WriteString(" " + method.Name + ":\n")

if err != nil {
return errors.Wrap(err, "failed to write to file")
}
}
}
}
Expand All @@ -49,7 +58,7 @@ func (plugin) Prepare() error { return nil }
func (plugin) PrintInfo(info generator.Infos) {}
func (plugin) Infos() map[string]string { return nil }

func Plugin() generator.Plugin {
func Plugin() generator.Plugin { //nolint:deadcode
return new(plugin)
}

Expand Down
2 changes: 1 addition & 1 deletion generator/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"path/filepath"

"github.com/pkg/errors"
"gopkg.in/yaml.v2"
yaml "gopkg.in/yaml.v2"
)

type ImportedPluginsConfigs struct {
Expand Down
4 changes: 2 additions & 2 deletions generator/plugins/dataloader/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package dataloader
type DataLoadersConfig struct {
OutputPath string `mapstructure:"output_path"`
}
type DataLoaderProviderConfig struct {
type ProviderConfig struct {
Name string `mapstructure:"name"`
WaitDurationMs int `mapstructure:"wait_duration_ms"`
}
type DataLoaderFieldConfig struct {
type FieldConfig struct {
FieldName string `mapstructure:"field_name"`
KeyFieldName string `mapstructure:"key_field_name"`
DataLoader string `mapstructure:"data_loader_name"`
Expand Down
5 changes: 3 additions & 2 deletions generator/plugins/dataloader/data_loader_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dataloader
import (
"bytes"
"fmt"
"io"
"os"
"reflect"
"text/template"
Expand Down Expand Up @@ -32,7 +33,7 @@ type Loader struct {
RequestGoType graphql.GoType
ResponseGoType graphql.GoType
OutputGraphqlType graphql.TypeResolver
Config DataLoaderProviderConfig
Config ProviderConfig
}

type LoaderGenerator struct {
Expand Down Expand Up @@ -201,7 +202,7 @@ func (p *LoaderGenerator) generateHead() ([]byte, error) {
return buf.Bytes(), nil
}

func (p *LoaderGenerator) renderLoaders(out *os.File) error {
func (p *LoaderGenerator) renderLoaders(out io.Writer) error {
body, err := p.generateBody()
if err != nil {
return errors.Wrap(err, "failed to generate body")
Expand Down
4 changes: 2 additions & 2 deletions generator/plugins/dataloader/dataloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ type LoaderModel struct {
OutputGoType graphql.GoType
OutputGraphqlType graphql.TypeResolver
FetchCode func(importer *importer.Importer) string
Config DataLoaderProviderConfig
Config ProviderConfig
}

func (p *Plugin) createDataLoader(config *DataLoadersConfig, vendorPath string, files map[string]*graphql.TypesFile) (*DataLoader, error) {
func (p *Plugin) createDataLoader(config *DataLoadersConfig, vendorPath string) (*DataLoader, error) {
if config == nil {
return nil, nil
}
Expand Down
7 changes: 3 additions & 4 deletions generator/plugins/dataloader/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (p *Plugin) validateOutputObjects(gqlFiles map[string]*graphql.TypesFile) e

if dataLoader.InputGoType.ElemType.Kind != outputArgument.GoType.Kind {
// TODO: use type casting if possible.
return errors.New("Input argument must be same type as output")
return errors.New("input argument must be same type as output")
}
}
}
Expand All @@ -129,8 +129,7 @@ func (p *Plugin) Generate() error {
return nil
}

gqlFiles := p.gqlPlugin.Types()
dataLoader, err := p.createDataLoader(p.dataLoaderConfigs, p.generateCfg.VendorPath, gqlFiles)
dataLoader, err := p.createDataLoader(p.dataLoaderConfigs, p.generateCfg.VendorPath)

if err != nil {
return errors.Wrap(err, "failed to process dataloader config")
Expand All @@ -142,7 +141,7 @@ func (p *Plugin) Generate() error {
dataLoader: p.dataLoader,
})

if err := p.validateOutputObjects(gqlFiles); err != nil {
if err := p.validateOutputObjects(p.gqlPlugin.Types()); err != nil {
return errors.Wrap(err, "failed to validate graphql files")
}

Expand Down
2 changes: 1 addition & 1 deletion generator/plugins/graphql/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ type gqlObject struct {
}

func (gqlObject *gqlObject) TypeName() string {
if gqlObject.QueryObject == true {
if gqlObject.QueryObject {
return "Query"
}

Expand Down
8 changes: 4 additions & 4 deletions generator/plugins/graphql/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,15 @@ func (p *Plugin) parseImportedSchema(cfg *SchemaConfig) error {
schema := p.findSchemaByName(importedSchemaName)

if schema == nil {
return errors.New("Schema " + importedSchemaName + " not defined")
return errors.New("schema " + importedSchemaName + " not defined")
}

if cfg.Queries != nil && schema.Queries.Type == SchemaNodeTypeService {
return errors.New("Cannot merge object with service in query")
return errors.New("cannot merge object with service in query")
}

if cfg.Mutations != nil && schema.Mutations.Type == SchemaNodeTypeService {
return errors.New("Cannot merge object with service in mutations")
return errors.New("cannot merge object with service in mutations")
}

if cfg.Queries != nil {
Expand Down Expand Up @@ -221,7 +221,7 @@ func (p *Plugin) SchemasObjects() ([]SchemaObjects, error) {
if err != nil {
return nil, errors.Wrapf(err, "failed to resolve schema %s output go package", schema.Name)
}
parser := NewSchemaParser(schema, p.files)
parser := newSchemaParser(schema, p.files)

schemaContext, err := parser.SchemaObjects()

Expand Down
12 changes: 7 additions & 5 deletions generator/plugins/graphql/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ graphql_schemas:
Convey("When the graphql plugin is initialized", func() {
graphqlPlugin := new(Plugin)

graphqlPlugin.Init(gc, []generator.Plugin{})
if err := graphqlPlugin.Init(gc, []generator.Plugin{}); err != nil {
t.Fatalf(err.Error())
}

Convey("Graphql schema should be merged recursively", func() {
So(graphqlPlugin.schemaConfigs[0], ShouldResemble, SchemaConfig{
Expand All @@ -70,23 +72,23 @@ graphql_schemas:
Queries: &SchemaNodeConfig{
Type: "OBJECT",
Fields: []SchemaNodeConfig{
SchemaNodeConfig{
{
Type: "OBJECT",
ObjectName: "Common",
Field: "common",
Fields: []SchemaNodeConfig{
SchemaNodeConfig{
{
Type: "OBJECT",
ObjectName: "CommonNested",
Field: "commonNested",
Fields: []SchemaNodeConfig{
SchemaNodeConfig{
{
Type: "SERVICE",
Service: "Service1",
ObjectName: "Service1Object",
Field: "service1Field",
},
SchemaNodeConfig{
{
Type: "SERVICE",
Service: "Service2",
ObjectName: "Service2Object",
Expand Down
4 changes: 2 additions & 2 deletions generator/plugins/graphql/schema_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type schemaParser struct {
types map[string]*TypesFile
}

func NewSchemaParser(schemaCfg SchemaConfig, types map[string]*TypesFile) *schemaParser {
func newSchemaParser(schemaCfg SchemaConfig, types map[string]*TypesFile) *schemaParser {
return &schemaParser{schemaCfg, types}
}

Expand Down Expand Up @@ -70,7 +70,7 @@ func (g *schemaParser) resolveObjectFields(nodeCfg SchemaNodeConfig, object *gql

var serviceMethods []Method

if object.QueryObject == true {
if object.QueryObject {
serviceMethods = service.QueryMethods
} else {
serviceMethods = service.MutationMethods
Expand Down
Loading

0 comments on commit 6fe483b

Please sign in to comment.