go2typings

package module
v0.0.0-...-f55f069 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2020 License: MIT Imports: 13 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 (
	RegularType = iota
	Enum
)

Variables

This section is empty.

Functions

func Type

func Type(field *Field) (string, string)

Types

type Field

type Field struct {
	Name       string `json:"name"`
	TsType     string `json:"type"`
	KeyType    string `json:"keyType,omitempty"`
	ValType    string `json:"valType,omitempty"`
	CanBeNull  bool   `json:"canBeNull"`
	IsOptional bool   `json:"isOptional"`
	IsDate     bool   `json:"isDate"`
	T          reflect.Type
}

type GetTypeName

type GetTypeName = func(t reflect.Type) string

type Kind

type Kind int

type Options

type Options struct {
}

type Struct

type Struct struct {
	Type          Kind
	ReferenceName string
	Namespace     string
	Name          string
	Fields        []*Field
	InheritedType []string
	Values        []reflect.Value
	T             reflect.Type
}

func MakeStruct

func MakeStruct(t reflect.Type, name, namespace string) *Struct

func (*Struct) RenderEnum

func (s *Struct) RenderEnum(opts *Options, w io.Writer) (err error)

func (*Struct) RenderTo

func (s *Struct) RenderTo(opts *Options, w io.Writer) (err error)

type StructToTS

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

func New

func New() *StructToTS

func (*StructToTS) Add

func (s *StructToTS) Add(v interface{}) *Struct

func (*StructToTS) AddWithName

func (s *StructToTS) AddWithName(v interface{}, name string) *Struct

func (*StructToTS) GenerateFile

func (s *StructToTS) GenerateFile(path string) (err error)

func (*StructToTS) GenerateOpenApi

func (root *StructToTS) GenerateOpenApi(s *Struct) spec.Schema

func (*StructToTS) GetTypeName

func (root *StructToTS) GetTypeName(t reflect.Type) string

func (*StructToTS) RenderTo

func (s *StructToTS) RenderTo(w io.Writer) (err error)

func (*StructToTS) RenderToSwagger

func (s *StructToTS) RenderToSwagger() spec.Swagger

type TypescriptEnumMember

type TypescriptEnumMember struct {
	Name    string
	Value   string
	Comment string
}

Directories

Path Synopsis
tests

Jump to

Keyboard shortcuts

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