-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
71 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package msg | ||
|
||
import ( | ||
"fmt" | ||
"mime" | ||
"strings" | ||
) | ||
|
||
var ( | ||
ErrInvalidContentType = fmt.Errorf("invalid Message ContentType") | ||
ErrMissingContentType = fmt.Errorf("empty ContentType") | ||
ErrMissingSerialiser = fmt.Errorf("missing serialiser") | ||
) | ||
|
||
// Returns the message type as defined in the contenttype, not the message as such | ||
func (m *Message) MessageType() (string, error) { | ||
|
||
_, params, err := m.ParseContentType() | ||
if err != nil { | ||
return "", ErrInvalidContentType | ||
} | ||
|
||
for k, v := range params { | ||
if k == "type" { | ||
return v, nil | ||
} | ||
} | ||
|
||
return "", ErrInvalidContentType | ||
} | ||
|
||
// Returns the message type as defined in the contenttype, not the message as such | ||
func (m *Message) MessageSerialiser() (string, error) { | ||
|
||
contentType, _, err := m.ParseContentType() | ||
if err != nil { | ||
return "", ErrInvalidContentType | ||
} | ||
|
||
elements := strings.Split(contentType, "+") | ||
if len(elements) == 1 { | ||
return contentType, fmt.Errorf("%s: %w", contentType, ErrMissingSerialiser) | ||
} | ||
|
||
return elements[1], nil | ||
} | ||
|
||
// A parser which add a little specific functionality to the standard mime.ParseMediaType | ||
// Use this instead. | ||
func (m *Message) ParseContentType() (string, map[string]string, error) { | ||
|
||
contentType, params, err := mime.ParseMediaType(m.ContentType) | ||
if err != nil { | ||
return contentType, params, fmt.Errorf("msg_parse_content_type: %w", err) | ||
} | ||
|
||
if contentType == "" { | ||
return contentType, params, ErrMissingContentType | ||
} | ||
|
||
if contentType != CONTENT_TYPE { | ||
return contentType, params, fmt.Errorf("%s: %w", contentType, ErrInvalidContentType) | ||
} | ||
|
||
return contentType, params, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters