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

Add compare/equal method to media type #3

Closed
jmattheis opened this issue Nov 1, 2021 · 1 comment · Fixed by #5
Closed

Add compare/equal method to media type #3

jmattheis opened this issue Nov 1, 2021 · 1 comment · Fixed by #5

Comments

@jmattheis
Copy link

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):

var MediaTypeEventStream contenttype.MediaType = contenttype.NewMediaType("application/vnd.my-product.v1+stream")
var MediaTypeEventJSON contenttype.MediaType = contenttype.NewMediaType("application/vnd.my-product.v1+json")
var MediaTypeEventXML contenttype.MediaType = contenttype.NewMediaType("application/xml")

func HandleCall(rw http.ResponseWriter, r *http.Request) {
	mediaType, _, err := contenttype.GetAcceptableMediaType(ctx.Request(), []contenttype.MediaType{
		MediaTypeEventStream,
		MediaTypeEventJSON,
		MediaTypeEventXML,
	})
	// handle err

	if mediaType.Type == MediaTypeEventStream.Type && mediaType.Subtype == MediaTypeEventStream.Subtype {
		// write stream to rw
	} else if mediaType.Type == MediaTypeEventJSON.Type && mediaType.Subtype == MediaTypeEventJSON.Subtype {
		// write json to rw
	} else {
		// write xml to rw
	}
}

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:

var MediaTypeStream MyMediaType = MyMediaType{Type: "application", SubType: "application/vnd.my-product.v1+stream"}
var MediaTypeJson MyMediaType = MyMediaType{Type: "application", SubType: "application/vnd.my-product.v1+json"}
var MediaTypeXML MyMediaType = MyMediaType{Type: "application", SubType: "application/vnd.my-product.v1+xml"}

func something(r *http.Request()) {
	mt, parameters, err := parseMyMediaType(r, []MyMediaType{..})
	switch mt {
	case MediaTypeJson:
		// write as json to response writer
	case MediaTypeXML:
		// write as xml to response writer
	case MediaTypeStream:
		// write as stream to response writer
	}
}

Thanks for your package and have a great day!

@gwvandesteeg
Copy link

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).

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

Successfully merging a pull request may close this issue.

2 participants