tomgos

package module
v0.0.0-...-4673145 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2021 License: MIT Imports: 9 Imported by: 0

README

tomgos

tomgos is a simple app to generate Golang struct based on TOML file.

Features

  • Detect field type based on value of the TOML
  • Detect multiple types of field
    • string
    • int64
    • map[string]interface
    • time
    • slice of type:
      • string
      • int
      • existing/inherited struct
      • map[string]interface
  • Detect other struct (inherited struct) as field type

Supported TOML

Just put a TOML file which represent your data. You can make/convert it from JSON from this site: https://toolkit.site/format.html

Example working TOML:

[recipe]
    id = "this-is-id"
    title = "recipe title"
    description = "short description"
    cooking_time = 100
    portion = 1
    create_time = "1987-07-05T05:45:00Z"
    metadata = {}
    chef = ['ridho', 'budi']
    category_ids = [1, 3, 4]
    metas = [{}]
Nested Struct

There are 2 ways for including nested struct as field type

  1. Using {video} bracket followed by struct's name inside of it. Example:

    [recipe]
        id = "this-is-id"
        title = "recipe title"
        description = "short description"
        cooking_time = 100
        portion = 1
        create_time = "1987-07-05T05:45:00Z"
        video = "{video}"
    
    [video]
        url = "http://url.com"
    

    Result:

    package tomgos
    
    import (
    	"time"
    )
    
    type recipe struct {
    	Description string    `json:"description"`
    	CookingTime int64     `json:"cooking_time"`
    	Portion     int64     `json:"portion"`
    	CreateTime  time.Time `json:"create_time"`
    	Video       video     `json:"video"`
    	Id          string    `json:"id"`
    	Title       string    `json:"title"`
    }
    
    type video struct {
    	Url string `json:"url"`
    }
    
  2. Write the children key toml inside the parent key. Example:

    [recipe]
        id = "this-is-id"
        title = "recipe title"
        description = "short description"
        cooking_time = 100
        portion = 1
        create_time = "1987-07-05T05:45:00Z"
    
        [recipe.video]
           url = "http://url.com"
    
        [[recipe.thumbnail]]
           url = "http://image.com" 
    
    [video]
        url = "http://url.com"
    
    [thumbnail]
        url = "http://image.com"
    

    Result:

    package tomgos
    
    import (
    	"time"
    )
    
    type recipe struct {
    	Thumbnail   []thumbnail `json:"thumbnail"`
    	Id          string      `json:"id"`
    	Title       string      `json:"title"`
    	Description string      `json:"description"`
    	CookingTime int64       `json:"cooking_time"`
    	Portion     int64       `json:"portion"`
    	CreateTime  time.Time   `json:"create_time"`
    	Video       video       `json:"video"`
    }
    
    type video struct {
    	Url string `json:"url"`
    }
    
    type thumbnail struct {
    	Url string `json:"url"`
    }
    

How to Use

  1. Build the binary. Run make
  2. Run ./tomgos generate to run the App.

TO-DO

  • Support nested data structure
  • Support dynamic object
  • Support JSON descriptor
  • Support slice string/int field type
  • Support slice nested struct field type
  • Support slice of dynamic object field type
  • Unit Test

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type GeneratedData

type GeneratedData struct {
	PackageName string
	Structs     []Struct
	UsingTime   bool
}

type Generator

type Generator interface {
	Generate(tomlPathFile string) ([]byte, error)
}

func NewGenerator

func NewGenerator(packageName, templateFilePath string) Generator

type Struct

type Struct struct {
	StructName string
	Fields     []StructFields
}

type StructFields

type StructFields struct {
	Name           string
	Type           string
	JSONDescriptor string
}

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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