Skip to content

Commit

Permalink
📝 fix parse flags
Browse files Browse the repository at this point in the history
  • Loading branch information
bannzai committed Jan 12, 2022
1 parent e510b64 commit 0ca785d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 46 deletions.
11 changes: 10 additions & 1 deletion cmd/nodecheck/main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
package main

import (
"flag"

"github.com/bannzai/nodecheck"
"github.com/gqlgo/gqlanalysis/multichecker"
)

var excludes string

func main() {
flag.StringVar(&excludes, "excludes", "", "exclude GraphQL types for node check. it can specify multiple values separated by `,` and it can use regex(e.g *Connection")
flag.Parse()

analyzer := nodecheck.Analyzer(excludes)

multichecker.Main(
nodecheck.Analyzer,
analyzer,
)
}
95 changes: 50 additions & 45 deletions core.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package nodecheck

import (
"flag"
"fmt"
"regexp"
"strings"
Expand All @@ -11,65 +10,71 @@ import (
"github.com/gqlgo/gqlanalysis"
)

var excludes string
var Analyzer = &gqlanalysis.Analyzer{
Name: "nodecheck",
Doc: "nodecheck finds invalid GraphQL type that type does not conform Node interface",
Flags: func() flag.FlagSet {
f := flag.NewFlagSet("node check", flag.ExitOnError)
f.StringVar(&excludes, "exclude", "", "exclude GraphQL types for node check. it can specify multiple values separated by `,` and it can use regex(e.g *Connection")
return *f
}(),
Run: run,
func Analyzer(excludes string) *gqlanalysis.Analyzer {
return &gqlanalysis.Analyzer{
Name: "nodecheck",
Doc: "nodecheck finds invalid GraphQL type that type does not conform Node interface",
Run: run(excludes),
}
}

func run(pass *gqlanalysis.Pass) (interface{}, error) {
allTypes := map[string]*ast.Definition{}
for _, def := range pass.Schema.Types {
if def.Kind == ast.Object {
allTypes[def.Name] = def
}
}
func run(excludes string) func(pass *gqlanalysis.Pass) (interface{}, error) {

allNodeImplements := map[string]*ast.Definition{}
for name, t := range allTypes {
for _, typeInterface := range pass.Schema.Implements[name] {
if typeInterface.Kind == ast.Interface && typeInterface.Name == "Node" {
allNodeImplements[name] = t
return func(pass *gqlanalysis.Pass) (interface{}, error) {
allTypes := map[string]*ast.Definition{}
for _, def := range pass.Schema.Types {
if def.Kind == ast.Object {
allTypes[def.Name] = def
}
}
}

unconformedTypes := map[string]*ast.Definition{}
for k, v := range allTypes {
if _, ok := allNodeImplements[k]; !ok {
unconformedTypes[v.Name] = v
allNodeImplements := map[string]*ast.Definition{}
for name, t := range allTypes {
for _, typeInterface := range pass.Schema.Implements[name] {
if typeInterface.Kind == ast.Interface && typeInterface.Name == "Node" {
allNodeImplements[name] = t
}
}
}
}

needToNodeTypes := []*ast.Definition{}
unconformedTypes := map[string]*ast.Definition{}
for k, v := range allTypes {
if _, ok := allNodeImplements[k]; !ok {
unconformedTypes[v.Name] = v
}
}

for k, v := range unconformedTypes {
ok := false
for _, rules := range strings.Split(excludes, ",") {
regex := regexp.MustCompile(rules)
needToNodeTypes := []*ast.Definition{}
for k, v := range unconformedTypes {
ok := false
for _, rule := range strings.Split(excludes, ",") {
if len(rule) > 0 {
regex := regexp.MustCompile(rule)

if ok {
break
if ok {
break
}
if regex.MatchString(k) {
ok = true
}
}
}
if regex.MatchString(k) {
ok = true

if !ok {
needToNodeTypes = append(needToNodeTypes, v)
}
}

if !ok {
needToNodeTypes = append(needToNodeTypes, v)
if len(needToNodeTypes) > 0 {
names := make([]string, len(needToNodeTypes))
for i, t := range needToNodeTypes {
names[i] = t.Name
}

out := strings.Join(names, "\n")
return nil, fmt.Errorf("GraphQL types need to conform to Node type %s", out)
}
}

if len(needToNodeTypes) > 0 {
return nil, fmt.Errorf("GraphQL types need to conform to Node type %s", needToNodeTypes)
return nil, nil
}

return nil, nil
}

0 comments on commit 0ca785d

Please sign in to comment.