droyo / go-xml

utility and code-generation libraries for XML
MIT License
304 stars 115 forks source link

Schema with restriction and base="xsd:dateTime" #114

Closed oskar-r closed 4 years ago

oskar-r commented 4 years ago

Came across the following when trying to use xsdgen with a xsd-schema containing

  <xsd:simpleType name="nonEmptyDate"> 
        <xsd:restriction base="xsd:dateTime"> 
            <xsd:minExclusive value="2000-01-01T00:00:00Z"/>
            <xsd:maxExclusive value="2100-01-01T00:00:00Z"/>
        </xsd:restriction> 
    </xsd:simpleType>  

Resulted in following error.

Error at simpleType(nonEmptyDate)>restriction: strconv.ParseFloat: parsing "2000-01-01T00:00:00Z": invalid syntax

I managed to get it to parse by adding and additional else if clause which checks for DateTime in

parseSimpleRestriction() 
...
else if v, ok := base.(Builtin); ok && v == DateTime {
    d, err := time.Parse(time.RFC3339, el.Attr("", "value"))
    if err != nil {
        stop(err.Error())
    }
    r.MinDate = d
}

This seems more than a walk around than a patch though.

droyo commented 4 years ago

This seems like a perfectly valid fix; we're already special-casing Date builtins in a similar way. It's a little unfortunate that it needs to be copied twice to check for min and max, but it's not the end of the world.

There are probably a few other base types where min/max are still valid restrictions but which don't make sense as floating-point numbers. But I'm happy to accept a PR just handling DateTime in the way you've done.

Thanks!