/Type2Go

Convert Data Models of Typescript Class into Golang Struct

Primary LanguageJavaScript

Type2Go

Convert Typescript DataModel into Golang.

Supported Data Types

  • basic types (int, float, string, Date)
  • container types (Record, Map, Array)
  • nested class
  • nullable member

Supported Customization

(By Annotations)

  • package name
  • struct name
  • tags to generate by auto naming
  • extra tag contents
  • custom naming to override auto naming

Example

@GoModel({
    packageName: 'model',
    modelName: 'UserModel',
    generateTags: ['json', 'gorm', 'bson'],
})
class User extends Base {
    @ExtraTags({json: 'omitempty'})
    id: string

    @CustomNaming({bson: 'UserName'})
    name: string

    @ExtraTags({sometag: ['a', 'b']})
    someArray: Date[]

    someNullable?: string

    someMap: Map<string, int[]>

    someInlineType: {
        a: int
        b: string
    }
}

Which converts to:

// Generated By Type2Go At SomeTime //

package model

import (
    "time"
)

type UserModel struct {
    Base

    Id string `json:"id;omitempty" gorm:"id" bson:"Id"` 
    Name string `json:"name" gorm:"name" bson:"UserName"` 
    SomeArray []time.Time `json:"someArray" gorm:"some_array" bson:"SomeArray" sometag:"a,b"` 
    SomeNullable *string `json:"someNullable" gorm:"some_nullable" bson:"SomeNullable"` /* nullable */
    SomeMap map[string][]int `json:"someMap" gorm:"some_map" bson:"SomeMap"` 
    SomeInlineType struct {
        A int `json:"a"` 
        B string `json:"b"` 
    } `json:"someInlineType" gorm:"some_inline_type" bson:"SomeInlineType"` 
}

Usage

  1. edit config.js
  2. put your ts models into config.inputModelPath
    • maybe you want model_helper.ts for code suggestion and completion
  3. mkdir config.outputModelDir
  4. run node main.js