hooklift / gowsdl

WSDL2Go code generation as well as its SOAP proxy
Mozilla Public License 2.0
1.14k stars 390 forks source link

Allow for marshalling/unmarshalling of XSDDateTime attributes #269

Open thomaspeugeot opened 1 month ago

thomaspeugeot commented 1 month ago

Some XSD have attributes with xsd:dateTime. For instance, http://www.omg.org/spec/ReqIF/20110401/reqif.xsd, used for exchanging requirements between authoring tools, have 24 of them.

    <xsd:attribute name="LAST-CHANGE" type="xsd:dateTime" use="required" />

At runtime, the marshalling/unmarshalling of an xml will fail

Error parsing XML: cannot unmarshal into soap.XSDDateTime

or

Error parsing XML: cannot marshal into soap.XSDDateTime

To allow for marshalling/unmarshalling, one have to patch the soap package with 2 additional methods.

Below is a non idomatic implementation that works so far

func (xdt XSDDateTime) MarshalXMLAttr(name xml.Name) (xml.Attr, error) {
    const myFormat = "2006-01-02T15:04:05.000-07:00"
    return xml.Attr{
        Name:  name,
        Value: xdt.ToGoTime().Format(myFormat),
    }, nil
}

func (xdt *XSDDateTime) UnmarshalXMLAttr(attr xml.Attr) error {
    var err error
    const layout = "2006-01-02T15:04:05.000-07:00"
    xdt.innerTime, err = time.Parse(layout, attr.Value)
    if err != nil {
        return fmt.Errorf("could not parse time: %v", err)
    }

    return nil
}