tomgos is a simple app to generate Golang struct based on TOML file.
- 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
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 = [{}]
There are 2 ways for including nested struct as field type
-
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"` }
-
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"` }
- Build the binary. Run
make
- Run
./tomgos generate
to run the App.
- 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