Skip to content

Commit

Permalink
Cleanup, print interfaces first, for SOAP
Browse files Browse the repository at this point in the history
For non-soap we print: functions, types

For SOAP we print: interface, constructor, types, implementation
  • Loading branch information
fiorix committed Mar 24, 2016
1 parent 4564f8f commit d137beb
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 192 deletions.
183 changes: 93 additions & 90 deletions wsdlgo/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,21 +131,26 @@ func (ge *goEncoder) encode(w io.Writer, d *wsdl.Definitions) error {
pkg = "internal"
}
var b bytes.Buffer
var ff []func(io.Writer, *wsdl.Definitions) error
if len(ge.soapOps) > 0 {
// TODO: rpc? meh.
ge.writePortType(&b, d)
}
err = ge.writeGoFuncs(&b, d) // functions first, for clarity
if err != nil {
return err
}
err = ge.writeInterfaceFuncs(&b, d)
if err != nil {
return err
}
err = ge.writeGoTypes(&b, d)
if err != nil {
return err
ff = append(ff,
ge.writeInterfaceFuncs,
ge.writeGoTypes,
ge.writePortType,
ge.writeGoFuncs,
)
} else {
// this is rpc; meh
ff = append(ff,
ge.writeGoFuncs,
ge.writeGoTypes,
)
}
for _, f := range ff {
err := f(&b, d)
if err != nil {
return err
}
}
fmt.Fprintf(w, "package %s\n\nimport (\n", pkg)
for pkg := range ge.needsStdPkg {
Expand Down Expand Up @@ -248,23 +253,89 @@ func (ge *goEncoder) cacheSOAPOperations(d *wsdl.Definitions) {
}
}

var interfaceTypeT = template.Must(template.New("interfaceType").Parse(`
// {{.Name}}Interface was auto-generated from WSDL
// and defines interface for the remote service. Useful for testing.
type {{.Name}} interface {
{{- range .Funcs }}
{{.Doc}}{{.Name}}({{.Input}}) ({{.Output}})
{{ end }}
}
// New{{.Name}} creates an initializes a {{.Name}}.
func New{{.Name}}(cli *soap.Client) {{.Name}} {
return &{{.Impl}}{cli}
}
`))

type interfaceTypeFunc struct{ Doc, Name, Input, Output string }

// writeInterfaceFuncs writes Go interface definitions from WSDL types to w.
// Functions are written in the same order of the WSDL document.
func (ge *goEncoder) writeInterfaceFuncs(w io.Writer, d *wsdl.Definitions) error {
funcs := make([]*interfaceTypeFunc, len(d.PortType.Operations))
// Looping over the operations to determine what are the interface
// functions.
for i, op := range d.PortType.Operations {
if _, exists := ge.soapOps[op.Name]; !exists {
// TODO: rpc?
continue
}
in, err := ge.inputParams(op)
if err != nil {
return err
}
out, err := ge.outputParams(op)
if err != nil {
return err
}
if len(in) != 1 && len(out) != 2 {
continue
}
name := strings.Title(op.Name)
in[0] = renameParam(in[0], "α")
out[0] = renameParam(out[0], "β")
var doc bytes.Buffer
ge.writeComments(&doc, name, op.Doc)
funcs[i] = &interfaceTypeFunc{
Doc: doc.String(),
Name: name,
Input: strings.Join(in, ","),
Output: strings.Join(out, ","),
}
}
n := d.PortType.Name
return interfaceTypeT.Execute(w, &struct {
Name string
Impl string // private type that implements the interface
Funcs []*interfaceTypeFunc
}{
strings.Title(n),
strings.ToLower(n)[:1] + n[1:],
funcs,
})
}

var portTypeT = template.Must(template.New("portType").Parse(`
// {{.}} was auto-generated from WSDL.
type {{.}} struct {
// {{.Name}} implements the {{.Interface}} interface.
type {{.Name}} struct {
cli *soap.Client
}
// New{{.}} creates an initializes a {{.}}.
func New{{.}}(cli *soap.Client) *{{.}} {
return &{{.}}{cli}
}
`))

func (ge *goEncoder) writePortType(w io.Writer, d *wsdl.Definitions) error {
if d.PortType.Operations == nil || len(d.PortType.Operations) == 0 {
return nil
}
return portTypeT.Execute(w, strings.Title(d.PortType.Name))
n := d.PortType.Name
return portTypeT.Execute(w, &struct {
Name string
Interface string
}{
strings.ToLower(n)[:1] + n[1:],
strings.Title(n),
})
}

// writeGoFuncs writes Go function definitions from WSDL types to w.
Expand Down Expand Up @@ -354,7 +425,7 @@ func (ge *goEncoder) writeSOAPFunc(w io.Writer, d *wsdl.Definitions, op *wsdl.Op
RetPtr bool
RetDef string
}{
strings.Title(d.PortType.Name),
strings.ToLower(d.PortType.Name[:1]) + d.PortType.Name[1:],
strings.Title(op.Name),
strings.Join(in, ","),
strings.Join(out, ","),
Expand All @@ -365,74 +436,6 @@ func (ge *goEncoder) writeSOAPFunc(w io.Writer, d *wsdl.Definitions, op *wsdl.Op
return true
}

var interfaceTypeT = template.Must(template.New("interfaceType").Parse(`
// {{.}}Interface was auto-generated from WSDL
// and defines interface for the data type.
type {{.}}Interface interface {
`))

// writeInterfaceFuncs writes Go interface definitions from WSDL types to w.
// Functions are written in the same order of the WSDL document.
func (ge *goEncoder) writeInterfaceFuncs(w io.Writer, d *wsdl.Definitions) error {
err := interfaceTypeT.Execute(w, strings.Title(d.PortType.Name))
if err != nil {
return err
}

// Looping over the operations to determine what are the interface
// functions
for _, op := range d.PortType.Operations {
in, err := ge.inputParams(op)
if err != nil {
return err
}
out, err := ge.outputParams(op)
if err != nil {
return err
}
ret := make([]string, len(out))
for i, p := range out {
parts := strings.SplitN(p, " ", 2)
if len(parts) == 2 {
ret[i] = ge.wsdl2goDefault(parts[1])
}
}
ge.writeInterfaceFunc(w, d, op, in, out, ret)

}

fmt.Fprintf(w, "}\n")
return nil

}

var interfaceFuncT = template.Must(template.New("interfaceFunc").Parse(
` {{.Name}}({{.Input}}) ({{.Output}})
`))

func (ge *goEncoder) writeInterfaceFunc(w io.Writer, d *wsdl.Definitions, op *wsdl.Operation, in, out, ret []string) {
if _, exists := ge.soapOps[op.Name]; !exists {
return
}
if len(in) != 1 && len(out) != 2 {
return
}
in[0] = renameParam(in[0], "α")
out[0] = renameParam(out[0], "β")
if strings.HasPrefix(ret[0], "&") {
ret[0] = "nil"
}
interfaceFuncT.Execute(w, &struct {
Name string
Input string
Output string
}{
strings.Title(op.Name),
strings.Join(in, ","),
strings.Join(out, ","),
})
}

func renameParam(p, name string) string {
v := strings.SplitN(p, " ", 2)
if len(v) != 2 {
Expand Down
93 changes: 49 additions & 44 deletions wsdlgo/testdata/memcache.golden
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,59 @@ import (
// Namespace was auto-generated from WSDL.
var Namespace = "http://localhost:8080/MemoryService.wsdl"

// MemoryServicePortType was auto-generated from WSDL.
type MemoryServicePortType struct {
cli *soap.Client
// MemoryServicePortTypeInterface was auto-generated from WSDL
// and defines interface for the remote service. Useful for testing.
type MemoryServicePortType interface {
// Get was auto-generated from WSDL.
Get(α string) (β *GetResponse, err error)

// Set was auto-generated from WSDL.
Set(α *SetRequest) (β bool, err error)

// GetMulti was auto-generated from WSDL.
GetMulti(α *GetMultiRequest) (β *GetMultiResponse, err error)
}

// NewMemoryServicePortType creates an initializes a MemoryServicePortType.
func NewMemoryServicePortType(cli *soap.Client) *MemoryServicePortType {
return &MemoryServicePortType{cli}
func NewMemoryServicePortType(cli *soap.Client) MemoryServicePortType {
return &memoryServicePortType{cli}
}

// Duration in WSDL format.
type Duration string

// GetMultiRequest was auto-generated from WSDL.
type GetMultiRequest struct {
XMLName xml.Name `xml:"http://localhost:8080/MemoryService.wsdl GetMultiRequest" json:"-" yaml:"-"`
Keys []string `xml:"Keys" json:"Keys" yaml:"Keys"`
}

// GetMultiResponse was auto-generated from WSDL.
type GetMultiResponse struct {
Values []*GetResponse `xml:"Values,omitempty" json:"Values,omitempty" yaml:"Values,omitempty"`
}

// GetResponse carries value and TTL.
type GetResponse struct {
Value string `xml:"Value,omitempty" json:"Value,omitempty" yaml:"Value,omitempty"`
TTL Duration `xml:"TTL,omitempty" json:"TTL,omitempty" yaml:"TTL,omitempty"`
}

// SetRequest carries a key-value pair.
type SetRequest struct {
XMLName xml.Name `xml:"http://localhost:8080/MemoryService.wsdl SetRequest" json:"-" yaml:"-"`
Key string `xml:"Key" json:"Key" yaml:"Key"`
Value string `xml:"Value" json:"Value" yaml:"Value"`
Expiration Duration `xml:"Expiration,omitempty" json:"Expiration,omitempty" yaml:"Expiration,omitempty"`
}

// memoryServicePortType implements the MemoryServicePortType interface.
type memoryServicePortType struct {
cli *soap.Client
}

// Get was auto-generated from WSDL.
func (p *MemoryServicePortType) Get(α string) (β *GetResponse, err error) {
func (p *memoryServicePortType) Get(α string) (β *GetResponse, err error) {
γ := struct {
XMLName xml.Name `xml:"Envelope"`
Body struct {
Expand All @@ -34,7 +75,7 @@ func (p *MemoryServicePortType) Get(α string) (β *GetResponse, err error) {
}

// Set was auto-generated from WSDL.
func (p *MemoryServicePortType) Set(α *SetRequest) (β bool, err error) {
func (p *memoryServicePortType) Set(α *SetRequest) (β bool, err error) {
γ := struct {
XMLName xml.Name `xml:"Envelope"`
Body struct {
Expand All @@ -48,7 +89,7 @@ func (p *MemoryServicePortType) Set(α *SetRequest) (β bool, err error) {
}

// GetMulti was auto-generated from WSDL.
func (p *MemoryServicePortType) GetMulti(α *GetMultiRequest) (β *GetMultiResponse, err error) {
func (p *memoryServicePortType) GetMulti(α *GetMultiRequest) (β *GetMultiResponse, err error) {
γ := struct {
XMLName xml.Name `xml:"Envelope"`
Body struct {
Expand All @@ -60,39 +101,3 @@ func (p *MemoryServicePortType) GetMulti(α *GetMultiRequest) (β *GetMultiRespo
}
return &γ.Body.M, nil
}

// MemoryServicePortTypeInterface was auto-generated from WSDL
// and defines interface for the data type.
type MemoryServicePortTypeInterface interface {
Get(α string) (β *GetResponse, err error)
Set(α *SetRequest) (β bool, err error)
GetMulti(α *GetMultiRequest) (β *GetMultiResponse, err error)
}

// Duration in WSDL format.
type Duration string

// GetMultiRequest was auto-generated from WSDL.
type GetMultiRequest struct {
XMLName xml.Name `xml:"http://localhost:8080/MemoryService.wsdl GetMultiRequest" json:"-" yaml:"-"`
Keys []string `xml:"Keys" json:"Keys" yaml:"Keys"`
}

// GetMultiResponse was auto-generated from WSDL.
type GetMultiResponse struct {
Values []*GetResponse `xml:"Values,omitempty" json:"Values,omitempty" yaml:"Values,omitempty"`
}

// GetResponse carries value and TTL.
type GetResponse struct {
Value string `xml:"Value,omitempty" json:"Value,omitempty" yaml:"Value,omitempty"`
TTL Duration `xml:"TTL,omitempty" json:"TTL,omitempty" yaml:"TTL,omitempty"`
}

// SetRequest carries a key-value pair.
type SetRequest struct {
XMLName xml.Name `xml:"http://localhost:8080/MemoryService.wsdl SetRequest" json:"-" yaml:"-"`
Key string `xml:"Key" json:"Key" yaml:"Key"`
Value string `xml:"Value" json:"Value" yaml:"Value"`
Expiration Duration `xml:"Expiration,omitempty" json:"Expiration,omitempty" yaml:"Expiration,omitempty"`
}
5 changes: 0 additions & 5 deletions wsdlgo/testdata/w3cexample1.golden
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,3 @@ import (
func GetTerm(ctx context.Context, term string) (value string, err error) {
return "", errors.New("not implemented")
}

// GlossaryTermsInterface was auto-generated from WSDL
// and defines interface for the data type.
type GlossaryTermsInterface interface {
}
5 changes: 0 additions & 5 deletions wsdlgo/testdata/w3cexample2.golden
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,3 @@ import (
func SetTerm(ctx context.Context, term string, value string) (err error) {
return errors.New("not implemented")
}

// GlossaryTermsInterface was auto-generated from WSDL
// and defines interface for the data type.
type GlossaryTermsInterface interface {
}
Loading

0 comments on commit d137beb

Please sign in to comment.