|
9 | 9 | package mediatype |
10 | 10 |
|
11 | 11 | import ( |
| 12 | + "fmt" |
12 | 13 | "mime" |
13 | | - "strings" |
14 | 14 | ) |
15 | 15 |
|
16 | 16 | /** |
@@ -40,120 +40,28 @@ const ( |
40 | 40 | // print out the reconstructed parts of the MediaType model in a standards |
41 | 41 | // compliant manner, following the `mime.FormatMediaType()` output |
42 | 42 | type MediaType interface { |
43 | | - FullType() string |
44 | | - Parameters() map[string]string |
| 43 | + fmt.Stringer |
| 44 | + |
45 | 45 | MainType() string |
46 | 46 | SubType() string |
47 | 47 | Trees() []string |
48 | 48 | Prefix() string |
49 | 49 | Suffix() string |
| 50 | + Parameters() map[string]string |
50 | 51 |
|
51 | | - String() string |
52 | | -} |
53 | | - |
54 | | -type mediaType struct { |
55 | | - fullType string |
56 | | - params map[string]string |
57 | | - |
58 | | - mainType string |
59 | | - trees []string |
60 | | - subType string |
61 | | - suffix string |
| 52 | + FullType() string |
62 | 53 | } |
63 | 54 |
|
64 | | -/** |
65 | | - * Functions |
66 | | - */ |
67 | | - |
68 | 55 | // Parse a raw media type string into a MediaType interface compatible struct |
69 | 56 | func Parse(raw string) (MediaType, error) { |
70 | | - normalized, params, err := mime.ParseMediaType(raw) |
| 57 | + normalizedFullType, params, err := mime.ParseMediaType(raw) |
71 | 58 |
|
72 | 59 | if nil != err { |
73 | 60 | return nil, err |
74 | 61 | } |
75 | 62 |
|
76 | | - // Build from the raw |
77 | | - mediaType := &mediaType{ |
78 | | - fullType: normalized, |
79 | | - params: params, |
80 | | - } |
81 | | - |
82 | | - mediaType.splitTypes() |
| 63 | + mediaType := splitTypes(normalizedFullType) |
| 64 | + mediaType.Params = params |
83 | 65 |
|
84 | 66 | return mediaType, nil |
85 | 67 | } |
86 | | - |
87 | | -// Get the normalized type and sub-type as a string |
88 | | -func (m *mediaType) FullType() string { |
89 | | - return m.fullType |
90 | | -} |
91 | | - |
92 | | -// Get the defined parameters of the media type |
93 | | -func (m *mediaType) Parameters() map[string]string { |
94 | | - return m.params |
95 | | -} |
96 | | - |
97 | | -// Get the "main" (top-level) type as a string |
98 | | -func (m *mediaType) MainType() string { |
99 | | - return m.mainType |
100 | | -} |
101 | | - |
102 | | -// Get the "sub" type as a string |
103 | | -func (m *mediaType) SubType() string { |
104 | | - return m.subType |
105 | | -} |
106 | | - |
107 | | -// Get the split "sub" type as an array of strings split by the namespace separator |
108 | | -func (m *mediaType) Trees() []string { |
109 | | - return m.trees |
110 | | -} |
111 | | - |
112 | | -// Get the prefix of the type's trees |
113 | | -func (m *mediaType) Prefix() string { |
114 | | - if 0 < len(m.trees) { |
115 | | - return m.trees[0] |
116 | | - } |
117 | | - |
118 | | - return "" |
119 | | -} |
120 | | - |
121 | | -// Get the "suffix" of the type as a string |
122 | | -func (m *mediaType) Suffix() string { |
123 | | - return m.suffix |
124 | | -} |
125 | | - |
126 | | -// Get a string representation conforming to RFC 2045 and RFC 2616 |
127 | | -func (m *mediaType) String() string { |
128 | | - return mime.FormatMediaType(m.fullType, m.params) |
129 | | -} |
130 | | - |
131 | | -// Split the full type string into parts and assign those values to our struct |
132 | | -func (m *mediaType) splitTypes() { |
133 | | - // Split the main/sub types |
134 | | - mainSubSplit := strings.Split(m.fullType, MainSubSplitCharacter) |
135 | | - |
136 | | - m.mainType = mainSubSplit[0] |
137 | | - |
138 | | - // If we got more than one part, we must have a sub-type |
139 | | - if 1 < len(mainSubSplit) { |
140 | | - // Split the remaining main/sub split from a possible suffix |
141 | | - subSuffixSplit := strings.Split(mainSubSplit[1], SuffixCharacter) |
142 | | - |
143 | | - // If we got more than one part, we must have a suffix |
144 | | - if 1 < len(subSuffixSplit) { |
145 | | - m.suffix = subSuffixSplit[1] |
146 | | - } |
147 | | - |
148 | | - // Split the sub-type split into the possibly different trees |
149 | | - treeSubSplit := strings.Split(subSuffixSplit[0], TreeSeparatorCharacter) |
150 | | - treeSubSplitLength := len(treeSubSplit) |
151 | | - |
152 | | - m.subType = treeSubSplit[treeSubSplitLength-1] |
153 | | - |
154 | | - // If we got more than one part, we must have tree definitions |
155 | | - if 1 < treeSubSplitLength { |
156 | | - m.trees = treeSubSplit[0 : treeSubSplitLength-1] |
157 | | - } |
158 | | - } |
159 | | -} |
0 commit comments