Created
May 14, 2018 18:12
-
-
Save khajer/562628c6a854a22f61abec93b8802995 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| func upperCaseText(str string) string{ | |
| return strings.ToUpper(str[0:1])+str[1:] | |
| } | |
| func parseData(dat map[string]interface{}, c GClass){ | |
| c.ClassName = c.ClassName | |
| str := "" | |
| for k,v := range dat{ | |
| vv := v | |
| str += k+":" | |
| t := reflect.ValueOf(vv) | |
| flgArr := false | |
| switch t.Kind() { | |
| case reflect.Slice: | |
| flgArr = true | |
| a := vv.([]interface{}) | |
| vv = a[0] | |
| t = reflect.ValueOf(vv) | |
| } | |
| if flgArr{ | |
| str += "[" | |
| } | |
| switch t.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" | |
| } | |
| 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) | |
| flgWrite := true | |
| if flgWrite == true{ | |
| err := os.MkdirAll("build", os.ModePerm) | |
| if err != nil { | |
| panic(err) | |
| } | |
| err = ioutil.WriteFile("./build/"+filename, []byte(sTxt), 0644) | |
| if err != nil { | |
| panic(err) | |
| } | |
| }else{ | |
| fmt.Println(sTxt) | |
| } | |
| } | |
| func feedStringWithProperties(c GClass) string{ | |
| var tmpFile =`{{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}} | |
| } | |
| }` | |
| 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(tmpFile)) | |
| err := t.Execute(&tpl, c) | |
| if err!= nil{ | |
| panic(err) | |
| } | |
| return tpl.String() | |
| } | |
| func main() { | |
| fmt.Println("########## [start process] ##########") | |
| fileTemplate := "./template.json" | |
| dat, err := ioutil.ReadFile(fileTemplate) | |
| if err != nil { | |
| fmt.Println("can not ReadFile") | |
| } | |
| jsonStr := string(dat) | |
| // fmt.Println(jsonStr) | |
| var datJson map[string]interface{} | |
| rootClass := GClass{ClassName:"Root",Properties:""} | |
| json.Unmarshal([]byte(jsonStr), &datJson) | |
| parseData(datJson, rootClass) | |
| feedDataWrite(arClass) | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "status": "OK", | |
| "message": "SUCCESS", | |
| "ar":["test", "est2"], | |
| "result": { | |
| "application": { | |
| "applicationId": 0, | |
| "applicationNo": null, | |
| "applicationRunningNo": null, | |
| "applicationStatus": "CBS", | |
| "applicationType": null, | |
| "applicationYearMonth": null, | |
| "approverOwner": null, | |
| "channel": null, | |
| "companyCode": null, | |
| "companyName": null, | |
| "costCenter": null, | |
| "createBy": null, | |
| "createDate": null, | |
| "custCifNo": "000012431786", | |
| "email": null, | |
| "exportBy": null, | |
| "exportDate": null, | |
| "exportFlag": null, | |
| "firstDecisionDate": null, | |
| "firstDecisionText": null, | |
| "firstSubmitDate": null, | |
| "isActive": null, | |
| "isDailyReport": null, | |
| "isOwner": null, | |
| "isRegisterSms": null, | |
| "ktbVisaDebitRate": null, | |
| "lastDecisionDate": null, | |
| "lastDecisionText": null, | |
| "localDebitCardRate": null, | |
| "merchantAvgSaleTransaction": null, | |
| "merchantAvgSaleVolume": null, | |
| "merchantId": 0, | |
| "merchantMccGroup": null, | |
| "merchantNameEng": null, | |
| "merchantNameThai": "สาขาสงขลา+97378", | |
| "merchantPhoneNo": null, | |
| "merchantRegType": null, | |
| "merchant_Tax_Ktb_Rate": null, | |
| "merchantWhtRate": null, | |
| "merchantZoneCode": null, | |
| "otherRate": null, | |
| "productType": null, | |
| "qrMerchantCode": null, | |
| "reSubmitDate": null, | |
| "saleBranch": null, | |
| "saleDepartmentCode": null, | |
| "saleId": null, | |
| "saleName": null, | |
| "saleOwner": null, | |
| "salePhoneNumber": null, | |
| "salePosition": null, | |
| "satelliteBy": null, | |
| "satelliteDate": null, | |
| "satelliteFlag": null, | |
| "specialFlag": null, | |
| "suspectListDate": null, | |
| "suspectListFlag": null, | |
| "suspectListNameEn": null, | |
| "suspectListNameTh": null, | |
| "taxId": "1959900081633", | |
| "updateBy": null, | |
| "updateDate": null, | |
| "use_Register_Tax": null | |
| }, | |
| "eligible": 1, | |
| "merchant": null, | |
| "whitelist": "" | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment