go-itg/generate-structs/main.go
2018-08-15 16:50:03 -06:00

60 lines
1.1 KiB
Go

package main
import (
"bytes"
"fmt"
"jflect"
"log"
"os"
"strconv"
"deadbeef.codes/steven/go-itg/itglue"
)
var itg *itglue.ITGAPI
func init() {
apiKey := os.Getenv("itgapikey")
if apiKey == "" {
log.Fatalf("API key is not set")
}
itg = itglue.NewITGAPI(apiKey)
}
func main() {
fats, err := itg.GetFlexibleAssetTypes()
if err != nil {
log.Fatalf("could get get FATs: %s", err)
}
w, _ := os.Create("flexible-asset-types.go")
w.WriteString("package itglue\n\n")
for _, fat := range fats.Data {
fmt.Printf("%s - %s\n", fat.FlexibleAssetTypeData.ID, fat.FlexibleAssetTypeData.Attributes.Name)
id, err := strconv.Atoi(fat.ID)
if err != nil {
log.Fatalf("could not convert %s to integer: %s", fat.ID, err)
}
fa, err := itg.GetFlexibleAssetsJSON(id)
if err != nil {
log.Fatalf("could get flexible asset with type ID %d: %s", id, err)
}
r := bytes.NewReader(fa)
fatName := fmt.Sprintf("FlexAsset%s", fat.FlexibleAssetTypeData.ID)
err = jflect.Read(fatName, r, w)
if err != nil {
log.Fatalf("could not parse json to struct: %s", err)
}
w.WriteString("\n\n")
//name := &fat.FlexibleAssetTypeData.Attributes.Name
}
}