Documentation
¶
Index ¶
- func CommentForObject(obj types.Object, pkg *packages.Package) []bindings.SyntheticComment
- func RecordReference(key, value bindings.ExpressionType) *bindings.ReferenceType
- type GoParser
- func (p *GoParser) CommentForObject(obj types.Object) []bindings.SyntheticComment
- func (p *GoParser) ExcludeCustom(fqnames ...string) error
- func (p *GoParser) Identifier(obj types.Object) bindings.Identifier
- func (p *GoParser) IncludeCustom(mappings map[GolangType]GolangType) error
- func (p *GoParser) IncludeCustomDeclaration(mappings map[string]TypeOverride)
- func (p *GoParser) IncludeGenerate(directory string) error
- func (p *GoParser) IncludeGenerateWithPrefix(directory string, prefix string) error
- func (p *GoParser) IncludeReference(directory string, prefix string) error
- func (p *GoParser) PreserveComments() *GoParser
- func (p *GoParser) ToTypescript() (*Typescript, error)
- type GolangType
- type MutationFunc
- type TypeOverride
- type Typescript
- func (ts *Typescript) ApplyMutations(muts ...MutationFunc)
- func (ts *Typescript) ForEach(node func(key string, node bindings.Node))
- func (ts *Typescript) Node(key string) (bindings.Node, bool)
- func (ts *Typescript) ReplaceNode(key string, node bindings.Node)
- func (ts *Typescript) Serialize() (string, error)
- func (ts *Typescript) SerializeInOrder(sort func(nodes map[string]bindings.Node) []bindings.Node) (string, error)
- func (ts *Typescript) SetNode(key string, node bindings.Node) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CommentForObject ¶ added in v1.6.0
CommentForObject returns the comment group associated with the object's declaration. For functions/methods it returns FuncDecl.Doc. For const/var/type it prefers Spec.Doc, else GenDecl.Doc. For struct/interface members it returns Field.Doc, else Field.Comment (trailing). Disclaimer: A lot of this code was AI generated. Feel free to improve it!
func RecordReference ¶
func RecordReference(key, value bindings.ExpressionType) *bindings.ReferenceType
RecordReference creates a reference to the 'Record' type in Typescript. The Record type takes in 2 type parameters, key and value.
Types ¶
type GoParser ¶
type GoParser struct {
Pkgs map[string]*packages.Package
Reference map[string]bool
Prefix map[string]string
Skips map[string]struct{}
// contains filtered or unexported fields
}
GoParser takes in Golang packages, and can convert them to the intermediate typescript representation. The intermediate representation is closely aligned with the typescript AST.
func NewGolangParser ¶
NewGolangParser returns a new GoParser object. This object is responsible for converting Go types into the intermediate typescript AST representation. All configuration of the GoParser should be done before calling 'ToTypescript'. For usage, see 'ExampleGeneration' in convert_test.go.
Example ¶
package main
import (
"fmt"
"github.com/coder/guts"
"github.com/coder/guts/config"
)
func main() {
// gen will convert the golang package to the typescript AST.
// Configure it before calling ToTypescript().
gen, _ := guts.NewGolangParser()
// Pass in the directory of the package you want to convert.
// You can mark a package as 'false' to include it as a reference, but not
// generate types for it.
_ = gen.IncludeGenerate("github.com/coder/guts/testdata/generics")
// Default type mappings are useful, feel free to add your own
gen.IncludeCustomDeclaration(config.StandardMappings())
_ = gen.IncludeCustom(map[string]string{
// To configure a custom type for a golang type, use the full package path.
"github.com/coder/guts/testdata/generics.ExampleType": "string",
// You can use golang type syntax to specify a type.
"github.com/coder/guts/testdata/generics.AnotherExampleType": "map[string]*string",
})
// ts is the typescript AST. It can be mutated before serializing.
ts, _ := gen.ToTypescript()
ts.ApplyMutations(
// Generates a constant which lists all enum values.
config.EnumLists,
// Adds 'readonly' to all interface fields.
config.ReadOnly,
// Adds 'export' to all top level types.
config.ExportTypes,
)
output, _ := ts.Serialize()
// Output is the typescript file text
fmt.Println(output)
}
func (*GoParser) CommentForObject ¶ added in v1.6.0
func (p *GoParser) CommentForObject(obj types.Object) []bindings.SyntheticComment
func (*GoParser) ExcludeCustom ¶ added in v1.6.0
ExcludeCustom flags golang types to not be generated in the Typescript output.
func (*GoParser) Identifier ¶
func (p *GoParser) Identifier(obj types.Object) bindings.Identifier
ObjectName returns the name of the object including any prefixes defined by the config.
func (*GoParser) IncludeCustom ¶
func (p *GoParser) IncludeCustom(mappings map[GolangType]GolangType) error
IncludeCustom takes in a remapping of golang types. Both the key and value of the map should be valid golang types. The key is the type to override, and the value is the new type. Typescript will be generated with the new type.
Only named types can be overridden. Examples: "github.com/your/repo/pkg.ExampleType": "string" "time.Time": "string"
func (*GoParser) IncludeCustomDeclaration ¶
func (p *GoParser) IncludeCustomDeclaration(mappings map[string]TypeOverride)
IncludeCustomDeclaration is an advanced form of IncludeCustom.
func (*GoParser) IncludeGenerate ¶
IncludeGenerate parses a directory and adds the parsed package to the list of packages. These package's types will be generated.
func (*GoParser) IncludeGenerateWithPrefix ¶
IncludeGenerateWithPrefix will include a prefix to all output generated types.
func (*GoParser) IncludeReference ¶
IncludeReference only generates types if they are referenced from the generated packages. This is useful for only generating a subset of the types that are being used.
func (*GoParser) PreserveComments ¶ added in v1.6.0
PreserveComments will attempt to preserve any comments associated with the golang types. This feature is still a work in progress, and may not preserve all comments or match all expectations.
func (*GoParser) ToTypescript ¶
func (p *GoParser) ToTypescript() (*Typescript, error)
ToTypescript translates the Go types into the intermediate typescript AST The returned typescript object can be mutated before serializing.
type GolangType ¶
type GolangType = string
type MutationFunc ¶ added in v1.6.0
type MutationFunc func(typescript *Typescript)
type TypeOverride ¶
type TypeOverride func() bindings.ExpressionType
type Typescript ¶
type Typescript struct {
// contains filtered or unexported fields
}
func (*Typescript) ApplyMutations ¶
func (ts *Typescript) ApplyMutations(muts ...MutationFunc)
func (*Typescript) ForEach ¶
func (ts *Typescript) ForEach(node func(key string, node bindings.Node))
ForEach iterates through all the nodes in the typescript AST.
func (*Typescript) ReplaceNode ¶
func (ts *Typescript) ReplaceNode(key string, node bindings.Node)
func (*Typescript) Serialize ¶
func (ts *Typescript) Serialize() (string, error)
Serialize will serialize the typescript AST to typescript code. It sorts all types alphabetically.