Documentation
¶
Overview ¶
Package gen provides generic management and and generic specialization.
Index ¶
- func SuggestTypesToGenerate(errs []error) iter.Seq[string]
- type Generic
- type Package
- type Specializer
- func (splzr *Specializer) Add(t Generic, source string) error
- func (splzr *Specializer) Dot(types []string) map[string]string
- func (splzr *Specializer) GenerateCodeForErrors(errs []error, pkg Package) iter.Seq2[[]string, error]
- func (splzr *Specializer) GenerateCodeForType(pkg Package, signature string) (messages []string, err error)
- func (splzr *Specializer) MapType(displayType, realType string)
- func (splzr *Specializer) SkipSpecialize(pkg Package, appl *apply) (bool, error)
- func (splzr *Specializer) Sort()
- func (splzr *Specializer) Specialize(pkg Package, appl *apply) (string, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SuggestTypesToGenerate ¶
SuggestTypesToGenerate will suggest specialization signatures for types that are missing based on the error detected. e.g. if the error mentions and ObservableInt that is missing a field or method MapFloat32 then the specialization signature will become "ObservableInt MapFloat32" this signature can then be used by specialization code to generate the required type, field or method definition.
Types ¶
type Generic ¶
type Generic struct {
// Packagename is the name of the package in which the generic was found.
// e.g. "rx"
PackageName string
// Name of the generic uniquely identifies it.
// e.g. "Observable<Foo> Map<Bar>"
Name string
// Vars contains the template vars found in the generic.
// e.g. ["Foo", "Bar"]
Vars []string
// Needs contains the generics this one requires
// e.g. ["Observable<Foo>", "<Foo>Observer"]
Needs []string
// Embeds contains the names of the generics that match the types this type
// embeds using golang type embedding. Only meant for single type generics
// (e.g. "Observable<Foo>") not for "Name Method" combinations.
// This helps jig to specialize methods of embedded types to satisfy dependencies
// on the embedding type. Adds the generics for the embedded type's methods to
// the set of generics that can be specialized for this type.
// e.g. ["Observable<Foo>"]
Embeds []string
// RequiredVars contains the set of template vars that are not allowed to be unassigned.
// e.g. ["Foo"]
RequiredVars []string
// contains filtered or unexported fields
}
Generic is a struct containing information about a generic type that can be used with concrete type information to generate a specialized type.
type Package ¶
type Package interface {
// HasSourceFragment will take a fragment name and return true if this has
// already been written as part of the package.
HasSourceFragment(name string) bool
// AppendSourceFragment given a package name, a fragment name and the source
// content for a fragment will append the source to the package, returning
// an error if something goes wrong.
AppendSourceFragment(packageName, name, source string) error
}
Package is the interface expected by the specializer to append generated source fragments to the package.
type Specializer ¶
type Specializer struct {
// Generics contains the generics sorted from longest to shortest Generic.Name length used
// by Specializer.FindApply() to match type signatures to templates.
Generics []*Generic
// GoTemplates is the repository for defined go templates added via Specializer.Parse()
GoTemplates *template.Template
// contains filtered or unexported fields
}
Specializer is used during the generics definition phase to Add generics while they are found in the source of the package being processed (or any packages imported by that package). Then after that is finished, Sort is used to sort all generics once. During the type-checking there is an algorithm that detects missing types. This algorithm will then use GenerateCodeForType() to find a matching generic and then generate code that implements the missing type using the passed in PackageWriter interface.
func NewSpecializer ¶
func NewSpecializer() *Specializer
func (*Specializer) Add ¶
func (splzr *Specializer) Add(t Generic, source string) error
Add will take the source and create a template based on it.
func (*Specializer) Dot ¶
func (splzr *Specializer) Dot(types []string) map[string]string
Dot maps canconical stdVar names used in templates to actual types to replace them with. So given a list of types e.g. ["Int","Mouse","Move"] and a typemap e.g. { "Mouse": "mouse" } return Dot e.g. { "T": "Int", "t": "int", "U": "Mouse", "u": "mouse", "V": "Move", "v": "Move"}
func (*Specializer) GenerateCodeForErrors ¶
func (*Specializer) GenerateCodeForType ¶
func (splzr *Specializer) GenerateCodeForType(pkg Package, signature string) (messages []string, err error)
GenerateCodeForType will specialize generics to implement the undefined type, function, field or method specified in the signature param. And error is only returned if something unexpected went wrong during the generating process. If no code can be generated for a signature, that is not considered an error.
func (*Specializer) MapType ¶
func (splzr *Specializer) MapType(displayType, realType string)
MapType is used to tell the specializer about the real type to use instead of the display type. Usefull when generating code for non-exported types. e.g. display type MyFoo maps to real type myfoo struct { ... } The display type is used in signatures e.g. NewMyFooDict() but the actual type definitions need to know about the real type myfoo
func (*Specializer) SkipSpecialize ¶
func (splzr *Specializer) SkipSpecialize(pkg Package, appl *apply) (bool, error)
func (*Specializer) Sort ¶
func (splzr *Specializer) Sort()
After adding all generics call Sort() once to sort all generics from longest to shortest Name length. This will make longest (and therefore more precise) matches match first.
func (*Specializer) Specialize ¶
func (splzr *Specializer) Specialize(pkg Package, appl *apply) (string, error)
Specialize will specialize a specific template using the info passed in via apply. When specialization succeeds, a message describing what template has been applied is returned. If an error was encountered, that is returned as second return value.