tots

package module
v0.0.0-...-5aff42a Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2024 License: Apache-2.0 Imports: 3 Imported by: 0

README

fork github.com/tkrajina/typescriptify-golang-structs/tscriptify

generate typescript interface for golang type

  • adjust comment position
  • remove ts_transform logic
  • remove convert to file
  • remove convert to class, only interface

example

type UserUid struct {
	Uid uint `gorm:"not null" json:"uid" ts_doc:"用户的id"`
}
type SocialAttribute struct {
	GORMModel
	UserUid

	Follows []uint `gorm:"type:integer[]" json:"follows"`
	Fans    []uint `gorm:"type:integer[]" json:"fans"`
}
package main
import ""
func exportTypescript() {
	converter := tots.
		New().
		ManageType(time.Time{}, tots.TypeOptions{TSType: "string"}).
		Add(model.SocialAttribute{})
	str, err := converter.Convert()
	if err != nil {
		panic(err.Error())
	}
	fmt.Println(str)
}
func main() {
    exportTypescript()
}

will export

/* Do not change, this code is generated from Golang structs */


export interface SocialAttribute {
    id: number;
    createdAt: string;
    updatedAt: string;
    /**
     *
     * 用户的id
     */
    uid: number;
    follows: number[];
    fans: number[];
}

TODO

  • []use validate judge generate field type

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func UnionTsType

func UnionTsType[T any](_items ...T) string

Types

type StructType

type StructType struct {
	Type         reflect.Type
	FieldOptions map[reflect.Type]TypeOptions
}

StructType stores settings for transforming one Golang struct.

func NewStruct

func NewStruct(i interface{}) *StructType

func (*StructType) WithFieldOpts

func (st *StructType) WithFieldOpts(i interface{}, opts TypeOptions) *StructType

type TSNamer

type TSNamer interface {
	TSName() string
}

type TypeOptions

type TypeOptions struct {
	TSType string
	TSDoc  string
}

TypeOptions overrides options set by `ts_*` tags.

type TypeScriptify

type TypeScriptify struct {
	Prefix     string
	Suffix     string
	Indent     string
	DontExport bool
	// contains filtered or unexported fields
}

func New

func New() *TypeScriptify

func (*TypeScriptify) Add

func (t *TypeScriptify) Add(objs ...interface{}) *TypeScriptify

func (*TypeScriptify) AddType

func (t *TypeScriptify) AddType(typeOf reflect.Type) *TypeScriptify

func (*TypeScriptify) Convert

func (t *TypeScriptify) Convert() (string, error)

func (*TypeScriptify) Debug

func (ts *TypeScriptify) Debug() *TypeScriptify

func (*TypeScriptify) ManageType

func (t *TypeScriptify) ManageType(fld interface{}, opts TypeOptions) *TypeScriptify

ManageType can define custom options for fields of a specified type.

This can be used instead of setting ts_type and ts_transform for all fields of a certain type.

func (*TypeScriptify) WithIndent

func (t *TypeScriptify) WithIndent(i string) *TypeScriptify

func (*TypeScriptify) WithPrefix

func (t *TypeScriptify) WithPrefix(p string) *TypeScriptify

func (*TypeScriptify) WithSuffix

func (t *TypeScriptify) WithSuffix(s string) *TypeScriptify

Jump to

Keyboard shortcuts

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