gqlscan

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 2, 2022 License: BSD-3-Clause Imports: 5 Imported by: 4

README

GraphQL Logo (Rhodamine) 4 (1)

Coverage Status GoReportCard GoDoc

gqlscan provides functions for fast and allocation-free lexical scanning and validation of GraphQL queries according to the GraphQL specification of October 2021.

The provided functions don't perform semantic analysis such as making sure that declared variables are used or that values match their declared types, etc. as this is outside the scope of lexical analysis.

Benchmark

All tests were performed on an Apple M1 Max 14" MBP running macOS Monterey 12.4.

Benchmark Suite

The benchmark suite allows testing arbitrary query sets for throughput.

go run cmd/bench/main.go -i cmd/bench/inputs

Single core:

gathered 4 input file(s):
 inputs/complex.graphql: 2.2 kB
 inputs/longstr.graphql: 3.8 kB
 inputs/longstr_blk.graphql: 4.4 kB
 inputs/small.graphql: 28 B
running 1 parallel goroutine(s) for 1m0s
finished (1m0.000025417s)
total processed: 72 GB (1.2 GB/s; 8.96 gbit/s)
total errors: 0

Multicore:

gathered 4 input file(s):
 inputs/complex.graphql: 2.2 kB
 inputs/longstr.graphql: 3.8 kB
 inputs/longstr_blk.graphql: 4.4 kB
 inputs/small.graphql: 28 B
running 10 parallel goroutine(s) for 1m0s
finished (1m0.000055208s)
total processed: 593 GB (9.9 GB/s; 73.66 gbit/s)
total errors: 0

Test Benchmarks

Тhe test benchmarks benchmark all test inputs.

go test -v -bench . -benchmem ./...
goos: darwin
goarch: arm64
pkg: github.com/graph-guard/gqlscan
BenchmarkScanAll
BenchmarkScanAll/gqlscan_test.go:29
BenchmarkScanAll/gqlscan_test.go:29-10         	370445240	        32.39 ns/op	       0 B/op	       0 allocs/op
BenchmarkScanAll/gqlscan_test.go:263
BenchmarkScanAll/gqlscan_test.go:263-10        	 5368155	      2236 ns/o       0 B/op	       0 allocs/op
PASS
ok  	github.com/graph-guard/gqlscan	29.850s

Documentation

Overview

Package gqlscan provides functions for fast and allocation-free lexical scanning and validation of GraphQL queries according to the GraphQL specification of October 2021 (https://spec.graphql.org/October2021/).

The provided functions don't perform semantic analysis such as making sure that declared variables are used or that values match their declared types, etc. as this is outside the scope of lexical analysis.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Error

type Error struct {
	Index       int
	AtIndex     rune
	Code        ErrorCode
	Expectation Expect
}

Error is a GraphQL lexical scan error.

func Scan

func Scan(str []byte, fn func(*Iterator) (err bool)) Error

Scan calls fn for every token it scans in str. If fn returns true then an error with code ErrCallbackFn is returned. If the returned error code == 0 then there was no error during the scan, this can also be checked using err.IsErr().

WARNING: *Iterator passed to fn should never be aliased and used after Scan returns because it's returned to the pool and may be acquired by another call to Scan!

func ScanAll

func ScanAll(str []byte, fn func(*Iterator)) Error

ScanAll calls fn for every token it scans in str. If the returned error code == 0 then there was no error during the scan, this can also be checked using err.IsErr().

WARNING: *Iterator passed to fn should never be aliased and used after ScanAll returns because it's returned to the pool and may be acquired by another call to ScanAll!

func (Error) Error

func (e Error) Error() string

func (Error) IsErr

func (e Error) IsErr() bool

IsErr returns true if there is an error, otherwise returns false.

type ErrorCode

type ErrorCode int

ErrorCode defines the type of an error.

const (
	ErrCallbackFn ErrorCode
	ErrUnexpToken
	ErrUnexpEOF
	ErrIllegalFragName
	ErrInvalNum
	ErrInvalType
)

type Expect

type Expect int

Expect defines an expectation

const (
	ExpectVal Expect
	ExpectValEnum
	ExpectDefaultVarVal
	ExpectDef
	ExpectOprName
	ExpectSelSet
	ExpectArgName
	ExpectEscapedSequence
	ExpectEscapedUnicodeSequence
	ExpectEndOfString
	ExpectEndOfBlockString
	ExpectColumnAfterArg
	ExpectFieldNameOrAlias
	ExpectFieldName
	ExpectSel
	ExpectDir
	ExpectDirName
	ExpectVar
	ExpectVarName
	ExpectVarRefName
	ExpectVarType
	ExpectColumnAfterVar
	ExpectObjFieldName
	ExpectColObjFieldName
	ExpectFragTypeCond
	ExpectFragKeywordOn
	ExpectFragName
	ExpectFrag
	ExpectSpreadName
	ExpectFragInlined
	ExpectAfterFieldName
	ExpectAfterSelection
	ExpectAfterValueInner
	ExpectAfterValueOuter
	ExpectAfterArgList
	ExpectAfterDefKeyword
	ExpectAfterVarType
	ExpectAfterVarTypeName
)

Expectations

func (Expect) String

func (e Expect) String() string

type Iterator

type Iterator struct {
	// contains filtered or unexported fields
}

Iterator is a GraphQL iterator for lexical analysis.

WARNING: An iterator instance shall never be aliased and/or used after Scan or ScanAll returns because it's returned to a global pool!

func (*Iterator) IndexHead

func (i *Iterator) IndexHead() int

IndexHead returns the current head index.

func (*Iterator) IndexTail

func (i *Iterator) IndexTail() int

IndexTail returns the current tail index. Returns -1 if the current token doesn't reflect a dynamic value.

func (*Iterator) LevelSelect

func (i *Iterator) LevelSelect() int

LevelSelect returns the current selector level.

func (*Iterator) ScanInterpreted

func (i *Iterator) ScanInterpreted(
	buffer []byte,
	fn func(buffer []byte) (stop bool),
)

ScanInterpreted calls fn writing the interpreted part of the value to buffer as long as fn doesn't return true and the scan didn't reach the end of the interpreted value.

func (*Iterator) Token

func (i *Iterator) Token() Token

Token returns the current token type.

func (*Iterator) Value

func (i *Iterator) Value() []byte

Value returns the raw value of the current token. For TokenStrBlock it's the raw uninterpreted body of the string, use ScanInterpreted for the interpreted value of the block string.

WARNING: The returned byte slice refers to the same underlying memory as the byte slice passed to Scan and ScanAll as str parameter, copy it or use with caution!

type Token

type Token int

Token defines the type of a token.

const (
	TokenDefQry Token
	TokenDefMut
	TokenDefSub
	TokenDefFrag
	TokenOprName
	TokenDirName
	TokenVarList
	TokenVarListEnd
	TokenArgList
	TokenArgListEnd
	TokenSet
	TokenSetEnd
	TokenFragTypeCond
	TokenFragName
	TokenFragInline
	TokenNamedSpread
	TokenFieldAlias
	TokenField
	TokenArgName
	TokenEnumVal
	TokenArr
	TokenArrEnd
	TokenStr
	TokenStrBlock
	TokenInt
	TokenFloat
	TokenTrue
	TokenFalse
	TokenNull
	TokenVarName
	TokenVarTypeName
	TokenVarTypeArr
	TokenVarTypeArrEnd
	TokenVarTypeNotNull
	TokenVarRef
	TokenObj
	TokenObjEnd
	TokenObjField
)

Token types

func (Token) String

func (t Token) String() string

Directories

Path Synopsis
cmd
gen

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL