gti

package module
v0.1.32 Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2023 License: BSD-3-Clause Imports: 10 Imported by: 64

README

gti

Package GTI provides general purpose type information for Go types, methods, functions and variables

Syntax for function / method arg comments and directives

This extra syntax is recognized for generating toolbar methodview actions in gi.

// MyFunc does some stuff
//    @name: name of the person
//    @age: age of the person
//    @return official name of person, whether they are over 50, and an error.. 
//
//gi:toolbar -shortcut "Ctrl+M" -confirm -show-return
//gti:add 
//gti:arg @name -def "Gopher Blue"
//gti:arg @age -def 42]
//gti:ret [@0|@string|@name] -hide
func MyFunc(name string, age int) (string, bool, error) {
}

key questions:

  • separate registries? YES -- keeps it type specific

  • TypeRegistry

  • FuncRegistry

not sure about these:

  • VarRegistry
  • ConstRegistry

Key functionality

  • generate tooltips for fields in structs

  • generate toolbar props for gogi methodview to generate greasi toolbars for arbitrary App types, and for Ki types.

  • in grease, command method docs and config field docs

  • in Ki (optional) generate a new token of given type by name (for Unmarshal)

  • Ki needs NameAndType, need to be able to use Type token to specify type. Also for making a new child of given type.

notes

  • GTI is NOT Go ast because it doesn't store Type info for Fields, methods, etc. There is no assumption that all types are processed -- only designated types. It only records names, comments and directives.

Documentation

Index

Constants

View Source
const (
	// Version is the version of this package being used
	Version = "v0.1.32"
	// GitCommit is the commit just before the latest version commit
	GitCommit = "4749ff1"
	// VersionDate is the date-time of the latest version commit in UTC (in the format 'YYYY-MM-DD HH:MM', which is the Go format '2006-01-02 15:04')
	VersionDate = "2023-12-26 23:26"
)

Variables

View Source
var (
	// Funcs records all types (i.e., a type registry)
	// key is long type name: package_url.Func, e.g., goki.dev/gi/v2/gi.Button
	Funcs = map[string]*Func{}

	// FuncIDCounter is an atomically incremented uint64 used
	// for assigning new [Func.ID] numbers
	FuncIDCounter uint64
)
View Source
var (
	// Types records all types (i.e., a type registry)
	// key is long type name: package_url.Type, e.g., goki.dev/gi/v2/gi.Button
	Types = map[string]*Type{}

	// TypeIDCounter is an atomically incremented uint64 used
	// for assigning new [Type.ID] numbers
	TypeIDCounter uint64
)

Functions

func FuncName

func FuncName(f any) string

FuncName returns the fully package-qualified name of given function This is guaranteed to be unique and used for the Funcs registry.

func GetDoc added in v0.1.24

func GetDoc(val, owner reflect.Value, field *reflect.StructField, label string) (string, bool)

GetDoc gets the documentation for the given value with the given owner value, field, and label. The value, owner value, and field may be nil/invalid. The owner value, if valid, is the value that contains the value (the parent struct, map, slice, or array). The field, if non-nil, is the struct field that the value represents. GetDoc uses the given label to format the documentation with sentence.Doc before returning it.

func TypeName

func TypeName(typ reflect.Type) string

TypeName returns the long, full package-path qualified type name. This is guaranteed to be unique and used for the Types registry.

func TypeNameObj

func TypeNameObj(v any) string

TypeNameObj returns the long, full package-path qualified type name from given object. Automatically finds the non-pointer base type. This is guaranteed to be unique and used for the Types registry.

Types

type Directive

type Directive struct {
	Tool      string
	Directive string
	Args      []string
}

Directive represents a comment directive in the format:

//tool:directive args...

func (*Directive) GoString

func (d *Directive) GoString() string

GoString returns the directive as Go code.

func (*Directive) String

func (d *Directive) String() string

String returns a string representation of the directive in the format:

//tool:directive args...

type Directives

type Directives []*Directive

func (Directives) ForTool

func (d Directives) ForTool(tool string) Directives

ForTool returns all of the directives in these directives that have the given tool name.

func (Directives) GoString

func (d Directives) GoString() string

GoString returns the directives as Go code.

type Field

type Field struct {
	// Name is the name of the field (eg: Icon)
	Name string

	// Type has the fully-package-path-qualified name of the type,
	// which can be used to look up the type in the Types registry
	// (eg: goki.dev/gi/v2/gi.Button)
	Type string

	// LocalType is the shorter, local name of the type from the
	// perspective of the code where this field is declared
	// (eg: gi.Button or Button, depending on where this is)
	LocalType string

	// Doc has all of the comment documentation
	// info as one string with directives removed.
	Doc string

	// Directives has the parsed comment directives
	Directives Directives

	// Tag, if this field is part of a struct, contains the struct
	// tag for it.
	Tag reflect.StructTag
}

Field represents a field or embed in a struct, or an argument or return value of a function.

func GetField added in v0.1.7

func GetField(typ *Type, field string) *Field

GetField recursively attempts to extract the gti.Field with the given name from the given struct gti.Type, by searching through all of the embeds if it can not find it directly in the struct.

type Fields

type Fields = ordmap.Map[string, *Field]

Fields represents a set of multiple Field objects

type Func

type Func struct {
	// Name is the fully-qualified name of the function
	// (eg: goki.dev/gi/v2/gi.NewButton)
	Name string

	// Doc has all of the comment documentation
	// info as one string with directives removed.
	Doc string

	// Directives has the parsed comment directives
	Directives Directives

	// Args are arguments to the function
	Args *Fields

	// Returns are return values of the function
	Returns *Fields

	// unique type ID number
	ID uint64
}

Func represents a global function

func AddFunc

func AddFunc(fun *Func) *Func

AddFunc adds a constructed Func to the registry and returns it. This sets the ID.

func FuncByName

func FuncByName(nm string) *Func

FuncByName returns a Func by name (package_url.Type, e.g., goki.dev/gi/v2/gi.Button),

func FuncByNameTry

func FuncByNameTry(nm string) (*Func, error)

FuncByNameTry returns a Func by name (package_url.Type, e.g., goki.dev/gi/v2/gi.Button), or error if not found

func FuncInfo

func FuncInfo(f any) *Func

FuncInfo returns function info for given function.

func FuncInfoTry

func FuncInfoTry(f any) (*Func, error)

FuncInfoTry returns function info for given function.

type Method

type Method struct {
	// Name is the name of the method (eg: NewChild)
	Name string

	// Doc has all of the comment documentation
	// info as one string with directives removed.
	Doc string

	// Directives has the parsed comment directives
	Directives Directives

	// Args are arguments to the method
	Args *Fields

	// Returns are return values of the method
	Returns *Fields
}

Method represents a method

type Methods

type Methods = ordmap.Map[string, *Method]

Methods represents a set of multiple Method objects

type Type

type Type struct {
	// Name is the fully package-path-qualified name of the type (eg: goki.dev/gi/v2/gi.Button)
	Name string

	// ShortName is the short, package-qualified name of the type (eg: gi.Button)
	ShortName string

	// IDName is the short, package-unqualified, kebab-case name of the type that is suitable
	// for use in an ID (eg: button)
	IDName string

	// Doc has all of the comment documentation
	// info as one string with directives removed.
	Doc string

	// Directives has the parsed comment directives
	Directives Directives

	// unique type ID number
	ID uint64

	// Methods are available for all types
	Methods *Methods

	// Embedded fields for struct types
	Embeds *Fields

	// Fields for struct types
	Fields *Fields

	// instance of the type
	Instance any

	// All embedded fields (including nested ones) for struct types;
	// not set by gtigen -- HasEmbed automatically compiles it as needed.
	// Key is the ID of the type.
	AllEmbeds map[uint64]*Type
}

Type represents a type

func AddType

func AddType(typ *Type) *Type

AddType adds a constructed Type to the registry and returns it. This sets the ID.

func AllEmbeddersOf added in v0.1.7

func AllEmbeddersOf(typ *Type) []*Type

AllEmbeddersOf returns all registered types that embed the given type. List is sorted in alpha order by fully package-path-qualified Name.

func TypeByName

func TypeByName(nm string) *Type

TypeByName returns a Type by name (package_url.Type, e.g., goki.dev/gi/v2/gi.Button),

func TypeByNameTry

func TypeByNameTry(nm string) (*Type, error)

TypeByNameTry returns a Type by name (package_url.Type, e.g., goki.dev/gi/v2/gi.Button), or error if not found

func TypeByReflectType added in v0.1.24

func TypeByReflectType(typ reflect.Type) *Type

TypeByReflectType returns the Type of the given reflect type

func TypeByReflectTypeTry added in v0.1.24

func TypeByReflectTypeTry(typ reflect.Type) (*Type, error)

TypeByReflectTypeTry returns the Type of the given reflect type, or an error if it is not found

func TypeByValue

func TypeByValue(val any) *Type

TypeByValue returns the Type of the given value

func TypeByValueTry

func TypeByValueTry(val any) (*Type, error)

TypeByValueTry returns the Type of the given value, or an error if it is not found

func (*Type) CompileEmbeds

func (tp *Type) CompileEmbeds()

func (*Type) HasEmbed

func (tp *Type) HasEmbed(typ *Type) bool

HasEmbed returns true if this type has the given type at any level of embedding depth, including if this type is the given type. The first time called it will Compile a map of all embedded types so subsequent calls are very fast.

func (*Type) Label added in v0.1.7

func (tp *Type) Label() string

func (*Type) ReflectType

func (tp *Type) ReflectType() reflect.Type

ReflectType returns the reflect.Type for this type, using the Instance

func (*Type) String added in v0.1.7

func (tp *Type) String() string

Directories

Path Synopsis
cmd
Package gtigen provides the generation of general purpose type information for Go types, methods, functions and variables
Package gtigen provides the generation of general purpose type information for Go types, methods, functions and variables

Jump to

Keyboard shortcuts

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