gowsdl

repository·master·Indexed 22 days ago

https://github.com/hooklift/gowsdl

A tool that generates idiomatic Go code from WSDL files. It supports WS-I compliant Document/Literal wrapped services using WSDL 1.1, XML Schema 1.0, and SOAP 1.1. The tool can resolve external XML Schemas and handle both local and remote WSDL files via a CLI or a Go API.

Tokens
3.6K
Snippets
4
Records
27
Agent score
79%

What's inside gowsdl

  1. Install gowsdl

    master

    You can install gowsdl using several methods depending on your Go version or package manager:

    • Go 1.20+: Use go install to get the latest version.
    • Go 1.15: Use go get.
    • Homebrew: Use brew install on macOS.
    • Manual: Download a release from GitHub and build it locally.
  2. Use gowsdl to generate Go code from a WSDL

    master

    The gowsdl CLI tool generates idiomatic Go code from a WSDL file. It supports WSDL 1.1, XML Schema 1.0, and SOAP 1.1, specifically focusing on WS-I compliant Document/Literal wrapped services. It can resolve external XML Schemas and handle both external and local WSDL files.

    Note on generated code: The output is a direct reflection of the WSDL. If the WSDL contains duplicate type definitions, the generated Go code will also contain them, which may prevent compilation.

    gowsdl [options] myservice.wsdl
  3. How GoWSDL handles XSD imports and includes

    master

    When generating code, GoWSDL automatically attempts to resolve external XSD files referenced via imports or includes within the WSDL or other schemas.

    • It uses the schemaLocation attribute to locate these files.
    • It supports both local file paths and remote URLs.
    • It implements a recursion limit (maxRecursion = 20) to prevent infinite loops in circular schema references.
    • It maintains a cache of resolved external schemas to avoid redundant downloads/reads.
  4. Implement a SOAP server using gowsdl types

    master

    When using gowsdl to implement a SOAP server, you can use the generated SOAPEnvelopeRequest, SOAPBodyRequest, and SOAPEnvelopeResponse types to handle incoming XML requests and outgoing responses.

    To serve requests, you typically use the Endpoint function (or a similar handler pattern) which decodes the incoming SOAPEnvelopeRequest and uses reflection to route the request to the appropriate method on the SOAPBodyRequest struct.

    Key Types

    • SOAPEnvelopeRequest: The top-level container for an incoming SOAP request.
    • SOAPBodyRequest: Contains the specific operation request types. The generated code adds methods to this struct following the pattern [RequestType]Func(request *[RequestType]) (*[ResponseType], error).
    • SOAPEnvelopeResponse: The top-level container for the outgoing SOAP response. It includes necessary XML namespaces (PrefixSoap, PrefixXsi, PrefixXsd).
    • Fault: Used within SOAPBodyResponse to represent SOAP errors.
  5. Reference gowsdl CLI flags

    master

    The gowsdl command accepts the following options to control the generation process:

    Usage: gowsdl [options] myservice.wsdl
      -o string
            File where the generated code will be saved (default "myservice.go")
      -p string
            Package under which code will be generated (default "myservice")
      -i    Skips TLS Verification
      -v    Shows gowsdl version
  6. Unmarshal an XSD Schema using XSDSchema

    master
    The XSDSchema type is the root structure for representing an entire XML Schema (XSD). It implements the xml.Unmarshaler interface, allowing you to decode an XSD XML document directly into a Go struct. It automatically handles namespace mapping (Xmlns), target namespaces (Tns, TargetNamespace), and collects global definitions like Elements, Attributes, ComplexTypes, and SimpleTypes.
  7. Resolve relative paths using Location.Parse

    master

    The Parse method on a Location receiver allows you to resolve a reference (a relative or absolute path/URL) against the existing Location.

    • If the receiver is a URL, Parse treats the reference as a URL path to be parsed against the base URL.
    • If the receiver is a file path, Parse treats the reference as a file path. If the reference is absolute, it returns that path; if it is relative, it joins it with the directory of the receiver's path.
    • If the reference is a URL with a scheme, it returns a new Location based on that URL.
  8. Initialize a WSDL generator with NewGoWSDL

    master

    Use NewGoWSDL to create a new GoWSDL instance for generating Go code from a WSDL file.

    Parameters:

    • file: The path to the WSDL file or a URL.
    • pkg: The name of the Go package to be generated (defaults to myservice if empty).
    • ignoreTLS: A boolean indicating whether to skip TLS certificate verification (useful for testing with self-signed certificates).
    • exportAllTypes: A boolean that, when true, makes all generated types public. If false, types are kept private by default.
  9. WSDLInput and WSDLOutput structures

    master

    These types define the data flow for a WSDLOperation:

    • WSDLInput: Contains the Name, the Message used, Doc documentation, a SOAPBody (WSDLSOAPBody), and optional SOAPHeaders ([]*WSDLSOAPHeader).
    • WSDLOutput: Contains the Name, the Message used, Doc documentation, a SOAPBody (WSDLSOAPBody), and optional SOAPHeaders ([]*WSDLSOAPHeader).
  10. Generate Go code using Start()

    master

    The Start() method initiates the code generation process. It performs unmarshalling, resolves external XSD schemas, and concurrently generates types, operations, and server components.

    Returns:

    • map[string][]byte: A map where keys represent the different parts of the generated code and values are the raw byte slices of the code content. The keys are:
      • "types": Generated data structures.
      • "operations": Generated service operations.
      • "server": Generated server implementation.
      • "header": Package header.
      • "server_header": Server package header.
      • "server_wsdl": The raw WSDL content assigned to a variable.
    • error: Any error encountered during the generation process.
  11. Unmarshal a WSDL file into the WSDL struct

    master
    The WSDL struct is the primary entry point for representing the global structure of a WSDL file. It implements the xml.Unmarshaler interface, allowing you to use standard Go encoding/xml functions to parse WSDL content. During unmarshaling, it automatically captures XML namespaces (Xmlns) and maps them to the internal XSDSchema structures.
  12. Parse a WSDL/XSD location with ParseLocation

    master
    Use ParseLocation to convert a raw string into a Location object. The function automatically detects if the input is an absolute URL or a file path. If a relative file path is provided, it is converted into an absolute path.