Skip to content

Commit

Permalink
Merge pull request #21 from jensneuse/fix-printer-path
Browse files Browse the repository at this point in the history
Fix printer path
  • Loading branch information
jensneuse authored Mar 2, 2019
2 parents 9b686be + 73bd4b4 commit bda7853
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 40 deletions.
5 changes: 0 additions & 5 deletions pkg/lookup/walker.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,11 +268,6 @@ func (w *Walker) walkFragmentSpreads(refs []int, parent int) {
if w.referenceFormsCycle(FRAGMENT_SPREAD, spreadRef, parent) {
continue
}

/*fragmentDefinition, index, ok := w.l.FragmentDefinitionByName(spread.FragmentName)
if ok {
w.walkFragmentDefinition(fragmentDefinition, index, ref)
}*/
}
}

Expand Down
20 changes: 4 additions & 16 deletions printer/printer.go → pkg/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,13 @@ type Printer struct {
}

func New() *Printer {
return &Printer{
w: lookup.NewWalker(1024, 8),
}
return &Printer{}
}

func (p *Printer) SetInput(parser *parser.Parser) {
func (p *Printer) SetInput(parser *parser.Parser, l *lookup.Lookup, w *lookup.Walker) {
p.p = parser
if p.l == nil {
p.l = lookup.New(parser, 256)
} else {
p.l.SetParser(parser)
}

p.w.SetLookup(p.l)
p.err = nil
p.l = l
p.w = w
}

func (p *Printer) write(bytes []byte) {
Expand All @@ -44,7 +36,6 @@ func (p *Printer) write(bytes []byte) {
func (p *Printer) PrintExecutableSchema(out io.Writer) {

p.out = out
p.w.WalkExecutable()

operations := p.w.OperationDefinitionIterable()
for operations.Next() {
Expand Down Expand Up @@ -136,7 +127,6 @@ func (p *Printer) printOperationType(operationType document.OperationType, hasNa
p.write(literal.SUBSCRIPTION)
p.write(literal.SPACE)
}
return
}

func (p *Printer) printSelectionSet(ref int) {
Expand Down Expand Up @@ -185,8 +175,6 @@ func (p *Printer) printField(ref int) {
p.write(literal.SPACE)
p.printSelectionSet(field.SelectionSet)
}

return
}

func (p *Printer) printFragmentSpread(ref int) {
Expand Down
25 changes: 22 additions & 3 deletions printer/printer_test.go → pkg/printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"bytes"
"fmt"
"github.com/jensneuse/graphql-go-tools/pkg/lookup"
"github.com/jensneuse/graphql-go-tools/pkg/parser"
"github.com/sebdah/goldie"
"testing"
Expand All @@ -21,8 +22,15 @@ func TestPrinter(t *testing.T) {
panic(err)
}

l := lookup.New(p, 256)
l.SetParser(p)

w := lookup.NewWalker(1024, 8)
w.SetLookup(l)
w.WalkExecutable()

printer := New()
printer.SetInput(p)
printer.SetInput(p, l, w)

buff := bytes.Buffer{}
out := bufio.NewWriter(&buff)
Expand Down Expand Up @@ -137,8 +145,13 @@ func TestPrinter_Regression(t *testing.T) {
panic(err)
}

l := lookup.New(p, 256)
w := lookup.NewWalker(1024, 8)
w.SetLookup(l)
w.WalkExecutable()

printer := New()
printer.SetInput(p)
printer.SetInput(p, l, w)

buff := bytes.Buffer{}
out := bufio.NewWriter(&buff)
Expand Down Expand Up @@ -171,12 +184,18 @@ func BenchmarkPrinter_PrintExecutableSchema(b *testing.B) {
buff := bytes.Buffer{}
bufOut := bufio.NewWriter(&buff)

l := lookup.New(p, 256)
w := lookup.NewWalker(1024, 8)

b.ResetTimer()
b.ReportAllocs()

for i := 0; i < b.N; i++ {

printer.SetInput(p)
w.SetLookup(l)
w.WalkExecutable()

printer.SetInput(p, l, w)
printer.PrintExecutableSchema(bufOut)
if printer.err != nil {
panic(printer.err)
Expand Down
18 changes: 5 additions & 13 deletions pkg/validator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package validator

import (
"github.com/jensneuse/graphql-go-tools/pkg/lookup"
"github.com/jensneuse/graphql-go-tools/pkg/parser"
"github.com/jensneuse/graphql-go-tools/pkg/validation"
"github.com/jensneuse/graphql-go-tools/pkg/validation/rules"
"github.com/jensneuse/graphql-go-tools/pkg/validation/rules/execution"
Expand All @@ -14,9 +13,7 @@ type Validator struct {
}

func New() *Validator {
return &Validator{
w: lookup.NewWalker(1024, 8),
}
return &Validator{}
}

var (
Expand All @@ -41,18 +38,13 @@ var (
}
)

func (v *Validator) SetInput(p *parser.Parser) {
if v.l == nil {
v.l = lookup.New(p, 256)
} else {
v.l.SetParser(p)
}

v.w.SetLookup(v.l)
func (v *Validator) SetInput(l *lookup.Lookup, w *lookup.Walker) {
v.l = l
v.w = w
}

func (v *Validator) ValidateExecutableDefinition(executionRules []rules.ExecutionRule) (result validation.Result) {
v.w.WalkExecutable()

for _, rule := range executionRules {
result = rule(v.l, v.w)
if !result.Valid {
Expand Down
16 changes: 13 additions & 3 deletions pkg/validator/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package validator

import (
"fmt"
"github.com/jensneuse/graphql-go-tools/pkg/lookup"
"github.com/jensneuse/graphql-go-tools/pkg/parser"
"testing"
)
Expand All @@ -20,9 +21,13 @@ func TestValidator(t *testing.T) {
panic(err)
}

l := lookup.New(p, 256)
l.SetParser(p)
w := lookup.NewWalker(1024, 8)
w.SetLookup(l)
w.WalkExecutable()
v := New()
v.SetInput(p)
v.SetInput(p)
v.SetInput(l, w)
result := v.ValidateExecutableDefinition(DefaultExecutionRules)
if wantResultValid != result.Valid {
panic(fmt.Errorf("want valid result: %t, got: %t (result: %+v,\n subject: %s)", wantResultValid, result.Valid, result, p.CachedByteSlice(result.Meta.SubjectNameRef)))
Expand Down Expand Up @@ -78,14 +83,19 @@ func BenchmarkValidator(b *testing.B) {
panic(err)
}

l := lookup.New(p, 256)
w := lookup.NewWalker(1024, 8)

v := New()

b.ResetTimer()
b.ReportAllocs()

for i := 0; i < b.N; i++ {
v.SetInput(p)
w.SetLookup(l)
v.SetInput(l, w)
v.l.ResetPool()
w.WalkExecutable()
v.ValidateExecutableDefinition(DefaultExecutionRules)
}
}
Expand Down

0 comments on commit bda7853

Please sign in to comment.