Documentation
¶
Index ¶
- func AppendComments(comment string, comments ...string) string
- func AppendTags(t string, tags ...string) string
- func Assert(err error)
- func Filter[X any](filter func(x X) bool, xs []X) []X
- func First[T any](xs []T) (T, bool)
- func FirstOrDefault[X comparable](xs []X, defaultIfMissing X) X
- func FormatSource(source string) (string, error)
- func IsAsciiNumber(x rune) bool
- func IsGoIdent(x rune) bool
- func IsGoIdentIdx(x rune, idx int) bool
- func IsGoIdentStarter(x rune) bool
- func Last[T any](xs []T) (T, bool)
- func Map[X, Y any](fn func(x X) Y, xs []X) []Y
- func MatchSimpleType(Type, matches string) bool
- func Must[T any](result T, err error) T
- func ParseTypeList(types string, visit func(x string, more bool) bool) bool
- func ParseTypeListRecursive(types string, visit func(depth int, x string) bool) bool
- func RecursiveCopySlice[X interface{ ... }](xs []X) []X
- func RemoveElementByIndex[X any](idx int, xs []X) []X
- func RewriteSignatureString(sig string, from string, to string) string
- func SimpleTypeExpr(x ast.Expr) (string, bool)
- func SplitTypeTuple(types string) ([]string, bool)
- func TestCompileAndRun(t *testing.T, source string, expected func(stdout string) error)
- type Set
- type TokenReplacer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AppendComments ¶
func AppendTags ¶
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 IsAsciiNumber ¶
func IsGoIdentIdx ¶
func IsGoIdentStarter ¶
func MatchSimpleType ¶
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 ParseTypeList ¶
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 ¶
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 RewriteSignatureString ¶
RewriteSignatureString performs the special '$' replacement in a function signature specified as a string.
TODO use tokenReplacer instead Deprecated.
func SimpleTypeExpr ¶
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 ¶
SplitTypeTuple parses a comma-separated list of types, which must not contain parenthesised sublists, and returns each token as a string.
func TestCompileAndRun ¶
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) SetDefaults ¶
func (t *TokenReplacer) SetDefaults()