Skip to content

Commit

Permalink
Merge pull request #20 from bannzai/support_introspection_header
Browse files Browse the repository at this point in the history
Add CLI argument for introspection-header
  • Loading branch information
tenntenn committed Aug 23, 2024
2 parents 29136e0 + 4064965 commit 82e7821
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 2 deletions.
9 changes: 7 additions & 2 deletions multichecker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package multichecker

import (
"flag"
"net/http"
"os"

"github.com/gqlgo/gqlanalysis"
Expand All @@ -13,16 +14,20 @@ var (
flagQuery string
)

var ih = make(introspectionHeader)

func init() {
flag.StringVar(&flagSchema, "schema", "schema/**/*.graphql", "pattern of schema")
flag.StringVar(&flagQuery, "query", "query/**/*.graphql", "pattern of query")
flag.Var(ih, "introspection-header", "format key1:value1,key2:value2")
}

func Main(analyzers ...*gqlanalysis.Analyzer) {
flag.Parse()
checker := &checker.Checker{
Schema: flagSchema,
Query: flagQuery,
Schema: flagSchema,
Query: flagQuery,
IntrospectionHeader: http.Header(ih),
}

os.Exit(checker.Run(analyzers...))
Expand Down
3 changes: 3 additions & 0 deletions multichecker/export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package multichecker

type ExportedIntrospectionHeader = introspectionHeader
34 changes: 34 additions & 0 deletions multichecker/introspection_header.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package multichecker

import (
"fmt"
"net/http"
"strings"
)

// introspectionHeader confirmed to `Value` interface in `flag` package.
type introspectionHeader http.Header

func (ih introspectionHeader) String() string {
var s string
for k, v := range ih {
if len(s) != 0 {
s += ","
}
s += fmt.Sprintf("%v:%v", k, v[0])
}
return s
}

func (ih introspectionHeader) Set(args string) error {
for _, keyAndValueString := range strings.Split(args, ",") {
keyAndValue := strings.Split(keyAndValueString, ":")
key := keyAndValue[0]
value := keyAndValue[1]

// Supports only one value.
ih[key] = []string{value}
}
return nil

}
54 changes: 54 additions & 0 deletions multichecker/introspection_header_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package multichecker_test

import (
"testing"

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

func TestIntrospectionHeader_Set(t *testing.T) {
cases := []struct {
args string
want map[string]string
}{
{"key1:value1", map[string]string{"key1": "value1"}},
{"key1:value1,key2:value2", map[string]string{"key1": "value1", "key2": "value2"}},
}

for _, tt := range cases {
actual := make(multichecker.ExportedIntrospectionHeader)
actual.Set(tt.args)

if len(actual) != len(tt.want) {
t.Errorf("len(actual) != len(tt.want). actual: %v, want: %v", actual, tt.want)
}
for wantKey, wantValue := range tt.want {
actualValue, ok := actual[wantKey]
if !ok {
t.Errorf("Does not contain key: %s", wantKey)
}
if wantValue != actualValue[0] {
t.Errorf("wantValue is not equal actualValue. key: %v, wantKey: %v, actualValue: %v", wantKey, wantValue, actualValue)
}
}
}
}

func TestIntrospectionHeader_Value(t *testing.T) {
cases := []struct {
want string
}{
{"key1:value1"},
{"key1:value1,key2:value2"},
}

for _, tt := range cases {
ih := make(multichecker.ExportedIntrospectionHeader)
ih.Set(tt.want)
actual := ih.String(
)
if actual != tt.want {
t.Errorf("actual != tt.want. actual: %v, want: %v", actual, tt.want)
}
}
}

0 comments on commit 82e7821

Please sign in to comment.