Skip to content

Instantly share code, notes, and snippets.

@khajer
Created May 17, 2018 09:41
Show Gist options
  • Select an option

  • Save khajer/b8f46469e1f3517613d05eda2e0e23ac to your computer and use it in GitHub Desktop.

Select an option

Save khajer/b8f46469e1f3517613d05eda2e0e23ac to your computer and use it in GitHub Desktop.
json to typescript with golang
{{range .GetArrayProperties}}{{$d := SplitText . ":" }}{{$t := index $d 1}}{{$t := Replace (Replace $t "[" "" 2) "]" "" 2}} {{if or (eq $t "any") (or (eq $t "string") (eq $t "number") ) }}{{else}}
import { {{$t}} } from './{{ToLower $t}}'{{end}}{{end}}
export class {{.ClassName}} {
{{range .GetArrayProperties }} {{.}};
{{end}}
constructor(obj?: any){ {{range .GetArrayProperties }}{{$d := SplitText . ":" }}
this.{{index $d 0}} = obj && obj.{{index $d 0}} || null;{{end}}
}
}
package main
import (
"fmt"
"encoding/json"
"io/ioutil"
"strings"
"reflect"
"os"
"bytes"
"text/template"
)
type GClass struct {
ClassName string
Properties string
}
func (g GClass) GetArrayProperties() []string{
ar := strings.Split(g.Properties, ";")
return ar[0:len(ar)-1]
}
func (g GClass) SplitProperty(data string) (string, string){
ar := strings.Split(data, ":")
ar = ar[0:len(ar)-1]
return ar[0], ar[1]
}
var arClass []GClass
var TYPE_SCRIPT_TEMPLATE = "ts.tmpl"
func upperCaseText(str string) string{
return strings.ToUpper(str[0:1])+str[1:]
}
func checkTypeReflect(typeReflect reflect.Value, vv interface{}, k string) string {
str := ""
switch typeReflect.Kind() {
case reflect.Uint, reflect.Uint8, reflect.Uint32, reflect.Uint64, reflect.Float32, reflect.Float64:
str += "number"
case reflect.String:
str += "string"
case reflect.Map:
str += upperCaseText(k)
parseData(vv.(map[string]interface{}), GClass{ClassName:upperCaseText(k), Properties:""})
default:
str += "any"
}
return str
}
func checkArrayValue(typeReflect reflect.Value, newV interface{}) (bool, reflect.Value){
flgArr := false
switch typeReflect.Kind() {
case reflect.Slice:
flgArr = true
newV = newV.([]interface{})[0]
typeReflect = reflect.ValueOf(newV)
}
return flgArr, typeReflect
}
func parseData(dat map[string]interface{}, c GClass){
c.ClassName = c.ClassName
str := ""
for k,v := range dat{
newV := v
str += k+":"
typeReflect := reflect.ValueOf(newV)
flgArr, typeReflect := checkArrayValue(typeReflect, newV)
if flgArr{ str += "[" }
str += checkTypeReflect(typeReflect, newV, k)
if flgArr{str += "]"}
str += ";"
}
c.Properties = str
arClass = append(arClass, c)
}
func feedDataWrite(arClassGen []GClass){
for _, c := range arClassGen{
writeFile(c)
}
}
func writeFile(c GClass){
filename := strings.ToLower(c.ClassName)+".ts"
fmt.Println("filename: "+filename)
sTxt := feedStringWithProperties(c)
err := os.MkdirAll("build", os.ModePerm)
if err != nil {
panic(err)
}
err = ioutil.WriteFile("./build/"+filename, []byte(sTxt), 0644)
if err != nil {
panic(err)
}
}
func readFile(tmplFile string) []byte{
strTmplFile, err := ioutil.ReadFile(tmplFile)
if err != nil {
fmt.Println("can not ReadFile")
panic(err)
}
return strTmplFile
}
func feedStringWithProperties(c GClass) string{
tmpFile := readFile(TYPE_SCRIPT_TEMPLATE)
var tpl bytes.Buffer
funcMap := template.FuncMap{
"SplitText": strings.Split,
"Replace": strings.Replace,
"ToLower": strings.ToLower,
}
t := template.Must(template.New("templateFile").Funcs(funcMap).Parse(string(tmpFile)))
err := t.Execute(&tpl, c)
if err!= nil{
panic(err)
}
return tpl.String()
}
func main() {
fmt.Println("########## [start process] ##########")
fileTemplate := "./template.json"
dat := readFile(fileTemplate)
rootClass := GClass{ClassName:"Root",Properties:""}
var datJson map[string]interface{}
json.Unmarshal([]byte(dat), &datJson)
parseData(datJson, rootClass)
feedDataWrite(arClass)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment