Use nil interface{} instead of struct{} for empty requests/responses.

The XML encoder doesn't like receiving struct{}.
This commit is contained in:
John Beisley 2014-01-06 19:47:57 +00:00
parent d8237236d8
commit a8a8987980
3 changed files with 62 additions and 23 deletions

View File

@ -70,8 +70,7 @@ func (client *{{$srvIdent}}) {{.Name}}({{range $inargs}}{{/*
*/}}) ({{range $outargs}}{{/* */}}) ({{range $outargs}}{{/*
*/}}{{$argWrap := $srv.WrapArgument .}}{{$argWrap.AsParameter}}, {{end}} err error) { */}}{{$argWrap := $srv.WrapArgument .}}{{$argWrap.AsParameter}}, {{end}} err error) {
// Request structure. // Request structure.
var request struct {{"{"}}{{range .Arguments}}{{if .IsInput}}{{.Name}} string request := {{if $inargs}}&{{template "argstruct" $inargs}}{{"{}"}}{{else}}{{"interface{}(nil)"}}{{end}}
{{end}}{{end}}}
// BEGIN Marshal arguments into request. // BEGIN Marshal arguments into request.
{{range $inargs}}{{$argWrap := $srv.WrapArgument .}} {{range $inargs}}{{$argWrap := $srv.WrapArgument .}}
if request.{{.Name}}, err = {{$argWrap.Marshal}}; err != nil { if request.{{.Name}}, err = {{$argWrap.Marshal}}; err != nil {
@ -80,11 +79,10 @@ func (client *{{$srvIdent}}) {{.Name}}({{range $inargs}}{{/*
// END Marshal arguments into request. // END Marshal arguments into request.
// Response structure. // Response structure.
var response struct {{"{"}}{{range $outargs}}{{.Name}} string response := {{if $outargs}}&{{template "argstruct" $outargs}}{{"{}"}}{{else}}{{"interface{}(nil)"}}{{end}}
{{end}}}
// Perform the SOAP call. // Perform the SOAP call.
if err = client.SOAPClient.PerformAction({{$srv.URNParts.Const}}, "{{.Name}}", &request, &response); err != nil { if err = client.SOAPClient.PerformAction({{$srv.URNParts.Const}}, "{{.Name}}", request, response); err != nil {
return return
} }
@ -98,4 +96,8 @@ func (client *{{$srvIdent}}) {{.Name}}({{range $inargs}}{{/*
} }
{{end}}{{/* range .SCPD.Actions */}} {{end}}{{/* range .SCPD.Actions */}}
{{end}}{{/* range .Services */}} {{end}}{{/* range .Services */}}
{{define "argstruct"}}struct {{"{"}}{{range .}}
{{.Name}} string
{{end}}{{"}"}}{{end}}
`)) `))

View File

@ -28,6 +28,8 @@ import (
var ( var (
specFilename = flag.String("spec", "", "Path to the specification file.") specFilename = flag.String("spec", "", "Path to the specification file.")
outDir = flag.String("out-dir", "", "Path to the output directory.") outDir = flag.String("out-dir", "", "Path to the output directory.")
enableGofmt = flag.Bool("gofmt", true, "Pass the output through gofmt. "+
"Disable if debugging code output problems.")
) )
var ( var (
@ -173,24 +175,55 @@ func (dcp *DCP) writePackage(outDir string) error {
if err != nil { if err != nil {
return err return err
} }
defer packageFile.Close() var output io.WriteCloser = packageFile
gofmt := exec.Command("gofmt") if *enableGofmt {
gofmt.Stdout = packageFile if output, err = NewGofmtWriteCloser(output); err != nil {
gofmt.Stderr = os.Stderr packageFile.Close()
gofmtWriter, err := gofmt.StdinPipe()
if err != nil {
return err return err
} }
}
if err = packageTmpl.Execute(output, dcp); err != nil {
output.Close()
return err
}
return output.Close()
}
type GofmtWriteCloser struct {
output io.WriteCloser
stdin io.WriteCloser
gofmt *exec.Cmd
}
func NewGofmtWriteCloser(output io.WriteCloser) (*GofmtWriteCloser, error) {
gofmt := exec.Command("gofmt")
gofmt.Stdout = output
gofmt.Stderr = os.Stderr
stdin, err := gofmt.StdinPipe()
if err != nil {
return nil, err
}
if err = gofmt.Start(); err != nil { if err = gofmt.Start(); err != nil {
return nil, err
}
return &GofmtWriteCloser{
output: output,
stdin: stdin,
gofmt: gofmt,
}, nil
}
func (gwc *GofmtWriteCloser) Write(p []byte) (int, error) {
return gwc.stdin.Write(p)
}
func (gwc *GofmtWriteCloser) Close() error {
gwc.stdin.Close()
if err := gwc.output.Close(); err != nil {
gwc.gofmt.Wait()
return err return err
} }
if err = packageTmpl.Execute(gofmtWriter, dcp); err != nil { return gwc.gofmt.Wait()
gofmtWriter.Close()
gofmt.Wait()
return err
}
gofmtWriter.Close()
return gofmt.Wait()
} }
func (dcp *DCP) processSCPDFile(file *zip.File) { func (dcp *DCP) processSCPDFile(file *zip.File) {

View File

@ -64,9 +64,11 @@ func (client *SOAPClient) PerformAction(actionNamespace, actionName string, inAc
return responseEnv.Body.Fault return responseEnv.Body.Fault
} }
if outAction != nil {
if err := xml.Unmarshal(responseEnv.Body.RawAction, outAction); err != nil { if err := xml.Unmarshal(responseEnv.Body.RawAction, outAction); err != nil {
return err return err
} }
}
return nil return nil
} }
@ -86,10 +88,12 @@ func newSOAPEnvelope() *soapEnvelope {
func encodeRequestAction(inAction interface{}) ([]byte, error) { func encodeRequestAction(inAction interface{}) ([]byte, error) {
requestBuf := new(bytes.Buffer) requestBuf := new(bytes.Buffer)
requestBuf.WriteString(soapPrefix) requestBuf.WriteString(soapPrefix)
if inAction != nil {
requestEnc := xml.NewEncoder(requestBuf) requestEnc := xml.NewEncoder(requestBuf)
if err := requestEnc.Encode(inAction); err != nil { if err := requestEnc.Encode(inAction); err != nil {
return nil, err return nil, err
} }
}
requestBuf.WriteString(soapSuffix) requestBuf.WriteString(soapSuffix)
return requestBuf.Bytes(), nil return requestBuf.Bytes(), nil
} }