Begin work on generate-structs for flexible assets

This commit is contained in:
Steven Polley 2018-07-22 22:39:46 -06:00
parent c2d7d160a3
commit 9a9079e42d
2 changed files with 65 additions and 14 deletions

60
generate-structs/main.go Normal file
View File

@ -0,0 +1,60 @@
package main
import (
"bytes"
"fmt"
"log"
"os"
"strconv"
"deadbeef.codes/steven/go-itg/itglue"
"github.com/ChimeraCoder/gojson"
)
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)
}
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)
}
name := &fat.FlexibleAssetTypeData.Attributes.Name
pkg := "itglue"
subStruct := false // try changing to true?
tagList := make([]string, 0)
tagList = append(tagList, "json")
var convertFloats bool
var parser gojson.Parser
parser = gojson.ParseJson
convertFloats = true
input := bytes.NewReader(fa)
output, err := gojson.Generate(input, parser, *name, pkg, tagList, subStruct, convertFloats)
fmt.Print(string(output))
}
}

View File

@ -32,25 +32,16 @@ type FlexibleAssetList struct {
Links struct{ Links } `json:"links"` Links struct{ Links } `json:"links"`
} }
//Rather than a "GetFlexibleAssets", we need to implement getters for each flexible asset type. This should probably be automated and go code should be generated by some external program. //GetFlexibleAssetsJSON is a special function. All flexible assets will return different data depending
func (itg *ITGAPI) GetFlexibleAssets(flexibleAssetTypeID int) error { //on which fields make up the flexible asset. Because of this, this function simply returns the JSON bytes.
func (itg *ITGAPI) GetFlexibleAssetsJSON(flexibleAssetTypeID int) ([]byte, error) {
req := itg.NewRequest("/flexible_assets", "GET", nil) req := itg.NewRequest("/flexible_assets", "GET", nil)
req.RawURLValues = fmt.Sprintf("filter[flexible_asset_type_id]=%d", flexibleAssetTypeID) req.RawURLValues = fmt.Sprintf("filter[flexible_asset_type_id]=%d", flexibleAssetTypeID)
err := req.Do() err := req.Do()
if err != nil { if err != nil {
return fmt.Errorf("request failed for %s: %s", req.RestAction, err) return nil, fmt.Errorf("request failed for %s: %s", req.RestAction, err)
} }
fmt.Println(string(req.Body)) return req.Body, nil
/*
flexibleAssets := &FlexibleAssetList{}
err = json.Unmarshal(req.Body, flexibleAssets)
if err != nil {
return nil, fmt.Errorf("could not get flexible asset: %s", err)
}
return flexibleAssets, nil
*/
return nil
} }