Documentation
¶
Index ¶
- func EmbedStructs(e *extractor)
- func FollowStructs(e *extractor)
- func GenerateEnumAsSumType(opt *generateOptions)
- func Render(types []TypescriptType, cfg ...GenerateOption) error
- func SortAlphabetically(e *extractor)
- type AnonStructNamer
- type DocHandler
- type EnumHandler
- type ExtractOption
- type GenerateOption
- type ParsedSourceDocHandler
- type ParsedSourceEnumHandler
- type TypeNamer
- type TypedElement
- type TypescriptEnumMember
- type TypescriptKind
- type TypescriptMember
- type TypescriptType
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func EmbedStructs ¶
func EmbedStructs(e *extractor)
EmbedStructs produces a single monolithic structure where all referenced/contained subtypes become a nested Typescript struct
func FollowStructs ¶
func FollowStructs(e *extractor)
FollowStructs enables transitive extraction of structs. By default we just emit that struct's name.
func GenerateEnumAsSumType ¶
func GenerateEnumAsSumType(opt *generateOptions)
GenerateEnumAsSumType causes enums to be be rendered as sum types
func Render ¶
func Render(types []TypescriptType, cfg ...GenerateOption) error
Render produces TypeScript code
func SortAlphabetically ¶
func SortAlphabetically(e *extractor)
SortAlphabetically sorts all types and their members alphabetically
Types ¶
type AnonStructNamer ¶
type AnonStructNamer func(reflect.StructField) string
AnonStructNamer gives a name to an otherwise anonymous struct
type DocHandler ¶
type DocHandler interface { // Type retrieves documentation for a type Type(t reflect.Type) string // Method retrieves documentation for an interface's method Method(parent reflect.Type, method reflect.Method) string }
DocHandler provides documentation for types
type EnumHandler ¶
type EnumHandler interface { // IsEnum determines whether a type is an enum or not IsEnum(t reflect.Type) bool // GetMember returns all members of an enum GetMember(t reflect.Type) ([]TypescriptEnumMember, error) }
EnumHandler can determine if a type is an "enum" and retrieve its options
type ExtractOption ¶
type ExtractOption func(*extractor)
ExtractOption is an option used with the Extract function
func CustomNamer ¶
func CustomNamer(namer TypeNamer) ExtractOption
CustomNamer sets a custom function for translating Golang naming convention to Typescript naming convention. This function does not have to translate the type names, just the way they are written.
func NameAnonStructs ¶
func NameAnonStructs(namer AnonStructNamer) ExtractOption
NameAnonStructs enables non-monolithic extraction of anonymous structs. Consider `struct { foo: struct { bar: int } }` where foo has an anonymous struct as type - with NameAnonStructs set, we'd extract that struct as its own Typescript interface.
func PrimitiveNamer ¶
func PrimitiveNamer(namer TypeNamer) ExtractOption
PrimitiveNamer sets a custom function for translating Golang naming convention to Typescript naming convention for primitive types. This function does not have to translate the type names, just the way they are written. Returning an empty string will cause bel to use the underlying primitive type
func WithDocumentation ¶
func WithDocumentation(handler DocHandler) ExtractOption
WithDocumentation configures a documentation handler which extracts documentation for types and methods.
func WithEnumerations ¶
func WithEnumerations(handler EnumHandler) ExtractOption
WithEnumerations configures an enum handler which detects and extracts enums from types and constants.
type GenerateOption ¶
type GenerateOption func(*generateOptions)
GenerateOption is an option used with the Generate function
func GenerateAdditionalPreamble ¶
func GenerateAdditionalPreamble(preamble string) GenerateOption
GenerateAdditionalPreamble produces additional output at the beginning of the Typescript code
func GenerateNamespace ¶
func GenerateNamespace(ns string) GenerateOption
GenerateNamespace produces a namespace in which the generated types live
func GenerateOutputTo ¶
func GenerateOutputTo(out io.Writer) GenerateOption
GenerateOutputTo sets the writer to which we'll write the generated TS code
func GeneratePreamble ¶
func GeneratePreamble(preamble string) GenerateOption
GeneratePreamble produces output at the beginning of the Typescript code
type ParsedSourceDocHandler ¶
type ParsedSourceDocHandler struct {
// contains filtered or unexported fields
}
ParsedSourceDocHandler provides Go doc documentation from
func NewParsedSourceDocHandler ¶
func NewParsedSourceDocHandler(srcdir, base string) (*ParsedSourceDocHandler, error)
NewParsedSourceDocHandler creates a new doc handler with a single pkg in its index
func (*ParsedSourceDocHandler) AddToIndex ¶
func (h *ParsedSourceDocHandler) AddToIndex(src, pkg string) error
AddToIndex adds another package to the handler's index. src is the path to the Go src folder of the package, pkg is its import path
type ParsedSourceEnumHandler ¶
type ParsedSourceEnumHandler struct {
// contains filtered or unexported fields
}
ParsedSourceEnumHandler discovers enums from type and const statements
func NewParsedSourceEnumHandler ¶
func NewParsedSourceEnumHandler(srcdir string) (*ParsedSourceEnumHandler, error)
NewParsedSourceEnumHandler creates a new enum handler that parses source code to discover enums
func (*ParsedSourceEnumHandler) GetMember ¶
func (h *ParsedSourceEnumHandler) GetMember(t reflect.Type) ([]TypescriptEnumMember, error)
GetMember returns all members/values of an enum
type TypeNamer ¶
TypeNamer translates Go type name convention to Typescript name convention. This function does not have to map Go types to Typescript types.
type TypedElement ¶
type TypedElement struct { Name string Type TypescriptType }
TypedElement pairs a name with a type
type TypescriptEnumMember ¶
TypescriptEnumMember is a member of a Typescript enum
type TypescriptKind ¶
type TypescriptKind string
TypescriptKind is the kind of a Typescript type (akin to the kind of Go types)
const ( // TypescriptSimpleKind means the type is merely a symbol TypescriptSimpleKind TypescriptKind = "simple" // TypescriptArrayKind means the type is an array TypescriptArrayKind TypescriptKind = "array" // TypescriptMapKind means the type is a map/dict TypescriptMapKind TypescriptKind = "map" // TypescriptInterfaceKind means the type is an interface TypescriptInterfaceKind TypescriptKind = "iface" // TypescriptEnumKind means the type is an enum TypescriptEnumKind TypescriptKind = "enum" )
type TypescriptMember ¶
type TypescriptMember struct { TypedElement Comment string IsOptional bool IsFunction bool Args []TypedElement }
TypescriptMember is a member of a Typescript interface
type TypescriptType ¶
type TypescriptType struct { Name string Comment string Kind TypescriptKind Members []TypescriptMember Params []TypescriptType EnumMembers []TypescriptEnumMember }
TypescriptType describes a type in the Typescript world
func Extract ¶
func Extract(s interface{}, opts ...ExtractOption) ([]TypescriptType, error)
Extract uses reflection to extract the information required to generate Typescript code