matchFormat checks if a parsed accept clause matches a given Format.
(ac goautoneg.Accept, f Format)
| 129 | |
| 130 | // matchFormat checks if a parsed accept clause matches a given Format. |
| 131 | func matchFormat(ac goautoneg.Accept, f Format) bool { |
| 132 | target, ok := formatToAccept[f] |
| 133 | if !ok { |
| 134 | parsed := goautoneg.ParseAccept(string(f)) |
| 135 | if len(parsed) == 0 { |
| 136 | return false |
| 137 | } |
| 138 | target = parsed[0] |
| 139 | } |
| 140 | |
| 141 | if ac.Type != "*" && ac.Type != target.Type { |
| 142 | return false |
| 143 | } |
| 144 | if ac.SubType != "*" && ac.SubType != target.SubType { |
| 145 | return false |
| 146 | } |
| 147 | |
| 148 | // Default OpenMetrics version to OpenMetricsVersion_0_0_1. |
| 149 | acVersion := ac.Params["version"] |
| 150 | if acVersion == "" && ac.Type+"/"+ac.SubType == OpenMetricsType { |
| 151 | acVersion = OpenMetricsVersion_0_0_1 |
| 152 | } |
| 153 | if acVersion == "" && ac.Type == "text" && ac.SubType == "plain" { |
| 154 | acVersion = TextVersion |
| 155 | } |
| 156 | |
| 157 | // General param matching. |
| 158 | for k, v := range target.Params { |
| 159 | if k == "charset" { |
| 160 | continue |
| 161 | } |
| 162 | acVal := ac.Params[k] |
| 163 | if k == "version" { |
| 164 | acVal = acVersion |
| 165 | } |
| 166 | if acVal != v { |
| 167 | return false |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | return true |
| 172 | } |
| 173 | |
| 174 | // NewEncoder returns a new encoder based on content type negotiation. All |
| 175 | // Encoder implementations returned by NewEncoder also implement Closer, and |