go2ts

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2020 License: BSD-3-Clause Imports: 6 Imported by: 1

README

go2ts

An extremely simple and powerful Go struct to Typescript interface generator.

Inspired by https://github.com/OneOfOne/struct2ts.

Install

go get github.com/skia-dev/go2ts

Example

Input:

type ComplexStruct struct {
	S           string       `json:"s,omitempty"`
	I           int          `json:"i,omitempty"`
	F           float64
}

func main() {
	s := go2ts.New()
	s.Add(ComplexStruct{})
	s.Render(os.Stdout)
}

Output:

export interface ComplexStruct {
  s?: string;
  i?: number;
  F: number;
}

Documentation

Overview

Package go2ts is a module for generating TypeScript definitions from Go structs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Go2TS

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

Go2TS writes TypeScript interface definitions for the given Go structs.

func New

func New() *Go2TS

New returns a new *StructToTS.

func (*Go2TS) Add

func (g *Go2TS) Add(v interface{}) error

Add a struct that needs a TypeScript definition.

Just a wrapper for AddWithName with an interfaceName of "".

See AddWithName.

func (*Go2TS) AddWithName

func (g *Go2TS) AddWithName(v interface{}, interfaceName string) error

AddWithName adds a struct that needs a TypeScript definition.

The value passed in must resolve to a struct, a reflect.Type, or a reflect.Value of a struct. That is, a string or number for v will cause AddWithName to return an error, but a pointer to a struct is fine.

The 'name' supplied will be the TypeScript interface name. If 'interfaceName' is "" then the struct name will be used. If the struct is anonymous it will be given a name of the form "AnonymousN".

The fields of the struct will be named following the convention for json serialization, including using the json tag if supplied.

Fields tagged with `json:",omitempty"` will have "| null" added to their type.

There is special handling of time.Time types to be TypeScript "string"s since they implement MarshalJSON, see https://pkg.go.dev/time?tab=doc#Time.MarshalJSON.

func (*Go2TS) Render

func (g *Go2TS) Render(w io.Writer)

Render the TypeScript definitions to the given io.Writer.

Jump to

Keyboard shortcuts

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