signald-go/tools/generator/requests.go.tmpl
2022-04-10 22:41:24 +00:00

52 lines
No EOL
1.4 KiB
Cheetah

package {{.Version}}
// {{ .Banner }}
import ({{if .Responses}}
"encoding/json"
"log"{{end}}
"gitlab.com/signald/signald-go/signald"
)
{{range $type, $action := .Actions}}
{{if ne $action.Request ""}}
{{if ne $action.Doc ""}}// Submit: {{$action.Doc}}{{end}}
func (r *{{$action.Request}}) Submit(conn *signald.Signald) ({{if ne $action.Response ""}}response {{$action.Response}}, {{end}}err error) {
r.Version = "{{$.Version}}"
{{else}}
{{if ne $action.Doc ""}}// {{$action.FnName}}: {{$action.Doc}}{{end}}
func {{$action.FnName}}(conn *signald.Signald) ({{if ne $action.Response ""}}response {{$action.Response}}, {{end}}err error) {
r := Request{Version: "{{.Version}}"}
{{end}} r.Type = "{{$type}}"
if(r.ID == "") {
r.ID = signald.GenerateID()
}
responseChannel := conn.GetResponseListener(r.ID)
defer conn.CloseResponseListener(r.ID)
err = conn.RawRequest(r)
if err != nil {
log.Println("signald-go: error submitting request to signald")
return
}
rawResponse := <- responseChannel
if rawResponse.Error != nil {
err = mkerr(rawResponse)
return
}
{{if ne $action.Response ""}}
err = json.Unmarshal(rawResponse.Data, &response)
if err != nil {
rawResponseJson, _ := rawResponse.Data.MarshalJSON()
log.Println("signald-go: error unmarshalling response from signald of type", rawResponse.Type, string(rawResponseJson))
return
}
return response, nil
{{else}}
return err
{{end}}
}
{{end}}