You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, there is no neat way to compare MediaTypes. E.g. when you need to handle multiple different media types, you have to add this ugly if else chain, with checks for type & subtype, because MediaType itself isn't comparable (because of the usage of map[..].. for the parameters):
It would be cool, if a EqualIgnoreParameters method could be added to the media type. Another even better solution would be to remove the parameter map from the MediaType, then it would be possible to use the media type in a switch case like this:
varMediaTypeStreamMyMediaType=MyMediaType{Type: "application", SubType: "application/vnd.my-product.v1+stream"}
varMediaTypeJsonMyMediaType=MyMediaType{Type: "application", SubType: "application/vnd.my-product.v1+json"}
varMediaTypeXMLMyMediaType=MyMediaType{Type: "application", SubType: "application/vnd.my-product.v1+xml"}
funcsomething(r*http.Request()) {
mt, parameters, err:=parseMyMediaType(r, []MyMediaType{..})
switchmt {
caseMediaTypeJson:
// write as json to response writercaseMediaTypeXML:
// write as xml to response writercaseMediaTypeStream:
// write as stream to response writer
}
}
Thanks for your package and have a great day!
The text was updated successfully, but these errors were encountered:
Added simple PR to achieve the first part of the comparison method reuest.
Additionally there's options for something like these
.Matches(t MediaType) bool // to compare exactly including the parameters, using reflect.DeepEqual could do this
.MatchesWildcard(t MediaType) bool // to compare whether they match when one is a wildcard, ie.. text/* == text/csv
You could either return a bool or an error, the latter would allow for dealing with the case where one of them isn't a valid mime type (such as the zero value of the struct).
Currently, there is no neat way to compare MediaTypes. E.g. when you need to handle multiple different media types, you have to add this ugly if else chain, with checks for type & subtype, because MediaType itself isn't comparable (because of the usage of map[..].. for the parameters):
It would be cool, if a
EqualIgnoreParameters
method could be added to the media type. Another even better solution would be to remove the parameter map from the MediaType, then it would be possible to use the media type in a switch case like this:Thanks for your package and have a great day!
The text was updated successfully, but these errors were encountered: