Skip to content

Allow for marshalling/unmarshalling of XSDDateTime attributes #269

@thomaspeugeot

Description

@thomaspeugeot

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
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions