go2ts

package module
v1.5.0 Latest Latest
Warning

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

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

README

go2ts

Go

An extremely simple and powerful Go to Typescript generator. It can handle all JSON serializable Go types and also has the ability to define TypeScript union types for your enum-like types.

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

This module does not supply a command-line interface. Just write a short Go program like the example below to generate your TypeScript files.

Install

go get github.com/skia-dev/go2ts

Example

Input:

import (
	"image/color"
	"log"
	"os"

	"github.com/skia-dev/go2ts"
)

func main() {
	type Direction string

	const (
		Up    Direction = "up"
		Down  Direction = "down"
		Left  Direction = "left"
		Right Direction = "right"
	)

	AllDirections := []Direction{Up, Down, Left, Right}

	type Position struct {
		X int
		Y int
	}

	type Turtle struct {
		Position  `json:"Coordinates"`
		Color     color.Alpha
		Direction Direction
	}

	generator := go2ts.New()
	generator.Add(Turtle{})
	generator.AddUnion(AllDirections)
	generator.Render(os.Stdout)
}

Output:

// DO NOT EDIT. This file is automatically generated.

export interface Position {
  X: number;
  Y: number;
}

export interface Alpha {
  A: number;
}

export interface Turtle {
  Coordinates: Position;
  Color: Alpha;
  Direction: Direction;
}

export type Direction = 'up' | 'down' | 'left' | 'right';

Documentation

Overview

Package go2ts is an extremely simple and powerful Go to TypeScript generator. It can handle all JSON serializable Go types and also has the ability to define TypeScript union types for your enum-like types.

Example
type Direction string

const (
	Up    Direction = "up"
	Down  Direction = "down"
	Left  Direction = "left"
	Right Direction = "right"
)

AllDirections := []Direction{Up, Down, Left, Right}

type Position struct {
	X int
	Y int
}

type Turtle struct {
	Position  Position `json:"Coordinates"`
	Color     color.Alpha
	Direction Direction
}

generator := New()
generator.Add(Turtle{})
generator.AddUnion(AllDirections)
err := generator.Render(os.Stdout)
if err != nil {
	log.Fatal(err)
}
Output:

// DO NOT EDIT. This file is automatically generated.

	X: number;
	Y: number;
}

	A: number;
}

	Coordinates: Position;
	Color: Alpha;
	Direction: Direction;
}

Index

Examples

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 definitions for Go types.

func New

func New() *Go2TS

New returns a new *Go2TS.

func (*Go2TS) Add

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

Add a type that needs a TypeScript definition.

See AddToNamespace() for more details.

func (*Go2TS) AddIgnoreNil added in v1.5.0

func (g *Go2TS) AddIgnoreNil(v interface{})

AddIgnoreNil adds a type that needs a TypeScript definition.

See AddToNamespaceIgnoreNil() for more details.

func (*Go2TS) AddMultiple added in v1.3.5

func (g *Go2TS) AddMultiple(values ...interface{})

AddMultiple adds multiple types in a single call.

See AddMultipleToNamespace() for more details.

func (*Go2TS) AddMultipleToNamespace added in v1.3.6

func (g *Go2TS) AddMultipleToNamespace(namespace string, values ...interface{})

AddMultipleToNamespace adds multiple types to the given TypeScript namespace in a single call.

Will stop at the first type that fails.

func (*Go2TS) AddMultipleUnion added in v1.3.5

func (g *Go2TS) AddMultipleUnion(values ...interface{})

AddMultipleUnion adds multple union types.

See AddMultipleUnionToNamespace() for more details.

func (*Go2TS) AddMultipleUnionToNamespace added in v1.3.6

func (g *Go2TS) AddMultipleUnionToNamespace(namespace string, values ...interface{})

AddMultipleUnionToNamespace adds multple union types to the given TypeScript namespace.

Will stop at the first union type that fails.

func (*Go2TS) AddToNamespace added in v1.3.6

func (g *Go2TS) AddToNamespace(v interface{}, namespace string)

AddToNamespace adds a type that needs a TypeScript definition to the given TypeScript namespace.

See AddWithNameToNamespace() for more details.

func (*Go2TS) AddToNamespaceIgnoreNil added in v1.5.0

func (g *Go2TS) AddToNamespaceIgnoreNil(v interface{}, namespace string)

AddToNamespaceIgnoreNil adds a type that needs a TypeScript definition to the given TypeScript namespace.

See AddWithNameToNamespaceIgnoreNil() for more details.

func (*Go2TS) AddUnion added in v1.3.0

func (g *Go2TS) AddUnion(v interface{})

AddUnion adds a TypeScript definition for a union type of the values in 'v', which must be a slice or an array.

See AddUnionToNamespace() for more details.

func (*Go2TS) AddUnionToNamespace added in v1.3.6

func (g *Go2TS) AddUnionToNamespace(v interface{}, namespace string)

AddUnionToNamespace adds a TypeScript definition for a union type of the values in 'v', which must be a slice or an array, to the given TypeScript namespace.

See AddUnionWithNameToNamespace() for more details.

func (*Go2TS) AddUnionWithName added in v1.3.0

func (g *Go2TS) AddUnionWithName(v interface{}, typeName string)

AddUnionWithName adds a TypeScript definition for a union type of the values in 'v', which must be a slice or an array.

See AddUnionWithNameToNamespace() for more details.

func (*Go2TS) AddUnionWithNameToNamespace added in v1.3.6

func (g *Go2TS) AddUnionWithNameToNamespace(v interface{}, typeName, namespace string)

AddUnionWithNameToNamespace adds a TypeScript definition for a union type of the values in 'v', which must be a slice or an array, to the given namespace.

If typeName is the empty string then the name of type of elements in the slice or array is used as the type name, otherwise the typeName supplied will be used as the TypeScript type name.

func (*Go2TS) AddWithName

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

AddWithName adds a type that needs a TypeScript definition.

See AddWithNameToNamespace() for more details.

func (*Go2TS) AddWithNameIgnoreNil added in v1.5.0

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

AddWithNameIgnoreNil adds a type that needs a TypeScript definition.

See AddWithNameToNamespaceIgnoreNil() for more details.

func (*Go2TS) AddWithNameToNamespace added in v1.3.6

func (g *Go2TS) AddWithNameToNamespace(v interface{}, interfaceName, namespace string)

AddWithNameToNamespace adds a type that needs a TypeScript definition.

The value passed in can be an instance of a type, a reflect.Type, or a reflect.Value.

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

If the type is a struct, 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 be marked as optional.

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

If namespace is non-empty, the type will be added inside a TypeScript namespace of that name.

func (*Go2TS) AddWithNameToNamespaceIgnoreNil added in v1.5.0

func (g *Go2TS) AddWithNameToNamespaceIgnoreNil(v interface{}, interfaceName, namespace string)

AddWithNameToNamespaceIgnoreNil adds a type that needs a TypeScript definition.

This method is identical to AddWithNameToNamespace, with the exception that any nillable values will be ignored (e.g. string slices will be treated as arrays, maps won't be nullable, etc.).

func (*Go2TS) Render

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

Render the TypeScript definitions to the given io.Writer.

Directories

Path Synopsis
Package typescript provides primitives to represent TypeScript types, and type declarations.
Package typescript provides primitives to represent TypeScript types, and type declarations.

Jump to

Keyboard shortcuts

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