Skip to content

Commit

Permalink
gometalinter pass
Browse files Browse the repository at this point in the history
  • Loading branch information
vektah committed Feb 5, 2018
1 parent 9ab81d6 commit a9352e3
Show file tree
Hide file tree
Showing 13 changed files with 73 additions and 1,942 deletions.
13 changes: 13 additions & 0 deletions .gometalinter.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"sort": ["path"],
"MinConfidence": 10,
"Linters": {
"errcheck": {
"Command": "errcheck -abspath -ignore '[rR]ead|[wW]rite'",
"Pattern": "PATH:LINE:COL:MESSAGE",
"InstallFrom": "github.com/kisielk/errcheck",
"PartitionStrategy": "packages"
}
},
"Disable": ["gas","golint","gocyclo"]
}
5 changes: 3 additions & 2 deletions cmd/ggraphqlc/dumper.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ func (w *writer) writeVars() {
}

func (w *writer) writeObjectResolver(object object) {
w.line("// nolint: gocyclo, errcheck, gas, goconst")
w.begin("func _%s(ec *executionContext, sel []query.Selection, it *%s) {", lcFirst(object.Type.GraphQLName), object.Type.Local())

w.line("groupedFieldSet := ec.collectFields(sel, %sSatisfies, map[string]bool{})", lcFirst(object.Type.GraphQLName))
Expand Down Expand Up @@ -215,11 +216,11 @@ func getFuncArgs(object object, field Field) string {
return strings.Join(args, ", ")
}

func (w *writer) writeJsonType(t Type, val string) {
func (w *writer) writeJsonType(t kind, val string) {
w.doWriteJsonType(t, val, t.Modifiers, false)
}

func (w *writer) doWriteJsonType(t Type, val string, remainingMods []string, isPtr bool) {
func (w *writer) doWriteJsonType(t kind, val string, remainingMods []string, isPtr bool) {
for i := 0; i < len(remainingMods); i++ {
switch remainingMods[i] {
case modPtr:
Expand Down
6 changes: 5 additions & 1 deletion cmd/ggraphqlc/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ import (
)

func (w *writer) writeExec() {
execTemplate.Execute(w.out, map[string]interface{}{
err := execTemplate.Execute(w.out, map[string]interface{}{
"RootQuery": "_" + lcFirst(w.QueryRoot),
"RootMutation": "_" + lcFirst(w.MutationRoot),
})
if err != nil {
panic(err)
}
}

// some very static code bundled into the same binary. The execution context has the custom interface in it
Expand Down Expand Up @@ -167,6 +170,7 @@ type collectedField struct {
Selections []query.Selection
}
// nolint: deadcode, megacheck
func unpackComplexArg(result interface{}, data interface{}) error {
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
TagName: "graphql",
Expand Down
48 changes: 24 additions & 24 deletions cmd/ggraphqlc/extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ func (e *extractor) errorf(format string, args ...interface{}) {

// getType to put in a file for a given fully resolved type, and add any Imports required
// eg name = github.com/my/pkg.myType will return `pkg.myType` and add an import for `github.com/my/pkg`
func (e *extractor) getType(name string) Type {
func (e *extractor) getType(name string) kind {
if fieldType, ok := e.goTypeMap[name]; ok {
parts := strings.Split(fieldType, ".")
if len(parts) == 1 {
return Type{
return kind{
GraphQLName: name,
Name: parts[0],
}
Expand All @@ -55,7 +55,7 @@ func (e *extractor) getType(name string) Type {
}

e.Imports[localName] = packageName
return Type{
return kind{
GraphQLName: name,
ImportedAs: localName,
Name: typeName,
Expand All @@ -75,13 +75,13 @@ func (e *extractor) getType(name string) Type {
fmt.Fprintf(os.Stderr, "unknown go type for %s, using interface{}. you should add it to types.json\n", name)
}
e.goTypeMap[name] = "interface{}"
return Type{
return kind{
GraphQLName: name,
Name: "interface{}",
}
}

func (e *extractor) buildType(t common.Type) Type {
func (e *extractor) buildType(t common.Type) kind {
var modifiers []string
usePtr := true
for {
Expand Down Expand Up @@ -119,7 +119,7 @@ func (e *extractor) buildType(t common.Type) Type {
default:
panic(fmt.Errorf("unknown scalar %s", val.Name))
}
return Type{
return kind{
Basic: true,
Modifiers: modifiers,
GraphQLName: val.Name,
Expand Down Expand Up @@ -159,7 +159,7 @@ func (e *extractor) buildType(t common.Type) Type {
t.Modifiers = modifiers
return t
case *schema.Enum:
return Type{
return kind{
Basic: true,
Modifiers: modifiers,
GraphQLName: val.Name,
Expand All @@ -177,31 +177,31 @@ func (e *extractor) extract() {
if !ok {
continue
}
object := object{
obj := object{
Name: schemaObject.Name,
Type: e.getType(schemaObject.Name),
}

for _, i := range schemaObject.Interfaces {
object.satisfies = append(object.satisfies, i.Name)
obj.satisfies = append(obj.satisfies, i.Name)
}

for _, field := range schemaObject.Fields {
var args []Arg
var args []FieldArgument
for _, arg := range field.Args {
args = append(args, Arg{
args = append(args, FieldArgument{
Name: arg.Name.Name,
Type: e.buildType(arg.Type),
})
}

object.Fields = append(object.Fields, Field{
obj.Fields = append(obj.Fields, Field{
GraphQLName: field.Name,
Type: e.buildType(field.Type),
Args: args,
})
}
e.Objects = append(e.Objects, object)
e.Objects = append(e.Objects, obj)
}

for name, typ := range e.schema.EntryPoints {
Expand Down Expand Up @@ -283,7 +283,7 @@ func (e *extractor) findBindTargets(t types.Type, object object) bool {

// check arg order matches code, not gql

var newArgs []Arg
var newArgs []FieldArgument
l2:
for j := 0; j < sig.Params().Len(); j++ {
param := sig.Params().At(j)
Expand Down Expand Up @@ -332,36 +332,36 @@ const (
modPtr = "*"
)

type Type struct {
type kind struct {
GraphQLName string
Name string
Package string
ImportedAs string
Modifiers []string
Implementors []Type
Implementors []kind
Basic bool
}

func (t Type) Local() string {
func (t kind) Local() string {
if t.ImportedAs == "" {
return strings.Join(t.Modifiers, "") + t.Name
}
return strings.Join(t.Modifiers, "") + t.ImportedAs + "." + t.Name
}

func (t Type) Ptr() Type {
func (t kind) Ptr() kind {
t.Modifiers = append(t.Modifiers, modPtr)
return t
}

func (t Type) IsPtr() bool {
func (t kind) IsPtr() bool {
return len(t.Modifiers) > 0 && t.Modifiers[0] == modPtr
}

type object struct {
Name string
Fields []Field
Type Type
Type kind
satisfies []string
Root bool
}
Expand All @@ -370,8 +370,8 @@ type Field struct {
GraphQLName string
MethodName string
VarName string
Type Type
Args []Arg
Type kind
Args []FieldArgument
NoErr bool
}

Expand All @@ -393,7 +393,7 @@ func (e *extractor) GetObject(name string) *object {
return nil
}

type Arg struct {
type FieldArgument struct {
Name string
Type Type
Type kind
}
4 changes: 2 additions & 2 deletions cmd/ggraphqlc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ func main() {
GraphQLName: "__type",
NoErr: true,
MethodName: "ec.introspectType",
Args: []Arg{{Name: "name", Type: Type{Basic: true, Name: "string"}}},
Args: []FieldArgument{{Name: "name", Type: kind{Basic: true, Name: "string"}}},
})

if len(e.Errors) != 0 {
for _, err := range e.Errors {
fmt.Println(os.Stderr, "err: "+err)
fmt.Fprintln(os.Stderr, "err: "+err)
}
os.Exit(1)
}
Expand Down
16 changes: 16 additions & 0 deletions example/starwars/gen/generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ var (
__TypeSatisfies = []string{"__Type"}
)

// nolint: gocyclo, errcheck, gas, goconst
func _droid(ec *executionContext, sel []query.Selection, it *starwars.Droid) {
groupedFieldSet := ec.collectFields(sel, droidSatisfies, map[string]bool{})
ec.json.BeginObject()
Expand Down Expand Up @@ -136,6 +137,7 @@ func _droid(ec *executionContext, sel []query.Selection, it *starwars.Droid) {
ec.json.EndObject()
}

// nolint: gocyclo, errcheck, gas, goconst
func _friendsConnection(ec *executionContext, sel []query.Selection, it *starwars.FriendsConnection) {
groupedFieldSet := ec.collectFields(sel, friendsConnectionSatisfies, map[string]bool{})
ec.json.BeginObject()
Expand Down Expand Up @@ -200,6 +202,7 @@ func _friendsConnection(ec *executionContext, sel []query.Selection, it *starwar
ec.json.EndObject()
}

// nolint: gocyclo, errcheck, gas, goconst
func _friendsEdge(ec *executionContext, sel []query.Selection, it *starwars.FriendsEdge) {
groupedFieldSet := ec.collectFields(sel, friendsEdgeSatisfies, map[string]bool{})
ec.json.BeginObject()
Expand Down Expand Up @@ -236,6 +239,7 @@ func _friendsEdge(ec *executionContext, sel []query.Selection, it *starwars.Frie
ec.json.EndObject()
}

// nolint: gocyclo, errcheck, gas, goconst
func _human(ec *executionContext, sel []query.Selection, it *starwars.Human) {
groupedFieldSet := ec.collectFields(sel, humanSatisfies, map[string]bool{})
ec.json.BeginObject()
Expand Down Expand Up @@ -342,6 +346,7 @@ func _human(ec *executionContext, sel []query.Selection, it *starwars.Human) {
ec.json.EndObject()
}

// nolint: gocyclo, errcheck, gas, goconst
func _mutation(ec *executionContext, sel []query.Selection, it *interface{}) {
groupedFieldSet := ec.collectFields(sel, mutationSatisfies, map[string]bool{})
ec.json.BeginObject()
Expand Down Expand Up @@ -376,6 +381,7 @@ func _mutation(ec *executionContext, sel []query.Selection, it *interface{}) {
ec.json.EndObject()
}

// nolint: gocyclo, errcheck, gas, goconst
func _pageInfo(ec *executionContext, sel []query.Selection, it *starwars.PageInfo) {
groupedFieldSet := ec.collectFields(sel, pageInfoSatisfies, map[string]bool{})
ec.json.BeginObject()
Expand Down Expand Up @@ -405,6 +411,7 @@ func _pageInfo(ec *executionContext, sel []query.Selection, it *starwars.PageInf
ec.json.EndObject()
}

// nolint: gocyclo, errcheck, gas, goconst
func _query(ec *executionContext, sel []query.Selection, it *interface{}) {
groupedFieldSet := ec.collectFields(sel, querySatisfies, map[string]bool{})
ec.json.BeginObject()
Expand Down Expand Up @@ -602,6 +609,7 @@ func _query(ec *executionContext, sel []query.Selection, it *interface{}) {
ec.json.EndObject()
}

// nolint: gocyclo, errcheck, gas, goconst
func _review(ec *executionContext, sel []query.Selection, it *starwars.Review) {
groupedFieldSet := ec.collectFields(sel, reviewSatisfies, map[string]bool{})
ec.json.BeginObject()
Expand Down Expand Up @@ -629,6 +637,7 @@ func _review(ec *executionContext, sel []query.Selection, it *starwars.Review) {
ec.json.EndObject()
}

// nolint: gocyclo, errcheck, gas, goconst
func _starship(ec *executionContext, sel []query.Selection, it *starwars.Starship) {
groupedFieldSet := ec.collectFields(sel, starshipSatisfies, map[string]bool{})
ec.json.BeginObject()
Expand Down Expand Up @@ -658,6 +667,7 @@ func _starship(ec *executionContext, sel []query.Selection, it *starwars.Starshi
ec.json.EndObject()
}

// nolint: gocyclo, errcheck, gas, goconst
func ___Directive(ec *executionContext, sel []query.Selection, it *introspection.Directive) {
groupedFieldSet := ec.collectFields(sel, __DirectiveSatisfies, map[string]bool{})
ec.json.BeginObject()
Expand Down Expand Up @@ -709,6 +719,7 @@ func ___Directive(ec *executionContext, sel []query.Selection, it *introspection
ec.json.EndObject()
}

// nolint: gocyclo, errcheck, gas, goconst
func ___EnumValue(ec *executionContext, sel []query.Selection, it *introspection.EnumValue) {
groupedFieldSet := ec.collectFields(sel, __EnumValueSatisfies, map[string]bool{})
ec.json.BeginObject()
Expand Down Expand Up @@ -752,6 +763,7 @@ func ___EnumValue(ec *executionContext, sel []query.Selection, it *introspection
ec.json.EndObject()
}

// nolint: gocyclo, errcheck, gas, goconst
func ___Field(ec *executionContext, sel []query.Selection, it *introspection.Field) {
groupedFieldSet := ec.collectFields(sel, __FieldSatisfies, map[string]bool{})
ec.json.BeginObject()
Expand Down Expand Up @@ -819,6 +831,7 @@ func ___Field(ec *executionContext, sel []query.Selection, it *introspection.Fie
ec.json.EndObject()
}

// nolint: gocyclo, errcheck, gas, goconst
func ___InputValue(ec *executionContext, sel []query.Selection, it *introspection.InputValue) {
groupedFieldSet := ec.collectFields(sel, __InputValueSatisfies, map[string]bool{})
ec.json.BeginObject()
Expand Down Expand Up @@ -866,6 +879,7 @@ func ___InputValue(ec *executionContext, sel []query.Selection, it *introspectio
ec.json.EndObject()
}

// nolint: gocyclo, errcheck, gas, goconst
func ___Schema(ec *executionContext, sel []query.Selection, it *introspection.Schema) {
groupedFieldSet := ec.collectFields(sel, __SchemaSatisfies, map[string]bool{})
ec.json.BeginObject()
Expand Down Expand Up @@ -935,6 +949,7 @@ func ___Schema(ec *executionContext, sel []query.Selection, it *introspection.Sc
ec.json.EndObject()
}

// nolint: gocyclo, errcheck, gas, goconst
func ___Type(ec *executionContext, sel []query.Selection, it *introspection.Type) {
groupedFieldSet := ec.collectFields(sel, __TypeSatisfies, map[string]bool{})
ec.json.BeginObject()
Expand Down Expand Up @@ -1235,6 +1250,7 @@ type collectedField struct {
Selections []query.Selection
}

// nolint: deadcode, megacheck
func unpackComplexArg(result interface{}, data interface{}) error {
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
TagName: "graphql",
Expand Down
Loading

0 comments on commit a9352e3

Please sign in to comment.