Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for marshalling/unmarshalling of XSDDateTime attributes #269

Open
thomaspeugeot opened this issue Jul 18, 2024 · 0 comments
Open

Allow for marshalling/unmarshalling of XSDDateTime attributes #269

thomaspeugeot opened this issue Jul 18, 2024 · 0 comments

Comments

@thomaspeugeot
Copy link

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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant