helperclauses

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package helperclauses provides clauses that simplify the modelling of new queries.

These clauses are not implementation specific and don't modify the schema or seed on their own, unless subqueries with such behavior are supplied.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Assembler

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

An Assembler returns the provided subclauses during generation and the provided template string when prompted. The assembler itself does not modify the schema or seed, though the subclauses might.

Example
package main

import (
	"fmt"

	"github.com/Anon10214/dinkel/models/mock"
	"github.com/Anon10214/dinkel/models/opencypher/schema"
	"github.com/Anon10214/dinkel/seed"
	"github.com/Anon10214/dinkel/translator"
	"github.com/Anon10214/dinkel/translator/helperclauses"
)

func generateMockClause(clause translator.Clause) string {
	return generateMockClauseWithSeed(clause, seed.GetRandomByteString())
}

func generateMockClauseWithSeed(clause translator.Clause, seed *seed.Seed) string {
	schema := &schema.Schema{}
	schema.Reset()

	return translator.GenerateStatement(
		seed,
		schema,
		clause,
		mock.Implementation{},
	)
}

func main() {
	stringer1 := helperclauses.CreateStringer("hello")
	stringer2 := helperclauses.CreateStringer("world")

	subclauses := []translator.Clause{stringer1, stringer2}
	templateString := "%s - %s"

	fmt.Println(
		generateMockClause(
			helperclauses.CreateAssembler(subclauses, templateString),
		),
	)
}
Output:
hello - world
Example (WithoutTemplateString)
package main

import (
	"fmt"

	"github.com/Anon10214/dinkel/models/mock"
	"github.com/Anon10214/dinkel/models/opencypher/schema"
	"github.com/Anon10214/dinkel/seed"
	"github.com/Anon10214/dinkel/translator"
	"github.com/Anon10214/dinkel/translator/helperclauses"
)

func generateMockClause(clause translator.Clause) string {
	return generateMockClauseWithSeed(clause, seed.GetRandomByteString())
}

func generateMockClauseWithSeed(clause translator.Clause, seed *seed.Seed) string {
	schema := &schema.Schema{}
	schema.Reset()

	return translator.GenerateStatement(
		seed,
		schema,
		clause,
		mock.Implementation{},
	)
}

func main() {
	stringer1 := helperclauses.CreateStringer("no")
	stringer2 := helperclauses.CreateStringer("spaces")

	subclauses := []translator.Clause{stringer1, stringer2}

	fmt.Println(
		generateMockClause(
			helperclauses.CreateAssemblerWithoutTemplateString(subclauses),
		),
	)
}
Output:
nospaces

func CreateAssembler

func CreateAssembler(subclauses []translator.Clause, templateString string) *Assembler

CreateAssembler returns an assembler given the passed subclauses and template string

func CreateAssemblerWithoutTemplateString

func CreateAssemblerWithoutTemplateString(subclauses []translator.Clause) *Assembler

CreateAssemblerWithoutTemplateString returns an assembler whose generated result will consist of the subclauses concatenated.

func (Assembler) Generate

func (c Assembler) Generate(*seed.Seed, *schema.Schema) []translator.Clause

Generate the assembler's subclauses, returns the subclauses the assembler was initialized with.

func (Assembler) TemplateString

func (c Assembler) TemplateString() string

TemplateString returns the template string the assembler was initialized with.

type ClauseCapturer

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

The ClauseCapturer essentially represents a clause singleton.

It takes in a clause and generates it when generating itself. From then on, every time the capturer get regenerated, it results in the exact same clause again, causing no changes in the schema, seed or its translated result.

Any side effects produced during the captured clause's generation won't be reapplied on subsequent invocations of the generation function.

Changes directly affecting the passed clause that got captured will be reflected in all other capturers with this captured clause.

This is useful if you want to have different subsequent or preceding clauses, but want to keep one clause the same.

Example (Regenerate)
package main

import (
	"fmt"
	"math/rand"

	"github.com/Anon10214/dinkel/models/mock"
	"github.com/Anon10214/dinkel/models/opencypher"
	"github.com/Anon10214/dinkel/models/opencypher/schema"
	"github.com/Anon10214/dinkel/seed"
	"github.com/Anon10214/dinkel/translator"
	"github.com/Anon10214/dinkel/translator/helperclauses"
)

func generateMockClauseWithSeed(clause translator.Clause, seed *seed.Seed) string {
	schema := &schema.Schema{}
	schema.Reset()

	return translator.GenerateStatement(
		seed,
		schema,
		clause,
		mock.Implementation{},
	)
}

func main() {
	// Get a clause capturer for an OpenCypher root clause
	clause := helperclauses.GetClauseCapturerForClause(&opencypher.RootClause{}, mock.Implementation{})

	// Generate a clause with a set underlying seed
	origSeed := seed.GetRandomByteStringWithSource(
		*rand.New(rand.NewSource(42)),
	)
	orig := generateMockClauseWithSeed(clause, origSeed)

	// Regenerate with a different underlying seed
	regeneratedSeed := seed.GetRandomByteStringWithSource(
		*rand.New(rand.NewSource(1337)),
	)
	regenerated := generateMockClauseWithSeed(clause, regeneratedSeed)

	// Regenerating the clause capturer results in the same clause
	fmt.Println(orig == regenerated)
}
Output:
true

func GetClauseCapturerForClause

func GetClauseCapturerForClause(clause translator.Clause, impl translator.Implementation) *ClauseCapturer

GetClauseCapturerForClause returns a new capturer for a clause and an implementation

func (ClauseCapturer) Copy

func (c ClauseCapturer) Copy() *ClauseCapturer

Copy returns a deep copy of the clause capturer.

func (*ClauseCapturer) Generate

func (c *ClauseCapturer) Generate(seed *seed.Seed, s *schema.Schema) []translator.Clause

Generate the ClauseCapturer's subclauses.

The first call to this method generates the captured clause and return its subclauses. Subsequent invocations, as long as the captured clause wasn't changed, cause these same subclauses to be returned.

func (*ClauseCapturer) GetCapturedClause

func (c *ClauseCapturer) GetCapturedClause() translator.Clause

GetCapturedClause returns the underlying captured clause

func (ClauseCapturer) GetCapturedSchema

func (c ClauseCapturer) GetCapturedSchema() *schema.Schema

GetCapturedSchema returns the schema the clause was originally generated with. Guaranteed to be non nil if the clause was once generated and not updated later.

func (ClauseCapturer) GetSubclauseClauseCapturers

func (c ClauseCapturer) GetSubclauseClauseCapturers() []*ClauseCapturer

GetSubclauseClauseCapturers returns the subclauses of the clause capturer, which are themselves also clause capturers. This will return nil if the clause hasn't been generated yet.

func (*ClauseCapturer) ModifySchema

func (c *ClauseCapturer) ModifySchema(s *schema.Schema)

ModifySchema invokes the captured clause's ModifySchema functions, if defined and if the clause hasn't been generated before.

It then sets generated to true, to ensure that the generation of the clause capturer returns the same results from now on.

func (*ClauseCapturer) RenewClause

func (c *ClauseCapturer) RenewClause(newClause translator.Clause)

RenewClause sets the captured clause to the passed clause.

Calling this method also sets generated to false, so if this ClauseCapturer's generate function gets invoked again, the captured clause gets regenerated again.

When regenerating, the schema passed during generation of the new clause gets used instead of the one used when originally generating the clause. If this is undesired, use UpdateClause instead.

func (ClauseCapturer) TemplateString

func (c ClauseCapturer) TemplateString() string

TemplateString returns the captured clause's template string, if defined. Else, TemplateString returns the default template string, causing the subclauses to simply get concatenated.

func (*ClauseCapturer) UpdateClause

func (c *ClauseCapturer) UpdateClause(newClause translator.Clause)

UpdateClause sets the captured clause to the passed clause.

Calling this method also sets generated to false, so if this ClauseCapturer's generate function gets invoked again, the captured clause gets regenerated again.

When regenerating, the schema which generated the original clause gets reused. If this is undesired, use RenewClause instead.

type EmptyClause

type EmptyClause struct{}

The EmptyClause simply results in an empty string during generation. It returns no subclauses and doesn't modify the schema or seed.

Example
package main

import (
	"fmt"

	"github.com/Anon10214/dinkel/models/mock"
	"github.com/Anon10214/dinkel/models/opencypher/schema"
	"github.com/Anon10214/dinkel/seed"
	"github.com/Anon10214/dinkel/translator"
	"github.com/Anon10214/dinkel/translator/helperclauses"
)

func generateMockClause(clause translator.Clause) string {
	return generateMockClauseWithSeed(clause, seed.GetRandomByteString())
}

func generateMockClauseWithSeed(clause translator.Clause, seed *seed.Seed) string {
	schema := &schema.Schema{}
	schema.Reset()

	return translator.GenerateStatement(
		seed,
		schema,
		clause,
		mock.Implementation{},
	)
}

func main() {
	fmt.Println(
		len(generateMockClause(&helperclauses.EmptyClause{})),
	)
}
Output:
0

func (*EmptyClause) Generate

func (c *EmptyClause) Generate(seed *seed.Seed, s *schema.Schema) []translator.Clause

Generate the EmptyClause subclauses, does nothing and returns nil

type HookClause

type HookClause struct {
	GenerateHook       func(*seed.Seed, *schema.Schema) []translator.Clause
	TemplateStringHook func() string
	ModifySchemaHook   func(*schema.Schema)
}

A HookClause calls provided hooks. It doesn't provide any value for fuzzing and is supposed to be used for testing.

func (HookClause) Generate

func (c HookClause) Generate(seed *seed.Seed, s *schema.Schema) []translator.Clause

Generate returns nil and calls the GenerateHook if defined

func (HookClause) ModifySchema

func (c HookClause) ModifySchema(s *schema.Schema)

ModifySchema does nothing and calls the ModifySchemaHook if defined

func (HookClause) TemplateString

func (c HookClause) TemplateString() string

TemplateString returns the empty string and calls the TemplateStringHook if defined.

type Stringer

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

A Stringer has no subclauses and does not modify the schema or seed. It simply returns the saved string as its template string.

Example
package main

import (
	"fmt"

	"github.com/Anon10214/dinkel/models/mock"
	"github.com/Anon10214/dinkel/models/opencypher/schema"
	"github.com/Anon10214/dinkel/seed"
	"github.com/Anon10214/dinkel/translator"
	"github.com/Anon10214/dinkel/translator/helperclauses"
)

func generateMockClause(clause translator.Clause) string {
	return generateMockClauseWithSeed(clause, seed.GetRandomByteString())
}

func generateMockClauseWithSeed(clause translator.Clause, seed *seed.Seed) string {
	schema := &schema.Schema{}
	schema.Reset()

	return translator.GenerateStatement(
		seed,
		schema,
		clause,
		mock.Implementation{},
	)
}

func main() {
	fmt.Println(
		generateMockClause(helperclauses.CreateStringer("ABC")),
	)
}
Output:
ABC

func CreateStringer

func CreateStringer(val string) *Stringer

CreateStringer returns a stringer generating the passed string.

func (Stringer) Generate

func (c Stringer) Generate(*seed.Seed, *schema.Schema) []translator.Clause

Generate the Stringer subclauses, does nothing and returns nil

func (Stringer) TemplateString

func (c Stringer) TemplateString() string

TemplateString returns the Stringer's template string, which is just its associated value

Jump to

Keyboard shortcuts

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