FormatType deduces an overall FormatType for the given format.
()
| 153 | |
| 154 | // FormatType deduces an overall FormatType for the given format. |
| 155 | func (f Format) FormatType() FormatType { |
| 156 | toks := strings.Split(string(f), ";") |
| 157 | params := make(map[string]string) |
| 158 | for i, t := range toks { |
| 159 | if i == 0 { |
| 160 | continue |
| 161 | } |
| 162 | args := strings.Split(t, "=") |
| 163 | if len(args) != 2 { |
| 164 | continue |
| 165 | } |
| 166 | params[strings.TrimSpace(args[0])] = strings.TrimSpace(args[1]) |
| 167 | } |
| 168 | |
| 169 | switch strings.TrimSpace(toks[0]) { |
| 170 | case ProtoType: |
| 171 | if params["proto"] != ProtoProtocol { |
| 172 | return TypeUnknown |
| 173 | } |
| 174 | switch params["encoding"] { |
| 175 | case "delimited": |
| 176 | return TypeProtoDelim |
| 177 | case "text": |
| 178 | return TypeProtoText |
| 179 | case "compact-text": |
| 180 | return TypeProtoCompact |
| 181 | default: |
| 182 | return TypeUnknown |
| 183 | } |
| 184 | case OpenMetricsType: |
| 185 | if params["charset"] != "utf-8" { |
| 186 | return TypeUnknown |
| 187 | } |
| 188 | return TypeOpenMetrics |
| 189 | case "text/plain": |
| 190 | v, ok := params["version"] |
| 191 | if !ok { |
| 192 | return TypeTextPlain |
| 193 | } |
| 194 | if v == TextVersion { |
| 195 | return TypeTextPlain |
| 196 | } |
| 197 | return TypeUnknown |
| 198 | default: |
| 199 | return TypeUnknown |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | // ToEscapingScheme returns an EscapingScheme depending on the Format. Iff the |
| 204 | // Format contains a escaping=allow-utf-8 term, it will select NoEscaping. If a valid |
no outgoing calls