- 
                Notifications
    
You must be signed in to change notification settings  - Fork 179
 
Exported mime types + support for explicit mime params #645
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
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Megakuul thanks for the PR, looks good and solves the problem but there is one thing:
mime.Type() and mime.String() will return the same string value most of the times (only a few MIMEs actually have optional parameters.) Users might get confused and I don't think many will spend the time to read and understand the difference.
Also, mime.Type() encourages them to use == operator on string values of MIME types (the problem they we're discussing here: golang/go#31572 (comment))
if mime.Type() == types.Text { // this works correctly, but encourages the use of ==
  println("this is text")
}
if mime.String() == types.Text { // this is broken and some users might write it :(
  println("this is text")
}I'd like to direct users more towards using safer functions: mime.Is() and EqualsAny().
So, the way I'm thinking to solve the problem in this PR:
- add constant string values for each MIME (already done)
 - inside 
tree.gouse the above constants (already done) - add a function to return all supported MIME string constants (not done)
Not sure what to call this function. Maybefunc All() []string? 
        
          
                .gitignore
              
                Outdated
          
        
      | @@ -0,0 +1,4 @@ | |||
| # ignore emacs files | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove this gitignore. I already made a decision to not add editor specific gitignore entries some time ago and it's only fair to the other people who tried to add this type of gitignore.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not really understand why this decision was made, but I see your point and removed the .gitignore.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason was to avoid user environment specific entries, i.e. entries that are not related to the project itself.
When I made that decision, I used the golang/go as inspiration. golang/go@d647b61
| @@ -0,0 +1,179 @@ | |||
| package types | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know some people have a types package and this package would conflict with it. Please move these constants in the root mimetype package.
Previously I suggested a new package, but I don't really know a good name for it. Seems mimetype is best location IMO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gabriel-vasile as you already pointed out in #127 I would rather not pollute the library scope with 150+ constants. Therefore I suggest keeping them in a separate package. The fact that types might collide is not a problem IMO, the package name should be clear and not unique as pointed out in effective go.
I used the name types because it's often used for similar packages (e.g. in aws-sdk-go-v2), but I am open to suggestions for other names, I just wouldn't put it in the root library scope...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
aws-sdk is generated code. aws has an SDK specification and then they generate code for different programming languages. As it happens their generator cannot follow good-practices of all languages so they lean more towards java.
I'm still leaning towards using the root package for these reasons:
typesis a popular name and I don't want to steal it from the usermimetypeseems good package to contain all mime types according to https://go.dev/blog/package-names
Edit: I checked how the docs would look when using the root package and seems ok to me...
The only downside I see is users will receive 100+ autocomplete suggestions when typing mimetype..
| 
           @gabriel-vasile regarding the function that returns all constants: I've already added a function called SupportedMIMEs() that should return the flattened tree as proposed in #452. I see your point that the user may get confused about  switch mime.Type() {
  case types.PLAIN:
    // handle text/plain
  case types.HTML:
    // handle text/html
  default:
    // handle default
}This requires direct access to the type. To avoid this confusion, I wrapped the types into a  IMO, this change eliminates ambiguity for the user, as it requires an explicit conversion to a string (  | 
    
          
 
 But this introduces a new type and type conversions... Edit: m := mimetype.Detect(file)
switch {
    case m.Is(mimetype.SVG), m.Is(mimetype.PNG):
        // do things
    default:
        //
} | 
    
| 
           I don't really see the problem with a new type or a type conversion... I mean,  I understand the go authors perspective, but they have a different context. They are building a standard library, and their main argument is not to overload it with specifics. This library is more specific, and that is perfectly fine IMO. The switch argument is not just about the switch-statement. I may want to use the type as a map key or hash it (e.g. to use it as part of a cache key). The argument is more about having a processable value per mime and not just a pointer to a struct.  | 
    
This is a initial proposal for #127