internal

package
v0.0.0-...-b05fda7 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2023 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendComments

func AppendComments(comment string, comments ...string) string

func AppendTags

func AppendTags(t string, tags ...string) string

func Assert

func Assert(err error)

func Filter

func Filter[X any](filter func(x X) bool, xs []X) []X

func First

func First[T any](xs []T) (T, bool)

func FirstOrDefault

func FirstOrDefault[X comparable](xs []X, defaultIfMissing X) X

FirstOrDefault returns the first value in the slice, or, if empty, the default value

func FormatSource

func FormatSource(source string) (string, error)

func IsAsciiNumber

func IsAsciiNumber(x rune) bool

func IsGoIdent

func IsGoIdent(x rune) bool

func IsGoIdentIdx

func IsGoIdentIdx(x rune, idx int) bool

func IsGoIdentStarter

func IsGoIdentStarter(x rune) bool

func Last

func Last[T any](xs []T) (T, bool)

func Map

func Map[X, Y any](fn func(x X) Y, xs []X) []Y

func MatchSimpleType

func MatchSimpleType(Type, matches string) bool

MatchSimpleType returns true if `Type` is a match for the simple type `matches`, which is a "simple" type (i.e. not a map, channel, slice, etc.). All type constraints on the field are ignored, and the type is still a match if the `Type` type is a pointer version of `matches`.

func Must

func Must[T any](result T, err error) T

func ParseTypeList

func ParseTypeList(types string, visit func(x string, more bool) bool) bool

ParseTypeList parses a comma-separated list of types, including parenthesised sublists of types, calling visit once for each type or sublist.

Sublists tuples are not recursively passed by this function but are simply indicated by calling visit on the entire sublist with "more" as true when calling the visit function.

For example:

ParseTypeList(0, "a, (b, (c, d)), x, y, func (e, f)", visit)

Calls visit with these arguments:

visit("a", false)
visit("b, (c, d)", true)
visit("x", false)
visit("y", false)
visit("func (e, f)", false)

Returns false on parse error such as unpaired parentheses.

func ParseTypeListRecursive

func ParseTypeListRecursive(types string, visit func(depth int, x string) bool) bool

ParseTypeListRecursive parses a comma-separated list of types, including parenthesised sublists of types, calling visit once for each type.

Parenthesised sublists are recursively parsed by this function, with the sublist nesting depth indicated by each call to visit.

For example:

ParseTypeList(0, "a, (b, (c, d)), func (e, f)", visit)

Calls visit with these arguments:

visit(0, "a")
visit(1, "b")
visit(2, "c")
visit(2, "d")
visit(0, "func (e, f)")

Returns false on parse error such as unpaired brackets.

func RecursiveCopySlice

func RecursiveCopySlice[X interface{ Copy() X }](xs []X) []X

RecursiveCopySlice deeply copies a slice of elements that each, in turn, must be copied using their own Copy method.

func RemoveElementByIndex

func RemoveElementByIndex[X any](idx int, xs []X) []X

func RewriteSignatureString

func RewriteSignatureString(sig string, from string, to string) string

RewriteSignatureString performs the special '$' replacement in a function signature specified as a string.

TODO use tokenReplacer instead Deprecated.

func SimpleTypeExpr

func SimpleTypeExpr(x ast.Expr) (string, bool)

SimpleTypeExpr takes a type expression and returns that type formatted as a string iff the type is simple (i.e. not a map, slice, function value, channel etc.), with any generic type constraints removed.

Otherwise, returns ("", false).

This is used to find the first FunctionSignature argument (or receiver) that matches a given type.

func SplitTypeTuple

func SplitTypeTuple(types string) ([]string, bool)

SplitTypeTuple parses a comma-separated list of types, which must not contain parenthesised sublists, and returns each token as a string.

func TestCompileAndRun

func TestCompileAndRun(t *testing.T, source string, expected func(stdout string) error)

TestCompileAndRun compiles and runs a Go program (using "go run") and verifies that:

  • it compiles successfully
  • it generates a normal exit code
  • it doesn't write to stderr
  • captures the writes to stdout
  • asserts that the provided "expected" func returns a nil error when called with the captured input.

WARNING: this function can compile and run arbitrary Go code. This function MUST NOT be used on untrusted sources.

Types

type Set

type Set[X comparable] interface {
	Add(x X)
	Contains(x X) bool
}

func NewSet

func NewSet[X comparable]() Set[X]

type TokenReplacer

type TokenReplacer struct {
	// Single converts the $-token "$" when not preceded by or following a dot.
	Single func() (string, bool)

	// ByIndex converts the $-token "$N" for some decimal "N".
	ByIndex func(index int) (string, bool)

	// ByName converts the $-token "$name" for some string "name".
	ByName func(name string) (string, bool)

	// TupleByIndex converts the $-token "$N.M" for some decimals "N" and "M".
	TupleByIndex func(index int, subidx int) (string, bool)

	// TupleByName converts the $-token "$name.M" for some string "name" and
	// some decimal "M".
	TupleByName func(name string, subidx int) (string, bool)

	// FieldByName converts the $-token "$name.field" for some struct value
	// "name" and some field "field".
	FieldByName func(structValue string, field string) (string, bool)

	// Modifier converts any $-token followed by ".$keyword", passing the
	// resolved token up to that point as an argument.
	Modifier func(keyword string, target string) (string, bool)
}

TokenReplacer can replace $-tokens with values.

In cases where a $-token is ambiguous, use parentheses e.g. "$(foo)".

func (TokenReplacer) Replace

func (t TokenReplacer) Replace(in string) (string, error)

func (*TokenReplacer) SetDefaults

func (t *TokenReplacer) SetDefaults()

Jump to

Keyboard shortcuts

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