typegen

package
v0.0.0-...-207416a Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2020 License: MIT Imports: 15 Imported by: 0

README

Golang structs to typescript typings convertor

Example

example.main.go

How to setup

  • create go file with the code bellow
  • run this code with go run
package main

import (
  "github.com/zmitry/go2ts"
   // you can use your own
  "github.com/zmitry/go2ts/example/types"
)

type Root struct {
	User types.User
	T    types.T
}

func main() {
	s := go2ts.New()
	s.Add(types.T{})
	s.Add(types.User{})

	err := s.GenerateFile("./test.ts")
	if err != nil {
		panic(err)
	}
}

Custom tags

we support custom tag ts it has the following syntax

type M struct {
	Username string `json:"Username2" ts:"string,optional"`
}

tsTag type

tsTag[0] = "string"|"date"|"-"
tsTag[1] = "optional"|"no-null"|"null"

see field.go for more info

Documentation

Index

Constants

View Source
const GlobalTemplate = `` /* 313-byte string literal not displayed */
View Source
const RecordTemplate = `` /* 173-byte string literal not displayed */

Variables

This section is empty.

Functions

func FormatDoc

func FormatDoc(str string) string

func GenerateOpenApi

func GenerateOpenApi(p *Parser, s IType) spec.Schema

func PrintSwagger

func PrintSwagger(p *Parser) spec.Swagger

func PrintTsTypes

func PrintTsTypes(parser *Parser, w io.Writer, stringify Stringifier)

Types

type BaseType

type BaseType struct {
	Doc     string
	Package string
	Name    string
	T       reflect.Type // indirect type
}

func (*BaseType) GetName

func (this *BaseType) GetName() string

func (*BaseType) GetPackage

func (this *BaseType) GetPackage() string

func (*BaseType) GetType

func (this *BaseType) GetType() reflect.Type

func (*BaseType) IsType

func (*BaseType) IsType()

func (*BaseType) RefName

func (this *BaseType) RefName() string

func (*BaseType) SetName

func (this *BaseType) SetName(s, pkg string)

type EnumDef

type EnumDef struct {
	BaseType
	Values []EnumValue
}

func (*EnumDef) IsType

func (*EnumDef) IsType()

type EnumValue

type EnumValue struct {
	// contains filtered or unexported fields
}

func (*EnumValue) Stringify

func (this *EnumValue) Stringify() string

type Fn

type Fn = func(t IType)

type IType

type IType interface {
	IsType()
	GetName() string
	SetName(name, pkg string)
	GetPackage() string
	RefName() string
	GetType() reflect.Type
}

type Package

type Package struct {
	Name  string
	Types []IType
}

type ParseResult

type ParseResult struct {
	FieldName string
	FieldType string
	State     PropertyState
}

func ParseStructTag

func ParseStructTag(structTag reflect.StructTag) (*ParseResult, error)

type Parser

type Parser struct {

	// You can skip field or replace it with another type
	CustomParse func(arg reflect.Type) (IType, bool)
	// contains filtered or unexported fields
}

func NewFromTypes

func NewFromTypes(types ...interface{}) *Parser

func NewParser

func NewParser(types ...RawType) *Parser

func (*Parser) GetVisited

func (this *Parser) GetVisited(t reflect.Type) IType

func (*Parser) Parse

func (this *Parser) Parse(rawTypes ...reflect.Type)

func (*Parser) ParseRaw

func (this *Parser) ParseRaw(rawTypes ...interface{})

type PkgParseResult

type PkgParseResult struct {
	Packages *packages.Package
	Docs     *doc.Package
}

func GetPackages

func GetPackages(pkgName string) (*PkgParseResult, error)

type PropertyState

type PropertyState int
const (
	Auto PropertyState = iota
	Ignored
	Optional
	Null
	NotNull
)

type RawType

type RawType struct {
	T reflect.Type
}

type RecordDef

type RecordDef struct {
	BaseType
	Fields   []*RecordField
	Embedded []reflect.Type
}

func (*RecordDef) IsType

func (*RecordDef) IsType()

type RecordField

type RecordField struct {
	Doc   string
	Key   string
	Tag   *ParseResult
	Type  reflect.Type
	IsRef bool
}

func (*RecordField) IsType

func (*RecordField) IsType()

type Stringifier

type Stringifier = func(t reflect.Type) string

type TypeDef

type TypeDef struct {
	BaseType
}

type TypeToString

type TypeToString = func(t reflect.Type) string

Directories

Path Synopsis
tests

Jump to

Keyboard shortcuts

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