ts2go

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2024 License: MIT Imports: 9 Imported by: 0

README

Typescript To Golang

This library can be used to convert TypeScript type definitions into Golang type definitions.

Package Documentation

Documentation

Overview

Package ts2go is a modular and highly customizable code generator for converting TypeScript type definitions into Golang code.

The Generate function is the primary entry point for the package. It accepts a TypeScript source file as input and writes the generated Golang code to an output writer.

The WithMixin function can be used to customize the data that is passed to the templates. This is useful for adding custom data to the parsed types before they are rendered.

The WithTemplateOverrideDir function can be used to specify a directory that contains template overrides. This is useful for customizing the generated code without modifying the built-in templates. The templates used by this package are highly modular, allowing you to override only the parts that you need.

Example
package main

import (
	"bytes"
	"fmt"
	"strings"

	"github.com/armsnyder/ts2go"
)

func main() {
	source := strings.NewReader(`
/**
 * My type
 */
type Foo = { bar?: string /* My field */ }`)

	output := &bytes.Buffer{}

	err := ts2go.Generate(source, output)

	if err != nil {
		panic(err)
	}

	fmt.Print(output)

}
Output:

// Code generated by ts2go. DO NOT EDIT.
package types

// My type
type Foo struct {
	// My field
	Bar *string `json:"bar,omitempty"`
}
Example (Mixins)
package main

import (
	"bytes"
	"fmt"
	"strings"

	"github.com/armsnyder/ts2go"
)

func main() {
	source := strings.NewReader(`
/**
 * My type
 */
type Foo = { bar?: string /* My field */ }`)

	output := &bytes.Buffer{}

	err := ts2go.Generate(source, output, ts2go.WithMixin(
		ts2go.SetPackageName("mypackage"),
		ts2go.SkipOptionalPointer(),
		func(td *ts2go.TemplateData) {
			td.Structs[0].Name = "Override"
		},
	))

	if err != nil {
		panic(err)
	}

	fmt.Print(output)

}
Output:

// Code generated by ts2go. DO NOT EDIT.
package mypackage

// My type
type Override struct {
	// My field
	Bar string `json:"bar,omitempty"`
}

Index

Examples

Constants

This section is empty.

Variables

View Source
var DefaultPackageName = "types"

Functions

func Generate

func Generate(source io.Reader, output io.Writer, opts ...Option) error

Generate converts the type definitions in the TypeScript source code into Golang and writes the result to the output writer.

Types

type ConstGroup

type ConstGroup struct {
	Doc        []string
	CustomData CustomData
}

ConstGroup is the data model for the const_group.tmpl template.

type CustomData

type CustomData map[string]any

CustomData contains arbitrary data that can be used by template overrides.

type Field

type Field struct {
	Name       string
	Doc        []string
	Type       string
	IsPointer  bool
	JSONName   string
	OmitEmpty  bool
	CustomData CustomData
}

Field is the data model for the field.tmpl template.

type Mixin

type Mixin func(*TemplateData)

Mixin is used to modify or add custom data to the TemplateData before it is rendered.

func SetPackageName

func SetPackageName(name string) Mixin

SetPackageName specifies the package name that should be used in the generated Golang code. If not specified, DefaultPackageName is used.

func SkipHeader

func SkipHeader() Mixin

SkipHeader specifies that the generated Go code should not include the header comment and package declaration. This is useful if you want to run Generate more than once and concatenate the results.

func SkipOptionalPointer

func SkipOptionalPointer() Mixin

SkipOptionalPointer specifies that optional fields should not be represented as pointers in the generated Go code.

type Option

type Option func(*generator)

Option customizes the behavior of Generate.

func WithMixin

func WithMixin(mixins ...Mixin) Option

WithMixin specifies one or more [Mixin]s to customize the data that is passed to the templates.

func WithTemplateOverrideDir

func WithTemplateOverrideDir(dir string) Option

WithTemplateOverrideDir specifies a directory which can contain template overrides.

You may override as many or as few templates as you like. This is useful in conjunction with mixins. You can add custom data to the parsed types, and then reference that custom data inside your template override. See the templates directory for the list of templates that can be overridden.

func WithTemplateOverrideFS

func WithTemplateOverrideFS(fsys fs.FS) Option

WithTemplateOverrideFS is the same as WithTemplateOverrideDir, but takes a file system interface instead of a directory.

type Struct

type Struct struct {
	Name       string
	Doc        []string
	Embeds     []string
	Fields     []*Field
	CustomData CustomData
}

Struct is the data model for the struct.tmpl template.

type TemplateData

type TemplateData struct {
	SkipHeader  bool
	PackageName string
	Structs     []*Struct
	TypeAliases []*TypeAlias
	ConstGroups []*ConstGroup
}

TemplateData is a wrapper around all of the data which is passed to the templates. It is used by Mixin functions to mutate the data before it is rendered.

type TypeAlias

type TypeAlias struct {
	Name       string
	Doc        []string
	Type       string
	CustomData CustomData
}

TypeAlias is the data model for the type_alias.tmpl template.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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