reader

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2020 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Root directory to scan for go source files
	RootDir string `json:"rootDir"`

	Types TypesConfig `json:"types,omitempty"`

	// Output options
	Output Output `json:"output,omitempty"`

	// Whether to scan directories recursively
	Recursive bool `json:"recursive,omitempty"`

	// Custom global transformation
	// Allows applying gots options globally to specific types instead of using gots tags in code
	Transforms []*Transform `json:"transforms"`

	// Paths/globs to include
	// If unset, all files found in root directory will be parsed (and subdirectories if recursive is set)
	Include []string `json:"include,omitempty"`

	// Paths/globs to exclude
	Exclude []string `json:"exclude,omitempty"`
}

Parser options

type Constant

type Constant struct {
	Name  string `json:"name"`  // Constant name
	Type  *Type  `json:"type"`  // Constant type
	Value string `json:"value"` // Constant value

	EnumValue bool   `json:"enumValue"` // This const defines an enum value
	EnumName  string `json:"enumName"`  // Enum name that this const defines a value for
}

type Enum

type Enum struct {
	Name   string      `json:"name"`
	Values []*Constant `json:"values"`
}

options for TypeScript enum

type ExtendsInterface

type ExtendsInterface struct {
	Name string `json:"name"` // Interface name
	From string `json:"from"` // Name of package containing this interface
}

interface extension opts

type File

type File struct {
	Constants  []*Constant  `json:"constants"`  // constants
	Interfaces []*Interface `json:"interfaces"` // interfaces
	Types      []*TypeAlias `json:"types"`      // types
	Enums      []*Enum      `json:"enums"`      // enums
	EnumValues []*Constant  `json:"enumValues"`
	SourcePath string       `json:"sourcePath"` // original source path
}

contains interfaces, types, and constants that were found in the same file

func (*File) EachConst

func (f *File) EachConst(fx func(t *Constant) (bool, error)) error

func (*File) EachInterface

func (f *File) EachInterface(fx func(iface *Interface) (bool, error)) error

func (*File) EachTypeAlias

func (f *File) EachTypeAlias(fx func(t *TypeAlias) (bool, error)) error

type Interface

type Interface struct {
	Name       string            `json:"name"`                 // Interface name
	Properties []*Property       `json:"properties,omitempty"` // Interface properties
	Extends    *ExtendsInterface `json:"extends,omitempty"`    // Extends another interface
}

interface options

type Output

type Output struct {
	// Output mode
	// Defaults to packages
	Mode OutputMode `json:"mode,omitempty"`

	// Root directory to output file(s) to
	// Default to current directory
	Path string `json:"path,omitempty"`

	// Filename to use when using AIO mode
	// Defaults to `types.ts`
	AIOFileName string `json:"aioFileName,omitempty"`
}

Output config

type OutputMode

type OutputMode string

Enum for output modes

const (
	// output all parsed types in a single file
	AIO OutputMode = "aio"

	// output each package into a separate file
	Packages OutputMode = "packages"

	// output directories and files that match the go source code
	Mirror OutputMode = "mirror"
)

type Package

type Package struct {
	Name  string   `json:"name"`
	Files []string `json:"files"`

	PkgAst   *ast.Package `json:"-"`
	FilesAst []*ast.File  `json:"-"`

	Consts     map[string]*Constant  `json:"constants"` // constants
	Structs    map[string]*Interface `json:"structs"`
	Interfaces map[string]*Interface `json:"interfaces"` // interfaces
	Types      map[string]*TypeAlias `json:"types"`      // types
	Enums      map[string]*Enum      `json:"enums"`      // enums
	EnumValues map[string]*Constant  `json:"enumValues"`

	Decls          []ast.Decl
	ConstDecls     []*ast.GenDecl
	StructDecls    []*ast.TypeSpec
	InterfaceDecls []*ast.TypeSpec
	TypeDecls      []*ast.TypeSpec

	Path string `json:"path"` // original source path
}

Package represents a group of files that exist in the same file/module

func NewPackage

func NewPackage(path string, pkg *ast.Package) *Package

type Property

type Property struct {
	Name     string `json:"name"`     // Property name
	Type     *Type  `json:"type"`     // Property type
	Optional bool   `json:"optional"` // Property is optional

	Inline bool `json:"inline"`
}

property options

type ReadConfig

type ReadConfig struct {
	Dir       string `json:"dir"`
	Recursive bool   `json:"recursive"`
}

func (*ReadConfig) Directories

func (rc *ReadConfig) Directories() ([]string, error)

func (*ReadConfig) Files

func (rc *ReadConfig) Files() ([]string, error)

Returns a list of files matching the provided config

type ReadFn

type ReadFn func(config *ReadConfig) (Spec, error)

type Spec

type Spec interface {
	Packages() []*Package // Returns the packages that were parsed
}

Root scope that contains uniform description of interfaces, types, constants, enums... etc

type Transform

type Transform struct {
	// Regex pattern to replace
	Pattern string `json:"pattern,omitempty"`

	// Exact type to replace
	// Ignored if pattern is set
	Type string `json:"type,omitempty"`

	// Type to use instead of pattern/from
	Target string `json:"target,omitempty"`

	// Force matches to be optional
	// If not set, it will fall back to default behaviour (parse tags)
	Optional bool `json:"optional,omitempty"`
}

Transform config

type Type

type Type struct {
	Name string `json:"name"` // Type name
	From string `json:"from"` // Package name that contains this type

	EnumValue bool `json:"enumValue"`

	Generic bool `json:"generic"` // Type is generic

	Array bool `json:"array"` // Type is an array

	Map      bool  `json:"bool"`     // Type is a map
	MapKey   *Type `json:"mapKey"`   // Map key type
	MapValue *Type `json:"mapValue"` // Map value type

	// Whether this type is a pointer
	Pointer bool `json:"pointer"`
}

Typescript type options

func (*Type) IsEmpty

func (t *Type) IsEmpty() bool

func (*Type) TSType

func (t *Type) TSType() string

type TypeAlias

type TypeAlias struct {
	Name        string `json:"name"`        // Alias name
	AliasedType *Type  `json:"aliasedType"` // Aliased type
}

Opts for TypeScript type alias

type TypesConfig

type TypesConfig struct {
	// Go interfaces
	Interfaces bool `json:"interfaces"`

	// Variable declarations that have default values
	Variables bool `json:"variables"`

	// Const declarations
	Constants bool `json:"constants"`

	// Type aliases
	Aliases bool `json:"aliases"`

	// Go structs
	Structs bool `json:"structs"`

	// Generate enum from type alias and constant combinations
	Enums bool `json:"enums"`
}

Configures what types to export

type WriteConfig

type WriteConfig struct {
	Output     Output       `json:"output"`
	Transforms []*Transform `json:"transforms,omitempty"`
	Packages   []*Package   `json:"packages,omitempty"`
}

type Writer

type Writer interface {
	// Write package(s) with the provided output config and transforms
	// Return a map with output data for each package
	Write(spec Spec, config *WriteConfig) (map[string][]byte, error)
}

Jump to

Keyboard shortcuts

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