Skip to content

Commit bd41eb6

Browse files
authored
Merge pull request #576 from ywwg/owilliams/unexport
Unexport Format strings
2 parents 773d566 + 05d7387 commit bd41eb6

File tree

5 files changed

+65
-44
lines changed

5 files changed

+65
-44
lines changed

expfmt/decode.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -45,29 +45,29 @@ func ResponseFormat(h http.Header) Format {
4545

4646
mediatype, params, err := mime.ParseMediaType(ct)
4747
if err != nil {
48-
return FmtUnknown
48+
return fmtUnknown
4949
}
5050

5151
const textType = "text/plain"
5252

5353
switch mediatype {
5454
case ProtoType:
5555
if p, ok := params["proto"]; ok && p != ProtoProtocol {
56-
return FmtUnknown
56+
return fmtUnknown
5757
}
5858
if e, ok := params["encoding"]; ok && e != "delimited" {
59-
return FmtUnknown
59+
return fmtUnknown
6060
}
61-
return FmtProtoDelim
61+
return fmtProtoDelim
6262

6363
case textType:
6464
if v, ok := params["version"]; ok && v != TextVersion {
65-
return FmtUnknown
65+
return fmtUnknown
6666
}
67-
return FmtText
67+
return fmtText
6868
}
6969

70-
return FmtUnknown
70+
return fmtUnknown
7171
}
7272

7373
// NewDecoder returns a new decoder based on the given input format.

expfmt/decode_test.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -421,27 +421,27 @@ func testDiscriminatorHTTPHeader(t testing.TB) {
421421
}{
422422
{
423423
input: map[string]string{"Content-Type": `application/vnd.google.protobuf; proto="io.prometheus.client.MetricFamily"; encoding="delimited"`},
424-
output: FmtProtoDelim,
424+
output: fmtProtoDelim,
425425
},
426426
{
427427
input: map[string]string{"Content-Type": `application/vnd.google.protobuf; proto="illegal"; encoding="delimited"`},
428-
output: FmtUnknown,
428+
output: fmtUnknown,
429429
},
430430
{
431431
input: map[string]string{"Content-Type": `application/vnd.google.protobuf; proto="io.prometheus.client.MetricFamily"; encoding="illegal"`},
432-
output: FmtUnknown,
432+
output: fmtUnknown,
433433
},
434434
{
435435
input: map[string]string{"Content-Type": `text/plain; version=0.0.4`},
436-
output: FmtText,
436+
output: fmtText,
437437
},
438438
{
439439
input: map[string]string{"Content-Type": `text/plain`},
440-
output: FmtText,
440+
output: fmtText,
441441
},
442442
{
443443
input: map[string]string{"Content-Type": `text/plain; version=0.0.3`},
444-
output: FmtUnknown,
444+
output: fmtUnknown,
445445
},
446446
}
447447

@@ -547,7 +547,7 @@ func TestTextDecoderWithBufioReader(t *testing.T) {
547547

548548
var decoded bool
549549
r := bufio.NewReader(strings.NewReader(example))
550-
dec := NewDecoder(r, FmtText)
550+
dec := NewDecoder(r, fmtText)
551551
for {
552552
var mf dto.MetricFamily
553553
if err := dec.Decode(&mf); err != nil {

expfmt/encode.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -76,18 +76,18 @@ func Negotiate(h http.Header) Format {
7676
if ac.Type+"/"+ac.SubType == ProtoType && ac.Params["proto"] == ProtoProtocol {
7777
switch ac.Params["encoding"] {
7878
case "delimited":
79-
return FmtProtoDelim + escapingScheme
79+
return fmtProtoDelim + escapingScheme
8080
case "text":
81-
return FmtProtoText + escapingScheme
81+
return fmtProtoText + escapingScheme
8282
case "compact-text":
83-
return FmtProtoCompact + escapingScheme
83+
return fmtProtoCompact + escapingScheme
8484
}
8585
}
8686
if ac.Type == "text" && ac.SubType == "plain" && (ver == TextVersion || ver == "") {
87-
return FmtText + escapingScheme
87+
return fmtText + escapingScheme
8888
}
8989
}
90-
return FmtText + escapingScheme
90+
return fmtText + escapingScheme
9191
}
9292

9393
// NegotiateIncludingOpenMetrics works like Negotiate but includes
@@ -109,26 +109,26 @@ func NegotiateIncludingOpenMetrics(h http.Header) Format {
109109
if ac.Type+"/"+ac.SubType == ProtoType && ac.Params["proto"] == ProtoProtocol {
110110
switch ac.Params["encoding"] {
111111
case "delimited":
112-
return FmtProtoDelim + escapingScheme
112+
return fmtProtoDelim + escapingScheme
113113
case "text":
114-
return FmtProtoText + escapingScheme
114+
return fmtProtoText + escapingScheme
115115
case "compact-text":
116-
return FmtProtoCompact + escapingScheme
116+
return fmtProtoCompact + escapingScheme
117117
}
118118
}
119119
if ac.Type == "text" && ac.SubType == "plain" && (ver == TextVersion || ver == "") {
120-
return FmtText + escapingScheme
120+
return fmtText + escapingScheme
121121
}
122122
if ac.Type+"/"+ac.SubType == OpenMetricsType && (ver == OpenMetricsVersion_0_0_1 || ver == OpenMetricsVersion_1_0_0 || ver == "") {
123123
switch ver {
124124
case OpenMetricsVersion_1_0_0:
125-
return FmtOpenMetrics_1_0_0 + escapingScheme
125+
return fmtOpenMetrics_1_0_0 + escapingScheme
126126
default:
127-
return FmtOpenMetrics_0_0_1 + escapingScheme
127+
return fmtOpenMetrics_0_0_1 + escapingScheme
128128
}
129129
}
130130
}
131-
return FmtText + escapingScheme
131+
return fmtText + escapingScheme
132132
}
133133

134134
// NewEncoder returns a new encoder based on content type negotiation. All

expfmt/encode_test.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ func TestNegotiateOpenMetrics(t *testing.T) {
201201

202202
func TestEncode(t *testing.T) {
203203
var buff bytes.Buffer
204-
delimEncoder := NewEncoder(&buff, FmtProtoDelim)
204+
delimEncoder := NewEncoder(&buff, fmtProtoDelim)
205205
metric := &dto.MetricFamily{
206206
Name: proto.String("foo_metric"),
207207
Type: dto.MetricType_UNTYPED.Enum(),
@@ -226,7 +226,7 @@ func TestEncode(t *testing.T) {
226226

227227
buff.Reset()
228228

229-
compactEncoder := NewEncoder(&buff, FmtProtoCompact)
229+
compactEncoder := NewEncoder(&buff, fmtProtoCompact)
230230
err = compactEncoder.Encode(metric)
231231
if err != nil {
232232
t.Errorf("unexpected error during encode: %s", err.Error())
@@ -239,7 +239,7 @@ func TestEncode(t *testing.T) {
239239

240240
buff.Reset()
241241

242-
protoTextEncoder := NewEncoder(&buff, FmtProtoText)
242+
protoTextEncoder := NewEncoder(&buff, fmtProtoText)
243243
err = protoTextEncoder.Encode(metric)
244244
if err != nil {
245245
t.Errorf("unexpected error during encode: %s", err.Error())
@@ -252,7 +252,7 @@ func TestEncode(t *testing.T) {
252252

253253
buff.Reset()
254254

255-
textEncoder := NewEncoder(&buff, FmtText)
255+
textEncoder := NewEncoder(&buff, fmtText)
256256
err = textEncoder.Encode(metric)
257257
if err != nil {
258258
t.Errorf("unexpected error during encode: %s", err.Error())
@@ -273,7 +273,7 @@ func TestEncode(t *testing.T) {
273273

274274
func TestEscapedEncode(t *testing.T) {
275275
var buff bytes.Buffer
276-
delimEncoder := NewEncoder(&buff, FmtProtoDelim+"; escaping=underscores")
276+
delimEncoder := NewEncoder(&buff, fmtProtoDelim+"; escaping=underscores")
277277
metric := &dto.MetricFamily{
278278
Name: proto.String("foo.metric"),
279279
Type: dto.MetricType_UNTYPED.Enum(),
@@ -309,7 +309,7 @@ func TestEscapedEncode(t *testing.T) {
309309

310310
buff.Reset()
311311

312-
compactEncoder := NewEncoder(&buff, FmtProtoCompact)
312+
compactEncoder := NewEncoder(&buff, fmtProtoCompact)
313313
err = compactEncoder.Encode(metric)
314314
if err != nil {
315315
t.Errorf("unexpected error during encode: %s", err.Error())
@@ -322,7 +322,7 @@ func TestEscapedEncode(t *testing.T) {
322322

323323
buff.Reset()
324324

325-
protoTextEncoder := NewEncoder(&buff, FmtProtoText)
325+
protoTextEncoder := NewEncoder(&buff, fmtProtoText)
326326
err = protoTextEncoder.Encode(metric)
327327
if err != nil {
328328
t.Errorf("unexpected error during encode: %s", err.Error())
@@ -335,7 +335,7 @@ func TestEscapedEncode(t *testing.T) {
335335

336336
buff.Reset()
337337

338-
textEncoder := NewEncoder(&buff, FmtText)
338+
textEncoder := NewEncoder(&buff, fmtText)
339339
err = textEncoder.Encode(metric)
340340
if err != nil {
341341
t.Errorf("unexpected error during encode: %s", err.Error())

expfmt/expfmt.go

+31-10
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,21 @@ const (
3434
TextVersion = "0.0.4"
3535
ProtoType = `application/vnd.google.protobuf`
3636
ProtoProtocol = `io.prometheus.client.MetricFamily`
37-
ProtoFmt = ProtoType + "; proto=" + ProtoProtocol + ";"
37+
protoFmt = ProtoType + "; proto=" + ProtoProtocol + ";"
3838
OpenMetricsType = `application/openmetrics-text`
3939
OpenMetricsVersion_0_0_1 = "0.0.1"
4040
OpenMetricsVersion_1_0_0 = "1.0.0"
4141

42-
// The Content-Type values for the different wire protocols. Do not do direct
43-
// comparisons to these constants, instead use the comparison functions.
44-
FmtUnknown Format = `<unknown>`
45-
FmtText Format = `text/plain; version=` + TextVersion + `; charset=utf-8`
46-
FmtProtoDelim Format = ProtoFmt + ` encoding=delimited`
47-
FmtProtoText Format = ProtoFmt + ` encoding=text`
48-
FmtProtoCompact Format = ProtoFmt + ` encoding=compact-text`
49-
FmtOpenMetrics_1_0_0 Format = OpenMetricsType + `; version=` + OpenMetricsVersion_1_0_0 + `; charset=utf-8`
50-
FmtOpenMetrics_0_0_1 Format = OpenMetricsType + `; version=` + OpenMetricsVersion_0_0_1 + `; charset=utf-8`
42+
// The Content-Type values for the different wire protocols. Note that these
43+
// values are now unexported. If code was relying on comparisons to these
44+
// constants, instead use FormatType().
45+
fmtUnknown Format = `<unknown>`
46+
fmtText Format = `text/plain; version=` + TextVersion + `; charset=utf-8`
47+
fmtProtoDelim Format = protoFmt + ` encoding=delimited`
48+
fmtProtoText Format = protoFmt + ` encoding=text`
49+
fmtProtoCompact Format = protoFmt + ` encoding=compact-text`
50+
fmtOpenMetrics_1_0_0 Format = OpenMetricsType + `; version=` + OpenMetricsVersion_1_0_0 + `; charset=utf-8`
51+
fmtOpenMetrics_0_0_1 Format = OpenMetricsType + `; version=` + OpenMetricsVersion_0_0_1 + `; charset=utf-8`
5152
)
5253

5354
const (
@@ -70,6 +71,26 @@ const (
7071
TypeOpenMetrics
7172
)
7273

74+
// NewFormat generates a new Format from the type provided. Mostly used for
75+
// tests, most Formats should be generated as part of content negotiation in
76+
// encode.go.
77+
func NewFormat(t FormatType) Format {
78+
switch t {
79+
case TypeProtoCompact:
80+
return fmtProtoCompact
81+
case TypeProtoDelim:
82+
return fmtProtoDelim
83+
case TypeProtoText:
84+
return fmtProtoText
85+
case TypeTextPlain:
86+
return fmtText
87+
case TypeOpenMetrics:
88+
return fmtOpenMetrics_1_0_0
89+
default:
90+
return fmtUnknown
91+
}
92+
}
93+
7394
// FormatType deduces an overall FormatType for the given format.
7495
func (f Format) FormatType() FormatType {
7596
toks := strings.Split(string(f), ";")

0 commit comments

Comments
 (0)