godeps: remove prometheus and deps
License: MIT Signed-off-by: Lars Gierth <larsg@systemli.org>
Lars Gierth committed
Apr 11, 2016 at 19:37 UTC
0e906466e727469ac880d065d415cd5dfcf5be5b
146 files changed
-31543
Godeps/Godeps.json
-32
@@ -14,10 +14,6 @@
14
"Comment": "null-5",
15
"Rev": "75cd24fc2f2c2a2088577d12123ddee5f54e0675"
16
},
17
- {
18
- "ImportPath": "github.com/beorn7/perks/quantile",
19
- "Rev": "b965b613227fddccbfffe13eae360ed3fa822f8d"
20
- },
17
{
18
"ImportPath": "github.com/bren2010/proquint",
19
"Rev": "5958552242606512f714d2e93513b380f43f9991"
@@ -74,10 +70,6 @@
70
"ImportPath": "github.com/gogo/protobuf/proto",
71
"Rev": "0ac967c269268f1af7d9bcc7927ccc9a589b2b36"
72
},
77
- {
78
- "ImportPath": "github.com/golang/protobuf/proto",
79
- "Rev": "aece6fb931241ad332956db4f62798dfbea944b3"
80
- },
73
{
74
"ImportPath": "github.com/hashicorp/golang-lru",
75
"Rev": "253b2dc1ca8bae42c3b5b6e53dd2eab1a7551116"
@@ -160,30 +152,6 @@
152
"ImportPath": "github.com/olekukonko/ts",
153
"Rev": "ecf753e7c962639ab5a1fb46f7da627d4c0a04b8"
154
},
163
- {
164
- "ImportPath": "github.com/prometheus/client_golang/model",
165
- "Comment": "0.6.0-15-g00e4c46",
166
- "Rev": "00e4c4629626a912cb8b44f8839a058768fc9c3f"
167
- },
168
- {
169
- "ImportPath": "github.com/prometheus/client_golang/prometheus",
170
- "Comment": "0.6.0-15-g00e4c46",
171
- "Rev": "00e4c4629626a912cb8b44f8839a058768fc9c3f"
172
- },
173
- {
174
- "ImportPath": "github.com/prometheus/client_golang/text",
175
- "Comment": "0.6.0-15-g00e4c46",
176
- "Rev": "00e4c4629626a912cb8b44f8839a058768fc9c3f"
177
- },
178
- {
179
- "ImportPath": "github.com/prometheus/client_model/go",
180
- "Comment": "model-0.0.2-12-gfa8ad6f",
181
- "Rev": "fa8ad6fec33561be4280a8f0514318c79d7f6cb6"
182
- },
183
- {
184
- "ImportPath": "github.com/prometheus/procfs",
185
- "Rev": "c91d8eefde16bd047416409eb56353ea84a186e4"
186
- },
155
{
156
"ImportPath": "github.com/rs/cors",
157
"Rev": "5e4ce6bc0ecd3472f6f943666d84876691be2ced"
Godeps/_workspace/src/github.com/beorn7/perks/quantile/bench_test.go
deleted
-63
@@ -1,63 +0,0 @@
1
-package quantile
2
-
3
-import (
4
- "testing"
5
-)
6
-
7
-func BenchmarkInsertTargeted(b *testing.B) {
8
- b.ReportAllocs()
9
-
10
- s := NewTargeted(Targets)
11
- b.ResetTimer()
12
- for i := float64(0); i < float64(b.N); i++ {
13
- s.Insert(i)
14
- }
15
-}
16
-
17
-func BenchmarkInsertTargetedSmallEpsilon(b *testing.B) {
18
- s := NewTargeted(TargetsSmallEpsilon)
19
- b.ResetTimer()
20
- for i := float64(0); i < float64(b.N); i++ {
21
- s.Insert(i)
22
- }
23
-}
24
-
25
-func BenchmarkInsertBiased(b *testing.B) {
26
- s := NewLowBiased(0.01)
27
- b.ResetTimer()
28
- for i := float64(0); i < float64(b.N); i++ {
29
- s.Insert(i)
30
- }
31
-}
32
-
33
-func BenchmarkInsertBiasedSmallEpsilon(b *testing.B) {
34
- s := NewLowBiased(0.0001)
35
- b.ResetTimer()
36
- for i := float64(0); i < float64(b.N); i++ {
37
- s.Insert(i)
38
- }
39
-}
40
-
41
-func BenchmarkQuery(b *testing.B) {
42
- s := NewTargeted(Targets)
43
- for i := float64(0); i < 1e6; i++ {
44
- s.Insert(i)
45
- }
46
- b.ResetTimer()
47
- n := float64(b.N)
48
- for i := float64(0); i < n; i++ {
49
- s.Query(i / n)
50
- }
51
-}
52
-
53
-func BenchmarkQuerySmallEpsilon(b *testing.B) {
54
- s := NewTargeted(TargetsSmallEpsilon)
55
- for i := float64(0); i < 1e6; i++ {
56
- s.Insert(i)
57
- }
58
- b.ResetTimer()
59
- n := float64(b.N)
60
- for i := float64(0); i < n; i++ {
61
- s.Query(i / n)
62
- }
63
-}
Godeps/_workspace/src/github.com/beorn7/perks/quantile/example_test.go
deleted
-121
@@ -1,121 +0,0 @@
1
-// +build go1.1
2
-
3
-package quantile_test
4
-
5
-import (
6
- "bufio"
7
- "fmt"
8
- "log"
9
- "os"
10
- "strconv"
11
- "time"
12
-
13
- "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/beorn7/perks/quantile"
14
-)
15
-
16
-func Example_simple() {
17
- ch := make(chan float64)
18
- go sendFloats(ch)
19
-
20
- // Compute the 50th, 90th, and 99th percentile.
21
- q := quantile.NewTargeted(map[float64]float64{
22
- 0.50: 0.005,
23
- 0.90: 0.001,
24
- 0.99: 0.0001,
25
- })
26
- for v := range ch {
27
- q.Insert(v)
28
- }
29
-
30
- fmt.Println("perc50:", q.Query(0.50))
31
- fmt.Println("perc90:", q.Query(0.90))
32
- fmt.Println("perc99:", q.Query(0.99))
33
- fmt.Println("count:", q.Count())
34
- // Output:
35
- // perc50: 5
36
- // perc90: 16
37
- // perc99: 223
38
- // count: 2388
39
-}
40
-
41
-func Example_mergeMultipleStreams() {
42
- // Scenario:
43
- // We have multiple database shards. On each shard, there is a process
44
- // collecting query response times from the database logs and inserting
45
- // them into a Stream (created via NewTargeted(0.90)), much like the
46
- // Simple example. These processes expose a network interface for us to
47
- // ask them to serialize and send us the results of their
48
- // Stream.Samples so we may Merge and Query them.
49
- //
50
- // NOTES:
51
- // * These sample sets are small, allowing us to get them
52
- // across the network much faster than sending the entire list of data
53
- // points.
54
- //
55
- // * For this to work correctly, we must supply the same quantiles
56
- // a priori the process collecting the samples supplied to NewTargeted,
57
- // even if we do not plan to query them all here.
58
- ch := make(chan quantile.Samples)
59
- getDBQuerySamples(ch)
60
- q := quantile.NewTargeted(map[float64]float64{0.90: 0.001})
61
- for samples := range ch {
62
- q.Merge(samples)
63
- }
64
- fmt.Println("perc90:", q.Query(0.90))
65
-}
66
-
67
-func Example_window() {
68
- // Scenario: We want the 90th, 95th, and 99th percentiles for each
69
- // minute.
70
-
71
- ch := make(chan float64)
72
- go sendStreamValues(ch)
73
-
74
- tick := time.NewTicker(1 * time.Minute)
75
- q := quantile.NewTargeted(map[float64]float64{
76
- 0.90: 0.001,
77
- 0.95: 0.0005,
78
- 0.99: 0.0001,
79
- })
80
- for {
81
- select {
82
- case t := <-tick.C:
83
- flushToDB(t, q.Samples())
84
- q.Reset()
85
- case v := <-ch:
86
- q.Insert(v)
87
- }
88
- }
89
-}
90
-
91
-func sendStreamValues(ch chan float64) {
92
- // Use your imagination
93
-}
94
-
95
-func flushToDB(t time.Time, samples quantile.Samples) {
96
- // Use your imagination
97
-}
98
-
99
-// This is a stub for the above example. In reality this would hit the remote
100
-// servers via http or something like it.
101
-func getDBQuerySamples(ch chan quantile.Samples) {}
102
-
103
-func sendFloats(ch chan<- float64) {
104
- f, err := os.Open("exampledata.txt")
105
- if err != nil {
106
- log.Fatal(err)
107
- }
108
- sc := bufio.NewScanner(f)
109
- for sc.Scan() {
110
- b := sc.Bytes()
111
- v, err := strconv.ParseFloat(string(b), 64)
112
- if err != nil {
113
- log.Fatal(err)
114
- }
115
- ch <- v
116
- }
117
- if sc.Err() != nil {
118
- log.Fatal(sc.Err())
119
- }
120
- close(ch)
121
-}
Godeps/_workspace/src/github.com/beorn7/perks/quantile/exampledata.txt
deleted
-2388
@@ -1,2388 +0,0 @@
1
-8
2
-5
3
-26
4
-12
5
-5
6
-235
7
-13
8
-6
9
-28
10
-30
11
-3
12
-3
13
-3
14
-3
15
-5
16
-2
17
-33
18
-7
19
-2
20
-4
21
-7
22
-12
23
-14
24
-5
25
-8
26
-3
27
-10
28
-4
29
-5
30
-3
31
-6
32
-6
33
-209
34
-20
35
-3
36
-10
37
-14
38
-3
39
-4
40
-6
41
-8
42
-5
43
-11
44
-7
45
-3
46
-2
47
-3
48
-3
49
-212
50
-5
51
-222
52
-4
53
-10
54
-10
55
-5
56
-6
57
-3
58
-8
59
-3
60
-10
61
-254
62
-220
63
-2
64
-3
65
-5
66
-24
67
-5
68
-4
69
-222
70
-7
71
-3
72
-3
73
-223
74
-8
75
-15
76
-12
77
-14
78
-14
79
-3
80
-2
81
-2
82
-3
83
-13
84
-3
85
-11
86
-4
87
-4
88
-6
89
-5
90
-7
91
-13
92
-5
93
-3
94
-5
95
-2
96
-5
97
-3
98
-5
99
-2
100
-7
101
-15
102
-17
103
-14
104
-3
105
-6
106
-6
107
-3
108
-17
109
-5
110
-4
111
-7
112
-6
113
-4
114
-4
115
-8
116
-6
117
-8
118
-3
119
-9
120
-3
121
-6
122
-3
123
-4
124
-5
125
-3
126
-3
127
-660
128
-4
129
-6
130
-10
131
-3
132
-6
133
-3
134
-2
135
-5
136
-13
137
-2
138
-4
139
-4
140
-10
141
-4
142
-8
143
-4
144
-3
145
-7
146
-9
147
-9
148
-3
149
-10
150
-37
151
-3
152
-13
153
-4
154
-12
155
-3
156
-6
157
-10
158
-8
159
-5
160
-21
161
-2
162
-3
163
-8
164
-3
165
-2
166
-3
167
-3
168
-4
169
-12
170
-2
171
-4
172
-8
173
-8
174
-4
175
-3
176
-2
177
-20
178
-1
179
-6
180
-32
181
-2
182
-11
183
-6
184
-18
185
-3
186
-8
187
-11
188
-3
189
-212
190
-3
191
-4
192
-2
193
-6
194
-7
195
-12
196
-11
197
-3
198
-2
199
-16
200
-10
201
-6
202
-4
203
-6
204
-3
205
-2
206
-7
207
-3
208
-2
209
-2
210
-2
211
-2
212
-5
213
-6
214
-4
215
-3
216
-10
217
-3
218
-4
219
-6
220
-5
221
-3
222
-4
223
-4
224
-5
225
-6
226
-4
227
-3
228
-4
229
-4
230
-5
231
-7
232
-5
233
-5
234
-3
235
-2
236
-7
237
-2
238
-4
239
-12
240
-4
241
-5
242
-6
243
-2
244
-4
245
-4
246
-8
247
-4
248
-15
249
-13
250
-7
251
-16
252
-5
253
-3
254
-23
255
-5
256
-5
257
-7
258
-3
259
-2
260
-9
261
-8
262
-7
263
-5
264
-8
265
-11
266
-4
267
-10
268
-76
269
-4
270
-47
271
-4
272
-3
273
-2
274
-7
275
-4
276
-2
277
-3
278
-37
279
-10
280
-4
281
-2
282
-20
283
-5
284
-4
285
-4
286
-10
287
-10
288
-4
289
-3
290
-7
291
-23
292
-240
293
-7
294
-13
295
-5
296
-5
297
-3
298
-3
299
-2
300
-5
301
-4
302
-2
303
-8
304
-7
305
-19
306
-2
307
-23
308
-8
309
-7
310
-2
311
-5
312
-3
313
-8
314
-3
315
-8
316
-13
317
-5
318
-5
319
-5
320
-2
321
-3
322
-23
323
-4
324
-9
325
-8
326
-4
327
-3
328
-3
329
-5
330
-220
331
-2
332
-3
333
-4
334
-6
335
-14
336
-3
337
-53
338
-6
339
-2
340
-5
341
-18
342
-6
343
-3
344
-219
345
-6
346
-5
347
-2
348
-5
349
-3
350
-6
351
-5
352
-15
353
-4
354
-3
355
-17
356
-3
357
-2
358
-4
359
-7
360
-2
361
-3
362
-3
363
-4
364
-4
365
-3
366
-2
367
-664
368
-6
369
-3
370
-23
371
-5
372
-5
373
-16
374
-5
375
-8
376
-2
377
-4
378
-2
379
-24
380
-12
381
-3
382
-2
383
-3
384
-5
385
-8
386
-3
387
-5
388
-4
389
-3
390
-14
391
-3
392
-5
393
-8
394
-2
395
-3
396
-7
397
-9
398
-4
399
-2
400
-3
401
-6
402
-8
403
-4
404
-3
405
-4
406
-6
407
-5
408
-3
409
-3
410
-6
411
-3
412
-19
413
-4
414
-4
415
-6
416
-3
417
-6
418
-3
419
-5
420
-22
421
-5
422
-4
423
-4
424
-3
425
-8
426
-11
427
-4
428
-9
429
-7
430
-6
431
-13
432
-4
433
-4
434
-4
435
-6
436
-17
437
-9
438
-3
439
-3
440
-3
441
-4
442
-3
443
-221
444
-5
445
-11
446
-3
447
-4
448
-2
449
-12
450
-6
451
-3
452
-5
453
-7
454
-5
455
-7
456
-4
457
-9
458
-7
459
-14
460
-37
461
-19
462
-217
463
-16
464
-3
465
-5
466
-2
467
-2
468
-7
469
-19
470
-7
471
-6
472
-7
473
-4
474
-24
475
-5
476
-11
477
-4
478
-7
479
-7
480
-9
481
-13
482
-3
483
-4
484
-3
485
-6
486
-28
487
-4
488
-4
489
-5
490
-5
491
-2
492
-5
493
-6
494
-4
495
-4
496
-6
497
-10
498
-5
499
-4
500
-3
501
-2
502
-3
503
-3
504
-6
505
-5
506
-5
507
-4
508
-3
509
-2
510
-3
511
-7
512
-4
513
-6
514
-18
515
-16
516
-8
517
-16
518
-4
519
-5
520
-8
521
-6
522
-9
523
-13
524
-1545
525
-6
526
-215
527
-6
528
-5
529
-6
530
-3
531
-45
532
-31
533
-5
534
-2
535
-2
536
-4
537
-3
538
-3
539
-2
540
-5
541
-4
542
-3
543
-5
544
-7
545
-7
546
-4
547
-5
548
-8
549
-5
550
-4
551
-749
552
-2
553
-31
554
-9
555
-11
556
-2
557
-11
558
-5
559
-4
560
-4
561
-7
562
-9
563
-11
564
-4
565
-5
566
-4
567
-7
568
-3
569
-4
570
-6
571
-2
572
-15
573
-3
574
-4
575
-3
576
-4
577
-3
578
-5
579
-2
580
-13
581
-5
582
-5
583
-3
584
-3
585
-23
586
-4
587
-4
588
-5
589
-7
590
-4
591
-13
592
-2
593
-4
594
-3
595
-4
596
-2
597
-6
598
-2
599
-7
600
-3
601
-5
602
-5
603
-3
604
-29
605
-5
606
-4
607
-4
608
-3
609
-10
610
-2
611
-3
612
-79
613
-16
614
-6
615
-6
616
-7
617
-7
618
-3
619
-5
620
-5
621
-7
622
-4
623
-3
624
-7
625
-9
626
-5
627
-6
628
-5
629
-9
630
-6
631
-3
632
-6
633
-4
634
-17
635
-2
636
-10
637
-9
638
-3
639
-6
640
-2
641
-3
642
-21
643
-22
644
-5
645
-11
646
-4
647
-2
648
-17
649
-2
650
-224
651
-2
652
-14
653
-3
654
-4
655
-4
656
-2
657
-4
658
-4
659
-4
660
-4
661
-5
662
-3
663
-4
664
-4
665
-10
666
-2
667
-6
668
-3
669
-3
670
-5
671
-7
672
-2
673
-7
674
-5
675
-6
676
-3
677
-218
678
-2
679
-2
680
-5
681
-2
682
-6
683
-3
684
-5
685
-222
686
-14
687
-6
688
-33
689
-3
690
-2
691
-5
692
-3
693
-3
694
-3
695
-9
696
-5
697
-3
698
-3
699
-2
700
-7
701
-4
702
-3
703
-4
704
-3
705
-5
706
-6
707
-5
708
-26
709
-4
710
-13
711
-9
712
-7
713
-3
714
-221
715
-3
716
-3
717
-4
718
-4
719
-4
720
-4
721
-2
722
-18
723
-5
724
-3
725
-7
726
-9
727
-6
728
-8
729
-3
730
-10
731
-3
732
-11
733
-9
734
-5
735
-4
736
-17
737
-5
738
-5
739
-6
740
-6
741
-3
742
-2
743
-4
744
-12
745
-17
746
-6
747
-7
748
-218
749
-4
750
-2
751
-4
752
-10
753
-3
754
-5
755
-15
756
-3
757
-9
758
-4
759
-3
760
-3
761
-6
762
-29
763
-3
764
-3
765
-4
766
-5
767
-5
768
-3
769
-8
770
-5
771
-6
772
-6
773
-7
774
-5
775
-3
776
-5
777
-3
778
-29
779
-2
780
-31
781
-5
782
-15
783
-24
784
-16
785
-5
786
-207
787
-4
788
-3
789
-3
790
-2
791
-15
792
-4
793
-4
794
-13
795
-5
796
-5
797
-4
798
-6
799
-10
800
-2
801
-7
802
-8
803
-4
804
-6
805
-20
806
-5
807
-3
808
-4
809
-3
810
-12
811
-12
812
-5
813
-17
814
-7
815
-3
816
-3
817
-3
818
-6
819
-10
820
-3
821
-5
822
-25
823
-80
824
-4
825
-9
826
-3
827
-2
828
-11
829
-3
830
-3
831
-2
832
-3
833
-8
834
-7
835
-5
836
-5
837
-19
838
-5
839
-3
840
-3
841
-12
842
-11
843
-2
844
-6
845
-5
846
-5
847
-5
848
-3
849
-3
850
-3
851
-4
852
-209
853
-14
854
-3
855
-2
856
-5
857
-19
858
-4
859
-4
860
-3
861
-4
862
-14
863
-5
864
-6
865
-4
866
-13
867
-9
868
-7
869
-4
870
-7
871
-10
872
-2
873
-9
874
-5
875
-7
876
-2
877
-8
878
-4
879
-6
880
-5
881
-5
882
-222
883
-8
884
-7
885
-12
886
-5
887
-216
888
-3
889
-4
890
-4
891
-6
892
-3
893
-14
894
-8
895
-7
896
-13
897
-4
898
-3
899
-3
900
-3
901
-3
902
-17
903
-5
904
-4
905
-3
906
-33
907
-6
908
-6
909
-33
910
-7
911
-5
912
-3
913
-8
914
-7
915
-5
916
-2
917
-9
918
-4
919
-2
920
-233
921
-24
922
-7
923
-4
924
-8
925
-10
926
-3
927
-4
928
-15
929
-2
930
-16
931
-3
932
-3
933
-13
934
-12
935
-7
936
-5
937
-4
938
-207
939
-4
940
-2
941
-4
942
-27
943
-15
944
-2
945
-5
946
-2
947
-25
948
-6
949
-5
950
-5
951
-6
952
-13
953
-6
954
-18
955
-6
956
-4
957
-12
958
-225
959
-10
960
-7
961
-5
962
-2
963
-2
964
-11
965
-4
966
-14
967
-21
968
-8
969
-10
970
-3
971
-5
972
-4
973
-232
974
-2
975
-5
976
-5
977
-3
978
-7
979
-17
980
-11
981
-6
982
-6
983
-23
984
-4
985
-6
986
-3
987
-5
988
-4
989
-2
990
-17
991
-3
992
-6
993
-5
994
-8
995
-3
996
-2
997
-2
998
-14
999
-9
1000
-4
1001
-4
1002
-2
1003
-5
1004
-5
1005
-3
1006
-7
1007
-6
1008
-12
1009
-6
1010
-10
1011
-3
1012
-6
1013
-2
1014
-2
1015
-19
1016
-5
1017
-4
1018
-4
1019
-9
1020
-2
1021
-4
1022
-13
1023
-3
1024
-5
1025
-6
1026
-3
1027
-6
1028
-5
1029
-4
1030
-9
1031
-6
1032
-3
1033
-5
1034
-7
1035
-3
1036
-6
1037
-6
1038
-4
1039
-3
1040
-10
1041
-6
1042
-3
1043
-221
1044
-3
1045
-5
1046
-3
1047
-6
1048
-4
1049
-8
1050
-5
1051
-3
1052
-6
1053
-4
1054
-4
1055
-2
1056
-54
1057
-5
1058
-6
1059
-11
1060
-3
1061
-3
1062
-4
1063
-4
1064
-4
1065
-3
1066
-7
1067
-3
1068
-11
1069
-11
1070
-7
1071
-10
1072
-6
1073
-13
1074
-223
1075
-213
1076
-15
1077
-231
1078
-7
1079
-3
1080
-7
1081
-228
1082
-2
1083
-3
1084
-4
1085
-4
1086
-5
1087
-6
1088
-7
1089
-4
1090
-13
1091
-3
1092
-4
1093
-5
1094
-3
1095
-6
1096
-4
1097
-6
1098
-7
1099
-2
1100
-4
1101
-3
1102
-4
1103
-3
1104
-3
1105
-6
1106
-3
1107
-7
1108
-3
1109
-5
1110
-18
1111
-5
1112
-6
1113
-8
1114
-10
1115
-3
1116
-3
1117
-3
1118
-2
1119
-4
1120
-2
1121
-4
1122
-4
1123
-5
1124
-6
1125
-6
1126
-4
1127
-10
1128
-13
1129
-3
1130
-12
1131
-5
1132
-12
1133
-16
1134
-8
1135
-4
1136
-19
1137
-11
1138
-2
1139
-4
1140
-5
1141
-6
1142
-8
1143
-5
1144
-6
1145
-4
1146
-18
1147
-10
1148
-4
1149
-2
1150
-216
1151
-6
1152
-6
1153
-6
1154
-2
1155
-4
1156
-12
1157
-8
1158
-3
1159
-11
1160
-5
1161
-6
1162
-14
1163
-5
1164
-3
1165
-13
1166
-4
1167
-5
1168
-4
1169
-5
1170
-3
1171
-28
1172
-6
1173
-3
1174
-7
1175
-219
1176
-3
1177
-9
1178
-7
1179
-3
1180
-10
1181
-6
1182
-3
1183
-4
1184
-19
1185
-5
1186
-7
1187
-11
1188
-6
1189
-15
1190
-19
1191
-4
1192
-13
1193
-11
1194
-3
1195
-7
1196
-5
1197
-10
1198
-2
1199
-8
1200
-11
1201
-2
1202
-6
1203
-4
1204
-6
1205
-24
1206
-6
1207
-3
1208
-3
1209
-3
1210
-3
1211
-6
1212
-18
1213
-4
1214
-11
1215
-4
1216
-2
1217
-5
1218
-10
1219
-8
1220
-3
1221
-9
1222
-5
1223
-3
1224
-4
1225
-5
1226
-6
1227
-2
1228
-5
1229
-7
1230
-4
1231
-4
1232
-14
1233
-6
1234
-4
1235
-4
1236
-5
1237
-5
1238
-7
1239
-2
1240
-4
1241
-3
1242
-7
1243
-3
1244
-3
1245
-6
1246
-4
1247
-5
1248
-4
1249
-4
1250
-4
1251
-3
1252
-3
1253
-3
1254
-3
1255
-8
1256
-14
1257
-2
1258
-3
1259
-5
1260
-3
1261
-2
1262
-4
1263
-5
1264
-3
1265
-7
1266
-3
1267
-3
1268
-18
1269
-3
1270
-4
1271
-4
1272
-5
1273
-7
1274
-3
1275
-3
1276
-3
1277
-13
1278
-5
1279
-4
1280
-8
1281
-211
1282
-5
1283
-5
1284
-3
1285
-5
1286
-2
1287
-5
1288
-4
1289
-2
1290
-655
1291
-6
1292
-3
1293
-5
1294
-11
1295
-2
1296
-5
1297
-3
1298
-12
1299
-9
1300
-15
1301
-11
1302
-5
1303
-12
1304
-217
1305
-2
1306
-6
1307
-17
1308
-3
1309
-3
1310
-207
1311
-5
1312
-5
1313
-4
1314
-5
1315
-9
1316
-3
1317
-2
1318
-8
1319
-5
1320
-4
1321
-3
1322
-2
1323
-5
1324
-12
1325
-4
1326
-14
1327
-5
1328
-4
1329
-2
1330
-13
1331
-5
1332
-8
1333
-4
1334
-225
1335
-4
1336
-3
1337
-4
1338
-5
1339
-4
1340
-3
1341
-3
1342
-6
1343
-23
1344
-9
1345
-2
1346
-6
1347
-7
1348
-233
1349
-4
1350
-4
1351
-6
1352
-18
1353
-3
1354
-4
1355
-6
1356
-3
1357
-4
1358
-4
1359
-2
1360
-3
1361
-7
1362
-4
1363
-13
1364
-227
1365
-4
1366
-3
1367
-5
1368
-4
1369
-2
1370
-12
1371
-9
1372
-17
1373
-3
1374
-7
1375
-14
1376
-6
1377
-4
1378
-5
1379
-21
1380
-4
1381
-8
1382
-9
1383
-2
1384
-9
1385
-25
1386
-16
1387
-3
1388
-6
1389
-4
1390
-7
1391
-8
1392
-5
1393
-2
1394
-3
1395
-5
1396
-4
1397
-3
1398
-3
1399
-5
1400
-3
1401
-3
1402
-3
1403
-2
1404
-3
1405
-19
1406
-2
1407
-4
1408
-3
1409
-4
1410
-2
1411
-3
1412
-4
1413
-4
1414
-2
1415
-4
1416
-3
1417
-3
1418
-3
1419
-2
1420
-6
1421
-3
1422
-17
1423
-5
1424
-6
1425
-4
1426
-3
1427
-13
1428
-5
1429
-3
1430
-3
1431
-3
1432
-4
1433
-9
1434
-4
1435
-2
1436
-14
1437
-12
1438
-4
1439
-5
1440
-24
1441
-4
1442
-3
1443
-37
1444
-12
1445
-11
1446
-21
1447
-3
1448
-4
1449
-3
1450
-13
1451
-4
1452
-2
1453
-3
1454
-15
1455
-4
1456
-11
1457
-4
1458
-4
1459
-3
1460
-8
1461
-3
1462
-4
1463
-4
1464
-12
1465
-8
1466
-5
1467
-3
1468
-3
1469
-4
1470
-2
1471
-220
1472
-3
1473
-5
1474
-223
1475
-3
1476
-3
1477
-3
1478
-10
1479
-3
1480
-15
1481
-4
1482
-241
1483
-9
1484
-7
1485
-3
1486
-6
1487
-6
1488
-23
1489
-4
1490
-13
1491
-7
1492
-3
1493
-4
1494
-7
1495
-4
1496
-9
1497
-3
1498
-3
1499
-4
1500
-10
1501
-5
1502
-5
1503
-1
1504
-5
1505
-24
1506
-2
1507
-4
1508
-5
1509
-5
1510
-6
1511
-14
1512
-3
1513
-8
1514
-2
1515
-3
1516
-5
1517
-13
1518
-13
1519
-3
1520
-5
1521
-2
1522
-3
1523
-15
1524
-3
1525
-4
1526
-2
1527
-10
1528
-4
1529
-4
1530
-4
1531
-5
1532
-5
1533
-3
1534
-5
1535
-3
1536
-4
1537
-7
1538
-4
1539
-27
1540
-3
1541
-6
1542
-4
1543
-15
1544
-3
1545
-5
1546
-6
1547
-6
1548
-5
1549
-4
1550
-8
1551
-3
1552
-9
1553
-2
1554
-6
1555
-3
1556
-4
1557
-3
1558
-7
1559
-4
1560
-18
1561
-3
1562
-11
1563
-3
1564
-3
1565
-8
1566
-9
1567
-7
1568
-24
1569
-3
1570
-219
1571
-7
1572
-10
1573
-4
1574
-5
1575
-9
1576
-12
1577
-2
1578
-5
1579
-4
1580
-4
1581
-4
1582
-3
1583
-3
1584
-19
1585
-5
1586
-8
1587
-16
1588
-8
1589
-6
1590
-22
1591
-3
1592
-23
1593
-3
1594
-242
1595
-9
1596
-4
1597
-3
1598
-3
1599
-5
1600
-7
1601
-3
1602
-3
1603
-5
1604
-8
1605
-3
1606
-7
1607
-5
1608
-14
1609
-8
1610
-10
1611
-3
1612
-4
1613
-3
1614
-7
1615
-4
1616
-6
1617
-7
1618
-4
1619
-10
1620
-4
1621
-3
1622
-11
1623
-3
1624
-7
1625
-10
1626
-3
1627
-13
1628
-6
1629
-8
1630
-12
1631
-10
1632
-5
1633
-7
1634
-9
1635
-3
1636
-4
1637
-7
1638
-7
1639
-10
1640
-8
1641
-30
1642
-9
1643
-19
1644
-4
1645
-3
1646
-19
1647
-15
1648
-4
1649
-13
1650
-3
1651
-215
1652
-223
1653
-4
1654
-7
1655
-4
1656
-8
1657
-17
1658
-16
1659
-3
1660
-7
1661
-6
1662
-5
1663
-5
1664
-4
1665
-12
1666
-3
1667
-7
1668
-4
1669
-4
1670
-13
1671
-4
1672
-5
1673
-2
1674
-5
1675
-6
1676
-5
1677
-6
1678
-6
1679
-7
1680
-10
1681
-18
1682
-23
1683
-9
1684
-3
1685
-3
1686
-6
1687
-5
1688
-2
1689
-4
1690
-2
1691
-7
1692
-3
1693
-3
1694
-2
1695
-5
1696
-5
1697
-14
1698
-10
1699
-224
1700
-6
1701
-3
1702
-4
1703
-3
1704
-7
1705
-5
1706
-9
1707
-3
1708
-6
1709
-4
1710
-2
1711
-5
1712
-11
1713
-4
1714
-3
1715
-3
1716
-2
1717
-8
1718
-4
1719
-7
1720
-4
1721
-10
1722
-7
1723
-3
1724
-3
1725
-18
1726
-18
1727
-17
1728
-3
1729
-3
1730
-3
1731
-4
1732
-5
1733
-3
1734
-3
1735
-4
1736
-12
1737
-7
1738
-3
1739
-11
1740
-13
1741
-5
1742
-4
1743
-7
1744
-13
1745
-5
1746
-4
1747
-11
1748
-3
1749
-12
1750
-3
1751
-6
1752
-4
1753
-4
1754
-21
1755
-4
1756
-6
1757
-9
1758
-5
1759
-3
1760
-10
1761
-8
1762
-4
1763
-6
1764
-4
1765
-4
1766
-6
1767
-5
1768
-4
1769
-8
1770
-6
1771
-4
1772
-6
1773
-4
1774
-4
1775
-5
1776
-9
1777
-6
1778
-3
1779
-4
1780
-2
1781
-9
1782
-3
1783
-18
1784
-2
1785
-4
1786
-3
1787
-13
1788
-3
1789
-6
1790
-6
1791
-8
1792
-7
1793
-9
1794
-3
1795
-2
1796
-16
1797
-3
1798
-4
1799
-6
1800
-3
1801
-2
1802
-33
1803
-22
1804
-14
1805
-4
1806
-9
1807
-12
1808
-4
1809
-5
1810
-6
1811
-3
1812
-23
1813
-9
1814
-4
1815
-3
1816
-5
1817
-5
1818
-3
1819
-4
1820
-5
1821
-3
1822
-5
1823
-3
1824
-10
1825
-4
1826
-5
1827
-5
1828
-8
1829
-4
1830
-4
1831
-6
1832
-8
1833
-5
1834
-4
1835
-3
1836
-4
1837
-6
1838
-3
1839
-3
1840
-3
1841
-5
1842
-9
1843
-12
1844
-6
1845
-5
1846
-9
1847
-3
1848
-5
1849
-3
1850
-2
1851
-2
1852
-2
1853
-18
1854
-3
1855
-2
1856
-21
1857
-2
1858
-5
1859
-4
1860
-6
1861
-4
1862
-5
1863
-10
1864
-3
1865
-9
1866
-3
1867
-2
1868
-10
1869
-7
1870
-3
1871
-6
1872
-6
1873
-4
1874
-4
1875
-8
1876
-12
1877
-7
1878
-3
1879
-7
1880
-3
1881
-3
1882
-9
1883
-3
1884
-4
1885
-5
1886
-4
1887
-4
1888
-5
1889
-5
1890
-10
1891
-15
1892
-4
1893
-4
1894
-14
1895
-6
1896
-227
1897
-3
1898
-14
1899
-5
1900
-216
1901
-22
1902
-5
1903
-4
1904
-2
1905
-2
1906
-6
1907
-3
1908
-4
1909
-2
1910
-9
1911
-9
1912
-4
1913
-3
1914
-28
1915
-13
1916
-11
1917
-4
1918
-5
1919
-3
1920
-3
1921
-2
1922
-3
1923
-3
1924
-5
1925
-3
1926
-4
1927
-3
1928
-5
1929
-23
1930
-26
1931
-3
1932
-4
1933
-5
1934
-6
1935
-4
1936
-6
1937
-3
1938
-5
1939
-5
1940
-3
1941
-4
1942
-3
1943
-2
1944
-2
1945
-2
1946
-7
1947
-14
1948
-3
1949
-6
1950
-7
1951
-17
1952
-2
1953
-2
1954
-15
1955
-14
1956
-16
1957
-4
1958
-6
1959
-7
1960
-13
1961
-6
1962
-4
1963
-5
1964
-6
1965
-16
1966
-3
1967
-3
1968
-28
1969
-3
1970
-6
1971
-15
1972
-3
1973
-9
1974
-2
1975
-4
1976
-6
1977
-3
1978
-3
1979
-22
1980
-4
1981
-12
1982
-6
1983
-7
1984
-2
1985
-5
1986
-4
1987
-10
1988
-3
1989
-16
1990
-6
1991
-9
1992
-2
1993
-5
1994
-12
1995
-7
1996
-5
1997
-5
1998
-5
1999
-5
2000
-2
2001
-11
2002
-9
2003
-17
2004
-4
2005
-3
2006
-11
2007
-7
2008
-3
2009
-5
2010
-15
2011
-4
2012
-3
2013
-4
2014
-211
2015
-8
2016
-7
2017
-5
2018
-4
2019
-7
2020
-6
2021
-7
2022
-6
2023
-3
2024
-6
2025
-5
2026
-6
2027
-5
2028
-3
2029
-4
2030
-4
2031
-26
2032
-4
2033
-6
2034
-10
2035
-4
2036
-4
2037
-3
2038
-2
2039
-3
2040
-3
2041
-4
2042
-5
2043
-9
2044
-3
2045
-9
2046
-4
2047
-4
2048
-5
2049
-5
2050
-8
2051
-2
2052
-4
2053
-2
2054
-3
2055
-8
2056
-4
2057
-11
2058
-19
2059
-5
2060
-8
2061
-6
2062
-3
2063
-5
2064
-6
2065
-12
2066
-3
2067
-2
2068
-4
2069
-16
2070
-12
2071
-3
2072
-4
2073
-4
2074
-8
2075
-6
2076
-5
2077
-6
2078
-6
2079
-219
2080
-8
2081
-222
2082
-6
2083
-16
2084
-3
2085
-13
2086
-19
2087
-5
2088
-4
2089
-3
2090
-11
2091
-6
2092
-10
2093
-4
2094
-7
2095
-7
2096
-12
2097
-5
2098
-3
2099
-3
2100
-5
2101
-6
2102
-10
2103
-3
2104
-8
2105
-2
2106
-5
2107
-4
2108
-7
2109
-2
2110
-4
2111
-4
2112
-2
2113
-12
2114
-9
2115
-6
2116
-4
2117
-2
2118
-40
2119
-2
2120
-4
2121
-10
2122
-4
2123
-223
2124
-4
2125
-2
2126
-20
2127
-6
2128
-7
2129
-24
2130
-5
2131
-4
2132
-5
2133
-2
2134
-20
2135
-16
2136
-6
2137
-5
2138
-13
2139
-2
2140
-3
2141
-3
2142
-19
2143
-3
2144
-2
2145
-4
2146
-5
2147
-6
2148
-7
2149
-11
2150
-12
2151
-5
2152
-6
2153
-7
2154
-7
2155
-3
2156
-5
2157
-3
2158
-5
2159
-3
2160
-14
2161
-3
2162
-4
2163
-4
2164
-2
2165
-11
2166
-1
2167
-7
2168
-3
2169
-9
2170
-6
2171
-11
2172
-12
2173
-5
2174
-8
2175
-6
2176
-221
2177
-4
2178
-2
2179
-12
2180
-4
2181
-3
2182
-15
2183
-4
2184
-5
2185
-226
2186
-7
2187
-218
2188
-7
2189
-5
2190
-4
2191
-5
2192
-18
2193
-4
2194
-5
2195
-9
2196
-4
2197
-4
2198
-2
2199
-9
2200
-18
2201
-18
2202
-9
2203
-5
2204
-6
2205
-6
2206
-3
2207
-3
2208
-7
2209
-3
2210
-5
2211
-4
2212
-4
2213
-4
2214
-12
2215
-3
2216
-6
2217
-31
2218
-5
2219
-4
2220
-7
2221
-3
2222
-6
2223
-5
2224
-6
2225
-5
2226
-11
2227
-2
2228
-2
2229
-11
2230
-11
2231
-6
2232
-7
2233
-5
2234
-8
2235
-7
2236
-10
2237
-5
2238
-23
2239
-7
2240
-4
2241
-3
2242
-5
2243
-34
2244
-2
2245
-5
2246
-23
2247
-7
2248
-3
2249
-6
2250
-8
2251
-4
2252
-4
2253
-4
2254
-2
2255
-5
2256
-3
2257
-8
2258
-5
2259
-4
2260
-8
2261
-25
2262
-2
2263
-3
2264
-17
2265
-8
2266
-3
2267
-4
2268
-8
2269
-7
2270
-3
2271
-15
2272
-6
2273
-5
2274
-7
2275
-21
2276
-9
2277
-5
2278
-6
2279
-6
2280
-5
2281
-3
2282
-2
2283
-3
2284
-10
2285
-3
2286
-6
2287
-3
2288
-14
2289
-7
2290
-4
2291
-4
2292
-8
2293
-7
2294
-8
2295
-2
2296
-6
2297
-12
2298
-4
2299
-213
2300
-6
2301
-5
2302
-21
2303
-8
2304
-2
2305
-5
2306
-23
2307
-3
2308
-11
2309
-2
2310
-3
2311
-6
2312
-25
2313
-2
2314
-3
2315
-6
2316
-7
2317
-6
2318
-6
2319
-4
2320
-4
2321
-6
2322
-3
2323
-17
2324
-9
2325
-7
2326
-6
2327
-4
2328
-3
2329
-10
2330
-7
2331
-2
2332
-3
2333
-3
2334
-3
2335
-11
2336
-8
2337
-3
2338
-7
2339
-6
2340
-4
2341
-14
2342
-36
2343
-3
2344
-4
2345
-3
2346
-3
2347
-22
2348
-13
2349
-21
2350
-4
2351
-2
2352
-7
2353
-4
2354
-4
2355
-17
2356
-15
2357
-3
2358
-7
2359
-11
2360
-2
2361
-4
2362
-7
2363
-6
2364
-209
2365
-6
2366
-3
2367
-2
2368
-2
2369
-24
2370
-4
2371
-9
2372
-4
2373
-3
2374
-3
2375
-3
2376
-29
2377
-2
2378
-2
2379
-4
2380
-3
2381
-3
2382
-5
2383
-4
2384
-6
2385
-3
2386
-3
2387
-2
2388
-4
Godeps/_workspace/src/github.com/beorn7/perks/quantile/stream.go
deleted
-292
@@ -1,292 +0,0 @@
1
-// Package quantile computes approximate quantiles over an unbounded data
2
-// stream within low memory and CPU bounds.
3
-//
4
-// A small amount of accuracy is traded to achieve the above properties.
5
-//
6
-// Multiple streams can be merged before calling Query to generate a single set
7
-// of results. This is meaningful when the streams represent the same type of
8
-// data. See Merge and Samples.
9
-//
10
-// For more detailed information about the algorithm used, see:
11
-//
12
-// Effective Computation of Biased Quantiles over Data Streams
13
-//
14
-// http://www.cs.rutgers.edu/~muthu/bquant.pdf
15
-package quantile
16
-
17
-import (
18
- "math"
19
- "sort"
20
-)
21
-
22
-// Sample holds an observed value and meta information for compression. JSON
23
-// tags have been added for convenience.
24
-type Sample struct {
25
- Value float64 `json:",string"`
26
- Width float64 `json:",string"`
27
- Delta float64 `json:",string"`
28
-}
29
-
30
-// Samples represents a slice of samples. It implements sort.Interface.
31
-type Samples []Sample
32
-
33
-func (a Samples) Len() int { return len(a) }
34
-func (a Samples) Less(i, j int) bool { return a[i].Value < a[j].Value }
35
-func (a Samples) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
36
-
37
-type invariant func(s *stream, r float64) float64
38
-
39
-// NewLowBiased returns an initialized Stream for low-biased quantiles
40
-// (e.g. 0.01, 0.1, 0.5) where the needed quantiles are not known a priori, but
41
-// error guarantees can still be given even for the lower ranks of the data
42
-// distribution.
43
-//
44
-// The provided epsilon is a relative error, i.e. the true quantile of a value
45
-// returned by a query is guaranteed to be within (1±Epsilon)*Quantile.
46
-//
47
-// See http://www.cs.rutgers.edu/~muthu/bquant.pdf for time, space, and error
48
-// properties.
49
-func NewLowBiased(epsilon float64) *Stream {
50
- ƒ := func(s *stream, r float64) float64 {
51
- return 2 * epsilon * r
52
- }
53
- return newStream(ƒ)
54
-}
55
-
56
-// NewHighBiased returns an initialized Stream for high-biased quantiles
57
-// (e.g. 0.01, 0.1, 0.5) where the needed quantiles are not known a priori, but
58
-// error guarantees can still be given even for the higher ranks of the data
59
-// distribution.
60
-//
61
-// The provided epsilon is a relative error, i.e. the true quantile of a value
62
-// returned by a query is guaranteed to be within 1-(1±Epsilon)*(1-Quantile).
63
-//
64
-// See http://www.cs.rutgers.edu/~muthu/bquant.pdf for time, space, and error
65
-// properties.
66
-func NewHighBiased(epsilon float64) *Stream {
67
- ƒ := func(s *stream, r float64) float64 {
68
- return 2 * epsilon * (s.n - r)
69
- }
70
- return newStream(ƒ)
71
-}
72
-
73
-// NewTargeted returns an initialized Stream concerned with a particular set of
74
-// quantile values that are supplied a priori. Knowing these a priori reduces
75
-// space and computation time. The targets map maps the desired quantiles to
76
-// their absolute errors, i.e. the true quantile of a value returned by a query
77
-// is guaranteed to be within (Quantile±Epsilon).
78
-//
79
-// See http://www.cs.rutgers.edu/~muthu/bquant.pdf for time, space, and error properties.
80
-func NewTargeted(targets map[float64]float64) *Stream {
81
- ƒ := func(s *stream, r float64) float64 {
82
- var m = math.MaxFloat64
83
- var f float64
84
- for quantile, epsilon := range targets {
85
- if quantile*s.n <= r {
86
- f = (2 * epsilon * r) / quantile
87
- } else {
88
- f = (2 * epsilon * (s.n - r)) / (1 - quantile)
89
- }
90
- if f < m {
91
- m = f
92
- }
93
- }
94
- return m
95
- }
96
- return newStream(ƒ)
97
-}
98
-
99
-// Stream computes quantiles for a stream of float64s. It is not thread-safe by
100
-// design. Take care when using across multiple goroutines.
101
-type Stream struct {
102
- *stream
103
- b Samples
104
- sorted bool
105
-}
106
-
107
-func newStream(ƒ invariant) *Stream {
108
- x := &stream{ƒ: ƒ}
109
- return &Stream{x, make(Samples, 0, 500), true}
110
-}
111
-
112
-// Insert inserts v into the stream.
113
-func (s *Stream) Insert(v float64) {
114
- s.insert(Sample{Value: v, Width: 1})
115
-}
116
-
117
-func (s *Stream) insert(sample Sample) {
118
- s.b = append(s.b, sample)
119
- s.sorted = false
120
- if len(s.b) == cap(s.b) {
121
- s.flush()
122
- }
123
-}
124
-
125
-// Query returns the computed qth percentiles value. If s was created with
126
-// NewTargeted, and q is not in the set of quantiles provided a priori, Query
127
-// will return an unspecified result.
128
-func (s *Stream) Query(q float64) float64 {
129
- if !s.flushed() {
130
- // Fast path when there hasn't been enough data for a flush;
131
- // this also yields better accuracy for small sets of data.
132
- l := len(s.b)
133
- if l == 0 {
134
- return 0
135
- }
136
- i := int(float64(l) * q)
137
- if i > 0 {
138
- i -= 1
139
- }
140
- s.maybeSort()
141
- return s.b[i].Value
142
- }
143
- s.flush()
144
- return s.stream.query(q)
145
-}
146
-
147
-// Merge merges samples into the underlying streams samples. This is handy when
148
-// merging multiple streams from separate threads, database shards, etc.
149
-//
150
-// ATTENTION: This method is broken and does not yield correct results. The
151
-// underlying algorithm is not capable of merging streams correctly.
152
-func (s *Stream) Merge(samples Samples) {
153
- sort.Sort(samples)
154
- s.stream.merge(samples)
155
-}
156
-
157
-// Reset reinitializes and clears the list reusing the samples buffer memory.
158
-func (s *Stream) Reset() {
159
- s.stream.reset()
160
- s.b = s.b[:0]
161
-}
162
-
163
-// Samples returns stream samples held by s.
164
-func (s *Stream) Samples() Samples {
165
- if !s.flushed() {
166
- return s.b
167
- }
168
- s.flush()
169
- return s.stream.samples()
170
-}
171
-
172
-// Count returns the total number of samples observed in the stream
173
-// since initialization.
174
-func (s *Stream) Count() int {
175
- return len(s.b) + s.stream.count()
176
-}
177
-
178
-func (s *Stream) flush() {
179
- s.maybeSort()
180
- s.stream.merge(s.b)
181
- s.b = s.b[:0]
182
-}
183
-
184
-func (s *Stream) maybeSort() {
185
- if !s.sorted {
186
- s.sorted = true
187
- sort.Sort(s.b)
188
- }
189
-}
190
-
191
-func (s *Stream) flushed() bool {
192
- return len(s.stream.l) > 0
193
-}
194
-
195
-type stream struct {
196
- n float64
197
- l []Sample
198
- ƒ invariant
199
-}
200
-
201
-func (s *stream) reset() {
202
- s.l = s.l[:0]
203
- s.n = 0
204
-}
205
-
206
-func (s *stream) insert(v float64) {
207
- s.merge(Samples{{v, 1, 0}})
208
-}
209
-
210
-func (s *stream) merge(samples Samples) {
211
- // TODO(beorn7): This tries to merge not only individual samples, but
212
- // whole summaries. The paper doesn't mention merging summaries at
213
- // all. Unittests show that the merging is inaccurate. Find out how to
214
- // do merges properly.
215
- var r float64
216
- i := 0
217
- for _, sample := range samples {
218
- for ; i < len(s.l); i++ {
219
- c := s.l[i]
220
- if c.Value > sample.Value {
221
- // Insert at position i.
222
- s.l = append(s.l, Sample{})
223
- copy(s.l[i+1:], s.l[i:])
224
- s.l[i] = Sample{
225
- sample.Value,
226
- sample.Width,
227
- math.Max(sample.Delta, math.Floor(s.ƒ(s, r))-1),
228
- // TODO(beorn7): How to calculate delta correctly?
229
- }
230
- i++
231
- goto inserted
232
- }
233
- r += c.Width
234
- }
235
- s.l = append(s.l, Sample{sample.Value, sample.Width, 0})
236
- i++
237
- inserted:
238
- s.n += sample.Width
239
- r += sample.Width
240
- }
241
- s.compress()
242
-}
243
-
244
-func (s *stream) count() int {
245
- return int(s.n)
246
-}
247
-
248
-func (s *stream) query(q float64) float64 {
249
- t := math.Ceil(q * s.n)
250
- t += math.Ceil(s.ƒ(s, t) / 2)
251
- p := s.l[0]
252
- var r float64
253
- for _, c := range s.l[1:] {
254
- r += p.Width
255
- if r+c.Width+c.Delta > t {
256
- return p.Value
257
- }
258
- p = c
259
- }
260
- return p.Value
261
-}
262
-
263
-func (s *stream) compress() {
264
- if len(s.l) < 2 {
265
- return
266
- }
267
- x := s.l[len(s.l)-1]
268
- xi := len(s.l) - 1
269
- r := s.n - 1 - x.Width
270
-
271
- for i := len(s.l) - 2; i >= 0; i-- {
272
- c := s.l[i]
273
- if c.Width+x.Width+x.Delta <= s.ƒ(s, r) {
274
- x.Width += c.Width
275
- s.l[xi] = x
276
- // Remove element at i.
277
- copy(s.l[i:], s.l[i+1:])
278
- s.l = s.l[:len(s.l)-1]
279
- xi -= 1
280
- } else {
281
- x = c
282
- xi = i
283
- }
284
- r -= c.Width
285
- }
286
-}
287
-
288
-func (s *stream) samples() Samples {
289
- samples := make(Samples, len(s.l))
290
- copy(samples, s.l)
291
- return samples
292
-}
Godeps/_workspace/src/github.com/beorn7/perks/quantile/stream_test.go
deleted
-188
@@ -1,188 +0,0 @@
1
-package quantile
2
-
3
-import (
4
- "math"
5
- "math/rand"
6
- "sort"
7
- "testing"
8
-)
9
-
10
-var (
11
- Targets = map[float64]float64{
12
- 0.01: 0.001,
13
- 0.10: 0.01,
14
- 0.50: 0.05,
15
- 0.90: 0.01,
16
- 0.99: 0.001,
17
- }
18
- TargetsSmallEpsilon = map[float64]float64{
19
- 0.01: 0.0001,
20
- 0.10: 0.001,
21
- 0.50: 0.005,
22
- 0.90: 0.001,
23
- 0.99: 0.0001,
24
- }
25
- LowQuantiles = []float64{0.01, 0.1, 0.5}
26
- HighQuantiles = []float64{0.99, 0.9, 0.5}
27
-)
28
-
29
-const RelativeEpsilon = 0.01
30
-
31
-func verifyPercsWithAbsoluteEpsilon(t *testing.T, a []float64, s *Stream) {
32
- sort.Float64s(a)
33
- for quantile, epsilon := range Targets {
34
- n := float64(len(a))
35
- k := int(quantile * n)
36
- lower := int((quantile - epsilon) * n)
37
- if lower < 1 {
38
- lower = 1
39
- }
40
- upper := int(math.Ceil((quantile + epsilon) * n))
41
- if upper > len(a) {
42
- upper = len(a)
43
- }
44
- w, min, max := a[k-1], a[lower-1], a[upper-1]
45
- if g := s.Query(quantile); g < min || g > max {
46
- t.Errorf("q=%f: want %v [%f,%f], got %v", quantile, w, min, max, g)
47
- }
48
- }
49
-}
50
-
51
-func verifyLowPercsWithRelativeEpsilon(t *testing.T, a []float64, s *Stream) {
52
- sort.Float64s(a)
53
- for _, qu := range LowQuantiles {
54
- n := float64(len(a))
55
- k := int(qu * n)
56
-
57
- lowerRank := int((1 - RelativeEpsilon) * qu * n)
58
- upperRank := int(math.Ceil((1 + RelativeEpsilon) * qu * n))
59
- w, min, max := a[k-1], a[lowerRank-1], a[upperRank-1]
60
- if g := s.Query(qu); g < min || g > max {
61
- t.Errorf("q=%f: want %v [%f,%f], got %v", qu, w, min, max, g)
62
- }
63
- }
64
-}
65
-
66
-func verifyHighPercsWithRelativeEpsilon(t *testing.T, a []float64, s *Stream) {
67
- sort.Float64s(a)
68
- for _, qu := range HighQuantiles {
69
- n := float64(len(a))
70
- k := int(qu * n)
71
-
72
- lowerRank := int((1 - (1+RelativeEpsilon)*(1-qu)) * n)
73
- upperRank := int(math.Ceil((1 - (1-RelativeEpsilon)*(1-qu)) * n))
74
- w, min, max := a[k-1], a[lowerRank-1], a[upperRank-1]
75
- if g := s.Query(qu); g < min || g > max {
76
- t.Errorf("q=%f: want %v [%f,%f], got %v", qu, w, min, max, g)
77
- }
78
- }
79
-}
80
-
81
-func populateStream(s *Stream) []float64 {
82
- a := make([]float64, 0, 1e5+100)
83
- for i := 0; i < cap(a); i++ {
84
- v := rand.NormFloat64()
85
- // Add 5% asymmetric outliers.
86
- if i%20 == 0 {
87
- v = v*v + 1
88
- }
89
- s.Insert(v)
90
- a = append(a, v)
91
- }
92
- return a
93
-}
94
-
95
-func TestTargetedQuery(t *testing.T) {
96
- rand.Seed(42)
97
- s := NewTargeted(Targets)
98
- a := populateStream(s)
99
- verifyPercsWithAbsoluteEpsilon(t, a, s)
100
-}
101
-
102
-func TestLowBiasedQuery(t *testing.T) {
103
- rand.Seed(42)
104
- s := NewLowBiased(RelativeEpsilon)
105
- a := populateStream(s)
106
- verifyLowPercsWithRelativeEpsilon(t, a, s)
107
-}
108
-
109
-func TestHighBiasedQuery(t *testing.T) {
110
- rand.Seed(42)
111
- s := NewHighBiased(RelativeEpsilon)
112
- a := populateStream(s)
113
- verifyHighPercsWithRelativeEpsilon(t, a, s)
114
-}
115
-
116
-// BrokenTestTargetedMerge is broken, see Merge doc comment.
117
-func BrokenTestTargetedMerge(t *testing.T) {
118
- rand.Seed(42)
119
- s1 := NewTargeted(Targets)
120
- s2 := NewTargeted(Targets)
121
- a := populateStream(s1)
122
- a = append(a, populateStream(s2)...)
123
- s1.Merge(s2.Samples())
124
- verifyPercsWithAbsoluteEpsilon(t, a, s1)
125
-}
126
-
127
-// BrokenTestLowBiasedMerge is broken, see Merge doc comment.
128
-func BrokenTestLowBiasedMerge(t *testing.T) {
129
- rand.Seed(42)
130
- s1 := NewLowBiased(RelativeEpsilon)
131
- s2 := NewLowBiased(RelativeEpsilon)
132
- a := populateStream(s1)
133
- a = append(a, populateStream(s2)...)
134
- s1.Merge(s2.Samples())
135
- verifyLowPercsWithRelativeEpsilon(t, a, s2)
136
-}
137
-
138
-// BrokenTestHighBiasedMerge is broken, see Merge doc comment.
139
-func BrokenTestHighBiasedMerge(t *testing.T) {
140
- rand.Seed(42)
141
- s1 := NewHighBiased(RelativeEpsilon)
142
- s2 := NewHighBiased(RelativeEpsilon)
143
- a := populateStream(s1)
144
- a = append(a, populateStream(s2)...)
145
- s1.Merge(s2.Samples())
146
- verifyHighPercsWithRelativeEpsilon(t, a, s2)
147
-}
148
-
149
-func TestUncompressed(t *testing.T) {
150
- q := NewTargeted(Targets)
151
- for i := 100; i > 0; i-- {
152
- q.Insert(float64(i))
153
- }
154
- if g := q.Count(); g != 100 {
155
- t.Errorf("want count 100, got %d", g)
156
- }
157
- // Before compression, Query should have 100% accuracy.
158
- for quantile := range Targets {
159
- w := quantile * 100
160
- if g := q.Query(quantile); g != w {
161
- t.Errorf("want %f, got %f", w, g)
162
- }
163
- }
164
-}
165
-
166
-func TestUncompressedSamples(t *testing.T) {
167
- q := NewTargeted(map[float64]float64{0.99: 0.001})
168
- for i := 1; i <= 100; i++ {
169
- q.Insert(float64(i))
170
- }
171
- if g := q.Samples().Len(); g != 100 {
172
- t.Errorf("want count 100, got %d", g)
173
- }
174
-}
175
-
176
-func TestUncompressedOne(t *testing.T) {
177
- q := NewTargeted(map[float64]float64{0.99: 0.01})
178
- q.Insert(3.14)
179
- if g := q.Query(0.90); g != 3.14 {
180
- t.Error("want PI, got", g)
181
- }
182
-}
183
-
184
-func TestDefaults(t *testing.T) {
185
- if g := NewTargeted(map[float64]float64{0.99: 0.001}).Query(0.99); g != 0 {
186
- t.Errorf("want 0, got %f", g)
187
- }
188
-}
Godeps/_workspace/src/github.com/golang/protobuf/proto/Makefile
deleted
-43
@@ -1,43 +0,0 @@
1
-# Go support for Protocol Buffers - Google's data interchange format
2
-#
3
-# Copyright 2010 The Go Authors. All rights reserved.
4
-# https://github.com/golang/protobuf
5
-#
6
-# Redistribution and use in source and binary forms, with or without
7
-# modification, are permitted provided that the following conditions are
8
-# met:
9
-#
10
-# * Redistributions of source code must retain the above copyright
11
-# notice, this list of conditions and the following disclaimer.
12
-# * Redistributions in binary form must reproduce the above
13
-# copyright notice, this list of conditions and the following disclaimer
14
-# in the documentation and/or other materials provided with the
15
-# distribution.
16
-# * Neither the name of Google Inc. nor the names of its
17
-# contributors may be used to endorse or promote products derived from
18
-# this software without specific prior written permission.
19
-#
20
-# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
-# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
-# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23
-# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24
-# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25
-# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26
-# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27
-# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28
-# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29
-# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
-# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
-
32
-install:
33
- go install
34
-
35
-test: install generate-test-pbs
36
- go test
37
-
38
-
39
-generate-test-pbs:
40
- make install
41
- make -C testdata
42
- protoc --go_out=Mtestdata/test.proto=github.com/golang/protobuf/proto/testdata:. proto3_proto/proto3.proto
43
- make
Godeps/_workspace/src/github.com/golang/protobuf/proto/all_test.go
deleted
-2083
@@ -1,2083 +0,0 @@
1
-// Go support for Protocol Buffers - Google's data interchange format
2
-//
3
-// Copyright 2010 The Go Authors. All rights reserved.
4
-// https://github.com/golang/protobuf
5
-//
6
-// Redistribution and use in source and binary forms, with or without
7
-// modification, are permitted provided that the following conditions are
8
-// met:
9
-//
10
-// * Redistributions of source code must retain the above copyright
11
-// notice, this list of conditions and the following disclaimer.
12
-// * Redistributions in binary form must reproduce the above
13
-// copyright notice, this list of conditions and the following disclaimer
14
-// in the documentation and/or other materials provided with the
15
-// distribution.
16
-// * Neither the name of Google Inc. nor the names of its
17
-// contributors may be used to endorse or promote products derived from
18
-// this software without specific prior written permission.
19
-//
20
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
-// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
-// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23
-// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24
-// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25
-// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26
-// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27
-// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28
-// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29
-// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
-// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
-
32
-package proto_test
33
-
34
-import (
35
- "bytes"
36
- "encoding/json"
37
- "errors"
38
- "fmt"
39
- "math"
40
- "math/rand"
41
- "reflect"
42
- "runtime/debug"
43
- "strings"
44
- "testing"
45
- "time"
46
-
47
- . "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/golang/protobuf/proto"
48
- . "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/golang/protobuf/proto/testdata"
49
-)
50
-
51
-var globalO *Buffer
52
-
53
-func old() *Buffer {
54
- if globalO == nil {
55
- globalO = NewBuffer(nil)
56
- }
57
- globalO.Reset()
58
- return globalO
59
-}
60
-
61
-func equalbytes(b1, b2 []byte, t *testing.T) {
62
- if len(b1) != len(b2) {
63
- t.Errorf("wrong lengths: 2*%d != %d", len(b1), len(b2))
64
- return
65
- }
66
- for i := 0; i < len(b1); i++ {
67
- if b1[i] != b2[i] {
68
- t.Errorf("bad byte[%d]:%x %x: %s %s", i, b1[i], b2[i], b1, b2)
69
- }
70
- }
71
-}
72
-
73
-func initGoTestField() *GoTestField {
74
- f := new(GoTestField)
75
- f.Label = String("label")
76
- f.Type = String("type")
77
- return f
78
-}
79
-
80
-// These are all structurally equivalent but the tag numbers differ.
81
-// (It's remarkable that required, optional, and repeated all have
82
-// 8 letters.)
83
-func initGoTest_RequiredGroup() *GoTest_RequiredGroup {
84
- return &GoTest_RequiredGroup{
85
- RequiredField: String("required"),
86
- }
87
-}
88
-
89
-func initGoTest_OptionalGroup() *GoTest_OptionalGroup {
90
- return &GoTest_OptionalGroup{
91
- RequiredField: String("optional"),
92
- }
93
-}
94
-
95
-func initGoTest_RepeatedGroup() *GoTest_RepeatedGroup {
96
- return &GoTest_RepeatedGroup{
97
- RequiredField: String("repeated"),
98
- }
99
-}
100
-
101
-func initGoTest(setdefaults bool) *GoTest {
102
- pb := new(GoTest)
103
- if setdefaults {
104
- pb.F_BoolDefaulted = Bool(Default_GoTest_F_BoolDefaulted)
105
- pb.F_Int32Defaulted = Int32(Default_GoTest_F_Int32Defaulted)
106
- pb.F_Int64Defaulted = Int64(Default_GoTest_F_Int64Defaulted)
107
- pb.F_Fixed32Defaulted = Uint32(Default_GoTest_F_Fixed32Defaulted)
108
- pb.F_Fixed64Defaulted = Uint64(Default_GoTest_F_Fixed64Defaulted)
109
- pb.F_Uint32Defaulted = Uint32(Default_GoTest_F_Uint32Defaulted)
110
- pb.F_Uint64Defaulted = Uint64(Default_GoTest_F_Uint64Defaulted)
111
- pb.F_FloatDefaulted = Float32(Default_GoTest_F_FloatDefaulted)
112
- pb.F_DoubleDefaulted = Float64(Default_GoTest_F_DoubleDefaulted)
113
- pb.F_StringDefaulted = String(Default_GoTest_F_StringDefaulted)
114
- pb.F_BytesDefaulted = Default_GoTest_F_BytesDefaulted
115
- pb.F_Sint32Defaulted = Int32(Default_GoTest_F_Sint32Defaulted)
116
- pb.F_Sint64Defaulted = Int64(Default_GoTest_F_Sint64Defaulted)
117
- }
118
-
119
- pb.Kind = GoTest_TIME.Enum()
120
- pb.RequiredField = initGoTestField()
121
- pb.F_BoolRequired = Bool(true)
122
- pb.F_Int32Required = Int32(3)
123
- pb.F_Int64Required = Int64(6)
124
- pb.F_Fixed32Required = Uint32(32)
125
- pb.F_Fixed64Required = Uint64(64)
126
- pb.F_Uint32Required = Uint32(3232)
127
- pb.F_Uint64Required = Uint64(6464)
128
- pb.F_FloatRequired = Float32(3232)
129
- pb.F_DoubleRequired = Float64(6464)
130
- pb.F_StringRequired = String("string")
131
- pb.F_BytesRequired = []byte("bytes")
132
- pb.F_Sint32Required = Int32(-32)
133
- pb.F_Sint64Required = Int64(-64)
134
- pb.Requiredgroup = initGoTest_RequiredGroup()
135
-
136
- return pb
137
-}
138
-
139
-func fail(msg string, b *bytes.Buffer, s string, t *testing.T) {
140
- data := b.Bytes()
141
- ld := len(data)
142
- ls := len(s) / 2
143
-
144
- fmt.Printf("fail %s ld=%d ls=%d\n", msg, ld, ls)
145
-
146
- // find the interesting spot - n
147
- n := ls
148
- if ld < ls {
149
- n = ld
150
- }
151
- j := 0
152
- for i := 0; i < n; i++ {
153
- bs := hex(s[j])*16 + hex(s[j+1])
154
- j += 2
155
- if data[i] == bs {
156
- continue
157
- }
158
- n = i
159
- break
160
- }
161
- l := n - 10
162
- if l < 0 {
163
- l = 0
164
- }
165
- h := n + 10
166
-
167
- // find the interesting spot - n
168
- fmt.Printf("is[%d]:", l)
169
- for i := l; i < h; i++ {
170
- if i >= ld {
171
- fmt.Printf(" --")
172
- continue
173
- }
174
- fmt.Printf(" %.2x", data[i])
175
- }
176
- fmt.Printf("\n")
177
-
178
- fmt.Printf("sb[%d]:", l)
179
- for i := l; i < h; i++ {
180
- if i >= ls {
181
- fmt.Printf(" --")
182
- continue
183
- }
184
- bs := hex(s[j])*16 + hex(s[j+1])
185
- j += 2
186
- fmt.Printf(" %.2x", bs)
187
- }
188
- fmt.Printf("\n")
189
-
190
- t.Fail()
191
-
192
- // t.Errorf("%s: \ngood: %s\nbad: %x", msg, s, b.Bytes())
193
- // Print the output in a partially-decoded format; can
194
- // be helpful when updating the test. It produces the output
195
- // that is pasted, with minor edits, into the argument to verify().
196
- // data := b.Bytes()
197
- // nesting := 0
198
- // for b.Len() > 0 {
199
- // start := len(data) - b.Len()
200
- // var u uint64
201
- // u, err := DecodeVarint(b)
202
- // if err != nil {
203
- // fmt.Printf("decode error on varint:", err)
204
- // return
205
- // }
206
- // wire := u & 0x7
207
- // tag := u >> 3
208
- // switch wire {
209
- // case WireVarint:
210
- // v, err := DecodeVarint(b)
211
- // if err != nil {
212
- // fmt.Printf("decode error on varint:", err)
213
- // return
214
- // }
215
- // fmt.Printf("\t\t\"%x\" // field %d, encoding %d, value %d\n",
216
- // data[start:len(data)-b.Len()], tag, wire, v)
217
- // case WireFixed32:
218
- // v, err := DecodeFixed32(b)
219
- // if err != nil {
220
- // fmt.Printf("decode error on fixed32:", err)
221
- // return
222
- // }
223
- // fmt.Printf("\t\t\"%x\" // field %d, encoding %d, value %d\n",
224
- // data[start:len(data)-b.Len()], tag, wire, v)
225
- // case WireFixed64:
226
- // v, err := DecodeFixed64(b)
227
- // if err != nil {
228
- // fmt.Printf("decode error on fixed64:", err)
229
- // return
230
- // }
231
- // fmt.Printf("\t\t\"%x\" // field %d, encoding %d, value %d\n",
232
- // data[start:len(data)-b.Len()], tag, wire, v)
233
- // case WireBytes:
234
- // nb, err := DecodeVarint(b)
235
- // if err != nil {
236
- // fmt.Printf("decode error on bytes:", err)
237
- // return
238
- // }
239
- // after_tag := len(data) - b.Len()
240
- // str := make([]byte, nb)
241
- // _, err = b.Read(str)
242
- // if err != nil {
243
- // fmt.Printf("decode error on bytes:", err)
244
- // return
245
- // }
246
- // fmt.Printf("\t\t\"%x\" \"%x\" // field %d, encoding %d (FIELD)\n",
247
- // data[start:after_tag], str, tag, wire)
248
- // case WireStartGroup:
249
- // nesting++
250
- // fmt.Printf("\t\t\"%x\"\t\t// start group field %d level %d\n",
251
- // data[start:len(data)-b.Len()], tag, nesting)
252
- // case WireEndGroup:
253
- // fmt.Printf("\t\t\"%x\"\t\t// end group field %d level %d\n",
254
- // data[start:len(data)-b.Len()], tag, nesting)
255
- // nesting--
256
- // default:
257
- // fmt.Printf("unrecognized wire type %d\n", wire)
258
- // return
259
- // }
260
- // }
261
-}
262
-
263
-func hex(c uint8) uint8 {
264
- if '0' <= c && c <= '9' {
265
- return c - '0'
266
- }
267
- if 'a' <= c && c <= 'f' {
268
- return 10 + c - 'a'
269
- }
270
- if 'A' <= c && c <= 'F' {
271
- return 10 + c - 'A'
272
- }
273
- return 0
274
-}
275
-
276
-func equal(b []byte, s string, t *testing.T) bool {
277
- if 2*len(b) != len(s) {
278
- // fail(fmt.Sprintf("wrong lengths: 2*%d != %d", len(b), len(s)), b, s, t)
279
- fmt.Printf("wrong lengths: 2*%d != %d\n", len(b), len(s))
280
- return false
281
- }
282
- for i, j := 0, 0; i < len(b); i, j = i+1, j+2 {
283
- x := hex(s[j])*16 + hex(s[j+1])
284
- if b[i] != x {
285
- // fail(fmt.Sprintf("bad byte[%d]:%x %x", i, b[i], x), b, s, t)
286
- fmt.Printf("bad byte[%d]:%x %x", i, b[i], x)
287
- return false
288
- }
289
- }
290
- return true
291
-}
292
-
293
-func overify(t *testing.T, pb *GoTest, expected string) {
294
- o := old()
295
- err := o.Marshal(pb)
296
- if err != nil {
297
- fmt.Printf("overify marshal-1 err = %v", err)
298
- o.DebugPrint("", o.Bytes())
299
- t.Fatalf("expected = %s", expected)
300
- }
301
- if !equal(o.Bytes(), expected, t) {
302
- o.DebugPrint("overify neq 1", o.Bytes())
303
- t.Fatalf("expected = %s", expected)
304
- }
305
-
306
- // Now test Unmarshal by recreating the original buffer.
307
- pbd := new(GoTest)
308
- err = o.Unmarshal(pbd)
309
- if err != nil {
310
- t.Fatalf("overify unmarshal err = %v", err)
311
- o.DebugPrint("", o.Bytes())
312
- t.Fatalf("string = %s", expected)
313
- }
314
- o.Reset()
315
- err = o.Marshal(pbd)
316
- if err != nil {
317
- t.Errorf("overify marshal-2 err = %v", err)
318
- o.DebugPrint("", o.Bytes())
319
- t.Fatalf("string = %s", expected)
320
- }
321
- if !equal(o.Bytes(), expected, t) {
322
- o.DebugPrint("overify neq 2", o.Bytes())
323
- t.Fatalf("string = %s", expected)
324
- }
325
-}
326
-
327
-// Simple tests for numeric encode/decode primitives (varint, etc.)
328
-func TestNumericPrimitives(t *testing.T) {
329
- for i := uint64(0); i < 1e6; i += 111 {
330
- o := old()
331
- if o.EncodeVarint(i) != nil {
332
- t.Error("EncodeVarint")
333
- break
334
- }
335
- x, e := o.DecodeVarint()
336
- if e != nil {
337
- t.Fatal("DecodeVarint")
338
- }
339
- if x != i {
340
- t.Fatal("varint decode fail:", i, x)
341
- }
342
-
343
- o = old()
344
- if o.EncodeFixed32(i) != nil {
345
- t.Fatal("encFixed32")
346
- }
347
- x, e = o.DecodeFixed32()
348
- if e != nil {
349
- t.Fatal("decFixed32")
350
- }
351
- if x != i {
352
- t.Fatal("fixed32 decode fail:", i, x)
353
- }
354
-
355
- o = old()
356
- if o.EncodeFixed64(i*1234567) != nil {
357
- t.Error("encFixed64")
358
- break
359
- }
360
- x, e = o.DecodeFixed64()
361
- if e != nil {
362
- t.Error("decFixed64")
363
- break
364
- }
365
- if x != i*1234567 {
366
- t.Error("fixed64 decode fail:", i*1234567, x)
367
- break
368
- }
369
-
370
- o = old()
371
- i32 := int32(i - 12345)
372
- if o.EncodeZigzag32(uint64(i32)) != nil {
373
- t.Fatal("EncodeZigzag32")
374
- }
375
- x, e = o.DecodeZigzag32()
376
- if e != nil {
377
- t.Fatal("DecodeZigzag32")
378
- }
379
- if x != uint64(uint32(i32)) {
380
- t.Fatal("zigzag32 decode fail:", i32, x)
381
- }
382
-
383
- o = old()
384
- i64 := int64(i - 12345)
385
- if o.EncodeZigzag64(uint64(i64)) != nil {
386
- t.Fatal("EncodeZigzag64")
387
- }
388
- x, e = o.DecodeZigzag64()
389
- if e != nil {
390
- t.Fatal("DecodeZigzag64")
391
- }
392
- if x != uint64(i64) {
393
- t.Fatal("zigzag64 decode fail:", i64, x)
394
- }
395
- }
396
-}
397
-
398
-// fakeMarshaler is a simple struct implementing Marshaler and Message interfaces.
399
-type fakeMarshaler struct {
400
- b []byte
401
- err error
402
-}
403
-
404
-func (f fakeMarshaler) Marshal() ([]byte, error) {
405
- return f.b, f.err
406
-}
407
-
408
-func (f fakeMarshaler) String() string {
409
- return fmt.Sprintf("Bytes: %v Error: %v", f.b, f.err)
410
-}
411
-
412
-func (f fakeMarshaler) ProtoMessage() {}
413
-
414
-func (f fakeMarshaler) Reset() {}
415
-
416
-// Simple tests for proto messages that implement the Marshaler interface.
417
-func TestMarshalerEncoding(t *testing.T) {
418
- tests := []struct {
419
- name string
420
- m Message
421
- want []byte
422
- wantErr error
423
- }{
424
- {
425
- name: "Marshaler that fails",
426
- m: fakeMarshaler{
427
- err: errors.New("some marshal err"),
428
- b: []byte{5, 6, 7},
429
- },
430
- // Since there's an error, nothing should be written to buffer.
431
- want: nil,
432
- wantErr: errors.New("some marshal err"),
433
- },
434
- {
435
- name: "Marshaler that succeeds",
436
- m: fakeMarshaler{
437
- b: []byte{0, 1, 2, 3, 4, 127, 255},
438
- },
439
- want: []byte{0, 1, 2, 3, 4, 127, 255},
440
- wantErr: nil,
441
- },
442
- }
443
- for _, test := range tests {
444
- b := NewBuffer(nil)
445
- err := b.Marshal(test.m)
446
- if !reflect.DeepEqual(test.wantErr, err) {
447
- t.Errorf("%s: got err %v wanted %v", test.name, err, test.wantErr)
448
- }
449
- if !reflect.DeepEqual(test.want, b.Bytes()) {
450
- t.Errorf("%s: got bytes %v wanted %v", test.name, b.Bytes(), test.want)
451
- }
452
- }
453
-}
454
-
455
-// Simple tests for bytes
456
-func TestBytesPrimitives(t *testing.T) {
457
- o := old()
458
- bytes := []byte{'n', 'o', 'w', ' ', 'i', 's', ' ', 't', 'h', 'e', ' ', 't', 'i', 'm', 'e'}
459
- if o.EncodeRawBytes(bytes) != nil {
460
- t.Error("EncodeRawBytes")
461
- }
462
- decb, e := o.DecodeRawBytes(false)
463
- if e != nil {
464
- t.Error("DecodeRawBytes")
465
- }
466
- equalbytes(bytes, decb, t)
467
-}
468
-
469
-// Simple tests for strings
470
-func TestStringPrimitives(t *testing.T) {
471
- o := old()
472
- s := "now is the time"
473
- if o.EncodeStringBytes(s) != nil {
474
- t.Error("enc_string")
475
- }
476
- decs, e := o.DecodeStringBytes()
477
- if e != nil {
478
- t.Error("dec_string")
479
- }
480
- if s != decs {
481
- t.Error("string encode/decode fail:", s, decs)
482
- }
483
-}
484
-
485
-// Do we catch the "required bit not set" case?
486
-func TestRequiredBit(t *testing.T) {
487
- o := old()
488
- pb := new(GoTest)
489
- err := o.Marshal(pb)
490
- if err == nil {
491
- t.Error("did not catch missing required fields")
492
- } else if strings.Index(err.Error(), "Kind") < 0 {
493
- t.Error("wrong error type:", err)
494
- }
495
-}
496
-
497
-// Check that all fields are nil.
498
-// Clearly silly, and a residue from a more interesting test with an earlier,
499
-// different initialization property, but it once caught a compiler bug so
500
-// it lives.
501
-func checkInitialized(pb *GoTest, t *testing.T) {
502
- if pb.F_BoolDefaulted != nil {
503
- t.Error("New or Reset did not set boolean:", *pb.F_BoolDefaulted)
504
- }
505
- if pb.F_Int32Defaulted != nil {
506
- t.Error("New or Reset did not set int32:", *pb.F_Int32Defaulted)
507
- }
508
- if pb.F_Int64Defaulted != nil {
509
- t.Error("New or Reset did not set int64:", *pb.F_Int64Defaulted)
510
- }
511
- if pb.F_Fixed32Defaulted != nil {
512
- t.Error("New or Reset did not set fixed32:", *pb.F_Fixed32Defaulted)
513
- }
514
- if pb.F_Fixed64Defaulted != nil {
515
- t.Error("New or Reset did not set fixed64:", *pb.F_Fixed64Defaulted)
516
- }
517
- if pb.F_Uint32Defaulted != nil {
518
- t.Error("New or Reset did not set uint32:", *pb.F_Uint32Defaulted)
519
- }
520
- if pb.F_Uint64Defaulted != nil {
521
- t.Error("New or Reset did not set uint64:", *pb.F_Uint64Defaulted)
522
- }
523
- if pb.F_FloatDefaulted != nil {
524
- t.Error("New or Reset did not set float:", *pb.F_FloatDefaulted)
525
- }
526
- if pb.F_DoubleDefaulted != nil {
527
- t.Error("New or Reset did not set double:", *pb.F_DoubleDefaulted)
528
- }
529
- if pb.F_StringDefaulted != nil {
530
- t.Error("New or Reset did not set string:", *pb.F_StringDefaulted)
531
- }
532
- if pb.F_BytesDefaulted != nil {
533
- t.Error("New or Reset did not set bytes:", string(pb.F_BytesDefaulted))
534
- }
535
- if pb.F_Sint32Defaulted != nil {
536
- t.Error("New or Reset did not set int32:", *pb.F_Sint32Defaulted)
537
- }
538
- if pb.F_Sint64Defaulted != nil {
539
- t.Error("New or Reset did not set int64:", *pb.F_Sint64Defaulted)
540
- }
541
-}
542
-
543
-// Does Reset() reset?
544
-func TestReset(t *testing.T) {
545
- pb := initGoTest(true)
546
- // muck with some values
547
- pb.F_BoolDefaulted = Bool(false)
548
- pb.F_Int32Defaulted = Int32(237)
549
- pb.F_Int64Defaulted = Int64(12346)
550
- pb.F_Fixed32Defaulted = Uint32(32000)
551
- pb.F_Fixed64Defaulted = Uint64(666)
552
- pb.F_Uint32Defaulted = Uint32(323232)
553
- pb.F_Uint64Defaulted = nil
554
- pb.F_FloatDefaulted = nil
555
- pb.F_DoubleDefaulted = Float64(0)
556
- pb.F_StringDefaulted = String("gotcha")
557
- pb.F_BytesDefaulted = []byte("asdfasdf")
558
- pb.F_Sint32Defaulted = Int32(123)
559
- pb.F_Sint64Defaulted = Int64(789)
560
- pb.Reset()
561
- checkInitialized(pb, t)
562
-}
563
-
564
-// All required fields set, no defaults provided.
565
-func TestEncodeDecode1(t *testing.T) {
566
- pb := initGoTest(false)
567
- overify(t, pb,
568
- "0807"+ // field 1, encoding 0, value 7
569
- "220d"+"0a056c6162656c120474797065"+ // field 4, encoding 2 (GoTestField)
570
- "5001"+ // field 10, encoding 0, value 1
571
- "5803"+ // field 11, encoding 0, value 3
572
- "6006"+ // field 12, encoding 0, value 6
573
- "6d20000000"+ // field 13, encoding 5, value 0x20
574
- "714000000000000000"+ // field 14, encoding 1, value 0x40
575
- "78a019"+ // field 15, encoding 0, value 0xca0 = 3232
576
- "8001c032"+ // field 16, encoding 0, value 0x1940 = 6464
577
- "8d0100004a45"+ // field 17, encoding 5, value 3232.0
578
- "9101000000000040b940"+ // field 18, encoding 1, value 6464.0
579
- "9a0106"+"737472696e67"+ // field 19, encoding 2, string "string"
580
- "b304"+ // field 70, encoding 3, start group
581
- "ba0408"+"7265717569726564"+ // field 71, encoding 2, string "required"
582
- "b404"+ // field 70, encoding 4, end group
583
- "aa0605"+"6279746573"+ // field 101, encoding 2, string "bytes"
584
- "b0063f"+ // field 102, encoding 0, 0x3f zigzag32
585
- "b8067f") // field 103, encoding 0, 0x7f zigzag64
586
-}
587
-
588
-// All required fields set, defaults provided.
589
-func TestEncodeDecode2(t *testing.T) {
590
- pb := initGoTest(true)
591
- overify(t, pb,
592
- "0807"+ // field 1, encoding 0, value 7
593
- "220d"+"0a056c6162656c120474797065"+ // field 4, encoding 2 (GoTestField)
594
- "5001"+ // field 10, encoding 0, value 1
595
- "5803"+ // field 11, encoding 0, value 3
596
- "6006"+ // field 12, encoding 0, value 6
597
- "6d20000000"+ // field 13, encoding 5, value 32
598
- "714000000000000000"+ // field 14, encoding 1, value 64
599
- "78a019"+ // field 15, encoding 0, value 3232
600
- "8001c032"+ // field 16, encoding 0, value 6464
601
- "8d0100004a45"+ // field 17, encoding 5, value 3232.0
602
- "9101000000000040b940"+ // field 18, encoding 1, value 6464.0
603
- "9a0106"+"737472696e67"+ // field 19, encoding 2 string "string"
604
- "c00201"+ // field 40, encoding 0, value 1
605
- "c80220"+ // field 41, encoding 0, value 32
606
- "d00240"+ // field 42, encoding 0, value 64
607
- "dd0240010000"+ // field 43, encoding 5, value 320
608
- "e1028002000000000000"+ // field 44, encoding 1, value 640
609
- "e8028019"+ // field 45, encoding 0, value 3200
610
- "f0028032"+ // field 46, encoding 0, value 6400
611
- "fd02e0659948"+ // field 47, encoding 5, value 314159.0
612
- "81030000000050971041"+ // field 48, encoding 1, value 271828.0
613
- "8a0310"+"68656c6c6f2c2022776f726c6421220a"+ // field 49, encoding 2 string "hello, \"world!\"\n"
614
- "b304"+ // start group field 70 level 1
615
- "ba0408"+"7265717569726564"+ // field 71, encoding 2, string "required"
616
- "b404"+ // end group field 70 level 1
617
- "aa0605"+"6279746573"+ // field 101, encoding 2 string "bytes"
618
- "b0063f"+ // field 102, encoding 0, 0x3f zigzag32
619
- "b8067f"+ // field 103, encoding 0, 0x7f zigzag64
620
- "8a1907"+"4269676e6f7365"+ // field 401, encoding 2, string "Bignose"
621
- "90193f"+ // field 402, encoding 0, value 63
622
- "98197f") // field 403, encoding 0, value 127
623
-
624
-}
625
-
626
-// All default fields set to their default value by hand
627
-func TestEncodeDecode3(t *testing.T) {
628
- pb := initGoTest(false)
629
- pb.F_BoolDefaulted = Bool(true)
630
- pb.F_Int32Defaulted = Int32(32)
631
- pb.F_Int64Defaulted = Int64(64)
632
- pb.F_Fixed32Defaulted = Uint32(320)
633
- pb.F_Fixed64Defaulted = Uint64(640)
634
- pb.F_Uint32Defaulted = Uint32(3200)
635
- pb.F_Uint64Defaulted = Uint64(6400)
636
- pb.F_FloatDefaulted = Float32(314159)
637
- pb.F_DoubleDefaulted = Float64(271828)
638
- pb.F_StringDefaulted = String("hello, \"world!\"\n")
639
- pb.F_BytesDefaulted = []byte("Bignose")
640
- pb.F_Sint32Defaulted = Int32(-32)
641
- pb.F_Sint64Defaulted = Int64(-64)
642
-
643
- overify(t, pb,
644
- "0807"+ // field 1, encoding 0, value 7
645
- "220d"+"0a056c6162656c120474797065"+ // field 4, encoding 2 (GoTestField)
646
- "5001"+ // field 10, encoding 0, value 1
647
- "5803"+ // field 11, encoding 0, value 3
648
- "6006"+ // field 12, encoding 0, value 6
649
- "6d20000000"+ // field 13, encoding 5, value 32
650
- "714000000000000000"+ // field 14, encoding 1, value 64
651
- "78a019"+ // field 15, encoding 0, value 3232
652
- "8001c032"+ // field 16, encoding 0, value 6464
653
- "8d0100004a45"+ // field 17, encoding 5, value 3232.0
654
- "9101000000000040b940"+ // field 18, encoding 1, value 6464.0
655
- "9a0106"+"737472696e67"+ // field 19, encoding 2 string "string"
656
- "c00201"+ // field 40, encoding 0, value 1
657
- "c80220"+ // field 41, encoding 0, value 32
658
- "d00240"+ // field 42, encoding 0, value 64
659
- "dd0240010000"+ // field 43, encoding 5, value 320
660
- "e1028002000000000000"+ // field 44, encoding 1, value 640
661
- "e8028019"+ // field 45, encoding 0, value 3200
662
- "f0028032"+ // field 46, encoding 0, value 6400
663
- "fd02e0659948"+ // field 47, encoding 5, value 314159.0
664
- "81030000000050971041"+ // field 48, encoding 1, value 271828.0
665
- "8a0310"+"68656c6c6f2c2022776f726c6421220a"+ // field 49, encoding 2 string "hello, \"world!\"\n"
666
- "b304"+ // start group field 70 level 1
667
- "ba0408"+"7265717569726564"+ // field 71, encoding 2, string "required"
668
- "b404"+ // end group field 70 level 1
669
- "aa0605"+"6279746573"+ // field 101, encoding 2 string "bytes"
670
- "b0063f"+ // field 102, encoding 0, 0x3f zigzag32
671
- "b8067f"+ // field 103, encoding 0, 0x7f zigzag64
672
- "8a1907"+"4269676e6f7365"+ // field 401, encoding 2, string "Bignose"
673
- "90193f"+ // field 402, encoding 0, value 63
674
- "98197f") // field 403, encoding 0, value 127
675
-
676
-}
677
-
678
-// All required fields set, defaults provided, all non-defaulted optional fields have values.
679
-func TestEncodeDecode4(t *testing.T) {
680
- pb := initGoTest(true)
681
- pb.Table = String("hello")
682
- pb.Param = Int32(7)
683
- pb.OptionalField = initGoTestField()
684
- pb.F_BoolOptional = Bool(true)
685
- pb.F_Int32Optional = Int32(32)
686
- pb.F_Int64Optional = Int64(64)
687
- pb.F_Fixed32Optional = Uint32(3232)
688
- pb.F_Fixed64Optional = Uint64(6464)
689
- pb.F_Uint32Optional = Uint32(323232)
690
- pb.F_Uint64Optional = Uint64(646464)
691
- pb.F_FloatOptional = Float32(32.)
692
- pb.F_DoubleOptional = Float64(64.)
693
- pb.F_StringOptional = String("hello")
694
- pb.F_BytesOptional = []byte("Bignose")
695
- pb.F_Sint32Optional = Int32(-32)
696
- pb.F_Sint64Optional = Int64(-64)
697
- pb.Optionalgroup = initGoTest_OptionalGroup()
698
-
699
- overify(t, pb,
700
- "0807"+ // field 1, encoding 0, value 7
701
- "1205"+"68656c6c6f"+ // field 2, encoding 2, string "hello"
702
- "1807"+ // field 3, encoding 0, value 7
703
- "220d"+"0a056c6162656c120474797065"+ // field 4, encoding 2 (GoTestField)
704
- "320d"+"0a056c6162656c120474797065"+ // field 6, encoding 2 (GoTestField)
705
- "5001"+ // field 10, encoding 0, value 1
706
- "5803"+ // field 11, encoding 0, value 3
707
- "6006"+ // field 12, encoding 0, value 6
708
- "6d20000000"+ // field 13, encoding 5, value 32
709
- "714000000000000000"+ // field 14, encoding 1, value 64
710
- "78a019"+ // field 15, encoding 0, value 3232
711
- "8001c032"+ // field 16, encoding 0, value 6464
712
- "8d0100004a45"+ // field 17, encoding 5, value 3232.0
713
- "9101000000000040b940"+ // field 18, encoding 1, value 6464.0
714
- "9a0106"+"737472696e67"+ // field 19, encoding 2 string "string"
715
- "f00101"+ // field 30, encoding 0, value 1
716
- "f80120"+ // field 31, encoding 0, value 32
717
- "800240"+ // field 32, encoding 0, value 64
718
- "8d02a00c0000"+ // field 33, encoding 5, value 3232
719
- "91024019000000000000"+ // field 34, encoding 1, value 6464
720
- "9802a0dd13"+ // field 35, encoding 0, value 323232
721
- "a002c0ba27"+ // field 36, encoding 0, value 646464
722
- "ad0200000042"+ // field 37, encoding 5, value 32.0
723
- "b1020000000000005040"+ // field 38, encoding 1, value 64.0
724
- "ba0205"+"68656c6c6f"+ // field 39, encoding 2, string "hello"
725
- "c00201"+ // field 40, encoding 0, value 1
726
- "c80220"+ // field 41, encoding 0, value 32
727
- "d00240"+ // field 42, encoding 0, value 64
728
- "dd0240010000"+ // field 43, encoding 5, value 320
729
- "e1028002000000000000"+ // field 44, encoding 1, value 640
730
- "e8028019"+ // field 45, encoding 0, value 3200
731
- "f0028032"+ // field 46, encoding 0, value 6400
732
- "fd02e0659948"+ // field 47, encoding 5, value 314159.0
733
- "81030000000050971041"+ // field 48, encoding 1, value 271828.0
734
- "8a0310"+"68656c6c6f2c2022776f726c6421220a"+ // field 49, encoding 2 string "hello, \"world!\"\n"
735
- "b304"+ // start group field 70 level 1
736
- "ba0408"+"7265717569726564"+ // field 71, encoding 2, string "required"
737
- "b404"+ // end group field 70 level 1
738
- "d305"+ // start group field 90 level 1
739
- "da0508"+"6f7074696f6e616c"+ // field 91, encoding 2, string "optional"
740
- "d405"+ // end group field 90 level 1
741
- "aa0605"+"6279746573"+ // field 101, encoding 2 string "bytes"
742
- "b0063f"+ // field 102, encoding 0, 0x3f zigzag32
743
- "b8067f"+ // field 103, encoding 0, 0x7f zigzag64
744
- "ea1207"+"4269676e6f7365"+ // field 301, encoding 2, string "Bignose"
745
- "f0123f"+ // field 302, encoding 0, value 63
746
- "f8127f"+ // field 303, encoding 0, value 127
747
- "8a1907"+"4269676e6f7365"+ // field 401, encoding 2, string "Bignose"
748
- "90193f"+ // field 402, encoding 0, value 63
749
- "98197f") // field 403, encoding 0, value 127
750
-
751
-}
752
-
753
-// All required fields set, defaults provided, all repeated fields given two values.
754
-func TestEncodeDecode5(t *testing.T) {
755
- pb := initGoTest(true)
756
- pb.RepeatedField = []*GoTestField{initGoTestField(), initGoTestField()}
757
- pb.F_BoolRepeated = []bool{false, true}
758
- pb.F_Int32Repeated = []int32{32, 33}
759
- pb.F_Int64Repeated = []int64{64, 65}
760
- pb.F_Fixed32Repeated = []uint32{3232, 3333}
761
- pb.F_Fixed64Repeated = []uint64{6464, 6565}
762
- pb.F_Uint32Repeated = []uint32{323232, 333333}
763
- pb.F_Uint64Repeated = []uint64{646464, 656565}
764
- pb.F_FloatRepeated = []float32{32., 33.}
765
- pb.F_DoubleRepeated = []float64{64., 65.}
766
- pb.F_StringRepeated = []string{"hello", "sailor"}
767
- pb.F_BytesRepeated = [][]byte{[]byte("big"), []byte("nose")}
768
- pb.F_Sint32Repeated = []int32{32, -32}
769
- pb.F_Sint64Repeated = []int64{64, -64}
770
- pb.Repeatedgroup = []*GoTest_RepeatedGroup{initGoTest_RepeatedGroup(), initGoTest_RepeatedGroup()}
771
-
772
- overify(t, pb,
773
- "0807"+ // field 1, encoding 0, value 7
774
- "220d"+"0a056c6162656c120474797065"+ // field 4, encoding 2 (GoTestField)
775
- "2a0d"+"0a056c6162656c120474797065"+ // field 5, encoding 2 (GoTestField)
776
- "2a0d"+"0a056c6162656c120474797065"+ // field 5, encoding 2 (GoTestField)
777
- "5001"+ // field 10, encoding 0, value 1
778
- "5803"+ // field 11, encoding 0, value 3
779
- "6006"+ // field 12, encoding 0, value 6
780
- "6d20000000"+ // field 13, encoding 5, value 32
781
- "714000000000000000"+ // field 14, encoding 1, value 64
782
- "78a019"+ // field 15, encoding 0, value 3232
783
- "8001c032"+ // field 16, encoding 0, value 6464
784
- "8d0100004a45"+ // field 17, encoding 5, value 3232.0
785
- "9101000000000040b940"+ // field 18, encoding 1, value 6464.0
786
- "9a0106"+"737472696e67"+ // field 19, encoding 2 string "string"
787
- "a00100"+ // field 20, encoding 0, value 0
788
- "a00101"+ // field 20, encoding 0, value 1
789
- "a80120"+ // field 21, encoding 0, value 32
790
- "a80121"+ // field 21, encoding 0, value 33
791
- "b00140"+ // field 22, encoding 0, value 64
792
- "b00141"+ // field 22, encoding 0, value 65
793
- "bd01a00c0000"+ // field 23, encoding 5, value 3232
794
- "bd01050d0000"+ // field 23, encoding 5, value 3333
795
- "c1014019000000000000"+ // field 24, encoding 1, value 6464
796
- "c101a519000000000000"+ // field 24, encoding 1, value 6565
797
- "c801a0dd13"+ // field 25, encoding 0, value 323232
798
- "c80195ac14"+ // field 25, encoding 0, value 333333
799
- "d001c0ba27"+ // field 26, encoding 0, value 646464
800
- "d001b58928"+ // field 26, encoding 0, value 656565
801
- "dd0100000042"+ // field 27, encoding 5, value 32.0
802
- "dd0100000442"+ // field 27, encoding 5, value 33.0
803
- "e1010000000000005040"+ // field 28, encoding 1, value 64.0
804
- "e1010000000000405040"+ // field 28, encoding 1, value 65.0
805
- "ea0105"+"68656c6c6f"+ // field 29, encoding 2, string "hello"
806
- "ea0106"+"7361696c6f72"+ // field 29, encoding 2, string "sailor"
807
- "c00201"+ // field 40, encoding 0, value 1
808
- "c80220"+ // field 41, encoding 0, value 32
809
- "d00240"+ // field 42, encoding 0, value 64
810
- "dd0240010000"+ // field 43, encoding 5, value 320
811
- "e1028002000000000000"+ // field 44, encoding 1, value 640
812
- "e8028019"+ // field 45, encoding 0, value 3200
813
- "f0028032"+ // field 46, encoding 0, value 6400
814
- "fd02e0659948"+ // field 47, encoding 5, value 314159.0
815
- "81030000000050971041"+ // field 48, encoding 1, value 271828.0
816
- "8a0310"+"68656c6c6f2c2022776f726c6421220a"+ // field 49, encoding 2 string "hello, \"world!\"\n"
817
- "b304"+ // start group field 70 level 1
818
- "ba0408"+"7265717569726564"+ // field 71, encoding 2, string "required"
819
- "b404"+ // end group field 70 level 1
820
- "8305"+ // start group field 80 level 1
821
- "8a0508"+"7265706561746564"+ // field 81, encoding 2, string "repeated"
822
- "8405"+ // end group field 80 level 1
823
- "8305"+ // start group field 80 level 1
824
- "8a0508"+"7265706561746564"+ // field 81, encoding 2, string "repeated"
825
- "8405"+ // end group field 80 level 1
826
- "aa0605"+"6279746573"+ // field 101, encoding 2 string "bytes"
827
- "b0063f"+ // field 102, encoding 0, 0x3f zigzag32
828
- "b8067f"+ // field 103, encoding 0, 0x7f zigzag64
829
- "ca0c03"+"626967"+ // field 201, encoding 2, string "big"
830
- "ca0c04"+"6e6f7365"+ // field 201, encoding 2, string "nose"
831
- "d00c40"+ // field 202, encoding 0, value 32
832
- "d00c3f"+ // field 202, encoding 0, value -32
833
- "d80c8001"+ // field 203, encoding 0, value 64
834
- "d80c7f"+ // field 203, encoding 0, value -64
835
- "8a1907"+"4269676e6f7365"+ // field 401, encoding 2, string "Bignose"
836
- "90193f"+ // field 402, encoding 0, value 63
837
- "98197f") // field 403, encoding 0, value 127
838
-
839
-}
840
-
841
-// All required fields set, all packed repeated fields given two values.
842
-func TestEncodeDecode6(t *testing.T) {
843
- pb := initGoTest(false)
844
- pb.F_BoolRepeatedPacked = []bool{false, true}
845
- pb.F_Int32RepeatedPacked = []int32{32, 33}
846
- pb.F_Int64RepeatedPacked = []int64{64, 65}
847
- pb.F_Fixed32RepeatedPacked = []uint32{3232, 3333}
848
- pb.F_Fixed64RepeatedPacked = []uint64{6464, 6565}
849
- pb.F_Uint32RepeatedPacked = []uint32{323232, 333333}
850
- pb.F_Uint64RepeatedPacked = []uint64{646464, 656565}
851
- pb.F_FloatRepeatedPacked = []float32{32., 33.}
852
- pb.F_DoubleRepeatedPacked = []float64{64., 65.}
853
- pb.F_Sint32RepeatedPacked = []int32{32, -32}
854
- pb.F_Sint64RepeatedPacked = []int64{64, -64}
855
-
856
- overify(t, pb,
857
- "0807"+ // field 1, encoding 0, value 7
858
- "220d"+"0a056c6162656c120474797065"+ // field 4, encoding 2 (GoTestField)
859
- "5001"+ // field 10, encoding 0, value 1
860
- "5803"+ // field 11, encoding 0, value 3
861
- "6006"+ // field 12, encoding 0, value 6
862
- "6d20000000"+ // field 13, encoding 5, value 32
863
- "714000000000000000"+ // field 14, encoding 1, value 64
864
- "78a019"+ // field 15, encoding 0, value 3232
865
- "8001c032"+ // field 16, encoding 0, value 6464
866
- "8d0100004a45"+ // field 17, encoding 5, value 3232.0
867
- "9101000000000040b940"+ // field 18, encoding 1, value 6464.0
868
- "9a0106"+"737472696e67"+ // field 19, encoding 2 string "string"
869
- "9203020001"+ // field 50, encoding 2, 2 bytes, value 0, value 1
870
- "9a03022021"+ // field 51, encoding 2, 2 bytes, value 32, value 33
871
- "a203024041"+ // field 52, encoding 2, 2 bytes, value 64, value 65
872
- "aa0308"+ // field 53, encoding 2, 8 bytes
873
- "a00c0000050d0000"+ // value 3232, value 3333
874
- "b20310"+ // field 54, encoding 2, 16 bytes
875
- "4019000000000000a519000000000000"+ // value 6464, value 6565
876
- "ba0306"+ // field 55, encoding 2, 6 bytes
877
- "a0dd1395ac14"+ // value 323232, value 333333
878
- "c20306"+ // field 56, encoding 2, 6 bytes
879
- "c0ba27b58928"+ // value 646464, value 656565
880
- "ca0308"+ // field 57, encoding 2, 8 bytes
881
- "0000004200000442"+ // value 32.0, value 33.0
882
- "d20310"+ // field 58, encoding 2, 16 bytes
883
- "00000000000050400000000000405040"+ // value 64.0, value 65.0
884
- "b304"+ // start group field 70 level 1
885
- "ba0408"+"7265717569726564"+ // field 71, encoding 2, string "required"
886
- "b404"+ // end group field 70 level 1
887
- "aa0605"+"6279746573"+ // field 101, encoding 2 string "bytes"
888
- "b0063f"+ // field 102, encoding 0, 0x3f zigzag32
889
- "b8067f"+ // field 103, encoding 0, 0x7f zigzag64
890
- "b21f02"+ // field 502, encoding 2, 2 bytes
891
- "403f"+ // value 32, value -32
892
- "ba1f03"+ // field 503, encoding 2, 3 bytes
893
- "80017f") // value 64, value -64
894
-}
895
-
896
-// Test that we can encode empty bytes fields.
897
-func TestEncodeDecodeBytes1(t *testing.T) {
898
- pb := initGoTest(false)
899
-
900
- // Create our bytes
901
- pb.F_BytesRequired = []byte{}
902
- pb.F_BytesRepeated = [][]byte{{}}
903
- pb.F_BytesOptional = []byte{}
904
-
905
- d, err := Marshal(pb)
906
- if err != nil {
907
- t.Error(err)
908
- }
909
-
910
- pbd := new(GoTest)
911
- if err := Unmarshal(d, pbd); err != nil {
912
- t.Error(err)
913
- }
914
-
915
- if pbd.F_BytesRequired == nil || len(pbd.F_BytesRequired) != 0 {
916
- t.Error("required empty bytes field is incorrect")
917
- }
918
- if pbd.F_BytesRepeated == nil || len(pbd.F_BytesRepeated) == 1 && pbd.F_BytesRepeated[0] == nil {
919
- t.Error("repeated empty bytes field is incorrect")
920
- }
921
- if pbd.F_BytesOptional == nil || len(pbd.F_BytesOptional) != 0 {
922
- t.Error("optional empty bytes field is incorrect")
923
- }
924
-}
925
-
926
-// Test that we encode nil-valued fields of a repeated bytes field correctly.
927
-// Since entries in a repeated field cannot be nil, nil must mean empty value.
928
-func TestEncodeDecodeBytes2(t *testing.T) {
929
- pb := initGoTest(false)
930
-
931
- // Create our bytes
932
- pb.F_BytesRepeated = [][]byte{nil}
933
-
934
- d, err := Marshal(pb)
935
- if err != nil {
936
- t.Error(err)
937
- }
938
-
939
- pbd := new(GoTest)
940
- if err := Unmarshal(d, pbd); err != nil {
941
- t.Error(err)
942
- }
943
-
944
- if len(pbd.F_BytesRepeated) != 1 || pbd.F_BytesRepeated[0] == nil {
945
- t.Error("Unexpected value for repeated bytes field")
946
- }
947
-}
948
-
949
-// All required fields set, defaults provided, all repeated fields given two values.
950
-func TestSkippingUnrecognizedFields(t *testing.T) {
951
- o := old()
952
- pb := initGoTestField()
953
-
954
- // Marshal it normally.
955
- o.Marshal(pb)
956
-
957
- // Now new a GoSkipTest record.
958
- skip := &GoSkipTest{
959
- SkipInt32: Int32(32),
960
- SkipFixed32: Uint32(3232),
961
- SkipFixed64: Uint64(6464),
962
- SkipString: String("skipper"),
963
- Skipgroup: &GoSkipTest_SkipGroup{
964
- GroupInt32: Int32(75),
965
- GroupString: String("wxyz"),
966
- },
967
- }
968
-
969
- // Marshal it into same buffer.
970
- o.Marshal(skip)
971
-
972
- pbd := new(GoTestField)
973
- o.Unmarshal(pbd)
974
-
975
- // The __unrecognized field should be a marshaling of GoSkipTest
976
- skipd := new(GoSkipTest)
977
-
978
- o.SetBuf(pbd.XXX_unrecognized)
979
- o.Unmarshal(skipd)
980
-
981
- if *skipd.SkipInt32 != *skip.SkipInt32 {
982
- t.Error("skip int32", skipd.SkipInt32)
983
- }
984
- if *skipd.SkipFixed32 != *skip.SkipFixed32 {
985
- t.Error("skip fixed32", skipd.SkipFixed32)
986
- }
987
- if *skipd.SkipFixed64 != *skip.SkipFixed64 {
988
- t.Error("skip fixed64", skipd.SkipFixed64)
989
- }
990
- if *skipd.SkipString != *skip.SkipString {
991
- t.Error("skip string", *skipd.SkipString)
992
- }
993
- if *skipd.Skipgroup.GroupInt32 != *skip.Skipgroup.GroupInt32 {
994
- t.Error("skip group int32", skipd.Skipgroup.GroupInt32)
995
- }
996
- if *skipd.Skipgroup.GroupString != *skip.Skipgroup.GroupString {
997
- t.Error("skip group string", *skipd.Skipgroup.GroupString)
998
- }
999
-}
1000
-
1001
-// Check that unrecognized fields of a submessage are preserved.
1002
-func TestSubmessageUnrecognizedFields(t *testing.T) {
1003
- nm := &NewMessage{
1004
- Nested: &NewMessage_Nested{
1005
- Name: String("Nigel"),
1006
- FoodGroup: String("carbs"),
1007
- },
1008
- }
1009
- b, err := Marshal(nm)
1010
- if err != nil {
1011
- t.Fatalf("Marshal of NewMessage: %v", err)
1012
- }
1013
-
1014
- // Unmarshal into an OldMessage.
1015
- om := new(OldMessage)
1016
- if err := Unmarshal(b, om); err != nil {
1017
- t.Fatalf("Unmarshal to OldMessage: %v", err)
1018
- }
1019
- exp := &OldMessage{
1020
- Nested: &OldMessage_Nested{
1021
- Name: String("Nigel"),
1022
- // normal protocol buffer users should not do this
1023
- XXX_unrecognized: []byte("\x12\x05carbs"),
1024
- },
1025
- }
1026
- if !Equal(om, exp) {
1027
- t.Errorf("om = %v, want %v", om, exp)
1028
- }
1029
-
1030
- // Clone the OldMessage.
1031
- om = Clone(om).(*OldMessage)
1032
- if !Equal(om, exp) {
1033
- t.Errorf("Clone(om) = %v, want %v", om, exp)
1034
- }
1035
-
1036
- // Marshal the OldMessage, then unmarshal it into an empty NewMessage.
1037
- if b, err = Marshal(om); err != nil {
1038
- t.Fatalf("Marshal of OldMessage: %v", err)
1039
- }
1040
- t.Logf("Marshal(%v) -> %q", om, b)
1041
- nm2 := new(NewMessage)
1042
- if err := Unmarshal(b, nm2); err != nil {
1043
- t.Fatalf("Unmarshal to NewMessage: %v", err)
1044
- }
1045
- if !Equal(nm, nm2) {
1046
- t.Errorf("NewMessage round-trip: %v => %v", nm, nm2)
1047
- }
1048
-}
1049
-
1050
-// Check that an int32 field can be upgraded to an int64 field.
1051
-func TestNegativeInt32(t *testing.T) {
1052
- om := &OldMessage{
1053
- Num: Int32(-1),
1054
- }
1055
- b, err := Marshal(om)
1056
- if err != nil {
1057
- t.Fatalf("Marshal of OldMessage: %v", err)
1058
- }
1059
-
1060
- // Check the size. It should be 11 bytes;
1061
- // 1 for the field/wire type, and 10 for the negative number.
1062
- if len(b) != 11 {
1063
- t.Errorf("%v marshaled as %q, wanted 11 bytes", om, b)
1064
- }
1065
-
1066
- // Unmarshal into a NewMessage.
1067
- nm := new(NewMessage)
1068
- if err := Unmarshal(b, nm); err != nil {
1069
- t.Fatalf("Unmarshal to NewMessage: %v", err)
1070
- }
1071
- want := &NewMessage{
1072
- Num: Int64(-1),
1073
- }
1074
- if !Equal(nm, want) {
1075
- t.Errorf("nm = %v, want %v", nm, want)
1076
- }
1077
-}
1078
-
1079
-// Check that we can grow an array (repeated field) to have many elements.
1080
-// This test doesn't depend only on our encoding; for variety, it makes sure
1081
-// we create, encode, and decode the correct contents explicitly. It's therefore
1082
-// a bit messier.
1083
-// This test also uses (and hence tests) the Marshal/Unmarshal functions
1084
-// instead of the methods.
1085
-func TestBigRepeated(t *testing.T) {
1086
- pb := initGoTest(true)
1087
-
1088
- // Create the arrays
1089
- const N = 50 // Internally the library starts much smaller.
1090
- pb.Repeatedgroup = make([]*GoTest_RepeatedGroup, N)
1091
- pb.F_Sint64Repeated = make([]int64, N)
1092
- pb.F_Sint32Repeated = make([]int32, N)
1093
- pb.F_BytesRepeated = make([][]byte, N)
1094
- pb.F_StringRepeated = make([]string, N)
1095
- pb.F_DoubleRepeated = make([]float64, N)
1096
- pb.F_FloatRepeated = make([]float32, N)
1097
- pb.F_Uint64Repeated = make([]uint64, N)
1098
- pb.F_Uint32Repeated = make([]uint32, N)
1099
- pb.F_Fixed64Repeated = make([]uint64, N)
1100
- pb.F_Fixed32Repeated = make([]uint32, N)
1101
- pb.F_Int64Repeated = make([]int64, N)
1102
- pb.F_Int32Repeated = make([]int32, N)
1103
- pb.F_BoolRepeated = make([]bool, N)
1104
- pb.RepeatedField = make([]*GoTestField, N)
1105
-
1106
- // Fill in the arrays with checkable values.
1107
- igtf := initGoTestField()
1108
- igtrg := initGoTest_RepeatedGroup()
1109
- for i := 0; i < N; i++ {
1110
- pb.Repeatedgroup[i] = igtrg
1111
- pb.F_Sint64Repeated[i] = int64(i)
1112
- pb.F_Sint32Repeated[i] = int32(i)
1113
- s := fmt.Sprint(i)
1114
- pb.F_BytesRepeated[i] = []byte(s)
1115
- pb.F_StringRepeated[i] = s
1116
- pb.F_DoubleRepeated[i] = float64(i)
1117
- pb.F_FloatRepeated[i] = float32(i)
1118
- pb.F_Uint64Repeated[i] = uint64(i)
1119
- pb.F_Uint32Repeated[i] = uint32(i)
1120
- pb.F_Fixed64Repeated[i] = uint64(i)
1121
- pb.F_Fixed32Repeated[i] = uint32(i)
1122
- pb.F_Int64Repeated[i] = int64(i)
1123
- pb.F_Int32Repeated[i] = int32(i)
1124
- pb.F_BoolRepeated[i] = i%2 == 0
1125
- pb.RepeatedField[i] = igtf
1126
- }
1127
-
1128
- // Marshal.
1129
- buf, _ := Marshal(pb)
1130
-
1131
- // Now test Unmarshal by recreating the original buffer.
1132
- pbd := new(GoTest)
1133
- Unmarshal(buf, pbd)
1134
-
1135
- // Check the checkable values
1136
- for i := uint64(0); i < N; i++ {
1137
- if pbd.Repeatedgroup[i] == nil { // TODO: more checking?
1138
- t.Error("pbd.Repeatedgroup bad")
1139
- }
1140
- var x uint64
1141
- x = uint64(pbd.F_Sint64Repeated[i])
1142
- if x != i {
1143
- t.Error("pbd.F_Sint64Repeated bad", x, i)
1144
- }
1145
- x = uint64(pbd.F_Sint32Repeated[i])
1146
- if x != i {
1147
- t.Error("pbd.F_Sint32Repeated bad", x, i)
1148
- }
1149
- s := fmt.Sprint(i)
1150
- equalbytes(pbd.F_BytesRepeated[i], []byte(s), t)
1151
- if pbd.F_StringRepeated[i] != s {
1152
- t.Error("pbd.F_Sint32Repeated bad", pbd.F_StringRepeated[i], i)
1153
- }
1154
- x = uint64(pbd.F_DoubleRepeated[i])
1155
- if x != i {
1156
- t.Error("pbd.F_DoubleRepeated bad", x, i)
1157
- }
1158
- x = uint64(pbd.F_FloatRepeated[i])
1159
- if x != i {
1160
- t.Error("pbd.F_FloatRepeated bad", x, i)
1161
- }
1162
- x = pbd.F_Uint64Repeated[i]
1163
- if x != i {
1164
- t.Error("pbd.F_Uint64Repeated bad", x, i)
1165
- }
1166
- x = uint64(pbd.F_Uint32Repeated[i])
1167
- if x != i {
1168
- t.Error("pbd.F_Uint32Repeated bad", x, i)
1169
- }
1170
- x = pbd.F_Fixed64Repeated[i]
1171
- if x != i {
1172
- t.Error("pbd.F_Fixed64Repeated bad", x, i)
1173
- }
1174
- x = uint64(pbd.F_Fixed32Repeated[i])
1175
- if x != i {
1176
- t.Error("pbd.F_Fixed32Repeated bad", x, i)
1177
- }
1178
- x = uint64(pbd.F_Int64Repeated[i])
1179
- if x != i {
1180
- t.Error("pbd.F_Int64Repeated bad", x, i)
1181
- }
1182
- x = uint64(pbd.F_Int32Repeated[i])
1183
- if x != i {
1184
- t.Error("pbd.F_Int32Repeated bad", x, i)
1185
- }
1186
- if pbd.F_BoolRepeated[i] != (i%2 == 0) {
1187
- t.Error("pbd.F_BoolRepeated bad", x, i)
1188
- }
1189
- if pbd.RepeatedField[i] == nil { // TODO: more checking?
1190
- t.Error("pbd.RepeatedField bad")
1191
- }
1192
- }
1193
-}
1194
-
1195
-// Verify we give a useful message when decoding to the wrong structure type.
1196
-func TestTypeMismatch(t *testing.T) {
1197
- pb1 := initGoTest(true)
1198
-
1199
- // Marshal
1200
- o := old()
1201
- o.Marshal(pb1)
1202
-
1203
- // Now Unmarshal it to the wrong type.
1204
- pb2 := initGoTestField()
1205
- err := o.Unmarshal(pb2)
1206
- if err == nil {
1207
- t.Error("expected error, got no error")
1208
- } else if !strings.Contains(err.Error(), "bad wiretype") {
1209
- t.Error("expected bad wiretype error, got", err)
1210
- }
1211
-}
1212
-
1213
-func encodeDecode(t *testing.T, in, out Message, msg string) {
1214
- buf, err := Marshal(in)
1215
- if err != nil {
1216
- t.Fatalf("failed marshaling %v: %v", msg, err)
1217
- }
1218
- if err := Unmarshal(buf, out); err != nil {
1219
- t.Fatalf("failed unmarshaling %v: %v", msg, err)
1220
- }
1221
-}
1222
-
1223
-func TestPackedNonPackedDecoderSwitching(t *testing.T) {
1224
- np, p := new(NonPackedTest), new(PackedTest)
1225
-
1226
- // non-packed -> packed
1227
- np.A = []int32{0, 1, 1, 2, 3, 5}
1228
- encodeDecode(t, np, p, "non-packed -> packed")
1229
- if !reflect.DeepEqual(np.A, p.B) {
1230
- t.Errorf("failed non-packed -> packed; np.A=%+v, p.B=%+v", np.A, p.B)
1231
- }
1232
-
1233
- // packed -> non-packed
1234
- np.Reset()
1235
- p.B = []int32{3, 1, 4, 1, 5, 9}
1236
- encodeDecode(t, p, np, "packed -> non-packed")
1237
- if !reflect.DeepEqual(p.B, np.A) {
1238
- t.Errorf("failed packed -> non-packed; p.B=%+v, np.A=%+v", p.B, np.A)
1239
- }
1240
-}
1241
-
1242
-func TestProto1RepeatedGroup(t *testing.T) {
1243
- pb := &MessageList{
1244
- Message: []*MessageList_Message{
1245
- {
1246
- Name: String("blah"),
1247
- Count: Int32(7),
1248
- },
1249
- // NOTE: pb.Message[1] is a nil
1250
- nil,
1251
- },
1252
- }
1253
-
1254
- o := old()
1255
- err := o.Marshal(pb)
1256
- if err == nil || !strings.Contains(err.Error(), "repeated field Message has nil") {
1257
- t.Fatalf("unexpected or no error when marshaling: %v", err)
1258
- }
1259
-}
1260
-
1261
-// Test that enums work. Checks for a bug introduced by making enums
1262
-// named types instead of int32: newInt32FromUint64 would crash with
1263
-// a type mismatch in reflect.PointTo.
1264
-func TestEnum(t *testing.T) {
1265
- pb := new(GoEnum)
1266
- pb.Foo = FOO_FOO1.Enum()
1267
- o := old()
1268
- if err := o.Marshal(pb); err != nil {
1269
- t.Fatal("error encoding enum:", err)
1270
- }
1271
- pb1 := new(GoEnum)
1272
- if err := o.Unmarshal(pb1); err != nil {
1273
- t.Fatal("error decoding enum:", err)
1274
- }
1275
- if *pb1.Foo != FOO_FOO1 {
1276
- t.Error("expected 7 but got ", *pb1.Foo)
1277
- }
1278
-}
1279
-
1280
-// Enum types have String methods. Check that enum fields can be printed.
1281
-// We don't care what the value actually is, just as long as it doesn't crash.
1282
-func TestPrintingNilEnumFields(t *testing.T) {
1283
- pb := new(GoEnum)
1284
- fmt.Sprintf("%+v", pb)
1285
-}
1286
-
1287
-// Verify that absent required fields cause Marshal/Unmarshal to return errors.
1288
-func TestRequiredFieldEnforcement(t *testing.T) {
1289
- pb := new(GoTestField)
1290
- _, err := Marshal(pb)
1291
- if err == nil {
1292
- t.Error("marshal: expected error, got nil")
1293
- } else if strings.Index(err.Error(), "Label") < 0 {
1294
- t.Errorf("marshal: bad error type: %v", err)
1295
- }
1296
-
1297
- // A slightly sneaky, yet valid, proto. It encodes the same required field twice,
1298
- // so simply counting the required fields is insufficient.
1299
- // field 1, encoding 2, value "hi"
1300
- buf := []byte("\x0A\x02hi\x0A\x02hi")
1301
- err = Unmarshal(buf, pb)
1302
- if err == nil {
1303
- t.Error("unmarshal: expected error, got nil")
1304
- } else if strings.Index(err.Error(), "{Unknown}") < 0 {
1305
- t.Errorf("unmarshal: bad error type: %v", err)
1306
- }
1307
-}
1308
-
1309
-func TestTypedNilMarshal(t *testing.T) {
1310
- // A typed nil should return ErrNil and not crash.
1311
- _, err := Marshal((*GoEnum)(nil))
1312
- if err != ErrNil {
1313
- t.Errorf("Marshal: got err %v, want ErrNil", err)
1314
- }
1315
-}
1316
-
1317
-// A type that implements the Marshaler interface, but is not nillable.
1318
-type nonNillableInt uint64
1319
-
1320
-func (nni nonNillableInt) Marshal() ([]byte, error) {
1321
- return EncodeVarint(uint64(nni)), nil
1322
-}
1323
-
1324
-type NNIMessage struct {
1325
- nni nonNillableInt
1326
-}
1327
-
1328
-func (*NNIMessage) Reset() {}
1329
-func (*NNIMessage) String() string { return "" }
1330
-func (*NNIMessage) ProtoMessage() {}
1331
-
1332
-// A type that implements the Marshaler interface and is nillable.
1333
-type nillableMessage struct {
1334
- x uint64
1335
-}
1336
-
1337
-func (nm *nillableMessage) Marshal() ([]byte, error) {
1338
- return EncodeVarint(nm.x), nil
1339
-}
1340
-
1341
-type NMMessage struct {
1342
- nm *nillableMessage
1343
-}
1344
-
1345
-func (*NMMessage) Reset() {}
1346
-func (*NMMessage) String() string { return "" }
1347
-func (*NMMessage) ProtoMessage() {}
1348
-
1349
-// Verify a type that uses the Marshaler interface, but has a nil pointer.
1350
-func TestNilMarshaler(t *testing.T) {
1351
- // Try a struct with a Marshaler field that is nil.
1352
- // It should be directly marshable.
1353
- nmm := new(NMMessage)
1354
- if _, err := Marshal(nmm); err != nil {
1355
- t.Error("unexpected error marshaling nmm: ", err)
1356
- }
1357
-
1358
- // Try a struct with a Marshaler field that is not nillable.
1359
- nnim := new(NNIMessage)
1360
- nnim.nni = 7
1361
- var _ Marshaler = nnim.nni // verify it is truly a Marshaler
1362
- if _, err := Marshal(nnim); err != nil {
1363
- t.Error("unexpected error marshaling nnim: ", err)
1364
- }
1365
-}
1366
-
1367
-func TestAllSetDefaults(t *testing.T) {
1368
- // Exercise SetDefaults with all scalar field types.
1369
- m := &Defaults{
1370
- // NaN != NaN, so override that here.
1371
- F_Nan: Float32(1.7),
1372
- }
1373
- expected := &Defaults{
1374
- F_Bool: Bool(true),
1375
- F_Int32: Int32(32),
1376
- F_Int64: Int64(64),
1377
- F_Fixed32: Uint32(320),
1378
- F_Fixed64: Uint64(640),
1379
- F_Uint32: Uint32(3200),
1380
- F_Uint64: Uint64(6400),
1381
- F_Float: Float32(314159),
1382
- F_Double: Float64(271828),
1383
- F_String: String(`hello, "world!"` + "\n"),
1384
- F_Bytes: []byte("Bignose"),
1385
- F_Sint32: Int32(-32),
1386
- F_Sint64: Int64(-64),
1387
- F_Enum: Defaults_GREEN.Enum(),
1388
- F_Pinf: Float32(float32(math.Inf(1))),
1389
- F_Ninf: Float32(float32(math.Inf(-1))),
1390
- F_Nan: Float32(1.7),
1391
- StrZero: String(""),
1392
- }
1393
- SetDefaults(m)
1394
- if !Equal(m, expected) {
1395
- t.Errorf("SetDefaults failed\n got %v\nwant %v", m, expected)
1396
- }
1397
-}
1398
-
1399
-func TestSetDefaultsWithSetField(t *testing.T) {
1400
- // Check that a set value is not overridden.
1401
- m := &Defaults{
1402
- F_Int32: Int32(12),
1403
- }
1404
- SetDefaults(m)
1405
- if v := m.GetF_Int32(); v != 12 {
1406
- t.Errorf("m.FInt32 = %v, want 12", v)
1407
- }
1408
-}
1409
-
1410
-func TestSetDefaultsWithSubMessage(t *testing.T) {
1411
- m := &OtherMessage{
1412
- Key: Int64(123),
1413
- Inner: &InnerMessage{
1414
- Host: String("gopher"),
1415
- },
1416
- }
1417
- expected := &OtherMessage{
1418
- Key: Int64(123),
1419
- Inner: &InnerMessage{
1420
- Host: String("gopher"),
1421
- Port: Int32(4000),
1422
- },
1423
- }
1424
- SetDefaults(m)
1425
- if !Equal(m, expected) {
1426
- t.Errorf("\n got %v\nwant %v", m, expected)
1427
- }
1428
-}
1429
-
1430
-func TestSetDefaultsWithRepeatedSubMessage(t *testing.T) {
1431
- m := &MyMessage{
1432
- RepInner: []*InnerMessage{{}},
1433
- }
1434
- expected := &MyMessage{
1435
- RepInner: []*InnerMessage{{
1436
- Port: Int32(4000),
1437
- }},
1438
- }
1439
- SetDefaults(m)
1440
- if !Equal(m, expected) {
1441
- t.Errorf("\n got %v\nwant %v", m, expected)
1442
- }
1443
-}
1444
-
1445
-func TestSetDefaultWithRepeatedNonMessage(t *testing.T) {
1446
- m := &MyMessage{
1447
- Pet: []string{"turtle", "wombat"},
1448
- }
1449
- expected := Clone(m)
1450
- SetDefaults(m)
1451
- if !Equal(m, expected) {
1452
- t.Errorf("\n got %v\nwant %v", m, expected)
1453
- }
1454
-}
1455
-
1456
-func TestMaximumTagNumber(t *testing.T) {
1457
- m := &MaxTag{
1458
- LastField: String("natural goat essence"),
1459
- }
1460
- buf, err := Marshal(m)
1461
- if err != nil {
1462
- t.Fatalf("proto.Marshal failed: %v", err)
1463
- }
1464
- m2 := new(MaxTag)
1465
- if err := Unmarshal(buf, m2); err != nil {
1466
- t.Fatalf("proto.Unmarshal failed: %v", err)
1467
- }
1468
- if got, want := m2.GetLastField(), *m.LastField; got != want {
1469
- t.Errorf("got %q, want %q", got, want)
1470
- }
1471
-}
1472
-
1473
-func TestJSON(t *testing.T) {
1474
- m := &MyMessage{
1475
- Count: Int32(4),
1476
- Pet: []string{"bunny", "kitty"},
1477
- Inner: &InnerMessage{
1478
- Host: String("cauchy"),
1479
- },
1480
- Bikeshed: MyMessage_GREEN.Enum(),
1481
- }
1482
- const expected = `{"count":4,"pet":["bunny","kitty"],"inner":{"host":"cauchy"},"bikeshed":1}`
1483
-
1484
- b, err := json.Marshal(m)
1485
- if err != nil {
1486
- t.Fatalf("json.Marshal failed: %v", err)
1487
- }
1488
- s := string(b)
1489
- if s != expected {
1490
- t.Errorf("got %s\nwant %s", s, expected)
1491
- }
1492
-
1493
- received := new(MyMessage)
1494
- if err := json.Unmarshal(b, received); err != nil {
1495
- t.Fatalf("json.Unmarshal failed: %v", err)
1496
- }
1497
- if !Equal(received, m) {
1498
- t.Fatalf("got %s, want %s", received, m)
1499
- }
1500
-
1501
- // Test unmarshalling of JSON with symbolic enum name.
1502
- const old = `{"count":4,"pet":["bunny","kitty"],"inner":{"host":"cauchy"},"bikeshed":"GREEN"}`
1503
- received.Reset()
1504
- if err := json.Unmarshal([]byte(old), received); err != nil {
1505
- t.Fatalf("json.Unmarshal failed: %v", err)
1506
- }
1507
- if !Equal(received, m) {
1508
- t.Fatalf("got %s, want %s", received, m)
1509
- }
1510
-}
1511
-
1512
-func TestBadWireType(t *testing.T) {
1513
- b := []byte{7<<3 | 6} // field 7, wire type 6
1514
- pb := new(OtherMessage)
1515
- if err := Unmarshal(b, pb); err == nil {
1516
- t.Errorf("Unmarshal did not fail")
1517
- } else if !strings.Contains(err.Error(), "unknown wire type") {
1518
- t.Errorf("wrong error: %v", err)
1519
- }
1520
-}
1521
-
1522
-func TestBytesWithInvalidLength(t *testing.T) {
1523
- // If a byte sequence has an invalid (negative) length, Unmarshal should not panic.
1524
- b := []byte{2<<3 | WireBytes, 0xff, 0xff, 0xff, 0xff, 0xff, 0}
1525
- Unmarshal(b, new(MyMessage))
1526
-}
1527
-
1528
-func TestLengthOverflow(t *testing.T) {
1529
- // Overflowing a length should not panic.
1530
- b := []byte{2<<3 | WireBytes, 1, 1, 3<<3 | WireBytes, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, 0x01}
1531
- Unmarshal(b, new(MyMessage))
1532
-}
1533
-
1534
-func TestVarintOverflow(t *testing.T) {
1535
- // Overflowing a 64-bit length should not be allowed.
1536
- b := []byte{1<<3 | WireVarint, 0x01, 3<<3 | WireBytes, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x01}
1537
- if err := Unmarshal(b, new(MyMessage)); err == nil {
1538
- t.Fatalf("Overflowed uint64 length without error")
1539
- }
1540
-}
1541
-
1542
-func TestUnmarshalFuzz(t *testing.T) {
1543
- const N = 1000
1544
- seed := time.Now().UnixNano()
1545
- t.Logf("RNG seed is %d", seed)
1546
- rng := rand.New(rand.NewSource(seed))
1547
- buf := make([]byte, 20)
1548
- for i := 0; i < N; i++ {
1549
- for j := range buf {
1550
- buf[j] = byte(rng.Intn(256))
1551
- }
1552
- fuzzUnmarshal(t, buf)
1553
- }
1554
-}
1555
-
1556
-func TestMergeMessages(t *testing.T) {
1557
- pb := &MessageList{Message: []*MessageList_Message{{Name: String("x"), Count: Int32(1)}}}
1558
- data, err := Marshal(pb)
1559
- if err != nil {
1560
- t.Fatalf("Marshal: %v", err)
1561
- }
1562
-
1563
- pb1 := new(MessageList)
1564
- if err := Unmarshal(data, pb1); err != nil {
1565
- t.Fatalf("first Unmarshal: %v", err)
1566
- }
1567
- if err := Unmarshal(data, pb1); err != nil {
1568
- t.Fatalf("second Unmarshal: %v", err)
1569
- }
1570
- if len(pb1.Message) != 1 {
1571
- t.Errorf("two Unmarshals produced %d Messages, want 1", len(pb1.Message))
1572
- }
1573
-
1574
- pb2 := new(MessageList)
1575
- if err := UnmarshalMerge(data, pb2); err != nil {
1576
- t.Fatalf("first UnmarshalMerge: %v", err)
1577
- }
1578
- if err := UnmarshalMerge(data, pb2); err != nil {
1579
- t.Fatalf("second UnmarshalMerge: %v", err)
1580
- }
1581
- if len(pb2.Message) != 2 {
1582
- t.Errorf("two UnmarshalMerges produced %d Messages, want 2", len(pb2.Message))
1583
- }
1584
-}
1585
-
1586
-func TestExtensionMarshalOrder(t *testing.T) {
1587
- m := &MyMessage{Count: Int(123)}
1588
- if err := SetExtension(m, E_Ext_More, &Ext{Data: String("alpha")}); err != nil {
1589
- t.Fatalf("SetExtension: %v", err)
1590
- }
1591
- if err := SetExtension(m, E_Ext_Text, String("aleph")); err != nil {
1592
- t.Fatalf("SetExtension: %v", err)
1593
- }
1594
- if err := SetExtension(m, E_Ext_Number, Int32(1)); err != nil {
1595
- t.Fatalf("SetExtension: %v", err)
1596
- }
1597
-
1598
- // Serialize m several times, and check we get the same bytes each time.
1599
- var orig []byte
1600
- for i := 0; i < 100; i++ {
1601
- b, err := Marshal(m)
1602
- if err != nil {
1603
- t.Fatalf("Marshal: %v", err)
1604
- }
1605
- if i == 0 {
1606
- orig = b
1607
- continue
1608
- }
1609
- if !bytes.Equal(b, orig) {
1610
- t.Errorf("Bytes differ on attempt #%d", i)
1611
- }
1612
- }
1613
-}
1614
-
1615
-// Many extensions, because small maps might not iterate differently on each iteration.
1616
-var exts = []*ExtensionDesc{
1617
- E_X201,
1618
- E_X202,
1619
- E_X203,
1620
- E_X204,
1621
- E_X205,
1622
- E_X206,
1623
- E_X207,
1624
- E_X208,
1625
- E_X209,
1626
- E_X210,
1627
- E_X211,
1628
- E_X212,
1629
- E_X213,
1630
- E_X214,
1631
- E_X215,
1632
- E_X216,
1633
- E_X217,
1634
- E_X218,
1635
- E_X219,
1636
- E_X220,
1637
- E_X221,
1638
- E_X222,
1639
- E_X223,
1640
- E_X224,
1641
- E_X225,
1642
- E_X226,
1643
- E_X227,
1644
- E_X228,
1645
- E_X229,
1646
- E_X230,
1647
- E_X231,
1648
- E_X232,
1649
- E_X233,
1650
- E_X234,
1651
- E_X235,
1652
- E_X236,
1653
- E_X237,
1654
- E_X238,
1655
- E_X239,
1656
- E_X240,
1657
- E_X241,
1658
- E_X242,
1659
- E_X243,
1660
- E_X244,
1661
- E_X245,
1662
- E_X246,
1663
- E_X247,
1664
- E_X248,
1665
- E_X249,
1666
- E_X250,
1667
-}
1668
-
1669
-func TestMessageSetMarshalOrder(t *testing.T) {
1670
- m := &MyMessageSet{}
1671
- for _, x := range exts {
1672
- if err := SetExtension(m, x, &Empty{}); err != nil {
1673
- t.Fatalf("SetExtension: %v", err)
1674
- }
1675
- }
1676
-
1677
- buf, err := Marshal(m)
1678
- if err != nil {
1679
- t.Fatalf("Marshal: %v", err)
1680
- }
1681
-
1682
- // Serialize m several times, and check we get the same bytes each time.
1683
- for i := 0; i < 10; i++ {
1684
- b1, err := Marshal(m)
1685
- if err != nil {
1686
- t.Fatalf("Marshal: %v", err)
1687
- }
1688
- if !bytes.Equal(b1, buf) {
1689
- t.Errorf("Bytes differ on re-Marshal #%d", i)
1690
- }
1691
-
1692
- m2 := &MyMessageSet{}
1693
- if err := Unmarshal(buf, m2); err != nil {
1694
- t.Errorf("Unmarshal: %v", err)
1695
- }
1696
- b2, err := Marshal(m2)
1697
- if err != nil {
1698
- t.Errorf("re-Marshal: %v", err)
1699
- }
1700
- if !bytes.Equal(b2, buf) {
1701
- t.Errorf("Bytes differ on round-trip #%d", i)
1702
- }
1703
- }
1704
-}
1705
-
1706
-func TestUnmarshalMergesMessages(t *testing.T) {
1707
- // If a nested message occurs twice in the input,
1708
- // the fields should be merged when decoding.
1709
- a := &OtherMessage{
1710
- Key: Int64(123),
1711
- Inner: &InnerMessage{
1712
- Host: String("polhode"),
1713
- Port: Int32(1234),
1714
- },
1715
- }
1716
- aData, err := Marshal(a)
1717
- if err != nil {
1718
- t.Fatalf("Marshal(a): %v", err)
1719
- }
1720
- b := &OtherMessage{
1721
- Weight: Float32(1.2),
1722
- Inner: &InnerMessage{
1723
- Host: String("herpolhode"),
1724
- Connected: Bool(true),
1725
- },
1726
- }
1727
- bData, err := Marshal(b)
1728
- if err != nil {
1729
- t.Fatalf("Marshal(b): %v", err)
1730
- }
1731
- want := &OtherMessage{
1732
- Key: Int64(123),
1733
- Weight: Float32(1.2),
1734
- Inner: &InnerMessage{
1735
- Host: String("herpolhode"),
1736
- Port: Int32(1234),
1737
- Connected: Bool(true),
1738
- },
1739
- }
1740
- got := new(OtherMessage)
1741
- if err := Unmarshal(append(aData, bData...), got); err != nil {
1742
- t.Fatalf("Unmarshal: %v", err)
1743
- }
1744
- if !Equal(got, want) {
1745
- t.Errorf("\n got %v\nwant %v", got, want)
1746
- }
1747
-}
1748
-
1749
-func TestEncodingSizes(t *testing.T) {
1750
- tests := []struct {
1751
- m Message
1752
- n int
1753
- }{
1754
- {&Defaults{F_Int32: Int32(math.MaxInt32)}, 6},
1755
- {&Defaults{F_Int32: Int32(math.MinInt32)}, 11},
1756
- {&Defaults{F_Uint32: Uint32(uint32(math.MaxInt32) + 1)}, 6},
1757
- {&Defaults{F_Uint32: Uint32(math.MaxUint32)}, 6},
1758
- }
1759
- for _, test := range tests {
1760
- b, err := Marshal(test.m)
1761
- if err != nil {
1762
- t.Errorf("Marshal(%v): %v", test.m, err)
1763
- continue
1764
- }
1765
- if len(b) != test.n {
1766
- t.Errorf("Marshal(%v) yielded %d bytes, want %d bytes", test.m, len(b), test.n)
1767
- }
1768
- }
1769
-}
1770
-
1771
-func TestRequiredNotSetError(t *testing.T) {
1772
- pb := initGoTest(false)
1773
- pb.RequiredField.Label = nil
1774
- pb.F_Int32Required = nil
1775
- pb.F_Int64Required = nil
1776
-
1777
- expected := "0807" + // field 1, encoding 0, value 7
1778
- "2206" + "120474797065" + // field 4, encoding 2 (GoTestField)
1779
- "5001" + // field 10, encoding 0, value 1
1780
- "6d20000000" + // field 13, encoding 5, value 0x20
1781
- "714000000000000000" + // field 14, encoding 1, value 0x40
1782
- "78a019" + // field 15, encoding 0, value 0xca0 = 3232
1783
- "8001c032" + // field 16, encoding 0, value 0x1940 = 6464
1784
- "8d0100004a45" + // field 17, encoding 5, value 3232.0
1785
- "9101000000000040b940" + // field 18, encoding 1, value 6464.0
1786
- "9a0106" + "737472696e67" + // field 19, encoding 2, string "string"
1787
- "b304" + // field 70, encoding 3, start group
1788
- "ba0408" + "7265717569726564" + // field 71, encoding 2, string "required"
1789
- "b404" + // field 70, encoding 4, end group
1790
- "aa0605" + "6279746573" + // field 101, encoding 2, string "bytes"
1791
- "b0063f" + // field 102, encoding 0, 0x3f zigzag32
1792
- "b8067f" // field 103, encoding 0, 0x7f zigzag64
1793
-
1794
- o := old()
1795
- bytes, err := Marshal(pb)
1796
- if _, ok := err.(*RequiredNotSetError); !ok {
1797
- fmt.Printf("marshal-1 err = %v, want *RequiredNotSetError", err)
1798
- o.DebugPrint("", bytes)
1799
- t.Fatalf("expected = %s", expected)
1800
- }
1801
- if strings.Index(err.Error(), "RequiredField.Label") < 0 {
1802
- t.Errorf("marshal-1 wrong err msg: %v", err)
1803
- }
1804
- if !equal(bytes, expected, t) {
1805
- o.DebugPrint("neq 1", bytes)
1806
- t.Fatalf("expected = %s", expected)
1807
- }
1808
-
1809
- // Now test Unmarshal by recreating the original buffer.
1810
- pbd := new(GoTest)
1811
- err = Unmarshal(bytes, pbd)
1812
- if _, ok := err.(*RequiredNotSetError); !ok {
1813
- t.Fatalf("unmarshal err = %v, want *RequiredNotSetError", err)
1814
- o.DebugPrint("", bytes)
1815
- t.Fatalf("string = %s", expected)
1816
- }
1817
- if strings.Index(err.Error(), "RequiredField.{Unknown}") < 0 {
1818
- t.Errorf("unmarshal wrong err msg: %v", err)
1819
- }
1820
- bytes, err = Marshal(pbd)
1821
- if _, ok := err.(*RequiredNotSetError); !ok {
1822
- t.Errorf("marshal-2 err = %v, want *RequiredNotSetError", err)
1823
- o.DebugPrint("", bytes)
1824
- t.Fatalf("string = %s", expected)
1825
- }
1826
- if strings.Index(err.Error(), "RequiredField.Label") < 0 {
1827
- t.Errorf("marshal-2 wrong err msg: %v", err)
1828
- }
1829
- if !equal(bytes, expected, t) {
1830
- o.DebugPrint("neq 2", bytes)
1831
- t.Fatalf("string = %s", expected)
1832
- }
1833
-}
1834
-
1835
-func fuzzUnmarshal(t *testing.T, data []byte) {
1836
- defer func() {
1837
- if e := recover(); e != nil {
1838
- t.Errorf("These bytes caused a panic: %+v", data)
1839
- t.Logf("Stack:\n%s", debug.Stack())
1840
- t.FailNow()
1841
- }
1842
- }()
1843
-
1844
- pb := new(MyMessage)
1845
- Unmarshal(data, pb)
1846
-}
1847
-
1848
-func TestMapFieldMarshal(t *testing.T) {
1849
- m := &MessageWithMap{
1850
- NameMapping: map[int32]string{
1851
- 1: "Rob",
1852
- 4: "Ian",
1853
- 8: "Dave",
1854
- },
1855
- }
1856
- b, err := Marshal(m)
1857
- if err != nil {
1858
- t.Fatalf("Marshal: %v", err)
1859
- }
1860
-
1861
- // b should be the concatenation of these three byte sequences in some order.
1862
- parts := []string{
1863
- "\n\a\b\x01\x12\x03Rob",
1864
- "\n\a\b\x04\x12\x03Ian",
1865
- "\n\b\b\x08\x12\x04Dave",
1866
- }
1867
- ok := false
1868
- for i := range parts {
1869
- for j := range parts {
1870
- if j == i {
1871
- continue
1872
- }
1873
- for k := range parts {
1874
- if k == i || k == j {
1875
- continue
1876
- }
1877
- try := parts[i] + parts[j] + parts[k]
1878
- if bytes.Equal(b, []byte(try)) {
1879
- ok = true
1880
- break
1881
- }
1882
- }
1883
- }
1884
- }
1885
- if !ok {
1886
- t.Fatalf("Incorrect Marshal output.\n got %q\nwant %q (or a permutation of that)", b, parts[0]+parts[1]+parts[2])
1887
- }
1888
- t.Logf("FYI b: %q", b)
1889
-
1890
- (new(Buffer)).DebugPrint("Dump of b", b)
1891
-}
1892
-
1893
-func TestMapFieldRoundTrips(t *testing.T) {
1894
- m := &MessageWithMap{
1895
- NameMapping: map[int32]string{
1896
- 1: "Rob",
1897
- 4: "Ian",
1898
- 8: "Dave",
1899
- },
1900
- MsgMapping: map[int64]*FloatingPoint{
1901
- 0x7001: &FloatingPoint{F: Float64(2.0)},
1902
- },
1903
- ByteMapping: map[bool][]byte{
1904
- false: []byte("that's not right!"),
1905
- true: []byte("aye, 'tis true!"),
1906
- },
1907
- }
1908
- b, err := Marshal(m)
1909
- if err != nil {
1910
- t.Fatalf("Marshal: %v", err)
1911
- }
1912
- t.Logf("FYI b: %q", b)
1913
- m2 := new(MessageWithMap)
1914
- if err := Unmarshal(b, m2); err != nil {
1915
- t.Fatalf("Unmarshal: %v", err)
1916
- }
1917
- for _, pair := range [][2]interface{}{
1918
- {m.NameMapping, m2.NameMapping},
1919
- {m.MsgMapping, m2.MsgMapping},
1920
- {m.ByteMapping, m2.ByteMapping},
1921
- } {
1922
- if !reflect.DeepEqual(pair[0], pair[1]) {
1923
- t.Errorf("Map did not survive a round trip.\ninitial: %v\n final: %v", pair[0], pair[1])
1924
- }
1925
- }
1926
-}
1927
-
1928
-func TestMapFieldWithNil(t *testing.T) {
1929
- m := &MessageWithMap{
1930
- MsgMapping: map[int64]*FloatingPoint{
1931
- 1: nil,
1932
- },
1933
- }
1934
- b, err := Marshal(m)
1935
- if err == nil {
1936
- t.Fatalf("Marshal of bad map should have failed, got these bytes: %v", b)
1937
- }
1938
-}
1939
-
1940
-// Benchmarks
1941
-
1942
-func testMsg() *GoTest {
1943
- pb := initGoTest(true)
1944
- const N = 1000 // Internally the library starts much smaller.
1945
- pb.F_Int32Repeated = make([]int32, N)
1946
- pb.F_DoubleRepeated = make([]float64, N)
1947
- for i := 0; i < N; i++ {
1948
- pb.F_Int32Repeated[i] = int32(i)
1949
- pb.F_DoubleRepeated[i] = float64(i)
1950
- }
1951
- return pb
1952
-}
1953
-
1954
-func bytesMsg() *GoTest {
1955
- pb := initGoTest(true)
1956
- buf := make([]byte, 4000)
1957
- for i := range buf {
1958
- buf[i] = byte(i)
1959
- }
1960
- pb.F_BytesDefaulted = buf
1961
- return pb
1962
-}
1963
-
1964
-func benchmarkMarshal(b *testing.B, pb Message, marshal func(Message) ([]byte, error)) {
1965
- d, _ := marshal(pb)
1966
- b.SetBytes(int64(len(d)))
1967
- b.ResetTimer()
1968
- for i := 0; i < b.N; i++ {
1969
- marshal(pb)
1970
- }
1971
-}
1972
-
1973
-func benchmarkBufferMarshal(b *testing.B, pb Message) {
1974
- p := NewBuffer(nil)
1975
- benchmarkMarshal(b, pb, func(pb0 Message) ([]byte, error) {
1976
- p.Reset()
1977
- err := p.Marshal(pb0)
1978
- return p.Bytes(), err
1979
- })
1980
-}
1981
-
1982
-func benchmarkSize(b *testing.B, pb Message) {
1983
- benchmarkMarshal(b, pb, func(pb0 Message) ([]byte, error) {
1984
- Size(pb)
1985
- return nil, nil
1986
- })
1987
-}
1988
-
1989
-func newOf(pb Message) Message {
1990
- in := reflect.ValueOf(pb)
1991
- if in.IsNil() {
1992
- return pb
1993
- }
1994
- return reflect.New(in.Type().Elem()).Interface().(Message)
1995
-}
1996
-
1997
-func benchmarkUnmarshal(b *testing.B, pb Message, unmarshal func([]byte, Message) error) {
1998
- d, _ := Marshal(pb)
1999
- b.SetBytes(int64(len(d)))
2000
- pbd := newOf(pb)
2001
-
2002
- b.ResetTimer()
2003
- for i := 0; i < b.N; i++ {
2004
- unmarshal(d, pbd)
2005
- }
2006
-}
2007
-
2008
-func benchmarkBufferUnmarshal(b *testing.B, pb Message) {
2009
- p := NewBuffer(nil)
2010
- benchmarkUnmarshal(b, pb, func(d []byte, pb0 Message) error {
2011
- p.SetBuf(d)
2012
- return p.Unmarshal(pb0)
2013
- })
2014
-}
2015
-
2016
-// Benchmark{Marshal,BufferMarshal,Size,Unmarshal,BufferUnmarshal}{,Bytes}
2017
-
2018
-func BenchmarkMarshal(b *testing.B) {
2019
- benchmarkMarshal(b, testMsg(), Marshal)
2020
-}
2021
-
2022
-func BenchmarkBufferMarshal(b *testing.B) {
2023
- benchmarkBufferMarshal(b, testMsg())
2024
-}
2025
-
2026
-func BenchmarkSize(b *testing.B) {
2027
- benchmarkSize(b, testMsg())
2028
-}
2029
-
2030
-func BenchmarkUnmarshal(b *testing.B) {
2031
- benchmarkUnmarshal(b, testMsg(), Unmarshal)
2032
-}
2033
-
2034
-func BenchmarkBufferUnmarshal(b *testing.B) {
2035
- benchmarkBufferUnmarshal(b, testMsg())
2036
-}
2037
-
2038
-func BenchmarkMarshalBytes(b *testing.B) {
2039
- benchmarkMarshal(b, bytesMsg(), Marshal)
2040
-}
2041
-
2042
-func BenchmarkBufferMarshalBytes(b *testing.B) {
2043
- benchmarkBufferMarshal(b, bytesMsg())
2044
-}
2045
-
2046
-func BenchmarkSizeBytes(b *testing.B) {
2047
- benchmarkSize(b, bytesMsg())
2048
-}
2049
-
2050
-func BenchmarkUnmarshalBytes(b *testing.B) {
2051
- benchmarkUnmarshal(b, bytesMsg(), Unmarshal)
2052
-}
2053
-
2054
-func BenchmarkBufferUnmarshalBytes(b *testing.B) {
2055
- benchmarkBufferUnmarshal(b, bytesMsg())
2056
-}
2057
-
2058
-func BenchmarkUnmarshalUnrecognizedFields(b *testing.B) {
2059
- b.StopTimer()
2060
- pb := initGoTestField()
2061
- skip := &GoSkipTest{
2062
- SkipInt32: Int32(32),
2063
- SkipFixed32: Uint32(3232),
2064
- SkipFixed64: Uint64(6464),
2065
- SkipString: String("skipper"),
2066
- Skipgroup: &GoSkipTest_SkipGroup{
2067
- GroupInt32: Int32(75),
2068
- GroupString: String("wxyz"),
2069
- },
2070
- }
2071
-
2072
- pbd := new(GoTestField)
2073
- p := NewBuffer(nil)
2074
- p.Marshal(pb)
2075
- p.Marshal(skip)
2076
- p2 := NewBuffer(nil)
2077
-
2078
- b.StartTimer()
2079
- for i := 0; i < b.N; i++ {
2080
- p2.SetBuf(p.Bytes())
2081
- p2.Unmarshal(pbd)
2082
- }
2083
-}
Godeps/_workspace/src/github.com/golang/protobuf/proto/clone.go
deleted
-197
@@ -1,197 +0,0 @@
1
-// Go support for Protocol Buffers - Google's data interchange format
2
-//
3
-// Copyright 2011 The Go Authors. All rights reserved.
4
-// https://github.com/golang/protobuf
5
-//
6
-// Redistribution and use in source and binary forms, with or without
7
-// modification, are permitted provided that the following conditions are
8
-// met:
9
-//
10
-// * Redistributions of source code must retain the above copyright
11
-// notice, this list of conditions and the following disclaimer.
12
-// * Redistributions in binary form must reproduce the above
13
-// copyright notice, this list of conditions and the following disclaimer
14
-// in the documentation and/or other materials provided with the
15
-// distribution.
16
-// * Neither the name of Google Inc. nor the names of its
17
-// contributors may be used to endorse or promote products derived from
18
-// this software without specific prior written permission.
19
-//
20
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
-// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
-// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23
-// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24
-// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25
-// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26
-// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27
-// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28
-// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29
-// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
-// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
-
32
-// Protocol buffer deep copy and merge.
33
-// TODO: MessageSet and RawMessage.
34
-
35
-package proto
36
-
37
-import (
38
- "log"
39
- "reflect"
40
- "strings"
41
-)
42
-
43
-// Clone returns a deep copy of a protocol buffer.
44
-func Clone(pb Message) Message {
45
- in := reflect.ValueOf(pb)
46
- if in.IsNil() {
47
- return pb
48
- }
49
-
50
- out := reflect.New(in.Type().Elem())
51
- // out is empty so a merge is a deep copy.
52
- mergeStruct(out.Elem(), in.Elem())
53
- return out.Interface().(Message)
54
-}
55
-
56
-// Merge merges src into dst.
57
-// Required and optional fields that are set in src will be set to that value in dst.
58
-// Elements of repeated fields will be appended.
59
-// Merge panics if src and dst are not the same type, or if dst is nil.
60
-func Merge(dst, src Message) {
61
- in := reflect.ValueOf(src)
62
- out := reflect.ValueOf(dst)
63
- if out.IsNil() {
64
- panic("proto: nil destination")
65
- }
66
- if in.Type() != out.Type() {
67
- // Explicit test prior to mergeStruct so that mistyped nils will fail
68
- panic("proto: type mismatch")
69
- }
70
- if in.IsNil() {
71
- // Merging nil into non-nil is a quiet no-op
72
- return
73
- }
74
- mergeStruct(out.Elem(), in.Elem())
75
-}
76
-
77
-func mergeStruct(out, in reflect.Value) {
78
- for i := 0; i < in.NumField(); i++ {
79
- f := in.Type().Field(i)
80
- if strings.HasPrefix(f.Name, "XXX_") {
81
- continue
82
- }
83
- mergeAny(out.Field(i), in.Field(i))
84
- }
85
-
86
- if emIn, ok := in.Addr().Interface().(extendableProto); ok {
87
- emOut := out.Addr().Interface().(extendableProto)
88
- mergeExtension(emOut.ExtensionMap(), emIn.ExtensionMap())
89
- }
90
-
91
- uf := in.FieldByName("XXX_unrecognized")
92
- if !uf.IsValid() {
93
- return
94
- }
95
- uin := uf.Bytes()
96
- if len(uin) > 0 {
97
- out.FieldByName("XXX_unrecognized").SetBytes(append([]byte(nil), uin...))
98
- }
99
-}
100
-
101
-func mergeAny(out, in reflect.Value) {
102
- if in.Type() == protoMessageType {
103
- if !in.IsNil() {
104
- if out.IsNil() {
105
- out.Set(reflect.ValueOf(Clone(in.Interface().(Message))))
106
- } else {
107
- Merge(out.Interface().(Message), in.Interface().(Message))
108
- }
109
- }
110
- return
111
- }
112
- switch in.Kind() {
113
- case reflect.Bool, reflect.Float32, reflect.Float64, reflect.Int32, reflect.Int64,
114
- reflect.String, reflect.Uint32, reflect.Uint64:
115
- out.Set(in)
116
- case reflect.Map:
117
- if in.Len() == 0 {
118
- return
119
- }
120
- if out.IsNil() {
121
- out.Set(reflect.MakeMap(in.Type()))
122
- }
123
- // For maps with value types of *T or []byte we need to deep copy each value.
124
- elemKind := in.Type().Elem().Kind()
125
- for _, key := range in.MapKeys() {
126
- var val reflect.Value
127
- switch elemKind {
128
- case reflect.Ptr:
129
- val = reflect.New(in.Type().Elem().Elem())
130
- mergeAny(val, in.MapIndex(key))
131
- case reflect.Slice:
132
- val = in.MapIndex(key)
133
- val = reflect.ValueOf(append([]byte{}, val.Bytes()...))
134
- default:
135
- val = in.MapIndex(key)
136
- }
137
- out.SetMapIndex(key, val)
138
- }
139
- case reflect.Ptr:
140
- if in.IsNil() {
141
- return
142
- }
143
- if out.IsNil() {
144
- out.Set(reflect.New(in.Elem().Type()))
145
- }
146
- mergeAny(out.Elem(), in.Elem())
147
- case reflect.Slice:
148
- if in.IsNil() {
149
- return
150
- }
151
- if in.Type().Elem().Kind() == reflect.Uint8 {
152
- // []byte is a scalar bytes field, not a repeated field.
153
- // Make a deep copy.
154
- // Append to []byte{} instead of []byte(nil) so that we never end up
155
- // with a nil result.
156
- out.SetBytes(append([]byte{}, in.Bytes()...))
157
- return
158
- }
159
- n := in.Len()
160
- if out.IsNil() {
161
- out.Set(reflect.MakeSlice(in.Type(), 0, n))
162
- }
163
- switch in.Type().Elem().Kind() {
164
- case reflect.Bool, reflect.Float32, reflect.Float64, reflect.Int32, reflect.Int64,
165
- reflect.String, reflect.Uint32, reflect.Uint64:
166
- out.Set(reflect.AppendSlice(out, in))
167
- default:
168
- for i := 0; i < n; i++ {
169
- x := reflect.Indirect(reflect.New(in.Type().Elem()))
170
- mergeAny(x, in.Index(i))
171
- out.Set(reflect.Append(out, x))
172
- }
173
- }
174
- case reflect.Struct:
175
- mergeStruct(out, in)
176
- default:
177
- // unknown type, so not a protocol buffer
178
- log.Printf("proto: don't know how to copy %v", in)
179
- }
180
-}
181
-
182
-func mergeExtension(out, in map[int32]Extension) {
183
- for extNum, eIn := range in {
184
- eOut := Extension{desc: eIn.desc}
185
- if eIn.value != nil {
186
- v := reflect.New(reflect.TypeOf(eIn.value)).Elem()
187
- mergeAny(v, reflect.ValueOf(eIn.value))
188
- eOut.value = v.Interface()
189
- }
190
- if eIn.enc != nil {
191
- eOut.enc = make([]byte, len(eIn.enc))
192
- copy(eOut.enc, eIn.enc)
193
- }
194
-
195
- out[extNum] = eOut
196
- }
197
-}
Godeps/_workspace/src/github.com/golang/protobuf/proto/clone_test.go
deleted
-227
@@ -1,227 +0,0 @@
1
-// Go support for Protocol Buffers - Google's data interchange format
2
-//
3
-// Copyright 2011 The Go Authors. All rights reserved.
4
-// https://github.com/golang/protobuf
5
-//
6
-// Redistribution and use in source and binary forms, with or without
7
-// modification, are permitted provided that the following conditions are
8
-// met:
9
-//
10
-// * Redistributions of source code must retain the above copyright
11
-// notice, this list of conditions and the following disclaimer.
12
-// * Redistributions in binary form must reproduce the above
13
-// copyright notice, this list of conditions and the following disclaimer
14
-// in the documentation and/or other materials provided with the
15
-// distribution.
16
-// * Neither the name of Google Inc. nor the names of its
17
-// contributors may be used to endorse or promote products derived from
18
-// this software without specific prior written permission.
19
-//
20
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
-// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
-// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23
-// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24
-// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25
-// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26
-// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27
-// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28
-// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29
-// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
-// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
-
32
-package proto_test
33
-
34
-import (
35
- "testing"
36
-
37
- "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/golang/protobuf/proto"
38
-
39
- pb "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/golang/protobuf/proto/testdata"
40
-)
41
-
42
-var cloneTestMessage = &pb.MyMessage{
43
- Count: proto.Int32(42),
44
- Name: proto.String("Dave"),
45
- Pet: []string{"bunny", "kitty", "horsey"},
46
- Inner: &pb.InnerMessage{
47
- Host: proto.String("niles"),
48
- Port: proto.Int32(9099),
49
- Connected: proto.Bool(true),
50
- },
51
- Others: []*pb.OtherMessage{
52
- {
53
- Value: []byte("some bytes"),
54
- },
55
- },
56
- Somegroup: &pb.MyMessage_SomeGroup{
57
- GroupField: proto.Int32(6),
58
- },
59
- RepBytes: [][]byte{[]byte("sham"), []byte("wow")},
60
-}
61
-
62
-func init() {
63
- ext := &pb.Ext{
64
- Data: proto.String("extension"),
65
- }
66
- if err := proto.SetExtension(cloneTestMessage, pb.E_Ext_More, ext); err != nil {
67
- panic("SetExtension: " + err.Error())
68
- }
69
-}
70
-
71
-func TestClone(t *testing.T) {
72
- m := proto.Clone(cloneTestMessage).(*pb.MyMessage)
73
- if !proto.Equal(m, cloneTestMessage) {
74
- t.Errorf("Clone(%v) = %v", cloneTestMessage, m)
75
- }
76
-
77
- // Verify it was a deep copy.
78
- *m.Inner.Port++
79
- if proto.Equal(m, cloneTestMessage) {
80
- t.Error("Mutating clone changed the original")
81
- }
82
- // Byte fields and repeated fields should be copied.
83
- if &m.Pet[0] == &cloneTestMessage.Pet[0] {
84
- t.Error("Pet: repeated field not copied")
85
- }
86
- if &m.Others[0] == &cloneTestMessage.Others[0] {
87
- t.Error("Others: repeated field not copied")
88
- }
89
- if &m.Others[0].Value[0] == &cloneTestMessage.Others[0].Value[0] {
90
- t.Error("Others[0].Value: bytes field not copied")
91
- }
92
- if &m.RepBytes[0] == &cloneTestMessage.RepBytes[0] {
93
- t.Error("RepBytes: repeated field not copied")
94
- }
95
- if &m.RepBytes[0][0] == &cloneTestMessage.RepBytes[0][0] {
96
- t.Error("RepBytes[0]: bytes field not copied")
97
- }
98
-}
99
-
100
-func TestCloneNil(t *testing.T) {
101
- var m *pb.MyMessage
102
- if c := proto.Clone(m); !proto.Equal(m, c) {
103
- t.Errorf("Clone(%v) = %v", m, c)
104
- }
105
-}
106
-
107
-var mergeTests = []struct {
108
- src, dst, want proto.Message
109
-}{
110
- {
111
- src: &pb.MyMessage{
112
- Count: proto.Int32(42),
113
- },
114
- dst: &pb.MyMessage{
115
- Name: proto.String("Dave"),
116
- },
117
- want: &pb.MyMessage{
118
- Count: proto.Int32(42),
119
- Name: proto.String("Dave"),
120
- },
121
- },
122
- {
123
- src: &pb.MyMessage{
124
- Inner: &pb.InnerMessage{
125
- Host: proto.String("hey"),
126
- Connected: proto.Bool(true),
127
- },
128
- Pet: []string{"horsey"},
129
- Others: []*pb.OtherMessage{
130
- {
131
- Value: []byte("some bytes"),
132
- },
133
- },
134
- },
135
- dst: &pb.MyMessage{
136
- Inner: &pb.InnerMessage{
137
- Host: proto.String("niles"),
138
- Port: proto.Int32(9099),
139
- },
140
- Pet: []string{"bunny", "kitty"},
141
- Others: []*pb.OtherMessage{
142
- {
143
- Key: proto.Int64(31415926535),
144
- },
145
- {
146
- // Explicitly test a src=nil field
147
- Inner: nil,
148
- },
149
- },
150
- },
151
- want: &pb.MyMessage{
152
- Inner: &pb.InnerMessage{
153
- Host: proto.String("hey"),
154
- Connected: proto.Bool(true),
155
- Port: proto.Int32(9099),
156
- },
157
- Pet: []string{"bunny", "kitty", "horsey"},
158
- Others: []*pb.OtherMessage{
159
- {
160
- Key: proto.Int64(31415926535),
161
- },
162
- {},
163
- {
164
- Value: []byte("some bytes"),
165
- },
166
- },
167
- },
168
- },
169
- {
170
- src: &pb.MyMessage{
171
- RepBytes: [][]byte{[]byte("wow")},
172
- },
173
- dst: &pb.MyMessage{
174
- Somegroup: &pb.MyMessage_SomeGroup{
175
- GroupField: proto.Int32(6),
176
- },
177
- RepBytes: [][]byte{[]byte("sham")},
178
- },
179
- want: &pb.MyMessage{
180
- Somegroup: &pb.MyMessage_SomeGroup{
181
- GroupField: proto.Int32(6),
182
- },
183
- RepBytes: [][]byte{[]byte("sham"), []byte("wow")},
184
- },
185
- },
186
- // Check that a scalar bytes field replaces rather than appends.
187
- {
188
- src: &pb.OtherMessage{Value: []byte("foo")},
189
- dst: &pb.OtherMessage{Value: []byte("bar")},
190
- want: &pb.OtherMessage{Value: []byte("foo")},
191
- },
192
- {
193
- src: &pb.MessageWithMap{
194
- NameMapping: map[int32]string{6: "Nigel"},
195
- MsgMapping: map[int64]*pb.FloatingPoint{
196
- 0x4001: &pb.FloatingPoint{F: proto.Float64(2.0)},
197
- },
198
- ByteMapping: map[bool][]byte{true: []byte("wowsa")},
199
- },
200
- dst: &pb.MessageWithMap{
201
- NameMapping: map[int32]string{
202
- 6: "Bruce", // should be overwritten
203
- 7: "Andrew",
204
- },
205
- },
206
- want: &pb.MessageWithMap{
207
- NameMapping: map[int32]string{
208
- 6: "Nigel",
209
- 7: "Andrew",
210
- },
211
- MsgMapping: map[int64]*pb.FloatingPoint{
212
- 0x4001: &pb.FloatingPoint{F: proto.Float64(2.0)},
213
- },
214
- ByteMapping: map[bool][]byte{true: []byte("wowsa")},
215
- },
216
- },
217
-}
218
-
219
-func TestMerge(t *testing.T) {
220
- for _, m := range mergeTests {
221
- got := proto.Clone(m.dst)
222
- proto.Merge(got, m.src)
223
- if !proto.Equal(got, m.want) {
224
- t.Errorf("Merge(%v, %v)\n got %v\nwant %v\n", m.dst, m.src, got, m.want)
225
- }
226
- }
227
-}
Godeps/_workspace/src/github.com/golang/protobuf/proto/decode.go
deleted
-827
@@ -1,827 +0,0 @@
1
-// Go support for Protocol Buffers - Google's data interchange format
2
-//
3
-// Copyright 2010 The Go Authors. All rights reserved.
4
-// https://github.com/golang/protobuf
5
-//
6
-// Redistribution and use in source and binary forms, with or without
7
-// modification, are permitted provided that the following conditions are
8
-// met:
9
-//
10
-// * Redistributions of source code must retain the above copyright
11
-// notice, this list of conditions and the following disclaimer.
12
-// * Redistributions in binary form must reproduce the above
13
-// copyright notice, this list of conditions and the following disclaimer
14
-// in the documentation and/or other materials provided with the
15
-// distribution.
16
-// * Neither the name of Google Inc. nor the names of its
17
-// contributors may be used to endorse or promote products derived from
18
-// this software without specific prior written permission.
19
-//
20
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
-// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
-// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23
-// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24
-// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25
-// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26
-// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27
-// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28
-// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29
-// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
-// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
-
32
-package proto
33
-
34
-/*
35
- * Routines for decoding protocol buffer data to construct in-memory representations.
36
- */
37
-
38
-import (
39
- "errors"
40
- "fmt"
41
- "io"
42
- "os"
43
- "reflect"
44
-)
45
-
46
-// errOverflow is returned when an integer is too large to be represented.
47
-var errOverflow = errors.New("proto: integer overflow")
48
-
49
-// The fundamental decoders that interpret bytes on the wire.
50
-// Those that take integer types all return uint64 and are
51
-// therefore of type valueDecoder.
52
-
53
-// DecodeVarint reads a varint-encoded integer from the slice.
54
-// It returns the integer and the number of bytes consumed, or
55
-// zero if there is not enough.
56
-// This is the format for the
57
-// int32, int64, uint32, uint64, bool, and enum
58
-// protocol buffer types.
59
-func DecodeVarint(buf []byte) (x uint64, n int) {
60
- // x, n already 0
61
- for shift := uint(0); shift < 64; shift += 7 {
62
- if n >= len(buf) {
63
- return 0, 0
64
- }
65
- b := uint64(buf[n])
66
- n++
67
- x |= (b & 0x7F) << shift
68
- if (b & 0x80) == 0 {
69
- return x, n
70
- }
71
- }
72
-
73
- // The number is too large to represent in a 64-bit value.
74
- return 0, 0
75
-}
76
-
77
-// DecodeVarint reads a varint-encoded integer from the Buffer.
78
-// This is the format for the
79
-// int32, int64, uint32, uint64, bool, and enum
80
-// protocol buffer types.
81
-func (p *Buffer) DecodeVarint() (x uint64, err error) {
82
- // x, err already 0
83
-
84
- i := p.index
85
- l := len(p.buf)
86
-
87
- for shift := uint(0); shift < 64; shift += 7 {
88
- if i >= l {
89
- err = io.ErrUnexpectedEOF
90
- return
91
- }
92
- b := p.buf[i]
93
- i++
94
- x |= (uint64(b) & 0x7F) << shift
95
- if b < 0x80 {
96
- p.index = i
97
- return
98
- }
99
- }
100
-
101
- // The number is too large to represent in a 64-bit value.
102
- err = errOverflow
103
- return
104
-}
105
-
106
-// DecodeFixed64 reads a 64-bit integer from the Buffer.
107
-// This is the format for the
108
-// fixed64, sfixed64, and double protocol buffer types.
109
-func (p *Buffer) DecodeFixed64() (x uint64, err error) {
110
- // x, err already 0
111
- i := p.index + 8
112
- if i < 0 || i > len(p.buf) {
113
- err = io.ErrUnexpectedEOF
114
- return
115
- }
116
- p.index = i
117
-
118
- x = uint64(p.buf[i-8])
119
- x |= uint64(p.buf[i-7]) << 8
120
- x |= uint64(p.buf[i-6]) << 16
121
- x |= uint64(p.buf[i-5]) << 24
122
- x |= uint64(p.buf[i-4]) << 32
123
- x |= uint64(p.buf[i-3]) << 40
124
- x |= uint64(p.buf[i-2]) << 48
125
- x |= uint64(p.buf[i-1]) << 56
126
- return
127
-}
128
-
129
-// DecodeFixed32 reads a 32-bit integer from the Buffer.
130
-// This is the format for the
131
-// fixed32, sfixed32, and float protocol buffer types.
132
-func (p *Buffer) DecodeFixed32() (x uint64, err error) {
133
- // x, err already 0
134
- i := p.index + 4
135
- if i < 0 || i > len(p.buf) {
136
- err = io.ErrUnexpectedEOF
137
- return
138
- }
139
- p.index = i
140
-
141
- x = uint64(p.buf[i-4])
142
- x |= uint64(p.buf[i-3]) << 8
143
- x |= uint64(p.buf[i-2]) << 16
144
- x |= uint64(p.buf[i-1]) << 24
145
- return
146
-}
147
-
148
-// DecodeZigzag64 reads a zigzag-encoded 64-bit integer
149
-// from the Buffer.
150
-// This is the format used for the sint64 protocol buffer type.
151
-func (p *Buffer) DecodeZigzag64() (x uint64, err error) {
152
- x, err = p.DecodeVarint()
153
- if err != nil {
154
- return
155
- }
156
- x = (x >> 1) ^ uint64((int64(x&1)<<63)>>63)
157
- return
158
-}
159
-
160
-// DecodeZigzag32 reads a zigzag-encoded 32-bit integer
161
-// from the Buffer.
162
-// This is the format used for the sint32 protocol buffer type.
163
-func (p *Buffer) DecodeZigzag32() (x uint64, err error) {
164
- x, err = p.DecodeVarint()
165
- if err != nil {
166
- return
167
- }
168
- x = uint64((uint32(x) >> 1) ^ uint32((int32(x&1)<<31)>>31))
169
- return
170
-}
171
-
172
-// These are not ValueDecoders: they produce an array of bytes or a string.
173
-// bytes, embedded messages
174
-
175
-// DecodeRawBytes reads a count-delimited byte buffer from the Buffer.
176
-// This is the format used for the bytes protocol buffer
177
-// type and for embedded messages.
178
-func (p *Buffer) DecodeRawBytes(alloc bool) (buf []byte, err error) {
179
- n, err := p.DecodeVarint()
180
- if err != nil {
181
- return nil, err
182
- }
183
-
184
- nb := int(n)
185
- if nb < 0 {
186
- return nil, fmt.Errorf("proto: bad byte length %d", nb)
187
- }
188
- end := p.index + nb
189
- if end < p.index || end > len(p.buf) {
190
- return nil, io.ErrUnexpectedEOF
191
- }
192
-
193
- if !alloc {
194
- // todo: check if can get more uses of alloc=false
195
- buf = p.buf[p.index:end]
196
- p.index += nb
197
- return
198
- }
199
-
200
- buf = make([]byte, nb)
201
- copy(buf, p.buf[p.index:])
202
- p.index += nb
203
- return
204
-}
205
-
206
-// DecodeStringBytes reads an encoded string from the Buffer.
207
-// This is the format used for the proto2 string type.
208
-func (p *Buffer) DecodeStringBytes() (s string, err error) {
209
- buf, err := p.DecodeRawBytes(false)
210
- if err != nil {
211
- return
212
- }
213
- return string(buf), nil
214
-}
215
-
216
-// Skip the next item in the buffer. Its wire type is decoded and presented as an argument.
217
-// If the protocol buffer has extensions, and the field matches, add it as an extension.
218
-// Otherwise, if the XXX_unrecognized field exists, append the skipped data there.
219
-func (o *Buffer) skipAndSave(t reflect.Type, tag, wire int, base structPointer, unrecField field) error {
220
- oi := o.index
221
-
222
- err := o.skip(t, tag, wire)
223
- if err != nil {
224
- return err
225
- }
226
-
227
- if !unrecField.IsValid() {
228
- return nil
229
- }
230
-
231
- ptr := structPointer_Bytes(base, unrecField)
232
-
233
- // Add the skipped field to struct field
234
- obuf := o.buf
235
-
236
- o.buf = *ptr
237
- o.EncodeVarint(uint64(tag<<3 | wire))
238
- *ptr = append(o.buf, obuf[oi:o.index]...)
239
-
240
- o.buf = obuf
241
-
242
- return nil
243
-}
244
-
245
-// Skip the next item in the buffer. Its wire type is decoded and presented as an argument.
246
-func (o *Buffer) skip(t reflect.Type, tag, wire int) error {
247
-
248
- var u uint64
249
- var err error
250
-
251
- switch wire {
252
- case WireVarint:
253
- _, err = o.DecodeVarint()
254
- case WireFixed64:
255
- _, err = o.DecodeFixed64()
256
- case WireBytes:
257
- _, err = o.DecodeRawBytes(false)
258
- case WireFixed32:
259
- _, err = o.DecodeFixed32()
260
- case WireStartGroup:
261
- for {
262
- u, err = o.DecodeVarint()
263
- if err != nil {
264
- break
265
- }
266
- fwire := int(u & 0x7)
267
- if fwire == WireEndGroup {
268
- break
269
- }
270
- ftag := int(u >> 3)
271
- err = o.skip(t, ftag, fwire)
272
- if err != nil {
273
- break
274
- }
275
- }
276
- default:
277
- err = fmt.Errorf("proto: can't skip unknown wire type %d for %s", wire, t)
278
- }
279
- return err
280
-}
281
-
282
-// Unmarshaler is the interface representing objects that can
283
-// unmarshal themselves. The method should reset the receiver before
284
-// decoding starts. The argument points to data that may be
285
-// overwritten, so implementations should not keep references to the
286
-// buffer.
287
-type Unmarshaler interface {
288
- Unmarshal([]byte) error
289
-}
290
-
291
-// Unmarshal parses the protocol buffer representation in buf and places the
292
-// decoded result in pb. If the struct underlying pb does not match
293
-// the data in buf, the results can be unpredictable.
294
-//
295
-// Unmarshal resets pb before starting to unmarshal, so any
296
-// existing data in pb is always removed. Use UnmarshalMerge
297
-// to preserve and append to existing data.
298
-func Unmarshal(buf []byte, pb Message) error {
299
- pb.Reset()
300
- return UnmarshalMerge(buf, pb)
301
-}
302
-
303
-// UnmarshalMerge parses the protocol buffer representation in buf and
304
-// writes the decoded result to pb. If the struct underlying pb does not match
305
-// the data in buf, the results can be unpredictable.
306
-//
307
-// UnmarshalMerge merges into existing data in pb.
308
-// Most code should use Unmarshal instead.
309
-func UnmarshalMerge(buf []byte, pb Message) error {
310
- // If the object can unmarshal itself, let it.
311
- if u, ok := pb.(Unmarshaler); ok {
312
- return u.Unmarshal(buf)
313
- }
314
- return NewBuffer(buf).Unmarshal(pb)
315
-}
316
-
317
-// Unmarshal parses the protocol buffer representation in the
318
-// Buffer and places the decoded result in pb. If the struct
319
-// underlying pb does not match the data in the buffer, the results can be
320
-// unpredictable.
321
-func (p *Buffer) Unmarshal(pb Message) error {
322
- // If the object can unmarshal itself, let it.
323
- if u, ok := pb.(Unmarshaler); ok {
324
- err := u.Unmarshal(p.buf[p.index:])
325
- p.index = len(p.buf)
326
- return err
327
- }
328
-
329
- typ, base, err := getbase(pb)
330
- if err != nil {
331
- return err
332
- }
333
-
334
- err = p.unmarshalType(typ.Elem(), GetProperties(typ.Elem()), false, base)
335
-
336
- if collectStats {
337
- stats.Decode++
338
- }
339
-
340
- return err
341
-}
342
-
343
-// unmarshalType does the work of unmarshaling a structure.
344
-func (o *Buffer) unmarshalType(st reflect.Type, prop *StructProperties, is_group bool, base structPointer) error {
345
- var state errorState
346
- required, reqFields := prop.reqCount, uint64(0)
347
-
348
- var err error
349
- for err == nil && o.index < len(o.buf) {
350
- oi := o.index
351
- var u uint64
352
- u, err = o.DecodeVarint()
353
- if err != nil {
354
- break
355
- }
356
- wire := int(u & 0x7)
357
- if wire == WireEndGroup {
358
- if is_group {
359
- return nil // input is satisfied
360
- }
361
- return fmt.Errorf("proto: %s: wiretype end group for non-group", st)
362
- }
363
- tag := int(u >> 3)
364
- if tag <= 0 {
365
- return fmt.Errorf("proto: %s: illegal tag %d (wire type %d)", st, tag, wire)
366
- }
367
- fieldnum, ok := prop.decoderTags.get(tag)
368
- if !ok {
369
- // Maybe it's an extension?
370
- if prop.extendable {
371
- if e := structPointer_Interface(base, st).(extendableProto); isExtensionField(e, int32(tag)) {
372
- if err = o.skip(st, tag, wire); err == nil {
373
- ext := e.ExtensionMap()[int32(tag)] // may be missing
374
- ext.enc = append(ext.enc, o.buf[oi:o.index]...)
375
- e.ExtensionMap()[int32(tag)] = ext
376
- }
377
- continue
378
- }
379
- }
380
- err = o.skipAndSave(st, tag, wire, base, prop.unrecField)
381
- continue
382
- }
383
- p := prop.Prop[fieldnum]
384
-
385
- if p.dec == nil {
386
- fmt.Fprintf(os.Stderr, "proto: no protobuf decoder for %s.%s\n", st, st.Field(fieldnum).Name)
387
- continue
388
- }
389
- dec := p.dec
390
- if wire != WireStartGroup && wire != p.WireType {
391
- if wire == WireBytes && p.packedDec != nil {
392
- // a packable field
393
- dec = p.packedDec
394
- } else {
395
- err = fmt.Errorf("proto: bad wiretype for field %s.%s: got wiretype %d, want %d", st, st.Field(fieldnum).Name, wire, p.WireType)
396
- continue
397
- }
398
- }
399
- decErr := dec(o, p, base)
400
- if decErr != nil && !state.shouldContinue(decErr, p) {
401
- err = decErr
402
- }
403
- if err == nil && p.Required {
404
- // Successfully decoded a required field.
405
- if tag <= 64 {
406
- // use bitmap for fields 1-64 to catch field reuse.
407
- var mask uint64 = 1 << uint64(tag-1)
408
- if reqFields&mask == 0 {
409
- // new required field
410
- reqFields |= mask
411
- required--
412
- }
413
- } else {
414
- // This is imprecise. It can be fooled by a required field
415
- // with a tag > 64 that is encoded twice; that's very rare.
416
- // A fully correct implementation would require allocating
417
- // a data structure, which we would like to avoid.
418
- required--
419
- }
420
- }
421
- }
422
- if err == nil {
423
- if is_group {
424
- return io.ErrUnexpectedEOF
425
- }
426
- if state.err != nil {
427
- return state.err
428
- }
429
- if required > 0 {
430
- // Not enough information to determine the exact field. If we use extra
431
- // CPU, we could determine the field only if the missing required field
432
- // has a tag <= 64 and we check reqFields.
433
- return &RequiredNotSetError{"{Unknown}"}
434
- }
435
- }
436
- return err
437
-}
438
-
439
-// Individual type decoders
440
-// For each,
441
-// u is the decoded value,
442
-// v is a pointer to the field (pointer) in the struct
443
-
444
-// Sizes of the pools to allocate inside the Buffer.
445
-// The goal is modest amortization and allocation
446
-// on at least 16-byte boundaries.
447
-const (
448
- boolPoolSize = 16
449
- uint32PoolSize = 8
450
- uint64PoolSize = 4
451
-)
452
-
453
-// Decode a bool.
454
-func (o *Buffer) dec_bool(p *Properties, base structPointer) error {
455
- u, err := p.valDec(o)
456
- if err != nil {
457
- return err
458
- }
459
- if len(o.bools) == 0 {
460
- o.bools = make([]bool, boolPoolSize)
461
- }
462
- o.bools[0] = u != 0
463
- *structPointer_Bool(base, p.field) = &o.bools[0]
464
- o.bools = o.bools[1:]
465
- return nil
466
-}
467
-
468
-func (o *Buffer) dec_proto3_bool(p *Properties, base structPointer) error {
469
- u, err := p.valDec(o)
470
- if err != nil {
471
- return err
472
- }
473
- *structPointer_BoolVal(base, p.field) = u != 0
474
- return nil
475
-}
476
-
477
-// Decode an int32.
478
-func (o *Buffer) dec_int32(p *Properties, base structPointer) error {
479
- u, err := p.valDec(o)
480
- if err != nil {
481
- return err
482
- }
483
- word32_Set(structPointer_Word32(base, p.field), o, uint32(u))
484
- return nil
485
-}
486
-
487
-func (o *Buffer) dec_proto3_int32(p *Properties, base structPointer) error {
488
- u, err := p.valDec(o)
489
- if err != nil {
490
- return err
491
- }
492
- word32Val_Set(structPointer_Word32Val(base, p.field), uint32(u))
493
- return nil
494
-}
495
-
496
-// Decode an int64.
497
-func (o *Buffer) dec_int64(p *Properties, base structPointer) error {
498
- u, err := p.valDec(o)
499
- if err != nil {
500
- return err
501
- }
502
- word64_Set(structPointer_Word64(base, p.field), o, u)
503
- return nil
504
-}
505
-
506
-func (o *Buffer) dec_proto3_int64(p *Properties, base structPointer) error {
507
- u, err := p.valDec(o)
508
- if err != nil {
509
- return err
510
- }
511
- word64Val_Set(structPointer_Word64Val(base, p.field), o, u)
512
- return nil
513
-}
514
-
515
-// Decode a string.
516
-func (o *Buffer) dec_string(p *Properties, base structPointer) error {
517
- s, err := o.DecodeStringBytes()
518
- if err != nil {
519
- return err
520
- }
521
- *structPointer_String(base, p.field) = &s
522
- return nil
523
-}
524
-
525
-func (o *Buffer) dec_proto3_string(p *Properties, base structPointer) error {
526
- s, err := o.DecodeStringBytes()
527
- if err != nil {
528
- return err
529
- }
530
- *structPointer_StringVal(base, p.field) = s
531
- return nil
532
-}
533
-
534
-// Decode a slice of bytes ([]byte).
535
-func (o *Buffer) dec_slice_byte(p *Properties, base structPointer) error {
536
- b, err := o.DecodeRawBytes(true)
537
- if err != nil {
538
- return err
539
- }
540
- *structPointer_Bytes(base, p.field) = b
541
- return nil
542
-}
543
-
544
-// Decode a slice of bools ([]bool).
545
-func (o *Buffer) dec_slice_bool(p *Properties, base structPointer) error {
546
- u, err := p.valDec(o)
547
- if err != nil {
548
- return err
549
- }
550
- v := structPointer_BoolSlice(base, p.field)
551
- *v = append(*v, u != 0)
552
- return nil
553
-}
554
-
555
-// Decode a slice of bools ([]bool) in packed format.
556
-func (o *Buffer) dec_slice_packed_bool(p *Properties, base structPointer) error {
557
- v := structPointer_BoolSlice(base, p.field)
558
-
559
- nn, err := o.DecodeVarint()
560
- if err != nil {
561
- return err
562
- }
563
- nb := int(nn) // number of bytes of encoded bools
564
-
565
- y := *v
566
- for i := 0; i < nb; i++ {
567
- u, err := p.valDec(o)
568
- if err != nil {
569
- return err
570
- }
571
- y = append(y, u != 0)
572
- }
573
-
574
- *v = y
575
- return nil
576
-}
577
-
578
-// Decode a slice of int32s ([]int32).
579
-func (o *Buffer) dec_slice_int32(p *Properties, base structPointer) error {
580
- u, err := p.valDec(o)
581
- if err != nil {
582
- return err
583
- }
584
- structPointer_Word32Slice(base, p.field).Append(uint32(u))
585
- return nil
586
-}
587
-
588
-// Decode a slice of int32s ([]int32) in packed format.
589
-func (o *Buffer) dec_slice_packed_int32(p *Properties, base structPointer) error {
590
- v := structPointer_Word32Slice(base, p.field)
591
-
592
- nn, err := o.DecodeVarint()
593
- if err != nil {
594
- return err
595
- }
596
- nb := int(nn) // number of bytes of encoded int32s
597
-
598
- fin := o.index + nb
599
- if fin < o.index {
600
- return errOverflow
601
- }
602
- for o.index < fin {
603
- u, err := p.valDec(o)
604
- if err != nil {
605
- return err
606
- }
607
- v.Append(uint32(u))
608
- }
609
- return nil
610
-}
611
-
612
-// Decode a slice of int64s ([]int64).
613
-func (o *Buffer) dec_slice_int64(p *Properties, base structPointer) error {
614
- u, err := p.valDec(o)
615
- if err != nil {
616
- return err
617
- }
618
-
619
- structPointer_Word64Slice(base, p.field).Append(u)
620
- return nil
621
-}
622
-
623
-// Decode a slice of int64s ([]int64) in packed format.
624
-func (o *Buffer) dec_slice_packed_int64(p *Properties, base structPointer) error {
625
- v := structPointer_Word64Slice(base, p.field)
626
-
627
- nn, err := o.DecodeVarint()
628
- if err != nil {
629
- return err
630
- }
631
- nb := int(nn) // number of bytes of encoded int64s
632
-
633
- fin := o.index + nb
634
- if fin < o.index {
635
- return errOverflow
636
- }
637
- for o.index < fin {
638
- u, err := p.valDec(o)
639
- if err != nil {
640
- return err
641
- }
642
- v.Append(u)
643
- }
644
- return nil
645
-}
646
-
647
-// Decode a slice of strings ([]string).
648
-func (o *Buffer) dec_slice_string(p *Properties, base structPointer) error {
649
- s, err := o.DecodeStringBytes()
650
- if err != nil {
651
- return err
652
- }
653
- v := structPointer_StringSlice(base, p.field)
654
- *v = append(*v, s)
655
- return nil
656
-}
657
-
658
-// Decode a slice of slice of bytes ([][]byte).
659
-func (o *Buffer) dec_slice_slice_byte(p *Properties, base structPointer) error {
660
- b, err := o.DecodeRawBytes(true)
661
- if err != nil {
662
- return err
663
- }
664
- v := structPointer_BytesSlice(base, p.field)
665
- *v = append(*v, b)
666
- return nil
667
-}
668
-
669
-// Decode a map field.
670
-func (o *Buffer) dec_new_map(p *Properties, base structPointer) error {
671
- raw, err := o.DecodeRawBytes(false)
672
- if err != nil {
673
- return err
674
- }
675
- oi := o.index // index at the end of this map entry
676
- o.index -= len(raw) // move buffer back to start of map entry
677
-
678
- mptr := structPointer_Map(base, p.field, p.mtype) // *map[K]V
679
- if mptr.Elem().IsNil() {
680
- mptr.Elem().Set(reflect.MakeMap(mptr.Type().Elem()))
681
- }
682
- v := mptr.Elem() // map[K]V
683
-
684
- // Prepare addressable doubly-indirect placeholders for the key and value types.
685
- // See enc_new_map for why.
686
- keyptr := reflect.New(reflect.PtrTo(p.mtype.Key())).Elem() // addressable *K
687
- keybase := toStructPointer(keyptr.Addr()) // **K
688
-
689
- var valbase structPointer
690
- var valptr reflect.Value
691
- switch p.mtype.Elem().Kind() {
692
- case reflect.Slice:
693
- // []byte
694
- var dummy []byte
695
- valptr = reflect.ValueOf(&dummy) // *[]byte
696
- valbase = toStructPointer(valptr) // *[]byte
697
- case reflect.Ptr:
698
- // message; valptr is **Msg; need to allocate the intermediate pointer
699
- valptr = reflect.New(reflect.PtrTo(p.mtype.Elem())).Elem() // addressable *V
700
- valptr.Set(reflect.New(valptr.Type().Elem()))
701
- valbase = toStructPointer(valptr)
702
- default:
703
- // everything else
704
- valptr = reflect.New(reflect.PtrTo(p.mtype.Elem())).Elem() // addressable *V
705
- valbase = toStructPointer(valptr.Addr()) // **V
706
- }
707
-
708
- // Decode.
709
- // This parses a restricted wire format, namely the encoding of a message
710
- // with two fields. See enc_new_map for the format.
711
- for o.index < oi {
712
- // tagcode for key and value properties are always a single byte
713
- // because they have tags 1 and 2.
714
- tagcode := o.buf[o.index]
715
- o.index++
716
- switch tagcode {
717
- case p.mkeyprop.tagcode[0]:
718
- if err := p.mkeyprop.dec(o, p.mkeyprop, keybase); err != nil {
719
- return err
720
- }
721
- case p.mvalprop.tagcode[0]:
722
- if err := p.mvalprop.dec(o, p.mvalprop, valbase); err != nil {
723
- return err
724
- }
725
- default:
726
- // TODO: Should we silently skip this instead?
727
- return fmt.Errorf("proto: bad map data tag %d", raw[0])
728
- }
729
- }
730
- keyelem, valelem := keyptr.Elem(), valptr.Elem()
731
- if !keyelem.IsValid() || !valelem.IsValid() {
732
- // We did not decode the key or the value in the map entry.
733
- // Either way, it's an invalid map entry.
734
- return fmt.Errorf("proto: bad map data: missing key/val")
735
- }
736
-
737
- v.SetMapIndex(keyelem, valelem)
738
- return nil
739
-}
740
-
741
-// Decode a group.
742
-func (o *Buffer) dec_struct_group(p *Properties, base structPointer) error {
743
- bas := structPointer_GetStructPointer(base, p.field)
744
- if structPointer_IsNil(bas) {
745
- // allocate new nested message
746
- bas = toStructPointer(reflect.New(p.stype))
747
- structPointer_SetStructPointer(base, p.field, bas)
748
- }
749
- return o.unmarshalType(p.stype, p.sprop, true, bas)
750
-}
751
-
752
-// Decode an embedded message.
753
-func (o *Buffer) dec_struct_message(p *Properties, base structPointer) (err error) {
754
- raw, e := o.DecodeRawBytes(false)
755
- if e != nil {
756
- return e
757
- }
758
-
759
- bas := structPointer_GetStructPointer(base, p.field)
760
- if structPointer_IsNil(bas) {
761
- // allocate new nested message
762
- bas = toStructPointer(reflect.New(p.stype))
763
- structPointer_SetStructPointer(base, p.field, bas)
764
- }
765
-
766
- // If the object can unmarshal itself, let it.
767
- if p.isUnmarshaler {
768
- iv := structPointer_Interface(bas, p.stype)
769
- return iv.(Unmarshaler).Unmarshal(raw)
770
- }
771
-
772
- obuf := o.buf
773
- oi := o.index
774
- o.buf = raw
775
- o.index = 0
776
-
777
- err = o.unmarshalType(p.stype, p.sprop, false, bas)
778
- o.buf = obuf
779
- o.index = oi
780
-
781
- return err
782
-}
783
-
784
-// Decode a slice of embedded messages.
785
-func (o *Buffer) dec_slice_struct_message(p *Properties, base structPointer) error {
786
- return o.dec_slice_struct(p, false, base)
787
-}
788
-
789
-// Decode a slice of embedded groups.
790
-func (o *Buffer) dec_slice_struct_group(p *Properties, base structPointer) error {
791
- return o.dec_slice_struct(p, true, base)
792
-}
793
-
794
-// Decode a slice of structs ([]*struct).
795
-func (o *Buffer) dec_slice_struct(p *Properties, is_group bool, base structPointer) error {
796
- v := reflect.New(p.stype)
797
- bas := toStructPointer(v)
798
- structPointer_StructPointerSlice(base, p.field).Append(bas)
799
-
800
- if is_group {
801
- err := o.unmarshalType(p.stype, p.sprop, is_group, bas)
802
- return err
803
- }
804
-
805
- raw, err := o.DecodeRawBytes(false)
806
- if err != nil {
807
- return err
808
- }
809
-
810
- // If the object can unmarshal itself, let it.
811
- if p.isUnmarshaler {
812
- iv := v.Interface()
813
- return iv.(Unmarshaler).Unmarshal(raw)
814
- }
815
-
816
- obuf := o.buf
817
- oi := o.index
818
- o.buf = raw
819
- o.index = 0
820
-
821
- err = o.unmarshalType(p.stype, p.sprop, is_group, bas)
822
-
823
- o.buf = obuf
824
- o.index = oi
825
-
826
- return err
827
-}
Godeps/_workspace/src/github.com/golang/protobuf/proto/encode.go
deleted
-1293
@@ -1,1293 +0,0 @@
1
-// Go support for Protocol Buffers - Google's data interchange format
2
-//
3
-// Copyright 2010 The Go Authors. All rights reserved.
4
-// https://github.com/golang/protobuf
5
-//
6
-// Redistribution and use in source and binary forms, with or without
7
-// modification, are permitted provided that the following conditions are
8
-// met:
9
-//
10
-// * Redistributions of source code must retain the above copyright
11
-// notice, this list of conditions and the following disclaimer.
12
-// * Redistributions in binary form must reproduce the above
13
-// copyright notice, this list of conditions and the following disclaimer
14
-// in the documentation and/or other materials provided with the
15
-// distribution.
16
-// * Neither the name of Google Inc. nor the names of its
17
-// contributors may be used to endorse or promote products derived from
18
-// this software without specific prior written permission.
19
-//
20
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
-// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
-// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23
-// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24
-// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25
-// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26
-// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27
-// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28
-// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29
-// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
-// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
-
32
-package proto
33
-
34
-/*
35
- * Routines for encoding data into the wire format for protocol buffers.
36
- */
37
-
38
-import (
39
- "errors"
40
- "fmt"
41
- "reflect"
42
- "sort"
43
-)
44
-
45
-// RequiredNotSetError is the error returned if Marshal is called with
46
-// a protocol buffer struct whose required fields have not
47
-// all been initialized. It is also the error returned if Unmarshal is
48
-// called with an encoded protocol buffer that does not include all the
49
-// required fields.
50
-//
51
-// When printed, RequiredNotSetError reports the first unset required field in a
52
-// message. If the field cannot be precisely determined, it is reported as
53
-// "{Unknown}".
54
-type RequiredNotSetError struct {
55
- field string
56
-}
57
-
58
-func (e *RequiredNotSetError) Error() string {
59
- return fmt.Sprintf("proto: required field %q not set", e.field)
60
-}
61
-
62
-var (
63
- // errRepeatedHasNil is the error returned if Marshal is called with
64
- // a struct with a repeated field containing a nil element.
65
- errRepeatedHasNil = errors.New("proto: repeated field has nil element")
66
-
67
- // ErrNil is the error returned if Marshal is called with nil.
68
- ErrNil = errors.New("proto: Marshal called with nil")
69
-)
70
-
71
-// The fundamental encoders that put bytes on the wire.
72
-// Those that take integer types all accept uint64 and are
73
-// therefore of type valueEncoder.
74
-
75
-const maxVarintBytes = 10 // maximum length of a varint
76
-
77
-// EncodeVarint returns the varint encoding of x.
78
-// This is the format for the
79
-// int32, int64, uint32, uint64, bool, and enum
80
-// protocol buffer types.
81
-// Not used by the package itself, but helpful to clients
82
-// wishing to use the same encoding.
83
-func EncodeVarint(x uint64) []byte {
84
- var buf [maxVarintBytes]byte
85
- var n int
86
- for n = 0; x > 127; n++ {
87
- buf[n] = 0x80 | uint8(x&0x7F)
88
- x >>= 7
89
- }
90
- buf[n] = uint8(x)
91
- n++
92
- return buf[0:n]
93
-}
94
-
95
-// EncodeVarint writes a varint-encoded integer to the Buffer.
96
-// This is the format for the
97
-// int32, int64, uint32, uint64, bool, and enum
98
-// protocol buffer types.
99
-func (p *Buffer) EncodeVarint(x uint64) error {
100
- for x >= 1<<7 {
101
- p.buf = append(p.buf, uint8(x&0x7f|0x80))
102
- x >>= 7
103
- }
104
- p.buf = append(p.buf, uint8(x))
105
- return nil
106
-}
107
-
108
-func sizeVarint(x uint64) (n int) {
109
- for {
110
- n++
111
- x >>= 7
112
- if x == 0 {
113
- break
114
- }
115
- }
116
- return n
117
-}
118
-
119
-// EncodeFixed64 writes a 64-bit integer to the Buffer.
120
-// This is the format for the
121
-// fixed64, sfixed64, and double protocol buffer types.
122
-func (p *Buffer) EncodeFixed64(x uint64) error {
123
- p.buf = append(p.buf,
124
- uint8(x),
125
- uint8(x>>8),
126
- uint8(x>>16),
127
- uint8(x>>24),
128
- uint8(x>>32),
129
- uint8(x>>40),
130
- uint8(x>>48),
131
- uint8(x>>56))
132
- return nil
133
-}
134
-
135
-func sizeFixed64(x uint64) int {
136
- return 8
137
-}
138
-
139
-// EncodeFixed32 writes a 32-bit integer to the Buffer.
140
-// This is the format for the
141
-// fixed32, sfixed32, and float protocol buffer types.
142
-func (p *Buffer) EncodeFixed32(x uint64) error {
143
- p.buf = append(p.buf,
144
- uint8(x),
145
- uint8(x>>8),
146
- uint8(x>>16),
147
- uint8(x>>24))
148
- return nil
149
-}
150
-
151
-func sizeFixed32(x uint64) int {
152
- return 4
153
-}
154
-
155
-// EncodeZigzag64 writes a zigzag-encoded 64-bit integer
156
-// to the Buffer.
157
-// This is the format used for the sint64 protocol buffer type.
158
-func (p *Buffer) EncodeZigzag64(x uint64) error {
159
- // use signed number to get arithmetic right shift.
160
- return p.EncodeVarint(uint64((x << 1) ^ uint64((int64(x) >> 63))))
161
-}
162
-
163
-func sizeZigzag64(x uint64) int {
164
- return sizeVarint(uint64((x << 1) ^ uint64((int64(x) >> 63))))
165
-}
166
-
167
-// EncodeZigzag32 writes a zigzag-encoded 32-bit integer
168
-// to the Buffer.
169
-// This is the format used for the sint32 protocol buffer type.
170
-func (p *Buffer) EncodeZigzag32(x uint64) error {
171
- // use signed number to get arithmetic right shift.
172
- return p.EncodeVarint(uint64((uint32(x) << 1) ^ uint32((int32(x) >> 31))))
173
-}
174
-
175
-func sizeZigzag32(x uint64) int {
176
- return sizeVarint(uint64((uint32(x) << 1) ^ uint32((int32(x) >> 31))))
177
-}
178
-
179
-// EncodeRawBytes writes a count-delimited byte buffer to the Buffer.
180
-// This is the format used for the bytes protocol buffer
181
-// type and for embedded messages.
182
-func (p *Buffer) EncodeRawBytes(b []byte) error {
183
- p.EncodeVarint(uint64(len(b)))
184
- p.buf = append(p.buf, b...)
185
- return nil
186
-}
187
-
188
-func sizeRawBytes(b []byte) int {
189
- return sizeVarint(uint64(len(b))) +
190
- len(b)
191
-}
192
-
193
-// EncodeStringBytes writes an encoded string to the Buffer.
194
-// This is the format used for the proto2 string type.
195
-func (p *Buffer) EncodeStringBytes(s string) error {
196
- p.EncodeVarint(uint64(len(s)))
197
- p.buf = append(p.buf, s...)
198
- return nil
199
-}
200
-
201
-func sizeStringBytes(s string) int {
202
- return sizeVarint(uint64(len(s))) +
203
- len(s)
204
-}
205
-
206
-// Marshaler is the interface representing objects that can marshal themselves.
207
-type Marshaler interface {
208
- Marshal() ([]byte, error)
209
-}
210
-
211
-// Marshal takes the protocol buffer
212
-// and encodes it into the wire format, returning the data.
213
-func Marshal(pb Message) ([]byte, error) {
214
- // Can the object marshal itself?
215
- if m, ok := pb.(Marshaler); ok {
216
- return m.Marshal()
217
- }
218
- p := NewBuffer(nil)
219
- err := p.Marshal(pb)
220
- var state errorState
221
- if err != nil && !state.shouldContinue(err, nil) {
222
- return nil, err
223
- }
224
- if p.buf == nil && err == nil {
225
- // Return a non-nil slice on success.
226
- return []byte{}, nil
227
- }
228
- return p.buf, err
229
-}
230
-
231
-// Marshal takes the protocol buffer
232
-// and encodes it into the wire format, writing the result to the
233
-// Buffer.
234
-func (p *Buffer) Marshal(pb Message) error {
235
- // Can the object marshal itself?
236
- if m, ok := pb.(Marshaler); ok {
237
- data, err := m.Marshal()
238
- if err != nil {
239
- return err
240
- }
241
- p.buf = append(p.buf, data...)
242
- return nil
243
- }
244
-
245
- t, base, err := getbase(pb)
246
- if structPointer_IsNil(base) {
247
- return ErrNil
248
- }
249
- if err == nil {
250
- err = p.enc_struct(GetProperties(t.Elem()), base)
251
- }
252
-
253
- if collectStats {
254
- stats.Encode++
255
- }
256
-
257
- return err
258
-}
259
-
260
-// Size returns the encoded size of a protocol buffer.
261
-func Size(pb Message) (n int) {
262
- // Can the object marshal itself? If so, Size is slow.
263
- // TODO: add Size to Marshaler, or add a Sizer interface.
264
- if m, ok := pb.(Marshaler); ok {
265
- b, _ := m.Marshal()
266
- return len(b)
267
- }
268
-
269
- t, base, err := getbase(pb)
270
- if structPointer_IsNil(base) {
271
- return 0
272
- }
273
- if err == nil {
274
- n = size_struct(GetProperties(t.Elem()), base)
275
- }
276
-
277
- if collectStats {
278
- stats.Size++
279
- }
280
-
281
- return
282
-}
283
-
284
-// Individual type encoders.
285
-
286
-// Encode a bool.
287
-func (o *Buffer) enc_bool(p *Properties, base structPointer) error {
288
- v := *structPointer_Bool(base, p.field)
289
- if v == nil {
290
- return ErrNil
291
- }
292
- x := 0
293
- if *v {
294
- x = 1
295
- }
296
- o.buf = append(o.buf, p.tagcode...)
297
- p.valEnc(o, uint64(x))
298
- return nil
299
-}
300
-
301
-func (o *Buffer) enc_proto3_bool(p *Properties, base structPointer) error {
302
- v := *structPointer_BoolVal(base, p.field)
303
- if !v {
304
- return ErrNil
305
- }
306
- o.buf = append(o.buf, p.tagcode...)
307
- p.valEnc(o, 1)
308
- return nil
309
-}
310
-
311
-func size_bool(p *Properties, base structPointer) int {
312
- v := *structPointer_Bool(base, p.field)
313
- if v == nil {
314
- return 0
315
- }
316
- return len(p.tagcode) + 1 // each bool takes exactly one byte
317
-}
318
-
319
-func size_proto3_bool(p *Properties, base structPointer) int {
320
- v := *structPointer_BoolVal(base, p.field)
321
- if !v {
322
- return 0
323
- }
324
- return len(p.tagcode) + 1 // each bool takes exactly one byte
325
-}
326
-
327
-// Encode an int32.
328
-func (o *Buffer) enc_int32(p *Properties, base structPointer) error {
329
- v := structPointer_Word32(base, p.field)
330
- if word32_IsNil(v) {
331
- return ErrNil
332
- }
333
- x := int32(word32_Get(v)) // permit sign extension to use full 64-bit range
334
- o.buf = append(o.buf, p.tagcode...)
335
- p.valEnc(o, uint64(x))
336
- return nil
337
-}
338
-
339
-func (o *Buffer) enc_proto3_int32(p *Properties, base structPointer) error {
340
- v := structPointer_Word32Val(base, p.field)
341
- x := int32(word32Val_Get(v)) // permit sign extension to use full 64-bit range
342
- if x == 0 {
343
- return ErrNil
344
- }
345
- o.buf = append(o.buf, p.tagcode...)
346
- p.valEnc(o, uint64(x))
347
- return nil
348
-}
349
-
350
-func size_int32(p *Properties, base structPointer) (n int) {
351
- v := structPointer_Word32(base, p.field)
352
- if word32_IsNil(v) {
353
- return 0
354
- }
355
- x := int32(word32_Get(v)) // permit sign extension to use full 64-bit range
356
- n += len(p.tagcode)
357
- n += p.valSize(uint64(x))
358
- return
359
-}
360
-
361
-func size_proto3_int32(p *Properties, base structPointer) (n int) {
362
- v := structPointer_Word32Val(base, p.field)
363
- x := int32(word32Val_Get(v)) // permit sign extension to use full 64-bit range
364
- if x == 0 {
365
- return 0
366
- }
367
- n += len(p.tagcode)
368
- n += p.valSize(uint64(x))
369
- return
370
-}
371
-
372
-// Encode a uint32.
373
-// Exactly the same as int32, except for no sign extension.
374
-func (o *Buffer) enc_uint32(p *Properties, base structPointer) error {
375
- v := structPointer_Word32(base, p.field)
376
- if word32_IsNil(v) {
377
- return ErrNil
378
- }
379
- x := word32_Get(v)
380
- o.buf = append(o.buf, p.tagcode...)
381
- p.valEnc(o, uint64(x))
382
- return nil
383
-}
384
-
385
-func (o *Buffer) enc_proto3_uint32(p *Properties, base structPointer) error {
386
- v := structPointer_Word32Val(base, p.field)
387
- x := word32Val_Get(v)
388
- if x == 0 {
389
- return ErrNil
390
- }
391
- o.buf = append(o.buf, p.tagcode...)
392
- p.valEnc(o, uint64(x))
393
- return nil
394
-}
395
-
396
-func size_uint32(p *Properties, base structPointer) (n int) {
397
- v := structPointer_Word32(base, p.field)
398
- if word32_IsNil(v) {
399
- return 0
400
- }
401
- x := word32_Get(v)
402
- n += len(p.tagcode)
403
- n += p.valSize(uint64(x))
404
- return
405
-}
406
-
407
-func size_proto3_uint32(p *Properties, base structPointer) (n int) {
408
- v := structPointer_Word32Val(base, p.field)
409
- x := word32Val_Get(v)
410
- if x == 0 {
411
- return 0
412
- }
413
- n += len(p.tagcode)
414
- n += p.valSize(uint64(x))
415
- return
416
-}
417
-
418
-// Encode an int64.
419
-func (o *Buffer) enc_int64(p *Properties, base structPointer) error {
420
- v := structPointer_Word64(base, p.field)
421
- if word64_IsNil(v) {
422
- return ErrNil
423
- }
424
- x := word64_Get(v)
425
- o.buf = append(o.buf, p.tagcode...)
426
- p.valEnc(o, x)
427
- return nil
428
-}
429
-
430
-func (o *Buffer) enc_proto3_int64(p *Properties, base structPointer) error {
431
- v := structPointer_Word64Val(base, p.field)
432
- x := word64Val_Get(v)
433
- if x == 0 {
434
- return ErrNil
435
- }
436
- o.buf = append(o.buf, p.tagcode...)
437
- p.valEnc(o, x)
438
- return nil
439
-}
440
-
441
-func size_int64(p *Properties, base structPointer) (n int) {
442
- v := structPointer_Word64(base, p.field)
443
- if word64_IsNil(v) {
444
- return 0
445
- }
446
- x := word64_Get(v)
447
- n += len(p.tagcode)
448
- n += p.valSize(x)
449
- return
450
-}
451
-
452
-func size_proto3_int64(p *Properties, base structPointer) (n int) {
453
- v := structPointer_Word64Val(base, p.field)
454
- x := word64Val_Get(v)
455
- if x == 0 {
456
- return 0
457
- }
458
- n += len(p.tagcode)
459
- n += p.valSize(x)
460
- return
461
-}
462
-
463
-// Encode a string.
464
-func (o *Buffer) enc_string(p *Properties, base structPointer) error {
465
- v := *structPointer_String(base, p.field)
466
- if v == nil {
467
- return ErrNil
468
- }
469
- x := *v
470
- o.buf = append(o.buf, p.tagcode...)
471
- o.EncodeStringBytes(x)
472
- return nil
473
-}
474
-
475
-func (o *Buffer) enc_proto3_string(p *Properties, base structPointer) error {
476
- v := *structPointer_StringVal(base, p.field)
477
- if v == "" {
478
- return ErrNil
479
- }
480
- o.buf = append(o.buf, p.tagcode...)
481
- o.EncodeStringBytes(v)
482
- return nil
483
-}
484
-
485
-func size_string(p *Properties, base structPointer) (n int) {
486
- v := *structPointer_String(base, p.field)
487
- if v == nil {
488
- return 0
489
- }
490
- x := *v
491
- n += len(p.tagcode)
492
- n += sizeStringBytes(x)
493
- return
494
-}
495
-
496
-func size_proto3_string(p *Properties, base structPointer) (n int) {
497
- v := *structPointer_StringVal(base, p.field)
498
- if v == "" {
499
- return 0
500
- }
501
- n += len(p.tagcode)
502
- n += sizeStringBytes(v)
503
- return
504
-}
505
-
506
-// All protocol buffer fields are nillable, but be careful.
507
-func isNil(v reflect.Value) bool {
508
- switch v.Kind() {
509
- case reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
510
- return v.IsNil()
511
- }
512
- return false
513
-}
514
-
515
-// Encode a message struct.
516
-func (o *Buffer) enc_struct_message(p *Properties, base structPointer) error {
517
- var state errorState
518
- structp := structPointer_GetStructPointer(base, p.field)
519
- if structPointer_IsNil(structp) {
520
- return ErrNil
521
- }
522
-
523
- // Can the object marshal itself?
524
- if p.isMarshaler {
525
- m := structPointer_Interface(structp, p.stype).(Marshaler)
526
- data, err := m.Marshal()
527
- if err != nil && !state.shouldContinue(err, nil) {
528
- return err
529
- }
530
- o.buf = append(o.buf, p.tagcode...)
531
- o.EncodeRawBytes(data)
532
- return nil
533
- }
534
-
535
- o.buf = append(o.buf, p.tagcode...)
536
- return o.enc_len_struct(p.sprop, structp, &state)
537
-}
538
-
539
-func size_struct_message(p *Properties, base structPointer) int {
540
- structp := structPointer_GetStructPointer(base, p.field)
541
- if structPointer_IsNil(structp) {
542
- return 0
543
- }
544
-
545
- // Can the object marshal itself?
546
- if p.isMarshaler {
547
- m := structPointer_Interface(structp, p.stype).(Marshaler)
548
- data, _ := m.Marshal()
549
- n0 := len(p.tagcode)
550
- n1 := sizeRawBytes(data)
551
- return n0 + n1
552
- }
553
-
554
- n0 := len(p.tagcode)
555
- n1 := size_struct(p.sprop, structp)
556
- n2 := sizeVarint(uint64(n1)) // size of encoded length
557
- return n0 + n1 + n2
558
-}
559
-
560
-// Encode a group struct.
561
-func (o *Buffer) enc_struct_group(p *Properties, base structPointer) error {
562
- var state errorState
563
- b := structPointer_GetStructPointer(base, p.field)
564
- if structPointer_IsNil(b) {
565
- return ErrNil
566
- }
567
-
568
- o.EncodeVarint(uint64((p.Tag << 3) | WireStartGroup))
569
- err := o.enc_struct(p.sprop, b)
570
- if err != nil && !state.shouldContinue(err, nil) {
571
- return err
572
- }
573
- o.EncodeVarint(uint64((p.Tag << 3) | WireEndGroup))
574
- return state.err
575
-}
576
-
577
-func size_struct_group(p *Properties, base structPointer) (n int) {
578
- b := structPointer_GetStructPointer(base, p.field)
579
- if structPointer_IsNil(b) {
580
- return 0
581
- }
582
-
583
- n += sizeVarint(uint64((p.Tag << 3) | WireStartGroup))
584
- n += size_struct(p.sprop, b)
585
- n += sizeVarint(uint64((p.Tag << 3) | WireEndGroup))
586
- return
587
-}
588
-
589
-// Encode a slice of bools ([]bool).
590
-func (o *Buffer) enc_slice_bool(p *Properties, base structPointer) error {
591
- s := *structPointer_BoolSlice(base, p.field)
592
- l := len(s)
593
- if l == 0 {
594
- return ErrNil
595
- }
596
- for _, x := range s {
597
- o.buf = append(o.buf, p.tagcode...)
598
- v := uint64(0)
599
- if x {
600
- v = 1
601
- }
602
- p.valEnc(o, v)
603
- }
604
- return nil
605
-}
606
-
607
-func size_slice_bool(p *Properties, base structPointer) int {
608
- s := *structPointer_BoolSlice(base, p.field)
609
- l := len(s)
610
- if l == 0 {
611
- return 0
612
- }
613
- return l * (len(p.tagcode) + 1) // each bool takes exactly one byte
614
-}
615
-
616
-// Encode a slice of bools ([]bool) in packed format.
617
-func (o *Buffer) enc_slice_packed_bool(p *Properties, base structPointer) error {
618
- s := *structPointer_BoolSlice(base, p.field)
619
- l := len(s)
620
- if l == 0 {
621
- return ErrNil
622
- }
623
- o.buf = append(o.buf, p.tagcode...)
624
- o.EncodeVarint(uint64(l)) // each bool takes exactly one byte
625
- for _, x := range s {
626
- v := uint64(0)
627
- if x {
628
- v = 1
629
- }
630
- p.valEnc(o, v)
631
- }
632
- return nil
633
-}
634
-
635
-func size_slice_packed_bool(p *Properties, base structPointer) (n int) {
636
- s := *structPointer_BoolSlice(base, p.field)
637
- l := len(s)
638
- if l == 0 {
639
- return 0
640
- }
641
- n += len(p.tagcode)
642
- n += sizeVarint(uint64(l))
643
- n += l // each bool takes exactly one byte
644
- return
645
-}
646
-
647
-// Encode a slice of bytes ([]byte).
648
-func (o *Buffer) enc_slice_byte(p *Properties, base structPointer) error {
649
- s := *structPointer_Bytes(base, p.field)
650
- if s == nil {
651
- return ErrNil
652
- }
653
- o.buf = append(o.buf, p.tagcode...)
654
- o.EncodeRawBytes(s)
655
- return nil
656
-}
657
-
658
-func (o *Buffer) enc_proto3_slice_byte(p *Properties, base structPointer) error {
659
- s := *structPointer_Bytes(base, p.field)
660
- if len(s) == 0 {
661
- return ErrNil
662
- }
663
- o.buf = append(o.buf, p.tagcode...)
664
- o.EncodeRawBytes(s)
665
- return nil
666
-}
667
-
668
-func size_slice_byte(p *Properties, base structPointer) (n int) {
669
- s := *structPointer_Bytes(base, p.field)
670
- if s == nil {
671
- return 0
672
- }
673
- n += len(p.tagcode)
674
- n += sizeRawBytes(s)
675
- return
676
-}
677
-
678
-func size_proto3_slice_byte(p *Properties, base structPointer) (n int) {
679
- s := *structPointer_Bytes(base, p.field)
680
- if len(s) == 0 {
681
- return 0
682
- }
683
- n += len(p.tagcode)
684
- n += sizeRawBytes(s)
685
- return
686
-}
687
-
688
-// Encode a slice of int32s ([]int32).
689
-func (o *Buffer) enc_slice_int32(p *Properties, base structPointer) error {
690
- s := structPointer_Word32Slice(base, p.field)
691
- l := s.Len()
692
- if l == 0 {
693
- return ErrNil
694
- }
695
- for i := 0; i < l; i++ {
696
- o.buf = append(o.buf, p.tagcode...)
697
- x := int32(s.Index(i)) // permit sign extension to use full 64-bit range
698
- p.valEnc(o, uint64(x))
699
- }
700
- return nil
701
-}
702
-
703
-func size_slice_int32(p *Properties, base structPointer) (n int) {
704
- s := structPointer_Word32Slice(base, p.field)
705
- l := s.Len()
706
- if l == 0 {
707
- return 0
708
- }
709
- for i := 0; i < l; i++ {
710
- n += len(p.tagcode)
711
- x := int32(s.Index(i)) // permit sign extension to use full 64-bit range
712
- n += p.valSize(uint64(x))
713
- }
714
- return
715
-}
716
-
717
-// Encode a slice of int32s ([]int32) in packed format.
718
-func (o *Buffer) enc_slice_packed_int32(p *Properties, base structPointer) error {
719
- s := structPointer_Word32Slice(base, p.field)
720
- l := s.Len()
721
- if l == 0 {
722
- return ErrNil
723
- }
724
- // TODO: Reuse a Buffer.
725
- buf := NewBuffer(nil)
726
- for i := 0; i < l; i++ {
727
- x := int32(s.Index(i)) // permit sign extension to use full 64-bit range
728
- p.valEnc(buf, uint64(x))
729
- }
730
-
731
- o.buf = append(o.buf, p.tagcode...)
732
- o.EncodeVarint(uint64(len(buf.buf)))
733
- o.buf = append(o.buf, buf.buf...)
734
- return nil
735
-}
736
-
737
-func size_slice_packed_int32(p *Properties, base structPointer) (n int) {
738
- s := structPointer_Word32Slice(base, p.field)
739
- l := s.Len()
740
- if l == 0 {
741
- return 0
742
- }
743
- var bufSize int
744
- for i := 0; i < l; i++ {
745
- x := int32(s.Index(i)) // permit sign extension to use full 64-bit range
746
- bufSize += p.valSize(uint64(x))
747
- }
748
-
749
- n += len(p.tagcode)
750
- n += sizeVarint(uint64(bufSize))
751
- n += bufSize
752
- return
753
-}
754
-
755
-// Encode a slice of uint32s ([]uint32).
756
-// Exactly the same as int32, except for no sign extension.
757
-func (o *Buffer) enc_slice_uint32(p *Properties, base structPointer) error {
758
- s := structPointer_Word32Slice(base, p.field)
759
- l := s.Len()
760
- if l == 0 {
761
- return ErrNil
762
- }
763
- for i := 0; i < l; i++ {
764
- o.buf = append(o.buf, p.tagcode...)
765
- x := s.Index(i)
766
- p.valEnc(o, uint64(x))
767
- }
768
- return nil
769
-}
770
-
771
-func size_slice_uint32(p *Properties, base structPointer) (n int) {
772
- s := structPointer_Word32Slice(base, p.field)
773
- l := s.Len()
774
- if l == 0 {
775
- return 0
776
- }
777
- for i := 0; i < l; i++ {
778
- n += len(p.tagcode)
779
- x := s.Index(i)
780
- n += p.valSize(uint64(x))
781
- }
782
- return
783
-}
784
-
785
-// Encode a slice of uint32s ([]uint32) in packed format.
786
-// Exactly the same as int32, except for no sign extension.
787
-func (o *Buffer) enc_slice_packed_uint32(p *Properties, base structPointer) error {
788
- s := structPointer_Word32Slice(base, p.field)
789
- l := s.Len()
790
- if l == 0 {
791
- return ErrNil
792
- }
793
- // TODO: Reuse a Buffer.
794
- buf := NewBuffer(nil)
795
- for i := 0; i < l; i++ {
796
- p.valEnc(buf, uint64(s.Index(i)))
797
- }
798
-
799
- o.buf = append(o.buf, p.tagcode...)
800
- o.EncodeVarint(uint64(len(buf.buf)))
801
- o.buf = append(o.buf, buf.buf...)
802
- return nil
803
-}
804
-
805
-func size_slice_packed_uint32(p *Properties, base structPointer) (n int) {
806
- s := structPointer_Word32Slice(base, p.field)
807
- l := s.Len()
808
- if l == 0 {
809
- return 0
810
- }
811
- var bufSize int
812
- for i := 0; i < l; i++ {
813
- bufSize += p.valSize(uint64(s.Index(i)))
814
- }
815
-
816
- n += len(p.tagcode)
817
- n += sizeVarint(uint64(bufSize))
818
- n += bufSize
819
- return
820
-}
821
-
822
-// Encode a slice of int64s ([]int64).
823
-func (o *Buffer) enc_slice_int64(p *Properties, base structPointer) error {
824
- s := structPointer_Word64Slice(base, p.field)
825
- l := s.Len()
826
- if l == 0 {
827
- return ErrNil
828
- }
829
- for i := 0; i < l; i++ {
830
- o.buf = append(o.buf, p.tagcode...)
831
- p.valEnc(o, s.Index(i))
832
- }
833
- return nil
834
-}
835
-
836
-func size_slice_int64(p *Properties, base structPointer) (n int) {
837
- s := structPointer_Word64Slice(base, p.field)
838
- l := s.Len()
839
- if l == 0 {
840
- return 0
841
- }
842
- for i := 0; i < l; i++ {
843
- n += len(p.tagcode)
844
- n += p.valSize(s.Index(i))
845
- }
846
- return
847
-}
848
-
849
-// Encode a slice of int64s ([]int64) in packed format.
850
-func (o *Buffer) enc_slice_packed_int64(p *Properties, base structPointer) error {
851
- s := structPointer_Word64Slice(base, p.field)
852
- l := s.Len()
853
- if l == 0 {
854
- return ErrNil
855
- }
856
- // TODO: Reuse a Buffer.
857
- buf := NewBuffer(nil)
858
- for i := 0; i < l; i++ {
859
- p.valEnc(buf, s.Index(i))
860
- }
861
-
862
- o.buf = append(o.buf, p.tagcode...)
863
- o.EncodeVarint(uint64(len(buf.buf)))
864
- o.buf = append(o.buf, buf.buf...)
865
- return nil
866
-}
867
-
868
-func size_slice_packed_int64(p *Properties, base structPointer) (n int) {
869
- s := structPointer_Word64Slice(base, p.field)
870
- l := s.Len()
871
- if l == 0 {
872
- return 0
873
- }
874
- var bufSize int
875
- for i := 0; i < l; i++ {
876
- bufSize += p.valSize(s.Index(i))
877
- }
878
-
879
- n += len(p.tagcode)
880
- n += sizeVarint(uint64(bufSize))
881
- n += bufSize
882
- return
883
-}
884
-
885
-// Encode a slice of slice of bytes ([][]byte).
886
-func (o *Buffer) enc_slice_slice_byte(p *Properties, base structPointer) error {
887
- ss := *structPointer_BytesSlice(base, p.field)
888
- l := len(ss)
889
- if l == 0 {
890
- return ErrNil
891
- }
892
- for i := 0; i < l; i++ {
893
- o.buf = append(o.buf, p.tagcode...)
894
- o.EncodeRawBytes(ss[i])
895
- }
896
- return nil
897
-}
898
-
899
-func size_slice_slice_byte(p *Properties, base structPointer) (n int) {
900
- ss := *structPointer_BytesSlice(base, p.field)
901
- l := len(ss)
902
- if l == 0 {
903
- return 0
904
- }
905
- n += l * len(p.tagcode)
906
- for i := 0; i < l; i++ {
907
- n += sizeRawBytes(ss[i])
908
- }
909
- return
910
-}
911
-
912
-// Encode a slice of strings ([]string).
913
-func (o *Buffer) enc_slice_string(p *Properties, base structPointer) error {
914
- ss := *structPointer_StringSlice(base, p.field)
915
- l := len(ss)
916
- for i := 0; i < l; i++ {
917
- o.buf = append(o.buf, p.tagcode...)
918
- o.EncodeStringBytes(ss[i])
919
- }
920
- return nil
921
-}
922
-
923
-func size_slice_string(p *Properties, base structPointer) (n int) {
924
- ss := *structPointer_StringSlice(base, p.field)
925
- l := len(ss)
926
- n += l * len(p.tagcode)
927
- for i := 0; i < l; i++ {
928
- n += sizeStringBytes(ss[i])
929
- }
930
- return
931
-}
932
-
933
-// Encode a slice of message structs ([]*struct).
934
-func (o *Buffer) enc_slice_struct_message(p *Properties, base structPointer) error {
935
- var state errorState
936
- s := structPointer_StructPointerSlice(base, p.field)
937
- l := s.Len()
938
-
939
- for i := 0; i < l; i++ {
940
- structp := s.Index(i)
941
- if structPointer_IsNil(structp) {
942
- return errRepeatedHasNil
943
- }
944
-
945
- // Can the object marshal itself?
946
- if p.isMarshaler {
947
- m := structPointer_Interface(structp, p.stype).(Marshaler)
948
- data, err := m.Marshal()
949
- if err != nil && !state.shouldContinue(err, nil) {
950
- return err
951
- }
952
- o.buf = append(o.buf, p.tagcode...)
953
- o.EncodeRawBytes(data)
954
- continue
955
- }
956
-
957
- o.buf = append(o.buf, p.tagcode...)
958
- err := o.enc_len_struct(p.sprop, structp, &state)
959
- if err != nil && !state.shouldContinue(err, nil) {
960
- if err == ErrNil {
961
- return errRepeatedHasNil
962
- }
963
- return err
964
- }
965
- }
966
- return state.err
967
-}
968
-
969
-func size_slice_struct_message(p *Properties, base structPointer) (n int) {
970
- s := structPointer_StructPointerSlice(base, p.field)
971
- l := s.Len()
972
- n += l * len(p.tagcode)
973
- for i := 0; i < l; i++ {
974
- structp := s.Index(i)
975
- if structPointer_IsNil(structp) {
976
- return // return the size up to this point
977
- }
978
-
979
- // Can the object marshal itself?
980
- if p.isMarshaler {
981
- m := structPointer_Interface(structp, p.stype).(Marshaler)
982
- data, _ := m.Marshal()
983
- n += len(p.tagcode)
984
- n += sizeRawBytes(data)
985
- continue
986
- }
987
-
988
- n0 := size_struct(p.sprop, structp)
989
- n1 := sizeVarint(uint64(n0)) // size of encoded length
990
- n += n0 + n1
991
- }
992
- return
993
-}
994
-
995
-// Encode a slice of group structs ([]*struct).
996
-func (o *Buffer) enc_slice_struct_group(p *Properties, base structPointer) error {
997
- var state errorState
998
- s := structPointer_StructPointerSlice(base, p.field)
999
- l := s.Len()
1000
-
1001
- for i := 0; i < l; i++ {
1002
- b := s.Index(i)
1003
- if structPointer_IsNil(b) {
1004
- return errRepeatedHasNil
1005
- }
1006
-
1007
- o.EncodeVarint(uint64((p.Tag << 3) | WireStartGroup))
1008
-
1009
- err := o.enc_struct(p.sprop, b)
1010
-
1011
- if err != nil && !state.shouldContinue(err, nil) {
1012
- if err == ErrNil {
1013
- return errRepeatedHasNil
1014
- }
1015
- return err
1016
- }
1017
-
1018
- o.EncodeVarint(uint64((p.Tag << 3) | WireEndGroup))
1019
- }
1020
- return state.err
1021
-}
1022
-
1023
-func size_slice_struct_group(p *Properties, base structPointer) (n int) {
1024
- s := structPointer_StructPointerSlice(base, p.field)
1025
- l := s.Len()
1026
-
1027
- n += l * sizeVarint(uint64((p.Tag<<3)|WireStartGroup))
1028
- n += l * sizeVarint(uint64((p.Tag<<3)|WireEndGroup))
1029
- for i := 0; i < l; i++ {
1030
- b := s.Index(i)
1031
- if structPointer_IsNil(b) {
1032
- return // return size up to this point
1033
- }
1034
-
1035
- n += size_struct(p.sprop, b)
1036
- }
1037
- return
1038
-}
1039
-
1040
-// Encode an extension map.
1041
-func (o *Buffer) enc_map(p *Properties, base structPointer) error {
1042
- v := *structPointer_ExtMap(base, p.field)
1043
- if err := encodeExtensionMap(v); err != nil {
1044
- return err
1045
- }
1046
- // Fast-path for common cases: zero or one extensions.
1047
- if len(v) <= 1 {
1048
- for _, e := range v {
1049
- o.buf = append(o.buf, e.enc...)
1050
- }
1051
- return nil
1052
- }
1053
-
1054
- // Sort keys to provide a deterministic encoding.
1055
- keys := make([]int, 0, len(v))
1056
- for k := range v {
1057
- keys = append(keys, int(k))
1058
- }
1059
- sort.Ints(keys)
1060
-
1061
- for _, k := range keys {
1062
- o.buf = append(o.buf, v[int32(k)].enc...)
1063
- }
1064
- return nil
1065
-}
1066
-
1067
-func size_map(p *Properties, base structPointer) int {
1068
- v := *structPointer_ExtMap(base, p.field)
1069
- return sizeExtensionMap(v)
1070
-}
1071
-
1072
-// Encode a map field.
1073
-func (o *Buffer) enc_new_map(p *Properties, base structPointer) error {
1074
- var state errorState // XXX: or do we need to plumb this through?
1075
-
1076
- /*
1077
- A map defined as
1078
- map<key_type, value_type> map_field = N;
1079
- is encoded in the same way as
1080
- message MapFieldEntry {
1081
- key_type key = 1;
1082
- value_type value = 2;
1083
- }
1084
- repeated MapFieldEntry map_field = N;
1085
- */
1086
-
1087
- v := structPointer_Map(base, p.field, p.mtype).Elem() // map[K]V
1088
- if v.Len() == 0 {
1089
- return nil
1090
- }
1091
-
1092
- keycopy, valcopy, keybase, valbase := mapEncodeScratch(p.mtype)
1093
-
1094
- enc := func() error {
1095
- if err := p.mkeyprop.enc(o, p.mkeyprop, keybase); err != nil {
1096
- return err
1097
- }
1098
- if err := p.mvalprop.enc(o, p.mvalprop, valbase); err != nil {
1099
- return err
1100
- }
1101
- return nil
1102
- }
1103
-
1104
- keys := v.MapKeys()
1105
- sort.Sort(mapKeys(keys))
1106
- for _, key := range keys {
1107
- val := v.MapIndex(key)
1108
-
1109
- // The only illegal map entry values are nil message pointers.
1110
- if val.Kind() == reflect.Ptr && val.IsNil() {
1111
- return errors.New("proto: map has nil element")
1112
- }
1113
-
1114
- keycopy.Set(key)
1115
- valcopy.Set(val)
1116
-
1117
- o.buf = append(o.buf, p.tagcode...)
1118
- if err := o.enc_len_thing(enc, &state); err != nil {
1119
- return err
1120
- }
1121
- }
1122
- return nil
1123
-}
1124
-
1125
-func size_new_map(p *Properties, base structPointer) int {
1126
- v := structPointer_Map(base, p.field, p.mtype).Elem() // map[K]V
1127
-
1128
- keycopy, valcopy, keybase, valbase := mapEncodeScratch(p.mtype)
1129
-
1130
- n := 0
1131
- for _, key := range v.MapKeys() {
1132
- val := v.MapIndex(key)
1133
- keycopy.Set(key)
1134
- valcopy.Set(val)
1135
-
1136
- // Tag codes for key and val are the responsibility of the sub-sizer.
1137
- keysize := p.mkeyprop.size(p.mkeyprop, keybase)
1138
- valsize := p.mvalprop.size(p.mvalprop, valbase)
1139
- entry := keysize + valsize
1140
- // Add on tag code and length of map entry itself.
1141
- n += len(p.tagcode) + sizeVarint(uint64(entry)) + entry
1142
- }
1143
- return n
1144
-}
1145
-
1146
-// mapEncodeScratch returns a new reflect.Value matching the map's value type,
1147
-// and a structPointer suitable for passing to an encoder or sizer.
1148
-func mapEncodeScratch(mapType reflect.Type) (keycopy, valcopy reflect.Value, keybase, valbase structPointer) {
1149
- // Prepare addressable doubly-indirect placeholders for the key and value types.
1150
- // This is needed because the element-type encoders expect **T, but the map iteration produces T.
1151
-
1152
- keycopy = reflect.New(mapType.Key()).Elem() // addressable K
1153
- keyptr := reflect.New(reflect.PtrTo(keycopy.Type())).Elem() // addressable *K
1154
- keyptr.Set(keycopy.Addr()) //
1155
- keybase = toStructPointer(keyptr.Addr()) // **K
1156
-
1157
- // Value types are more varied and require special handling.
1158
- switch mapType.Elem().Kind() {
1159
- case reflect.Slice:
1160
- // []byte
1161
- var dummy []byte
1162
- valcopy = reflect.ValueOf(&dummy).Elem() // addressable []byte
1163
- valbase = toStructPointer(valcopy.Addr())
1164
- case reflect.Ptr:
1165
- // message; the generated field type is map[K]*Msg (so V is *Msg),
1166
- // so we only need one level of indirection.
1167
- valcopy = reflect.New(mapType.Elem()).Elem() // addressable V
1168
- valbase = toStructPointer(valcopy.Addr())
1169
- default:
1170
- // everything else
1171
- valcopy = reflect.New(mapType.Elem()).Elem() // addressable V
1172
- valptr := reflect.New(reflect.PtrTo(valcopy.Type())).Elem() // addressable *V
1173
- valptr.Set(valcopy.Addr()) //
1174
- valbase = toStructPointer(valptr.Addr()) // **V
1175
- }
1176
- return
1177
-}
1178
-
1179
-// Encode a struct.
1180
-func (o *Buffer) enc_struct(prop *StructProperties, base structPointer) error {
1181
- var state errorState
1182
- // Encode fields in tag order so that decoders may use optimizations
1183
- // that depend on the ordering.
1184
- // https://developers.google.com/protocol-buffers/docs/encoding#order
1185
- for _, i := range prop.order {
1186
- p := prop.Prop[i]
1187
- if p.enc != nil {
1188
- err := p.enc(o, p, base)
1189
- if err != nil {
1190
- if err == ErrNil {
1191
- if p.Required && state.err == nil {
1192
- state.err = &RequiredNotSetError{p.Name}
1193
- }
1194
- } else if err == errRepeatedHasNil {
1195
- // Give more context to nil values in repeated fields.
1196
- return errors.New("repeated field " + p.OrigName + " has nil element")
1197
- } else if !state.shouldContinue(err, p) {
1198
- return err
1199
- }
1200
- }
1201
- }
1202
- }
1203
-
1204
- // Add unrecognized fields at the end.
1205
- if prop.unrecField.IsValid() {
1206
- v := *structPointer_Bytes(base, prop.unrecField)
1207
- if len(v) > 0 {
1208
- o.buf = append(o.buf, v...)
1209
- }
1210
- }
1211
-
1212
- return state.err
1213
-}
1214
-
1215
-func size_struct(prop *StructProperties, base structPointer) (n int) {
1216
- for _, i := range prop.order {
1217
- p := prop.Prop[i]
1218
- if p.size != nil {
1219
- n += p.size(p, base)
1220
- }
1221
- }
1222
-
1223
- // Add unrecognized fields at the end.
1224
- if prop.unrecField.IsValid() {
1225
- v := *structPointer_Bytes(base, prop.unrecField)
1226
- n += len(v)
1227
- }
1228
-
1229
- return
1230
-}
1231
-
1232
-var zeroes [20]byte // longer than any conceivable sizeVarint
1233
-
1234
-// Encode a struct, preceded by its encoded length (as a varint).
1235
-func (o *Buffer) enc_len_struct(prop *StructProperties, base structPointer, state *errorState) error {
1236
- return o.enc_len_thing(func() error { return o.enc_struct(prop, base) }, state)
1237
-}
1238
-
1239
-// Encode something, preceded by its encoded length (as a varint).
1240
-func (o *Buffer) enc_len_thing(enc func() error, state *errorState) error {
1241
- iLen := len(o.buf)
1242
- o.buf = append(o.buf, 0, 0, 0, 0) // reserve four bytes for length
1243
- iMsg := len(o.buf)
1244
- err := enc()
1245
- if err != nil && !state.shouldContinue(err, nil) {
1246
- return err
1247
- }
1248
- lMsg := len(o.buf) - iMsg
1249
- lLen := sizeVarint(uint64(lMsg))
1250
- switch x := lLen - (iMsg - iLen); {
1251
- case x > 0: // actual length is x bytes larger than the space we reserved
1252
- // Move msg x bytes right.
1253
- o.buf = append(o.buf, zeroes[:x]...)
1254
- copy(o.buf[iMsg+x:], o.buf[iMsg:iMsg+lMsg])
1255
- case x < 0: // actual length is x bytes smaller than the space we reserved
1256
- // Move msg x bytes left.
1257
- copy(o.buf[iMsg+x:], o.buf[iMsg:iMsg+lMsg])
1258
- o.buf = o.buf[:len(o.buf)+x] // x is negative
1259
- }
1260
- // Encode the length in the reserved space.
1261
- o.buf = o.buf[:iLen]
1262
- o.EncodeVarint(uint64(lMsg))
1263
- o.buf = o.buf[:len(o.buf)+lMsg]
1264
- return state.err
1265
-}
1266
-
1267
-// errorState maintains the first error that occurs and updates that error
1268
-// with additional context.
1269
-type errorState struct {
1270
- err error
1271
-}
1272
-
1273
-// shouldContinue reports whether encoding should continue upon encountering the
1274
-// given error. If the error is RequiredNotSetError, shouldContinue returns true
1275
-// and, if this is the first appearance of that error, remembers it for future
1276
-// reporting.
1277
-//
1278
-// If prop is not nil, it may update any error with additional context about the
1279
-// field with the error.
1280
-func (s *errorState) shouldContinue(err error, prop *Properties) bool {
1281
- // Ignore unset required fields.
1282
- reqNotSet, ok := err.(*RequiredNotSetError)
1283
- if !ok {
1284
- return false
1285
- }
1286
- if s.err == nil {
1287
- if prop != nil {
1288
- err = &RequiredNotSetError{prop.Name + "." + reqNotSet.field}
1289
- }
1290
- s.err = err
1291
- }
1292
- return true
1293
-}
Godeps/_workspace/src/github.com/golang/protobuf/proto/equal.go
deleted
-256
@@ -1,256 +0,0 @@
1
-// Go support for Protocol Buffers - Google's data interchange format
2
-//
3
-// Copyright 2011 The Go Authors. All rights reserved.
4
-// https://github.com/golang/protobuf
5
-//
6
-// Redistribution and use in source and binary forms, with or without
7
-// modification, are permitted provided that the following conditions are
8
-// met:
9
-//
10
-// * Redistributions of source code must retain the above copyright
11
-// notice, this list of conditions and the following disclaimer.
12
-// * Redistributions in binary form must reproduce the above
13
-// copyright notice, this list of conditions and the following disclaimer
14
-// in the documentation and/or other materials provided with the
15
-// distribution.
16
-// * Neither the name of Google Inc. nor the names of its
17
-// contributors may be used to endorse or promote products derived from
18
-// this software without specific prior written permission.
19
-//
20
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
-// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
-// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23
-// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24
-// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25
-// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26
-// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27
-// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28
-// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29
-// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
-// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
-
32
-// Protocol buffer comparison.
33
-// TODO: MessageSet.
34
-
35
-package proto
36
-
37
-import (
38
- "bytes"
39
- "log"
40
- "reflect"
41
- "strings"
42
-)
43
-
44
-/*
45
-Equal returns true iff protocol buffers a and b are equal.
46
-The arguments must both be pointers to protocol buffer structs.
47
-
48
-Equality is defined in this way:
49
- - Two messages are equal iff they are the same type,
50
- corresponding fields are equal, unknown field sets
51
- are equal, and extensions sets are equal.
52
- - Two set scalar fields are equal iff their values are equal.
53
- If the fields are of a floating-point type, remember that
54
- NaN != x for all x, including NaN.
55
- - Two repeated fields are equal iff their lengths are the same,
56
- and their corresponding elements are equal (a "bytes" field,
57
- although represented by []byte, is not a repeated field)
58
- - Two unset fields are equal.
59
- - Two unknown field sets are equal if their current
60
- encoded state is equal.
61
- - Two extension sets are equal iff they have corresponding
62
- elements that are pairwise equal.
63
- - Every other combination of things are not equal.
64
-
65
-The return value is undefined if a and b are not protocol buffers.
66
-*/
67
-func Equal(a, b Message) bool {
68
- if a == nil || b == nil {
69
- return a == b
70
- }
71
- v1, v2 := reflect.ValueOf(a), reflect.ValueOf(b)
72
- if v1.Type() != v2.Type() {
73
- return false
74
- }
75
- if v1.Kind() == reflect.Ptr {
76
- if v1.IsNil() {
77
- return v2.IsNil()
78
- }
79
- if v2.IsNil() {
80
- return false
81
- }
82
- v1, v2 = v1.Elem(), v2.Elem()
83
- }
84
- if v1.Kind() != reflect.Struct {
85
- return false
86
- }
87
- return equalStruct(v1, v2)
88
-}
89
-
90
-// v1 and v2 are known to have the same type.
91
-func equalStruct(v1, v2 reflect.Value) bool {
92
- for i := 0; i < v1.NumField(); i++ {
93
- f := v1.Type().Field(i)
94
- if strings.HasPrefix(f.Name, "XXX_") {
95
- continue
96
- }
97
- f1, f2 := v1.Field(i), v2.Field(i)
98
- if f.Type.Kind() == reflect.Ptr {
99
- if n1, n2 := f1.IsNil(), f2.IsNil(); n1 && n2 {
100
- // both unset
101
- continue
102
- } else if n1 != n2 {
103
- // set/unset mismatch
104
- return false
105
- }
106
- b1, ok := f1.Interface().(raw)
107
- if ok {
108
- b2 := f2.Interface().(raw)
109
- // RawMessage
110
- if !bytes.Equal(b1.Bytes(), b2.Bytes()) {
111
- return false
112
- }
113
- continue
114
- }
115
- f1, f2 = f1.Elem(), f2.Elem()
116
- }
117
- if !equalAny(f1, f2) {
118
- return false
119
- }
120
- }
121
-
122
- if em1 := v1.FieldByName("XXX_extensions"); em1.IsValid() {
123
- em2 := v2.FieldByName("XXX_extensions")
124
- if !equalExtensions(v1.Type(), em1.Interface().(map[int32]Extension), em2.Interface().(map[int32]Extension)) {
125
- return false
126
- }
127
- }
128
-
129
- uf := v1.FieldByName("XXX_unrecognized")
130
- if !uf.IsValid() {
131
- return true
132
- }
133
-
134
- u1 := uf.Bytes()
135
- u2 := v2.FieldByName("XXX_unrecognized").Bytes()
136
- if !bytes.Equal(u1, u2) {
137
- return false
138
- }
139
-
140
- return true
141
-}
142
-
143
-// v1 and v2 are known to have the same type.
144
-func equalAny(v1, v2 reflect.Value) bool {
145
- if v1.Type() == protoMessageType {
146
- m1, _ := v1.Interface().(Message)
147
- m2, _ := v2.Interface().(Message)
148
- return Equal(m1, m2)
149
- }
150
- switch v1.Kind() {
151
- case reflect.Bool:
152
- return v1.Bool() == v2.Bool()
153
- case reflect.Float32, reflect.Float64:
154
- return v1.Float() == v2.Float()
155
- case reflect.Int32, reflect.Int64:
156
- return v1.Int() == v2.Int()
157
- case reflect.Map:
158
- if v1.Len() != v2.Len() {
159
- return false
160
- }
161
- for _, key := range v1.MapKeys() {
162
- val2 := v2.MapIndex(key)
163
- if !val2.IsValid() {
164
- // This key was not found in the second map.
165
- return false
166
- }
167
- if !equalAny(v1.MapIndex(key), val2) {
168
- return false
169
- }
170
- }
171
- return true
172
- case reflect.Ptr:
173
- return equalAny(v1.Elem(), v2.Elem())
174
- case reflect.Slice:
175
- if v1.Type().Elem().Kind() == reflect.Uint8 {
176
- // short circuit: []byte
177
- if v1.IsNil() != v2.IsNil() {
178
- return false
179
- }
180
- return bytes.Equal(v1.Interface().([]byte), v2.Interface().([]byte))
181
- }
182
-
183
- if v1.Len() != v2.Len() {
184
- return false
185
- }
186
- for i := 0; i < v1.Len(); i++ {
187
- if !equalAny(v1.Index(i), v2.Index(i)) {
188
- return false
189
- }
190
- }
191
- return true
192
- case reflect.String:
193
- return v1.Interface().(string) == v2.Interface().(string)
194
- case reflect.Struct:
195
- return equalStruct(v1, v2)
196
- case reflect.Uint32, reflect.Uint64:
197
- return v1.Uint() == v2.Uint()
198
- }
199
-
200
- // unknown type, so not a protocol buffer
201
- log.Printf("proto: don't know how to compare %v", v1)
202
- return false
203
-}
204
-
205
-// base is the struct type that the extensions are based on.
206
-// em1 and em2 are extension maps.
207
-func equalExtensions(base reflect.Type, em1, em2 map[int32]Extension) bool {
208
- if len(em1) != len(em2) {
209
- return false
210
- }
211
-
212
- for extNum, e1 := range em1 {
213
- e2, ok := em2[extNum]
214
- if !ok {
215
- return false
216
- }
217
-
218
- m1, m2 := e1.value, e2.value
219
-
220
- if m1 != nil && m2 != nil {
221
- // Both are unencoded.
222
- if !equalAny(reflect.ValueOf(m1), reflect.ValueOf(m2)) {
223
- return false
224
- }
225
- continue
226
- }
227
-
228
- // At least one is encoded. To do a semantically correct comparison
229
- // we need to unmarshal them first.
230
- var desc *ExtensionDesc
231
- if m := extensionMaps[base]; m != nil {
232
- desc = m[extNum]
233
- }
234
- if desc == nil {
235
- log.Printf("proto: don't know how to compare extension %d of %v", extNum, base)
236
- continue
237
- }
238
- var err error
239
- if m1 == nil {
240
- m1, err = decodeExtension(e1.enc, desc)
241
- }
242
- if m2 == nil && err == nil {
243
- m2, err = decodeExtension(e2.enc, desc)
244
- }
245
- if err != nil {
246
- // The encoded form is invalid.
247
- log.Printf("proto: badly encoded extension %d of %v: %v", extNum, base, err)
248
- return false
249
- }
250
- if !equalAny(reflect.ValueOf(m1), reflect.ValueOf(m2)) {
251
- return false
252
- }
253
- }
254
-
255
- return true
256
-}
Godeps/_workspace/src/github.com/golang/protobuf/proto/equal_test.go
deleted
-191
@@ -1,191 +0,0 @@
1
-// Go support for Protocol Buffers - Google's data interchange format
2
-//
3
-// Copyright 2011 The Go Authors. All rights reserved.
4
-// https://github.com/golang/protobuf
5
-//
6
-// Redistribution and use in source and binary forms, with or without
7
-// modification, are permitted provided that the following conditions are
8
-// met:
9
-//
10
-// * Redistributions of source code must retain the above copyright
11
-// notice, this list of conditions and the following disclaimer.
12
-// * Redistributions in binary form must reproduce the above
13
-// copyright notice, this list of conditions and the following disclaimer
14
-// in the documentation and/or other materials provided with the
15
-// distribution.
16
-// * Neither the name of Google Inc. nor the names of its
17
-// contributors may be used to endorse or promote products derived from
18
-// this software without specific prior written permission.
19
-//
20
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
-// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
-// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23
-// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24
-// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25
-// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26
-// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27
-// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28
-// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29
-// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
-// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
-
32
-package proto_test
33
-
34
-import (
35
- "testing"
36
-
37
- . "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/golang/protobuf/proto"
38
- pb "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/golang/protobuf/proto/testdata"
39
-)
40
-
41
-// Four identical base messages.
42
-// The init function adds extensions to some of them.
43
-var messageWithoutExtension = &pb.MyMessage{Count: Int32(7)}
44
-var messageWithExtension1a = &pb.MyMessage{Count: Int32(7)}
45
-var messageWithExtension1b = &pb.MyMessage{Count: Int32(7)}
46
-var messageWithExtension2 = &pb.MyMessage{Count: Int32(7)}
47
-
48
-// Two messages with non-message extensions.
49
-var messageWithInt32Extension1 = &pb.MyMessage{Count: Int32(8)}
50
-var messageWithInt32Extension2 = &pb.MyMessage{Count: Int32(8)}
51
-
52
-func init() {
53
- ext1 := &pb.Ext{Data: String("Kirk")}
54
- ext2 := &pb.Ext{Data: String("Picard")}
55
-
56
- // messageWithExtension1a has ext1, but never marshals it.
57
- if err := SetExtension(messageWithExtension1a, pb.E_Ext_More, ext1); err != nil {
58
- panic("SetExtension on 1a failed: " + err.Error())
59
- }
60
-
61
- // messageWithExtension1b is the unmarshaled form of messageWithExtension1a.
62
- if err := SetExtension(messageWithExtension1b, pb.E_Ext_More, ext1); err != nil {
63
- panic("SetExtension on 1b failed: " + err.Error())
64
- }
65
- buf, err := Marshal(messageWithExtension1b)
66
- if err != nil {
67
- panic("Marshal of 1b failed: " + err.Error())
68
- }
69
- messageWithExtension1b.Reset()
70
- if err := Unmarshal(buf, messageWithExtension1b); err != nil {
71
- panic("Unmarshal of 1b failed: " + err.Error())
72
- }
73
-
74
- // messageWithExtension2 has ext2.
75
- if err := SetExtension(messageWithExtension2, pb.E_Ext_More, ext2); err != nil {
76
- panic("SetExtension on 2 failed: " + err.Error())
77
- }
78
-
79
- if err := SetExtension(messageWithInt32Extension1, pb.E_Ext_Number, Int32(23)); err != nil {
80
- panic("SetExtension on Int32-1 failed: " + err.Error())
81
- }
82
- if err := SetExtension(messageWithInt32Extension1, pb.E_Ext_Number, Int32(24)); err != nil {
83
- panic("SetExtension on Int32-2 failed: " + err.Error())
84
- }
85
-}
86
-
87
-var EqualTests = []struct {
88
- desc string
89
- a, b Message
90
- exp bool
91
-}{
92
- {"different types", &pb.GoEnum{}, &pb.GoTestField{}, false},
93
- {"equal empty", &pb.GoEnum{}, &pb.GoEnum{}, true},
94
- {"nil vs nil", nil, nil, true},
95
- {"typed nil vs typed nil", (*pb.GoEnum)(nil), (*pb.GoEnum)(nil), true},
96
- {"typed nil vs empty", (*pb.GoEnum)(nil), &pb.GoEnum{}, false},
97
- {"different typed nil", (*pb.GoEnum)(nil), (*pb.GoTestField)(nil), false},
98
-
99
- {"one set field, one unset field", &pb.GoTestField{Label: String("foo")}, &pb.GoTestField{}, false},
100
- {"one set field zero, one unset field", &pb.GoTest{Param: Int32(0)}, &pb.GoTest{}, false},
101
- {"different set fields", &pb.GoTestField{Label: String("foo")}, &pb.GoTestField{Label: String("bar")}, false},
102
- {"equal set", &pb.GoTestField{Label: String("foo")}, &pb.GoTestField{Label: String("foo")}, true},
103
-
104
- {"repeated, one set", &pb.GoTest{F_Int32Repeated: []int32{2, 3}}, &pb.GoTest{}, false},
105
- {"repeated, different length", &pb.GoTest{F_Int32Repeated: []int32{2, 3}}, &pb.GoTest{F_Int32Repeated: []int32{2}}, false},
106
- {"repeated, different value", &pb.GoTest{F_Int32Repeated: []int32{2}}, &pb.GoTest{F_Int32Repeated: []int32{3}}, false},
107
- {"repeated, equal", &pb.GoTest{F_Int32Repeated: []int32{2, 4}}, &pb.GoTest{F_Int32Repeated: []int32{2, 4}}, true},
108
- {"repeated, nil equal nil", &pb.GoTest{F_Int32Repeated: nil}, &pb.GoTest{F_Int32Repeated: nil}, true},
109
- {"repeated, nil equal empty", &pb.GoTest{F_Int32Repeated: nil}, &pb.GoTest{F_Int32Repeated: []int32{}}, true},
110
- {"repeated, empty equal nil", &pb.GoTest{F_Int32Repeated: []int32{}}, &pb.GoTest{F_Int32Repeated: nil}, true},
111
-
112
- {
113
- "nested, different",
114
- &pb.GoTest{RequiredField: &pb.GoTestField{Label: String("foo")}},
115
- &pb.GoTest{RequiredField: &pb.GoTestField{Label: String("bar")}},
116
- false,
117
- },
118
- {
119
- "nested, equal",
120
- &pb.GoTest{RequiredField: &pb.GoTestField{Label: String("wow")}},
121
- &pb.GoTest{RequiredField: &pb.GoTestField{Label: String("wow")}},
122
- true,
123
- },
124
-
125
- {"bytes", &pb.OtherMessage{Value: []byte("foo")}, &pb.OtherMessage{Value: []byte("foo")}, true},
126
- {"bytes, empty", &pb.OtherMessage{Value: []byte{}}, &pb.OtherMessage{Value: []byte{}}, true},
127
- {"bytes, empty vs nil", &pb.OtherMessage{Value: []byte{}}, &pb.OtherMessage{Value: nil}, false},
128
- {
129
- "repeated bytes",
130
- &pb.MyMessage{RepBytes: [][]byte{[]byte("sham"), []byte("wow")}},
131
- &pb.MyMessage{RepBytes: [][]byte{[]byte("sham"), []byte("wow")}},
132
- true,
133
- },
134
-
135
- {"extension vs. no extension", messageWithoutExtension, messageWithExtension1a, false},
136
- {"extension vs. same extension", messageWithExtension1a, messageWithExtension1b, true},
137
- {"extension vs. different extension", messageWithExtension1a, messageWithExtension2, false},
138
-
139
- {"int32 extension vs. itself", messageWithInt32Extension1, messageWithInt32Extension1, true},
140
- {"int32 extension vs. a different int32", messageWithInt32Extension1, messageWithInt32Extension2, false},
141
-
142
- {
143
- "message with group",
144
- &pb.MyMessage{
145
- Count: Int32(1),
146
- Somegroup: &pb.MyMessage_SomeGroup{
147
- GroupField: Int32(5),
148
- },
149
- },
150
- &pb.MyMessage{
151
- Count: Int32(1),
152
- Somegroup: &pb.MyMessage_SomeGroup{
153
- GroupField: Int32(5),
154
- },
155
- },
156
- true,
157
- },
158
-
159
- {
160
- "map same",
161
- &pb.MessageWithMap{NameMapping: map[int32]string{1: "Ken"}},
162
- &pb.MessageWithMap{NameMapping: map[int32]string{1: "Ken"}},
163
- true,
164
- },
165
- {
166
- "map different entry",
167
- &pb.MessageWithMap{NameMapping: map[int32]string{1: "Ken"}},
168
- &pb.MessageWithMap{NameMapping: map[int32]string{2: "Rob"}},
169
- false,
170
- },
171
- {
172
- "map different key only",
173
- &pb.MessageWithMap{NameMapping: map[int32]string{1: "Ken"}},
174
- &pb.MessageWithMap{NameMapping: map[int32]string{2: "Ken"}},
175
- false,
176
- },
177
- {
178
- "map different value only",
179
- &pb.MessageWithMap{NameMapping: map[int32]string{1: "Ken"}},
180
- &pb.MessageWithMap{NameMapping: map[int32]string{1: "Rob"}},
181
- false,
182
- },
183
-}
184
-
185
-func TestEqual(t *testing.T) {
186
- for _, tc := range EqualTests {
187
- if res := Equal(tc.a, tc.b); res != tc.exp {
188
- t.Errorf("%v: Equal(%v, %v) = %v, want %v", tc.desc, tc.a, tc.b, res, tc.exp)
189
- }
190
- }
191
-}
Godeps/_workspace/src/github.com/golang/protobuf/proto/extensions.go
deleted
-400
@@ -1,400 +0,0 @@
1
-// Go support for Protocol Buffers - Google's data interchange format
2
-//
3
-// Copyright 2010 The Go Authors. All rights reserved.
4
-// https://github.com/golang/protobuf
5
-//
6
-// Redistribution and use in source and binary forms, with or without
7
-// modification, are permitted provided that the following conditions are
8
-// met:
9
-//
10
-// * Redistributions of source code must retain the above copyright
11
-// notice, this list of conditions and the following disclaimer.
12
-// * Redistributions in binary form must reproduce the above
13
-// copyright notice, this list of conditions and the following disclaimer
14
-// in the documentation and/or other materials provided with the
15
-// distribution.
16
-// * Neither the name of Google Inc. nor the names of its
17
-// contributors may be used to endorse or promote products derived from
18
-// this software without specific prior written permission.
19
-//
20
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
-// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
-// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23
-// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24
-// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25
-// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26
-// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27
-// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28
-// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29
-// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
-// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
-
32
-package proto
33
-
34
-/*
35
- * Types and routines for supporting protocol buffer extensions.
36
- */
37
-
38
-import (
39
- "errors"
40
- "fmt"
41
- "reflect"
42
- "strconv"
43
- "sync"
44
-)
45
-
46
-// ErrMissingExtension is the error returned by GetExtension if the named extension is not in the message.
47
-var ErrMissingExtension = errors.New("proto: missing extension")
48
-
49
-// ExtensionRange represents a range of message extensions for a protocol buffer.
50
-// Used in code generated by the protocol compiler.
51
-type ExtensionRange struct {
52
- Start, End int32 // both inclusive
53
-}
54
-
55
-// extendableProto is an interface implemented by any protocol buffer that may be extended.
56
-type extendableProto interface {
57
- Message
58
- ExtensionRangeArray() []ExtensionRange
59
- ExtensionMap() map[int32]Extension
60
-}
61
-
62
-var extendableProtoType = reflect.TypeOf((*extendableProto)(nil)).Elem()
63
-
64
-// ExtensionDesc represents an extension specification.
65
-// Used in generated code from the protocol compiler.
66
-type ExtensionDesc struct {
67
- ExtendedType Message // nil pointer to the type that is being extended
68
- ExtensionType interface{} // nil pointer to the extension type
69
- Field int32 // field number
70
- Name string // fully-qualified name of extension, for text formatting
71
- Tag string // protobuf tag style
72
-}
73
-
74
-func (ed *ExtensionDesc) repeated() bool {
75
- t := reflect.TypeOf(ed.ExtensionType)
76
- return t.Kind() == reflect.Slice && t.Elem().Kind() != reflect.Uint8
77
-}
78
-
79
-// Extension represents an extension in a message.
80
-type Extension struct {
81
- // When an extension is stored in a message using SetExtension
82
- // only desc and value are set. When the message is marshaled
83
- // enc will be set to the encoded form of the message.
84
- //
85
- // When a message is unmarshaled and contains extensions, each
86
- // extension will have only enc set. When such an extension is
87
- // accessed using GetExtension (or GetExtensions) desc and value
88
- // will be set.
89
- desc *ExtensionDesc
90
- value interface{}
91
- enc []byte
92
-}
93
-
94
-// SetRawExtension is for testing only.
95
-func SetRawExtension(base extendableProto, id int32, b []byte) {
96
- base.ExtensionMap()[id] = Extension{enc: b}
97
-}
98
-
99
-// isExtensionField returns true iff the given field number is in an extension range.
100
-func isExtensionField(pb extendableProto, field int32) bool {
101
- for _, er := range pb.ExtensionRangeArray() {
102
- if er.Start <= field && field <= er.End {
103
- return true
104
- }
105
- }
106
- return false
107
-}
108
-
109
-// checkExtensionTypes checks that the given extension is valid for pb.
110
-func checkExtensionTypes(pb extendableProto, extension *ExtensionDesc) error {
111
- // Check the extended type.
112
- if a, b := reflect.TypeOf(pb), reflect.TypeOf(extension.ExtendedType); a != b {
113
- return errors.New("proto: bad extended type; " + b.String() + " does not extend " + a.String())
114
- }
115
- // Check the range.
116
- if !isExtensionField(pb, extension.Field) {
117
- return errors.New("proto: bad extension number; not in declared ranges")
118
- }
119
- return nil
120
-}
121
-
122
-// extPropKey is sufficient to uniquely identify an extension.
123
-type extPropKey struct {
124
- base reflect.Type
125
- field int32
126
-}
127
-
128
-var extProp = struct {
129
- sync.RWMutex
130
- m map[extPropKey]*Properties
131
-}{
132
- m: make(map[extPropKey]*Properties),
133
-}
134
-
135
-func extensionProperties(ed *ExtensionDesc) *Properties {
136
- key := extPropKey{base: reflect.TypeOf(ed.ExtendedType), field: ed.Field}
137
-
138
- extProp.RLock()
139
- if prop, ok := extProp.m[key]; ok {
140
- extProp.RUnlock()
141
- return prop
142
- }
143
- extProp.RUnlock()
144
-
145
- extProp.Lock()
146
- defer extProp.Unlock()
147
- // Check again.
148
- if prop, ok := extProp.m[key]; ok {
149
- return prop
150
- }
151
-
152
- prop := new(Properties)
153
- prop.Init(reflect.TypeOf(ed.ExtensionType), "unknown_name", ed.Tag, nil)
154
- extProp.m[key] = prop
155
- return prop
156
-}
157
-
158
-// encodeExtensionMap encodes any unmarshaled (unencoded) extensions in m.
159
-func encodeExtensionMap(m map[int32]Extension) error {
160
- for k, e := range m {
161
- if e.value == nil || e.desc == nil {
162
- // Extension is only in its encoded form.
163
- continue
164
- }
165
-
166
- // We don't skip extensions that have an encoded form set,
167
- // because the extension value may have been mutated after
168
- // the last time this function was called.
169
-
170
- et := reflect.TypeOf(e.desc.ExtensionType)
171
- props := extensionProperties(e.desc)
172
-
173
- p := NewBuffer(nil)
174
- // If e.value has type T, the encoder expects a *struct{ X T }.
175
- // Pass a *T with a zero field and hope it all works out.
176
- x := reflect.New(et)
177
- x.Elem().Set(reflect.ValueOf(e.value))
178
- if err := props.enc(p, props, toStructPointer(x)); err != nil {
179
- return err
180
- }
181
- e.enc = p.buf
182
- m[k] = e
183
- }
184
- return nil
185
-}
186
-
187
-func sizeExtensionMap(m map[int32]Extension) (n int) {
188
- for _, e := range m {
189
- if e.value == nil || e.desc == nil {
190
- // Extension is only in its encoded form.
191
- n += len(e.enc)
192
- continue
193
- }
194
-
195
- // We don't skip extensions that have an encoded form set,
196
- // because the extension value may have been mutated after
197
- // the last time this function was called.
198
-
199
- et := reflect.TypeOf(e.desc.ExtensionType)
200
- props := extensionProperties(e.desc)
201
-
202
- // If e.value has type T, the encoder expects a *struct{ X T }.
203
- // Pass a *T with a zero field and hope it all works out.
204
- x := reflect.New(et)
205
- x.Elem().Set(reflect.ValueOf(e.value))
206
- n += props.size(props, toStructPointer(x))
207
- }
208
- return
209
-}
210
-
211
-// HasExtension returns whether the given extension is present in pb.
212
-func HasExtension(pb extendableProto, extension *ExtensionDesc) bool {
213
- // TODO: Check types, field numbers, etc.?
214
- _, ok := pb.ExtensionMap()[extension.Field]
215
- return ok
216
-}
217
-
218
-// ClearExtension removes the given extension from pb.
219
-func ClearExtension(pb extendableProto, extension *ExtensionDesc) {
220
- // TODO: Check types, field numbers, etc.?
221
- delete(pb.ExtensionMap(), extension.Field)
222
-}
223
-
224
-// GetExtension parses and returns the given extension of pb.
225
-// If the extension is not present and has no default value it returns ErrMissingExtension.
226
-func GetExtension(pb extendableProto, extension *ExtensionDesc) (interface{}, error) {
227
- if err := checkExtensionTypes(pb, extension); err != nil {
228
- return nil, err
229
- }
230
-
231
- emap := pb.ExtensionMap()
232
- e, ok := emap[extension.Field]
233
- if !ok {
234
- // defaultExtensionValue returns the default value or
235
- // ErrMissingExtension if there is no default.
236
- return defaultExtensionValue(extension)
237
- }
238
-
239
- if e.value != nil {
240
- // Already decoded. Check the descriptor, though.
241
- if e.desc != extension {
242
- // This shouldn't happen. If it does, it means that
243
- // GetExtension was called twice with two different
244
- // descriptors with the same field number.
245
- return nil, errors.New("proto: descriptor conflict")
246
- }
247
- return e.value, nil
248
- }
249
-
250
- v, err := decodeExtension(e.enc, extension)
251
- if err != nil {
252
- return nil, err
253
- }
254
-
255
- // Remember the decoded version and drop the encoded version.
256
- // That way it is safe to mutate what we return.
257
- e.value = v
258
- e.desc = extension
259
- e.enc = nil
260
- emap[extension.Field] = e
261
- return e.value, nil
262
-}
263
-
264
-// defaultExtensionValue returns the default value for extension.
265
-// If no default for an extension is defined ErrMissingExtension is returned.
266
-func defaultExtensionValue(extension *ExtensionDesc) (interface{}, error) {
267
- t := reflect.TypeOf(extension.ExtensionType)
268
- props := extensionProperties(extension)
269
-
270
- sf, _, err := fieldDefault(t, props)
271
- if err != nil {
272
- return nil, err
273
- }
274
-
275
- if sf == nil || sf.value == nil {
276
- // There is no default value.
277
- return nil, ErrMissingExtension
278
- }
279
-
280
- if t.Kind() != reflect.Ptr {
281
- // We do not need to return a Ptr, we can directly return sf.value.
282
- return sf.value, nil
283
- }
284
-
285
- // We need to return an interface{} that is a pointer to sf.value.
286
- value := reflect.New(t).Elem()
287
- value.Set(reflect.New(value.Type().Elem()))
288
- if sf.kind == reflect.Int32 {
289
- // We may have an int32 or an enum, but the underlying data is int32.
290
- // Since we can't set an int32 into a non int32 reflect.value directly
291
- // set it as a int32.
292
- value.Elem().SetInt(int64(sf.value.(int32)))
293
- } else {
294
- value.Elem().Set(reflect.ValueOf(sf.value))
295
- }
296
- return value.Interface(), nil
297
-}
298
-
299
-// decodeExtension decodes an extension encoded in b.
300
-func decodeExtension(b []byte, extension *ExtensionDesc) (interface{}, error) {
301
- o := NewBuffer(b)
302
-
303
- t := reflect.TypeOf(extension.ExtensionType)
304
- rep := extension.repeated()
305
-
306
- props := extensionProperties(extension)
307
-
308
- // t is a pointer to a struct, pointer to basic type or a slice.
309
- // Allocate a "field" to store the pointer/slice itself; the
310
- // pointer/slice will be stored here. We pass
311
- // the address of this field to props.dec.
312
- // This passes a zero field and a *t and lets props.dec
313
- // interpret it as a *struct{ x t }.
314
- value := reflect.New(t).Elem()
315
-
316
- for {
317
- // Discard wire type and field number varint. It isn't needed.
318
- if _, err := o.DecodeVarint(); err != nil {
319
- return nil, err
320
- }
321
-
322
- if err := props.dec(o, props, toStructPointer(value.Addr())); err != nil {
323
- return nil, err
324
- }
325
-
326
- if !rep || o.index >= len(o.buf) {
327
- break
328
- }
329
- }
330
- return value.Interface(), nil
331
-}
332
-
333
-// GetExtensions returns a slice of the extensions present in pb that are also listed in es.
334
-// The returned slice has the same length as es; missing extensions will appear as nil elements.
335
-func GetExtensions(pb Message, es []*ExtensionDesc) (extensions []interface{}, err error) {
336
- epb, ok := pb.(extendableProto)
337
- if !ok {
338
- err = errors.New("proto: not an extendable proto")
339
- return
340
- }
341
- extensions = make([]interface{}, len(es))
342
- for i, e := range es {
343
- extensions[i], err = GetExtension(epb, e)
344
- if err == ErrMissingExtension {
345
- err = nil
346
- }
347
- if err != nil {
348
- return
349
- }
350
- }
351
- return
352
-}
353
-
354
-// SetExtension sets the specified extension of pb to the specified value.
355
-func SetExtension(pb extendableProto, extension *ExtensionDesc, value interface{}) error {
356
- if err := checkExtensionTypes(pb, extension); err != nil {
357
- return err
358
- }
359
- typ := reflect.TypeOf(extension.ExtensionType)
360
- if typ != reflect.TypeOf(value) {
361
- return errors.New("proto: bad extension value type")
362
- }
363
- // nil extension values need to be caught early, because the
364
- // encoder can't distinguish an ErrNil due to a nil extension
365
- // from an ErrNil due to a missing field. Extensions are
366
- // always optional, so the encoder would just swallow the error
367
- // and drop all the extensions from the encoded message.
368
- if reflect.ValueOf(value).IsNil() {
369
- return fmt.Errorf("proto: SetExtension called with nil value of type %T", value)
370
- }
371
-
372
- pb.ExtensionMap()[extension.Field] = Extension{desc: extension, value: value}
373
- return nil
374
-}
375
-
376
-// A global registry of extensions.
377
-// The generated code will register the generated descriptors by calling RegisterExtension.
378
-
379
-var extensionMaps = make(map[reflect.Type]map[int32]*ExtensionDesc)
380
-
381
-// RegisterExtension is called from the generated code.
382
-func RegisterExtension(desc *ExtensionDesc) {
383
- st := reflect.TypeOf(desc.ExtendedType).Elem()
384
- m := extensionMaps[st]
385
- if m == nil {
386
- m = make(map[int32]*ExtensionDesc)
387
- extensionMaps[st] = m
388
- }
389
- if _, ok := m[desc.Field]; ok {
390
- panic("proto: duplicate extension registered: " + st.String() + " " + strconv.Itoa(int(desc.Field)))
391
- }
392
- m[desc.Field] = desc
393
-}
394
-
395
-// RegisteredExtensions returns a map of the registered extensions of a
396
-// protocol buffer struct, indexed by the extension number.
397
-// The argument pb should be a nil pointer to the struct type.
398
-func RegisteredExtensions(pb Message) map[int32]*ExtensionDesc {
399
- return extensionMaps[reflect.TypeOf(pb).Elem()]
400
-}
Godeps/_workspace/src/github.com/golang/protobuf/proto/extensions_test.go
deleted
-292
@@ -1,292 +0,0 @@
1
-// Go support for Protocol Buffers - Google's data interchange format
2
-//
3
-// Copyright 2014 The Go Authors. All rights reserved.
4
-// https://github.com/golang/protobuf
5
-//
6
-// Redistribution and use in source and binary forms, with or without
7
-// modification, are permitted provided that the following conditions are
8
-// met:
9
-//
10
-// * Redistributions of source code must retain the above copyright
11
-// notice, this list of conditions and the following disclaimer.
12
-// * Redistributions in binary form must reproduce the above
13
-// copyright notice, this list of conditions and the following disclaimer
14
-// in the documentation and/or other materials provided with the
15
-// distribution.
16
-// * Neither the name of Google Inc. nor the names of its
17
-// contributors may be used to endorse or promote products derived from
18
-// this software without specific prior written permission.
19
-//
20
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
-// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
-// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23
-// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24
-// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25
-// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26
-// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27
-// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28
-// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29
-// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
-// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
-
32
-package proto_test
33
-
34
-import (
35
- "fmt"
36
- "reflect"
37
- "testing"
38
-
39
- "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/golang/protobuf/proto"
40
- pb "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/golang/protobuf/proto/testdata"
41
-)
42
-
43
-func TestGetExtensionsWithMissingExtensions(t *testing.T) {
44
- msg := &pb.MyMessage{}
45
- ext1 := &pb.Ext{}
46
- if err := proto.SetExtension(msg, pb.E_Ext_More, ext1); err != nil {
47
- t.Fatalf("Could not set ext1: %s", ext1)
48
- }
49
- exts, err := proto.GetExtensions(msg, []*proto.ExtensionDesc{
50
- pb.E_Ext_More,
51
- pb.E_Ext_Text,
52
- })
53
- if err != nil {
54
- t.Fatalf("GetExtensions() failed: %s", err)
55
- }
56
- if exts[0] != ext1 {
57
- t.Errorf("ext1 not in returned extensions: %T %v", exts[0], exts[0])
58
- }
59
- if exts[1] != nil {
60
- t.Errorf("ext2 in returned extensions: %T %v", exts[1], exts[1])
61
- }
62
-}
63
-
64
-func TestGetExtensionStability(t *testing.T) {
65
- check := func(m *pb.MyMessage) bool {
66
- ext1, err := proto.GetExtension(m, pb.E_Ext_More)
67
- if err != nil {
68
- t.Fatalf("GetExtension() failed: %s", err)
69
- }
70
- ext2, err := proto.GetExtension(m, pb.E_Ext_More)
71
- if err != nil {
72
- t.Fatalf("GetExtension() failed: %s", err)
73
- }
74
- return ext1 == ext2
75
- }
76
- msg := &pb.MyMessage{Count: proto.Int32(4)}
77
- ext0 := &pb.Ext{}
78
- if err := proto.SetExtension(msg, pb.E_Ext_More, ext0); err != nil {
79
- t.Fatalf("Could not set ext1: %s", ext0)
80
- }
81
- if !check(msg) {
82
- t.Errorf("GetExtension() not stable before marshaling")
83
- }
84
- bb, err := proto.Marshal(msg)
85
- if err != nil {
86
- t.Fatalf("Marshal() failed: %s", err)
87
- }
88
- msg1 := &pb.MyMessage{}
89
- err = proto.Unmarshal(bb, msg1)
90
- if err != nil {
91
- t.Fatalf("Unmarshal() failed: %s", err)
92
- }
93
- if !check(msg1) {
94
- t.Errorf("GetExtension() not stable after unmarshaling")
95
- }
96
-}
97
-
98
-func TestGetExtensionDefaults(t *testing.T) {
99
- var setFloat64 float64 = 1
100
- var setFloat32 float32 = 2
101
- var setInt32 int32 = 3
102
- var setInt64 int64 = 4
103
- var setUint32 uint32 = 5
104
- var setUint64 uint64 = 6
105
- var setBool = true
106
- var setBool2 = false
107
- var setString = "Goodnight string"
108
- var setBytes = []byte("Goodnight bytes")
109
- var setEnum = pb.DefaultsMessage_TWO
110
-
111
- type testcase struct {
112
- ext *proto.ExtensionDesc // Extension we are testing.
113
- want interface{} // Expected value of extension, or nil (meaning that GetExtension will fail).
114
- def interface{} // Expected value of extension after ClearExtension().
115
- }
116
- tests := []testcase{
117
- {pb.E_NoDefaultDouble, setFloat64, nil},
118
- {pb.E_NoDefaultFloat, setFloat32, nil},
119
- {pb.E_NoDefaultInt32, setInt32, nil},
120
- {pb.E_NoDefaultInt64, setInt64, nil},
121
- {pb.E_NoDefaultUint32, setUint32, nil},
122
- {pb.E_NoDefaultUint64, setUint64, nil},
123
- {pb.E_NoDefaultSint32, setInt32, nil},
124
- {pb.E_NoDefaultSint64, setInt64, nil},
125
- {pb.E_NoDefaultFixed32, setUint32, nil},
126
- {pb.E_NoDefaultFixed64, setUint64, nil},
127
- {pb.E_NoDefaultSfixed32, setInt32, nil},
128
- {pb.E_NoDefaultSfixed64, setInt64, nil},
129
- {pb.E_NoDefaultBool, setBool, nil},
130
- {pb.E_NoDefaultBool, setBool2, nil},
131
- {pb.E_NoDefaultString, setString, nil},
132
- {pb.E_NoDefaultBytes, setBytes, nil},
133
- {pb.E_NoDefaultEnum, setEnum, nil},
134
- {pb.E_DefaultDouble, setFloat64, float64(3.1415)},
135
- {pb.E_DefaultFloat, setFloat32, float32(3.14)},
136
- {pb.E_DefaultInt32, setInt32, int32(42)},
137
- {pb.E_DefaultInt64, setInt64, int64(43)},
138
- {pb.E_DefaultUint32, setUint32, uint32(44)},
139
- {pb.E_DefaultUint64, setUint64, uint64(45)},
140
- {pb.E_DefaultSint32, setInt32, int32(46)},
141
- {pb.E_DefaultSint64, setInt64, int64(47)},
142
- {pb.E_DefaultFixed32, setUint32, uint32(48)},
143
- {pb.E_DefaultFixed64, setUint64, uint64(49)},
144
- {pb.E_DefaultSfixed32, setInt32, int32(50)},
145
- {pb.E_DefaultSfixed64, setInt64, int64(51)},
146
- {pb.E_DefaultBool, setBool, true},
147
- {pb.E_DefaultBool, setBool2, true},
148
- {pb.E_DefaultString, setString, "Hello, string"},
149
- {pb.E_DefaultBytes, setBytes, []byte("Hello, bytes")},
150
- {pb.E_DefaultEnum, setEnum, pb.DefaultsMessage_ONE},
151
- }
152
-
153
- checkVal := func(test testcase, msg *pb.DefaultsMessage, valWant interface{}) error {
154
- val, err := proto.GetExtension(msg, test.ext)
155
- if err != nil {
156
- if valWant != nil {
157
- return fmt.Errorf("GetExtension(): %s", err)
158
- }
159
- if want := proto.ErrMissingExtension; err != want {
160
- return fmt.Errorf("Unexpected error: got %v, want %v", err, want)
161
- }
162
- return nil
163
- }
164
-
165
- // All proto2 extension values are either a pointer to a value or a slice of values.
166
- ty := reflect.TypeOf(val)
167
- tyWant := reflect.TypeOf(test.ext.ExtensionType)
168
- if got, want := ty, tyWant; got != want {
169
- return fmt.Errorf("unexpected reflect.TypeOf(): got %v want %v", got, want)
170
- }
171
- tye := ty.Elem()
172
- tyeWant := tyWant.Elem()
173
- if got, want := tye, tyeWant; got != want {
174
- return fmt.Errorf("unexpected reflect.TypeOf().Elem(): got %v want %v", got, want)
175
- }
176
-
177
- // Check the name of the type of the value.
178
- // If it is an enum it will be type int32 with the name of the enum.
179
- if got, want := tye.Name(), tye.Name(); got != want {
180
- return fmt.Errorf("unexpected reflect.TypeOf().Elem().Name(): got %v want %v", got, want)
181
- }
182
-
183
- // Check that value is what we expect.
184
- // If we have a pointer in val, get the value it points to.
185
- valExp := val
186
- if ty.Kind() == reflect.Ptr {
187
- valExp = reflect.ValueOf(val).Elem().Interface()
188
- }
189
- if got, want := valExp, valWant; !reflect.DeepEqual(got, want) {
190
- return fmt.Errorf("unexpected reflect.DeepEqual(): got %v want %v", got, want)
191
- }
192
-
193
- return nil
194
- }
195
-
196
- setTo := func(test testcase) interface{} {
197
- setTo := reflect.ValueOf(test.want)
198
- if typ := reflect.TypeOf(test.ext.ExtensionType); typ.Kind() == reflect.Ptr {
199
- setTo = reflect.New(typ).Elem()
200
- setTo.Set(reflect.New(setTo.Type().Elem()))
201
- setTo.Elem().Set(reflect.ValueOf(test.want))
202
- }
203
- return setTo.Interface()
204
- }
205
-
206
- for _, test := range tests {
207
- msg := &pb.DefaultsMessage{}
208
- name := test.ext.Name
209
-
210
- // Check the initial value.
211
- if err := checkVal(test, msg, test.def); err != nil {
212
- t.Errorf("%s: %v", name, err)
213
- }
214
-
215
- // Set the per-type value and check value.
216
- name = fmt.Sprintf("%s (set to %T %v)", name, test.want, test.want)
217
- if err := proto.SetExtension(msg, test.ext, setTo(test)); err != nil {
218
- t.Errorf("%s: SetExtension(): %v", name, err)
219
- continue
220
- }
221
- if err := checkVal(test, msg, test.want); err != nil {
222
- t.Errorf("%s: %v", name, err)
223
- continue
224
- }
225
-
226
- // Set and check the value.
227
- name += " (cleared)"
228
- proto.ClearExtension(msg, test.ext)
229
- if err := checkVal(test, msg, test.def); err != nil {
230
- t.Errorf("%s: %v", name, err)
231
- }
232
- }
233
-}
234
-
235
-func TestExtensionsRoundTrip(t *testing.T) {
236
- msg := &pb.MyMessage{}
237
- ext1 := &pb.Ext{
238
- Data: proto.String("hi"),
239
- }
240
- ext2 := &pb.Ext{
241
- Data: proto.String("there"),
242
- }
243
- exists := proto.HasExtension(msg, pb.E_Ext_More)
244
- if exists {
245
- t.Error("Extension More present unexpectedly")
246
- }
247
- if err := proto.SetExtension(msg, pb.E_Ext_More, ext1); err != nil {
248
- t.Error(err)
249
- }
250
- if err := proto.SetExtension(msg, pb.E_Ext_More, ext2); err != nil {
251
- t.Error(err)
252
- }
253
- e, err := proto.GetExtension(msg, pb.E_Ext_More)
254
- if err != nil {
255
- t.Error(err)
256
- }
257
- x, ok := e.(*pb.Ext)
258
- if !ok {
259
- t.Errorf("e has type %T, expected testdata.Ext", e)
260
- } else if *x.Data != "there" {
261
- t.Errorf("SetExtension failed to overwrite, got %+v, not 'there'", x)
262
- }
263
- proto.ClearExtension(msg, pb.E_Ext_More)
264
- if _, err = proto.GetExtension(msg, pb.E_Ext_More); err != proto.ErrMissingExtension {
265
- t.Errorf("got %v, expected ErrMissingExtension", e)
266
- }
267
- if _, err := proto.GetExtension(msg, pb.E_X215); err == nil {
268
- t.Error("expected bad extension error, got nil")
269
- }
270
- if err := proto.SetExtension(msg, pb.E_X215, 12); err == nil {
271
- t.Error("expected extension err")
272
- }
273
- if err := proto.SetExtension(msg, pb.E_Ext_More, 12); err == nil {
274
- t.Error("expected some sort of type mismatch error, got nil")
275
- }
276
-}
277
-
278
-func TestNilExtension(t *testing.T) {
279
- msg := &pb.MyMessage{
280
- Count: proto.Int32(1),
281
- }
282
- if err := proto.SetExtension(msg, pb.E_Ext_Text, proto.String("hello")); err != nil {
283
- t.Fatal(err)
284
- }
285
- if err := proto.SetExtension(msg, pb.E_Ext_More, (*pb.Ext)(nil)); err == nil {
286
- t.Error("expected SetExtension to fail due to a nil extension")
287
- } else if want := "proto: SetExtension called with nil value of type *testdata.Ext"; err.Error() != want {
288
- t.Errorf("expected error %v, got %v", want, err)
289
- }
290
- // Note: if the behavior of Marshal is ever changed to ignore nil extensions, update
291
- // this test to verify that E_Ext_Text is properly propagated through marshal->unmarshal.
292
-}
Godeps/_workspace/src/github.com/golang/protobuf/proto/lib.go
deleted
-796
@@ -1,796 +0,0 @@
1
-// Go support for Protocol Buffers - Google's data interchange format
2
-//
3
-// Copyright 2010 The Go Authors. All rights reserved.
4
-// https://github.com/golang/protobuf
5
-//
6
-// Redistribution and use in source and binary forms, with or without
7
-// modification, are permitted provided that the following conditions are
8
-// met:
9
-//
10
-// * Redistributions of source code must retain the above copyright
11
-// notice, this list of conditions and the following disclaimer.
12
-// * Redistributions in binary form must reproduce the above
13
-// copyright notice, this list of conditions and the following disclaimer
14
-// in the documentation and/or other materials provided with the
15
-// distribution.
16
-// * Neither the name of Google Inc. nor the names of its
17
-// contributors may be used to endorse or promote products derived from
18
-// this software without specific prior written permission.
19
-//
20
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
-// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
-// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23
-// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24
-// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25
-// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26
-// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27
-// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28
-// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29
-// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
-// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
-
32
-/*
33
-Package proto converts data structures to and from the wire format of
34
-protocol buffers. It works in concert with the Go source code generated
35
-for .proto files by the protocol compiler.
36
-
37
-A summary of the properties of the protocol buffer interface
38
-for a protocol buffer variable v:
39
-
40
- - Names are turned from camel_case to CamelCase for export.
41
- - There are no methods on v to set fields; just treat
42
- them as structure fields.
43
- - There are getters that return a field's value if set,
44
- and return the field's default value if unset.
45
- The getters work even if the receiver is a nil message.
46
- - The zero value for a struct is its correct initialization state.
47
- All desired fields must be set before marshaling.
48
- - A Reset() method will restore a protobuf struct to its zero state.
49
- - Non-repeated fields are pointers to the values; nil means unset.
50
- That is, optional or required field int32 f becomes F *int32.
51
- - Repeated fields are slices.
52
- - Helper functions are available to aid the setting of fields.
53
- msg.Foo = proto.String("hello") // set field
54
- - Constants are defined to hold the default values of all fields that
55
- have them. They have the form Default_StructName_FieldName.
56
- Because the getter methods handle defaulted values,
57
- direct use of these constants should be rare.
58
- - Enums are given type names and maps from names to values.
59
- Enum values are prefixed by the enclosing message's name, or by the
60
- enum's type name if it is a top-level enum. Enum types have a String
61
- method, and a Enum method to assist in message construction.
62
- - Nested messages, groups and enums have type names prefixed with the name of
63
- the surrounding message type.
64
- - Extensions are given descriptor names that start with E_,
65
- followed by an underscore-delimited list of the nested messages
66
- that contain it (if any) followed by the CamelCased name of the
67
- extension field itself. HasExtension, ClearExtension, GetExtension
68
- and SetExtension are functions for manipulating extensions.
69
- - Marshal and Unmarshal are functions to encode and decode the wire format.
70
-
71
-The simplest way to describe this is to see an example.
72
-Given file test.proto, containing
73
-
74
- package example;
75
-
76
- enum FOO { X = 17; }
77
-
78
- message Test {
79
- required string label = 1;
80
- optional int32 type = 2 [default=77];
81
- repeated int64 reps = 3;
82
- optional group OptionalGroup = 4 {
83
- required string RequiredField = 5;
84
- }
85
- }
86
-
87
-The resulting file, test.pb.go, is:
88
-
89
- package example
90
-
91
- import proto "github.com/golang/protobuf/proto"
92
- import math "math"
93
-
94
- type FOO int32
95
- const (
96
- FOO_X FOO = 17
97
- )
98
- var FOO_name = map[int32]string{
99
- 17: "X",
100
- }
101
- var FOO_value = map[string]int32{
102
- "X": 17,
103
- }
104
-
105
- func (x FOO) Enum() *FOO {
106
- p := new(FOO)
107
- *p = x
108
- return p
109
- }
110
- func (x FOO) String() string {
111
- return proto.EnumName(FOO_name, int32(x))
112
- }
113
- func (x *FOO) UnmarshalJSON(data []byte) error {
114
- value, err := proto.UnmarshalJSONEnum(FOO_value, data)
115
- if err != nil {
116
- return err
117
- }
118
- *x = FOO(value)
119
- return nil
120
- }
121
-
122
- type Test struct {
123
- Label *string `protobuf:"bytes,1,req,name=label" json:"label,omitempty"`
124
- Type *int32 `protobuf:"varint,2,opt,name=type,def=77" json:"type,omitempty"`
125
- Reps []int64 `protobuf:"varint,3,rep,name=reps" json:"reps,omitempty"`
126
- Optionalgroup *Test_OptionalGroup `protobuf:"group,4,opt,name=OptionalGroup" json:"optionalgroup,omitempty"`
127
- XXX_unrecognized []byte `json:"-"`
128
- }
129
- func (m *Test) Reset() { *m = Test{} }
130
- func (m *Test) String() string { return proto.CompactTextString(m) }
131
- func (*Test) ProtoMessage() {}
132
- const Default_Test_Type int32 = 77
133
-
134
- func (m *Test) GetLabel() string {
135
- if m != nil && m.Label != nil {
136
- return *m.Label
137
- }
138
- return ""
139
- }
140
-
141
- func (m *Test) GetType() int32 {
142
- if m != nil && m.Type != nil {
143
- return *m.Type
144
- }
145
- return Default_Test_Type
146
- }
147
-
148
- func (m *Test) GetOptionalgroup() *Test_OptionalGroup {
149
- if m != nil {
150
- return m.Optionalgroup
151
- }
152
- return nil
153
- }
154
-
155
- type Test_OptionalGroup struct {
156
- RequiredField *string `protobuf:"bytes,5,req" json:"RequiredField,omitempty"`
157
- }
158
- func (m *Test_OptionalGroup) Reset() { *m = Test_OptionalGroup{} }
159
- func (m *Test_OptionalGroup) String() string { return proto.CompactTextString(m) }
160
-
161
- func (m *Test_OptionalGroup) GetRequiredField() string {
162
- if m != nil && m.RequiredField != nil {
163
- return *m.RequiredField
164
- }
165
- return ""
166
- }
167
-
168
- func init() {
169
- proto.RegisterEnum("example.FOO", FOO_name, FOO_value)
170
- }
171
-
172
-To create and play with a Test object:
173
-
174
-package main
175
-
176
- import (
177
- "log"
178
-
179
- "github.com/golang/protobuf/proto"
180
- pb "./example.pb"
181
- )
182
-
183
- func main() {
184
- test := &pb.Test{
185
- Label: proto.String("hello"),
186
- Type: proto.Int32(17),
187
- Optionalgroup: &pb.Test_OptionalGroup{
188
- RequiredField: proto.String("good bye"),
189
- },
190
- }
191
- data, err := proto.Marshal(test)
192
- if err != nil {
193
- log.Fatal("marshaling error: ", err)
194
- }
195
- newTest := &pb.Test{}
196
- err = proto.Unmarshal(data, newTest)
197
- if err != nil {
198
- log.Fatal("unmarshaling error: ", err)
199
- }
200
- // Now test and newTest contain the same data.
201
- if test.GetLabel() != newTest.GetLabel() {
202
- log.Fatalf("data mismatch %q != %q", test.GetLabel(), newTest.GetLabel())
203
- }
204
- // etc.
205
- }
206
-*/
207
-package proto
208
-
209
-import (
210
- "encoding/json"
211
- "fmt"
212
- "log"
213
- "reflect"
214
- "strconv"
215
- "sync"
216
-)
217
-
218
-// Message is implemented by generated protocol buffer messages.
219
-type Message interface {
220
- Reset()
221
- String() string
222
- ProtoMessage()
223
-}
224
-
225
-// Stats records allocation details about the protocol buffer encoders
226
-// and decoders. Useful for tuning the library itself.
227
-type Stats struct {
228
- Emalloc uint64 // mallocs in encode
229
- Dmalloc uint64 // mallocs in decode
230
- Encode uint64 // number of encodes
231
- Decode uint64 // number of decodes
232
- Chit uint64 // number of cache hits
233
- Cmiss uint64 // number of cache misses
234
- Size uint64 // number of sizes
235
-}
236
-
237
-// Set to true to enable stats collection.
238
-const collectStats = false
239
-
240
-var stats Stats
241
-
242
-// GetStats returns a copy of the global Stats structure.
243
-func GetStats() Stats { return stats }
244
-
245
-// A Buffer is a buffer manager for marshaling and unmarshaling
246
-// protocol buffers. It may be reused between invocations to
247
-// reduce memory usage. It is not necessary to use a Buffer;
248
-// the global functions Marshal and Unmarshal create a
249
-// temporary Buffer and are fine for most applications.
250
-type Buffer struct {
251
- buf []byte // encode/decode byte stream
252
- index int // write point
253
-
254
- // pools of basic types to amortize allocation.
255
- bools []bool
256
- uint32s []uint32
257
- uint64s []uint64
258
-
259
- // extra pools, only used with pointer_reflect.go
260
- int32s []int32
261
- int64s []int64
262
- float32s []float32
263
- float64s []float64
264
-}
265
-
266
-// NewBuffer allocates a new Buffer and initializes its internal data to
267
-// the contents of the argument slice.
268
-func NewBuffer(e []byte) *Buffer {
269
- return &Buffer{buf: e}
270
-}
271
-
272
-// Reset resets the Buffer, ready for marshaling a new protocol buffer.
273
-func (p *Buffer) Reset() {
274
- p.buf = p.buf[0:0] // for reading/writing
275
- p.index = 0 // for reading
276
-}
277
-
278
-// SetBuf replaces the internal buffer with the slice,
279
-// ready for unmarshaling the contents of the slice.
280
-func (p *Buffer) SetBuf(s []byte) {
281
- p.buf = s
282
- p.index = 0
283
-}
284
-
285
-// Bytes returns the contents of the Buffer.
286
-func (p *Buffer) Bytes() []byte { return p.buf }
287
-
288
-/*
289
- * Helper routines for simplifying the creation of optional fields of basic type.
290
- */
291
-
292
-// Bool is a helper routine that allocates a new bool value
293
-// to store v and returns a pointer to it.
294
-func Bool(v bool) *bool {
295
- return &v
296
-}
297
-
298
-// Int32 is a helper routine that allocates a new int32 value
299
-// to store v and returns a pointer to it.
300
-func Int32(v int32) *int32 {
301
- return &v
302
-}
303
-
304
-// Int is a helper routine that allocates a new int32 value
305
-// to store v and returns a pointer to it, but unlike Int32
306
-// its argument value is an int.
307
-func Int(v int) *int32 {
308
- p := new(int32)
309
- *p = int32(v)
310
- return p
311
-}
312
-
313
-// Int64 is a helper routine that allocates a new int64 value
314
-// to store v and returns a pointer to it.
315
-func Int64(v int64) *int64 {
316
- return &v
317
-}
318
-
319
-// Float32 is a helper routine that allocates a new float32 value
320
-// to store v and returns a pointer to it.
321
-func Float32(v float32) *float32 {
322
- return &v
323
-}
324
-
325
-// Float64 is a helper routine that allocates a new float64 value
326
-// to store v and returns a pointer to it.
327
-func Float64(v float64) *float64 {
328
- return &v
329
-}
330
-
331
-// Uint32 is a helper routine that allocates a new uint32 value
332
-// to store v and returns a pointer to it.
333
-func Uint32(v uint32) *uint32 {
334
- return &v
335
-}
336
-
337
-// Uint64 is a helper routine that allocates a new uint64 value
338
-// to store v and returns a pointer to it.
339
-func Uint64(v uint64) *uint64 {
340
- return &v
341
-}
342
-
343
-// String is a helper routine that allocates a new string value
344
-// to store v and returns a pointer to it.
345
-func String(v string) *string {
346
- return &v
347
-}
348
-
349
-// EnumName is a helper function to simplify printing protocol buffer enums
350
-// by name. Given an enum map and a value, it returns a useful string.
351
-func EnumName(m map[int32]string, v int32) string {
352
- s, ok := m[v]
353
- if ok {
354
- return s
355
- }
356
- return strconv.Itoa(int(v))
357
-}
358
-
359
-// UnmarshalJSONEnum is a helper function to simplify recovering enum int values
360
-// from their JSON-encoded representation. Given a map from the enum's symbolic
361
-// names to its int values, and a byte buffer containing the JSON-encoded
362
-// value, it returns an int32 that can be cast to the enum type by the caller.
363
-//
364
-// The function can deal with both JSON representations, numeric and symbolic.
365
-func UnmarshalJSONEnum(m map[string]int32, data []byte, enumName string) (int32, error) {
366
- if data[0] == '"' {
367
- // New style: enums are strings.
368
- var repr string
369
- if err := json.Unmarshal(data, &repr); err != nil {
370
- return -1, err
371
- }
372
- val, ok := m[repr]
373
- if !ok {
374
- return 0, fmt.Errorf("unrecognized enum %s value %q", enumName, repr)
375
- }
376
- return val, nil
377
- }
378
- // Old style: enums are ints.
379
- var val int32
380
- if err := json.Unmarshal(data, &val); err != nil {
381
- return 0, fmt.Errorf("cannot unmarshal %#q into enum %s", data, enumName)
382
- }
383
- return val, nil
384
-}
385
-
386
-// DebugPrint dumps the encoded data in b in a debugging format with a header
387
-// including the string s. Used in testing but made available for general debugging.
388
-func (p *Buffer) DebugPrint(s string, b []byte) {
389
- var u uint64
390
-
391
- obuf := p.buf
392
- index := p.index
393
- p.buf = b
394
- p.index = 0
395
- depth := 0
396
-
397
- fmt.Printf("\n--- %s ---\n", s)
398
-
399
-out:
400
- for {
401
- for i := 0; i < depth; i++ {
402
- fmt.Print(" ")
403
- }
404
-
405
- index := p.index
406
- if index == len(p.buf) {
407
- break
408
- }
409
-
410
- op, err := p.DecodeVarint()
411
- if err != nil {
412
- fmt.Printf("%3d: fetching op err %v\n", index, err)
413
- break out
414
- }
415
- tag := op >> 3
416
- wire := op & 7
417
-
418
- switch wire {
419
- default:
420
- fmt.Printf("%3d: t=%3d unknown wire=%d\n",
421
- index, tag, wire)
422
- break out
423
-
424
- case WireBytes:
425
- var r []byte
426
-
427
- r, err = p.DecodeRawBytes(false)
428
- if err != nil {
429
- break out
430
- }
431
- fmt.Printf("%3d: t=%3d bytes [%d]", index, tag, len(r))
432
- if len(r) <= 6 {
433
- for i := 0; i < len(r); i++ {
434
- fmt.Printf(" %.2x", r[i])
435
- }
436
- } else {
437
- for i := 0; i < 3; i++ {
438
- fmt.Printf(" %.2x", r[i])
439
- }
440
- fmt.Printf(" ..")
441
- for i := len(r) - 3; i < len(r); i++ {
442
- fmt.Printf(" %.2x", r[i])
443
- }
444
- }
445
- fmt.Printf("\n")
446
-
447
- case WireFixed32:
448
- u, err = p.DecodeFixed32()
449
- if err != nil {
450
- fmt.Printf("%3d: t=%3d fix32 err %v\n", index, tag, err)
451
- break out
452
- }
453
- fmt.Printf("%3d: t=%3d fix32 %d\n", index, tag, u)
454
-
455
- case WireFixed64:
456
- u, err = p.DecodeFixed64()
457
- if err != nil {
458
- fmt.Printf("%3d: t=%3d fix64 err %v\n", index, tag, err)
459
- break out
460
- }
461
- fmt.Printf("%3d: t=%3d fix64 %d\n", index, tag, u)
462
- break
463
-
464
- case WireVarint:
465
- u, err = p.DecodeVarint()
466
- if err != nil {
467
- fmt.Printf("%3d: t=%3d varint err %v\n", index, tag, err)
468
- break out
469
- }
470
- fmt.Printf("%3d: t=%3d varint %d\n", index, tag, u)
471
-
472
- case WireStartGroup:
473
- if err != nil {
474
- fmt.Printf("%3d: t=%3d start err %v\n", index, tag, err)
475
- break out
476
- }
477
- fmt.Printf("%3d: t=%3d start\n", index, tag)
478
- depth++
479
-
480
- case WireEndGroup:
481
- depth--
482
- if err != nil {
483
- fmt.Printf("%3d: t=%3d end err %v\n", index, tag, err)
484
- break out
485
- }
486
- fmt.Printf("%3d: t=%3d end\n", index, tag)
487
- }
488
- }
489
-
490
- if depth != 0 {
491
- fmt.Printf("%3d: start-end not balanced %d\n", p.index, depth)
492
- }
493
- fmt.Printf("\n")
494
-
495
- p.buf = obuf
496
- p.index = index
497
-}
498
-
499
-// SetDefaults sets unset protocol buffer fields to their default values.
500
-// It only modifies fields that are both unset and have defined defaults.
501
-// It recursively sets default values in any non-nil sub-messages.
502
-func SetDefaults(pb Message) {
503
- setDefaults(reflect.ValueOf(pb), true, false)
504
-}
505
-
506
-// v is a pointer to a struct.
507
-func setDefaults(v reflect.Value, recur, zeros bool) {
508
- v = v.Elem()
509
-
510
- defaultMu.RLock()
511
- dm, ok := defaults[v.Type()]
512
- defaultMu.RUnlock()
513
- if !ok {
514
- dm = buildDefaultMessage(v.Type())
515
- defaultMu.Lock()
516
- defaults[v.Type()] = dm
517
- defaultMu.Unlock()
518
- }
519
-
520
- for _, sf := range dm.scalars {
521
- f := v.Field(sf.index)
522
- if !f.IsNil() {
523
- // field already set
524
- continue
525
- }
526
- dv := sf.value
527
- if dv == nil && !zeros {
528
- // no explicit default, and don't want to set zeros
529
- continue
530
- }
531
- fptr := f.Addr().Interface() // **T
532
- // TODO: Consider batching the allocations we do here.
533
- switch sf.kind {
534
- case reflect.Bool:
535
- b := new(bool)
536
- if dv != nil {
537
- *b = dv.(bool)
538
- }
539
- *(fptr.(**bool)) = b
540
- case reflect.Float32:
541
- f := new(float32)
542
- if dv != nil {
543
- *f = dv.(float32)
544
- }
545
- *(fptr.(**float32)) = f
546
- case reflect.Float64:
547
- f := new(float64)
548
- if dv != nil {
549
- *f = dv.(float64)
550
- }
551
- *(fptr.(**float64)) = f
552
- case reflect.Int32:
553
- // might be an enum
554
- if ft := f.Type(); ft != int32PtrType {
555
- // enum
556
- f.Set(reflect.New(ft.Elem()))
557
- if dv != nil {
558
- f.Elem().SetInt(int64(dv.(int32)))
559
- }
560
- } else {
561
- // int32 field
562
- i := new(int32)
563
- if dv != nil {
564
- *i = dv.(int32)
565
- }
566
- *(fptr.(**int32)) = i
567
- }
568
- case reflect.Int64:
569
- i := new(int64)
570
- if dv != nil {
571
- *i = dv.(int64)
572
- }
573
- *(fptr.(**int64)) = i
574
- case reflect.String:
575
- s := new(string)
576
- if dv != nil {
577
- *s = dv.(string)
578
- }
579
- *(fptr.(**string)) = s
580
- case reflect.Uint8:
581
- // exceptional case: []byte
582
- var b []byte
583
- if dv != nil {
584
- db := dv.([]byte)
585
- b = make([]byte, len(db))
586
- copy(b, db)
587
- } else {
588
- b = []byte{}
589
- }
590
- *(fptr.(*[]byte)) = b
591
- case reflect.Uint32:
592
- u := new(uint32)
593
- if dv != nil {
594
- *u = dv.(uint32)
595
- }
596
- *(fptr.(**uint32)) = u
597
- case reflect.Uint64:
598
- u := new(uint64)
599
- if dv != nil {
600
- *u = dv.(uint64)
601
- }
602
- *(fptr.(**uint64)) = u
603
- default:
604
- log.Printf("proto: can't set default for field %v (sf.kind=%v)", f, sf.kind)
605
- }
606
- }
607
-
608
- for _, ni := range dm.nested {
609
- f := v.Field(ni)
610
- // f is *T or []*T or map[T]*T
611
- switch f.Kind() {
612
- case reflect.Ptr:
613
- if f.IsNil() {
614
- continue
615
- }
616
- setDefaults(f, recur, zeros)
617
-
618
- case reflect.Slice:
619
- for i := 0; i < f.Len(); i++ {
620
- e := f.Index(i)
621
- if e.IsNil() {
622
- continue
623
- }
624
- setDefaults(e, recur, zeros)
625
- }
626
-
627
- case reflect.Map:
628
- for _, k := range f.MapKeys() {
629
- e := f.MapIndex(k)
630
- if e.IsNil() {
631
- continue
632
- }
633
- setDefaults(e, recur, zeros)
634
- }
635
- }
636
- }
637
-}
638
-
639
-var (
640
- // defaults maps a protocol buffer struct type to a slice of the fields,
641
- // with its scalar fields set to their proto-declared non-zero default values.
642
- defaultMu sync.RWMutex
643
- defaults = make(map[reflect.Type]defaultMessage)
644
-
645
- int32PtrType = reflect.TypeOf((*int32)(nil))
646
-)
647
-
648
-// defaultMessage represents information about the default values of a message.
649
-type defaultMessage struct {
650
- scalars []scalarField
651
- nested []int // struct field index of nested messages
652
-}
653
-
654
-type scalarField struct {
655
- index int // struct field index
656
- kind reflect.Kind // element type (the T in *T or []T)
657
- value interface{} // the proto-declared default value, or nil
658
-}
659
-
660
-// t is a struct type.
661
-func buildDefaultMessage(t reflect.Type) (dm defaultMessage) {
662
- sprop := GetProperties(t)
663
- for _, prop := range sprop.Prop {
664
- fi, ok := sprop.decoderTags.get(prop.Tag)
665
- if !ok {
666
- // XXX_unrecognized
667
- continue
668
- }
669
- ft := t.Field(fi).Type
670
-
671
- sf, nested, err := fieldDefault(ft, prop)
672
- switch {
673
- case err != nil:
674
- log.Print(err)
675
- case nested:
676
- dm.nested = append(dm.nested, fi)
677
- case sf != nil:
678
- sf.index = fi
679
- dm.scalars = append(dm.scalars, *sf)
680
- }
681
- }
682
-
683
- return dm
684
-}
685
-
686
-// fieldDefault returns the scalarField for field type ft.
687
-// sf will be nil if the field can not have a default.
688
-// nestedMessage will be true if this is a nested message.
689
-// Note that sf.index is not set on return.
690
-func fieldDefault(ft reflect.Type, prop *Properties) (sf *scalarField, nestedMessage bool, err error) {
691
- var canHaveDefault bool
692
- switch ft.Kind() {
693
- case reflect.Ptr:
694
- if ft.Elem().Kind() == reflect.Struct {
695
- nestedMessage = true
696
- } else {
697
- canHaveDefault = true // proto2 scalar field
698
- }
699
-
700
- case reflect.Slice:
701
- switch ft.Elem().Kind() {
702
- case reflect.Ptr:
703
- nestedMessage = true // repeated message
704
- case reflect.Uint8:
705
- canHaveDefault = true // bytes field
706
- }
707
-
708
- case reflect.Map:
709
- if ft.Elem().Kind() == reflect.Ptr {
710
- nestedMessage = true // map with message values
711
- }
712
- }
713
-
714
- if !canHaveDefault {
715
- if nestedMessage {
716
- return nil, true, nil
717
- }
718
- return nil, false, nil
719
- }
720
-
721
- // We now know that ft is a pointer or slice.
722
- sf = &scalarField{kind: ft.Elem().Kind()}
723
-
724
- // scalar fields without defaults
725
- if !prop.HasDefault {
726
- return sf, false, nil
727
- }
728
-
729
- // a scalar field: either *T or []byte
730
- switch ft.Elem().Kind() {
731
- case reflect.Bool:
732
- x, err := strconv.ParseBool(prop.Default)
733
- if err != nil {
734
- return nil, false, fmt.Errorf("proto: bad default bool %q: %v", prop.Default, err)
735
- }
736
- sf.value = x
737
- case reflect.Float32:
738
- x, err := strconv.ParseFloat(prop.Default, 32)
739
- if err != nil {
740
- return nil, false, fmt.Errorf("proto: bad default float32 %q: %v", prop.Default, err)
741
- }
742
- sf.value = float32(x)
743
- case reflect.Float64:
744
- x, err := strconv.ParseFloat(prop.Default, 64)
745
- if err != nil {
746
- return nil, false, fmt.Errorf("proto: bad default float64 %q: %v", prop.Default, err)
747
- }
748
- sf.value = x
749
- case reflect.Int32:
750
- x, err := strconv.ParseInt(prop.Default, 10, 32)
751
- if err != nil {
752
- return nil, false, fmt.Errorf("proto: bad default int32 %q: %v", prop.Default, err)
753
- }
754
- sf.value = int32(x)
755
- case reflect.Int64:
756
- x, err := strconv.ParseInt(prop.Default, 10, 64)
757
- if err != nil {
758
- return nil, false, fmt.Errorf("proto: bad default int64 %q: %v", prop.Default, err)
759
- }
760
- sf.value = x
761
- case reflect.String:
762
- sf.value = prop.Default
763
- case reflect.Uint8:
764
- // []byte (not *uint8)
765
- sf.value = []byte(prop.Default)
766
- case reflect.Uint32:
767
- x, err := strconv.ParseUint(prop.Default, 10, 32)
768
- if err != nil {
769
- return nil, false, fmt.Errorf("proto: bad default uint32 %q: %v", prop.Default, err)
770
- }
771
- sf.value = uint32(x)
772
- case reflect.Uint64:
773
- x, err := strconv.ParseUint(prop.Default, 10, 64)
774
- if err != nil {
775
- return nil, false, fmt.Errorf("proto: bad default uint64 %q: %v", prop.Default, err)
776
- }
777
- sf.value = x
778
- default:
779
- return nil, false, fmt.Errorf("proto: unhandled def kind %v", ft.Elem().Kind())
780
- }
781
-
782
- return sf, false, nil
783
-}
784
-
785
-// Map fields may have key types of non-float scalars, strings and enums.
786
-// The easiest way to sort them in some deterministic order is to use fmt.
787
-// If this turns out to be inefficient we can always consider other options,
788
-// such as doing a Schwartzian transform.
789
-
790
-type mapKeys []reflect.Value
791
-
792
-func (s mapKeys) Len() int { return len(s) }
793
-func (s mapKeys) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
794
-func (s mapKeys) Less(i, j int) bool {
795
- return fmt.Sprint(s[i].Interface()) < fmt.Sprint(s[j].Interface())
796
-}
Godeps/_workspace/src/github.com/golang/protobuf/proto/message_set.go
deleted
-287
@@ -1,287 +0,0 @@
1
-// Go support for Protocol Buffers - Google's data interchange format
2
-//
3
-// Copyright 2010 The Go Authors. All rights reserved.
4
-// https://github.com/golang/protobuf
5
-//
6
-// Redistribution and use in source and binary forms, with or without
7
-// modification, are permitted provided that the following conditions are
8
-// met:
9
-//
10
-// * Redistributions of source code must retain the above copyright
11
-// notice, this list of conditions and the following disclaimer.
12
-// * Redistributions in binary form must reproduce the above
13
-// copyright notice, this list of conditions and the following disclaimer
14
-// in the documentation and/or other materials provided with the
15
-// distribution.
16
-// * Neither the name of Google Inc. nor the names of its
17
-// contributors may be used to endorse or promote products derived from
18
-// this software without specific prior written permission.
19
-//
20
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
-// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
-// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23
-// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24
-// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25
-// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26
-// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27
-// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28
-// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29
-// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
-// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
-
32
-package proto
33
-
34
-/*
35
- * Support for message sets.
36
- */
37
-
38
-import (
39
- "bytes"
40
- "encoding/json"
41
- "errors"
42
- "fmt"
43
- "reflect"
44
- "sort"
45
-)
46
-
47
-// ErrNoMessageTypeId occurs when a protocol buffer does not have a message type ID.
48
-// A message type ID is required for storing a protocol buffer in a message set.
49
-var ErrNoMessageTypeId = errors.New("proto does not have a message type ID")
50
-
51
-// The first two types (_MessageSet_Item and MessageSet)
52
-// model what the protocol compiler produces for the following protocol message:
53
-// message MessageSet {
54
-// repeated group Item = 1 {
55
-// required int32 type_id = 2;
56
-// required string message = 3;
57
-// };
58
-// }
59
-// That is the MessageSet wire format. We can't use a proto to generate these
60
-// because that would introduce a circular dependency between it and this package.
61
-//
62
-// When a proto1 proto has a field that looks like:
63
-// optional message<MessageSet> info = 3;
64
-// the protocol compiler produces a field in the generated struct that looks like:
65
-// Info *_proto_.MessageSet `protobuf:"bytes,3,opt,name=info"`
66
-// The package is automatically inserted so there is no need for that proto file to
67
-// import this package.
68
-
69
-type _MessageSet_Item struct {
70
- TypeId *int32 `protobuf:"varint,2,req,name=type_id"`
71
- Message []byte `protobuf:"bytes,3,req,name=message"`
72
-}
73
-
74
-type MessageSet struct {
75
- Item []*_MessageSet_Item `protobuf:"group,1,rep"`
76
- XXX_unrecognized []byte
77
- // TODO: caching?
78
-}
79
-
80
-// Make sure MessageSet is a Message.
81
-var _ Message = (*MessageSet)(nil)
82
-
83
-// messageTypeIder is an interface satisfied by a protocol buffer type
84
-// that may be stored in a MessageSet.
85
-type messageTypeIder interface {
86
- MessageTypeId() int32
87
-}
88
-
89
-func (ms *MessageSet) find(pb Message) *_MessageSet_Item {
90
- mti, ok := pb.(messageTypeIder)
91
- if !ok {
92
- return nil
93
- }
94
- id := mti.MessageTypeId()
95
- for _, item := range ms.Item {
96
- if *item.TypeId == id {
97
- return item
98
- }
99
- }
100
- return nil
101
-}
102
-
103
-func (ms *MessageSet) Has(pb Message) bool {
104
- if ms.find(pb) != nil {
105
- return true
106
- }
107
- return false
108
-}
109
-
110
-func (ms *MessageSet) Unmarshal(pb Message) error {
111
- if item := ms.find(pb); item != nil {
112
- return Unmarshal(item.Message, pb)
113
- }
114
- if _, ok := pb.(messageTypeIder); !ok {
115
- return ErrNoMessageTypeId
116
- }
117
- return nil // TODO: return error instead?
118
-}
119
-
120
-func (ms *MessageSet) Marshal(pb Message) error {
121
- msg, err := Marshal(pb)
122
- if err != nil {
123
- return err
124
- }
125
- if item := ms.find(pb); item != nil {
126
- // reuse existing item
127
- item.Message = msg
128
- return nil
129
- }
130
-
131
- mti, ok := pb.(messageTypeIder)
132
- if !ok {
133
- return ErrNoMessageTypeId
134
- }
135
-
136
- mtid := mti.MessageTypeId()
137
- ms.Item = append(ms.Item, &_MessageSet_Item{
138
- TypeId: &mtid,
139
- Message: msg,
140
- })
141
- return nil
142
-}
143
-
144
-func (ms *MessageSet) Reset() { *ms = MessageSet{} }
145
-func (ms *MessageSet) String() string { return CompactTextString(ms) }
146
-func (*MessageSet) ProtoMessage() {}
147
-
148
-// Support for the message_set_wire_format message option.
149
-
150
-func skipVarint(buf []byte) []byte {
151
- i := 0
152
- for ; buf[i]&0x80 != 0; i++ {
153
- }
154
- return buf[i+1:]
155
-}
156
-
157
-// MarshalMessageSet encodes the extension map represented by m in the message set wire format.
158
-// It is called by generated Marshal methods on protocol buffer messages with the message_set_wire_format option.
159
-func MarshalMessageSet(m map[int32]Extension) ([]byte, error) {
160
- if err := encodeExtensionMap(m); err != nil {
161
- return nil, err
162
- }
163
-
164
- // Sort extension IDs to provide a deterministic encoding.
165
- // See also enc_map in encode.go.
166
- ids := make([]int, 0, len(m))
167
- for id := range m {
168
- ids = append(ids, int(id))
169
- }
170
- sort.Ints(ids)
171
-
172
- ms := &MessageSet{Item: make([]*_MessageSet_Item, 0, len(m))}
173
- for _, id := range ids {
174
- e := m[int32(id)]
175
- // Remove the wire type and field number varint, as well as the length varint.
176
- msg := skipVarint(skipVarint(e.enc))
177
-
178
- ms.Item = append(ms.Item, &_MessageSet_Item{
179
- TypeId: Int32(int32(id)),
180
- Message: msg,
181
- })
182
- }
183
- return Marshal(ms)
184
-}
185
-
186
-// UnmarshalMessageSet decodes the extension map encoded in buf in the message set wire format.
187
-// It is called by generated Unmarshal methods on protocol buffer messages with the message_set_wire_format option.
188
-func UnmarshalMessageSet(buf []byte, m map[int32]Extension) error {
189
- ms := new(MessageSet)
190
- if err := Unmarshal(buf, ms); err != nil {
191
- return err
192
- }
193
- for _, item := range ms.Item {
194
- id := *item.TypeId
195
- msg := item.Message
196
-
197
- // Restore wire type and field number varint, plus length varint.
198
- // Be careful to preserve duplicate items.
199
- b := EncodeVarint(uint64(id)<<3 | WireBytes)
200
- if ext, ok := m[id]; ok {
201
- // Existing data; rip off the tag and length varint
202
- // so we join the new data correctly.
203
- // We can assume that ext.enc is set because we are unmarshaling.
204
- o := ext.enc[len(b):] // skip wire type and field number
205
- _, n := DecodeVarint(o) // calculate length of length varint
206
- o = o[n:] // skip length varint
207
- msg = append(o, msg...) // join old data and new data
208
- }
209
- b = append(b, EncodeVarint(uint64(len(msg)))...)
210
- b = append(b, msg...)
211
-
212
- m[id] = Extension{enc: b}
213
- }
214
- return nil
215
-}
216
-
217
-// MarshalMessageSetJSON encodes the extension map represented by m in JSON format.
218
-// It is called by generated MarshalJSON methods on protocol buffer messages with the message_set_wire_format option.
219
-func MarshalMessageSetJSON(m map[int32]Extension) ([]byte, error) {
220
- var b bytes.Buffer
221
- b.WriteByte('{')
222
-
223
- // Process the map in key order for deterministic output.
224
- ids := make([]int32, 0, len(m))
225
- for id := range m {
226
- ids = append(ids, id)
227
- }
228
- sort.Sort(int32Slice(ids)) // int32Slice defined in text.go
229
-
230
- for i, id := range ids {
231
- ext := m[id]
232
- if i > 0 {
233
- b.WriteByte(',')
234
- }
235
-
236
- msd, ok := messageSetMap[id]
237
- if !ok {
238
- // Unknown type; we can't render it, so skip it.
239
- continue
240
- }
241
- fmt.Fprintf(&b, `"[%s]":`, msd.name)
242
-
243
- x := ext.value
244
- if x == nil {
245
- x = reflect.New(msd.t.Elem()).Interface()
246
- if err := Unmarshal(ext.enc, x.(Message)); err != nil {
247
- return nil, err
248
- }
249
- }
250
- d, err := json.Marshal(x)
251
- if err != nil {
252
- return nil, err
253
- }
254
- b.Write(d)
255
- }
256
- b.WriteByte('}')
257
- return b.Bytes(), nil
258
-}
259
-
260
-// UnmarshalMessageSetJSON decodes the extension map encoded in buf in JSON format.
261
-// It is called by generated UnmarshalJSON methods on protocol buffer messages with the message_set_wire_format option.
262
-func UnmarshalMessageSetJSON(buf []byte, m map[int32]Extension) error {
263
- // Common-case fast path.
264
- if len(buf) == 0 || bytes.Equal(buf, []byte("{}")) {
265
- return nil
266
- }
267
-
268
- // This is fairly tricky, and it's not clear that it is needed.
269
- return errors.New("TODO: UnmarshalMessageSetJSON not yet implemented")
270
-}
271
-
272
-// A global registry of types that can be used in a MessageSet.
273
-
274
-var messageSetMap = make(map[int32]messageSetDesc)
275
-
276
-type messageSetDesc struct {
277
- t reflect.Type // pointer to struct
278
- name string
279
-}
280
-
281
-// RegisterMessageSetType is called from the generated code.
282
-func RegisterMessageSetType(m Message, fieldNum int32, name string) {
283
- messageSetMap[fieldNum] = messageSetDesc{
284
- t: reflect.TypeOf(m),
285
- name: name,
286
- }
287
-}
Godeps/_workspace/src/github.com/golang/protobuf/proto/message_set_test.go
deleted
-66
@@ -1,66 +0,0 @@
1
-// Go support for Protocol Buffers - Google's data interchange format
2
-//
3
-// Copyright 2014 The Go Authors. All rights reserved.
4
-// https://github.com/golang/protobuf
5
-//
6
-// Redistribution and use in source and binary forms, with or without
7
-// modification, are permitted provided that the following conditions are
8
-// met:
9
-//
10
-// * Redistributions of source code must retain the above copyright
11
-// notice, this list of conditions and the following disclaimer.
12
-// * Redistributions in binary form must reproduce the above
13
-// copyright notice, this list of conditions and the following disclaimer
14
-// in the documentation and/or other materials provided with the
15
-// distribution.
16
-// * Neither the name of Google Inc. nor the names of its
17
-// contributors may be used to endorse or promote products derived from
18
-// this software without specific prior written permission.
19
-//
20
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
-// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
-// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23
-// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24
-// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25
-// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26
-// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27
-// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28
-// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29
-// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
-// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
-
32
-package proto
33
-
34
-import (
35
- "bytes"
36
- "testing"
37
-)
38
-
39
-func TestUnmarshalMessageSetWithDuplicate(t *testing.T) {
40
- // Check that a repeated message set entry will be concatenated.
41
- in := &MessageSet{
42
- Item: []*_MessageSet_Item{
43
- {TypeId: Int32(12345), Message: []byte("hoo")},
44
- {TypeId: Int32(12345), Message: []byte("hah")},
45
- },
46
- }
47
- b, err := Marshal(in)
48
- if err != nil {
49
- t.Fatalf("Marshal: %v", err)
50
- }
51
- t.Logf("Marshaled bytes: %q", b)
52
-
53
- m := make(map[int32]Extension)
54
- if err := UnmarshalMessageSet(b, m); err != nil {
55
- t.Fatalf("UnmarshalMessageSet: %v", err)
56
- }
57
- ext, ok := m[12345]
58
- if !ok {
59
- t.Fatalf("Didn't retrieve extension 12345; map is %v", m)
60
- }
61
- // Skip wire type/field number and length varints.
62
- got := skipVarint(skipVarint(ext.enc))
63
- if want := []byte("hoohah"); !bytes.Equal(got, want) {
64
- t.Errorf("Combined extension is %q, want %q", got, want)
65
- }
66
-}
Godeps/_workspace/src/github.com/golang/protobuf/proto/pointer_reflect.go
deleted
-479
@@ -1,479 +0,0 @@
1
-// Go support for Protocol Buffers - Google's data interchange format
2
-//
3
-// Copyright 2012 The Go Authors. All rights reserved.
4
-// https://github.com/golang/protobuf
5
-//
6
-// Redistribution and use in source and binary forms, with or without
7
-// modification, are permitted provided that the following conditions are
8
-// met:
9
-//
10
-// * Redistributions of source code must retain the above copyright
11
-// notice, this list of conditions and the following disclaimer.
12
-// * Redistributions in binary form must reproduce the above
13
-// copyright notice, this list of conditions and the following disclaimer
14
-// in the documentation and/or other materials provided with the
15
-// distribution.
16
-// * Neither the name of Google Inc. nor the names of its
17
-// contributors may be used to endorse or promote products derived from
18
-// this software without specific prior written permission.
19
-//
20
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
-// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
-// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23
-// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24
-// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25
-// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26
-// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27
-// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28
-// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29
-// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
-// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
-
32
-// +build appengine
33
-
34
-// This file contains an implementation of proto field accesses using package reflect.
35
-// It is slower than the code in pointer_unsafe.go but it avoids package unsafe and can
36
-// be used on App Engine.
37
-
38
-package proto
39
-
40
-import (
41
- "math"
42
- "reflect"
43
-)
44
-
45
-// A structPointer is a pointer to a struct.
46
-type structPointer struct {
47
- v reflect.Value
48
-}
49
-
50
-// toStructPointer returns a structPointer equivalent to the given reflect value.
51
-// The reflect value must itself be a pointer to a struct.
52
-func toStructPointer(v reflect.Value) structPointer {
53
- return structPointer{v}
54
-}
55
-
56
-// IsNil reports whether p is nil.
57
-func structPointer_IsNil(p structPointer) bool {
58
- return p.v.IsNil()
59
-}
60
-
61
-// Interface returns the struct pointer as an interface value.
62
-func structPointer_Interface(p structPointer, _ reflect.Type) interface{} {
63
- return p.v.Interface()
64
-}
65
-
66
-// A field identifies a field in a struct, accessible from a structPointer.
67
-// In this implementation, a field is identified by the sequence of field indices
68
-// passed to reflect's FieldByIndex.
69
-type field []int
70
-
71
-// toField returns a field equivalent to the given reflect field.
72
-func toField(f *reflect.StructField) field {
73
- return f.Index
74
-}
75
-
76
-// invalidField is an invalid field identifier.
77
-var invalidField = field(nil)
78
-
79
-// IsValid reports whether the field identifier is valid.
80
-func (f field) IsValid() bool { return f != nil }
81
-
82
-// field returns the given field in the struct as a reflect value.
83
-func structPointer_field(p structPointer, f field) reflect.Value {
84
- // Special case: an extension map entry with a value of type T
85
- // passes a *T to the struct-handling code with a zero field,
86
- // expecting that it will be treated as equivalent to *struct{ X T },
87
- // which has the same memory layout. We have to handle that case
88
- // specially, because reflect will panic if we call FieldByIndex on a
89
- // non-struct.
90
- if f == nil {
91
- return p.v.Elem()
92
- }
93
-
94
- return p.v.Elem().FieldByIndex(f)
95
-}
96
-
97
-// ifield returns the given field in the struct as an interface value.
98
-func structPointer_ifield(p structPointer, f field) interface{} {
99
- return structPointer_field(p, f).Addr().Interface()
100
-}
101
-
102
-// Bytes returns the address of a []byte field in the struct.
103
-func structPointer_Bytes(p structPointer, f field) *[]byte {
104
- return structPointer_ifield(p, f).(*[]byte)
105
-}
106
-
107
-// BytesSlice returns the address of a [][]byte field in the struct.
108
-func structPointer_BytesSlice(p structPointer, f field) *[][]byte {
109
- return structPointer_ifield(p, f).(*[][]byte)
110
-}
111
-
112
-// Bool returns the address of a *bool field in the struct.
113
-func structPointer_Bool(p structPointer, f field) **bool {
114
- return structPointer_ifield(p, f).(**bool)
115
-}
116
-
117
-// BoolVal returns the address of a bool field in the struct.
118
-func structPointer_BoolVal(p structPointer, f field) *bool {
119
- return structPointer_ifield(p, f).(*bool)
120
-}
121
-
122
-// BoolSlice returns the address of a []bool field in the struct.
123
-func structPointer_BoolSlice(p structPointer, f field) *[]bool {
124
- return structPointer_ifield(p, f).(*[]bool)
125
-}
126
-
127
-// String returns the address of a *string field in the struct.
128
-func structPointer_String(p structPointer, f field) **string {
129
- return structPointer_ifield(p, f).(**string)
130
-}
131
-
132
-// StringVal returns the address of a string field in the struct.
133
-func structPointer_StringVal(p structPointer, f field) *string {
134
- return structPointer_ifield(p, f).(*string)
135
-}
136
-
137
-// StringSlice returns the address of a []string field in the struct.
138
-func structPointer_StringSlice(p structPointer, f field) *[]string {
139
- return structPointer_ifield(p, f).(*[]string)
140
-}
141
-
142
-// ExtMap returns the address of an extension map field in the struct.
143
-func structPointer_ExtMap(p structPointer, f field) *map[int32]Extension {
144
- return structPointer_ifield(p, f).(*map[int32]Extension)
145
-}
146
-
147
-// Map returns the reflect.Value for the address of a map field in the struct.
148
-func structPointer_Map(p structPointer, f field, typ reflect.Type) reflect.Value {
149
- return structPointer_field(p, f).Addr()
150
-}
151
-
152
-// SetStructPointer writes a *struct field in the struct.
153
-func structPointer_SetStructPointer(p structPointer, f field, q structPointer) {
154
- structPointer_field(p, f).Set(q.v)
155
-}
156
-
157
-// GetStructPointer reads a *struct field in the struct.
158
-func structPointer_GetStructPointer(p structPointer, f field) structPointer {
159
- return structPointer{structPointer_field(p, f)}
160
-}
161
-
162
-// StructPointerSlice the address of a []*struct field in the struct.
163
-func structPointer_StructPointerSlice(p structPointer, f field) structPointerSlice {
164
- return structPointerSlice{structPointer_field(p, f)}
165
-}
166
-
167
-// A structPointerSlice represents the address of a slice of pointers to structs
168
-// (themselves messages or groups). That is, v.Type() is *[]*struct{...}.
169
-type structPointerSlice struct {
170
- v reflect.Value
171
-}
172
-
173
-func (p structPointerSlice) Len() int { return p.v.Len() }
174
-func (p structPointerSlice) Index(i int) structPointer { return structPointer{p.v.Index(i)} }
175
-func (p structPointerSlice) Append(q structPointer) {
176
- p.v.Set(reflect.Append(p.v, q.v))
177
-}
178
-
179
-var (
180
- int32Type = reflect.TypeOf(int32(0))
181
- uint32Type = reflect.TypeOf(uint32(0))
182
- float32Type = reflect.TypeOf(float32(0))
183
- int64Type = reflect.TypeOf(int64(0))
184
- uint64Type = reflect.TypeOf(uint64(0))
185
- float64Type = reflect.TypeOf(float64(0))
186
-)
187
-
188
-// A word32 represents a field of type *int32, *uint32, *float32, or *enum.
189
-// That is, v.Type() is *int32, *uint32, *float32, or *enum and v is assignable.
190
-type word32 struct {
191
- v reflect.Value
192
-}
193
-
194
-// IsNil reports whether p is nil.
195
-func word32_IsNil(p word32) bool {
196
- return p.v.IsNil()
197
-}
198
-
199
-// Set sets p to point at a newly allocated word with bits set to x.
200
-func word32_Set(p word32, o *Buffer, x uint32) {
201
- t := p.v.Type().Elem()
202
- switch t {
203
- case int32Type:
204
- if len(o.int32s) == 0 {
205
- o.int32s = make([]int32, uint32PoolSize)
206
- }
207
- o.int32s[0] = int32(x)
208
- p.v.Set(reflect.ValueOf(&o.int32s[0]))
209
- o.int32s = o.int32s[1:]
210
- return
211
- case uint32Type:
212
- if len(o.uint32s) == 0 {
213
- o.uint32s = make([]uint32, uint32PoolSize)
214
- }
215
- o.uint32s[0] = x
216
- p.v.Set(reflect.ValueOf(&o.uint32s[0]))
217
- o.uint32s = o.uint32s[1:]
218
- return
219
- case float32Type:
220
- if len(o.float32s) == 0 {
221
- o.float32s = make([]float32, uint32PoolSize)
222
- }
223
- o.float32s[0] = math.Float32frombits(x)
224
- p.v.Set(reflect.ValueOf(&o.float32s[0]))
225
- o.float32s = o.float32s[1:]
226
- return
227
- }
228
-
229
- // must be enum
230
- p.v.Set(reflect.New(t))
231
- p.v.Elem().SetInt(int64(int32(x)))
232
-}
233
-
234
-// Get gets the bits pointed at by p, as a uint32.
235
-func word32_Get(p word32) uint32 {
236
- elem := p.v.Elem()
237
- switch elem.Kind() {
238
- case reflect.Int32:
239
- return uint32(elem.Int())
240
- case reflect.Uint32:
241
- return uint32(elem.Uint())
242
- case reflect.Float32:
243
- return math.Float32bits(float32(elem.Float()))
244
- }
245
- panic("unreachable")
246
-}
247
-
248
-// Word32 returns a reference to a *int32, *uint32, *float32, or *enum field in the struct.
249
-func structPointer_Word32(p structPointer, f field) word32 {
250
- return word32{structPointer_field(p, f)}
251
-}
252
-
253
-// A word32Val represents a field of type int32, uint32, float32, or enum.
254
-// That is, v.Type() is int32, uint32, float32, or enum and v is assignable.
255
-type word32Val struct {
256
- v reflect.Value
257
-}
258
-
259
-// Set sets *p to x.
260
-func word32Val_Set(p word32Val, x uint32) {
261
- switch p.v.Type() {
262
- case int32Type:
263
- p.v.SetInt(int64(x))
264
- return
265
- case uint32Type:
266
- p.v.SetUint(uint64(x))
267
- return
268
- case float32Type:
269
- p.v.SetFloat(float64(math.Float32frombits(x)))
270
- return
271
- }
272
-
273
- // must be enum
274
- p.v.SetInt(int64(int32(x)))
275
-}
276
-
277
-// Get gets the bits pointed at by p, as a uint32.
278
-func word32Val_Get(p word32Val) uint32 {
279
- elem := p.v
280
- switch elem.Kind() {
281
- case reflect.Int32:
282
- return uint32(elem.Int())
283
- case reflect.Uint32:
284
- return uint32(elem.Uint())
285
- case reflect.Float32:
286
- return math.Float32bits(float32(elem.Float()))
287
- }
288
- panic("unreachable")
289
-}
290
-
291
-// Word32Val returns a reference to a int32, uint32, float32, or enum field in the struct.
292
-func structPointer_Word32Val(p structPointer, f field) word32Val {
293
- return word32Val{structPointer_field(p, f)}
294
-}
295
-
296
-// A word32Slice is a slice of 32-bit values.
297
-// That is, v.Type() is []int32, []uint32, []float32, or []enum.
298
-type word32Slice struct {
299
- v reflect.Value
300
-}
301
-
302
-func (p word32Slice) Append(x uint32) {
303
- n, m := p.v.Len(), p.v.Cap()
304
- if n < m {
305
- p.v.SetLen(n + 1)
306
- } else {
307
- t := p.v.Type().Elem()
308
- p.v.Set(reflect.Append(p.v, reflect.Zero(t)))
309
- }
310
- elem := p.v.Index(n)
311
- switch elem.Kind() {
312
- case reflect.Int32:
313
- elem.SetInt(int64(int32(x)))
314
- case reflect.Uint32:
315
- elem.SetUint(uint64(x))
316
- case reflect.Float32:
317
- elem.SetFloat(float64(math.Float32frombits(x)))
318
- }
319
-}
320
-
321
-func (p word32Slice) Len() int {
322
- return p.v.Len()
323
-}
324
-
325
-func (p word32Slice) Index(i int) uint32 {
326
- elem := p.v.Index(i)
327
- switch elem.Kind() {
328
- case reflect.Int32:
329
- return uint32(elem.Int())
330
- case reflect.Uint32:
331
- return uint32(elem.Uint())
332
- case reflect.Float32:
333
- return math.Float32bits(float32(elem.Float()))
334
- }
335
- panic("unreachable")
336
-}
337
-
338
-// Word32Slice returns a reference to a []int32, []uint32, []float32, or []enum field in the struct.
339
-func structPointer_Word32Slice(p structPointer, f field) word32Slice {
340
- return word32Slice{structPointer_field(p, f)}
341
-}
342
-
343
-// word64 is like word32 but for 64-bit values.
344
-type word64 struct {
345
- v reflect.Value
346
-}
347
-
348
-func word64_Set(p word64, o *Buffer, x uint64) {
349
- t := p.v.Type().Elem()
350
- switch t {
351
- case int64Type:
352
- if len(o.int64s) == 0 {
353
- o.int64s = make([]int64, uint64PoolSize)
354
- }
355
- o.int64s[0] = int64(x)
356
- p.v.Set(reflect.ValueOf(&o.int64s[0]))
357
- o.int64s = o.int64s[1:]
358
- return
359
- case uint64Type:
360
- if len(o.uint64s) == 0 {
361
- o.uint64s = make([]uint64, uint64PoolSize)
362
- }
363
- o.uint64s[0] = x
364
- p.v.Set(reflect.ValueOf(&o.uint64s[0]))
365
- o.uint64s = o.uint64s[1:]
366
- return
367
- case float64Type:
368
- if len(o.float64s) == 0 {
369
- o.float64s = make([]float64, uint64PoolSize)
370
- }
371
- o.float64s[0] = math.Float64frombits(x)
372
- p.v.Set(reflect.ValueOf(&o.float64s[0]))
373
- o.float64s = o.float64s[1:]
374
- return
375
- }
376
- panic("unreachable")
377
-}
378
-
379
-func word64_IsNil(p word64) bool {
380
- return p.v.IsNil()
381
-}
382
-
383
-func word64_Get(p word64) uint64 {
384
- elem := p.v.Elem()
385
- switch elem.Kind() {
386
- case reflect.Int64:
387
- return uint64(elem.Int())
388
- case reflect.Uint64:
389
- return elem.Uint()
390
- case reflect.Float64:
391
- return math.Float64bits(elem.Float())
392
- }
393
- panic("unreachable")
394
-}
395
-
396
-func structPointer_Word64(p structPointer, f field) word64 {
397
- return word64{structPointer_field(p, f)}
398
-}
399
-
400
-// word64Val is like word32Val but for 64-bit values.
401
-type word64Val struct {
402
- v reflect.Value
403
-}
404
-
405
-func word64Val_Set(p word64Val, o *Buffer, x uint64) {
406
- switch p.v.Type() {
407
- case int64Type:
408
- p.v.SetInt(int64(x))
409
- return
410
- case uint64Type:
411
- p.v.SetUint(x)
412
- return
413
- case float64Type:
414
- p.v.SetFloat(math.Float64frombits(x))
415
- return
416
- }
417
- panic("unreachable")
418
-}
419
-
420
-func word64Val_Get(p word64Val) uint64 {
421
- elem := p.v
422
- switch elem.Kind() {
423
- case reflect.Int64:
424
- return uint64(elem.Int())
425
- case reflect.Uint64:
426
- return elem.Uint()
427
- case reflect.Float64:
428
- return math.Float64bits(elem.Float())
429
- }
430
- panic("unreachable")
431
-}
432
-
433
-func structPointer_Word64Val(p structPointer, f field) word64Val {
434
- return word64Val{structPointer_field(p, f)}
435
-}
436
-
437
-type word64Slice struct {
438
- v reflect.Value
439
-}
440
-
441
-func (p word64Slice) Append(x uint64) {
442
- n, m := p.v.Len(), p.v.Cap()
443
- if n < m {
444
- p.v.SetLen(n + 1)
445
- } else {
446
- t := p.v.Type().Elem()
447
- p.v.Set(reflect.Append(p.v, reflect.Zero(t)))
448
- }
449
- elem := p.v.Index(n)
450
- switch elem.Kind() {
451
- case reflect.Int64:
452
- elem.SetInt(int64(int64(x)))
453
- case reflect.Uint64:
454
- elem.SetUint(uint64(x))
455
- case reflect.Float64:
456
- elem.SetFloat(float64(math.Float64frombits(x)))
457
- }
458
-}
459
-
460
-func (p word64Slice) Len() int {
461
- return p.v.Len()
462
-}
463
-
464
-func (p word64Slice) Index(i int) uint64 {
465
- elem := p.v.Index(i)
466
- switch elem.Kind() {
467
- case reflect.Int64:
468
- return uint64(elem.Int())
469
- case reflect.Uint64:
470
- return uint64(elem.Uint())
471
- case reflect.Float64:
472
- return math.Float64bits(float64(elem.Float()))
473
- }
474
- panic("unreachable")
475
-}
476
-
477
-func structPointer_Word64Slice(p structPointer, f field) word64Slice {
478
- return word64Slice{structPointer_field(p, f)}
479
-}
Godeps/_workspace/src/github.com/golang/protobuf/proto/pointer_unsafe.go
deleted
-266
@@ -1,266 +0,0 @@
1
-// Go support for Protocol Buffers - Google's data interchange format
2
-//
3
-// Copyright 2012 The Go Authors. All rights reserved.
4
-// https://github.com/golang/protobuf
5
-//
6
-// Redistribution and use in source and binary forms, with or without
7
-// modification, are permitted provided that the following conditions are
8
-// met:
9
-//
10
-// * Redistributions of source code must retain the above copyright
11
-// notice, this list of conditions and the following disclaimer.
12
-// * Redistributions in binary form must reproduce the above
13
-// copyright notice, this list of conditions and the following disclaimer
14
-// in the documentation and/or other materials provided with the
15
-// distribution.
16
-// * Neither the name of Google Inc. nor the names of its
17
-// contributors may be used to endorse or promote products derived from
18
-// this software without specific prior written permission.
19
-//
20
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
-// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
-// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23
-// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24
-// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25
-// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26
-// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27
-// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28
-// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29
-// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
-// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
-
32
-// +build !appengine
33
-
34
-// This file contains the implementation of the proto field accesses using package unsafe.
35
-
36
-package proto
37
-
38
-import (
39
- "reflect"
40
- "unsafe"
41
-)
42
-
43
-// NOTE: These type_Foo functions would more idiomatically be methods,
44
-// but Go does not allow methods on pointer types, and we must preserve
45
-// some pointer type for the garbage collector. We use these
46
-// funcs with clunky names as our poor approximation to methods.
47
-//
48
-// An alternative would be
49
-// type structPointer struct { p unsafe.Pointer }
50
-// but that does not registerize as well.
51
-
52
-// A structPointer is a pointer to a struct.
53
-type structPointer unsafe.Pointer
54
-
55
-// toStructPointer returns a structPointer equivalent to the given reflect value.
56
-func toStructPointer(v reflect.Value) structPointer {
57
- return structPointer(unsafe.Pointer(v.Pointer()))
58
-}
59
-
60
-// IsNil reports whether p is nil.
61
-func structPointer_IsNil(p structPointer) bool {
62
- return p == nil
63
-}
64
-
65
-// Interface returns the struct pointer, assumed to have element type t,
66
-// as an interface value.
67
-func structPointer_Interface(p structPointer, t reflect.Type) interface{} {
68
- return reflect.NewAt(t, unsafe.Pointer(p)).Interface()
69
-}
70
-
71
-// A field identifies a field in a struct, accessible from a structPointer.
72
-// In this implementation, a field is identified by its byte offset from the start of the struct.
73
-type field uintptr
74
-
75
-// toField returns a field equivalent to the given reflect field.
76
-func toField(f *reflect.StructField) field {
77
- return field(f.Offset)
78
-}
79
-
80
-// invalidField is an invalid field identifier.
81
-const invalidField = ^field(0)
82
-
83
-// IsValid reports whether the field identifier is valid.
84
-func (f field) IsValid() bool {
85
- return f != ^field(0)
86
-}
87
-
88
-// Bytes returns the address of a []byte field in the struct.
89
-func structPointer_Bytes(p structPointer, f field) *[]byte {
90
- return (*[]byte)(unsafe.Pointer(uintptr(p) + uintptr(f)))
91
-}
92
-
93
-// BytesSlice returns the address of a [][]byte field in the struct.
94
-func structPointer_BytesSlice(p structPointer, f field) *[][]byte {
95
- return (*[][]byte)(unsafe.Pointer(uintptr(p) + uintptr(f)))
96
-}
97
-
98
-// Bool returns the address of a *bool field in the struct.
99
-func structPointer_Bool(p structPointer, f field) **bool {
100
- return (**bool)(unsafe.Pointer(uintptr(p) + uintptr(f)))
101
-}
102
-
103
-// BoolVal returns the address of a bool field in the struct.
104
-func structPointer_BoolVal(p structPointer, f field) *bool {
105
- return (*bool)(unsafe.Pointer(uintptr(p) + uintptr(f)))
106
-}
107
-
108
-// BoolSlice returns the address of a []bool field in the struct.
109
-func structPointer_BoolSlice(p structPointer, f field) *[]bool {
110
- return (*[]bool)(unsafe.Pointer(uintptr(p) + uintptr(f)))
111
-}
112
-
113
-// String returns the address of a *string field in the struct.
114
-func structPointer_String(p structPointer, f field) **string {
115
- return (**string)(unsafe.Pointer(uintptr(p) + uintptr(f)))
116
-}
117
-
118
-// StringVal returns the address of a string field in the struct.
119
-func structPointer_StringVal(p structPointer, f field) *string {
120
- return (*string)(unsafe.Pointer(uintptr(p) + uintptr(f)))
121
-}
122
-
123
-// StringSlice returns the address of a []string field in the struct.
124
-func structPointer_StringSlice(p structPointer, f field) *[]string {
125
- return (*[]string)(unsafe.Pointer(uintptr(p) + uintptr(f)))
126
-}
127
-
128
-// ExtMap returns the address of an extension map field in the struct.
129
-func structPointer_ExtMap(p structPointer, f field) *map[int32]Extension {
130
- return (*map[int32]Extension)(unsafe.Pointer(uintptr(p) + uintptr(f)))
131
-}
132
-
133
-// Map returns the reflect.Value for the address of a map field in the struct.
134
-func structPointer_Map(p structPointer, f field, typ reflect.Type) reflect.Value {
135
- return reflect.NewAt(typ, unsafe.Pointer(uintptr(p)+uintptr(f)))
136
-}
137
-
138
-// SetStructPointer writes a *struct field in the struct.
139
-func structPointer_SetStructPointer(p structPointer, f field, q structPointer) {
140
- *(*structPointer)(unsafe.Pointer(uintptr(p) + uintptr(f))) = q
141
-}
142
-
143
-// GetStructPointer reads a *struct field in the struct.
144
-func structPointer_GetStructPointer(p structPointer, f field) structPointer {
145
- return *(*structPointer)(unsafe.Pointer(uintptr(p) + uintptr(f)))
146
-}
147
-
148
-// StructPointerSlice the address of a []*struct field in the struct.
149
-func structPointer_StructPointerSlice(p structPointer, f field) *structPointerSlice {
150
- return (*structPointerSlice)(unsafe.Pointer(uintptr(p) + uintptr(f)))
151
-}
152
-
153
-// A structPointerSlice represents a slice of pointers to structs (themselves submessages or groups).
154
-type structPointerSlice []structPointer
155
-
156
-func (v *structPointerSlice) Len() int { return len(*v) }
157
-func (v *structPointerSlice) Index(i int) structPointer { return (*v)[i] }
158
-func (v *structPointerSlice) Append(p structPointer) { *v = append(*v, p) }
159
-
160
-// A word32 is the address of a "pointer to 32-bit value" field.
161
-type word32 **uint32
162
-
163
-// IsNil reports whether *v is nil.
164
-func word32_IsNil(p word32) bool {
165
- return *p == nil
166
-}
167
-
168
-// Set sets *v to point at a newly allocated word set to x.
169
-func word32_Set(p word32, o *Buffer, x uint32) {
170
- if len(o.uint32s) == 0 {
171
- o.uint32s = make([]uint32, uint32PoolSize)
172
- }
173
- o.uint32s[0] = x
174
- *p = &o.uint32s[0]
175
- o.uint32s = o.uint32s[1:]
176
-}
177
-
178
-// Get gets the value pointed at by *v.
179
-func word32_Get(p word32) uint32 {
180
- return **p
181
-}
182
-
183
-// Word32 returns the address of a *int32, *uint32, *float32, or *enum field in the struct.
184
-func structPointer_Word32(p structPointer, f field) word32 {
185
- return word32((**uint32)(unsafe.Pointer(uintptr(p) + uintptr(f))))
186
-}
187
-
188
-// A word32Val is the address of a 32-bit value field.
189
-type word32Val *uint32
190
-
191
-// Set sets *p to x.
192
-func word32Val_Set(p word32Val, x uint32) {
193
- *p = x
194
-}
195
-
196
-// Get gets the value pointed at by p.
197
-func word32Val_Get(p word32Val) uint32 {
198
- return *p
199
-}
200
-
201
-// Word32Val returns the address of a *int32, *uint32, *float32, or *enum field in the struct.
202
-func structPointer_Word32Val(p structPointer, f field) word32Val {
203
- return word32Val((*uint32)(unsafe.Pointer(uintptr(p) + uintptr(f))))
204
-}
205
-
206
-// A word32Slice is a slice of 32-bit values.
207
-type word32Slice []uint32
208
-
209
-func (v *word32Slice) Append(x uint32) { *v = append(*v, x) }
210
-func (v *word32Slice) Len() int { return len(*v) }
211
-func (v *word32Slice) Index(i int) uint32 { return (*v)[i] }
212
-
213
-// Word32Slice returns the address of a []int32, []uint32, []float32, or []enum field in the struct.
214
-func structPointer_Word32Slice(p structPointer, f field) *word32Slice {
215
- return (*word32Slice)(unsafe.Pointer(uintptr(p) + uintptr(f)))
216
-}
217
-
218
-// word64 is like word32 but for 64-bit values.
219
-type word64 **uint64
220
-
221
-func word64_Set(p word64, o *Buffer, x uint64) {
222
- if len(o.uint64s) == 0 {
223
- o.uint64s = make([]uint64, uint64PoolSize)
224
- }
225
- o.uint64s[0] = x
226
- *p = &o.uint64s[0]
227
- o.uint64s = o.uint64s[1:]
228
-}
229
-
230
-func word64_IsNil(p word64) bool {
231
- return *p == nil
232
-}
233
-
234
-func word64_Get(p word64) uint64 {
235
- return **p
236
-}
237
-
238
-func structPointer_Word64(p structPointer, f field) word64 {
239
- return word64((**uint64)(unsafe.Pointer(uintptr(p) + uintptr(f))))
240
-}
241
-
242
-// word64Val is like word32Val but for 64-bit values.
243
-type word64Val *uint64
244
-
245
-func word64Val_Set(p word64Val, o *Buffer, x uint64) {
246
- *p = x
247
-}
248
-
249
-func word64Val_Get(p word64Val) uint64 {
250
- return *p
251
-}
252
-
253
-func structPointer_Word64Val(p structPointer, f field) word64Val {
254
- return word64Val((*uint64)(unsafe.Pointer(uintptr(p) + uintptr(f))))
255
-}
256
-
257
-// word64Slice is like word32Slice but for 64-bit values.
258
-type word64Slice []uint64
259
-
260
-func (v *word64Slice) Append(x uint64) { *v = append(*v, x) }
261
-func (v *word64Slice) Len() int { return len(*v) }
262
-func (v *word64Slice) Index(i int) uint64 { return (*v)[i] }
263
-
264
-func structPointer_Word64Slice(p structPointer, f field) *word64Slice {
265
- return (*word64Slice)(unsafe.Pointer(uintptr(p) + uintptr(f)))
266
-}
Godeps/_workspace/src/github.com/golang/protobuf/proto/properties.go
deleted
-742
@@ -1,742 +0,0 @@
1
-// Go support for Protocol Buffers - Google's data interchange format
2
-//
3
-// Copyright 2010 The Go Authors. All rights reserved.
4
-// https://github.com/golang/protobuf
5
-//
6
-// Redistribution and use in source and binary forms, with or without
7
-// modification, are permitted provided that the following conditions are
8
-// met:
9
-//
10
-// * Redistributions of source code must retain the above copyright
11
-// notice, this list of conditions and the following disclaimer.
12
-// * Redistributions in binary form must reproduce the above
13
-// copyright notice, this list of conditions and the following disclaimer
14
-// in the documentation and/or other materials provided with the
15
-// distribution.
16
-// * Neither the name of Google Inc. nor the names of its
17
-// contributors may be used to endorse or promote products derived from
18
-// this software without specific prior written permission.
19
-//
20
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
-// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
-// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23
-// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24
-// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25
-// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26
-// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27
-// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28
-// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29
-// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
-// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
-
32
-package proto
33
-
34
-/*
35
- * Routines for encoding data into the wire format for protocol buffers.
36
- */
37
-
38
-import (
39
- "fmt"
40
- "os"
41
- "reflect"
42
- "sort"
43
- "strconv"
44
- "strings"
45
- "sync"
46
-)
47
-
48
-const debug bool = false
49
-
50
-// Constants that identify the encoding of a value on the wire.
51
-const (
52
- WireVarint = 0
53
- WireFixed64 = 1
54
- WireBytes = 2
55
- WireStartGroup = 3
56
- WireEndGroup = 4
57
- WireFixed32 = 5
58
-)
59
-
60
-const startSize = 10 // initial slice/string sizes
61
-
62
-// Encoders are defined in encode.go
63
-// An encoder outputs the full representation of a field, including its
64
-// tag and encoder type.
65
-type encoder func(p *Buffer, prop *Properties, base structPointer) error
66
-
67
-// A valueEncoder encodes a single integer in a particular encoding.
68
-type valueEncoder func(o *Buffer, x uint64) error
69
-
70
-// Sizers are defined in encode.go
71
-// A sizer returns the encoded size of a field, including its tag and encoder
72
-// type.
73
-type sizer func(prop *Properties, base structPointer) int
74
-
75
-// A valueSizer returns the encoded size of a single integer in a particular
76
-// encoding.
77
-type valueSizer func(x uint64) int
78
-
79
-// Decoders are defined in decode.go
80
-// A decoder creates a value from its wire representation.
81
-// Unrecognized subelements are saved in unrec.
82
-type decoder func(p *Buffer, prop *Properties, base structPointer) error
83
-
84
-// A valueDecoder decodes a single integer in a particular encoding.
85
-type valueDecoder func(o *Buffer) (x uint64, err error)
86
-
87
-// tagMap is an optimization over map[int]int for typical protocol buffer
88
-// use-cases. Encoded protocol buffers are often in tag order with small tag
89
-// numbers.
90
-type tagMap struct {
91
- fastTags []int
92
- slowTags map[int]int
93
-}
94
-
95
-// tagMapFastLimit is the upper bound on the tag number that will be stored in
96
-// the tagMap slice rather than its map.
97
-const tagMapFastLimit = 1024
98
-
99
-func (p *tagMap) get(t int) (int, bool) {
100
- if t > 0 && t < tagMapFastLimit {
101
- if t >= len(p.fastTags) {
102
- return 0, false
103
- }
104
- fi := p.fastTags[t]
105
- return fi, fi >= 0
106
- }
107
- fi, ok := p.slowTags[t]
108
- return fi, ok
109
-}
110
-
111
-func (p *tagMap) put(t int, fi int) {
112
- if t > 0 && t < tagMapFastLimit {
113
- for len(p.fastTags) < t+1 {
114
- p.fastTags = append(p.fastTags, -1)
115
- }
116
- p.fastTags[t] = fi
117
- return
118
- }
119
- if p.slowTags == nil {
120
- p.slowTags = make(map[int]int)
121
- }
122
- p.slowTags[t] = fi
123
-}
124
-
125
-// StructProperties represents properties for all the fields of a struct.
126
-// decoderTags and decoderOrigNames should only be used by the decoder.
127
-type StructProperties struct {
128
- Prop []*Properties // properties for each field
129
- reqCount int // required count
130
- decoderTags tagMap // map from proto tag to struct field number
131
- decoderOrigNames map[string]int // map from original name to struct field number
132
- order []int // list of struct field numbers in tag order
133
- unrecField field // field id of the XXX_unrecognized []byte field
134
- extendable bool // is this an extendable proto
135
-}
136
-
137
-// Implement the sorting interface so we can sort the fields in tag order, as recommended by the spec.
138
-// See encode.go, (*Buffer).enc_struct.
139
-
140
-func (sp *StructProperties) Len() int { return len(sp.order) }
141
-func (sp *StructProperties) Less(i, j int) bool {
142
- return sp.Prop[sp.order[i]].Tag < sp.Prop[sp.order[j]].Tag
143
-}
144
-func (sp *StructProperties) Swap(i, j int) { sp.order[i], sp.order[j] = sp.order[j], sp.order[i] }
145
-
146
-// Properties represents the protocol-specific behavior of a single struct field.
147
-type Properties struct {
148
- Name string // name of the field, for error messages
149
- OrigName string // original name before protocol compiler (always set)
150
- Wire string
151
- WireType int
152
- Tag int
153
- Required bool
154
- Optional bool
155
- Repeated bool
156
- Packed bool // relevant for repeated primitives only
157
- Enum string // set for enum types only
158
- proto3 bool // whether this is known to be a proto3 field; set for []byte only
159
-
160
- Default string // default value
161
- HasDefault bool // whether an explicit default was provided
162
- def_uint64 uint64
163
-
164
- enc encoder
165
- valEnc valueEncoder // set for bool and numeric types only
166
- field field
167
- tagcode []byte // encoding of EncodeVarint((Tag<<3)|WireType)
168
- tagbuf [8]byte
169
- stype reflect.Type // set for struct types only
170
- sprop *StructProperties // set for struct types only
171
- isMarshaler bool
172
- isUnmarshaler bool
173
-
174
- mtype reflect.Type // set for map types only
175
- mkeyprop *Properties // set for map types only
176
- mvalprop *Properties // set for map types only
177
-
178
- size sizer
179
- valSize valueSizer // set for bool and numeric types only
180
-
181
- dec decoder
182
- valDec valueDecoder // set for bool and numeric types only
183
-
184
- // If this is a packable field, this will be the decoder for the packed version of the field.
185
- packedDec decoder
186
-}
187
-
188
-// String formats the properties in the protobuf struct field tag style.
189
-func (p *Properties) String() string {
190
- s := p.Wire
191
- s = ","
192
- s += strconv.Itoa(p.Tag)
193
- if p.Required {
194
- s += ",req"
195
- }
196
- if p.Optional {
197
- s += ",opt"
198
- }
199
- if p.Repeated {
200
- s += ",rep"
201
- }
202
- if p.Packed {
203
- s += ",packed"
204
- }
205
- if p.OrigName != p.Name {
206
- s += ",name=" + p.OrigName
207
- }
208
- if p.proto3 {
209
- s += ",proto3"
210
- }
211
- if len(p.Enum) > 0 {
212
- s += ",enum=" + p.Enum
213
- }
214
- if p.HasDefault {
215
- s += ",def=" + p.Default
216
- }
217
- return s
218
-}
219
-
220
-// Parse populates p by parsing a string in the protobuf struct field tag style.
221
-func (p *Properties) Parse(s string) {
222
- // "bytes,49,opt,name=foo,def=hello!"
223
- fields := strings.Split(s, ",") // breaks def=, but handled below.
224
- if len(fields) < 2 {
225
- fmt.Fprintf(os.Stderr, "proto: tag has too few fields: %q\n", s)
226
- return
227
- }
228
-
229
- p.Wire = fields[0]
230
- switch p.Wire {
231
- case "varint":
232
- p.WireType = WireVarint
233
- p.valEnc = (*Buffer).EncodeVarint
234
- p.valDec = (*Buffer).DecodeVarint
235
- p.valSize = sizeVarint
236
- case "fixed32":
237
- p.WireType = WireFixed32
238
- p.valEnc = (*Buffer).EncodeFixed32
239
- p.valDec = (*Buffer).DecodeFixed32
240
- p.valSize = sizeFixed32
241
- case "fixed64":
242
- p.WireType = WireFixed64
243
- p.valEnc = (*Buffer).EncodeFixed64
244
- p.valDec = (*Buffer).DecodeFixed64
245
- p.valSize = sizeFixed64
246
- case "zigzag32":
247
- p.WireType = WireVarint
248
- p.valEnc = (*Buffer).EncodeZigzag32
249
- p.valDec = (*Buffer).DecodeZigzag32
250
- p.valSize = sizeZigzag32
251
- case "zigzag64":
252
- p.WireType = WireVarint
253
- p.valEnc = (*Buffer).EncodeZigzag64
254
- p.valDec = (*Buffer).DecodeZigzag64
255
- p.valSize = sizeZigzag64
256
- case "bytes", "group":
257
- p.WireType = WireBytes
258
- // no numeric converter for non-numeric types
259
- default:
260
- fmt.Fprintf(os.Stderr, "proto: tag has unknown wire type: %q\n", s)
261
- return
262
- }
263
-
264
- var err error
265
- p.Tag, err = strconv.Atoi(fields[1])
266
- if err != nil {
267
- return
268
- }
269
-
270
- for i := 2; i < len(fields); i++ {
271
- f := fields[i]
272
- switch {
273
- case f == "req":
274
- p.Required = true
275
- case f == "opt":
276
- p.Optional = true
277
- case f == "rep":
278
- p.Repeated = true
279
- case f == "packed":
280
- p.Packed = true
281
- case strings.HasPrefix(f, "name="):
282
- p.OrigName = f[5:]
283
- case strings.HasPrefix(f, "enum="):
284
- p.Enum = f[5:]
285
- case f == "proto3":
286
- p.proto3 = true
287
- case strings.HasPrefix(f, "def="):
288
- p.HasDefault = true
289
- p.Default = f[4:] // rest of string
290
- if i+1 < len(fields) {
291
- // Commas aren't escaped, and def is always last.
292
- p.Default += "," + strings.Join(fields[i+1:], ",")
293
- break
294
- }
295
- }
296
- }
297
-}
298
-
299
-func logNoSliceEnc(t1, t2 reflect.Type) {
300
- fmt.Fprintf(os.Stderr, "proto: no slice oenc for %T = []%T\n", t1, t2)
301
-}
302
-
303
-var protoMessageType = reflect.TypeOf((*Message)(nil)).Elem()
304
-
305
-// Initialize the fields for encoding and decoding.
306
-func (p *Properties) setEncAndDec(typ reflect.Type, f *reflect.StructField, lockGetProp bool) {
307
- p.enc = nil
308
- p.dec = nil
309
- p.size = nil
310
-
311
- switch t1 := typ; t1.Kind() {
312
- default:
313
- fmt.Fprintf(os.Stderr, "proto: no coders for %v\n", t1)
314
-
315
- // proto3 scalar types
316
-
317
- case reflect.Bool:
318
- p.enc = (*Buffer).enc_proto3_bool
319
- p.dec = (*Buffer).dec_proto3_bool
320
- p.size = size_proto3_bool
321
- case reflect.Int32:
322
- p.enc = (*Buffer).enc_proto3_int32
323
- p.dec = (*Buffer).dec_proto3_int32
324
- p.size = size_proto3_int32
325
- case reflect.Uint32:
326
- p.enc = (*Buffer).enc_proto3_uint32
327
- p.dec = (*Buffer).dec_proto3_int32 // can reuse
328
- p.size = size_proto3_uint32
329
- case reflect.Int64, reflect.Uint64:
330
- p.enc = (*Buffer).enc_proto3_int64
331
- p.dec = (*Buffer).dec_proto3_int64
332
- p.size = size_proto3_int64
333
- case reflect.Float32:
334
- p.enc = (*Buffer).enc_proto3_uint32 // can just treat them as bits
335
- p.dec = (*Buffer).dec_proto3_int32
336
- p.size = size_proto3_uint32
337
- case reflect.Float64:
338
- p.enc = (*Buffer).enc_proto3_int64 // can just treat them as bits
339
- p.dec = (*Buffer).dec_proto3_int64
340
- p.size = size_proto3_int64
341
- case reflect.String:
342
- p.enc = (*Buffer).enc_proto3_string
343
- p.dec = (*Buffer).dec_proto3_string
344
- p.size = size_proto3_string
345
-
346
- case reflect.Ptr:
347
- switch t2 := t1.Elem(); t2.Kind() {
348
- default:
349
- fmt.Fprintf(os.Stderr, "proto: no encoder function for %v -> %v\n", t1, t2)
350
- break
351
- case reflect.Bool:
352
- p.enc = (*Buffer).enc_bool
353
- p.dec = (*Buffer).dec_bool
354
- p.size = size_bool
355
- case reflect.Int32:
356
- p.enc = (*Buffer).enc_int32
357
- p.dec = (*Buffer).dec_int32
358
- p.size = size_int32
359
- case reflect.Uint32:
360
- p.enc = (*Buffer).enc_uint32
361
- p.dec = (*Buffer).dec_int32 // can reuse
362
- p.size = size_uint32
363
- case reflect.Int64, reflect.Uint64:
364
- p.enc = (*Buffer).enc_int64
365
- p.dec = (*Buffer).dec_int64
366
- p.size = size_int64
367
- case reflect.Float32:
368
- p.enc = (*Buffer).enc_uint32 // can just treat them as bits
369
- p.dec = (*Buffer).dec_int32
370
- p.size = size_uint32
371
- case reflect.Float64:
372
- p.enc = (*Buffer).enc_int64 // can just treat them as bits
373
- p.dec = (*Buffer).dec_int64
374
- p.size = size_int64
375
- case reflect.String:
376
- p.enc = (*Buffer).enc_string
377
- p.dec = (*Buffer).dec_string
378
- p.size = size_string
379
- case reflect.Struct:
380
- p.stype = t1.Elem()
381
- p.isMarshaler = isMarshaler(t1)
382
- p.isUnmarshaler = isUnmarshaler(t1)
383
- if p.Wire == "bytes" {
384
- p.enc = (*Buffer).enc_struct_message
385
- p.dec = (*Buffer).dec_struct_message
386
- p.size = size_struct_message
387
- } else {
388
- p.enc = (*Buffer).enc_struct_group
389
- p.dec = (*Buffer).dec_struct_group
390
- p.size = size_struct_group
391
- }
392
- }
393
-
394
- case reflect.Slice:
395
- switch t2 := t1.Elem(); t2.Kind() {
396
- default:
397
- logNoSliceEnc(t1, t2)
398
- break
399
- case reflect.Bool:
400
- if p.Packed {
401
- p.enc = (*Buffer).enc_slice_packed_bool
402
- p.size = size_slice_packed_bool
403
- } else {
404
- p.enc = (*Buffer).enc_slice_bool
405
- p.size = size_slice_bool
406
- }
407
- p.dec = (*Buffer).dec_slice_bool
408
- p.packedDec = (*Buffer).dec_slice_packed_bool
409
- case reflect.Int32:
410
- if p.Packed {
411
- p.enc = (*Buffer).enc_slice_packed_int32
412
- p.size = size_slice_packed_int32
413
- } else {
414
- p.enc = (*Buffer).enc_slice_int32
415
- p.size = size_slice_int32
416
- }
417
- p.dec = (*Buffer).dec_slice_int32
418
- p.packedDec = (*Buffer).dec_slice_packed_int32
419
- case reflect.Uint32:
420
- if p.Packed {
421
- p.enc = (*Buffer).enc_slice_packed_uint32
422
- p.size = size_slice_packed_uint32
423
- } else {
424
- p.enc = (*Buffer).enc_slice_uint32
425
- p.size = size_slice_uint32
426
- }
427
- p.dec = (*Buffer).dec_slice_int32
428
- p.packedDec = (*Buffer).dec_slice_packed_int32
429
- case reflect.Int64, reflect.Uint64:
430
- if p.Packed {
431
- p.enc = (*Buffer).enc_slice_packed_int64
432
- p.size = size_slice_packed_int64
433
- } else {
434
- p.enc = (*Buffer).enc_slice_int64
435
- p.size = size_slice_int64
436
- }
437
- p.dec = (*Buffer).dec_slice_int64
438
- p.packedDec = (*Buffer).dec_slice_packed_int64
439
- case reflect.Uint8:
440
- p.enc = (*Buffer).enc_slice_byte
441
- p.dec = (*Buffer).dec_slice_byte
442
- p.size = size_slice_byte
443
- // This is a []byte, which is either a bytes field,
444
- // or the value of a map field. In the latter case,
445
- // we always encode an empty []byte, so we should not
446
- // use the proto3 enc/size funcs.
447
- // f == nil iff this is the key/value of a map field.
448
- if p.proto3 && f != nil {
449
- p.enc = (*Buffer).enc_proto3_slice_byte
450
- p.size = size_proto3_slice_byte
451
- }
452
- case reflect.Float32, reflect.Float64:
453
- switch t2.Bits() {
454
- case 32:
455
- // can just treat them as bits
456
- if p.Packed {
457
- p.enc = (*Buffer).enc_slice_packed_uint32
458
- p.size = size_slice_packed_uint32
459
- } else {
460
- p.enc = (*Buffer).enc_slice_uint32
461
- p.size = size_slice_uint32
462
- }
463
- p.dec = (*Buffer).dec_slice_int32
464
- p.packedDec = (*Buffer).dec_slice_packed_int32
465
- case 64:
466
- // can just treat them as bits
467
- if p.Packed {
468
- p.enc = (*Buffer).enc_slice_packed_int64
469
- p.size = size_slice_packed_int64
470
- } else {
471
- p.enc = (*Buffer).enc_slice_int64
472
- p.size = size_slice_int64
473
- }
474
- p.dec = (*Buffer).dec_slice_int64
475
- p.packedDec = (*Buffer).dec_slice_packed_int64
476
- default:
477
- logNoSliceEnc(t1, t2)
478
- break
479
- }
480
- case reflect.String:
481
- p.enc = (*Buffer).enc_slice_string
482
- p.dec = (*Buffer).dec_slice_string
483
- p.size = size_slice_string
484
- case reflect.Ptr:
485
- switch t3 := t2.Elem(); t3.Kind() {
486
- default:
487
- fmt.Fprintf(os.Stderr, "proto: no ptr oenc for %T -> %T -> %T\n", t1, t2, t3)
488
- break
489
- case reflect.Struct:
490
- p.stype = t2.Elem()
491
- p.isMarshaler = isMarshaler(t2)
492
- p.isUnmarshaler = isUnmarshaler(t2)
493
- if p.Wire == "bytes" {
494
- p.enc = (*Buffer).enc_slice_struct_message
495
- p.dec = (*Buffer).dec_slice_struct_message
496
- p.size = size_slice_struct_message
497
- } else {
498
- p.enc = (*Buffer).enc_slice_struct_group
499
- p.dec = (*Buffer).dec_slice_struct_group
500
- p.size = size_slice_struct_group
501
- }
502
- }
503
- case reflect.Slice:
504
- switch t2.Elem().Kind() {
505
- default:
506
- fmt.Fprintf(os.Stderr, "proto: no slice elem oenc for %T -> %T -> %T\n", t1, t2, t2.Elem())
507
- break
508
- case reflect.Uint8:
509
- p.enc = (*Buffer).enc_slice_slice_byte
510
- p.dec = (*Buffer).dec_slice_slice_byte
511
- p.size = size_slice_slice_byte
512
- }
513
- }
514
-
515
- case reflect.Map:
516
- p.enc = (*Buffer).enc_new_map
517
- p.dec = (*Buffer).dec_new_map
518
- p.size = size_new_map
519
-
520
- p.mtype = t1
521
- p.mkeyprop = &Properties{}
522
- p.mkeyprop.init(reflect.PtrTo(p.mtype.Key()), "Key", f.Tag.Get("protobuf_key"), nil, lockGetProp)
523
- p.mvalprop = &Properties{}
524
- vtype := p.mtype.Elem()
525
- if vtype.Kind() != reflect.Ptr && vtype.Kind() != reflect.Slice {
526
- // The value type is not a message (*T) or bytes ([]byte),
527
- // so we need encoders for the pointer to this type.
528
- vtype = reflect.PtrTo(vtype)
529
- }
530
- p.mvalprop.init(vtype, "Value", f.Tag.Get("protobuf_val"), nil, lockGetProp)
531
- }
532
-
533
- // precalculate tag code
534
- wire := p.WireType
535
- if p.Packed {
536
- wire = WireBytes
537
- }
538
- x := uint32(p.Tag)<<3 | uint32(wire)
539
- i := 0
540
- for i = 0; x > 127; i++ {
541
- p.tagbuf[i] = 0x80 | uint8(x&0x7F)
542
- x >>= 7
543
- }
544
- p.tagbuf[i] = uint8(x)
545
- p.tagcode = p.tagbuf[0 : i+1]
546
-
547
- if p.stype != nil {
548
- if lockGetProp {
549
- p.sprop = GetProperties(p.stype)
550
- } else {
551
- p.sprop = getPropertiesLocked(p.stype)
552
- }
553
- }
554
-}
555
-
556
-var (
557
- marshalerType = reflect.TypeOf((*Marshaler)(nil)).Elem()
558
- unmarshalerType = reflect.TypeOf((*Unmarshaler)(nil)).Elem()
559
-)
560
-
561
-// isMarshaler reports whether type t implements Marshaler.
562
-func isMarshaler(t reflect.Type) bool {
563
- // We're checking for (likely) pointer-receiver methods
564
- // so if t is not a pointer, something is very wrong.
565
- // The calls above only invoke isMarshaler on pointer types.
566
- if t.Kind() != reflect.Ptr {
567
- panic("proto: misuse of isMarshaler")
568
- }
569
- return t.Implements(marshalerType)
570
-}
571
-
572
-// isUnmarshaler reports whether type t implements Unmarshaler.
573
-func isUnmarshaler(t reflect.Type) bool {
574
- // We're checking for (likely) pointer-receiver methods
575
- // so if t is not a pointer, something is very wrong.
576
- // The calls above only invoke isUnmarshaler on pointer types.
577
- if t.Kind() != reflect.Ptr {
578
- panic("proto: misuse of isUnmarshaler")
579
- }
580
- return t.Implements(unmarshalerType)
581
-}
582
-
583
-// Init populates the properties from a protocol buffer struct tag.
584
-func (p *Properties) Init(typ reflect.Type, name, tag string, f *reflect.StructField) {
585
- p.init(typ, name, tag, f, true)
586
-}
587
-
588
-func (p *Properties) init(typ reflect.Type, name, tag string, f *reflect.StructField, lockGetProp bool) {
589
- // "bytes,49,opt,def=hello!"
590
- p.Name = name
591
- p.OrigName = name
592
- if f != nil {
593
- p.field = toField(f)
594
- }
595
- if tag == "" {
596
- return
597
- }
598
- p.Parse(tag)
599
- p.setEncAndDec(typ, f, lockGetProp)
600
-}
601
-
602
-var (
603
- propertiesMu sync.RWMutex
604
- propertiesMap = make(map[reflect.Type]*StructProperties)
605
-)
606
-
607
-// GetProperties returns the list of properties for the type represented by t.
608
-// t must represent a generated struct type of a protocol message.
609
-func GetProperties(t reflect.Type) *StructProperties {
610
- if t.Kind() != reflect.Struct {
611
- panic("proto: type must have kind struct")
612
- }
613
-
614
- // Most calls to GetProperties in a long-running program will be
615
- // retrieving details for types we have seen before.
616
- propertiesMu.RLock()
617
- sprop, ok := propertiesMap[t]
618
- propertiesMu.RUnlock()
619
- if ok {
620
- if collectStats {
621
- stats.Chit++
622
- }
623
- return sprop
624
- }
625
-
626
- propertiesMu.Lock()
627
- sprop = getPropertiesLocked(t)
628
- propertiesMu.Unlock()
629
- return sprop
630
-}
631
-
632
-// getPropertiesLocked requires that propertiesMu is held.
633
-func getPropertiesLocked(t reflect.Type) *StructProperties {
634
- if prop, ok := propertiesMap[t]; ok {
635
- if collectStats {
636
- stats.Chit++
637
- }
638
- return prop
639
- }
640
- if collectStats {
641
- stats.Cmiss++
642
- }
643
-
644
- prop := new(StructProperties)
645
- // in case of recursive protos, fill this in now.
646
- propertiesMap[t] = prop
647
-
648
- // build properties
649
- prop.extendable = reflect.PtrTo(t).Implements(extendableProtoType)
650
- prop.unrecField = invalidField
651
- prop.Prop = make([]*Properties, t.NumField())
652
- prop.order = make([]int, t.NumField())
653
-
654
- for i := 0; i < t.NumField(); i++ {
655
- f := t.Field(i)
656
- p := new(Properties)
657
- name := f.Name
658
- p.init(f.Type, name, f.Tag.Get("protobuf"), &f, false)
659
-
660
- if f.Name == "XXX_extensions" { // special case
661
- p.enc = (*Buffer).enc_map
662
- p.dec = nil // not needed
663
- p.size = size_map
664
- }
665
- if f.Name == "XXX_unrecognized" { // special case
666
- prop.unrecField = toField(&f)
667
- }
668
- prop.Prop[i] = p
669
- prop.order[i] = i
670
- if debug {
671
- print(i, " ", f.Name, " ", t.String(), " ")
672
- if p.Tag > 0 {
673
- print(p.String())
674
- }
675
- print("\n")
676
- }
677
- if p.enc == nil && !strings.HasPrefix(f.Name, "XXX_") {
678
- fmt.Fprintln(os.Stderr, "proto: no encoder for", f.Name, f.Type.String(), "[GetProperties]")
679
- }
680
- }
681
-
682
- // Re-order prop.order.
683
- sort.Sort(prop)
684
-
685
- // build required counts
686
- // build tags
687
- reqCount := 0
688
- prop.decoderOrigNames = make(map[string]int)
689
- for i, p := range prop.Prop {
690
- if strings.HasPrefix(p.Name, "XXX_") {
691
- // Internal fields should not appear in tags/origNames maps.
692
- // They are handled specially when encoding and decoding.
693
- continue
694
- }
695
- if p.Required {
696
- reqCount++
697
- }
698
- prop.decoderTags.put(p.Tag, i)
699
- prop.decoderOrigNames[p.OrigName] = i
700
- }
701
- prop.reqCount = reqCount
702
-
703
- return prop
704
-}
705
-
706
-// Return the Properties object for the x[0]'th field of the structure.
707
-func propByIndex(t reflect.Type, x []int) *Properties {
708
- if len(x) != 1 {
709
- fmt.Fprintf(os.Stderr, "proto: field index dimension %d (not 1) for type %s\n", len(x), t)
710
- return nil
711
- }
712
- prop := GetProperties(t)
713
- return prop.Prop[x[0]]
714
-}
715
-
716
-// Get the address and type of a pointer to a struct from an interface.
717
-func getbase(pb Message) (t reflect.Type, b structPointer, err error) {
718
- if pb == nil {
719
- err = ErrNil
720
- return
721
- }
722
- // get the reflect type of the pointer to the struct.
723
- t = reflect.TypeOf(pb)
724
- // get the address of the struct.
725
- value := reflect.ValueOf(pb)
726
- b = toStructPointer(value)
727
- return
728
-}
729
-
730
-// A global registry of enum types.
731
-// The generated code will register the generated maps by calling RegisterEnum.
732
-
733
-var enumValueMaps = make(map[string]map[string]int32)
734
-
735
-// RegisterEnum is called from the generated code to install the enum descriptor
736
-// maps into the global table to aid parsing text format protocol buffers.
737
-func RegisterEnum(typeName string, unusedNameMap map[int32]string, valueMap map[string]int32) {
738
- if _, ok := enumValueMaps[typeName]; ok {
739
- panic("proto: duplicate enum registered: " + typeName)
740
- }
741
- enumValueMaps[typeName] = valueMap
742
-}
Godeps/_workspace/src/github.com/golang/protobuf/proto/proto3_proto/proto3.pb.go
deleted
-122
@@ -1,122 +0,0 @@
1
-// Code generated by protoc-gen-go.
2
-// source: proto3_proto/proto3.proto
3
-// DO NOT EDIT!
4
-
5
-/*
6
-Package proto3_proto is a generated protocol buffer package.
7
-
8
-It is generated from these files:
9
- proto3_proto/proto3.proto
10
-
11
-It has these top-level messages:
12
- Message
13
- Nested
14
- MessageWithMap
15
-*/
16
-package proto3_proto
17
-
18
-import proto "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/golang/protobuf/proto"
19
-import testdata "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/golang/protobuf/proto/testdata"
20
-
21
-// Reference imports to suppress errors if they are not otherwise used.
22
-var _ = proto.Marshal
23
-
24
-type Message_Humour int32
25
-
26
-const (
27
- Message_UNKNOWN Message_Humour = 0
28
- Message_PUNS Message_Humour = 1
29
- Message_SLAPSTICK Message_Humour = 2
30
- Message_BILL_BAILEY Message_Humour = 3
31
-)
32
-
33
-var Message_Humour_name = map[int32]string{
34
- 0: "UNKNOWN",
35
- 1: "PUNS",
36
- 2: "SLAPSTICK",
37
- 3: "BILL_BAILEY",
38
-}
39
-var Message_Humour_value = map[string]int32{
40
- "UNKNOWN": 0,
41
- "PUNS": 1,
42
- "SLAPSTICK": 2,
43
- "BILL_BAILEY": 3,
44
-}
45
-
46
-func (x Message_Humour) String() string {
47
- return proto.EnumName(Message_Humour_name, int32(x))
48
-}
49
-
50
-type Message struct {
51
- Name string `protobuf:"bytes,1,opt,name=name" json:"name,omitempty"`
52
- Hilarity Message_Humour `protobuf:"varint,2,opt,name=hilarity,enum=proto3_proto.Message_Humour" json:"hilarity,omitempty"`
53
- HeightInCm uint32 `protobuf:"varint,3,opt,name=height_in_cm" json:"height_in_cm,omitempty"`
54
- Data []byte `protobuf:"bytes,4,opt,name=data,proto3" json:"data,omitempty"`
55
- ResultCount int64 `protobuf:"varint,7,opt,name=result_count" json:"result_count,omitempty"`
56
- TrueScotsman bool `protobuf:"varint,8,opt,name=true_scotsman" json:"true_scotsman,omitempty"`
57
- Score float32 `protobuf:"fixed32,9,opt,name=score" json:"score,omitempty"`
58
- Key []uint64 `protobuf:"varint,5,rep,name=key" json:"key,omitempty"`
59
- Nested *Nested `protobuf:"bytes,6,opt,name=nested" json:"nested,omitempty"`
60
- Terrain map[string]*Nested `protobuf:"bytes,10,rep,name=terrain" json:"terrain,omitempty" protobuf_key:"bytes,1,opt,name=key" protobuf_val:"bytes,2,opt,name=value"`
61
- Proto2Field *testdata.SubDefaults `protobuf:"bytes,11,opt,name=proto2_field" json:"proto2_field,omitempty"`
62
- Proto2Value map[string]*testdata.SubDefaults `protobuf:"bytes,13,rep,name=proto2_value" json:"proto2_value,omitempty" protobuf_key:"bytes,1,opt,name=key" protobuf_val:"bytes,2,opt,name=value"`
63
-}
64
-
65
-func (m *Message) Reset() { *m = Message{} }
66
-func (m *Message) String() string { return proto.CompactTextString(m) }
67
-func (*Message) ProtoMessage() {}
68
-
69
-func (m *Message) GetNested() *Nested {
70
- if m != nil {
71
- return m.Nested
72
- }
73
- return nil
74
-}
75
-
76
-func (m *Message) GetTerrain() map[string]*Nested {
77
- if m != nil {
78
- return m.Terrain
79
- }
80
- return nil
81
-}
82
-
83
-func (m *Message) GetProto2Field() *testdata.SubDefaults {
84
- if m != nil {
85
- return m.Proto2Field
86
- }
87
- return nil
88
-}
89
-
90
-func (m *Message) GetProto2Value() map[string]*testdata.SubDefaults {
91
- if m != nil {
92
- return m.Proto2Value
93
- }
94
- return nil
95
-}
96
-
97
-type Nested struct {
98
- Bunny string `protobuf:"bytes,1,opt,name=bunny" json:"bunny,omitempty"`
99
-}
100
-
101
-func (m *Nested) Reset() { *m = Nested{} }
102
-func (m *Nested) String() string { return proto.CompactTextString(m) }
103
-func (*Nested) ProtoMessage() {}
104
-
105
-type MessageWithMap struct {
106
- ByteMapping map[bool][]byte `protobuf:"bytes,1,rep,name=byte_mapping" json:"byte_mapping,omitempty" protobuf_key:"varint,1,opt,name=key" protobuf_val:"bytes,2,opt,name=value,proto3"`
107
-}
108
-
109
-func (m *MessageWithMap) Reset() { *m = MessageWithMap{} }
110
-func (m *MessageWithMap) String() string { return proto.CompactTextString(m) }
111
-func (*MessageWithMap) ProtoMessage() {}
112
-
113
-func (m *MessageWithMap) GetByteMapping() map[bool][]byte {
114
- if m != nil {
115
- return m.ByteMapping
116
- }
117
- return nil
118
-}
119
-
120
-func init() {
121
- proto.RegisterEnum("proto3_proto.Message_Humour", Message_Humour_name, Message_Humour_value)
122
-}
Godeps/_workspace/src/github.com/golang/protobuf/proto/proto3_proto/proto3.proto
deleted
-68
@@ -1,68 +0,0 @@
1
-// Go support for Protocol Buffers - Google's data interchange format
2
-//
3
-// Copyright 2014 The Go Authors. All rights reserved.
4
-// https://github.com/golang/protobuf
5
-//
6
-// Redistribution and use in source and binary forms, with or without
7
-// modification, are permitted provided that the following conditions are
8
-// met:
9
-//
10
-// * Redistributions of source code must retain the above copyright
11
-// notice, this list of conditions and the following disclaimer.
12
-// * Redistributions in binary form must reproduce the above
13
-// copyright notice, this list of conditions and the following disclaimer
14
-// in the documentation and/or other materials provided with the
15
-// distribution.
16
-// * Neither the name of Google Inc. nor the names of its
17
-// contributors may be used to endorse or promote products derived from
18
-// this software without specific prior written permission.
19
-//
20
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
-// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
-// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23
-// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24
-// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25
-// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26
-// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27
-// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28
-// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29
-// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
-// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
-
32
-syntax = "proto3";
33
-
34
-import "testdata/test.proto";
35
-
36
-package proto3_proto;
37
-
38
-message Message {
39
- enum Humour {
40
- UNKNOWN = 0;
41
- PUNS = 1;
42
- SLAPSTICK = 2;
43
- BILL_BAILEY = 3;
44
- }
45
-
46
- string name = 1;
47
- Humour hilarity = 2;
48
- uint32 height_in_cm = 3;
49
- bytes data = 4;
50
- int64 result_count = 7;
51
- bool true_scotsman = 8;
52
- float score = 9;
53
-
54
- repeated uint64 key = 5;
55
- Nested nested = 6;
56
-
57
- map<string, Nested> terrain = 10;
58
- testdata.SubDefaults proto2_field = 11;
59
- map<string, testdata.SubDefaults> proto2_value = 13;
60
-}
61
-
62
-message Nested {
63
- string bunny = 1;
64
-}
65
-
66
-message MessageWithMap {
67
- map<bool, bytes> byte_mapping = 1;
68
-}
Godeps/_workspace/src/github.com/golang/protobuf/proto/proto3_test.go
deleted
-125
@@ -1,125 +0,0 @@
1
-// Go support for Protocol Buffers - Google's data interchange format
2
-//
3
-// Copyright 2014 The Go Authors. All rights reserved.
4
-// https://github.com/golang/protobuf
5
-//
6
-// Redistribution and use in source and binary forms, with or without
7
-// modification, are permitted provided that the following conditions are
8
-// met:
9
-//
10
-// * Redistributions of source code must retain the above copyright
11
-// notice, this list of conditions and the following disclaimer.
12
-// * Redistributions in binary form must reproduce the above
13
-// copyright notice, this list of conditions and the following disclaimer
14
-// in the documentation and/or other materials provided with the
15
-// distribution.
16
-// * Neither the name of Google Inc. nor the names of its
17
-// contributors may be used to endorse or promote products derived from
18
-// this software without specific prior written permission.
19
-//
20
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
-// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
-// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23
-// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24
-// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25
-// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26
-// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27
-// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28
-// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29
-// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
-// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
-
32
-package proto_test
33
-
34
-import (
35
- "testing"
36
-
37
- "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/golang/protobuf/proto"
38
- pb "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/golang/protobuf/proto/proto3_proto"
39
- tpb "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/golang/protobuf/proto/testdata"
40
-)
41
-
42
-func TestProto3ZeroValues(t *testing.T) {
43
- tests := []struct {
44
- desc string
45
- m proto.Message
46
- }{
47
- {"zero message", &pb.Message{}},
48
- {"empty bytes field", &pb.Message{Data: []byte{}}},
49
- }
50
- for _, test := range tests {
51
- b, err := proto.Marshal(test.m)
52
- if err != nil {
53
- t.Errorf("%s: proto.Marshal: %v", test.desc, err)
54
- continue
55
- }
56
- if len(b) > 0 {
57
- t.Errorf("%s: Encoding is non-empty: %q", test.desc, b)
58
- }
59
- }
60
-}
61
-
62
-func TestRoundTripProto3(t *testing.T) {
63
- m := &pb.Message{
64
- Name: "David", // (2 | 1<<3): 0x0a 0x05 "David"
65
- Hilarity: pb.Message_PUNS, // (0 | 2<<3): 0x10 0x01
66
- HeightInCm: 178, // (0 | 3<<3): 0x18 0xb2 0x01
67
- Data: []byte("roboto"), // (2 | 4<<3): 0x20 0x06 "roboto"
68
- ResultCount: 47, // (0 | 7<<3): 0x38 0x2f
69
- TrueScotsman: true, // (0 | 8<<3): 0x40 0x01
70
- Score: 8.1, // (5 | 9<<3): 0x4d <8.1>
71
-
72
- Key: []uint64{1, 0xdeadbeef},
73
- Nested: &pb.Nested{
74
- Bunny: "Monty",
75
- },
76
- }
77
- t.Logf(" m: %v", m)
78
-
79
- b, err := proto.Marshal(m)
80
- if err != nil {
81
- t.Fatalf("proto.Marshal: %v", err)
82
- }
83
- t.Logf(" b: %q", b)
84
-
85
- m2 := new(pb.Message)
86
- if err := proto.Unmarshal(b, m2); err != nil {
87
- t.Fatalf("proto.Unmarshal: %v", err)
88
- }
89
- t.Logf("m2: %v", m2)
90
-
91
- if !proto.Equal(m, m2) {
92
- t.Errorf("proto.Equal returned false:\n m: %v\nm2: %v", m, m2)
93
- }
94
-}
95
-
96
-func TestProto3SetDefaults(t *testing.T) {
97
- in := &pb.Message{
98
- Terrain: map[string]*pb.Nested{
99
- "meadow": new(pb.Nested),
100
- },
101
- Proto2Field: new(tpb.SubDefaults),
102
- Proto2Value: map[string]*tpb.SubDefaults{
103
- "badlands": new(tpb.SubDefaults),
104
- },
105
- }
106
-
107
- got := proto.Clone(in).(*pb.Message)
108
- proto.SetDefaults(got)
109
-
110
- // There are no defaults in proto3. Everything should be the zero value, but
111
- // we need to remember to set defaults for nested proto2 messages.
112
- want := &pb.Message{
113
- Terrain: map[string]*pb.Nested{
114
- "meadow": new(pb.Nested),
115
- },
116
- Proto2Field: &tpb.SubDefaults{N: proto.Int64(7)},
117
- Proto2Value: map[string]*tpb.SubDefaults{
118
- "badlands": &tpb.SubDefaults{N: proto.Int64(7)},
119
- },
120
- }
121
-
122
- if !proto.Equal(got, want) {
123
- t.Errorf("with in = %v\nproto.SetDefaults(in) =>\ngot %v\nwant %v", in, got, want)
124
- }
125
-}
Godeps/_workspace/src/github.com/golang/protobuf/proto/size2_test.go
deleted
-63
@@ -1,63 +0,0 @@
1
-// Go support for Protocol Buffers - Google's data interchange format
2
-//
3
-// Copyright 2012 The Go Authors. All rights reserved.
4
-// https://github.com/golang/protobuf
5
-//
6
-// Redistribution and use in source and binary forms, with or without
7
-// modification, are permitted provided that the following conditions are
8
-// met:
9
-//
10
-// * Redistributions of source code must retain the above copyright
11
-// notice, this list of conditions and the following disclaimer.
12
-// * Redistributions in binary form must reproduce the above
13
-// copyright notice, this list of conditions and the following disclaimer
14
-// in the documentation and/or other materials provided with the
15
-// distribution.
16
-// * Neither the name of Google Inc. nor the names of its
17
-// contributors may be used to endorse or promote products derived from
18
-// this software without specific prior written permission.
19
-//
20
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
-// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
-// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23
-// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24
-// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25
-// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26
-// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27
-// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28
-// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29
-// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
-// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
-
32
-package proto
33
-
34
-import (
35
- "testing"
36
-)
37
-
38
-// This is a separate file and package from size_test.go because that one uses
39
-// generated messages and thus may not be in package proto without having a circular
40
-// dependency, whereas this file tests unexported details of size.go.
41
-
42
-func TestVarintSize(t *testing.T) {
43
- // Check the edge cases carefully.
44
- testCases := []struct {
45
- n uint64
46
- size int
47
- }{
48
- {0, 1},
49
- {1, 1},
50
- {127, 1},
51
- {128, 2},
52
- {16383, 2},
53
- {16384, 3},
54
- {1<<63 - 1, 9},
55
- {1 << 63, 10},
56
- }
57
- for _, tc := range testCases {
58
- size := sizeVarint(tc.n)
59
- if size != tc.size {
60
- t.Errorf("sizeVarint(%d) = %d, want %d", tc.n, size, tc.size)
61
- }
62
- }
63
-}
Godeps/_workspace/src/github.com/golang/protobuf/proto/size_test.go
deleted
-142
@@ -1,142 +0,0 @@
1
-// Go support for Protocol Buffers - Google's data interchange format
2
-//
3
-// Copyright 2012 The Go Authors. All rights reserved.
4
-// https://github.com/golang/protobuf
5
-//
6
-// Redistribution and use in source and binary forms, with or without
7
-// modification, are permitted provided that the following conditions are
8
-// met:
9
-//
10
-// * Redistributions of source code must retain the above copyright
11
-// notice, this list of conditions and the following disclaimer.
12
-// * Redistributions in binary form must reproduce the above
13
-// copyright notice, this list of conditions and the following disclaimer
14
-// in the documentation and/or other materials provided with the
15
-// distribution.
16
-// * Neither the name of Google Inc. nor the names of its
17
-// contributors may be used to endorse or promote products derived from
18
-// this software without specific prior written permission.
19
-//
20
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
-// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
-// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23
-// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24
-// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25
-// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26
-// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27
-// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28
-// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29
-// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
-// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
-
32
-package proto_test
33
-
34
-import (
35
- "log"
36
- "strings"
37
- "testing"
38
-
39
- . "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/golang/protobuf/proto"
40
- proto3pb "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/golang/protobuf/proto/proto3_proto"
41
- pb "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/golang/protobuf/proto/testdata"
42
-)
43
-
44
-var messageWithExtension1 = &pb.MyMessage{Count: Int32(7)}
45
-
46
-// messageWithExtension2 is in equal_test.go.
47
-var messageWithExtension3 = &pb.MyMessage{Count: Int32(8)}
48
-
49
-func init() {
50
- if err := SetExtension(messageWithExtension1, pb.E_Ext_More, &pb.Ext{Data: String("Abbott")}); err != nil {
51
- log.Panicf("SetExtension: %v", err)
52
- }
53
- if err := SetExtension(messageWithExtension3, pb.E_Ext_More, &pb.Ext{Data: String("Costello")}); err != nil {
54
- log.Panicf("SetExtension: %v", err)
55
- }
56
-
57
- // Force messageWithExtension3 to have the extension encoded.
58
- Marshal(messageWithExtension3)
59
-
60
-}
61
-
62
-var SizeTests = []struct {
63
- desc string
64
- pb Message
65
-}{
66
- {"empty", &pb.OtherMessage{}},
67
- // Basic types.
68
- {"bool", &pb.Defaults{F_Bool: Bool(true)}},
69
- {"int32", &pb.Defaults{F_Int32: Int32(12)}},
70
- {"negative int32", &pb.Defaults{F_Int32: Int32(-1)}},
71
- {"small int64", &pb.Defaults{F_Int64: Int64(1)}},
72
- {"big int64", &pb.Defaults{F_Int64: Int64(1 << 20)}},
73
- {"negative int64", &pb.Defaults{F_Int64: Int64(-1)}},
74
- {"fixed32", &pb.Defaults{F_Fixed32: Uint32(71)}},
75
- {"fixed64", &pb.Defaults{F_Fixed64: Uint64(72)}},
76
- {"uint32", &pb.Defaults{F_Uint32: Uint32(123)}},
77
- {"uint64", &pb.Defaults{F_Uint64: Uint64(124)}},
78
- {"float", &pb.Defaults{F_Float: Float32(12.6)}},
79
- {"double", &pb.Defaults{F_Double: Float64(13.9)}},
80
- {"string", &pb.Defaults{F_String: String("niles")}},
81
- {"bytes", &pb.Defaults{F_Bytes: []byte("wowsa")}},
82
- {"bytes, empty", &pb.Defaults{F_Bytes: []byte{}}},
83
- {"sint32", &pb.Defaults{F_Sint32: Int32(65)}},
84
- {"sint64", &pb.Defaults{F_Sint64: Int64(67)}},
85
- {"enum", &pb.Defaults{F_Enum: pb.Defaults_BLUE.Enum()}},
86
- // Repeated.
87
- {"empty repeated bool", &pb.MoreRepeated{Bools: []bool{}}},
88
- {"repeated bool", &pb.MoreRepeated{Bools: []bool{false, true, true, false}}},
89
- {"packed repeated bool", &pb.MoreRepeated{BoolsPacked: []bool{false, true, true, false, true, true, true}}},
90
- {"repeated int32", &pb.MoreRepeated{Ints: []int32{1, 12203, 1729, -1}}},
91
- {"repeated int32 packed", &pb.MoreRepeated{IntsPacked: []int32{1, 12203, 1729}}},
92
- {"repeated int64 packed", &pb.MoreRepeated{Int64SPacked: []int64{
93
- // Need enough large numbers to verify that the header is counting the number of bytes
94
- // for the field, not the number of elements.
95
- 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62,
96
- 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62,
97
- }}},
98
- {"repeated string", &pb.MoreRepeated{Strings: []string{"r", "ken", "gri"}}},
99
- {"repeated fixed", &pb.MoreRepeated{Fixeds: []uint32{1, 2, 3, 4}}},
100
- // Nested.
101
- {"nested", &pb.OldMessage{Nested: &pb.OldMessage_Nested{Name: String("whatever")}}},
102
- {"group", &pb.GroupOld{G: &pb.GroupOld_G{X: Int32(12345)}}},
103
- // Other things.
104
- {"unrecognized", &pb.MoreRepeated{XXX_unrecognized: []byte{13<<3 | 0, 4}}},
105
- {"extension (unencoded)", messageWithExtension1},
106
- {"extension (encoded)", messageWithExtension3},
107
- // proto3 message
108
- {"proto3 empty", &proto3pb.Message{}},
109
- {"proto3 bool", &proto3pb.Message{TrueScotsman: true}},
110
- {"proto3 int64", &proto3pb.Message{ResultCount: 1}},
111
- {"proto3 uint32", &proto3pb.Message{HeightInCm: 123}},
112
- {"proto3 float", &proto3pb.Message{Score: 12.6}},
113
- {"proto3 string", &proto3pb.Message{Name: "Snezana"}},
114
- {"proto3 bytes", &proto3pb.Message{Data: []byte("wowsa")}},
115
- {"proto3 bytes, empty", &proto3pb.Message{Data: []byte{}}},
116
- {"proto3 enum", &proto3pb.Message{Hilarity: proto3pb.Message_PUNS}},
117
- {"proto3 map field with empty bytes", &proto3pb.MessageWithMap{ByteMapping: map[bool][]byte{false: []byte{}}}},
118
-
119
- {"map field", &pb.MessageWithMap{NameMapping: map[int32]string{1: "Rob", 7: "Andrew"}}},
120
- {"map field with message", &pb.MessageWithMap{MsgMapping: map[int64]*pb.FloatingPoint{0x7001: &pb.FloatingPoint{F: Float64(2.0)}}}},
121
- {"map field with bytes", &pb.MessageWithMap{ByteMapping: map[bool][]byte{true: []byte("this time for sure")}}},
122
- {"map field with empty bytes", &pb.MessageWithMap{ByteMapping: map[bool][]byte{true: []byte{}}}},
123
-
124
- {"map field with big entry", &pb.MessageWithMap{NameMapping: map[int32]string{8: strings.Repeat("x", 125)}}},
125
- {"map field with big key and val", &pb.MessageWithMap{StrToStr: map[string]string{strings.Repeat("x", 70): strings.Repeat("y", 70)}}},
126
- {"map field with big numeric key", &pb.MessageWithMap{NameMapping: map[int32]string{0xf00d: "om nom nom"}}},
127
-}
128
-
129
-func TestSize(t *testing.T) {
130
- for _, tc := range SizeTests {
131
- size := Size(tc.pb)
132
- b, err := Marshal(tc.pb)
133
- if err != nil {
134
- t.Errorf("%v: Marshal failed: %v", tc.desc, err)
135
- continue
136
- }
137
- if size != len(b) {
138
- t.Errorf("%v: Size(%v) = %d, want %d", tc.desc, tc.pb, size, len(b))
139
- t.Logf("%v: bytes: %#v", tc.desc, b)
140
- }
141
- }
142
-}
Godeps/_workspace/src/github.com/golang/protobuf/proto/testdata/Makefile
deleted
-50
@@ -1,50 +0,0 @@
1
-# Go support for Protocol Buffers - Google's data interchange format
2
-#
3
-# Copyright 2010 The Go Authors. All rights reserved.
4
-# https://github.com/golang/protobuf
5
-#
6
-# Redistribution and use in source and binary forms, with or without
7
-# modification, are permitted provided that the following conditions are
8
-# met:
9
-#
10
-# * Redistributions of source code must retain the above copyright
11
-# notice, this list of conditions and the following disclaimer.
12
-# * Redistributions in binary form must reproduce the above
13
-# copyright notice, this list of conditions and the following disclaimer
14
-# in the documentation and/or other materials provided with the
15
-# distribution.
16
-# * Neither the name of Google Inc. nor the names of its
17
-# contributors may be used to endorse or promote products derived from
18
-# this software without specific prior written permission.
19
-#
20
-# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
-# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
-# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23
-# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24
-# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25
-# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26
-# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27
-# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28
-# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29
-# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
-# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
-
32
-
33
-include ../../Make.protobuf
34
-
35
-all: regenerate
36
-
37
-regenerate:
38
- rm -f test.pb.go
39
- make test.pb.go
40
-
41
-# The following rules are just aids to development. Not needed for typical testing.
42
-
43
-diff: regenerate
44
- git diff test.pb.go
45
-
46
-restore:
47
- cp test.pb.go.golden test.pb.go
48
-
49
-preserve:
50
- cp test.pb.go test.pb.go.golden
Godeps/_workspace/src/github.com/golang/protobuf/proto/testdata/golden_test.go
deleted
-86
@@ -1,86 +0,0 @@
1
-// Go support for Protocol Buffers - Google's data interchange format
2
-//
3
-// Copyright 2012 The Go Authors. All rights reserved.
4
-// https://github.com/golang/protobuf
5
-//
6
-// Redistribution and use in source and binary forms, with or without
7
-// modification, are permitted provided that the following conditions are
8
-// met:
9
-//
10
-// * Redistributions of source code must retain the above copyright
11
-// notice, this list of conditions and the following disclaimer.
12
-// * Redistributions in binary form must reproduce the above
13
-// copyright notice, this list of conditions and the following disclaimer
14
-// in the documentation and/or other materials provided with the
15
-// distribution.
16
-// * Neither the name of Google Inc. nor the names of its
17
-// contributors may be used to endorse or promote products derived from
18
-// this software without specific prior written permission.
19
-//
20
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
-// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
-// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23
-// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24
-// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25
-// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26
-// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27
-// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28
-// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29
-// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
-// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
-
32
-// Verify that the compiler output for test.proto is unchanged.
33
-
34
-package testdata
35
-
36
-import (
37
- "crypto/sha1"
38
- "fmt"
39
- "io/ioutil"
40
- "os"
41
- "os/exec"
42
- "path/filepath"
43
- "testing"
44
-)
45
-
46
-// sum returns in string form (for easy comparison) the SHA-1 hash of the named file.
47
-func sum(t *testing.T, name string) string {
48
- data, err := ioutil.ReadFile(name)
49
- if err != nil {
50
- t.Fatal(err)
51
- }
52
- t.Logf("sum(%q): length is %d", name, len(data))
53
- hash := sha1.New()
54
- _, err = hash.Write(data)
55
- if err != nil {
56
- t.Fatal(err)
57
- }
58
- return fmt.Sprintf("% x", hash.Sum(nil))
59
-}
60
-
61
-func run(t *testing.T, name string, args ...string) {
62
- cmd := exec.Command(name, args...)
63
- cmd.Stdin = os.Stdin
64
- cmd.Stdout = os.Stdout
65
- cmd.Stderr = os.Stderr
66
- err := cmd.Run()
67
- if err != nil {
68
- t.Fatal(err)
69
- }
70
-}
71
-
72
-func TestGolden(t *testing.T) {
73
- // Compute the original checksum.
74
- goldenSum := sum(t, "test.pb.go")
75
- // Run the proto compiler.
76
- run(t, "protoc", "--go_out="+os.TempDir(), "test.proto")
77
- newFile := filepath.Join(os.TempDir(), "test.pb.go")
78
- defer os.Remove(newFile)
79
- // Compute the new checksum.
80
- newSum := sum(t, newFile)
81
- // Verify
82
- if newSum != goldenSum {
83
- run(t, "diff", "-u", "test.pb.go", newFile)
84
- t.Fatal("Code generated by protoc-gen-go has changed; update test.pb.go")
85
- }
86
-}
Godeps/_workspace/src/github.com/golang/protobuf/proto/testdata/test.pb.go
deleted
-2746
@@ -1,2746 +0,0 @@
1
-// Code generated by protoc-gen-go.
2
-// source: test.proto
3
-// DO NOT EDIT!
4
-
5
-/*
6
-Package testdata is a generated protocol buffer package.
7
-
8
-It is generated from these files:
9
- test.proto
10
-
11
-It has these top-level messages:
12
- GoEnum
13
- GoTestField
14
- GoTest
15
- GoSkipTest
16
- NonPackedTest
17
- PackedTest
18
- MaxTag
19
- OldMessage
20
- NewMessage
21
- InnerMessage
22
- OtherMessage
23
- MyMessage
24
- Ext
25
- DefaultsMessage
26
- MyMessageSet
27
- Empty
28
- MessageList
29
- Strings
30
- Defaults
31
- SubDefaults
32
- RepeatedEnum
33
- MoreRepeated
34
- GroupOld
35
- GroupNew
36
- FloatingPoint
37
- MessageWithMap
38
-*/
39
-package testdata
40
-
41
-import proto "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/golang/protobuf/proto"
42
-import math "math"
43
-
44
-// Reference imports to suppress errors if they are not otherwise used.
45
-var _ = proto.Marshal
46
-var _ = math.Inf
47
-
48
-type FOO int32
49
-
50
-const (
51
- FOO_FOO1 FOO = 1
52
-)
53
-
54
-var FOO_name = map[int32]string{
55
- 1: "FOO1",
56
-}
57
-var FOO_value = map[string]int32{
58
- "FOO1": 1,
59
-}
60
-
61
-func (x FOO) Enum() *FOO {
62
- p := new(FOO)
63
- *p = x
64
- return p
65
-}
66
-func (x FOO) String() string {
67
- return proto.EnumName(FOO_name, int32(x))
68
-}
69
-func (x *FOO) UnmarshalJSON(data []byte) error {
70
- value, err := proto.UnmarshalJSONEnum(FOO_value, data, "FOO")
71
- if err != nil {
72
- return err
73
- }
74
- *x = FOO(value)
75
- return nil
76
-}
77
-
78
-// An enum, for completeness.
79
-type GoTest_KIND int32
80
-
81
-const (
82
- GoTest_VOID GoTest_KIND = 0
83
- // Basic types
84
- GoTest_BOOL GoTest_KIND = 1
85
- GoTest_BYTES GoTest_KIND = 2
86
- GoTest_FINGERPRINT GoTest_KIND = 3
87
- GoTest_FLOAT GoTest_KIND = 4
88
- GoTest_INT GoTest_KIND = 5
89
- GoTest_STRING GoTest_KIND = 6
90
- GoTest_TIME GoTest_KIND = 7
91
- // Groupings
92
- GoTest_TUPLE GoTest_KIND = 8
93
- GoTest_ARRAY GoTest_KIND = 9
94
- GoTest_MAP GoTest_KIND = 10
95
- // Table types
96
- GoTest_TABLE GoTest_KIND = 11
97
- // Functions
98
- GoTest_FUNCTION GoTest_KIND = 12
99
-)
100
-
101
-var GoTest_KIND_name = map[int32]string{
102
- 0: "VOID",
103
- 1: "BOOL",
104
- 2: "BYTES",
105
- 3: "FINGERPRINT",
106
- 4: "FLOAT",
107
- 5: "INT",
108
- 6: "STRING",
109
- 7: "TIME",
110
- 8: "TUPLE",
111
- 9: "ARRAY",
112
- 10: "MAP",
113
- 11: "TABLE",
114
- 12: "FUNCTION",
115
-}
116
-var GoTest_KIND_value = map[string]int32{
117
- "VOID": 0,
118
- "BOOL": 1,
119
- "BYTES": 2,
120
- "FINGERPRINT": 3,
121
- "FLOAT": 4,
122
- "INT": 5,
123
- "STRING": 6,
124
- "TIME": 7,
125
- "TUPLE": 8,
126
- "ARRAY": 9,
127
- "MAP": 10,
128
- "TABLE": 11,
129
- "FUNCTION": 12,
130
-}
131
-
132
-func (x GoTest_KIND) Enum() *GoTest_KIND {
133
- p := new(GoTest_KIND)
134
- *p = x
135
- return p
136
-}
137
-func (x GoTest_KIND) String() string {
138
- return proto.EnumName(GoTest_KIND_name, int32(x))
139
-}
140
-func (x *GoTest_KIND) UnmarshalJSON(data []byte) error {
141
- value, err := proto.UnmarshalJSONEnum(GoTest_KIND_value, data, "GoTest_KIND")
142
- if err != nil {
143
- return err
144
- }
145
- *x = GoTest_KIND(value)
146
- return nil
147
-}
148
-
149
-type MyMessage_Color int32
150
-
151
-const (
152
- MyMessage_RED MyMessage_Color = 0
153
- MyMessage_GREEN MyMessage_Color = 1
154
- MyMessage_BLUE MyMessage_Color = 2
155
-)
156
-
157
-var MyMessage_Color_name = map[int32]string{
158
- 0: "RED",
159
- 1: "GREEN",
160
- 2: "BLUE",
161
-}
162
-var MyMessage_Color_value = map[string]int32{
163
- "RED": 0,
164
- "GREEN": 1,
165
- "BLUE": 2,
166
-}
167
-
168
-func (x MyMessage_Color) Enum() *MyMessage_Color {
169
- p := new(MyMessage_Color)
170
- *p = x
171
- return p
172
-}
173
-func (x MyMessage_Color) String() string {
174
- return proto.EnumName(MyMessage_Color_name, int32(x))
175
-}
176
-func (x *MyMessage_Color) UnmarshalJSON(data []byte) error {
177
- value, err := proto.UnmarshalJSONEnum(MyMessage_Color_value, data, "MyMessage_Color")
178
- if err != nil {
179
- return err
180
- }
181
- *x = MyMessage_Color(value)
182
- return nil
183
-}
184
-
185
-type DefaultsMessage_DefaultsEnum int32
186
-
187
-const (
188
- DefaultsMessage_ZERO DefaultsMessage_DefaultsEnum = 0
189
- DefaultsMessage_ONE DefaultsMessage_DefaultsEnum = 1
190
- DefaultsMessage_TWO DefaultsMessage_DefaultsEnum = 2
191
-)
192
-
193
-var DefaultsMessage_DefaultsEnum_name = map[int32]string{
194
- 0: "ZERO",
195
- 1: "ONE",
196
- 2: "TWO",
197
-}
198
-var DefaultsMessage_DefaultsEnum_value = map[string]int32{
199
- "ZERO": 0,
200
- "ONE": 1,
201
- "TWO": 2,
202
-}
203
-
204
-func (x DefaultsMessage_DefaultsEnum) Enum() *DefaultsMessage_DefaultsEnum {
205
- p := new(DefaultsMessage_DefaultsEnum)
206
- *p = x
207
- return p
208
-}
209
-func (x DefaultsMessage_DefaultsEnum) String() string {
210
- return proto.EnumName(DefaultsMessage_DefaultsEnum_name, int32(x))
211
-}
212
-func (x *DefaultsMessage_DefaultsEnum) UnmarshalJSON(data []byte) error {
213
- value, err := proto.UnmarshalJSONEnum(DefaultsMessage_DefaultsEnum_value, data, "DefaultsMessage_DefaultsEnum")
214
- if err != nil {
215
- return err
216
- }
217
- *x = DefaultsMessage_DefaultsEnum(value)
218
- return nil
219
-}
220
-
221
-type Defaults_Color int32
222
-
223
-const (
224
- Defaults_RED Defaults_Color = 0
225
- Defaults_GREEN Defaults_Color = 1
226
- Defaults_BLUE Defaults_Color = 2
227
-)
228
-
229
-var Defaults_Color_name = map[int32]string{
230
- 0: "RED",
231
- 1: "GREEN",
232
- 2: "BLUE",
233
-}
234
-var Defaults_Color_value = map[string]int32{
235
- "RED": 0,
236
- "GREEN": 1,
237
- "BLUE": 2,
238
-}
239
-
240
-func (x Defaults_Color) Enum() *Defaults_Color {
241
- p := new(Defaults_Color)
242
- *p = x
243
- return p
244
-}
245
-func (x Defaults_Color) String() string {
246
- return proto.EnumName(Defaults_Color_name, int32(x))
247
-}
248
-func (x *Defaults_Color) UnmarshalJSON(data []byte) error {
249
- value, err := proto.UnmarshalJSONEnum(Defaults_Color_value, data, "Defaults_Color")
250
- if err != nil {
251
- return err
252
- }
253
- *x = Defaults_Color(value)
254
- return nil
255
-}
256
-
257
-type RepeatedEnum_Color int32
258
-
259
-const (
260
- RepeatedEnum_RED RepeatedEnum_Color = 1
261
-)
262
-
263
-var RepeatedEnum_Color_name = map[int32]string{
264
- 1: "RED",
265
-}
266
-var RepeatedEnum_Color_value = map[string]int32{
267
- "RED": 1,
268
-}
269
-
270
-func (x RepeatedEnum_Color) Enum() *RepeatedEnum_Color {
271
- p := new(RepeatedEnum_Color)
272
- *p = x
273
- return p
274
-}
275
-func (x RepeatedEnum_Color) String() string {
276
- return proto.EnumName(RepeatedEnum_Color_name, int32(x))
277
-}
278
-func (x *RepeatedEnum_Color) UnmarshalJSON(data []byte) error {
279
- value, err := proto.UnmarshalJSONEnum(RepeatedEnum_Color_value, data, "RepeatedEnum_Color")
280
- if err != nil {
281
- return err
282
- }
283
- *x = RepeatedEnum_Color(value)
284
- return nil
285
-}
286
-
287
-type GoEnum struct {
288
- Foo *FOO `protobuf:"varint,1,req,name=foo,enum=testdata.FOO" json:"foo,omitempty"`
289
- XXX_unrecognized []byte `json:"-"`
290
-}
291
-
292
-func (m *GoEnum) Reset() { *m = GoEnum{} }
293
-func (m *GoEnum) String() string { return proto.CompactTextString(m) }
294
-func (*GoEnum) ProtoMessage() {}
295
-
296
-func (m *GoEnum) GetFoo() FOO {
297
- if m != nil && m.Foo != nil {
298
- return *m.Foo
299
- }
300
- return FOO_FOO1
301
-}
302
-
303
-type GoTestField struct {
304
- Label *string `protobuf:"bytes,1,req" json:"Label,omitempty"`
305
- Type *string `protobuf:"bytes,2,req" json:"Type,omitempty"`
306
- XXX_unrecognized []byte `json:"-"`
307
-}
308
-
309
-func (m *GoTestField) Reset() { *m = GoTestField{} }
310
-func (m *GoTestField) String() string { return proto.CompactTextString(m) }
311
-func (*GoTestField) ProtoMessage() {}
312
-
313
-func (m *GoTestField) GetLabel() string {
314
- if m != nil && m.Label != nil {
315
- return *m.Label
316
- }
317
- return ""
318
-}
319
-
320
-func (m *GoTestField) GetType() string {
321
- if m != nil && m.Type != nil {
322
- return *m.Type
323
- }
324
- return ""
325
-}
326
-
327
-type GoTest struct {
328
- // Some typical parameters
329
- Kind *GoTest_KIND `protobuf:"varint,1,req,enum=testdata.GoTest_KIND" json:"Kind,omitempty"`
330
- Table *string `protobuf:"bytes,2,opt" json:"Table,omitempty"`
331
- Param *int32 `protobuf:"varint,3,opt" json:"Param,omitempty"`
332
- // Required, repeated and optional foreign fields.
333
- RequiredField *GoTestField `protobuf:"bytes,4,req" json:"RequiredField,omitempty"`
334
- RepeatedField []*GoTestField `protobuf:"bytes,5,rep" json:"RepeatedField,omitempty"`
335
- OptionalField *GoTestField `protobuf:"bytes,6,opt" json:"OptionalField,omitempty"`
336
- // Required fields of all basic types
337
- F_BoolRequired *bool `protobuf:"varint,10,req,name=F_Bool_required" json:"F_Bool_required,omitempty"`
338
- F_Int32Required *int32 `protobuf:"varint,11,req,name=F_Int32_required" json:"F_Int32_required,omitempty"`
339
- F_Int64Required *int64 `protobuf:"varint,12,req,name=F_Int64_required" json:"F_Int64_required,omitempty"`
340
- F_Fixed32Required *uint32 `protobuf:"fixed32,13,req,name=F_Fixed32_required" json:"F_Fixed32_required,omitempty"`
341
- F_Fixed64Required *uint64 `protobuf:"fixed64,14,req,name=F_Fixed64_required" json:"F_Fixed64_required,omitempty"`
342
- F_Uint32Required *uint32 `protobuf:"varint,15,req,name=F_Uint32_required" json:"F_Uint32_required,omitempty"`
343
- F_Uint64Required *uint64 `protobuf:"varint,16,req,name=F_Uint64_required" json:"F_Uint64_required,omitempty"`
344
- F_FloatRequired *float32 `protobuf:"fixed32,17,req,name=F_Float_required" json:"F_Float_required,omitempty"`
345
- F_DoubleRequired *float64 `protobuf:"fixed64,18,req,name=F_Double_required" json:"F_Double_required,omitempty"`
346
- F_StringRequired *string `protobuf:"bytes,19,req,name=F_String_required" json:"F_String_required,omitempty"`
347
- F_BytesRequired []byte `protobuf:"bytes,101,req,name=F_Bytes_required" json:"F_Bytes_required,omitempty"`
348
- F_Sint32Required *int32 `protobuf:"zigzag32,102,req,name=F_Sint32_required" json:"F_Sint32_required,omitempty"`
349
- F_Sint64Required *int64 `protobuf:"zigzag64,103,req,name=F_Sint64_required" json:"F_Sint64_required,omitempty"`
350
- // Repeated fields of all basic types
351
- F_BoolRepeated []bool `protobuf:"varint,20,rep,name=F_Bool_repeated" json:"F_Bool_repeated,omitempty"`
352
- F_Int32Repeated []int32 `protobuf:"varint,21,rep,name=F_Int32_repeated" json:"F_Int32_repeated,omitempty"`
353
- F_Int64Repeated []int64 `protobuf:"varint,22,rep,name=F_Int64_repeated" json:"F_Int64_repeated,omitempty"`
354
- F_Fixed32Repeated []uint32 `protobuf:"fixed32,23,rep,name=F_Fixed32_repeated" json:"F_Fixed32_repeated,omitempty"`
355
- F_Fixed64Repeated []uint64 `protobuf:"fixed64,24,rep,name=F_Fixed64_repeated" json:"F_Fixed64_repeated,omitempty"`
356
- F_Uint32Repeated []uint32 `protobuf:"varint,25,rep,name=F_Uint32_repeated" json:"F_Uint32_repeated,omitempty"`
357
- F_Uint64Repeated []uint64 `protobuf:"varint,26,rep,name=F_Uint64_repeated" json:"F_Uint64_repeated,omitempty"`
358
- F_FloatRepeated []float32 `protobuf:"fixed32,27,rep,name=F_Float_repeated" json:"F_Float_repeated,omitempty"`
359
- F_DoubleRepeated []float64 `protobuf:"fixed64,28,rep,name=F_Double_repeated" json:"F_Double_repeated,omitempty"`
360
- F_StringRepeated []string `protobuf:"bytes,29,rep,name=F_String_repeated" json:"F_String_repeated,omitempty"`
361
- F_BytesRepeated [][]byte `protobuf:"bytes,201,rep,name=F_Bytes_repeated" json:"F_Bytes_repeated,omitempty"`
362
- F_Sint32Repeated []int32 `protobuf:"zigzag32,202,rep,name=F_Sint32_repeated" json:"F_Sint32_repeated,omitempty"`
363
- F_Sint64Repeated []int64 `protobuf:"zigzag64,203,rep,name=F_Sint64_repeated" json:"F_Sint64_repeated,omitempty"`
364
- // Optional fields of all basic types
365
- F_BoolOptional *bool `protobuf:"varint,30,opt,name=F_Bool_optional" json:"F_Bool_optional,omitempty"`
366
- F_Int32Optional *int32 `protobuf:"varint,31,opt,name=F_Int32_optional" json:"F_Int32_optional,omitempty"`
367
- F_Int64Optional *int64 `protobuf:"varint,32,opt,name=F_Int64_optional" json:"F_Int64_optional,omitempty"`
368
- F_Fixed32Optional *uint32 `protobuf:"fixed32,33,opt,name=F_Fixed32_optional" json:"F_Fixed32_optional,omitempty"`
369
- F_Fixed64Optional *uint64 `protobuf:"fixed64,34,opt,name=F_Fixed64_optional" json:"F_Fixed64_optional,omitempty"`
370
- F_Uint32Optional *uint32 `protobuf:"varint,35,opt,name=F_Uint32_optional" json:"F_Uint32_optional,omitempty"`
371
- F_Uint64Optional *uint64 `protobuf:"varint,36,opt,name=F_Uint64_optional" json:"F_Uint64_optional,omitempty"`
372
- F_FloatOptional *float32 `protobuf:"fixed32,37,opt,name=F_Float_optional" json:"F_Float_optional,omitempty"`
373
- F_DoubleOptional *float64 `protobuf:"fixed64,38,opt,name=F_Double_optional" json:"F_Double_optional,omitempty"`
374
- F_StringOptional *string `protobuf:"bytes,39,opt,name=F_String_optional" json:"F_String_optional,omitempty"`
375
- F_BytesOptional []byte `protobuf:"bytes,301,opt,name=F_Bytes_optional" json:"F_Bytes_optional,omitempty"`
376
- F_Sint32Optional *int32 `protobuf:"zigzag32,302,opt,name=F_Sint32_optional" json:"F_Sint32_optional,omitempty"`
377
- F_Sint64Optional *int64 `protobuf:"zigzag64,303,opt,name=F_Sint64_optional" json:"F_Sint64_optional,omitempty"`
378
- // Default-valued fields of all basic types
379
- F_BoolDefaulted *bool `protobuf:"varint,40,opt,name=F_Bool_defaulted,def=1" json:"F_Bool_defaulted,omitempty"`
380
- F_Int32Defaulted *int32 `protobuf:"varint,41,opt,name=F_Int32_defaulted,def=32" json:"F_Int32_defaulted,omitempty"`
381
- F_Int64Defaulted *int64 `protobuf:"varint,42,opt,name=F_Int64_defaulted,def=64" json:"F_Int64_defaulted,omitempty"`
382
- F_Fixed32Defaulted *uint32 `protobuf:"fixed32,43,opt,name=F_Fixed32_defaulted,def=320" json:"F_Fixed32_defaulted,omitempty"`
383
- F_Fixed64Defaulted *uint64 `protobuf:"fixed64,44,opt,name=F_Fixed64_defaulted,def=640" json:"F_Fixed64_defaulted,omitempty"`
384
- F_Uint32Defaulted *uint32 `protobuf:"varint,45,opt,name=F_Uint32_defaulted,def=3200" json:"F_Uint32_defaulted,omitempty"`
385
- F_Uint64Defaulted *uint64 `protobuf:"varint,46,opt,name=F_Uint64_defaulted,def=6400" json:"F_Uint64_defaulted,omitempty"`
386
- F_FloatDefaulted *float32 `protobuf:"fixed32,47,opt,name=F_Float_defaulted,def=314159" json:"F_Float_defaulted,omitempty"`
387
- F_DoubleDefaulted *float64 `protobuf:"fixed64,48,opt,name=F_Double_defaulted,def=271828" json:"F_Double_defaulted,omitempty"`
388
- F_StringDefaulted *string `protobuf:"bytes,49,opt,name=F_String_defaulted,def=hello, \"world!\"\n" json:"F_String_defaulted,omitempty"`
389
- F_BytesDefaulted []byte `protobuf:"bytes,401,opt,name=F_Bytes_defaulted,def=Bignose" json:"F_Bytes_defaulted,omitempty"`
390
- F_Sint32Defaulted *int32 `protobuf:"zigzag32,402,opt,name=F_Sint32_defaulted,def=-32" json:"F_Sint32_defaulted,omitempty"`
391
- F_Sint64Defaulted *int64 `protobuf:"zigzag64,403,opt,name=F_Sint64_defaulted,def=-64" json:"F_Sint64_defaulted,omitempty"`
392
- // Packed repeated fields (no string or bytes).
393
- F_BoolRepeatedPacked []bool `protobuf:"varint,50,rep,packed,name=F_Bool_repeated_packed" json:"F_Bool_repeated_packed,omitempty"`
394
- F_Int32RepeatedPacked []int32 `protobuf:"varint,51,rep,packed,name=F_Int32_repeated_packed" json:"F_Int32_repeated_packed,omitempty"`
395
- F_Int64RepeatedPacked []int64 `protobuf:"varint,52,rep,packed,name=F_Int64_repeated_packed" json:"F_Int64_repeated_packed,omitempty"`
396
- F_Fixed32RepeatedPacked []uint32 `protobuf:"fixed32,53,rep,packed,name=F_Fixed32_repeated_packed" json:"F_Fixed32_repeated_packed,omitempty"`
397
- F_Fixed64RepeatedPacked []uint64 `protobuf:"fixed64,54,rep,packed,name=F_Fixed64_repeated_packed" json:"F_Fixed64_repeated_packed,omitempty"`
398
- F_Uint32RepeatedPacked []uint32 `protobuf:"varint,55,rep,packed,name=F_Uint32_repeated_packed" json:"F_Uint32_repeated_packed,omitempty"`
399
- F_Uint64RepeatedPacked []uint64 `protobuf:"varint,56,rep,packed,name=F_Uint64_repeated_packed" json:"F_Uint64_repeated_packed,omitempty"`
400
- F_FloatRepeatedPacked []float32 `protobuf:"fixed32,57,rep,packed,name=F_Float_repeated_packed" json:"F_Float_repeated_packed,omitempty"`
401
- F_DoubleRepeatedPacked []float64 `protobuf:"fixed64,58,rep,packed,name=F_Double_repeated_packed" json:"F_Double_repeated_packed,omitempty"`
402
- F_Sint32RepeatedPacked []int32 `protobuf:"zigzag32,502,rep,packed,name=F_Sint32_repeated_packed" json:"F_Sint32_repeated_packed,omitempty"`
403
- F_Sint64RepeatedPacked []int64 `protobuf:"zigzag64,503,rep,packed,name=F_Sint64_repeated_packed" json:"F_Sint64_repeated_packed,omitempty"`
404
- Requiredgroup *GoTest_RequiredGroup `protobuf:"group,70,req,name=RequiredGroup" json:"requiredgroup,omitempty"`
405
- Repeatedgroup []*GoTest_RepeatedGroup `protobuf:"group,80,rep,name=RepeatedGroup" json:"repeatedgroup,omitempty"`
406
- Optionalgroup *GoTest_OptionalGroup `protobuf:"group,90,opt,name=OptionalGroup" json:"optionalgroup,omitempty"`
407
- XXX_unrecognized []byte `json:"-"`
408
-}
409
-
410
-func (m *GoTest) Reset() { *m = GoTest{} }
411
-func (m *GoTest) String() string { return proto.CompactTextString(m) }
412
-func (*GoTest) ProtoMessage() {}
413
-
414
-const Default_GoTest_F_BoolDefaulted bool = true
415
-const Default_GoTest_F_Int32Defaulted int32 = 32
416
-const Default_GoTest_F_Int64Defaulted int64 = 64
417
-const Default_GoTest_F_Fixed32Defaulted uint32 = 320
418
-const Default_GoTest_F_Fixed64Defaulted uint64 = 640
419
-const Default_GoTest_F_Uint32Defaulted uint32 = 3200
420
-const Default_GoTest_F_Uint64Defaulted uint64 = 6400
421
-const Default_GoTest_F_FloatDefaulted float32 = 314159
422
-const Default_GoTest_F_DoubleDefaulted float64 = 271828
423
-const Default_GoTest_F_StringDefaulted string = "hello, \"world!\"\n"
424
-
425
-var Default_GoTest_F_BytesDefaulted []byte = []byte("Bignose")
426
-
427
-const Default_GoTest_F_Sint32Defaulted int32 = -32
428
-const Default_GoTest_F_Sint64Defaulted int64 = -64
429
-
430
-func (m *GoTest) GetKind() GoTest_KIND {
431
- if m != nil && m.Kind != nil {
432
- return *m.Kind
433
- }
434
- return GoTest_VOID
435
-}
436
-
437
-func (m *GoTest) GetTable() string {
438
- if m != nil && m.Table != nil {
439
- return *m.Table
440
- }
441
- return ""
442
-}
443
-
444
-func (m *GoTest) GetParam() int32 {
445
- if m != nil && m.Param != nil {
446
- return *m.Param
447
- }
448
- return 0
449
-}
450
-
451
-func (m *GoTest) GetRequiredField() *GoTestField {
452
- if m != nil {
453
- return m.RequiredField
454
- }
455
- return nil
456
-}
457
-
458
-func (m *GoTest) GetRepeatedField() []*GoTestField {
459
- if m != nil {
460
- return m.RepeatedField
461
- }
462
- return nil
463
-}
464
-
465
-func (m *GoTest) GetOptionalField() *GoTestField {
466
- if m != nil {
467
- return m.OptionalField
468
- }
469
- return nil
470
-}
471
-
472
-func (m *GoTest) GetF_BoolRequired() bool {
473
- if m != nil && m.F_BoolRequired != nil {
474
- return *m.F_BoolRequired
475
- }
476
- return false
477
-}
478
-
479
-func (m *GoTest) GetF_Int32Required() int32 {
480
- if m != nil && m.F_Int32Required != nil {
481
- return *m.F_Int32Required
482
- }
483
- return 0
484
-}
485
-
486
-func (m *GoTest) GetF_Int64Required() int64 {
487
- if m != nil && m.F_Int64Required != nil {
488
- return *m.F_Int64Required
489
- }
490
- return 0
491
-}
492
-
493
-func (m *GoTest) GetF_Fixed32Required() uint32 {
494
- if m != nil && m.F_Fixed32Required != nil {
495
- return *m.F_Fixed32Required
496
- }
497
- return 0
498
-}
499
-
500
-func (m *GoTest) GetF_Fixed64Required() uint64 {
501
- if m != nil && m.F_Fixed64Required != nil {
502
- return *m.F_Fixed64Required
503
- }
504
- return 0
505
-}
506
-
507
-func (m *GoTest) GetF_Uint32Required() uint32 {
508
- if m != nil && m.F_Uint32Required != nil {
509
- return *m.F_Uint32Required
510
- }
511
- return 0
512
-}
513
-
514
-func (m *GoTest) GetF_Uint64Required() uint64 {
515
- if m != nil && m.F_Uint64Required != nil {
516
- return *m.F_Uint64Required
517
- }
518
- return 0
519
-}
520
-
521
-func (m *GoTest) GetF_FloatRequired() float32 {
522
- if m != nil && m.F_FloatRequired != nil {
523
- return *m.F_FloatRequired
524
- }
525
- return 0
526
-}
527
-
528
-func (m *GoTest) GetF_DoubleRequired() float64 {
529
- if m != nil && m.F_DoubleRequired != nil {
530
- return *m.F_DoubleRequired
531
- }
532
- return 0
533
-}
534
-
535
-func (m *GoTest) GetF_StringRequired() string {
536
- if m != nil && m.F_StringRequired != nil {
537
- return *m.F_StringRequired
538
- }
539
- return ""
540
-}
541
-
542
-func (m *GoTest) GetF_BytesRequired() []byte {
543
- if m != nil {
544
- return m.F_BytesRequired
545
- }
546
- return nil
547
-}
548
-
549
-func (m *GoTest) GetF_Sint32Required() int32 {
550
- if m != nil && m.F_Sint32Required != nil {
551
- return *m.F_Sint32Required
552
- }
553
- return 0
554
-}
555
-
556
-func (m *GoTest) GetF_Sint64Required() int64 {
557
- if m != nil && m.F_Sint64Required != nil {
558
- return *m.F_Sint64Required
559
- }
560
- return 0
561
-}
562
-
563
-func (m *GoTest) GetF_BoolRepeated() []bool {
564
- if m != nil {
565
- return m.F_BoolRepeated
566
- }
567
- return nil
568
-}
569
-
570
-func (m *GoTest) GetF_Int32Repeated() []int32 {
571
- if m != nil {
572
- return m.F_Int32Repeated
573
- }
574
- return nil
575
-}
576
-
577
-func (m *GoTest) GetF_Int64Repeated() []int64 {
578
- if m != nil {
579
- return m.F_Int64Repeated
580
- }
581
- return nil
582
-}
583
-
584
-func (m *GoTest) GetF_Fixed32Repeated() []uint32 {
585
- if m != nil {
586
- return m.F_Fixed32Repeated
587
- }
588
- return nil
589
-}
590
-
591
-func (m *GoTest) GetF_Fixed64Repeated() []uint64 {
592
- if m != nil {
593
- return m.F_Fixed64Repeated
594
- }
595
- return nil
596
-}
597
-
598
-func (m *GoTest) GetF_Uint32Repeated() []uint32 {
599
- if m != nil {
600
- return m.F_Uint32Repeated
601
- }
602
- return nil
603
-}
604
-
605
-func (m *GoTest) GetF_Uint64Repeated() []uint64 {
606
- if m != nil {
607
- return m.F_Uint64Repeated
608
- }
609
- return nil
610
-}
611
-
612
-func (m *GoTest) GetF_FloatRepeated() []float32 {
613
- if m != nil {
614
- return m.F_FloatRepeated
615
- }
616
- return nil
617
-}
618
-
619
-func (m *GoTest) GetF_DoubleRepeated() []float64 {
620
- if m != nil {
621
- return m.F_DoubleRepeated
622
- }
623
- return nil
624
-}
625
-
626
-func (m *GoTest) GetF_StringRepeated() []string {
627
- if m != nil {
628
- return m.F_StringRepeated
629
- }
630
- return nil
631
-}
632
-
633
-func (m *GoTest) GetF_BytesRepeated() [][]byte {
634
- if m != nil {
635
- return m.F_BytesRepeated
636
- }
637
- return nil
638
-}
639
-
640
-func (m *GoTest) GetF_Sint32Repeated() []int32 {
641
- if m != nil {
642
- return m.F_Sint32Repeated
643
- }
644
- return nil
645
-}
646
-
647
-func (m *GoTest) GetF_Sint64Repeated() []int64 {
648
- if m != nil {
649
- return m.F_Sint64Repeated
650
- }
651
- return nil
652
-}
653
-
654
-func (m *GoTest) GetF_BoolOptional() bool {
655
- if m != nil && m.F_BoolOptional != nil {
656
- return *m.F_BoolOptional
657
- }
658
- return false
659
-}
660
-
661
-func (m *GoTest) GetF_Int32Optional() int32 {
662
- if m != nil && m.F_Int32Optional != nil {
663
- return *m.F_Int32Optional
664
- }
665
- return 0
666
-}
667
-
668
-func (m *GoTest) GetF_Int64Optional() int64 {
669
- if m != nil && m.F_Int64Optional != nil {
670
- return *m.F_Int64Optional
671
- }
672
- return 0
673
-}
674
-
675
-func (m *GoTest) GetF_Fixed32Optional() uint32 {
676
- if m != nil && m.F_Fixed32Optional != nil {
677
- return *m.F_Fixed32Optional
678
- }
679
- return 0
680
-}
681
-
682
-func (m *GoTest) GetF_Fixed64Optional() uint64 {
683
- if m != nil && m.F_Fixed64Optional != nil {
684
- return *m.F_Fixed64Optional
685
- }
686
- return 0
687
-}
688
-
689
-func (m *GoTest) GetF_Uint32Optional() uint32 {
690
- if m != nil && m.F_Uint32Optional != nil {
691
- return *m.F_Uint32Optional
692
- }
693
- return 0
694
-}
695
-
696
-func (m *GoTest) GetF_Uint64Optional() uint64 {
697
- if m != nil && m.F_Uint64Optional != nil {
698
- return *m.F_Uint64Optional
699
- }
700
- return 0
701
-}
702
-
703
-func (m *GoTest) GetF_FloatOptional() float32 {
704
- if m != nil && m.F_FloatOptional != nil {
705
- return *m.F_FloatOptional
706
- }
707
- return 0
708
-}
709
-
710
-func (m *GoTest) GetF_DoubleOptional() float64 {
711
- if m != nil && m.F_DoubleOptional != nil {
712
- return *m.F_DoubleOptional
713
- }
714
- return 0
715
-}
716
-
717
-func (m *GoTest) GetF_StringOptional() string {
718
- if m != nil && m.F_StringOptional != nil {
719
- return *m.F_StringOptional
720
- }
721
- return ""
722
-}
723
-
724
-func (m *GoTest) GetF_BytesOptional() []byte {
725
- if m != nil {
726
- return m.F_BytesOptional
727
- }
728
- return nil
729
-}
730
-
731
-func (m *GoTest) GetF_Sint32Optional() int32 {
732
- if m != nil && m.F_Sint32Optional != nil {
733
- return *m.F_Sint32Optional
734
- }
735
- return 0
736
-}
737
-
738
-func (m *GoTest) GetF_Sint64Optional() int64 {
739
- if m != nil && m.F_Sint64Optional != nil {
740
- return *m.F_Sint64Optional
741
- }
742
- return 0
743
-}
744
-
745
-func (m *GoTest) GetF_BoolDefaulted() bool {
746
- if m != nil && m.F_BoolDefaulted != nil {
747
- return *m.F_BoolDefaulted
748
- }
749
- return Default_GoTest_F_BoolDefaulted
750
-}
751
-
752
-func (m *GoTest) GetF_Int32Defaulted() int32 {
753
- if m != nil && m.F_Int32Defaulted != nil {
754
- return *m.F_Int32Defaulted
755
- }
756
- return Default_GoTest_F_Int32Defaulted
757
-}
758
-
759
-func (m *GoTest) GetF_Int64Defaulted() int64 {
760
- if m != nil && m.F_Int64Defaulted != nil {
761
- return *m.F_Int64Defaulted
762
- }
763
- return Default_GoTest_F_Int64Defaulted
764
-}
765
-
766
-func (m *GoTest) GetF_Fixed32Defaulted() uint32 {
767
- if m != nil && m.F_Fixed32Defaulted != nil {
768
- return *m.F_Fixed32Defaulted
769
- }
770
- return Default_GoTest_F_Fixed32Defaulted
771
-}
772
-
773
-func (m *GoTest) GetF_Fixed64Defaulted() uint64 {
774
- if m != nil && m.F_Fixed64Defaulted != nil {
775
- return *m.F_Fixed64Defaulted
776
- }
777
- return Default_GoTest_F_Fixed64Defaulted
778
-}
779
-
780
-func (m *GoTest) GetF_Uint32Defaulted() uint32 {
781
- if m != nil && m.F_Uint32Defaulted != nil {
782
- return *m.F_Uint32Defaulted
783
- }
784
- return Default_GoTest_F_Uint32Defaulted
785
-}
786
-
787
-func (m *GoTest) GetF_Uint64Defaulted() uint64 {
788
- if m != nil && m.F_Uint64Defaulted != nil {
789
- return *m.F_Uint64Defaulted
790
- }
791
- return Default_GoTest_F_Uint64Defaulted
792
-}
793
-
794
-func (m *GoTest) GetF_FloatDefaulted() float32 {
795
- if m != nil && m.F_FloatDefaulted != nil {
796
- return *m.F_FloatDefaulted
797
- }
798
- return Default_GoTest_F_FloatDefaulted
799
-}
800
-
801
-func (m *GoTest) GetF_DoubleDefaulted() float64 {
802
- if m != nil && m.F_DoubleDefaulted != nil {
803
- return *m.F_DoubleDefaulted
804
- }
805
- return Default_GoTest_F_DoubleDefaulted
806
-}
807
-
808
-func (m *GoTest) GetF_StringDefaulted() string {
809
- if m != nil && m.F_StringDefaulted != nil {
810
- return *m.F_StringDefaulted
811
- }
812
- return Default_GoTest_F_StringDefaulted
813
-}
814
-
815
-func (m *GoTest) GetF_BytesDefaulted() []byte {
816
- if m != nil && m.F_BytesDefaulted != nil {
817
- return m.F_BytesDefaulted
818
- }
819
- return append([]byte(nil), Default_GoTest_F_BytesDefaulted...)
820
-}
821
-
822
-func (m *GoTest) GetF_Sint32Defaulted() int32 {
823
- if m != nil && m.F_Sint32Defaulted != nil {
824
- return *m.F_Sint32Defaulted
825
- }
826
- return Default_GoTest_F_Sint32Defaulted
827
-}
828
-
829
-func (m *GoTest) GetF_Sint64Defaulted() int64 {
830
- if m != nil && m.F_Sint64Defaulted != nil {
831
- return *m.F_Sint64Defaulted
832
- }
833
- return Default_GoTest_F_Sint64Defaulted
834
-}
835
-
836
-func (m *GoTest) GetF_BoolRepeatedPacked() []bool {
837
- if m != nil {
838
- return m.F_BoolRepeatedPacked
839
- }
840
- return nil
841
-}
842
-
843
-func (m *GoTest) GetF_Int32RepeatedPacked() []int32 {
844
- if m != nil {
845
- return m.F_Int32RepeatedPacked
846
- }
847
- return nil
848
-}
849
-
850
-func (m *GoTest) GetF_Int64RepeatedPacked() []int64 {
851
- if m != nil {
852
- return m.F_Int64RepeatedPacked
853
- }
854
- return nil
855
-}
856
-
857
-func (m *GoTest) GetF_Fixed32RepeatedPacked() []uint32 {
858
- if m != nil {
859
- return m.F_Fixed32RepeatedPacked
860
- }
861
- return nil
862
-}
863
-
864
-func (m *GoTest) GetF_Fixed64RepeatedPacked() []uint64 {
865
- if m != nil {
866
- return m.F_Fixed64RepeatedPacked
867
- }
868
- return nil
869
-}
870
-
871
-func (m *GoTest) GetF_Uint32RepeatedPacked() []uint32 {
872
- if m != nil {
873
- return m.F_Uint32RepeatedPacked
874
- }
875
- return nil
876
-}
877
-
878
-func (m *GoTest) GetF_Uint64RepeatedPacked() []uint64 {
879
- if m != nil {
880
- return m.F_Uint64RepeatedPacked
881
- }
882
- return nil
883
-}
884
-
885
-func (m *GoTest) GetF_FloatRepeatedPacked() []float32 {
886
- if m != nil {
887
- return m.F_FloatRepeatedPacked
888
- }
889
- return nil
890
-}
891
-
892
-func (m *GoTest) GetF_DoubleRepeatedPacked() []float64 {
893
- if m != nil {
894
- return m.F_DoubleRepeatedPacked
895
- }
896
- return nil
897
-}
898
-
899
-func (m *GoTest) GetF_Sint32RepeatedPacked() []int32 {
900
- if m != nil {
901
- return m.F_Sint32RepeatedPacked
902
- }
903
- return nil
904
-}
905
-
906
-func (m *GoTest) GetF_Sint64RepeatedPacked() []int64 {
907
- if m != nil {
908
- return m.F_Sint64RepeatedPacked
909
- }
910
- return nil
911
-}
912
-
913
-func (m *GoTest) GetRequiredgroup() *GoTest_RequiredGroup {
914
- if m != nil {
915
- return m.Requiredgroup
916
- }
917
- return nil
918
-}
919
-
920
-func (m *GoTest) GetRepeatedgroup() []*GoTest_RepeatedGroup {
921
- if m != nil {
922
- return m.Repeatedgroup
923
- }
924
- return nil
925
-}
926
-
927
-func (m *GoTest) GetOptionalgroup() *GoTest_OptionalGroup {
928
- if m != nil {
929
- return m.Optionalgroup
930
- }
931
- return nil
932
-}
933
-
934
-// Required, repeated, and optional groups.
935
-type GoTest_RequiredGroup struct {
936
- RequiredField *string `protobuf:"bytes,71,req" json:"RequiredField,omitempty"`
937
- XXX_unrecognized []byte `json:"-"`
938
-}
939
-
940
-func (m *GoTest_RequiredGroup) Reset() { *m = GoTest_RequiredGroup{} }
941
-func (m *GoTest_RequiredGroup) String() string { return proto.CompactTextString(m) }
942
-func (*GoTest_RequiredGroup) ProtoMessage() {}
943
-
944
-func (m *GoTest_RequiredGroup) GetRequiredField() string {
945
- if m != nil && m.RequiredField != nil {
946
- return *m.RequiredField
947
- }
948
- return ""
949
-}
950
-
951
-type GoTest_RepeatedGroup struct {
952
- RequiredField *string `protobuf:"bytes,81,req" json:"RequiredField,omitempty"`
953
- XXX_unrecognized []byte `json:"-"`
954
-}
955
-
956
-func (m *GoTest_RepeatedGroup) Reset() { *m = GoTest_RepeatedGroup{} }
957
-func (m *GoTest_RepeatedGroup) String() string { return proto.CompactTextString(m) }
958
-func (*GoTest_RepeatedGroup) ProtoMessage() {}
959
-
960
-func (m *GoTest_RepeatedGroup) GetRequiredField() string {
961
- if m != nil && m.RequiredField != nil {
962
- return *m.RequiredField
963
- }
964
- return ""
965
-}
966
-
967
-type GoTest_OptionalGroup struct {
968
- RequiredField *string `protobuf:"bytes,91,req" json:"RequiredField,omitempty"`
969
- XXX_unrecognized []byte `json:"-"`
970
-}
971
-
972
-func (m *GoTest_OptionalGroup) Reset() { *m = GoTest_OptionalGroup{} }
973
-func (m *GoTest_OptionalGroup) String() string { return proto.CompactTextString(m) }
974
-func (*GoTest_OptionalGroup) ProtoMessage() {}
975
-
976
-func (m *GoTest_OptionalGroup) GetRequiredField() string {
977
- if m != nil && m.RequiredField != nil {
978
- return *m.RequiredField
979
- }
980
- return ""
981
-}
982
-
983
-// For testing skipping of unrecognized fields.
984
-// Numbers are all big, larger than tag numbers in GoTestField,
985
-// the message used in the corresponding test.
986
-type GoSkipTest struct {
987
- SkipInt32 *int32 `protobuf:"varint,11,req,name=skip_int32" json:"skip_int32,omitempty"`
988
- SkipFixed32 *uint32 `protobuf:"fixed32,12,req,name=skip_fixed32" json:"skip_fixed32,omitempty"`
989
- SkipFixed64 *uint64 `protobuf:"fixed64,13,req,name=skip_fixed64" json:"skip_fixed64,omitempty"`
990
- SkipString *string `protobuf:"bytes,14,req,name=skip_string" json:"skip_string,omitempty"`
991
- Skipgroup *GoSkipTest_SkipGroup `protobuf:"group,15,req,name=SkipGroup" json:"skipgroup,omitempty"`
992
- XXX_unrecognized []byte `json:"-"`
993
-}
994
-
995
-func (m *GoSkipTest) Reset() { *m = GoSkipTest{} }
996
-func (m *GoSkipTest) String() string { return proto.CompactTextString(m) }
997
-func (*GoSkipTest) ProtoMessage() {}
998
-
999
-func (m *GoSkipTest) GetSkipInt32() int32 {
1000
- if m != nil && m.SkipInt32 != nil {
1001
- return *m.SkipInt32
1002
- }
1003
- return 0
1004
-}
1005
-
1006
-func (m *GoSkipTest) GetSkipFixed32() uint32 {
1007
- if m != nil && m.SkipFixed32 != nil {
1008
- return *m.SkipFixed32
1009
- }
1010
- return 0
1011
-}
1012
-
1013
-func (m *GoSkipTest) GetSkipFixed64() uint64 {
1014
- if m != nil && m.SkipFixed64 != nil {
1015
- return *m.SkipFixed64
1016
- }
1017
- return 0
1018
-}
1019
-
1020
-func (m *GoSkipTest) GetSkipString() string {
1021
- if m != nil && m.SkipString != nil {
1022
- return *m.SkipString
1023
- }
1024
- return ""
1025
-}
1026
-
1027
-func (m *GoSkipTest) GetSkipgroup() *GoSkipTest_SkipGroup {
1028
- if m != nil {
1029
- return m.Skipgroup
1030
- }
1031
- return nil
1032
-}
1033
-
1034
-type GoSkipTest_SkipGroup struct {
1035
- GroupInt32 *int32 `protobuf:"varint,16,req,name=group_int32" json:"group_int32,omitempty"`
1036
- GroupString *string `protobuf:"bytes,17,req,name=group_string" json:"group_string,omitempty"`
1037
- XXX_unrecognized []byte `json:"-"`
1038
-}
1039
-
1040
-func (m *GoSkipTest_SkipGroup) Reset() { *m = GoSkipTest_SkipGroup{} }
1041
-func (m *GoSkipTest_SkipGroup) String() string { return proto.CompactTextString(m) }
1042
-func (*GoSkipTest_SkipGroup) ProtoMessage() {}
1043
-
1044
-func (m *GoSkipTest_SkipGroup) GetGroupInt32() int32 {
1045
- if m != nil && m.GroupInt32 != nil {
1046
- return *m.GroupInt32
1047
- }
1048
- return 0
1049
-}
1050
-
1051
-func (m *GoSkipTest_SkipGroup) GetGroupString() string {
1052
- if m != nil && m.GroupString != nil {
1053
- return *m.GroupString
1054
- }
1055
- return ""
1056
-}
1057
-
1058
-// For testing packed/non-packed decoder switching.
1059
-// A serialized instance of one should be deserializable as the other.
1060
-type NonPackedTest struct {
1061
- A []int32 `protobuf:"varint,1,rep,name=a" json:"a,omitempty"`
1062
- XXX_unrecognized []byte `json:"-"`
1063
-}
1064
-
1065
-func (m *NonPackedTest) Reset() { *m = NonPackedTest{} }
1066
-func (m *NonPackedTest) String() string { return proto.CompactTextString(m) }
1067
-func (*NonPackedTest) ProtoMessage() {}
1068
-
1069
-func (m *NonPackedTest) GetA() []int32 {
1070
- if m != nil {
1071
- return m.A
1072
- }
1073
- return nil
1074
-}
1075
-
1076
-type PackedTest struct {
1077
- B []int32 `protobuf:"varint,1,rep,packed,name=b" json:"b,omitempty"`
1078
- XXX_unrecognized []byte `json:"-"`
1079
-}
1080
-
1081
-func (m *PackedTest) Reset() { *m = PackedTest{} }
1082
-func (m *PackedTest) String() string { return proto.CompactTextString(m) }
1083
-func (*PackedTest) ProtoMessage() {}
1084
-
1085
-func (m *PackedTest) GetB() []int32 {
1086
- if m != nil {
1087
- return m.B
1088
- }
1089
- return nil
1090
-}
1091
-
1092
-type MaxTag struct {
1093
- // Maximum possible tag number.
1094
- LastField *string `protobuf:"bytes,536870911,opt,name=last_field" json:"last_field,omitempty"`
1095
- XXX_unrecognized []byte `json:"-"`
1096
-}
1097
-
1098
-func (m *MaxTag) Reset() { *m = MaxTag{} }
1099
-func (m *MaxTag) String() string { return proto.CompactTextString(m) }
1100
-func (*MaxTag) ProtoMessage() {}
1101
-
1102
-func (m *MaxTag) GetLastField() string {
1103
- if m != nil && m.LastField != nil {
1104
- return *m.LastField
1105
- }
1106
- return ""
1107
-}
1108
-
1109
-type OldMessage struct {
1110
- Nested *OldMessage_Nested `protobuf:"bytes,1,opt,name=nested" json:"nested,omitempty"`
1111
- Num *int32 `protobuf:"varint,2,opt,name=num" json:"num,omitempty"`
1112
- XXX_unrecognized []byte `json:"-"`
1113
-}
1114
-
1115
-func (m *OldMessage) Reset() { *m = OldMessage{} }
1116
-func (m *OldMessage) String() string { return proto.CompactTextString(m) }
1117
-func (*OldMessage) ProtoMessage() {}
1118
-
1119
-func (m *OldMessage) GetNested() *OldMessage_Nested {
1120
- if m != nil {
1121
- return m.Nested
1122
- }
1123
- return nil
1124
-}
1125
-
1126
-func (m *OldMessage) GetNum() int32 {
1127
- if m != nil && m.Num != nil {
1128
- return *m.Num
1129
- }
1130
- return 0
1131
-}
1132
-
1133
-type OldMessage_Nested struct {
1134
- Name *string `protobuf:"bytes,1,opt,name=name" json:"name,omitempty"`
1135
- XXX_unrecognized []byte `json:"-"`
1136
-}
1137
-
1138
-func (m *OldMessage_Nested) Reset() { *m = OldMessage_Nested{} }
1139
-func (m *OldMessage_Nested) String() string { return proto.CompactTextString(m) }
1140
-func (*OldMessage_Nested) ProtoMessage() {}
1141
-
1142
-func (m *OldMessage_Nested) GetName() string {
1143
- if m != nil && m.Name != nil {
1144
- return *m.Name
1145
- }
1146
- return ""
1147
-}
1148
-
1149
-// NewMessage is wire compatible with OldMessage;
1150
-// imagine it as a future version.
1151
-type NewMessage struct {
1152
- Nested *NewMessage_Nested `protobuf:"bytes,1,opt,name=nested" json:"nested,omitempty"`
1153
- // This is an int32 in OldMessage.
1154
- Num *int64 `protobuf:"varint,2,opt,name=num" json:"num,omitempty"`
1155
- XXX_unrecognized []byte `json:"-"`
1156
-}
1157
-
1158
-func (m *NewMessage) Reset() { *m = NewMessage{} }
1159
-func (m *NewMessage) String() string { return proto.CompactTextString(m) }
1160
-func (*NewMessage) ProtoMessage() {}
1161
-
1162
-func (m *NewMessage) GetNested() *NewMessage_Nested {
1163
- if m != nil {
1164
- return m.Nested
1165
- }
1166
- return nil
1167
-}
1168
-
1169
-func (m *NewMessage) GetNum() int64 {
1170
- if m != nil && m.Num != nil {
1171
- return *m.Num
1172
- }
1173
- return 0
1174
-}
1175
-
1176
-type NewMessage_Nested struct {
1177
- Name *string `protobuf:"bytes,1,opt,name=name" json:"name,omitempty"`
1178
- FoodGroup *string `protobuf:"bytes,2,opt,name=food_group" json:"food_group,omitempty"`
1179
- XXX_unrecognized []byte `json:"-"`
1180
-}
1181
-
1182
-func (m *NewMessage_Nested) Reset() { *m = NewMessage_Nested{} }
1183
-func (m *NewMessage_Nested) String() string { return proto.CompactTextString(m) }
1184
-func (*NewMessage_Nested) ProtoMessage() {}
1185
-
1186
-func (m *NewMessage_Nested) GetName() string {
1187
- if m != nil && m.Name != nil {
1188
- return *m.Name
1189
- }
1190
- return ""
1191
-}
1192
-
1193
-func (m *NewMessage_Nested) GetFoodGroup() string {
1194
- if m != nil && m.FoodGroup != nil {
1195
- return *m.FoodGroup
1196
- }
1197
- return ""
1198
-}
1199
-
1200
-type InnerMessage struct {
1201
- Host *string `protobuf:"bytes,1,req,name=host" json:"host,omitempty"`
1202
- Port *int32 `protobuf:"varint,2,opt,name=port,def=4000" json:"port,omitempty"`
1203
- Connected *bool `protobuf:"varint,3,opt,name=connected" json:"connected,omitempty"`
1204
- XXX_unrecognized []byte `json:"-"`
1205
-}
1206
-
1207
-func (m *InnerMessage) Reset() { *m = InnerMessage{} }
1208
-func (m *InnerMessage) String() string { return proto.CompactTextString(m) }
1209
-func (*InnerMessage) ProtoMessage() {}
1210
-
1211
-const Default_InnerMessage_Port int32 = 4000
1212
-
1213
-func (m *InnerMessage) GetHost() string {
1214
- if m != nil && m.Host != nil {
1215
- return *m.Host
1216
- }
1217
- return ""
1218
-}
1219
-
1220
-func (m *InnerMessage) GetPort() int32 {
1221
- if m != nil && m.Port != nil {
1222
- return *m.Port
1223
- }
1224
- return Default_InnerMessage_Port
1225
-}
1226
-
1227
-func (m *InnerMessage) GetConnected() bool {
1228
- if m != nil && m.Connected != nil {
1229
- return *m.Connected
1230
- }
1231
- return false
1232
-}
1233
-
1234
-type OtherMessage struct {
1235
- Key *int64 `protobuf:"varint,1,opt,name=key" json:"key,omitempty"`
1236
- Value []byte `protobuf:"bytes,2,opt,name=value" json:"value,omitempty"`
1237
- Weight *float32 `protobuf:"fixed32,3,opt,name=weight" json:"weight,omitempty"`
1238
- Inner *InnerMessage `protobuf:"bytes,4,opt,name=inner" json:"inner,omitempty"`
1239
- XXX_unrecognized []byte `json:"-"`
1240
-}
1241
-
1242
-func (m *OtherMessage) Reset() { *m = OtherMessage{} }
1243
-func (m *OtherMessage) String() string { return proto.CompactTextString(m) }
1244
-func (*OtherMessage) ProtoMessage() {}
1245
-
1246
-func (m *OtherMessage) GetKey() int64 {
1247
- if m != nil && m.Key != nil {
1248
- return *m.Key
1249
- }
1250
- return 0
1251
-}
1252
-
1253
-func (m *OtherMessage) GetValue() []byte {
1254
- if m != nil {
1255
- return m.Value
1256
- }
1257
- return nil
1258
-}
1259
-
1260
-func (m *OtherMessage) GetWeight() float32 {
1261
- if m != nil && m.Weight != nil {
1262
- return *m.Weight
1263
- }
1264
- return 0
1265
-}
1266
-
1267
-func (m *OtherMessage) GetInner() *InnerMessage {
1268
- if m != nil {
1269
- return m.Inner
1270
- }
1271
- return nil
1272
-}
1273
-
1274
-type MyMessage struct {
1275
- Count *int32 `protobuf:"varint,1,req,name=count" json:"count,omitempty"`
1276
- Name *string `protobuf:"bytes,2,opt,name=name" json:"name,omitempty"`
1277
- Quote *string `protobuf:"bytes,3,opt,name=quote" json:"quote,omitempty"`
1278
- Pet []string `protobuf:"bytes,4,rep,name=pet" json:"pet,omitempty"`
1279
- Inner *InnerMessage `protobuf:"bytes,5,opt,name=inner" json:"inner,omitempty"`
1280
- Others []*OtherMessage `protobuf:"bytes,6,rep,name=others" json:"others,omitempty"`
1281
- RepInner []*InnerMessage `protobuf:"bytes,12,rep,name=rep_inner" json:"rep_inner,omitempty"`
1282
- Bikeshed *MyMessage_Color `protobuf:"varint,7,opt,name=bikeshed,enum=testdata.MyMessage_Color" json:"bikeshed,omitempty"`
1283
- Somegroup *MyMessage_SomeGroup `protobuf:"group,8,opt,name=SomeGroup" json:"somegroup,omitempty"`
1284
- // This field becomes [][]byte in the generated code.
1285
- RepBytes [][]byte `protobuf:"bytes,10,rep,name=rep_bytes" json:"rep_bytes,omitempty"`
1286
- Bigfloat *float64 `protobuf:"fixed64,11,opt,name=bigfloat" json:"bigfloat,omitempty"`
1287
- XXX_extensions map[int32]proto.Extension `json:"-"`
1288
- XXX_unrecognized []byte `json:"-"`
1289
-}
1290
-
1291
-func (m *MyMessage) Reset() { *m = MyMessage{} }
1292
-func (m *MyMessage) String() string { return proto.CompactTextString(m) }
1293
-func (*MyMessage) ProtoMessage() {}
1294
-
1295
-var extRange_MyMessage = []proto.ExtensionRange{
1296
- {100, 536870911},
1297
-}
1298
-
1299
-func (*MyMessage) ExtensionRangeArray() []proto.ExtensionRange {
1300
- return extRange_MyMessage
1301
-}
1302
-func (m *MyMessage) ExtensionMap() map[int32]proto.Extension {
1303
- if m.XXX_extensions == nil {
1304
- m.XXX_extensions = make(map[int32]proto.Extension)
1305
- }
1306
- return m.XXX_extensions
1307
-}
1308
-
1309
-func (m *MyMessage) GetCount() int32 {
1310
- if m != nil && m.Count != nil {
1311
- return *m.Count
1312
- }
1313
- return 0
1314
-}
1315
-
1316
-func (m *MyMessage) GetName() string {
1317
- if m != nil && m.Name != nil {
1318
- return *m.Name
1319
- }
1320
- return ""
1321
-}
1322
-
1323
-func (m *MyMessage) GetQuote() string {
1324
- if m != nil && m.Quote != nil {
1325
- return *m.Quote
1326
- }
1327
- return ""
1328
-}
1329
-
1330
-func (m *MyMessage) GetPet() []string {
1331
- if m != nil {
1332
- return m.Pet
1333
- }
1334
- return nil
1335
-}
1336
-
1337
-func (m *MyMessage) GetInner() *InnerMessage {
1338
- if m != nil {
1339
- return m.Inner
1340
- }
1341
- return nil
1342
-}
1343
-
1344
-func (m *MyMessage) GetOthers() []*OtherMessage {
1345
- if m != nil {
1346
- return m.Others
1347
- }
1348
- return nil
1349
-}
1350
-
1351
-func (m *MyMessage) GetRepInner() []*InnerMessage {
1352
- if m != nil {
1353
- return m.RepInner
1354
- }
1355
- return nil
1356
-}
1357
-
1358
-func (m *MyMessage) GetBikeshed() MyMessage_Color {
1359
- if m != nil && m.Bikeshed != nil {
1360
- return *m.Bikeshed
1361
- }
1362
- return MyMessage_RED
1363
-}
1364
-
1365
-func (m *MyMessage) GetSomegroup() *MyMessage_SomeGroup {
1366
- if m != nil {
1367
- return m.Somegroup
1368
- }
1369
- return nil
1370
-}
1371
-
1372
-func (m *MyMessage) GetRepBytes() [][]byte {
1373
- if m != nil {
1374
- return m.RepBytes
1375
- }
1376
- return nil
1377
-}
1378
-
1379
-func (m *MyMessage) GetBigfloat() float64 {
1380
- if m != nil && m.Bigfloat != nil {
1381
- return *m.Bigfloat
1382
- }
1383
- return 0
1384
-}
1385
-
1386
-type MyMessage_SomeGroup struct {
1387
- GroupField *int32 `protobuf:"varint,9,opt,name=group_field" json:"group_field,omitempty"`
1388
- XXX_unrecognized []byte `json:"-"`
1389
-}
1390
-
1391
-func (m *MyMessage_SomeGroup) Reset() { *m = MyMessage_SomeGroup{} }
1392
-func (m *MyMessage_SomeGroup) String() string { return proto.CompactTextString(m) }
1393
-func (*MyMessage_SomeGroup) ProtoMessage() {}
1394
-
1395
-func (m *MyMessage_SomeGroup) GetGroupField() int32 {
1396
- if m != nil && m.GroupField != nil {
1397
- return *m.GroupField
1398
- }
1399
- return 0
1400
-}
1401
-
1402
-type Ext struct {
1403
- Data *string `protobuf:"bytes,1,opt,name=data" json:"data,omitempty"`
1404
- XXX_unrecognized []byte `json:"-"`
1405
-}
1406
-
1407
-func (m *Ext) Reset() { *m = Ext{} }
1408
-func (m *Ext) String() string { return proto.CompactTextString(m) }
1409
-func (*Ext) ProtoMessage() {}
1410
-
1411
-func (m *Ext) GetData() string {
1412
- if m != nil && m.Data != nil {
1413
- return *m.Data
1414
- }
1415
- return ""
1416
-}
1417
-
1418
-var E_Ext_More = &proto.ExtensionDesc{
1419
- ExtendedType: (*MyMessage)(nil),
1420
- ExtensionType: (*Ext)(nil),
1421
- Field: 103,
1422
- Name: "testdata.Ext.more",
1423
- Tag: "bytes,103,opt,name=more",
1424
-}
1425
-
1426
-var E_Ext_Text = &proto.ExtensionDesc{
1427
- ExtendedType: (*MyMessage)(nil),
1428
- ExtensionType: (*string)(nil),
1429
- Field: 104,
1430
- Name: "testdata.Ext.text",
1431
- Tag: "bytes,104,opt,name=text",
1432
-}
1433
-
1434
-var E_Ext_Number = &proto.ExtensionDesc{
1435
- ExtendedType: (*MyMessage)(nil),
1436
- ExtensionType: (*int32)(nil),
1437
- Field: 105,
1438
- Name: "testdata.Ext.number",
1439
- Tag: "varint,105,opt,name=number",
1440
-}
1441
-
1442
-type DefaultsMessage struct {
1443
- XXX_extensions map[int32]proto.Extension `json:"-"`
1444
- XXX_unrecognized []byte `json:"-"`
1445
-}
1446
-
1447
-func (m *DefaultsMessage) Reset() { *m = DefaultsMessage{} }
1448
-func (m *DefaultsMessage) String() string { return proto.CompactTextString(m) }
1449
-func (*DefaultsMessage) ProtoMessage() {}
1450
-
1451
-var extRange_DefaultsMessage = []proto.ExtensionRange{
1452
- {100, 536870911},
1453
-}
1454
-
1455
-func (*DefaultsMessage) ExtensionRangeArray() []proto.ExtensionRange {
1456
- return extRange_DefaultsMessage
1457
-}
1458
-func (m *DefaultsMessage) ExtensionMap() map[int32]proto.Extension {
1459
- if m.XXX_extensions == nil {
1460
- m.XXX_extensions = make(map[int32]proto.Extension)
1461
- }
1462
- return m.XXX_extensions
1463
-}
1464
-
1465
-type MyMessageSet struct {
1466
- XXX_extensions map[int32]proto.Extension `json:"-"`
1467
- XXX_unrecognized []byte `json:"-"`
1468
-}
1469
-
1470
-func (m *MyMessageSet) Reset() { *m = MyMessageSet{} }
1471
-func (m *MyMessageSet) String() string { return proto.CompactTextString(m) }
1472
-func (*MyMessageSet) ProtoMessage() {}
1473
-
1474
-func (m *MyMessageSet) Marshal() ([]byte, error) {
1475
- return proto.MarshalMessageSet(m.ExtensionMap())
1476
-}
1477
-func (m *MyMessageSet) Unmarshal(buf []byte) error {
1478
- return proto.UnmarshalMessageSet(buf, m.ExtensionMap())
1479
-}
1480
-func (m *MyMessageSet) MarshalJSON() ([]byte, error) {
1481
- return proto.MarshalMessageSetJSON(m.XXX_extensions)
1482
-}
1483
-func (m *MyMessageSet) UnmarshalJSON(buf []byte) error {
1484
- return proto.UnmarshalMessageSetJSON(buf, m.XXX_extensions)
1485
-}
1486
-
1487
-// ensure MyMessageSet satisfies proto.Marshaler and proto.Unmarshaler
1488
-var _ proto.Marshaler = (*MyMessageSet)(nil)
1489
-var _ proto.Unmarshaler = (*MyMessageSet)(nil)
1490
-
1491
-var extRange_MyMessageSet = []proto.ExtensionRange{
1492
- {100, 2147483646},
1493
-}
1494
-
1495
-func (*MyMessageSet) ExtensionRangeArray() []proto.ExtensionRange {
1496
- return extRange_MyMessageSet
1497
-}
1498
-func (m *MyMessageSet) ExtensionMap() map[int32]proto.Extension {
1499
- if m.XXX_extensions == nil {
1500
- m.XXX_extensions = make(map[int32]proto.Extension)
1501
- }
1502
- return m.XXX_extensions
1503
-}
1504
-
1505
-type Empty struct {
1506
- XXX_unrecognized []byte `json:"-"`
1507
-}
1508
-
1509
-func (m *Empty) Reset() { *m = Empty{} }
1510
-func (m *Empty) String() string { return proto.CompactTextString(m) }
1511
-func (*Empty) ProtoMessage() {}
1512
-
1513
-type MessageList struct {
1514
- Message []*MessageList_Message `protobuf:"group,1,rep" json:"message,omitempty"`
1515
- XXX_unrecognized []byte `json:"-"`
1516
-}
1517
-
1518
-func (m *MessageList) Reset() { *m = MessageList{} }
1519
-func (m *MessageList) String() string { return proto.CompactTextString(m) }
1520
-func (*MessageList) ProtoMessage() {}
1521
-
1522
-func (m *MessageList) GetMessage() []*MessageList_Message {
1523
- if m != nil {
1524
- return m.Message
1525
- }
1526
- return nil
1527
-}
1528
-
1529
-type MessageList_Message struct {
1530
- Name *string `protobuf:"bytes,2,req,name=name" json:"name,omitempty"`
1531
- Count *int32 `protobuf:"varint,3,req,name=count" json:"count,omitempty"`
1532
- XXX_unrecognized []byte `json:"-"`
1533
-}
1534
-
1535
-func (m *MessageList_Message) Reset() { *m = MessageList_Message{} }
1536
-func (m *MessageList_Message) String() string { return proto.CompactTextString(m) }
1537
-func (*MessageList_Message) ProtoMessage() {}
1538
-
1539
-func (m *MessageList_Message) GetName() string {
1540
- if m != nil && m.Name != nil {
1541
- return *m.Name
1542
- }
1543
- return ""
1544
-}
1545
-
1546
-func (m *MessageList_Message) GetCount() int32 {
1547
- if m != nil && m.Count != nil {
1548
- return *m.Count
1549
- }
1550
- return 0
1551
-}
1552
-
1553
-type Strings struct {
1554
- StringField *string `protobuf:"bytes,1,opt,name=string_field" json:"string_field,omitempty"`
1555
- BytesField []byte `protobuf:"bytes,2,opt,name=bytes_field" json:"bytes_field,omitempty"`
1556
- XXX_unrecognized []byte `json:"-"`
1557
-}
1558
-
1559
-func (m *Strings) Reset() { *m = Strings{} }
1560
-func (m *Strings) String() string { return proto.CompactTextString(m) }
1561
-func (*Strings) ProtoMessage() {}
1562
-
1563
-func (m *Strings) GetStringField() string {
1564
- if m != nil && m.StringField != nil {
1565
- return *m.StringField
1566
- }
1567
- return ""
1568
-}
1569
-
1570
-func (m *Strings) GetBytesField() []byte {
1571
- if m != nil {
1572
- return m.BytesField
1573
- }
1574
- return nil
1575
-}
1576
-
1577
-type Defaults struct {
1578
- // Default-valued fields of all basic types.
1579
- // Same as GoTest, but copied here to make testing easier.
1580
- F_Bool *bool `protobuf:"varint,1,opt,def=1" json:"F_Bool,omitempty"`
1581
- F_Int32 *int32 `protobuf:"varint,2,opt,def=32" json:"F_Int32,omitempty"`
1582
- F_Int64 *int64 `protobuf:"varint,3,opt,def=64" json:"F_Int64,omitempty"`
1583
- F_Fixed32 *uint32 `protobuf:"fixed32,4,opt,def=320" json:"F_Fixed32,omitempty"`
1584
- F_Fixed64 *uint64 `protobuf:"fixed64,5,opt,def=640" json:"F_Fixed64,omitempty"`
1585
- F_Uint32 *uint32 `protobuf:"varint,6,opt,def=3200" json:"F_Uint32,omitempty"`
1586
- F_Uint64 *uint64 `protobuf:"varint,7,opt,def=6400" json:"F_Uint64,omitempty"`
1587
- F_Float *float32 `protobuf:"fixed32,8,opt,def=314159" json:"F_Float,omitempty"`
1588
- F_Double *float64 `protobuf:"fixed64,9,opt,def=271828" json:"F_Double,omitempty"`
1589
- F_String *string `protobuf:"bytes,10,opt,def=hello, \"world!\"\n" json:"F_String,omitempty"`
1590
- F_Bytes []byte `protobuf:"bytes,11,opt,def=Bignose" json:"F_Bytes,omitempty"`
1591
- F_Sint32 *int32 `protobuf:"zigzag32,12,opt,def=-32" json:"F_Sint32,omitempty"`
1592
- F_Sint64 *int64 `protobuf:"zigzag64,13,opt,def=-64" json:"F_Sint64,omitempty"`
1593
- F_Enum *Defaults_Color `protobuf:"varint,14,opt,enum=testdata.Defaults_Color,def=1" json:"F_Enum,omitempty"`
1594
- // More fields with crazy defaults.
1595
- F_Pinf *float32 `protobuf:"fixed32,15,opt,def=inf" json:"F_Pinf,omitempty"`
1596
- F_Ninf *float32 `protobuf:"fixed32,16,opt,def=-inf" json:"F_Ninf,omitempty"`
1597
- F_Nan *float32 `protobuf:"fixed32,17,opt,def=nan" json:"F_Nan,omitempty"`
1598
- // Sub-message.
1599
- Sub *SubDefaults `protobuf:"bytes,18,opt,name=sub" json:"sub,omitempty"`
1600
- // Redundant but explicit defaults.
1601
- StrZero *string `protobuf:"bytes,19,opt,name=str_zero,def=" json:"str_zero,omitempty"`
1602
- XXX_unrecognized []byte `json:"-"`
1603
-}
1604
-
1605
-func (m *Defaults) Reset() { *m = Defaults{} }
1606
-func (m *Defaults) String() string { return proto.CompactTextString(m) }
1607
-func (*Defaults) ProtoMessage() {}
1608
-
1609
-const Default_Defaults_F_Bool bool = true
1610
-const Default_Defaults_F_Int32 int32 = 32
1611
-const Default_Defaults_F_Int64 int64 = 64
1612
-const Default_Defaults_F_Fixed32 uint32 = 320
1613
-const Default_Defaults_F_Fixed64 uint64 = 640
1614
-const Default_Defaults_F_Uint32 uint32 = 3200
1615
-const Default_Defaults_F_Uint64 uint64 = 6400
1616
-const Default_Defaults_F_Float float32 = 314159
1617
-const Default_Defaults_F_Double float64 = 271828
1618
-const Default_Defaults_F_String string = "hello, \"world!\"\n"
1619
-
1620
-var Default_Defaults_F_Bytes []byte = []byte("Bignose")
1621
-
1622
-const Default_Defaults_F_Sint32 int32 = -32
1623
-const Default_Defaults_F_Sint64 int64 = -64
1624
-const Default_Defaults_F_Enum Defaults_Color = Defaults_GREEN
1625
-
1626
-var Default_Defaults_F_Pinf float32 = float32(math.Inf(1))
1627
-var Default_Defaults_F_Ninf float32 = float32(math.Inf(-1))
1628
-var Default_Defaults_F_Nan float32 = float32(math.NaN())
1629
-
1630
-func (m *Defaults) GetF_Bool() bool {
1631
- if m != nil && m.F_Bool != nil {
1632
- return *m.F_Bool
1633
- }
1634
- return Default_Defaults_F_Bool
1635
-}
1636
-
1637
-func (m *Defaults) GetF_Int32() int32 {
1638
- if m != nil && m.F_Int32 != nil {
1639
- return *m.F_Int32
1640
- }
1641
- return Default_Defaults_F_Int32
1642
-}
1643
-
1644
-func (m *Defaults) GetF_Int64() int64 {
1645
- if m != nil && m.F_Int64 != nil {
1646
- return *m.F_Int64
1647
- }
1648
- return Default_Defaults_F_Int64
1649
-}
1650
-
1651
-func (m *Defaults) GetF_Fixed32() uint32 {
1652
- if m != nil && m.F_Fixed32 != nil {
1653
- return *m.F_Fixed32
1654
- }
1655
- return Default_Defaults_F_Fixed32
1656
-}
1657
-
1658
-func (m *Defaults) GetF_Fixed64() uint64 {
1659
- if m != nil && m.F_Fixed64 != nil {
1660
- return *m.F_Fixed64
1661
- }
1662
- return Default_Defaults_F_Fixed64
1663
-}
1664
-
1665
-func (m *Defaults) GetF_Uint32() uint32 {
1666
- if m != nil && m.F_Uint32 != nil {
1667
- return *m.F_Uint32
1668
- }
1669
- return Default_Defaults_F_Uint32
1670
-}
1671
-
1672
-func (m *Defaults) GetF_Uint64() uint64 {
1673
- if m != nil && m.F_Uint64 != nil {
1674
- return *m.F_Uint64
1675
- }
1676
- return Default_Defaults_F_Uint64
1677
-}
1678
-
1679
-func (m *Defaults) GetF_Float() float32 {
1680
- if m != nil && m.F_Float != nil {
1681
- return *m.F_Float
1682
- }
1683
- return Default_Defaults_F_Float
1684
-}
1685
-
1686
-func (m *Defaults) GetF_Double() float64 {
1687
- if m != nil && m.F_Double != nil {
1688
- return *m.F_Double
1689
- }
1690
- return Default_Defaults_F_Double
1691
-}
1692
-
1693
-func (m *Defaults) GetF_String() string {
1694
- if m != nil && m.F_String != nil {
1695
- return *m.F_String
1696
- }
1697
- return Default_Defaults_F_String
1698
-}
1699
-
1700
-func (m *Defaults) GetF_Bytes() []byte {
1701
- if m != nil && m.F_Bytes != nil {
1702
- return m.F_Bytes
1703
- }
1704
- return append([]byte(nil), Default_Defaults_F_Bytes...)
1705
-}
1706
-
1707
-func (m *Defaults) GetF_Sint32() int32 {
1708
- if m != nil && m.F_Sint32 != nil {
1709
- return *m.F_Sint32
1710
- }
1711
- return Default_Defaults_F_Sint32
1712
-}
1713
-
1714
-func (m *Defaults) GetF_Sint64() int64 {
1715
- if m != nil && m.F_Sint64 != nil {
1716
- return *m.F_Sint64
1717
- }
1718
- return Default_Defaults_F_Sint64
1719
-}
1720
-
1721
-func (m *Defaults) GetF_Enum() Defaults_Color {
1722
- if m != nil && m.F_Enum != nil {
1723
- return *m.F_Enum
1724
- }
1725
- return Default_Defaults_F_Enum
1726
-}
1727
-
1728
-func (m *Defaults) GetF_Pinf() float32 {
1729
- if m != nil && m.F_Pinf != nil {
1730
- return *m.F_Pinf
1731
- }
1732
- return Default_Defaults_F_Pinf
1733
-}
1734
-
1735
-func (m *Defaults) GetF_Ninf() float32 {
1736
- if m != nil && m.F_Ninf != nil {
1737
- return *m.F_Ninf
1738
- }
1739
- return Default_Defaults_F_Ninf
1740
-}
1741
-
1742
-func (m *Defaults) GetF_Nan() float32 {
1743
- if m != nil && m.F_Nan != nil {
1744
- return *m.F_Nan
1745
- }
1746
- return Default_Defaults_F_Nan
1747
-}
1748
-
1749
-func (m *Defaults) GetSub() *SubDefaults {
1750
- if m != nil {
1751
- return m.Sub
1752
- }
1753
- return nil
1754
-}
1755
-
1756
-func (m *Defaults) GetStrZero() string {
1757
- if m != nil && m.StrZero != nil {
1758
- return *m.StrZero
1759
- }
1760
- return ""
1761
-}
1762
-
1763
-type SubDefaults struct {
1764
- N *int64 `protobuf:"varint,1,opt,name=n,def=7" json:"n,omitempty"`
1765
- XXX_unrecognized []byte `json:"-"`
1766
-}
1767
-
1768
-func (m *SubDefaults) Reset() { *m = SubDefaults{} }
1769
-func (m *SubDefaults) String() string { return proto.CompactTextString(m) }
1770
-func (*SubDefaults) ProtoMessage() {}
1771
-
1772
-const Default_SubDefaults_N int64 = 7
1773
-
1774
-func (m *SubDefaults) GetN() int64 {
1775
- if m != nil && m.N != nil {
1776
- return *m.N
1777
- }
1778
- return Default_SubDefaults_N
1779
-}
1780
-
1781
-type RepeatedEnum struct {
1782
- Color []RepeatedEnum_Color `protobuf:"varint,1,rep,name=color,enum=testdata.RepeatedEnum_Color" json:"color,omitempty"`
1783
- XXX_unrecognized []byte `json:"-"`
1784
-}
1785
-
1786
-func (m *RepeatedEnum) Reset() { *m = RepeatedEnum{} }
1787
-func (m *RepeatedEnum) String() string { return proto.CompactTextString(m) }
1788
-func (*RepeatedEnum) ProtoMessage() {}
1789
-
1790
-func (m *RepeatedEnum) GetColor() []RepeatedEnum_Color {
1791
- if m != nil {
1792
- return m.Color
1793
- }
1794
- return nil
1795
-}
1796
-
1797
-type MoreRepeated struct {
1798
- Bools []bool `protobuf:"varint,1,rep,name=bools" json:"bools,omitempty"`
1799
- BoolsPacked []bool `protobuf:"varint,2,rep,packed,name=bools_packed" json:"bools_packed,omitempty"`
1800
- Ints []int32 `protobuf:"varint,3,rep,name=ints" json:"ints,omitempty"`
1801
- IntsPacked []int32 `protobuf:"varint,4,rep,packed,name=ints_packed" json:"ints_packed,omitempty"`
1802
- Int64SPacked []int64 `protobuf:"varint,7,rep,packed,name=int64s_packed" json:"int64s_packed,omitempty"`
1803
- Strings []string `protobuf:"bytes,5,rep,name=strings" json:"strings,omitempty"`
1804
- Fixeds []uint32 `protobuf:"fixed32,6,rep,name=fixeds" json:"fixeds,omitempty"`
1805
- XXX_unrecognized []byte `json:"-"`
1806
-}
1807
-
1808
-func (m *MoreRepeated) Reset() { *m = MoreRepeated{} }
1809
-func (m *MoreRepeated) String() string { return proto.CompactTextString(m) }
1810
-func (*MoreRepeated) ProtoMessage() {}
1811
-
1812
-func (m *MoreRepeated) GetBools() []bool {
1813
- if m != nil {
1814
- return m.Bools
1815
- }
1816
- return nil
1817
-}
1818
-
1819
-func (m *MoreRepeated) GetBoolsPacked() []bool {
1820
- if m != nil {
1821
- return m.BoolsPacked
1822
- }
1823
- return nil
1824
-}
1825
-
1826
-func (m *MoreRepeated) GetInts() []int32 {
1827
- if m != nil {
1828
- return m.Ints
1829
- }
1830
- return nil
1831
-}
1832
-
1833
-func (m *MoreRepeated) GetIntsPacked() []int32 {
1834
- if m != nil {
1835
- return m.IntsPacked
1836
- }
1837
- return nil
1838
-}
1839
-
1840
-func (m *MoreRepeated) GetInt64SPacked() []int64 {
1841
- if m != nil {
1842
- return m.Int64SPacked
1843
- }
1844
- return nil
1845
-}
1846
-
1847
-func (m *MoreRepeated) GetStrings() []string {
1848
- if m != nil {
1849
- return m.Strings
1850
- }
1851
- return nil
1852
-}
1853
-
1854
-func (m *MoreRepeated) GetFixeds() []uint32 {
1855
- if m != nil {
1856
- return m.Fixeds
1857
- }
1858
- return nil
1859
-}
1860
-
1861
-type GroupOld struct {
1862
- G *GroupOld_G `protobuf:"group,101,opt" json:"g,omitempty"`
1863
- XXX_unrecognized []byte `json:"-"`
1864
-}
1865
-
1866
-func (m *GroupOld) Reset() { *m = GroupOld{} }
1867
-func (m *GroupOld) String() string { return proto.CompactTextString(m) }
1868
-func (*GroupOld) ProtoMessage() {}
1869
-
1870
-func (m *GroupOld) GetG() *GroupOld_G {
1871
- if m != nil {
1872
- return m.G
1873
- }
1874
- return nil
1875
-}
1876
-
1877
-type GroupOld_G struct {
1878
- X *int32 `protobuf:"varint,2,opt,name=x" json:"x,omitempty"`
1879
- XXX_unrecognized []byte `json:"-"`
1880
-}
1881
-
1882
-func (m *GroupOld_G) Reset() { *m = GroupOld_G{} }
1883
-func (m *GroupOld_G) String() string { return proto.CompactTextString(m) }
1884
-func (*GroupOld_G) ProtoMessage() {}
1885
-
1886
-func (m *GroupOld_G) GetX() int32 {
1887
- if m != nil && m.X != nil {
1888
- return *m.X
1889
- }
1890
- return 0
1891
-}
1892
-
1893
-type GroupNew struct {
1894
- G *GroupNew_G `protobuf:"group,101,opt" json:"g,omitempty"`
1895
- XXX_unrecognized []byte `json:"-"`
1896
-}
1897
-
1898
-func (m *GroupNew) Reset() { *m = GroupNew{} }
1899
-func (m *GroupNew) String() string { return proto.CompactTextString(m) }
1900
-func (*GroupNew) ProtoMessage() {}
1901
-
1902
-func (m *GroupNew) GetG() *GroupNew_G {
1903
- if m != nil {
1904
- return m.G
1905
- }
1906
- return nil
1907
-}
1908
-
1909
-type GroupNew_G struct {
1910
- X *int32 `protobuf:"varint,2,opt,name=x" json:"x,omitempty"`
1911
- Y *int32 `protobuf:"varint,3,opt,name=y" json:"y,omitempty"`
1912
- XXX_unrecognized []byte `json:"-"`
1913
-}
1914
-
1915
-func (m *GroupNew_G) Reset() { *m = GroupNew_G{} }
1916
-func (m *GroupNew_G) String() string { return proto.CompactTextString(m) }
1917
-func (*GroupNew_G) ProtoMessage() {}
1918
-
1919
-func (m *GroupNew_G) GetX() int32 {
1920
- if m != nil && m.X != nil {
1921
- return *m.X
1922
- }
1923
- return 0
1924
-}
1925
-
1926
-func (m *GroupNew_G) GetY() int32 {
1927
- if m != nil && m.Y != nil {
1928
- return *m.Y
1929
- }
1930
- return 0
1931
-}
1932
-
1933
-type FloatingPoint struct {
1934
- F *float64 `protobuf:"fixed64,1,req,name=f" json:"f,omitempty"`
1935
- XXX_unrecognized []byte `json:"-"`
1936
-}
1937
-
1938
-func (m *FloatingPoint) Reset() { *m = FloatingPoint{} }
1939
-func (m *FloatingPoint) String() string { return proto.CompactTextString(m) }
1940
-func (*FloatingPoint) ProtoMessage() {}
1941
-
1942
-func (m *FloatingPoint) GetF() float64 {
1943
- if m != nil && m.F != nil {
1944
- return *m.F
1945
- }
1946
- return 0
1947
-}
1948
-
1949
-type MessageWithMap struct {
1950
- NameMapping map[int32]string `protobuf:"bytes,1,rep,name=name_mapping" json:"name_mapping,omitempty" protobuf_key:"varint,1,opt,name=key" protobuf_val:"bytes,2,opt,name=value"`
1951
- MsgMapping map[int64]*FloatingPoint `protobuf:"bytes,2,rep,name=msg_mapping" json:"msg_mapping,omitempty" protobuf_key:"zigzag64,1,opt,name=key" protobuf_val:"bytes,2,opt,name=value"`
1952
- ByteMapping map[bool][]byte `protobuf:"bytes,3,rep,name=byte_mapping" json:"byte_mapping,omitempty" protobuf_key:"varint,1,opt,name=key" protobuf_val:"bytes,2,opt,name=value"`
1953
- StrToStr map[string]string `protobuf:"bytes,4,rep,name=str_to_str" json:"str_to_str,omitempty" protobuf_key:"bytes,1,opt,name=key" protobuf_val:"bytes,2,opt,name=value"`
1954
- XXX_unrecognized []byte `json:"-"`
1955
-}
1956
-
1957
-func (m *MessageWithMap) Reset() { *m = MessageWithMap{} }
1958
-func (m *MessageWithMap) String() string { return proto.CompactTextString(m) }
1959
-func (*MessageWithMap) ProtoMessage() {}
1960
-
1961
-func (m *MessageWithMap) GetNameMapping() map[int32]string {
1962
- if m != nil {
1963
- return m.NameMapping
1964
- }
1965
- return nil
1966
-}
1967
-
1968
-func (m *MessageWithMap) GetMsgMapping() map[int64]*FloatingPoint {
1969
- if m != nil {
1970
- return m.MsgMapping
1971
- }
1972
- return nil
1973
-}
1974
-
1975
-func (m *MessageWithMap) GetByteMapping() map[bool][]byte {
1976
- if m != nil {
1977
- return m.ByteMapping
1978
- }
1979
- return nil
1980
-}
1981
-
1982
-func (m *MessageWithMap) GetStrToStr() map[string]string {
1983
- if m != nil {
1984
- return m.StrToStr
1985
- }
1986
- return nil
1987
-}
1988
-
1989
-var E_Greeting = &proto.ExtensionDesc{
1990
- ExtendedType: (*MyMessage)(nil),
1991
- ExtensionType: ([]string)(nil),
1992
- Field: 106,
1993
- Name: "testdata.greeting",
1994
- Tag: "bytes,106,rep,name=greeting",
1995
-}
1996
-
1997
-var E_NoDefaultDouble = &proto.ExtensionDesc{
1998
- ExtendedType: (*DefaultsMessage)(nil),
1999
- ExtensionType: (*float64)(nil),
2000
- Field: 101,
2001
- Name: "testdata.no_default_double",
2002
- Tag: "fixed64,101,opt,name=no_default_double",
2003
-}
2004
-
2005
-var E_NoDefaultFloat = &proto.ExtensionDesc{
2006
- ExtendedType: (*DefaultsMessage)(nil),
2007
- ExtensionType: (*float32)(nil),
2008
- Field: 102,
2009
- Name: "testdata.no_default_float",
2010
- Tag: "fixed32,102,opt,name=no_default_float",
2011
-}
2012
-
2013
-var E_NoDefaultInt32 = &proto.ExtensionDesc{
2014
- ExtendedType: (*DefaultsMessage)(nil),
2015
- ExtensionType: (*int32)(nil),
2016
- Field: 103,
2017
- Name: "testdata.no_default_int32",
2018
- Tag: "varint,103,opt,name=no_default_int32",
2019
-}
2020
-
2021
-var E_NoDefaultInt64 = &proto.ExtensionDesc{
2022
- ExtendedType: (*DefaultsMessage)(nil),
2023
- ExtensionType: (*int64)(nil),
2024
- Field: 104,
2025
- Name: "testdata.no_default_int64",
2026
- Tag: "varint,104,opt,name=no_default_int64",
2027
-}
2028
-
2029
-var E_NoDefaultUint32 = &proto.ExtensionDesc{
2030
- ExtendedType: (*DefaultsMessage)(nil),
2031
- ExtensionType: (*uint32)(nil),
2032
- Field: 105,
2033
- Name: "testdata.no_default_uint32",
2034
- Tag: "varint,105,opt,name=no_default_uint32",
2035
-}
2036
-
2037
-var E_NoDefaultUint64 = &proto.ExtensionDesc{
2038
- ExtendedType: (*DefaultsMessage)(nil),
2039
- ExtensionType: (*uint64)(nil),
2040
- Field: 106,
2041
- Name: "testdata.no_default_uint64",
2042
- Tag: "varint,106,opt,name=no_default_uint64",
2043
-}
2044
-
2045
-var E_NoDefaultSint32 = &proto.ExtensionDesc{
2046
- ExtendedType: (*DefaultsMessage)(nil),
2047
- ExtensionType: (*int32)(nil),
2048
- Field: 107,
2049
- Name: "testdata.no_default_sint32",
2050
- Tag: "zigzag32,107,opt,name=no_default_sint32",
2051
-}
2052
-
2053
-var E_NoDefaultSint64 = &proto.ExtensionDesc{
2054
- ExtendedType: (*DefaultsMessage)(nil),
2055
- ExtensionType: (*int64)(nil),
2056
- Field: 108,
2057
- Name: "testdata.no_default_sint64",
2058
- Tag: "zigzag64,108,opt,name=no_default_sint64",
2059
-}
2060
-
2061
-var E_NoDefaultFixed32 = &proto.ExtensionDesc{
2062
- ExtendedType: (*DefaultsMessage)(nil),
2063
- ExtensionType: (*uint32)(nil),
2064
- Field: 109,
2065
- Name: "testdata.no_default_fixed32",
2066
- Tag: "fixed32,109,opt,name=no_default_fixed32",
2067
-}
2068
-
2069
-var E_NoDefaultFixed64 = &proto.ExtensionDesc{
2070
- ExtendedType: (*DefaultsMessage)(nil),
2071
- ExtensionType: (*uint64)(nil),
2072
- Field: 110,
2073
- Name: "testdata.no_default_fixed64",
2074
- Tag: "fixed64,110,opt,name=no_default_fixed64",
2075
-}
2076
-
2077
-var E_NoDefaultSfixed32 = &proto.ExtensionDesc{
2078
- ExtendedType: (*DefaultsMessage)(nil),
2079
- ExtensionType: (*int32)(nil),
2080
- Field: 111,
2081
- Name: "testdata.no_default_sfixed32",
2082
- Tag: "fixed32,111,opt,name=no_default_sfixed32",
2083
-}
2084
-
2085
-var E_NoDefaultSfixed64 = &proto.ExtensionDesc{
2086
- ExtendedType: (*DefaultsMessage)(nil),
2087
- ExtensionType: (*int64)(nil),
2088
- Field: 112,
2089
- Name: "testdata.no_default_sfixed64",
2090
- Tag: "fixed64,112,opt,name=no_default_sfixed64",
2091
-}
2092
-
2093
-var E_NoDefaultBool = &proto.ExtensionDesc{
2094
- ExtendedType: (*DefaultsMessage)(nil),
2095
- ExtensionType: (*bool)(nil),
2096
- Field: 113,
2097
- Name: "testdata.no_default_bool",
2098
- Tag: "varint,113,opt,name=no_default_bool",
2099
-}
2100
-
2101
-var E_NoDefaultString = &proto.ExtensionDesc{
2102
- ExtendedType: (*DefaultsMessage)(nil),
2103
- ExtensionType: (*string)(nil),
2104
- Field: 114,
2105
- Name: "testdata.no_default_string",
2106
- Tag: "bytes,114,opt,name=no_default_string",
2107
-}
2108
-
2109
-var E_NoDefaultBytes = &proto.ExtensionDesc{
2110
- ExtendedType: (*DefaultsMessage)(nil),
2111
- ExtensionType: ([]byte)(nil),
2112
- Field: 115,
2113
- Name: "testdata.no_default_bytes",
2114
- Tag: "bytes,115,opt,name=no_default_bytes",
2115
-}
2116
-
2117
-var E_NoDefaultEnum = &proto.ExtensionDesc{
2118
- ExtendedType: (*DefaultsMessage)(nil),
2119
- ExtensionType: (*DefaultsMessage_DefaultsEnum)(nil),
2120
- Field: 116,
2121
- Name: "testdata.no_default_enum",
2122
- Tag: "varint,116,opt,name=no_default_enum,enum=testdata.DefaultsMessage_DefaultsEnum",
2123
-}
2124
-
2125
-var E_DefaultDouble = &proto.ExtensionDesc{
2126
- ExtendedType: (*DefaultsMessage)(nil),
2127
- ExtensionType: (*float64)(nil),
2128
- Field: 201,
2129
- Name: "testdata.default_double",
2130
- Tag: "fixed64,201,opt,name=default_double,def=3.1415",
2131
-}
2132
-
2133
-var E_DefaultFloat = &proto.ExtensionDesc{
2134
- ExtendedType: (*DefaultsMessage)(nil),
2135
- ExtensionType: (*float32)(nil),
2136
- Field: 202,
2137
- Name: "testdata.default_float",
2138
- Tag: "fixed32,202,opt,name=default_float,def=3.14",
2139
-}
2140
-
2141
-var E_DefaultInt32 = &proto.ExtensionDesc{
2142
- ExtendedType: (*DefaultsMessage)(nil),
2143
- ExtensionType: (*int32)(nil),
2144
- Field: 203,
2145
- Name: "testdata.default_int32",
2146
- Tag: "varint,203,opt,name=default_int32,def=42",
2147
-}
2148
-
2149
-var E_DefaultInt64 = &proto.ExtensionDesc{
2150
- ExtendedType: (*DefaultsMessage)(nil),
2151
- ExtensionType: (*int64)(nil),
2152
- Field: 204,
2153
- Name: "testdata.default_int64",
2154
- Tag: "varint,204,opt,name=default_int64,def=43",
2155
-}
2156
-
2157
-var E_DefaultUint32 = &proto.ExtensionDesc{
2158
- ExtendedType: (*DefaultsMessage)(nil),
2159
- ExtensionType: (*uint32)(nil),
2160
- Field: 205,
2161
- Name: "testdata.default_uint32",
2162
- Tag: "varint,205,opt,name=default_uint32,def=44",
2163
-}
2164
-
2165
-var E_DefaultUint64 = &proto.ExtensionDesc{
2166
- ExtendedType: (*DefaultsMessage)(nil),
2167
- ExtensionType: (*uint64)(nil),
2168
- Field: 206,
2169
- Name: "testdata.default_uint64",
2170
- Tag: "varint,206,opt,name=default_uint64,def=45",
2171
-}
2172
-
2173
-var E_DefaultSint32 = &proto.ExtensionDesc{
2174
- ExtendedType: (*DefaultsMessage)(nil),
2175
- ExtensionType: (*int32)(nil),
2176
- Field: 207,
2177
- Name: "testdata.default_sint32",
2178
- Tag: "zigzag32,207,opt,name=default_sint32,def=46",
2179
-}
2180
-
2181
-var E_DefaultSint64 = &proto.ExtensionDesc{
2182
- ExtendedType: (*DefaultsMessage)(nil),
2183
- ExtensionType: (*int64)(nil),
2184
- Field: 208,
2185
- Name: "testdata.default_sint64",
2186
- Tag: "zigzag64,208,opt,name=default_sint64,def=47",
2187
-}
2188
-
2189
-var E_DefaultFixed32 = &proto.ExtensionDesc{
2190
- ExtendedType: (*DefaultsMessage)(nil),
2191
- ExtensionType: (*uint32)(nil),
2192
- Field: 209,
2193
- Name: "testdata.default_fixed32",
2194
- Tag: "fixed32,209,opt,name=default_fixed32,def=48",
2195
-}
2196
-
2197
-var E_DefaultFixed64 = &proto.ExtensionDesc{
2198
- ExtendedType: (*DefaultsMessage)(nil),
2199
- ExtensionType: (*uint64)(nil),
2200
- Field: 210,
2201
- Name: "testdata.default_fixed64",
2202
- Tag: "fixed64,210,opt,name=default_fixed64,def=49",
2203
-}
2204
-
2205
-var E_DefaultSfixed32 = &proto.ExtensionDesc{
2206
- ExtendedType: (*DefaultsMessage)(nil),
2207
- ExtensionType: (*int32)(nil),
2208
- Field: 211,
2209
- Name: "testdata.default_sfixed32",
2210
- Tag: "fixed32,211,opt,name=default_sfixed32,def=50",
2211
-}
2212
-
2213
-var E_DefaultSfixed64 = &proto.ExtensionDesc{
2214
- ExtendedType: (*DefaultsMessage)(nil),
2215
- ExtensionType: (*int64)(nil),
2216
- Field: 212,
2217
- Name: "testdata.default_sfixed64",
2218
- Tag: "fixed64,212,opt,name=default_sfixed64,def=51",
2219
-}
2220
-
2221
-var E_DefaultBool = &proto.ExtensionDesc{
2222
- ExtendedType: (*DefaultsMessage)(nil),
2223
- ExtensionType: (*bool)(nil),
2224
- Field: 213,
2225
- Name: "testdata.default_bool",
2226
- Tag: "varint,213,opt,name=default_bool,def=1",
2227
-}
2228
-
2229
-var E_DefaultString = &proto.ExtensionDesc{
2230
- ExtendedType: (*DefaultsMessage)(nil),
2231
- ExtensionType: (*string)(nil),
2232
- Field: 214,
2233
- Name: "testdata.default_string",
2234
- Tag: "bytes,214,opt,name=default_string,def=Hello, string",
2235
-}
2236
-
2237
-var E_DefaultBytes = &proto.ExtensionDesc{
2238
- ExtendedType: (*DefaultsMessage)(nil),
2239
- ExtensionType: ([]byte)(nil),
2240
- Field: 215,
2241
- Name: "testdata.default_bytes",
2242
- Tag: "bytes,215,opt,name=default_bytes,def=Hello, bytes",
2243
-}
2244
-
2245
-var E_DefaultEnum = &proto.ExtensionDesc{
2246
- ExtendedType: (*DefaultsMessage)(nil),
2247
- ExtensionType: (*DefaultsMessage_DefaultsEnum)(nil),
2248
- Field: 216,
2249
- Name: "testdata.default_enum",
2250
- Tag: "varint,216,opt,name=default_enum,enum=testdata.DefaultsMessage_DefaultsEnum,def=1",
2251
-}
2252
-
2253
-var E_X201 = &proto.ExtensionDesc{
2254
- ExtendedType: (*MyMessageSet)(nil),
2255
- ExtensionType: (*Empty)(nil),
2256
- Field: 201,
2257
- Name: "testdata.x201",
2258
- Tag: "bytes,201,opt,name=x201",
2259
-}
2260
-
2261
-var E_X202 = &proto.ExtensionDesc{
2262
- ExtendedType: (*MyMessageSet)(nil),
2263
- ExtensionType: (*Empty)(nil),
2264
- Field: 202,
2265
- Name: "testdata.x202",
2266
- Tag: "bytes,202,opt,name=x202",
2267
-}
2268
-
2269
-var E_X203 = &proto.ExtensionDesc{
2270
- ExtendedType: (*MyMessageSet)(nil),
2271
- ExtensionType: (*Empty)(nil),
2272
- Field: 203,
2273
- Name: "testdata.x203",
2274
- Tag: "bytes,203,opt,name=x203",
2275
-}
2276
-
2277
-var E_X204 = &proto.ExtensionDesc{
2278
- ExtendedType: (*MyMessageSet)(nil),
2279
- ExtensionType: (*Empty)(nil),
2280
- Field: 204,
2281
- Name: "testdata.x204",
2282
- Tag: "bytes,204,opt,name=x204",
2283
-}
2284
-
2285
-var E_X205 = &proto.ExtensionDesc{
2286
- ExtendedType: (*MyMessageSet)(nil),
2287
- ExtensionType: (*Empty)(nil),
2288
- Field: 205,
2289
- Name: "testdata.x205",
2290
- Tag: "bytes,205,opt,name=x205",
2291
-}
2292
-
2293
-var E_X206 = &proto.ExtensionDesc{
2294
- ExtendedType: (*MyMessageSet)(nil),
2295
- ExtensionType: (*Empty)(nil),
2296
- Field: 206,
2297
- Name: "testdata.x206",
2298
- Tag: "bytes,206,opt,name=x206",
2299
-}
2300
-
2301
-var E_X207 = &proto.ExtensionDesc{
2302
- ExtendedType: (*MyMessageSet)(nil),
2303
- ExtensionType: (*Empty)(nil),
2304
- Field: 207,
2305
- Name: "testdata.x207",
2306
- Tag: "bytes,207,opt,name=x207",
2307
-}
2308
-
2309
-var E_X208 = &proto.ExtensionDesc{
2310
- ExtendedType: (*MyMessageSet)(nil),
2311
- ExtensionType: (*Empty)(nil),
2312
- Field: 208,
2313
- Name: "testdata.x208",
2314
- Tag: "bytes,208,opt,name=x208",
2315
-}
2316
-
2317
-var E_X209 = &proto.ExtensionDesc{
2318
- ExtendedType: (*MyMessageSet)(nil),
2319
- ExtensionType: (*Empty)(nil),
2320
- Field: 209,
2321
- Name: "testdata.x209",
2322
- Tag: "bytes,209,opt,name=x209",
2323
-}
2324
-
2325
-var E_X210 = &proto.ExtensionDesc{
2326
- ExtendedType: (*MyMessageSet)(nil),
2327
- ExtensionType: (*Empty)(nil),
2328
- Field: 210,
2329
- Name: "testdata.x210",
2330
- Tag: "bytes,210,opt,name=x210",
2331
-}
2332
-
2333
-var E_X211 = &proto.ExtensionDesc{
2334
- ExtendedType: (*MyMessageSet)(nil),
2335
- ExtensionType: (*Empty)(nil),
2336
- Field: 211,
2337
- Name: "testdata.x211",
2338
- Tag: "bytes,211,opt,name=x211",
2339
-}
2340
-
2341
-var E_X212 = &proto.ExtensionDesc{
2342
- ExtendedType: (*MyMessageSet)(nil),
2343
- ExtensionType: (*Empty)(nil),
2344
- Field: 212,
2345
- Name: "testdata.x212",
2346
- Tag: "bytes,212,opt,name=x212",
2347
-}
2348
-
2349
-var E_X213 = &proto.ExtensionDesc{
2350
- ExtendedType: (*MyMessageSet)(nil),
2351
- ExtensionType: (*Empty)(nil),
2352
- Field: 213,
2353
- Name: "testdata.x213",
2354
- Tag: "bytes,213,opt,name=x213",
2355
-}
2356
-
2357
-var E_X214 = &proto.ExtensionDesc{
2358
- ExtendedType: (*MyMessageSet)(nil),
2359
- ExtensionType: (*Empty)(nil),
2360
- Field: 214,
2361
- Name: "testdata.x214",
2362
- Tag: "bytes,214,opt,name=x214",
2363
-}
2364
-
2365
-var E_X215 = &proto.ExtensionDesc{
2366
- ExtendedType: (*MyMessageSet)(nil),
2367
- ExtensionType: (*Empty)(nil),
2368
- Field: 215,
2369
- Name: "testdata.x215",
2370
- Tag: "bytes,215,opt,name=x215",
2371
-}
2372
-
2373
-var E_X216 = &proto.ExtensionDesc{
2374
- ExtendedType: (*MyMessageSet)(nil),
2375
- ExtensionType: (*Empty)(nil),
2376
- Field: 216,
2377
- Name: "testdata.x216",
2378
- Tag: "bytes,216,opt,name=x216",
2379
-}
2380
-
2381
-var E_X217 = &proto.ExtensionDesc{
2382
- ExtendedType: (*MyMessageSet)(nil),
2383
- ExtensionType: (*Empty)(nil),
2384
- Field: 217,
2385
- Name: "testdata.x217",
2386
- Tag: "bytes,217,opt,name=x217",
2387
-}
2388
-
2389
-var E_X218 = &proto.ExtensionDesc{
2390
- ExtendedType: (*MyMessageSet)(nil),
2391
- ExtensionType: (*Empty)(nil),
2392
- Field: 218,
2393
- Name: "testdata.x218",
2394
- Tag: "bytes,218,opt,name=x218",
2395
-}
2396
-
2397
-var E_X219 = &proto.ExtensionDesc{
2398
- ExtendedType: (*MyMessageSet)(nil),
2399
- ExtensionType: (*Empty)(nil),
2400
- Field: 219,
2401
- Name: "testdata.x219",
2402
- Tag: "bytes,219,opt,name=x219",
2403
-}
2404
-
2405
-var E_X220 = &proto.ExtensionDesc{
2406
- ExtendedType: (*MyMessageSet)(nil),
2407
- ExtensionType: (*Empty)(nil),
2408
- Field: 220,
2409
- Name: "testdata.x220",
2410
- Tag: "bytes,220,opt,name=x220",
2411
-}
2412
-
2413
-var E_X221 = &proto.ExtensionDesc{
2414
- ExtendedType: (*MyMessageSet)(nil),
2415
- ExtensionType: (*Empty)(nil),
2416
- Field: 221,
2417
- Name: "testdata.x221",
2418
- Tag: "bytes,221,opt,name=x221",
2419
-}
2420
-
2421
-var E_X222 = &proto.ExtensionDesc{
2422
- ExtendedType: (*MyMessageSet)(nil),
2423
- ExtensionType: (*Empty)(nil),
2424
- Field: 222,
2425
- Name: "testdata.x222",
2426
- Tag: "bytes,222,opt,name=x222",
2427
-}
2428
-
2429
-var E_X223 = &proto.ExtensionDesc{
2430
- ExtendedType: (*MyMessageSet)(nil),
2431
- ExtensionType: (*Empty)(nil),
2432
- Field: 223,
2433
- Name: "testdata.x223",
2434
- Tag: "bytes,223,opt,name=x223",
2435
-}
2436
-
2437
-var E_X224 = &proto.ExtensionDesc{
2438
- ExtendedType: (*MyMessageSet)(nil),
2439
- ExtensionType: (*Empty)(nil),
2440
- Field: 224,
2441
- Name: "testdata.x224",
2442
- Tag: "bytes,224,opt,name=x224",
2443
-}
2444
-
2445
-var E_X225 = &proto.ExtensionDesc{
2446
- ExtendedType: (*MyMessageSet)(nil),
2447
- ExtensionType: (*Empty)(nil),
2448
- Field: 225,
2449
- Name: "testdata.x225",
2450
- Tag: "bytes,225,opt,name=x225",
2451
-}
2452
-
2453
-var E_X226 = &proto.ExtensionDesc{
2454
- ExtendedType: (*MyMessageSet)(nil),
2455
- ExtensionType: (*Empty)(nil),
2456
- Field: 226,
2457
- Name: "testdata.x226",
2458
- Tag: "bytes,226,opt,name=x226",
2459
-}
2460
-
2461
-var E_X227 = &proto.ExtensionDesc{
2462
- ExtendedType: (*MyMessageSet)(nil),
2463
- ExtensionType: (*Empty)(nil),
2464
- Field: 227,
2465
- Name: "testdata.x227",
2466
- Tag: "bytes,227,opt,name=x227",
2467
-}
2468
-
2469
-var E_X228 = &proto.ExtensionDesc{
2470
- ExtendedType: (*MyMessageSet)(nil),
2471
- ExtensionType: (*Empty)(nil),
2472
- Field: 228,
2473
- Name: "testdata.x228",
2474
- Tag: "bytes,228,opt,name=x228",
2475
-}
2476
-
2477
-var E_X229 = &proto.ExtensionDesc{
2478
- ExtendedType: (*MyMessageSet)(nil),
2479
- ExtensionType: (*Empty)(nil),
2480
- Field: 229,
2481
- Name: "testdata.x229",
2482
- Tag: "bytes,229,opt,name=x229",
2483
-}
2484
-
2485
-var E_X230 = &proto.ExtensionDesc{
2486
- ExtendedType: (*MyMessageSet)(nil),
2487
- ExtensionType: (*Empty)(nil),
2488
- Field: 230,
2489
- Name: "testdata.x230",
2490
- Tag: "bytes,230,opt,name=x230",
2491
-}
2492
-
2493
-var E_X231 = &proto.ExtensionDesc{
2494
- ExtendedType: (*MyMessageSet)(nil),
2495
- ExtensionType: (*Empty)(nil),
2496
- Field: 231,
2497
- Name: "testdata.x231",
2498
- Tag: "bytes,231,opt,name=x231",
2499
-}
2500
-
2501
-var E_X232 = &proto.ExtensionDesc{
2502
- ExtendedType: (*MyMessageSet)(nil),
2503
- ExtensionType: (*Empty)(nil),
2504
- Field: 232,
2505
- Name: "testdata.x232",
2506
- Tag: "bytes,232,opt,name=x232",
2507
-}
2508
-
2509
-var E_X233 = &proto.ExtensionDesc{
2510
- ExtendedType: (*MyMessageSet)(nil),
2511
- ExtensionType: (*Empty)(nil),
2512
- Field: 233,
2513
- Name: "testdata.x233",
2514
- Tag: "bytes,233,opt,name=x233",
2515
-}
2516
-
2517
-var E_X234 = &proto.ExtensionDesc{
2518
- ExtendedType: (*MyMessageSet)(nil),
2519
- ExtensionType: (*Empty)(nil),
2520
- Field: 234,
2521
- Name: "testdata.x234",
2522
- Tag: "bytes,234,opt,name=x234",
2523
-}
2524
-
2525
-var E_X235 = &proto.ExtensionDesc{
2526
- ExtendedType: (*MyMessageSet)(nil),
2527
- ExtensionType: (*Empty)(nil),
2528
- Field: 235,
2529
- Name: "testdata.x235",
2530
- Tag: "bytes,235,opt,name=x235",
2531
-}
2532
-
2533
-var E_X236 = &proto.ExtensionDesc{
2534
- ExtendedType: (*MyMessageSet)(nil),
2535
- ExtensionType: (*Empty)(nil),
2536
- Field: 236,
2537
- Name: "testdata.x236",
2538
- Tag: "bytes,236,opt,name=x236",
2539
-}
2540
-
2541
-var E_X237 = &proto.ExtensionDesc{
2542
- ExtendedType: (*MyMessageSet)(nil),
2543
- ExtensionType: (*Empty)(nil),
2544
- Field: 237,
2545
- Name: "testdata.x237",
2546
- Tag: "bytes,237,opt,name=x237",
2547
-}
2548
-
2549
-var E_X238 = &proto.ExtensionDesc{
2550
- ExtendedType: (*MyMessageSet)(nil),
2551
- ExtensionType: (*Empty)(nil),
2552
- Field: 238,
2553
- Name: "testdata.x238",
2554
- Tag: "bytes,238,opt,name=x238",
2555
-}
2556
-
2557
-var E_X239 = &proto.ExtensionDesc{
2558
- ExtendedType: (*MyMessageSet)(nil),
2559
- ExtensionType: (*Empty)(nil),
2560
- Field: 239,
2561
- Name: "testdata.x239",
2562
- Tag: "bytes,239,opt,name=x239",
2563
-}
2564
-
2565
-var E_X240 = &proto.ExtensionDesc{
2566
- ExtendedType: (*MyMessageSet)(nil),
2567
- ExtensionType: (*Empty)(nil),
2568
- Field: 240,
2569
- Name: "testdata.x240",
2570
- Tag: "bytes,240,opt,name=x240",
2571
-}
2572
-
2573
-var E_X241 = &proto.ExtensionDesc{
2574
- ExtendedType: (*MyMessageSet)(nil),
2575
- ExtensionType: (*Empty)(nil),
2576
- Field: 241,
2577
- Name: "testdata.x241",
2578
- Tag: "bytes,241,opt,name=x241",
2579
-}
2580
-
2581
-var E_X242 = &proto.ExtensionDesc{
2582
- ExtendedType: (*MyMessageSet)(nil),
2583
- ExtensionType: (*Empty)(nil),
2584
- Field: 242,
2585
- Name: "testdata.x242",
2586
- Tag: "bytes,242,opt,name=x242",
2587
-}
2588
-
2589
-var E_X243 = &proto.ExtensionDesc{
2590
- ExtendedType: (*MyMessageSet)(nil),
2591
- ExtensionType: (*Empty)(nil),
2592
- Field: 243,
2593
- Name: "testdata.x243",
2594
- Tag: "bytes,243,opt,name=x243",
2595
-}
2596
-
2597
-var E_X244 = &proto.ExtensionDesc{
2598
- ExtendedType: (*MyMessageSet)(nil),
2599
- ExtensionType: (*Empty)(nil),
2600
- Field: 244,
2601
- Name: "testdata.x244",
2602
- Tag: "bytes,244,opt,name=x244",
2603
-}
2604
-
2605
-var E_X245 = &proto.ExtensionDesc{
2606
- ExtendedType: (*MyMessageSet)(nil),
2607
- ExtensionType: (*Empty)(nil),
2608
- Field: 245,
2609
- Name: "testdata.x245",
2610
- Tag: "bytes,245,opt,name=x245",
2611
-}
2612
-
2613
-var E_X246 = &proto.ExtensionDesc{
2614
- ExtendedType: (*MyMessageSet)(nil),
2615
- ExtensionType: (*Empty)(nil),
2616
- Field: 246,
2617
- Name: "testdata.x246",
2618
- Tag: "bytes,246,opt,name=x246",
2619
-}
2620
-
2621
-var E_X247 = &proto.ExtensionDesc{
2622
- ExtendedType: (*MyMessageSet)(nil),
2623
- ExtensionType: (*Empty)(nil),
2624
- Field: 247,
2625
- Name: "testdata.x247",
2626
- Tag: "bytes,247,opt,name=x247",
2627
-}
2628
-
2629
-var E_X248 = &proto.ExtensionDesc{
2630
- ExtendedType: (*MyMessageSet)(nil),
2631
- ExtensionType: (*Empty)(nil),
2632
- Field: 248,
2633
- Name: "testdata.x248",
2634
- Tag: "bytes,248,opt,name=x248",
2635
-}
2636
-
2637
-var E_X249 = &proto.ExtensionDesc{
2638
- ExtendedType: (*MyMessageSet)(nil),
2639
- ExtensionType: (*Empty)(nil),
2640
- Field: 249,
2641
- Name: "testdata.x249",
2642
- Tag: "bytes,249,opt,name=x249",
2643
-}
2644
-
2645
-var E_X250 = &proto.ExtensionDesc{
2646
- ExtendedType: (*MyMessageSet)(nil),
2647
- ExtensionType: (*Empty)(nil),
2648
- Field: 250,
2649
- Name: "testdata.x250",
2650
- Tag: "bytes,250,opt,name=x250",
2651
-}
2652
-
2653
-func init() {
2654
- proto.RegisterEnum("testdata.FOO", FOO_name, FOO_value)
2655
- proto.RegisterEnum("testdata.GoTest_KIND", GoTest_KIND_name, GoTest_KIND_value)
2656
- proto.RegisterEnum("testdata.MyMessage_Color", MyMessage_Color_name, MyMessage_Color_value)
2657
- proto.RegisterEnum("testdata.DefaultsMessage_DefaultsEnum", DefaultsMessage_DefaultsEnum_name, DefaultsMessage_DefaultsEnum_value)
2658
- proto.RegisterEnum("testdata.Defaults_Color", Defaults_Color_name, Defaults_Color_value)
2659
- proto.RegisterEnum("testdata.RepeatedEnum_Color", RepeatedEnum_Color_name, RepeatedEnum_Color_value)
2660
- proto.RegisterExtension(E_Ext_More)
2661
- proto.RegisterExtension(E_Ext_Text)
2662
- proto.RegisterExtension(E_Ext_Number)
2663
- proto.RegisterExtension(E_Greeting)
2664
- proto.RegisterExtension(E_NoDefaultDouble)
2665
- proto.RegisterExtension(E_NoDefaultFloat)
2666
- proto.RegisterExtension(E_NoDefaultInt32)
2667
- proto.RegisterExtension(E_NoDefaultInt64)
2668
- proto.RegisterExtension(E_NoDefaultUint32)
2669
- proto.RegisterExtension(E_NoDefaultUint64)
2670
- proto.RegisterExtension(E_NoDefaultSint32)
2671
- proto.RegisterExtension(E_NoDefaultSint64)
2672
- proto.RegisterExtension(E_NoDefaultFixed32)
2673
- proto.RegisterExtension(E_NoDefaultFixed64)
2674
- proto.RegisterExtension(E_NoDefaultSfixed32)
2675
- proto.RegisterExtension(E_NoDefaultSfixed64)
2676
- proto.RegisterExtension(E_NoDefaultBool)
2677
- proto.RegisterExtension(E_NoDefaultString)
2678
- proto.RegisterExtension(E_NoDefaultBytes)
2679
- proto.RegisterExtension(E_NoDefaultEnum)
2680
- proto.RegisterExtension(E_DefaultDouble)
2681
- proto.RegisterExtension(E_DefaultFloat)
2682
- proto.RegisterExtension(E_DefaultInt32)
2683
- proto.RegisterExtension(E_DefaultInt64)
2684
- proto.RegisterExtension(E_DefaultUint32)
2685
- proto.RegisterExtension(E_DefaultUint64)
2686
- proto.RegisterExtension(E_DefaultSint32)
2687
- proto.RegisterExtension(E_DefaultSint64)
2688
- proto.RegisterExtension(E_DefaultFixed32)
2689
- proto.RegisterExtension(E_DefaultFixed64)
2690
- proto.RegisterExtension(E_DefaultSfixed32)
2691
- proto.RegisterExtension(E_DefaultSfixed64)
2692
- proto.RegisterExtension(E_DefaultBool)
2693
- proto.RegisterExtension(E_DefaultString)
2694
- proto.RegisterExtension(E_DefaultBytes)
2695
- proto.RegisterExtension(E_DefaultEnum)
2696
- proto.RegisterExtension(E_X201)
2697
- proto.RegisterExtension(E_X202)
2698
- proto.RegisterExtension(E_X203)
2699
- proto.RegisterExtension(E_X204)
2700
- proto.RegisterExtension(E_X205)
2701
- proto.RegisterExtension(E_X206)
2702
- proto.RegisterExtension(E_X207)
2703
- proto.RegisterExtension(E_X208)
2704
- proto.RegisterExtension(E_X209)
2705
- proto.RegisterExtension(E_X210)
2706
- proto.RegisterExtension(E_X211)
2707
- proto.RegisterExtension(E_X212)
2708
- proto.RegisterExtension(E_X213)
2709
- proto.RegisterExtension(E_X214)
2710
- proto.RegisterExtension(E_X215)
2711
- proto.RegisterExtension(E_X216)
2712
- proto.RegisterExtension(E_X217)
2713
- proto.RegisterExtension(E_X218)
2714
- proto.RegisterExtension(E_X219)
2715
- proto.RegisterExtension(E_X220)
2716
- proto.RegisterExtension(E_X221)
2717
- proto.RegisterExtension(E_X222)
2718
- proto.RegisterExtension(E_X223)
2719
- proto.RegisterExtension(E_X224)
2720
- proto.RegisterExtension(E_X225)
2721
- proto.RegisterExtension(E_X226)
2722
- proto.RegisterExtension(E_X227)
2723
- proto.RegisterExtension(E_X228)
2724
- proto.RegisterExtension(E_X229)
2725
- proto.RegisterExtension(E_X230)
2726
- proto.RegisterExtension(E_X231)
2727
- proto.RegisterExtension(E_X232)
2728
- proto.RegisterExtension(E_X233)
2729
- proto.RegisterExtension(E_X234)
2730
- proto.RegisterExtension(E_X235)
2731
- proto.RegisterExtension(E_X236)
2732
- proto.RegisterExtension(E_X237)
2733
- proto.RegisterExtension(E_X238)
2734
- proto.RegisterExtension(E_X239)
2735
- proto.RegisterExtension(E_X240)
2736
- proto.RegisterExtension(E_X241)
2737
- proto.RegisterExtension(E_X242)
2738
- proto.RegisterExtension(E_X243)
2739
- proto.RegisterExtension(E_X244)
2740
- proto.RegisterExtension(E_X245)
2741
- proto.RegisterExtension(E_X246)
2742
- proto.RegisterExtension(E_X247)
2743
- proto.RegisterExtension(E_X248)
2744
- proto.RegisterExtension(E_X249)
2745
- proto.RegisterExtension(E_X250)
2746
-}
Godeps/_workspace/src/github.com/golang/protobuf/proto/testdata/test.proto
deleted
-480
@@ -1,480 +0,0 @@
1
-// Go support for Protocol Buffers - Google's data interchange format
2
-//
3
-// Copyright 2010 The Go Authors. All rights reserved.
4
-// https://github.com/golang/protobuf
5
-//
6
-// Redistribution and use in source and binary forms, with or without
7
-// modification, are permitted provided that the following conditions are
8
-// met:
9
-//
10
-// * Redistributions of source code must retain the above copyright
11
-// notice, this list of conditions and the following disclaimer.
12
-// * Redistributions in binary form must reproduce the above
13
-// copyright notice, this list of conditions and the following disclaimer
14
-// in the documentation and/or other materials provided with the
15
-// distribution.
16
-// * Neither the name of Google Inc. nor the names of its
17
-// contributors may be used to endorse or promote products derived from
18
-// this software without specific prior written permission.
19
-//
20
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
-// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
-// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23
-// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24
-// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25
-// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26
-// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27
-// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28
-// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29
-// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
-// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
-
32
-// A feature-rich test file for the protocol compiler and libraries.
33
-
34
-syntax = "proto2";
35
-
36
-package testdata;
37
-
38
-enum FOO { FOO1 = 1; };
39
-
40
-message GoEnum {
41
- required FOO foo = 1;
42
-}
43
-
44
-message GoTestField {
45
- required string Label = 1;
46
- required string Type = 2;
47
-}
48
-
49
-message GoTest {
50
- // An enum, for completeness.
51
- enum KIND {
52
- VOID = 0;
53
-
54
- // Basic types
55
- BOOL = 1;
56
- BYTES = 2;
57
- FINGERPRINT = 3;
58
- FLOAT = 4;
59
- INT = 5;
60
- STRING = 6;
61
- TIME = 7;
62
-
63
- // Groupings
64
- TUPLE = 8;
65
- ARRAY = 9;
66
- MAP = 10;
67
-
68
- // Table types
69
- TABLE = 11;
70
-
71
- // Functions
72
- FUNCTION = 12; // last tag
73
- };
74
-
75
- // Some typical parameters
76
- required KIND Kind = 1;
77
- optional string Table = 2;
78
- optional int32 Param = 3;
79
-
80
- // Required, repeated and optional foreign fields.
81
- required GoTestField RequiredField = 4;
82
- repeated GoTestField RepeatedField = 5;
83
- optional GoTestField OptionalField = 6;
84
-
85
- // Required fields of all basic types
86
- required bool F_Bool_required = 10;
87
- required int32 F_Int32_required = 11;
88
- required int64 F_Int64_required = 12;
89
- required fixed32 F_Fixed32_required = 13;
90
- required fixed64 F_Fixed64_required = 14;
91
- required uint32 F_Uint32_required = 15;
92
- required uint64 F_Uint64_required = 16;
93
- required float F_Float_required = 17;
94
- required double F_Double_required = 18;
95
- required string F_String_required = 19;
96
- required bytes F_Bytes_required = 101;
97
- required sint32 F_Sint32_required = 102;
98
- required sint64 F_Sint64_required = 103;
99
-
100
- // Repeated fields of all basic types
101
- repeated bool F_Bool_repeated = 20;
102
- repeated int32 F_Int32_repeated = 21;
103
- repeated int64 F_Int64_repeated = 22;
104
- repeated fixed32 F_Fixed32_repeated = 23;
105
- repeated fixed64 F_Fixed64_repeated = 24;
106
- repeated uint32 F_Uint32_repeated = 25;
107
- repeated uint64 F_Uint64_repeated = 26;
108
- repeated float F_Float_repeated = 27;
109
- repeated double F_Double_repeated = 28;
110
- repeated string F_String_repeated = 29;
111
- repeated bytes F_Bytes_repeated = 201;
112
- repeated sint32 F_Sint32_repeated = 202;
113
- repeated sint64 F_Sint64_repeated = 203;
114
-
115
- // Optional fields of all basic types
116
- optional bool F_Bool_optional = 30;
117
- optional int32 F_Int32_optional = 31;
118
- optional int64 F_Int64_optional = 32;
119
- optional fixed32 F_Fixed32_optional = 33;
120
- optional fixed64 F_Fixed64_optional = 34;
121
- optional uint32 F_Uint32_optional = 35;
122
- optional uint64 F_Uint64_optional = 36;
123
- optional float F_Float_optional = 37;
124
- optional double F_Double_optional = 38;
125
- optional string F_String_optional = 39;
126
- optional bytes F_Bytes_optional = 301;
127
- optional sint32 F_Sint32_optional = 302;
128
- optional sint64 F_Sint64_optional = 303;
129
-
130
- // Default-valued fields of all basic types
131
- optional bool F_Bool_defaulted = 40 [default=true];
132
- optional int32 F_Int32_defaulted = 41 [default=32];
133
- optional int64 F_Int64_defaulted = 42 [default=64];
134
- optional fixed32 F_Fixed32_defaulted = 43 [default=320];
135
- optional fixed64 F_Fixed64_defaulted = 44 [default=640];
136
- optional uint32 F_Uint32_defaulted = 45 [default=3200];
137
- optional uint64 F_Uint64_defaulted = 46 [default=6400];
138
- optional float F_Float_defaulted = 47 [default=314159.];
139
- optional double F_Double_defaulted = 48 [default=271828.];
140
- optional string F_String_defaulted = 49 [default="hello, \"world!\"\n"];
141
- optional bytes F_Bytes_defaulted = 401 [default="Bignose"];
142
- optional sint32 F_Sint32_defaulted = 402 [default = -32];
143
- optional sint64 F_Sint64_defaulted = 403 [default = -64];
144
-
145
- // Packed repeated fields (no string or bytes).
146
- repeated bool F_Bool_repeated_packed = 50 [packed=true];
147
- repeated int32 F_Int32_repeated_packed = 51 [packed=true];
148
- repeated int64 F_Int64_repeated_packed = 52 [packed=true];
149
- repeated fixed32 F_Fixed32_repeated_packed = 53 [packed=true];
150
- repeated fixed64 F_Fixed64_repeated_packed = 54 [packed=true];
151
- repeated uint32 F_Uint32_repeated_packed = 55 [packed=true];
152
- repeated uint64 F_Uint64_repeated_packed = 56 [packed=true];
153
- repeated float F_Float_repeated_packed = 57 [packed=true];
154
- repeated double F_Double_repeated_packed = 58 [packed=true];
155
- repeated sint32 F_Sint32_repeated_packed = 502 [packed=true];
156
- repeated sint64 F_Sint64_repeated_packed = 503 [packed=true];
157
-
158
- // Required, repeated, and optional groups.
159
- required group RequiredGroup = 70 {
160
- required string RequiredField = 71;
161
- };
162
-
163
- repeated group RepeatedGroup = 80 {
164
- required string RequiredField = 81;
165
- };
166
-
167
- optional group OptionalGroup = 90 {
168
- required string RequiredField = 91;
169
- };
170
-}
171
-
172
-// For testing skipping of unrecognized fields.
173
-// Numbers are all big, larger than tag numbers in GoTestField,
174
-// the message used in the corresponding test.
175
-message GoSkipTest {
176
- required int32 skip_int32 = 11;
177
- required fixed32 skip_fixed32 = 12;
178
- required fixed64 skip_fixed64 = 13;
179
- required string skip_string = 14;
180
- required group SkipGroup = 15 {
181
- required int32 group_int32 = 16;
182
- required string group_string = 17;
183
- }
184
-}
185
-
186
-// For testing packed/non-packed decoder switching.
187
-// A serialized instance of one should be deserializable as the other.
188
-message NonPackedTest {
189
- repeated int32 a = 1;
190
-}
191
-
192
-message PackedTest {
193
- repeated int32 b = 1 [packed=true];
194
-}
195
-
196
-message MaxTag {
197
- // Maximum possible tag number.
198
- optional string last_field = 536870911;
199
-}
200
-
201
-message OldMessage {
202
- message Nested {
203
- optional string name = 1;
204
- }
205
- optional Nested nested = 1;
206
-
207
- optional int32 num = 2;
208
-}
209
-
210
-// NewMessage is wire compatible with OldMessage;
211
-// imagine it as a future version.
212
-message NewMessage {
213
- message Nested {
214
- optional string name = 1;
215
- optional string food_group = 2;
216
- }
217
- optional Nested nested = 1;
218
-
219
- // This is an int32 in OldMessage.
220
- optional int64 num = 2;
221
-}
222
-
223
-// Smaller tests for ASCII formatting.
224
-
225
-message InnerMessage {
226
- required string host = 1;
227
- optional int32 port = 2 [default=4000];
228
- optional bool connected = 3;
229
-}
230
-
231
-message OtherMessage {
232
- optional int64 key = 1;
233
- optional bytes value = 2;
234
- optional float weight = 3;
235
- optional InnerMessage inner = 4;
236
-}
237
-
238
-message MyMessage {
239
- required int32 count = 1;
240
- optional string name = 2;
241
- optional string quote = 3;
242
- repeated string pet = 4;
243
- optional InnerMessage inner = 5;
244
- repeated OtherMessage others = 6;
245
- repeated InnerMessage rep_inner = 12;
246
-
247
- enum Color {
248
- RED = 0;
249
- GREEN = 1;
250
- BLUE = 2;
251
- };
252
- optional Color bikeshed = 7;
253
-
254
- optional group SomeGroup = 8 {
255
- optional int32 group_field = 9;
256
- }
257
-
258
- // This field becomes [][]byte in the generated code.
259
- repeated bytes rep_bytes = 10;
260
-
261
- optional double bigfloat = 11;
262
-
263
- extensions 100 to max;
264
-}
265
-
266
-message Ext {
267
- extend MyMessage {
268
- optional Ext more = 103;
269
- optional string text = 104;
270
- optional int32 number = 105;
271
- }
272
-
273
- optional string data = 1;
274
-}
275
-
276
-extend MyMessage {
277
- repeated string greeting = 106;
278
-}
279
-
280
-message DefaultsMessage {
281
- enum DefaultsEnum {
282
- ZERO = 0;
283
- ONE = 1;
284
- TWO = 2;
285
- };
286
- extensions 100 to max;
287
-}
288
-
289
-extend DefaultsMessage {
290
- optional double no_default_double = 101;
291
- optional float no_default_float = 102;
292
- optional int32 no_default_int32 = 103;
293
- optional int64 no_default_int64 = 104;
294
- optional uint32 no_default_uint32 = 105;
295
- optional uint64 no_default_uint64 = 106;
296
- optional sint32 no_default_sint32 = 107;
297
- optional sint64 no_default_sint64 = 108;
298
- optional fixed32 no_default_fixed32 = 109;
299
- optional fixed64 no_default_fixed64 = 110;
300
- optional sfixed32 no_default_sfixed32 = 111;
301
- optional sfixed64 no_default_sfixed64 = 112;
302
- optional bool no_default_bool = 113;
303
- optional string no_default_string = 114;
304
- optional bytes no_default_bytes = 115;
305
- optional DefaultsMessage.DefaultsEnum no_default_enum = 116;
306
-
307
- optional double default_double = 201 [default = 3.1415];
308
- optional float default_float = 202 [default = 3.14];
309
- optional int32 default_int32 = 203 [default = 42];
310
- optional int64 default_int64 = 204 [default = 43];
311
- optional uint32 default_uint32 = 205 [default = 44];
312
- optional uint64 default_uint64 = 206 [default = 45];
313
- optional sint32 default_sint32 = 207 [default = 46];
314
- optional sint64 default_sint64 = 208 [default = 47];
315
- optional fixed32 default_fixed32 = 209 [default = 48];
316
- optional fixed64 default_fixed64 = 210 [default = 49];
317
- optional sfixed32 default_sfixed32 = 211 [default = 50];
318
- optional sfixed64 default_sfixed64 = 212 [default = 51];
319
- optional bool default_bool = 213 [default = true];
320
- optional string default_string = 214 [default = "Hello, string"];
321
- optional bytes default_bytes = 215 [default = "Hello, bytes"];
322
- optional DefaultsMessage.DefaultsEnum default_enum = 216 [default = ONE];
323
-}
324
-
325
-message MyMessageSet {
326
- option message_set_wire_format = true;
327
- extensions 100 to max;
328
-}
329
-
330
-message Empty {
331
-}
332
-
333
-extend MyMessageSet {
334
- optional Empty x201 = 201;
335
- optional Empty x202 = 202;
336
- optional Empty x203 = 203;
337
- optional Empty x204 = 204;
338
- optional Empty x205 = 205;
339
- optional Empty x206 = 206;
340
- optional Empty x207 = 207;
341
- optional Empty x208 = 208;
342
- optional Empty x209 = 209;
343
- optional Empty x210 = 210;
344
- optional Empty x211 = 211;
345
- optional Empty x212 = 212;
346
- optional Empty x213 = 213;
347
- optional Empty x214 = 214;
348
- optional Empty x215 = 215;
349
- optional Empty x216 = 216;
350
- optional Empty x217 = 217;
351
- optional Empty x218 = 218;
352
- optional Empty x219 = 219;
353
- optional Empty x220 = 220;
354
- optional Empty x221 = 221;
355
- optional Empty x222 = 222;
356
- optional Empty x223 = 223;
357
- optional Empty x224 = 224;
358
- optional Empty x225 = 225;
359
- optional Empty x226 = 226;
360
- optional Empty x227 = 227;
361
- optional Empty x228 = 228;
362
- optional Empty x229 = 229;
363
- optional Empty x230 = 230;
364
- optional Empty x231 = 231;
365
- optional Empty x232 = 232;
366
- optional Empty x233 = 233;
367
- optional Empty x234 = 234;
368
- optional Empty x235 = 235;
369
- optional Empty x236 = 236;
370
- optional Empty x237 = 237;
371
- optional Empty x238 = 238;
372
- optional Empty x239 = 239;
373
- optional Empty x240 = 240;
374
- optional Empty x241 = 241;
375
- optional Empty x242 = 242;
376
- optional Empty x243 = 243;
377
- optional Empty x244 = 244;
378
- optional Empty x245 = 245;
379
- optional Empty x246 = 246;
380
- optional Empty x247 = 247;
381
- optional Empty x248 = 248;
382
- optional Empty x249 = 249;
383
- optional Empty x250 = 250;
384
-}
385
-
386
-message MessageList {
387
- repeated group Message = 1 {
388
- required string name = 2;
389
- required int32 count = 3;
390
- }
391
-}
392
-
393
-message Strings {
394
- optional string string_field = 1;
395
- optional bytes bytes_field = 2;
396
-}
397
-
398
-message Defaults {
399
- enum Color {
400
- RED = 0;
401
- GREEN = 1;
402
- BLUE = 2;
403
- }
404
-
405
- // Default-valued fields of all basic types.
406
- // Same as GoTest, but copied here to make testing easier.
407
- optional bool F_Bool = 1 [default=true];
408
- optional int32 F_Int32 = 2 [default=32];
409
- optional int64 F_Int64 = 3 [default=64];
410
- optional fixed32 F_Fixed32 = 4 [default=320];
411
- optional fixed64 F_Fixed64 = 5 [default=640];
412
- optional uint32 F_Uint32 = 6 [default=3200];
413
- optional uint64 F_Uint64 = 7 [default=6400];
414
- optional float F_Float = 8 [default=314159.];
415
- optional double F_Double = 9 [default=271828.];
416
- optional string F_String = 10 [default="hello, \"world!\"\n"];
417
- optional bytes F_Bytes = 11 [default="Bignose"];
418
- optional sint32 F_Sint32 = 12 [default=-32];
419
- optional sint64 F_Sint64 = 13 [default=-64];
420
- optional Color F_Enum = 14 [default=GREEN];
421
-
422
- // More fields with crazy defaults.
423
- optional float F_Pinf = 15 [default=inf];
424
- optional float F_Ninf = 16 [default=-inf];
425
- optional float F_Nan = 17 [default=nan];
426
-
427
- // Sub-message.
428
- optional SubDefaults sub = 18;
429
-
430
- // Redundant but explicit defaults.
431
- optional string str_zero = 19 [default=""];
432
-}
433
-
434
-message SubDefaults {
435
- optional int64 n = 1 [default=7];
436
-}
437
-
438
-message RepeatedEnum {
439
- enum Color {
440
- RED = 1;
441
- }
442
- repeated Color color = 1;
443
-}
444
-
445
-message MoreRepeated {
446
- repeated bool bools = 1;
447
- repeated bool bools_packed = 2 [packed=true];
448
- repeated int32 ints = 3;
449
- repeated int32 ints_packed = 4 [packed=true];
450
- repeated int64 int64s_packed = 7 [packed=true];
451
- repeated string strings = 5;
452
- repeated fixed32 fixeds = 6;
453
-}
454
-
455
-// GroupOld and GroupNew have the same wire format.
456
-// GroupNew has a new field inside a group.
457
-
458
-message GroupOld {
459
- optional group G = 101 {
460
- optional int32 x = 2;
461
- }
462
-}
463
-
464
-message GroupNew {
465
- optional group G = 101 {
466
- optional int32 x = 2;
467
- optional int32 y = 3;
468
- }
469
-}
470
-
471
-message FloatingPoint {
472
- required double f = 1;
473
-}
474
-
475
-message MessageWithMap {
476
- map<int32, string> name_mapping = 1;
477
- map<sint64, FloatingPoint> msg_mapping = 2;
478
- map<bool, bytes> byte_mapping = 3;
479
- map<string, string> str_to_str = 4;
480
-}
Godeps/_workspace/src/github.com/golang/protobuf/proto/text.go
deleted
-788
@@ -1,788 +0,0 @@
1
-// Go support for Protocol Buffers - Google's data interchange format
2
-//
3
-// Copyright 2010 The Go Authors. All rights reserved.
4
-// https://github.com/golang/protobuf
5
-//
6
-// Redistribution and use in source and binary forms, with or without
7
-// modification, are permitted provided that the following conditions are
8
-// met:
9
-//
10
-// * Redistributions of source code must retain the above copyright
11
-// notice, this list of conditions and the following disclaimer.
12
-// * Redistributions in binary form must reproduce the above
13
-// copyright notice, this list of conditions and the following disclaimer
14
-// in the documentation and/or other materials provided with the
15
-// distribution.
16
-// * Neither the name of Google Inc. nor the names of its
17
-// contributors may be used to endorse or promote products derived from
18
-// this software without specific prior written permission.
19
-//
20
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
-// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
-// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23
-// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24
-// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25
-// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26
-// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27
-// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28
-// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29
-// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
-// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
-
32
-package proto
33
-
34
-// Functions for writing the text protocol buffer format.
35
-
36
-import (
37
- "bufio"
38
- "bytes"
39
- "encoding"
40
- "fmt"
41
- "io"
42
- "log"
43
- "math"
44
- "reflect"
45
- "sort"
46
- "strings"
47
-)
48
-
49
-var (
50
- newline = []byte("\n")
51
- spaces = []byte(" ")
52
- gtNewline = []byte(">\n")
53
- endBraceNewline = []byte("}\n")
54
- backslashN = []byte{'\\', 'n'}
55
- backslashR = []byte{'\\', 'r'}
56
- backslashT = []byte{'\\', 't'}
57
- backslashDQ = []byte{'\\', '"'}
58
- backslashBS = []byte{'\\', '\\'}
59
- posInf = []byte("inf")
60
- negInf = []byte("-inf")
61
- nan = []byte("nan")
62
-)
63
-
64
-type writer interface {
65
- io.Writer
66
- WriteByte(byte) error
67
-}
68
-
69
-// textWriter is an io.Writer that tracks its indentation level.
70
-type textWriter struct {
71
- ind int
72
- complete bool // if the current position is a complete line
73
- compact bool // whether to write out as a one-liner
74
- w writer
75
-}
76
-
77
-func (w *textWriter) WriteString(s string) (n int, err error) {
78
- if !strings.Contains(s, "\n") {
79
- if !w.compact && w.complete {
80
- w.writeIndent()
81
- }
82
- w.complete = false
83
- return io.WriteString(w.w, s)
84
- }
85
- // WriteString is typically called without newlines, so this
86
- // codepath and its copy are rare. We copy to avoid
87
- // duplicating all of Write's logic here.
88
- return w.Write([]byte(s))
89
-}
90
-
91
-func (w *textWriter) Write(p []byte) (n int, err error) {
92
- newlines := bytes.Count(p, newline)
93
- if newlines == 0 {
94
- if !w.compact && w.complete {
95
- w.writeIndent()
96
- }
97
- n, err = w.w.Write(p)
98
- w.complete = false
99
- return n, err
100
- }
101
-
102
- frags := bytes.SplitN(p, newline, newlines+1)
103
- if w.compact {
104
- for i, frag := range frags {
105
- if i > 0 {
106
- if err := w.w.WriteByte(' '); err != nil {
107
- return n, err
108
- }
109
- n++
110
- }
111
- nn, err := w.w.Write(frag)
112
- n += nn
113
- if err != nil {
114
- return n, err
115
- }
116
- }
117
- return n, nil
118
- }
119
-
120
- for i, frag := range frags {
121
- if w.complete {
122
- w.writeIndent()
123
- }
124
- nn, err := w.w.Write(frag)
125
- n += nn
126
- if err != nil {
127
- return n, err
128
- }
129
- if i+1 < len(frags) {
130
- if err := w.w.WriteByte('\n'); err != nil {
131
- return n, err
132
- }
133
- n++
134
- }
135
- }
136
- w.complete = len(frags[len(frags)-1]) == 0
137
- return n, nil
138
-}
139
-
140
-func (w *textWriter) WriteByte(c byte) error {
141
- if w.compact && c == '\n' {
142
- c = ' '
143
- }
144
- if !w.compact && w.complete {
145
- w.writeIndent()
146
- }
147
- err := w.w.WriteByte(c)
148
- w.complete = c == '\n'
149
- return err
150
-}
151
-
152
-func (w *textWriter) indent() { w.ind++ }
153
-
154
-func (w *textWriter) unindent() {
155
- if w.ind == 0 {
156
- log.Printf("proto: textWriter unindented too far")
157
- return
158
- }
159
- w.ind--
160
-}
161
-
162
-func writeName(w *textWriter, props *Properties) error {
163
- if _, err := w.WriteString(props.OrigName); err != nil {
164
- return err
165
- }
166
- if props.Wire != "group" {
167
- return w.WriteByte(':')
168
- }
169
- return nil
170
-}
171
-
172
-var (
173
- messageSetType = reflect.TypeOf((*MessageSet)(nil)).Elem()
174
-)
175
-
176
-// raw is the interface satisfied by RawMessage.
177
-type raw interface {
178
- Bytes() []byte
179
-}
180
-
181
-func writeStruct(w *textWriter, sv reflect.Value) error {
182
- if sv.Type() == messageSetType {
183
- return writeMessageSet(w, sv.Addr().Interface().(*MessageSet))
184
- }
185
-
186
- st := sv.Type()
187
- sprops := GetProperties(st)
188
- for i := 0; i < sv.NumField(); i++ {
189
- fv := sv.Field(i)
190
- props := sprops.Prop[i]
191
- name := st.Field(i).Name
192
-
193
- if strings.HasPrefix(name, "XXX_") {
194
- // There are two XXX_ fields:
195
- // XXX_unrecognized []byte
196
- // XXX_extensions map[int32]proto.Extension
197
- // The first is handled here;
198
- // the second is handled at the bottom of this function.
199
- if name == "XXX_unrecognized" && !fv.IsNil() {
200
- if err := writeUnknownStruct(w, fv.Interface().([]byte)); err != nil {
201
- return err
202
- }
203
- }
204
- continue
205
- }
206
- if fv.Kind() == reflect.Ptr && fv.IsNil() {
207
- // Field not filled in. This could be an optional field or
208
- // a required field that wasn't filled in. Either way, there
209
- // isn't anything we can show for it.
210
- continue
211
- }
212
- if fv.Kind() == reflect.Slice && fv.IsNil() {
213
- // Repeated field that is empty, or a bytes field that is unused.
214
- continue
215
- }
216
-
217
- if props.Repeated && fv.Kind() == reflect.Slice {
218
- // Repeated field.
219
- for j := 0; j < fv.Len(); j++ {
220
- if err := writeName(w, props); err != nil {
221
- return err
222
- }
223
- if !w.compact {
224
- if err := w.WriteByte(' '); err != nil {
225
- return err
226
- }
227
- }
228
- v := fv.Index(j)
229
- if v.Kind() == reflect.Ptr && v.IsNil() {
230
- // A nil message in a repeated field is not valid,
231
- // but we can handle that more gracefully than panicking.
232
- if _, err := w.Write([]byte("<nil>\n")); err != nil {
233
- return err
234
- }
235
- continue
236
- }
237
- if err := writeAny(w, v, props); err != nil {
238
- return err
239
- }
240
- if err := w.WriteByte('\n'); err != nil {
241
- return err
242
- }
243
- }
244
- continue
245
- }
246
- if fv.Kind() == reflect.Map {
247
- // Map fields are rendered as a repeated struct with key/value fields.
248
- keys := fv.MapKeys() // TODO: should we sort these for deterministic output?
249
- sort.Sort(mapKeys(keys))
250
- for _, key := range keys {
251
- val := fv.MapIndex(key)
252
- if err := writeName(w, props); err != nil {
253
- return err
254
- }
255
- if !w.compact {
256
- if err := w.WriteByte(' '); err != nil {
257
- return err
258
- }
259
- }
260
- // open struct
261
- if err := w.WriteByte('<'); err != nil {
262
- return err
263
- }
264
- if !w.compact {
265
- if err := w.WriteByte('\n'); err != nil {
266
- return err
267
- }
268
- }
269
- w.indent()
270
- // key
271
- if _, err := w.WriteString("key:"); err != nil {
272
- return err
273
- }
274
- if !w.compact {
275
- if err := w.WriteByte(' '); err != nil {
276
- return err
277
- }
278
- }
279
- if err := writeAny(w, key, props.mkeyprop); err != nil {
280
- return err
281
- }
282
- if err := w.WriteByte('\n'); err != nil {
283
- return err
284
- }
285
- // nil values aren't legal, but we can avoid panicking because of them.
286
- if val.Kind() != reflect.Ptr || !val.IsNil() {
287
- // value
288
- if _, err := w.WriteString("value:"); err != nil {
289
- return err
290
- }
291
- if !w.compact {
292
- if err := w.WriteByte(' '); err != nil {
293
- return err
294
- }
295
- }
296
- if err := writeAny(w, val, props.mvalprop); err != nil {
297
- return err
298
- }
299
- if err := w.WriteByte('\n'); err != nil {
300
- return err
301
- }
302
- }
303
- // close struct
304
- w.unindent()
305
- if err := w.WriteByte('>'); err != nil {
306
- return err
307
- }
308
- if err := w.WriteByte('\n'); err != nil {
309
- return err
310
- }
311
- }
312
- continue
313
- }
314
- if props.proto3 && fv.Kind() == reflect.Slice && fv.Len() == 0 {
315
- // empty bytes field
316
- continue
317
- }
318
- if fv.Kind() != reflect.Ptr && fv.Kind() != reflect.Slice {
319
- // proto3 non-repeated scalar field; skip if zero value
320
- switch fv.Kind() {
321
- case reflect.Bool:
322
- if !fv.Bool() {
323
- continue
324
- }
325
- case reflect.Int32, reflect.Int64:
326
- if fv.Int() == 0 {
327
- continue
328
- }
329
- case reflect.Uint32, reflect.Uint64:
330
- if fv.Uint() == 0 {
331
- continue
332
- }
333
- case reflect.Float32, reflect.Float64:
334
- if fv.Float() == 0 {
335
- continue
336
- }
337
- case reflect.String:
338
- if fv.String() == "" {
339
- continue
340
- }
341
- }
342
- }
343
-
344
- if err := writeName(w, props); err != nil {
345
- return err
346
- }
347
- if !w.compact {
348
- if err := w.WriteByte(' '); err != nil {
349
- return err
350
- }
351
- }
352
- if b, ok := fv.Interface().(raw); ok {
353
- if err := writeRaw(w, b.Bytes()); err != nil {
354
- return err
355
- }
356
- continue
357
- }
358
-
359
- // Enums have a String method, so writeAny will work fine.
360
- if err := writeAny(w, fv, props); err != nil {
361
- return err
362
- }
363
-
364
- if err := w.WriteByte('\n'); err != nil {
365
- return err
366
- }
367
- }
368
-
369
- // Extensions (the XXX_extensions field).
370
- pv := sv.Addr()
371
- if pv.Type().Implements(extendableProtoType) {
372
- if err := writeExtensions(w, pv); err != nil {
373
- return err
374
- }
375
- }
376
-
377
- return nil
378
-}
379
-
380
-// writeRaw writes an uninterpreted raw message.
381
-func writeRaw(w *textWriter, b []byte) error {
382
- if err := w.WriteByte('<'); err != nil {
383
- return err
384
- }
385
- if !w.compact {
386
- if err := w.WriteByte('\n'); err != nil {
387
- return err
388
- }
389
- }
390
- w.indent()
391
- if err := writeUnknownStruct(w, b); err != nil {
392
- return err
393
- }
394
- w.unindent()
395
- if err := w.WriteByte('>'); err != nil {
396
- return err
397
- }
398
- return nil
399
-}
400
-
401
-// writeAny writes an arbitrary field.
402
-func writeAny(w *textWriter, v reflect.Value, props *Properties) error {
403
- v = reflect.Indirect(v)
404
-
405
- // Floats have special cases.
406
- if v.Kind() == reflect.Float32 || v.Kind() == reflect.Float64 {
407
- x := v.Float()
408
- var b []byte
409
- switch {
410
- case math.IsInf(x, 1):
411
- b = posInf
412
- case math.IsInf(x, -1):
413
- b = negInf
414
- case math.IsNaN(x):
415
- b = nan
416
- }
417
- if b != nil {
418
- _, err := w.Write(b)
419
- return err
420
- }
421
- // Other values are handled below.
422
- }
423
-
424
- // We don't attempt to serialise every possible value type; only those
425
- // that can occur in protocol buffers.
426
- switch v.Kind() {
427
- case reflect.Slice:
428
- // Should only be a []byte; repeated fields are handled in writeStruct.
429
- if err := writeString(w, string(v.Interface().([]byte))); err != nil {
430
- return err
431
- }
432
- case reflect.String:
433
- if err := writeString(w, v.String()); err != nil {
434
- return err
435
- }
436
- case reflect.Struct:
437
- // Required/optional group/message.
438
- var bra, ket byte = '<', '>'
439
- if props != nil && props.Wire == "group" {
440
- bra, ket = '{', '}'
441
- }
442
- if err := w.WriteByte(bra); err != nil {
443
- return err
444
- }
445
- if !w.compact {
446
- if err := w.WriteByte('\n'); err != nil {
447
- return err
448
- }
449
- }
450
- w.indent()
451
- if tm, ok := v.Interface().(encoding.TextMarshaler); ok {
452
- text, err := tm.MarshalText()
453
- if err != nil {
454
- return err
455
- }
456
- if _, err = w.Write(text); err != nil {
457
- return err
458
- }
459
- } else if err := writeStruct(w, v); err != nil {
460
- return err
461
- }
462
- w.unindent()
463
- if err := w.WriteByte(ket); err != nil {
464
- return err
465
- }
466
- default:
467
- _, err := fmt.Fprint(w, v.Interface())
468
- return err
469
- }
470
- return nil
471
-}
472
-
473
-// equivalent to C's isprint.
474
-func isprint(c byte) bool {
475
- return c >= 0x20 && c < 0x7f
476
-}
477
-
478
-// writeString writes a string in the protocol buffer text format.
479
-// It is similar to strconv.Quote except we don't use Go escape sequences,
480
-// we treat the string as a byte sequence, and we use octal escapes.
481
-// These differences are to maintain interoperability with the other
482
-// languages' implementations of the text format.
483
-func writeString(w *textWriter, s string) error {
484
- // use WriteByte here to get any needed indent
485
- if err := w.WriteByte('"'); err != nil {
486
- return err
487
- }
488
- // Loop over the bytes, not the runes.
489
- for i := 0; i < len(s); i++ {
490
- var err error
491
- // Divergence from C++: we don't escape apostrophes.
492
- // There's no need to escape them, and the C++ parser
493
- // copes with a naked apostrophe.
494
- switch c := s[i]; c {
495
- case '\n':
496
- _, err = w.w.Write(backslashN)
497
- case '\r':
498
- _, err = w.w.Write(backslashR)
499
- case '\t':
500
- _, err = w.w.Write(backslashT)
501
- case '"':
502
- _, err = w.w.Write(backslashDQ)
503
- case '\\':
504
- _, err = w.w.Write(backslashBS)
505
- default:
506
- if isprint(c) {
507
- err = w.w.WriteByte(c)
508
- } else {
509
- _, err = fmt.Fprintf(w.w, "\\%03o", c)
510
- }
511
- }
512
- if err != nil {
513
- return err
514
- }
515
- }
516
- return w.WriteByte('"')
517
-}
518
-
519
-func writeMessageSet(w *textWriter, ms *MessageSet) error {
520
- for _, item := range ms.Item {
521
- id := *item.TypeId
522
- if msd, ok := messageSetMap[id]; ok {
523
- // Known message set type.
524
- if _, err := fmt.Fprintf(w, "[%s]: <\n", msd.name); err != nil {
525
- return err
526
- }
527
- w.indent()
528
-
529
- pb := reflect.New(msd.t.Elem())
530
- if err := Unmarshal(item.Message, pb.Interface().(Message)); err != nil {
531
- if _, err := fmt.Fprintf(w, "/* bad message: %v */\n", err); err != nil {
532
- return err
533
- }
534
- } else {
535
- if err := writeStruct(w, pb.Elem()); err != nil {
536
- return err
537
- }
538
- }
539
- } else {
540
- // Unknown type.
541
- if _, err := fmt.Fprintf(w, "[%d]: <\n", id); err != nil {
542
- return err
543
- }
544
- w.indent()
545
- if err := writeUnknownStruct(w, item.Message); err != nil {
546
- return err
547
- }
548
- }
549
- w.unindent()
550
- if _, err := w.Write(gtNewline); err != nil {
551
- return err
552
- }
553
- }
554
- return nil
555
-}
556
-
557
-func writeUnknownStruct(w *textWriter, data []byte) (err error) {
558
- if !w.compact {
559
- if _, err := fmt.Fprintf(w, "/* %d unknown bytes */\n", len(data)); err != nil {
560
- return err
561
- }
562
- }
563
- b := NewBuffer(data)
564
- for b.index < len(b.buf) {
565
- x, err := b.DecodeVarint()
566
- if err != nil {
567
- _, err := fmt.Fprintf(w, "/* %v */\n", err)
568
- return err
569
- }
570
- wire, tag := x&7, x>>3
571
- if wire == WireEndGroup {
572
- w.unindent()
573
- if _, err := w.Write(endBraceNewline); err != nil {
574
- return err
575
- }
576
- continue
577
- }
578
- if _, err := fmt.Fprint(w, tag); err != nil {
579
- return err
580
- }
581
- if wire != WireStartGroup {
582
- if err := w.WriteByte(':'); err != nil {
583
- return err
584
- }
585
- }
586
- if !w.compact || wire == WireStartGroup {
587
- if err := w.WriteByte(' '); err != nil {
588
- return err
589
- }
590
- }
591
- switch wire {
592
- case WireBytes:
593
- buf, e := b.DecodeRawBytes(false)
594
- if e == nil {
595
- _, err = fmt.Fprintf(w, "%q", buf)
596
- } else {
597
- _, err = fmt.Fprintf(w, "/* %v */", e)
598
- }
599
- case WireFixed32:
600
- x, err = b.DecodeFixed32()
601
- err = writeUnknownInt(w, x, err)
602
- case WireFixed64:
603
- x, err = b.DecodeFixed64()
604
- err = writeUnknownInt(w, x, err)
605
- case WireStartGroup:
606
- err = w.WriteByte('{')
607
- w.indent()
608
- case WireVarint:
609
- x, err = b.DecodeVarint()
610
- err = writeUnknownInt(w, x, err)
611
- default:
612
- _, err = fmt.Fprintf(w, "/* unknown wire type %d */", wire)
613
- }
614
- if err != nil {
615
- return err
616
- }
617
- if err = w.WriteByte('\n'); err != nil {
618
- return err
619
- }
620
- }
621
- return nil
622
-}
623
-
624
-func writeUnknownInt(w *textWriter, x uint64, err error) error {
625
- if err == nil {
626
- _, err = fmt.Fprint(w, x)
627
- } else {
628
- _, err = fmt.Fprintf(w, "/* %v */", err)
629
- }
630
- return err
631
-}
632
-
633
-type int32Slice []int32
634
-
635
-func (s int32Slice) Len() int { return len(s) }
636
-func (s int32Slice) Less(i, j int) bool { return s[i] < s[j] }
637
-func (s int32Slice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
638
-
639
-// writeExtensions writes all the extensions in pv.
640
-// pv is assumed to be a pointer to a protocol message struct that is extendable.
641
-func writeExtensions(w *textWriter, pv reflect.Value) error {
642
- emap := extensionMaps[pv.Type().Elem()]
643
- ep := pv.Interface().(extendableProto)
644
-
645
- // Order the extensions by ID.
646
- // This isn't strictly necessary, but it will give us
647
- // canonical output, which will also make testing easier.
648
- m := ep.ExtensionMap()
649
- ids := make([]int32, 0, len(m))
650
- for id := range m {
651
- ids = append(ids, id)
652
- }
653
- sort.Sort(int32Slice(ids))
654
-
655
- for _, extNum := range ids {
656
- ext := m[extNum]
657
- var desc *ExtensionDesc
658
- if emap != nil {
659
- desc = emap[extNum]
660
- }
661
- if desc == nil {
662
- // Unknown extension.
663
- if err := writeUnknownStruct(w, ext.enc); err != nil {
664
- return err
665
- }
666
- continue
667
- }
668
-
669
- pb, err := GetExtension(ep, desc)
670
- if err != nil {
671
- return fmt.Errorf("failed getting extension: %v", err)
672
- }
673
-
674
- // Repeated extensions will appear as a slice.
675
- if !desc.repeated() {
676
- if err := writeExtension(w, desc.Name, pb); err != nil {
677
- return err
678
- }
679
- } else {
680
- v := reflect.ValueOf(pb)
681
- for i := 0; i < v.Len(); i++ {
682
- if err := writeExtension(w, desc.Name, v.Index(i).Interface()); err != nil {
683
- return err
684
- }
685
- }
686
- }
687
- }
688
- return nil
689
-}
690
-
691
-func writeExtension(w *textWriter, name string, pb interface{}) error {
692
- if _, err := fmt.Fprintf(w, "[%s]:", name); err != nil {
693
- return err
694
- }
695
- if !w.compact {
696
- if err := w.WriteByte(' '); err != nil {
697
- return err
698
- }
699
- }
700
- if err := writeAny(w, reflect.ValueOf(pb), nil); err != nil {
701
- return err
702
- }
703
- if err := w.WriteByte('\n'); err != nil {
704
- return err
705
- }
706
- return nil
707
-}
708
-
709
-func (w *textWriter) writeIndent() {
710
- if !w.complete {
711
- return
712
- }
713
- remain := w.ind * 2
714
- for remain > 0 {
715
- n := remain
716
- if n > len(spaces) {
717
- n = len(spaces)
718
- }
719
- w.w.Write(spaces[:n])
720
- remain -= n
721
- }
722
- w.complete = false
723
-}
724
-
725
-func marshalText(w io.Writer, pb Message, compact bool) error {
726
- val := reflect.ValueOf(pb)
727
- if pb == nil || val.IsNil() {
728
- w.Write([]byte("<nil>"))
729
- return nil
730
- }
731
- var bw *bufio.Writer
732
- ww, ok := w.(writer)
733
- if !ok {
734
- bw = bufio.NewWriter(w)
735
- ww = bw
736
- }
737
- aw := &textWriter{
738
- w: ww,
739
- complete: true,
740
- compact: compact,
741
- }
742
-
743
- if tm, ok := pb.(encoding.TextMarshaler); ok {
744
- text, err := tm.MarshalText()
745
- if err != nil {
746
- return err
747
- }
748
- if _, err = aw.Write(text); err != nil {
749
- return err
750
- }
751
- if bw != nil {
752
- return bw.Flush()
753
- }
754
- return nil
755
- }
756
- // Dereference the received pointer so we don't have outer < and >.
757
- v := reflect.Indirect(val)
758
- if err := writeStruct(aw, v); err != nil {
759
- return err
760
- }
761
- if bw != nil {
762
- return bw.Flush()
763
- }
764
- return nil
765
-}
766
-
767
-// MarshalText writes a given protocol buffer in text format.
768
-// The only errors returned are from w.
769
-func MarshalText(w io.Writer, pb Message) error {
770
- return marshalText(w, pb, false)
771
-}
772
-
773
-// MarshalTextString is the same as MarshalText, but returns the string directly.
774
-func MarshalTextString(pb Message) string {
775
- var buf bytes.Buffer
776
- marshalText(&buf, pb, false)
777
- return buf.String()
778
-}
779
-
780
-// CompactText writes a given protocol buffer in compact text format (one line).
781
-func CompactText(w io.Writer, pb Message) error { return marshalText(w, pb, true) }
782
-
783
-// CompactTextString is the same as CompactText, but returns the string directly.
784
-func CompactTextString(pb Message) string {
785
- var buf bytes.Buffer
786
- marshalText(&buf, pb, true)
787
- return buf.String()
788
-}
Godeps/_workspace/src/github.com/golang/protobuf/proto/text_parser.go
deleted
-772
@@ -1,772 +0,0 @@
1
-// Go support for Protocol Buffers - Google's data interchange format
2
-//
3
-// Copyright 2010 The Go Authors. All rights reserved.
4
-// https://github.com/golang/protobuf
5
-//
6
-// Redistribution and use in source and binary forms, with or without
7
-// modification, are permitted provided that the following conditions are
8
-// met:
9
-//
10
-// * Redistributions of source code must retain the above copyright
11
-// notice, this list of conditions and the following disclaimer.
12
-// * Redistributions in binary form must reproduce the above
13
-// copyright notice, this list of conditions and the following disclaimer
14
-// in the documentation and/or other materials provided with the
15
-// distribution.
16
-// * Neither the name of Google Inc. nor the names of its
17
-// contributors may be used to endorse or promote products derived from
18
-// this software without specific prior written permission.
19
-//
20
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
-// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
-// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23
-// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24
-// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25
-// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26
-// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27
-// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28
-// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29
-// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
-// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
-
32
-package proto
33
-
34
-// Functions for parsing the Text protocol buffer format.
35
-// TODO: message sets.
36
-
37
-import (
38
- "encoding"
39
- "errors"
40
- "fmt"
41
- "reflect"
42
- "strconv"
43
- "strings"
44
- "unicode/utf8"
45
-)
46
-
47
-type ParseError struct {
48
- Message string
49
- Line int // 1-based line number
50
- Offset int // 0-based byte offset from start of input
51
-}
52
-
53
-func (p *ParseError) Error() string {
54
- if p.Line == 1 {
55
- // show offset only for first line
56
- return fmt.Sprintf("line 1.%d: %v", p.Offset, p.Message)
57
- }
58
- return fmt.Sprintf("line %d: %v", p.Line, p.Message)
59
-}
60
-
61
-type token struct {
62
- value string
63
- err *ParseError
64
- line int // line number
65
- offset int // byte number from start of input, not start of line
66
- unquoted string // the unquoted version of value, if it was a quoted string
67
-}
68
-
69
-func (t *token) String() string {
70
- if t.err == nil {
71
- return fmt.Sprintf("%q (line=%d, offset=%d)", t.value, t.line, t.offset)
72
- }
73
- return fmt.Sprintf("parse error: %v", t.err)
74
-}
75
-
76
-type textParser struct {
77
- s string // remaining input
78
- done bool // whether the parsing is finished (success or error)
79
- backed bool // whether back() was called
80
- offset, line int
81
- cur token
82
-}
83
-
84
-func newTextParser(s string) *textParser {
85
- p := new(textParser)
86
- p.s = s
87
- p.line = 1
88
- p.cur.line = 1
89
- return p
90
-}
91
-
92
-func (p *textParser) errorf(format string, a ...interface{}) *ParseError {
93
- pe := &ParseError{fmt.Sprintf(format, a...), p.cur.line, p.cur.offset}
94
- p.cur.err = pe
95
- p.done = true
96
- return pe
97
-}
98
-
99
-// Numbers and identifiers are matched by [-+._A-Za-z0-9]
100
-func isIdentOrNumberChar(c byte) bool {
101
- switch {
102
- case 'A' <= c && c <= 'Z', 'a' <= c && c <= 'z':
103
- return true
104
- case '0' <= c && c <= '9':
105
- return true
106
- }
107
- switch c {
108
- case '-', '+', '.', '_':
109
- return true
110
- }
111
- return false
112
-}
113
-
114
-func isWhitespace(c byte) bool {
115
- switch c {
116
- case ' ', '\t', '\n', '\r':
117
- return true
118
- }
119
- return false
120
-}
121
-
122
-func (p *textParser) skipWhitespace() {
123
- i := 0
124
- for i < len(p.s) && (isWhitespace(p.s[i]) || p.s[i] == '#') {
125
- if p.s[i] == '#' {
126
- // comment; skip to end of line or input
127
- for i < len(p.s) && p.s[i] != '\n' {
128
- i++
129
- }
130
- if i == len(p.s) {
131
- break
132
- }
133
- }
134
- if p.s[i] == '\n' {
135
- p.line++
136
- }
137
- i++
138
- }
139
- p.offset += i
140
- p.s = p.s[i:len(p.s)]
141
- if len(p.s) == 0 {
142
- p.done = true
143
- }
144
-}
145
-
146
-func (p *textParser) advance() {
147
- // Skip whitespace
148
- p.skipWhitespace()
149
- if p.done {
150
- return
151
- }
152
-
153
- // Start of non-whitespace
154
- p.cur.err = nil
155
- p.cur.offset, p.cur.line = p.offset, p.line
156
- p.cur.unquoted = ""
157
- switch p.s[0] {
158
- case '<', '>', '{', '}', ':', '[', ']', ';', ',':
159
- // Single symbol
160
- p.cur.value, p.s = p.s[0:1], p.s[1:len(p.s)]
161
- case '"', '\'':
162
- // Quoted string
163
- i := 1
164
- for i < len(p.s) && p.s[i] != p.s[0] && p.s[i] != '\n' {
165
- if p.s[i] == '\\' && i+1 < len(p.s) {
166
- // skip escaped char
167
- i++
168
- }
169
- i++
170
- }
171
- if i >= len(p.s) || p.s[i] != p.s[0] {
172
- p.errorf("unmatched quote")
173
- return
174
- }
175
- unq, err := unquoteC(p.s[1:i], rune(p.s[0]))
176
- if err != nil {
177
- p.errorf("invalid quoted string %v", p.s[0:i+1])
178
- return
179
- }
180
- p.cur.value, p.s = p.s[0:i+1], p.s[i+1:len(p.s)]
181
- p.cur.unquoted = unq
182
- default:
183
- i := 0
184
- for i < len(p.s) && isIdentOrNumberChar(p.s[i]) {
185
- i++
186
- }
187
- if i == 0 {
188
- p.errorf("unexpected byte %#x", p.s[0])
189
- return
190
- }
191
- p.cur.value, p.s = p.s[0:i], p.s[i:len(p.s)]
192
- }
193
- p.offset += len(p.cur.value)
194
-}
195
-
196
-var (
197
- errBadUTF8 = errors.New("proto: bad UTF-8")
198
- errBadHex = errors.New("proto: bad hexadecimal")
199
-)
200
-
201
-func unquoteC(s string, quote rune) (string, error) {
202
- // This is based on C++'s tokenizer.cc.
203
- // Despite its name, this is *not* parsing C syntax.
204
- // For instance, "\0" is an invalid quoted string.
205
-
206
- // Avoid allocation in trivial cases.
207
- simple := true
208
- for _, r := range s {
209
- if r == '\\' || r == quote {
210
- simple = false
211
- break
212
- }
213
- }
214
- if simple {
215
- return s, nil
216
- }
217
-
218
- buf := make([]byte, 0, 3*len(s)/2)
219
- for len(s) > 0 {
220
- r, n := utf8.DecodeRuneInString(s)
221
- if r == utf8.RuneError && n == 1 {
222
- return "", errBadUTF8
223
- }
224
- s = s[n:]
225
- if r != '\\' {
226
- if r < utf8.RuneSelf {
227
- buf = append(buf, byte(r))
228
- } else {
229
- buf = append(buf, string(r)...)
230
- }
231
- continue
232
- }
233
-
234
- ch, tail, err := unescape(s)
235
- if err != nil {
236
- return "", err
237
- }
238
- buf = append(buf, ch...)
239
- s = tail
240
- }
241
- return string(buf), nil
242
-}
243
-
244
-func unescape(s string) (ch string, tail string, err error) {
245
- r, n := utf8.DecodeRuneInString(s)
246
- if r == utf8.RuneError && n == 1 {
247
- return "", "", errBadUTF8
248
- }
249
- s = s[n:]
250
- switch r {
251
- case 'a':
252
- return "\a", s, nil
253
- case 'b':
254
- return "\b", s, nil
255
- case 'f':
256
- return "\f", s, nil
257
- case 'n':
258
- return "\n", s, nil
259
- case 'r':
260
- return "\r", s, nil
261
- case 't':
262
- return "\t", s, nil
263
- case 'v':
264
- return "\v", s, nil
265
- case '?':
266
- return "?", s, nil // trigraph workaround
267
- case '\'', '"', '\\':
268
- return string(r), s, nil
269
- case '0', '1', '2', '3', '4', '5', '6', '7', 'x', 'X':
270
- if len(s) < 2 {
271
- return "", "", fmt.Errorf(`\%c requires 2 following digits`, r)
272
- }
273
- base := 8
274
- ss := s[:2]
275
- s = s[2:]
276
- if r == 'x' || r == 'X' {
277
- base = 16
278
- } else {
279
- ss = string(r) + ss
280
- }
281
- i, err := strconv.ParseUint(ss, base, 8)
282
- if err != nil {
283
- return "", "", err
284
- }
285
- return string([]byte{byte(i)}), s, nil
286
- case 'u', 'U':
287
- n := 4
288
- if r == 'U' {
289
- n = 8
290
- }
291
- if len(s) < n {
292
- return "", "", fmt.Errorf(`\%c requires %d digits`, r, n)
293
- }
294
-
295
- bs := make([]byte, n/2)
296
- for i := 0; i < n; i += 2 {
297
- a, ok1 := unhex(s[i])
298
- b, ok2 := unhex(s[i+1])
299
- if !ok1 || !ok2 {
300
- return "", "", errBadHex
301
- }
302
- bs[i/2] = a<<4 | b
303
- }
304
- s = s[n:]
305
- return string(bs), s, nil
306
- }
307
- return "", "", fmt.Errorf(`unknown escape \%c`, r)
308
-}
309
-
310
-// Adapted from src/pkg/strconv/quote.go.
311
-func unhex(b byte) (v byte, ok bool) {
312
- switch {
313
- case '0' <= b && b <= '9':
314
- return b - '0', true
315
- case 'a' <= b && b <= 'f':
316
- return b - 'a' + 10, true
317
- case 'A' <= b && b <= 'F':
318
- return b - 'A' + 10, true
319
- }
320
- return 0, false
321
-}
322
-
323
-// Back off the parser by one token. Can only be done between calls to next().
324
-// It makes the next advance() a no-op.
325
-func (p *textParser) back() { p.backed = true }
326
-
327
-// Advances the parser and returns the new current token.
328
-func (p *textParser) next() *token {
329
- if p.backed || p.done {
330
- p.backed = false
331
- return &p.cur
332
- }
333
- p.advance()
334
- if p.done {
335
- p.cur.value = ""
336
- } else if len(p.cur.value) > 0 && p.cur.value[0] == '"' {
337
- // Look for multiple quoted strings separated by whitespace,
338
- // and concatenate them.
339
- cat := p.cur
340
- for {
341
- p.skipWhitespace()
342
- if p.done || p.s[0] != '"' {
343
- break
344
- }
345
- p.advance()
346
- if p.cur.err != nil {
347
- return &p.cur
348
- }
349
- cat.value += " " + p.cur.value
350
- cat.unquoted += p.cur.unquoted
351
- }
352
- p.done = false // parser may have seen EOF, but we want to return cat
353
- p.cur = cat
354
- }
355
- return &p.cur
356
-}
357
-
358
-func (p *textParser) consumeToken(s string) error {
359
- tok := p.next()
360
- if tok.err != nil {
361
- return tok.err
362
- }
363
- if tok.value != s {
364
- p.back()
365
- return p.errorf("expected %q, found %q", s, tok.value)
366
- }
367
- return nil
368
-}
369
-
370
-// Return a RequiredNotSetError indicating which required field was not set.
371
-func (p *textParser) missingRequiredFieldError(sv reflect.Value) *RequiredNotSetError {
372
- st := sv.Type()
373
- sprops := GetProperties(st)
374
- for i := 0; i < st.NumField(); i++ {
375
- if !isNil(sv.Field(i)) {
376
- continue
377
- }
378
-
379
- props := sprops.Prop[i]
380
- if props.Required {
381
- return &RequiredNotSetError{fmt.Sprintf("%v.%v", st, props.OrigName)}
382
- }
383
- }
384
- return &RequiredNotSetError{fmt.Sprintf("%v.<unknown field name>", st)} // should not happen
385
-}
386
-
387
-// Returns the index in the struct for the named field, as well as the parsed tag properties.
388
-func structFieldByName(st reflect.Type, name string) (int, *Properties, bool) {
389
- sprops := GetProperties(st)
390
- i, ok := sprops.decoderOrigNames[name]
391
- if ok {
392
- return i, sprops.Prop[i], true
393
- }
394
- return -1, nil, false
395
-}
396
-
397
-// Consume a ':' from the input stream (if the next token is a colon),
398
-// returning an error if a colon is needed but not present.
399
-func (p *textParser) checkForColon(props *Properties, typ reflect.Type) *ParseError {
400
- tok := p.next()
401
- if tok.err != nil {
402
- return tok.err
403
- }
404
- if tok.value != ":" {
405
- // Colon is optional when the field is a group or message.
406
- needColon := true
407
- switch props.Wire {
408
- case "group":
409
- needColon = false
410
- case "bytes":
411
- // A "bytes" field is either a message, a string, or a repeated field;
412
- // those three become *T, *string and []T respectively, so we can check for
413
- // this field being a pointer to a non-string.
414
- if typ.Kind() == reflect.Ptr {
415
- // *T or *string
416
- if typ.Elem().Kind() == reflect.String {
417
- break
418
- }
419
- } else if typ.Kind() == reflect.Slice {
420
- // []T or []*T
421
- if typ.Elem().Kind() != reflect.Ptr {
422
- break
423
- }
424
- } else if typ.Kind() == reflect.String {
425
- // The proto3 exception is for a string field,
426
- // which requires a colon.
427
- break
428
- }
429
- needColon = false
430
- }
431
- if needColon {
432
- return p.errorf("expected ':', found %q", tok.value)
433
- }
434
- p.back()
435
- }
436
- return nil
437
-}
438
-
439
-func (p *textParser) readStruct(sv reflect.Value, terminator string) error {
440
- st := sv.Type()
441
- reqCount := GetProperties(st).reqCount
442
- var reqFieldErr error
443
- fieldSet := make(map[string]bool)
444
- // A struct is a sequence of "name: value", terminated by one of
445
- // '>' or '}', or the end of the input. A name may also be
446
- // "[extension]".
447
- for {
448
- tok := p.next()
449
- if tok.err != nil {
450
- return tok.err
451
- }
452
- if tok.value == terminator {
453
- break
454
- }
455
- if tok.value == "[" {
456
- // Looks like an extension.
457
- //
458
- // TODO: Check whether we need to handle
459
- // namespace rooted names (e.g. ".something.Foo").
460
- tok = p.next()
461
- if tok.err != nil {
462
- return tok.err
463
- }
464
- var desc *ExtensionDesc
465
- // This could be faster, but it's functional.
466
- // TODO: Do something smarter than a linear scan.
467
- for _, d := range RegisteredExtensions(reflect.New(st).Interface().(Message)) {
468
- if d.Name == tok.value {
469
- desc = d
470
- break
471
- }
472
- }
473
- if desc == nil {
474
- return p.errorf("unrecognized extension %q", tok.value)
475
- }
476
- // Check the extension terminator.
477
- tok = p.next()
478
- if tok.err != nil {
479
- return tok.err
480
- }
481
- if tok.value != "]" {
482
- return p.errorf("unrecognized extension terminator %q", tok.value)
483
- }
484
-
485
- props := &Properties{}
486
- props.Parse(desc.Tag)
487
-
488
- typ := reflect.TypeOf(desc.ExtensionType)
489
- if err := p.checkForColon(props, typ); err != nil {
490
- return err
491
- }
492
-
493
- rep := desc.repeated()
494
-
495
- // Read the extension structure, and set it in
496
- // the value we're constructing.
497
- var ext reflect.Value
498
- if !rep {
499
- ext = reflect.New(typ).Elem()
500
- } else {
501
- ext = reflect.New(typ.Elem()).Elem()
502
- }
503
- if err := p.readAny(ext, props); err != nil {
504
- if _, ok := err.(*RequiredNotSetError); !ok {
505
- return err
506
- }
507
- reqFieldErr = err
508
- }
509
- ep := sv.Addr().Interface().(extendableProto)
510
- if !rep {
511
- SetExtension(ep, desc, ext.Interface())
512
- } else {
513
- old, err := GetExtension(ep, desc)
514
- var sl reflect.Value
515
- if err == nil {
516
- sl = reflect.ValueOf(old) // existing slice
517
- } else {
518
- sl = reflect.MakeSlice(typ, 0, 1)
519
- }
520
- sl = reflect.Append(sl, ext)
521
- SetExtension(ep, desc, sl.Interface())
522
- }
523
- } else {
524
- // This is a normal, non-extension field.
525
- name := tok.value
526
- fi, props, ok := structFieldByName(st, name)
527
- if !ok {
528
- return p.errorf("unknown field name %q in %v", name, st)
529
- }
530
-
531
- dst := sv.Field(fi)
532
-
533
- if dst.Kind() == reflect.Map {
534
- // Consume any colon.
535
- if err := p.checkForColon(props, dst.Type()); err != nil {
536
- return err
537
- }
538
-
539
- // Construct the map if it doesn't already exist.
540
- if dst.IsNil() {
541
- dst.Set(reflect.MakeMap(dst.Type()))
542
- }
543
- key := reflect.New(dst.Type().Key()).Elem()
544
- val := reflect.New(dst.Type().Elem()).Elem()
545
-
546
- // The map entry should be this sequence of tokens:
547
- // < key : KEY value : VALUE >
548
- // Technically the "key" and "value" could come in any order,
549
- // but in practice they won't.
550
-
551
- tok := p.next()
552
- var terminator string
553
- switch tok.value {
554
- case "<":
555
- terminator = ">"
556
- case "{":
557
- terminator = "}"
558
- default:
559
- return p.errorf("expected '{' or '<', found %q", tok.value)
560
- }
561
- if err := p.consumeToken("key"); err != nil {
562
- return err
563
- }
564
- if err := p.consumeToken(":"); err != nil {
565
- return err
566
- }
567
- if err := p.readAny(key, props.mkeyprop); err != nil {
568
- return err
569
- }
570
- if err := p.consumeOptionalSeparator(); err != nil {
571
- return err
572
- }
573
- if err := p.consumeToken("value"); err != nil {
574
- return err
575
- }
576
- if err := p.checkForColon(props.mvalprop, dst.Type().Elem()); err != nil {
577
- return err
578
- }
579
- if err := p.readAny(val, props.mvalprop); err != nil {
580
- return err
581
- }
582
- if err := p.consumeOptionalSeparator(); err != nil {
583
- return err
584
- }
585
- if err := p.consumeToken(terminator); err != nil {
586
- return err
587
- }
588
-
589
- dst.SetMapIndex(key, val)
590
- continue
591
- }
592
-
593
- // Check that it's not already set if it's not a repeated field.
594
- if !props.Repeated && fieldSet[name] {
595
- return p.errorf("non-repeated field %q was repeated", name)
596
- }
597
-
598
- if err := p.checkForColon(props, st.Field(fi).Type); err != nil {
599
- return err
600
- }
601
-
602
- // Parse into the field.
603
- fieldSet[name] = true
604
- if err := p.readAny(dst, props); err != nil {
605
- if _, ok := err.(*RequiredNotSetError); !ok {
606
- return err
607
- }
608
- reqFieldErr = err
609
- } else if props.Required {
610
- reqCount--
611
- }
612
- }
613
-
614
- if err := p.consumeOptionalSeparator(); err != nil {
615
- return err
616
- }
617
-
618
- }
619
-
620
- if reqCount > 0 {
621
- return p.missingRequiredFieldError(sv)
622
- }
623
- return reqFieldErr
624
-}
625
-
626
-// consumeOptionalSeparator consumes an optional semicolon or comma.
627
-// It is used in readStruct to provide backward compatibility.
628
-func (p *textParser) consumeOptionalSeparator() error {
629
- tok := p.next()
630
- if tok.err != nil {
631
- return tok.err
632
- }
633
- if tok.value != ";" && tok.value != "," {
634
- p.back()
635
- }
636
- return nil
637
-}
638
-
639
-func (p *textParser) readAny(v reflect.Value, props *Properties) error {
640
- tok := p.next()
641
- if tok.err != nil {
642
- return tok.err
643
- }
644
- if tok.value == "" {
645
- return p.errorf("unexpected EOF")
646
- }
647
-
648
- switch fv := v; fv.Kind() {
649
- case reflect.Slice:
650
- at := v.Type()
651
- if at.Elem().Kind() == reflect.Uint8 {
652
- // Special case for []byte
653
- if tok.value[0] != '"' && tok.value[0] != '\'' {
654
- // Deliberately written out here, as the error after
655
- // this switch statement would write "invalid []byte: ...",
656
- // which is not as user-friendly.
657
- return p.errorf("invalid string: %v", tok.value)
658
- }
659
- bytes := []byte(tok.unquoted)
660
- fv.Set(reflect.ValueOf(bytes))
661
- return nil
662
- }
663
- // Repeated field. May already exist.
664
- flen := fv.Len()
665
- if flen == fv.Cap() {
666
- nav := reflect.MakeSlice(at, flen, 2*flen+1)
667
- reflect.Copy(nav, fv)
668
- fv.Set(nav)
669
- }
670
- fv.SetLen(flen + 1)
671
-
672
- // Read one.
673
- p.back()
674
- return p.readAny(fv.Index(flen), props)
675
- case reflect.Bool:
676
- // Either "true", "false", 1 or 0.
677
- switch tok.value {
678
- case "true", "1":
679
- fv.SetBool(true)
680
- return nil
681
- case "false", "0":
682
- fv.SetBool(false)
683
- return nil
684
- }
685
- case reflect.Float32, reflect.Float64:
686
- v := tok.value
687
- // Ignore 'f' for compatibility with output generated by C++, but don't
688
- // remove 'f' when the value is "-inf" or "inf".
689
- if strings.HasSuffix(v, "f") && tok.value != "-inf" && tok.value != "inf" {
690
- v = v[:len(v)-1]
691
- }
692
- if f, err := strconv.ParseFloat(v, fv.Type().Bits()); err == nil {
693
- fv.SetFloat(f)
694
- return nil
695
- }
696
- case reflect.Int32:
697
- if x, err := strconv.ParseInt(tok.value, 0, 32); err == nil {
698
- fv.SetInt(x)
699
- return nil
700
- }
701
-
702
- if len(props.Enum) == 0 {
703
- break
704
- }
705
- m, ok := enumValueMaps[props.Enum]
706
- if !ok {
707
- break
708
- }
709
- x, ok := m[tok.value]
710
- if !ok {
711
- break
712
- }
713
- fv.SetInt(int64(x))
714
- return nil
715
- case reflect.Int64:
716
- if x, err := strconv.ParseInt(tok.value, 0, 64); err == nil {
717
- fv.SetInt(x)
718
- return nil
719
- }
720
-
721
- case reflect.Ptr:
722
- // A basic field (indirected through pointer), or a repeated message/group
723
- p.back()
724
- fv.Set(reflect.New(fv.Type().Elem()))
725
- return p.readAny(fv.Elem(), props)
726
- case reflect.String:
727
- if tok.value[0] == '"' || tok.value[0] == '\'' {
728
- fv.SetString(tok.unquoted)
729
- return nil
730
- }
731
- case reflect.Struct:
732
- var terminator string
733
- switch tok.value {
734
- case "{":
735
- terminator = "}"
736
- case "<":
737
- terminator = ">"
738
- default:
739
- return p.errorf("expected '{' or '<', found %q", tok.value)
740
- }
741
- // TODO: Handle nested messages which implement encoding.TextUnmarshaler.
742
- return p.readStruct(fv, terminator)
743
- case reflect.Uint32:
744
- if x, err := strconv.ParseUint(tok.value, 0, 32); err == nil {
745
- fv.SetUint(uint64(x))
746
- return nil
747
- }
748
- case reflect.Uint64:
749
- if x, err := strconv.ParseUint(tok.value, 0, 64); err == nil {
750
- fv.SetUint(x)
751
- return nil
752
- }
753
- }
754
- return p.errorf("invalid %v: %v", v.Type(), tok.value)
755
-}
756
-
757
-// UnmarshalText reads a protocol buffer in Text format. UnmarshalText resets pb
758
-// before starting to unmarshal, so any existing data in pb is always removed.
759
-// If a required field is not set and no other error occurs,
760
-// UnmarshalText returns *RequiredNotSetError.
761
-func UnmarshalText(s string, pb Message) error {
762
- if um, ok := pb.(encoding.TextUnmarshaler); ok {
763
- err := um.UnmarshalText([]byte(s))
764
- return err
765
- }
766
- pb.Reset()
767
- v := reflect.ValueOf(pb)
768
- if pe := newTextParser(s).readStruct(v.Elem(), ""); pe != nil {
769
- return pe
770
- }
771
- return nil
772
-}
Godeps/_workspace/src/github.com/golang/protobuf/proto/text_parser_test.go
deleted
-511
@@ -1,511 +0,0 @@
1
-// Go support for Protocol Buffers - Google's data interchange format
2
-//
3
-// Copyright 2010 The Go Authors. All rights reserved.
4
-// https://github.com/golang/protobuf
5
-//
6
-// Redistribution and use in source and binary forms, with or without
7
-// modification, are permitted provided that the following conditions are
8
-// met:
9
-//
10
-// * Redistributions of source code must retain the above copyright
11
-// notice, this list of conditions and the following disclaimer.
12
-// * Redistributions in binary form must reproduce the above
13
-// copyright notice, this list of conditions and the following disclaimer
14
-// in the documentation and/or other materials provided with the
15
-// distribution.
16
-// * Neither the name of Google Inc. nor the names of its
17
-// contributors may be used to endorse or promote products derived from
18
-// this software without specific prior written permission.
19
-//
20
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
-// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
-// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23
-// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24
-// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25
-// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26
-// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27
-// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28
-// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29
-// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
-// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
-
32
-package proto_test
33
-
34
-import (
35
- "math"
36
- "reflect"
37
- "testing"
38
-
39
- . "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/golang/protobuf/proto"
40
- proto3pb "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/golang/protobuf/proto/proto3_proto"
41
- . "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/golang/protobuf/proto/testdata"
42
-)
43
-
44
-type UnmarshalTextTest struct {
45
- in string
46
- err string // if "", no error expected
47
- out *MyMessage
48
-}
49
-
50
-func buildExtStructTest(text string) UnmarshalTextTest {
51
- msg := &MyMessage{
52
- Count: Int32(42),
53
- }
54
- SetExtension(msg, E_Ext_More, &Ext{
55
- Data: String("Hello, world!"),
56
- })
57
- return UnmarshalTextTest{in: text, out: msg}
58
-}
59
-
60
-func buildExtDataTest(text string) UnmarshalTextTest {
61
- msg := &MyMessage{
62
- Count: Int32(42),
63
- }
64
- SetExtension(msg, E_Ext_Text, String("Hello, world!"))
65
- SetExtension(msg, E_Ext_Number, Int32(1729))
66
- return UnmarshalTextTest{in: text, out: msg}
67
-}
68
-
69
-func buildExtRepStringTest(text string) UnmarshalTextTest {
70
- msg := &MyMessage{
71
- Count: Int32(42),
72
- }
73
- if err := SetExtension(msg, E_Greeting, []string{"bula", "hola"}); err != nil {
74
- panic(err)
75
- }
76
- return UnmarshalTextTest{in: text, out: msg}
77
-}
78
-
79
-var unMarshalTextTests = []UnmarshalTextTest{
80
- // Basic
81
- {
82
- in: " count:42\n name:\"Dave\" ",
83
- out: &MyMessage{
84
- Count: Int32(42),
85
- Name: String("Dave"),
86
- },
87
- },
88
-
89
- // Empty quoted string
90
- {
91
- in: `count:42 name:""`,
92
- out: &MyMessage{
93
- Count: Int32(42),
94
- Name: String(""),
95
- },
96
- },
97
-
98
- // Quoted string concatenation
99
- {
100
- in: `count:42 name: "My name is "` + "\n" + `"elsewhere"`,
101
- out: &MyMessage{
102
- Count: Int32(42),
103
- Name: String("My name is elsewhere"),
104
- },
105
- },
106
-
107
- // Quoted string with escaped apostrophe
108
- {
109
- in: `count:42 name: "HOLIDAY - New Year\'s Day"`,
110
- out: &MyMessage{
111
- Count: Int32(42),
112
- Name: String("HOLIDAY - New Year's Day"),
113
- },
114
- },
115
-
116
- // Quoted string with single quote
117
- {
118
- in: `count:42 name: 'Roger "The Ramster" Ramjet'`,
119
- out: &MyMessage{
120
- Count: Int32(42),
121
- Name: String(`Roger "The Ramster" Ramjet`),
122
- },
123
- },
124
-
125
- // Quoted string with all the accepted special characters from the C++ test
126
- {
127
- in: `count:42 name: ` + "\"\\\"A string with \\' characters \\n and \\r newlines and \\t tabs and \\001 slashes \\\\ and multiple spaces\"",
128
- out: &MyMessage{
129
- Count: Int32(42),
130
- Name: String("\"A string with ' characters \n and \r newlines and \t tabs and \001 slashes \\ and multiple spaces"),
131
- },
132
- },
133
-
134
- // Quoted string with quoted backslash
135
- {
136
- in: `count:42 name: "\\'xyz"`,
137
- out: &MyMessage{
138
- Count: Int32(42),
139
- Name: String(`\'xyz`),
140
- },
141
- },
142
-
143
- // Quoted string with UTF-8 bytes.
144
- {
145
- in: "count:42 name: '\303\277\302\201\xAB'",
146
- out: &MyMessage{
147
- Count: Int32(42),
148
- Name: String("\303\277\302\201\xAB"),
149
- },
150
- },
151
-
152
- // Bad quoted string
153
- {
154
- in: `inner: < host: "\0" >` + "\n",
155
- err: `line 1.15: invalid quoted string "\0"`,
156
- },
157
-
158
- // Number too large for int64
159
- {
160
- in: "count: 1 others { key: 123456789012345678901 }",
161
- err: "line 1.23: invalid int64: 123456789012345678901",
162
- },
163
-
164
- // Number too large for int32
165
- {
166
- in: "count: 1234567890123",
167
- err: "line 1.7: invalid int32: 1234567890123",
168
- },
169
-
170
- // Number in hexadecimal
171
- {
172
- in: "count: 0x2beef",
173
- out: &MyMessage{
174
- Count: Int32(0x2beef),
175
- },
176
- },
177
-
178
- // Number in octal
179
- {
180
- in: "count: 024601",
181
- out: &MyMessage{
182
- Count: Int32(024601),
183
- },
184
- },
185
-
186
- // Floating point number with "f" suffix
187
- {
188
- in: "count: 4 others:< weight: 17.0f >",
189
- out: &MyMessage{
190
- Count: Int32(4),
191
- Others: []*OtherMessage{
192
- {
193
- Weight: Float32(17),
194
- },
195
- },
196
- },
197
- },
198
-
199
- // Floating point positive infinity
200
- {
201
- in: "count: 4 bigfloat: inf",
202
- out: &MyMessage{
203
- Count: Int32(4),
204
- Bigfloat: Float64(math.Inf(1)),
205
- },
206
- },
207
-
208
- // Floating point negative infinity
209
- {
210
- in: "count: 4 bigfloat: -inf",
211
- out: &MyMessage{
212
- Count: Int32(4),
213
- Bigfloat: Float64(math.Inf(-1)),
214
- },
215
- },
216
-
217
- // Number too large for float32
218
- {
219
- in: "others:< weight: 12345678901234567890123456789012345678901234567890 >",
220
- err: "line 1.17: invalid float32: 12345678901234567890123456789012345678901234567890",
221
- },
222
-
223
- // Number posing as a quoted string
224
- {
225
- in: `inner: < host: 12 >` + "\n",
226
- err: `line 1.15: invalid string: 12`,
227
- },
228
-
229
- // Quoted string posing as int32
230
- {
231
- in: `count: "12"`,
232
- err: `line 1.7: invalid int32: "12"`,
233
- },
234
-
235
- // Quoted string posing a float32
236
- {
237
- in: `others:< weight: "17.4" >`,
238
- err: `line 1.17: invalid float32: "17.4"`,
239
- },
240
-
241
- // Enum
242
- {
243
- in: `count:42 bikeshed: BLUE`,
244
- out: &MyMessage{
245
- Count: Int32(42),
246
- Bikeshed: MyMessage_BLUE.Enum(),
247
- },
248
- },
249
-
250
- // Repeated field
251
- {
252
- in: `count:42 pet: "horsey" pet:"bunny"`,
253
- out: &MyMessage{
254
- Count: Int32(42),
255
- Pet: []string{"horsey", "bunny"},
256
- },
257
- },
258
-
259
- // Repeated message with/without colon and <>/{}
260
- {
261
- in: `count:42 others:{} others{} others:<> others:{}`,
262
- out: &MyMessage{
263
- Count: Int32(42),
264
- Others: []*OtherMessage{
265
- {},
266
- {},
267
- {},
268
- {},
269
- },
270
- },
271
- },
272
-
273
- // Missing colon for inner message
274
- {
275
- in: `count:42 inner < host: "cauchy.syd" >`,
276
- out: &MyMessage{
277
- Count: Int32(42),
278
- Inner: &InnerMessage{
279
- Host: String("cauchy.syd"),
280
- },
281
- },
282
- },
283
-
284
- // Missing colon for string field
285
- {
286
- in: `name "Dave"`,
287
- err: `line 1.5: expected ':', found "\"Dave\""`,
288
- },
289
-
290
- // Missing colon for int32 field
291
- {
292
- in: `count 42`,
293
- err: `line 1.6: expected ':', found "42"`,
294
- },
295
-
296
- // Missing required field
297
- {
298
- in: `name: "Pawel"`,
299
- err: `proto: required field "testdata.MyMessage.count" not set`,
300
- out: &MyMessage{
301
- Name: String("Pawel"),
302
- },
303
- },
304
-
305
- // Repeated non-repeated field
306
- {
307
- in: `name: "Rob" name: "Russ"`,
308
- err: `line 1.12: non-repeated field "name" was repeated`,
309
- },
310
-
311
- // Group
312
- {
313
- in: `count: 17 SomeGroup { group_field: 12 }`,
314
- out: &MyMessage{
315
- Count: Int32(17),
316
- Somegroup: &MyMessage_SomeGroup{
317
- GroupField: Int32(12),
318
- },
319
- },
320
- },
321
-
322
- // Semicolon between fields
323
- {
324
- in: `count:3;name:"Calvin"`,
325
- out: &MyMessage{
326
- Count: Int32(3),
327
- Name: String("Calvin"),
328
- },
329
- },
330
- // Comma between fields
331
- {
332
- in: `count:4,name:"Ezekiel"`,
333
- out: &MyMessage{
334
- Count: Int32(4),
335
- Name: String("Ezekiel"),
336
- },
337
- },
338
-
339
- // Extension
340
- buildExtStructTest(`count: 42 [testdata.Ext.more]:<data:"Hello, world!" >`),
341
- buildExtStructTest(`count: 42 [testdata.Ext.more] {data:"Hello, world!"}`),
342
- buildExtDataTest(`count: 42 [testdata.Ext.text]:"Hello, world!" [testdata.Ext.number]:1729`),
343
- buildExtRepStringTest(`count: 42 [testdata.greeting]:"bula" [testdata.greeting]:"hola"`),
344
-
345
- // Big all-in-one
346
- {
347
- in: "count:42 # Meaning\n" +
348
- `name:"Dave" ` +
349
- `quote:"\"I didn't want to go.\"" ` +
350
- `pet:"bunny" ` +
351
- `pet:"kitty" ` +
352
- `pet:"horsey" ` +
353
- `inner:<` +
354
- ` host:"footrest.syd" ` +
355
- ` port:7001 ` +
356
- ` connected:true ` +
357
- `> ` +
358
- `others:<` +
359
- ` key:3735928559 ` +
360
- ` value:"\x01A\a\f" ` +
361
- `> ` +
362
- `others:<` +
363
- " weight:58.9 # Atomic weight of Co\n" +
364
- ` inner:<` +
365
- ` host:"lesha.mtv" ` +
366
- ` port:8002 ` +
367
- ` >` +
368
- `>`,
369
- out: &MyMessage{
370
- Count: Int32(42),
371
- Name: String("Dave"),
372
- Quote: String(`"I didn't want to go."`),
373
- Pet: []string{"bunny", "kitty", "horsey"},
374
- Inner: &InnerMessage{
375
- Host: String("footrest.syd"),
376
- Port: Int32(7001),
377
- Connected: Bool(true),
378
- },
379
- Others: []*OtherMessage{
380
- {
381
- Key: Int64(3735928559),
382
- Value: []byte{0x1, 'A', '\a', '\f'},
383
- },
384
- {
385
- Weight: Float32(58.9),
386
- Inner: &InnerMessage{
387
- Host: String("lesha.mtv"),
388
- Port: Int32(8002),
389
- },
390
- },
391
- },
392
- },
393
- },
394
-}
395
-
396
-func TestUnmarshalText(t *testing.T) {
397
- for i, test := range unMarshalTextTests {
398
- pb := new(MyMessage)
399
- err := UnmarshalText(test.in, pb)
400
- if test.err == "" {
401
- // We don't expect failure.
402
- if err != nil {
403
- t.Errorf("Test %d: Unexpected error: %v", i, err)
404
- } else if !reflect.DeepEqual(pb, test.out) {
405
- t.Errorf("Test %d: Incorrect populated \nHave: %v\nWant: %v",
406
- i, pb, test.out)
407
- }
408
- } else {
409
- // We do expect failure.
410
- if err == nil {
411
- t.Errorf("Test %d: Didn't get expected error: %v", i, test.err)
412
- } else if err.Error() != test.err {
413
- t.Errorf("Test %d: Incorrect error.\nHave: %v\nWant: %v",
414
- i, err.Error(), test.err)
415
- } else if _, ok := err.(*RequiredNotSetError); ok && test.out != nil && !reflect.DeepEqual(pb, test.out) {
416
- t.Errorf("Test %d: Incorrect populated \nHave: %v\nWant: %v",
417
- i, pb, test.out)
418
- }
419
- }
420
- }
421
-}
422
-
423
-func TestUnmarshalTextCustomMessage(t *testing.T) {
424
- msg := &textMessage{}
425
- if err := UnmarshalText("custom", msg); err != nil {
426
- t.Errorf("Unexpected error from custom unmarshal: %v", err)
427
- }
428
- if UnmarshalText("not custom", msg) == nil {
429
- t.Errorf("Didn't get expected error from custom unmarshal")
430
- }
431
-}
432
-
433
-// Regression test; this caused a panic.
434
-func TestRepeatedEnum(t *testing.T) {
435
- pb := new(RepeatedEnum)
436
- if err := UnmarshalText("color: RED", pb); err != nil {
437
- t.Fatal(err)
438
- }
439
- exp := &RepeatedEnum{
440
- Color: []RepeatedEnum_Color{RepeatedEnum_RED},
441
- }
442
- if !Equal(pb, exp) {
443
- t.Errorf("Incorrect populated \nHave: %v\nWant: %v", pb, exp)
444
- }
445
-}
446
-
447
-func TestProto3TextParsing(t *testing.T) {
448
- m := new(proto3pb.Message)
449
- const in = `name: "Wallace" true_scotsman: true`
450
- want := &proto3pb.Message{
451
- Name: "Wallace",
452
- TrueScotsman: true,
453
- }
454
- if err := UnmarshalText(in, m); err != nil {
455
- t.Fatal(err)
456
- }
457
- if !Equal(m, want) {
458
- t.Errorf("\n got %v\nwant %v", m, want)
459
- }
460
-}
461
-
462
-func TestMapParsing(t *testing.T) {
463
- m := new(MessageWithMap)
464
- const in = `name_mapping:<key:1234 value:"Feist"> name_mapping:<key:1 value:"Beatles">` +
465
- `msg_mapping:<key:-4, value:<f: 2.0>,>` + // separating commas are okay
466
- `msg_mapping<key:-2 value<f: 4.0>>` + // no colon after "value"
467
- `byte_mapping:<key:true value:"so be it">`
468
- want := &MessageWithMap{
469
- NameMapping: map[int32]string{
470
- 1: "Beatles",
471
- 1234: "Feist",
472
- },
473
- MsgMapping: map[int64]*FloatingPoint{
474
- -4: {F: Float64(2.0)},
475
- -2: {F: Float64(4.0)},
476
- },
477
- ByteMapping: map[bool][]byte{
478
- true: []byte("so be it"),
479
- },
480
- }
481
- if err := UnmarshalText(in, m); err != nil {
482
- t.Fatal(err)
483
- }
484
- if !Equal(m, want) {
485
- t.Errorf("\n got %v\nwant %v", m, want)
486
- }
487
-}
488
-
489
-var benchInput string
490
-
491
-func init() {
492
- benchInput = "count: 4\n"
493
- for i := 0; i < 1000; i++ {
494
- benchInput += "pet: \"fido\"\n"
495
- }
496
-
497
- // Check it is valid input.
498
- pb := new(MyMessage)
499
- err := UnmarshalText(benchInput, pb)
500
- if err != nil {
501
- panic("Bad benchmark input: " + err.Error())
502
- }
503
-}
504
-
505
-func BenchmarkUnmarshalText(b *testing.B) {
506
- pb := new(MyMessage)
507
- for i := 0; i < b.N; i++ {
508
- UnmarshalText(benchInput, pb)
509
- }
510
- b.SetBytes(int64(len(benchInput)))
511
-}
Godeps/_workspace/src/github.com/golang/protobuf/proto/text_test.go
deleted
-441
@@ -1,441 +0,0 @@
1
-// Go support for Protocol Buffers - Google's data interchange format
2
-//
3
-// Copyright 2010 The Go Authors. All rights reserved.
4
-// https://github.com/golang/protobuf
5
-//
6
-// Redistribution and use in source and binary forms, with or without
7
-// modification, are permitted provided that the following conditions are
8
-// met:
9
-//
10
-// * Redistributions of source code must retain the above copyright
11
-// notice, this list of conditions and the following disclaimer.
12
-// * Redistributions in binary form must reproduce the above
13
-// copyright notice, this list of conditions and the following disclaimer
14
-// in the documentation and/or other materials provided with the
15
-// distribution.
16
-// * Neither the name of Google Inc. nor the names of its
17
-// contributors may be used to endorse or promote products derived from
18
-// this software without specific prior written permission.
19
-//
20
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
-// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
-// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23
-// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24
-// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25
-// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26
-// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27
-// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28
-// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29
-// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
-// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
-
32
-package proto_test
33
-
34
-import (
35
- "bytes"
36
- "errors"
37
- "io/ioutil"
38
- "math"
39
- "strings"
40
- "testing"
41
-
42
- "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/golang/protobuf/proto"
43
-
44
- proto3pb "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/golang/protobuf/proto/proto3_proto"
45
- pb "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/golang/protobuf/proto/testdata"
46
-)
47
-
48
-// textMessage implements the methods that allow it to marshal and unmarshal
49
-// itself as text.
50
-type textMessage struct {
51
-}
52
-
53
-func (*textMessage) MarshalText() ([]byte, error) {
54
- return []byte("custom"), nil
55
-}
56
-
57
-func (*textMessage) UnmarshalText(bytes []byte) error {
58
- if string(bytes) != "custom" {
59
- return errors.New("expected 'custom'")
60
- }
61
- return nil
62
-}
63
-
64
-func (*textMessage) Reset() {}
65
-func (*textMessage) String() string { return "" }
66
-func (*textMessage) ProtoMessage() {}
67
-
68
-func newTestMessage() *pb.MyMessage {
69
- msg := &pb.MyMessage{
70
- Count: proto.Int32(42),
71
- Name: proto.String("Dave"),
72
- Quote: proto.String(`"I didn't want to go."`),
73
- Pet: []string{"bunny", "kitty", "horsey"},
74
- Inner: &pb.InnerMessage{
75
- Host: proto.String("footrest.syd"),
76
- Port: proto.Int32(7001),
77
- Connected: proto.Bool(true),
78
- },
79
- Others: []*pb.OtherMessage{
80
- {
81
- Key: proto.Int64(0xdeadbeef),
82
- Value: []byte{1, 65, 7, 12},
83
- },
84
- {
85
- Weight: proto.Float32(6.022),
86
- Inner: &pb.InnerMessage{
87
- Host: proto.String("lesha.mtv"),
88
- Port: proto.Int32(8002),
89
- },
90
- },
91
- },
92
- Bikeshed: pb.MyMessage_BLUE.Enum(),
93
- Somegroup: &pb.MyMessage_SomeGroup{
94
- GroupField: proto.Int32(8),
95
- },
96
- // One normally wouldn't do this.
97
- // This is an undeclared tag 13, as a varint (wire type 0) with value 4.
98
- XXX_unrecognized: []byte{13<<3 | 0, 4},
99
- }
100
- ext := &pb.Ext{
101
- Data: proto.String("Big gobs for big rats"),
102
- }
103
- if err := proto.SetExtension(msg, pb.E_Ext_More, ext); err != nil {
104
- panic(err)
105
- }
106
- greetings := []string{"adg", "easy", "cow"}
107
- if err := proto.SetExtension(msg, pb.E_Greeting, greetings); err != nil {
108
- panic(err)
109
- }
110
-
111
- // Add an unknown extension. We marshal a pb.Ext, and fake the ID.
112
- b, err := proto.Marshal(&pb.Ext{Data: proto.String("3G skiing")})
113
- if err != nil {
114
- panic(err)
115
- }
116
- b = append(proto.EncodeVarint(201<<3|proto.WireBytes), b...)
117
- proto.SetRawExtension(msg, 201, b)
118
-
119
- // Extensions can be plain fields, too, so let's test that.
120
- b = append(proto.EncodeVarint(202<<3|proto.WireVarint), 19)
121
- proto.SetRawExtension(msg, 202, b)
122
-
123
- return msg
124
-}
125
-
126
-const text = `count: 42
127
-name: "Dave"
128
-quote: "\"I didn't want to go.\""
129
-pet: "bunny"
130
-pet: "kitty"
131
-pet: "horsey"
132
-inner: <
133
- host: "footrest.syd"
134
- port: 7001
135
- connected: true
136
->
137
-others: <
138
- key: 3735928559
139
- value: "\001A\007\014"
140
->
141
-others: <
142
- weight: 6.022
143
- inner: <
144
- host: "lesha.mtv"
145
- port: 8002
146
- >
147
->
148
-bikeshed: BLUE
149
-SomeGroup {
150
- group_field: 8
151
-}
152
-/* 2 unknown bytes */
153
-13: 4
154
-[testdata.Ext.more]: <
155
- data: "Big gobs for big rats"
156
->
157
-[testdata.greeting]: "adg"
158
-[testdata.greeting]: "easy"
159
-[testdata.greeting]: "cow"
160
-/* 13 unknown bytes */
161
-201: "\t3G skiing"
162
-/* 3 unknown bytes */
163
-202: 19
164
-`
165
-
166
-func TestMarshalText(t *testing.T) {
167
- buf := new(bytes.Buffer)
168
- if err := proto.MarshalText(buf, newTestMessage()); err != nil {
169
- t.Fatalf("proto.MarshalText: %v", err)
170
- }
171
- s := buf.String()
172
- if s != text {
173
- t.Errorf("Got:\n===\n%v===\nExpected:\n===\n%v===\n", s, text)
174
- }
175
-}
176
-
177
-func TestMarshalTextCustomMessage(t *testing.T) {
178
- buf := new(bytes.Buffer)
179
- if err := proto.MarshalText(buf, &textMessage{}); err != nil {
180
- t.Fatalf("proto.MarshalText: %v", err)
181
- }
182
- s := buf.String()
183
- if s != "custom" {
184
- t.Errorf("Got %q, expected %q", s, "custom")
185
- }
186
-}
187
-func TestMarshalTextNil(t *testing.T) {
188
- want := "<nil>"
189
- tests := []proto.Message{nil, (*pb.MyMessage)(nil)}
190
- for i, test := range tests {
191
- buf := new(bytes.Buffer)
192
- if err := proto.MarshalText(buf, test); err != nil {
193
- t.Fatal(err)
194
- }
195
- if got := buf.String(); got != want {
196
- t.Errorf("%d: got %q want %q", i, got, want)
197
- }
198
- }
199
-}
200
-
201
-func TestMarshalTextUnknownEnum(t *testing.T) {
202
- // The Color enum only specifies values 0-2.
203
- m := &pb.MyMessage{Bikeshed: pb.MyMessage_Color(3).Enum()}
204
- got := m.String()
205
- const want = `bikeshed:3 `
206
- if got != want {
207
- t.Errorf("\n got %q\nwant %q", got, want)
208
- }
209
-}
210
-
211
-func BenchmarkMarshalTextBuffered(b *testing.B) {
212
- buf := new(bytes.Buffer)
213
- m := newTestMessage()
214
- for i := 0; i < b.N; i++ {
215
- buf.Reset()
216
- proto.MarshalText(buf, m)
217
- }
218
-}
219
-
220
-func BenchmarkMarshalTextUnbuffered(b *testing.B) {
221
- w := ioutil.Discard
222
- m := newTestMessage()
223
- for i := 0; i < b.N; i++ {
224
- proto.MarshalText(w, m)
225
- }
226
-}
227
-
228
-func compact(src string) string {
229
- // s/[ \n]+/ /g; s/ $//;
230
- dst := make([]byte, len(src))
231
- space, comment := false, false
232
- j := 0
233
- for i := 0; i < len(src); i++ {
234
- if strings.HasPrefix(src[i:], "/*") {
235
- comment = true
236
- i++
237
- continue
238
- }
239
- if comment && strings.HasPrefix(src[i:], "*/") {
240
- comment = false
241
- i++
242
- continue
243
- }
244
- if comment {
245
- continue
246
- }
247
- c := src[i]
248
- if c == ' ' || c == '\n' {
249
- space = true
250
- continue
251
- }
252
- if j > 0 && (dst[j-1] == ':' || dst[j-1] == '<' || dst[j-1] == '{') {
253
- space = false
254
- }
255
- if c == '{' {
256
- space = false
257
- }
258
- if space {
259
- dst[j] = ' '
260
- j++
261
- space = false
262
- }
263
- dst[j] = c
264
- j++
265
- }
266
- if space {
267
- dst[j] = ' '
268
- j++
269
- }
270
- return string(dst[0:j])
271
-}
272
-
273
-var compactText = compact(text)
274
-
275
-func TestCompactText(t *testing.T) {
276
- s := proto.CompactTextString(newTestMessage())
277
- if s != compactText {
278
- t.Errorf("Got:\n===\n%v===\nExpected:\n===\n%v\n===\n", s, compactText)
279
- }
280
-}
281
-
282
-func TestStringEscaping(t *testing.T) {
283
- testCases := []struct {
284
- in *pb.Strings
285
- out string
286
- }{
287
- {
288
- // Test data from C++ test (TextFormatTest.StringEscape).
289
- // Single divergence: we don't escape apostrophes.
290
- &pb.Strings{StringField: proto.String("\"A string with ' characters \n and \r newlines and \t tabs and \001 slashes \\ and multiple spaces")},
291
- "string_field: \"\\\"A string with ' characters \\n and \\r newlines and \\t tabs and \\001 slashes \\\\ and multiple spaces\"\n",
292
- },
293
- {
294
- // Test data from the same C++ test.
295
- &pb.Strings{StringField: proto.String("\350\260\267\346\255\214")},
296
- "string_field: \"\\350\\260\\267\\346\\255\\214\"\n",
297
- },
298
- {
299
- // Some UTF-8.
300
- &pb.Strings{StringField: proto.String("\x00\x01\xff\x81")},
301
- `string_field: "\000\001\377\201"` + "\n",
302
- },
303
- }
304
-
305
- for i, tc := range testCases {
306
- var buf bytes.Buffer
307
- if err := proto.MarshalText(&buf, tc.in); err != nil {
308
- t.Errorf("proto.MarsalText: %v", err)
309
- continue
310
- }
311
- s := buf.String()
312
- if s != tc.out {
313
- t.Errorf("#%d: Got:\n%s\nExpected:\n%s\n", i, s, tc.out)
314
- continue
315
- }
316
-
317
- // Check round-trip.
318
- pb := new(pb.Strings)
319
- if err := proto.UnmarshalText(s, pb); err != nil {
320
- t.Errorf("#%d: UnmarshalText: %v", i, err)
321
- continue
322
- }
323
- if !proto.Equal(pb, tc.in) {
324
- t.Errorf("#%d: Round-trip failed:\nstart: %v\n end: %v", i, tc.in, pb)
325
- }
326
- }
327
-}
328
-
329
-// A limitedWriter accepts some output before it fails.
330
-// This is a proxy for something like a nearly-full or imminently-failing disk,
331
-// or a network connection that is about to die.
332
-type limitedWriter struct {
333
- b bytes.Buffer
334
- limit int
335
-}
336
-
337
-var outOfSpace = errors.New("proto: insufficient space")
338
-
339
-func (w *limitedWriter) Write(p []byte) (n int, err error) {
340
- var avail = w.limit - w.b.Len()
341
- if avail <= 0 {
342
- return 0, outOfSpace
343
- }
344
- if len(p) <= avail {
345
- return w.b.Write(p)
346
- }
347
- n, _ = w.b.Write(p[:avail])
348
- return n, outOfSpace
349
-}
350
-
351
-func TestMarshalTextFailing(t *testing.T) {
352
- // Try lots of different sizes to exercise more error code-paths.
353
- for lim := 0; lim < len(text); lim++ {
354
- buf := new(limitedWriter)
355
- buf.limit = lim
356
- err := proto.MarshalText(buf, newTestMessage())
357
- // We expect a certain error, but also some partial results in the buffer.
358
- if err != outOfSpace {
359
- t.Errorf("Got:\n===\n%v===\nExpected:\n===\n%v===\n", err, outOfSpace)
360
- }
361
- s := buf.b.String()
362
- x := text[:buf.limit]
363
- if s != x {
364
- t.Errorf("Got:\n===\n%v===\nExpected:\n===\n%v===\n", s, x)
365
- }
366
- }
367
-}
368
-
369
-func TestFloats(t *testing.T) {
370
- tests := []struct {
371
- f float64
372
- want string
373
- }{
374
- {0, "0"},
375
- {4.7, "4.7"},
376
- {math.Inf(1), "inf"},
377
- {math.Inf(-1), "-inf"},
378
- {math.NaN(), "nan"},
379
- }
380
- for _, test := range tests {
381
- msg := &pb.FloatingPoint{F: &test.f}
382
- got := strings.TrimSpace(msg.String())
383
- want := `f:` + test.want
384
- if got != want {
385
- t.Errorf("f=%f: got %q, want %q", test.f, got, want)
386
- }
387
- }
388
-}
389
-
390
-func TestRepeatedNilText(t *testing.T) {
391
- m := &pb.MessageList{
392
- Message: []*pb.MessageList_Message{
393
- nil,
394
- &pb.MessageList_Message{
395
- Name: proto.String("Horse"),
396
- },
397
- nil,
398
- },
399
- }
400
- want := `Message <nil>
401
-Message {
402
- name: "Horse"
403
-}
404
-Message <nil>
405
-`
406
- if s := proto.MarshalTextString(m); s != want {
407
- t.Errorf(" got: %s\nwant: %s", s, want)
408
- }
409
-}
410
-
411
-func TestProto3Text(t *testing.T) {
412
- tests := []struct {
413
- m proto.Message
414
- want string
415
- }{
416
- // zero message
417
- {&proto3pb.Message{}, ``},
418
- // zero message except for an empty byte slice
419
- {&proto3pb.Message{Data: []byte{}}, ``},
420
- // trivial case
421
- {&proto3pb.Message{Name: "Rob", HeightInCm: 175}, `name:"Rob" height_in_cm:175`},
422
- // empty map
423
- {&pb.MessageWithMap{}, ``},
424
- // non-empty map; current map format is the same as a repeated struct
425
- {
426
- &pb.MessageWithMap{NameMapping: map[int32]string{1234: "Feist"}},
427
- `name_mapping:<key:1234 value:"Feist" >`,
428
- },
429
- // map with nil value; not well-defined, but we shouldn't crash
430
- {
431
- &pb.MessageWithMap{MsgMapping: map[int64]*pb.FloatingPoint{7: nil}},
432
- `msg_mapping:<key:7 >`,
433
- },
434
- }
435
- for _, test := range tests {
436
- got := strings.TrimSpace(test.m.String())
437
- if got != test.want {
438
- t.Errorf("\n got %s\nwant %s", got, test.want)
439
- }
440
- }
441
-}
Godeps/_workspace/src/github.com/prometheus/client_golang/model/fingerprinting.go
deleted
-110
@@ -1,110 +0,0 @@
1
-// Copyright 2013 The Prometheus Authors
2
-// Licensed under the Apache License, Version 2.0 (the "License");
3
-// you may not use this file except in compliance with the License.
4
-// You may obtain a copy of the License at
5
-//
6
-// http://www.apache.org/licenses/LICENSE-2.0
7
-//
8
-// Unless required by applicable law or agreed to in writing, software
9
-// distributed under the License is distributed on an "AS IS" BASIS,
10
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
-// See the License for the specific language governing permissions and
12
-// limitations under the License.
13
-
14
-package model
15
-
16
-import (
17
- "fmt"
18
- "strconv"
19
-)
20
-
21
-// Fingerprint provides a hash-capable representation of a Metric.
22
-// For our purposes, FNV-1A 64-bit is used.
23
-type Fingerprint uint64
24
-
25
-func (f Fingerprint) String() string {
26
- return fmt.Sprintf("%016x", uint64(f))
27
-}
28
-
29
-// Less implements sort.Interface.
30
-func (f Fingerprint) Less(o Fingerprint) bool {
31
- return f < o
32
-}
33
-
34
-// Equal implements sort.Interface.
35
-func (f Fingerprint) Equal(o Fingerprint) bool {
36
- return f == o
37
-}
38
-
39
-// LoadFromString transforms a string representation into a Fingerprint.
40
-func (f *Fingerprint) LoadFromString(s string) error {
41
- num, err := strconv.ParseUint(s, 16, 64)
42
- if err != nil {
43
- return err
44
- }
45
- *f = Fingerprint(num)
46
- return nil
47
-}
48
-
49
-// Fingerprints represents a collection of Fingerprint subject to a given
50
-// natural sorting scheme. It implements sort.Interface.
51
-type Fingerprints []Fingerprint
52
-
53
-// Len implements sort.Interface.
54
-func (f Fingerprints) Len() int {
55
- return len(f)
56
-}
57
-
58
-// Less implements sort.Interface.
59
-func (f Fingerprints) Less(i, j int) bool {
60
- return f[i] < f[j]
61
-}
62
-
63
-// Swap implements sort.Interface.
64
-func (f Fingerprints) Swap(i, j int) {
65
- f[i], f[j] = f[j], f[i]
66
-}
67
-
68
-// FingerprintSet is a set of Fingerprints.
69
-type FingerprintSet map[Fingerprint]struct{}
70
-
71
-// Equal returns true if both sets contain the same elements (and not more).
72
-func (s FingerprintSet) Equal(o FingerprintSet) bool {
73
- if len(s) != len(o) {
74
- return false
75
- }
76
-
77
- for k := range s {
78
- if _, ok := o[k]; !ok {
79
- return false
80
- }
81
- }
82
-
83
- return true
84
-}
85
-
86
-// Intersection returns the elements contained in both sets.
87
-func (s FingerprintSet) Intersection(o FingerprintSet) FingerprintSet {
88
- myLength, otherLength := len(s), len(o)
89
- if myLength == 0 || otherLength == 0 {
90
- return FingerprintSet{}
91
- }
92
-
93
- subSet := s
94
- superSet := o
95
-
96
- if otherLength < myLength {
97
- subSet = o
98
- superSet = s
99
- }
100
-
101
- out := FingerprintSet{}
102
-
103
- for k := range subSet {
104
- if _, ok := superSet[k]; ok {
105
- out[k] = struct{}{}
106
- }
107
- }
108
-
109
- return out
110
-}
Godeps/_workspace/src/github.com/prometheus/client_golang/model/labelname.go
deleted
-119
@@ -1,119 +0,0 @@
1
-// Copyright 2013 The Prometheus Authors
2
-// Licensed under the Apache License, Version 2.0 (the "License");
3
-// you may not use this file except in compliance with the License.
4
-// You may obtain a copy of the License at
5
-//
6
-// http://www.apache.org/licenses/LICENSE-2.0
7
-//
8
-// Unless required by applicable law or agreed to in writing, software
9
-// distributed under the License is distributed on an "AS IS" BASIS,
10
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
-// See the License for the specific language governing permissions and
12
-// limitations under the License.
13
-
14
-package model
15
-
16
-import (
17
- "encoding/json"
18
- "fmt"
19
- "regexp"
20
- "strings"
21
-)
22
-
23
-const (
24
- // ExporterLabelPrefix is the label name prefix to prepend if a
25
- // synthetic label is already present in the exported metrics.
26
- ExporterLabelPrefix LabelName = "exporter_"
27
-
28
- // MetricNameLabel is the label name indicating the metric name of a
29
- // timeseries.
30
- MetricNameLabel LabelName = "__name__"
31
-
32
- // AddressLabel is the name of the label that holds the address of
33
- // a scrape target.
34
- AddressLabel LabelName = "__address__"
35
-
36
- // MetricsPathLabel is the name of the label that holds the path on which to
37
- // scrape a target.
38
- MetricsPathLabel LabelName = "__metrics_path__"
39
-
40
- // ReservedLabelPrefix is a prefix which is not legal in user-supplied
41
- // label names.
42
- ReservedLabelPrefix = "__"
43
-
44
- // MetaLabelPrefix is a prefix for labels that provide meta information.
45
- // Labels with this prefix are used for intermediate label processing and
46
- // will not be attached to time series.
47
- MetaLabelPrefix = "__meta_"
48
-
49
- // JobLabel is the label name indicating the job from which a timeseries
50
- // was scraped.
51
- JobLabel LabelName = "job"
52
-
53
- // InstanceLabel is the label name used for the instance label.
54
- InstanceLabel LabelName = "instance"
55
-
56
- // BucketLabel is used for the label that defines the upper bound of a
57
- // bucket of a histogram ("le" -> "less or equal").
58
- BucketLabel = "le"
59
-
60
- // QuantileLabel is used for the label that defines the quantile in a
61
- // summary.
62
- QuantileLabel = "quantile"
63
-)
64
-
65
-// LabelNameRE is a regular expression matching valid label names.
66
-var LabelNameRE = regexp.MustCompile("^[a-zA-Z_][a-zA-Z0-9_]*$")
67
-
68
-// A LabelName is a key for a LabelSet or Metric. It has a value associated
69
-// therewith.
70
-type LabelName string
71
-
72
-// UnmarshalYAML implements the yaml.Unmarshaler interface.
73
-func (ln *LabelName) UnmarshalYAML(unmarshal func(interface{}) error) error {
74
- var s string
75
- if err := unmarshal(&s); err != nil {
76
- return err
77
- }
78
- if !LabelNameRE.MatchString(s) {
79
- return fmt.Errorf("%q is not a valid label name", s)
80
- }
81
- *ln = LabelName(s)
82
- return nil
83
-}
84
-
85
-// UnmarshalJSON implements the json.Unmarshaler interface.
86
-func (ln *LabelName) UnmarshalJSON(b []byte) error {
87
- var s string
88
- if err := json.Unmarshal(b, &s); err != nil {
89
- return err
90
- }
91
- if !LabelNameRE.MatchString(s) {
92
- return fmt.Errorf("%q is not a valid label name", s)
93
- }
94
- *ln = LabelName(s)
95
- return nil
96
-}
97
-
98
-// LabelNames is a sortable LabelName slice. In implements sort.Interface.
99
-type LabelNames []LabelName
100
-
101
-func (l LabelNames) Len() int {
102
- return len(l)
103
-}
104
-
105
-func (l LabelNames) Less(i, j int) bool {
106
- return l[i] < l[j]
107
-}
108
-
109
-func (l LabelNames) Swap(i, j int) {
110
- l[i], l[j] = l[j], l[i]
111
-}
112
-
113
-func (l LabelNames) String() string {
114
- labelStrings := make([]string, 0, len(l))
115
- for _, label := range l {
116
- labelStrings = append(labelStrings, string(label))
117
- }
118
- return strings.Join(labelStrings, ", ")
119
-}
Godeps/_workspace/src/github.com/prometheus/client_golang/model/labelname_test.go
deleted
-55
@@ -1,55 +0,0 @@
1
-// Copyright 2013 The Prometheus Authors
2
-// Licensed under the Apache License, Version 2.0 (the "License");
3
-// you may not use this file except in compliance with the License.
4
-// You may obtain a copy of the License at
5
-//
6
-// http://www.apache.org/licenses/LICENSE-2.0
7
-//
8
-// Unless required by applicable law or agreed to in writing, software
9
-// distributed under the License is distributed on an "AS IS" BASIS,
10
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
-// See the License for the specific language governing permissions and
12
-// limitations under the License.
13
-
14
-package model
15
-
16
-import (
17
- "sort"
18
- "testing"
19
-)
20
-
21
-func testLabelNames(t testing.TB) {
22
- var scenarios = []struct {
23
- in LabelNames
24
- out LabelNames
25
- }{
26
- {
27
- in: LabelNames{"ZZZ", "zzz"},
28
- out: LabelNames{"ZZZ", "zzz"},
29
- },
30
- {
31
- in: LabelNames{"aaa", "AAA"},
32
- out: LabelNames{"AAA", "aaa"},
33
- },
34
- }
35
-
36
- for i, scenario := range scenarios {
37
- sort.Sort(scenario.in)
38
-
39
- for j, expected := range scenario.out {
40
- if expected != scenario.in[j] {
41
- t.Errorf("%d.%d expected %s, got %s", i, j, expected, scenario.in[j])
42
- }
43
- }
44
- }
45
-}
46
-
47
-func TestLabelNames(t *testing.T) {
48
- testLabelNames(t)
49
-}
50
-
51
-func BenchmarkLabelNames(b *testing.B) {
52
- for i := 0; i < b.N; i++ {
53
- testLabelNames(b)
54
- }
55
-}
Godeps/_workspace/src/github.com/prometheus/client_golang/model/labelset.go
deleted
-83
@@ -1,83 +0,0 @@
1
-// Copyright 2013 The Prometheus Authors
2
-// Licensed under the Apache License, Version 2.0 (the "License");
3
-// you may not use this file except in compliance with the License.
4
-// You may obtain a copy of the License at
5
-//
6
-// http://www.apache.org/licenses/LICENSE-2.0
7
-//
8
-// Unless required by applicable law or agreed to in writing, software
9
-// distributed under the License is distributed on an "AS IS" BASIS,
10
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
-// See the License for the specific language governing permissions and
12
-// limitations under the License.
13
-
14
-package model
15
-
16
-import (
17
- "encoding/json"
18
- "fmt"
19
- "sort"
20
- "strings"
21
-)
22
-
23
-// A LabelSet is a collection of LabelName and LabelValue pairs. The LabelSet
24
-// may be fully-qualified down to the point where it may resolve to a single
25
-// Metric in the data store or not. All operations that occur within the realm
26
-// of a LabelSet can emit a vector of Metric entities to which the LabelSet may
27
-// match.
28
-type LabelSet map[LabelName]LabelValue
29
-
30
-// Merge is a helper function to non-destructively merge two label sets.
31
-func (l LabelSet) Merge(other LabelSet) LabelSet {
32
- result := make(LabelSet, len(l))
33
-
34
- for k, v := range l {
35
- result[k] = v
36
- }
37
-
38
- for k, v := range other {
39
- result[k] = v
40
- }
41
-
42
- return result
43
-}
44
-
45
-func (l LabelSet) String() string {
46
- labelStrings := make([]string, 0, len(l))
47
- for label, value := range l {
48
- labelStrings = append(labelStrings, fmt.Sprintf("%s=%q", label, value))
49
- }
50
-
51
- switch len(labelStrings) {
52
- case 0:
53
- return ""
54
- default:
55
- sort.Strings(labelStrings)
56
- return fmt.Sprintf("{%s}", strings.Join(labelStrings, ", "))
57
- }
58
-}
59
-
60
-// MergeFromMetric merges Metric into this LabelSet.
61
-func (l LabelSet) MergeFromMetric(m Metric) {
62
- for k, v := range m {
63
- l[k] = v
64
- }
65
-}
66
-
67
-// UnmarshalJSON implements the json.Unmarshaler interface.
68
-func (l *LabelSet) UnmarshalJSON(b []byte) error {
69
- var m map[LabelName]LabelValue
70
- if err := json.Unmarshal(b, &m); err != nil {
71
- return err
72
- }
73
- // encoding/json only unmarshals maps of the form map[string]T. It treats
74
- // LabelName as a string and does not call its UnmarshalJSON method.
75
- // Thus, we have to replicate the behavior here.
76
- for ln := range m {
77
- if !LabelNameRE.MatchString(string(ln)) {
78
- return fmt.Errorf("%q is not a valid label name", ln)
79
- }
80
- }
81
- *l = LabelSet(m)
82
- return nil
83
-}
Godeps/_workspace/src/github.com/prometheus/client_golang/model/labelvalue.go
deleted
-36
@@ -1,36 +0,0 @@
1
-// Copyright 2013 The Prometheus Authors
2
-// Licensed under the Apache License, Version 2.0 (the "License");
3
-// you may not use this file except in compliance with the License.
4
-// You may obtain a copy of the License at
5
-//
6
-// http://www.apache.org/licenses/LICENSE-2.0
7
-//
8
-// Unless required by applicable law or agreed to in writing, software
9
-// distributed under the License is distributed on an "AS IS" BASIS,
10
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
-// See the License for the specific language governing permissions and
12
-// limitations under the License.
13
-
14
-package model
15
-
16
-import (
17
- "sort"
18
-)
19
-
20
-// A LabelValue is an associated value for a LabelName.
21
-type LabelValue string
22
-
23
-// LabelValues is a sortable LabelValue slice. It implements sort.Interface.
24
-type LabelValues []LabelValue
25
-
26
-func (l LabelValues) Len() int {
27
- return len(l)
28
-}
29
-
30
-func (l LabelValues) Less(i, j int) bool {
31
- return sort.StringsAreSorted([]string{string(l[i]), string(l[j])})
32
-}
33
-
34
-func (l LabelValues) Swap(i, j int) {
35
- l[i], l[j] = l[j], l[i]
36
-}
Godeps/_workspace/src/github.com/prometheus/client_golang/model/labelvalue_test.go
deleted
-55
@@ -1,55 +0,0 @@
1
-// Copyright 2013 The Prometheus Authors
2
-// Licensed under the Apache License, Version 2.0 (the "License");
3
-// you may not use this file except in compliance with the License.
4
-// You may obtain a copy of the License at
5
-//
6
-// http://www.apache.org/licenses/LICENSE-2.0
7
-//
8
-// Unless required by applicable law or agreed to in writing, software
9
-// distributed under the License is distributed on an "AS IS" BASIS,
10
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
-// See the License for the specific language governing permissions and
12
-// limitations under the License.
13
-
14
-package model
15
-
16
-import (
17
- "sort"
18
- "testing"
19
-)
20
-
21
-func testLabelValues(t testing.TB) {
22
- var scenarios = []struct {
23
- in LabelValues
24
- out LabelValues
25
- }{
26
- {
27
- in: LabelValues{"ZZZ", "zzz"},
28
- out: LabelValues{"ZZZ", "zzz"},
29
- },
30
- {
31
- in: LabelValues{"aaa", "AAA"},
32
- out: LabelValues{"AAA", "aaa"},
33
- },
34
- }
35
-
36
- for i, scenario := range scenarios {
37
- sort.Sort(scenario.in)
38
-
39
- for j, expected := range scenario.out {
40
- if expected != scenario.in[j] {
41
- t.Errorf("%d.%d expected %s, got %s", i, j, expected, scenario.in[j])
42
- }
43
- }
44
- }
45
-}
46
-
47
-func TestLabelValues(t *testing.T) {
48
- testLabelValues(t)
49
-}
50
-
51
-func BenchmarkLabelValues(b *testing.B) {
52
- for i := 0; i < b.N; i++ {
53
- testLabelValues(b)
54
- }
55
-}
Godeps/_workspace/src/github.com/prometheus/client_golang/model/metric.go
deleted
-192
@@ -1,192 +0,0 @@
1
-// Copyright 2013 The Prometheus Authors
2
-// Licensed under the Apache License, Version 2.0 (the "License");
3
-// you may not use this file except in compliance with the License.
4
-// You may obtain a copy of the License at
5
-//
6
-// http://www.apache.org/licenses/LICENSE-2.0
7
-//
8
-// Unless required by applicable law or agreed to in writing, software
9
-// distributed under the License is distributed on an "AS IS" BASIS,
10
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
-// See the License for the specific language governing permissions and
12
-// limitations under the License.
13
-
14
-package model
15
-
16
-import (
17
- "encoding/json"
18
- "fmt"
19
- "sort"
20
- "strings"
21
-)
22
-
23
-var separator = []byte{0}
24
-
25
-// A Metric is similar to a LabelSet, but the key difference is that a Metric is
26
-// a singleton and refers to one and only one stream of samples.
27
-type Metric map[LabelName]LabelValue
28
-
29
-// Equal compares the metrics.
30
-func (m Metric) Equal(o Metric) bool {
31
- if len(m) != len(o) {
32
- return false
33
- }
34
- for ln, lv := range m {
35
- olv, ok := o[ln]
36
- if !ok {
37
- return false
38
- }
39
- if olv != lv {
40
- return false
41
- }
42
- }
43
- return true
44
-}
45
-
46
-// Before compares the metrics, using the following criteria:
47
-//
48
-// If m has fewer labels than o, it is before o. If it has more, it is not.
49
-//
50
-// If the number of labels is the same, the superset of all label names is
51
-// sorted alphanumerically. The first differing label pair found in that order
52
-// determines the outcome: If the label does not exist at all in m, then m is
53
-// before o, and vice versa. Otherwise the label value is compared
54
-// alphanumerically.
55
-//
56
-// If m and o are equal, the method returns false.
57
-func (m Metric) Before(o Metric) bool {
58
- if len(m) < len(o) {
59
- return true
60
- }
61
- if len(m) > len(o) {
62
- return false
63
- }
64
-
65
- lns := make(LabelNames, 0, len(m)+len(o))
66
- for ln := range m {
67
- lns = append(lns, ln)
68
- }
69
- for ln := range o {
70
- lns = append(lns, ln)
71
- }
72
- // It's probably not worth it to de-dup lns.
73
- sort.Sort(lns)
74
- for _, ln := range lns {
75
- mlv, ok := m[ln]
76
- if !ok {
77
- return true
78
- }
79
- olv, ok := o[ln]
80
- if !ok {
81
- return false
82
- }
83
- if mlv < olv {
84
- return true
85
- }
86
- if mlv > olv {
87
- return false
88
- }
89
- }
90
- return false
91
-}
92
-
93
-// String implements Stringer.
94
-func (m Metric) String() string {
95
- metricName, hasName := m[MetricNameLabel]
96
- numLabels := len(m) - 1
97
- if !hasName {
98
- numLabels = len(m)
99
- }
100
- labelStrings := make([]string, 0, numLabels)
101
- for label, value := range m {
102
- if label != MetricNameLabel {
103
- labelStrings = append(labelStrings, fmt.Sprintf("%s=%q", label, value))
104
- }
105
- }
106
-
107
- switch numLabels {
108
- case 0:
109
- if hasName {
110
- return string(metricName)
111
- }
112
- return "{}"
113
- default:
114
- sort.Strings(labelStrings)
115
- return fmt.Sprintf("%s{%s}", metricName, strings.Join(labelStrings, ", "))
116
- }
117
-}
118
-
119
-// Fingerprint returns a Metric's Fingerprint.
120
-func (m Metric) Fingerprint() Fingerprint {
121
- return metricToFingerprint(m)
122
-}
123
-
124
-// FastFingerprint returns a Metric's Fingerprint calculated by a faster hashing
125
-// algorithm, which is, however, more susceptible to hash collisions.
126
-func (m Metric) FastFingerprint() Fingerprint {
127
- return metricToFastFingerprint(m)
128
-}
129
-
130
-// Clone returns a copy of the Metric.
131
-func (m Metric) Clone() Metric {
132
- clone := Metric{}
133
- for k, v := range m {
134
- clone[k] = v
135
- }
136
- return clone
137
-}
138
-
139
-// MergeFromLabelSet merges a label set into this Metric, prefixing a collision
140
-// prefix to the label names merged from the label set where required.
141
-func (m Metric) MergeFromLabelSet(labels LabelSet, collisionPrefix LabelName) {
142
- for k, v := range labels {
143
- if collisionPrefix != "" {
144
- for {
145
- if _, exists := m[k]; !exists {
146
- break
147
- }
148
- k = collisionPrefix + k
149
- }
150
- }
151
-
152
- m[k] = v
153
- }
154
-}
155
-
156
-// COWMetric wraps a Metric to enable copy-on-write access patterns.
157
-type COWMetric struct {
158
- Copied bool
159
- Metric Metric
160
-}
161
-
162
-// Set sets a label name in the wrapped Metric to a given value and copies the
163
-// Metric initially, if it is not already a copy.
164
-func (m *COWMetric) Set(ln LabelName, lv LabelValue) {
165
- m.doCOW()
166
- m.Metric[ln] = lv
167
-}
168
-
169
-// Delete deletes a given label name from the wrapped Metric and copies the
170
-// Metric initially, if it is not already a copy.
171
-func (m *COWMetric) Delete(ln LabelName) {
172
- m.doCOW()
173
- delete(m.Metric, ln)
174
-}
175
-
176
-// doCOW copies the underlying Metric if it is not already a copy.
177
-func (m *COWMetric) doCOW() {
178
- if !m.Copied {
179
- m.Metric = m.Metric.Clone()
180
- m.Copied = true
181
- }
182
-}
183
-
184
-// String implements fmt.Stringer.
185
-func (m COWMetric) String() string {
186
- return m.Metric.String()
187
-}
188
-
189
-// MarshalJSON implements json.Marshaler.
190
-func (m COWMetric) MarshalJSON() ([]byte, error) {
191
- return json.Marshal(m.Metric)
192
-}
Godeps/_workspace/src/github.com/prometheus/client_golang/model/metric_test.go
deleted
-130
@@ -1,130 +0,0 @@
1
-// Copyright 2013 The Prometheus Authors
2
-// Licensed under the Apache License, Version 2.0 (the "License");
3
-// you may not use this file except in compliance with the License.
4
-// You may obtain a copy of the License at
5
-//
6
-// http://www.apache.org/licenses/LICENSE-2.0
7
-//
8
-// Unless required by applicable law or agreed to in writing, software
9
-// distributed under the License is distributed on an "AS IS" BASIS,
10
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
-// See the License for the specific language governing permissions and
12
-// limitations under the License.
13
-
14
-package model
15
-
16
-import "testing"
17
-
18
-func testMetric(t testing.TB) {
19
- var scenarios = []struct {
20
- input Metric
21
- fingerprint Fingerprint
22
- fastFingerprint Fingerprint
23
- }{
24
- {
25
- input: Metric{},
26
- fingerprint: 14695981039346656037,
27
- fastFingerprint: 14695981039346656037,
28
- },
29
- {
30
- input: Metric{
31
- "first_name": "electro",
32
- "occupation": "robot",
33
- "manufacturer": "westinghouse",
34
- },
35
- fingerprint: 5911716720268894962,
36
- fastFingerprint: 11310079640881077873,
37
- },
38
- {
39
- input: Metric{
40
- "x": "y",
41
- },
42
- fingerprint: 8241431561484471700,
43
- fastFingerprint: 13948396922932177635,
44
- },
45
- {
46
- input: Metric{
47
- "a": "bb",
48
- "b": "c",
49
- },
50
- fingerprint: 3016285359649981711,
51
- fastFingerprint: 3198632812309449502,
52
- },
53
- {
54
- input: Metric{
55
- "a": "b",
56
- "bb": "c",
57
- },
58
- fingerprint: 7122421792099404749,
59
- fastFingerprint: 5774953389407657638,
60
- },
61
- }
62
-
63
- for i, scenario := range scenarios {
64
- if scenario.fingerprint != scenario.input.Fingerprint() {
65
- t.Errorf("%d. expected %d, got %d", i, scenario.fingerprint, scenario.input.Fingerprint())
66
- }
67
- if scenario.fastFingerprint != scenario.input.FastFingerprint() {
68
- t.Errorf("%d. expected %d, got %d", i, scenario.fastFingerprint, scenario.input.FastFingerprint())
69
- }
70
- }
71
-}
72
-
73
-func TestMetric(t *testing.T) {
74
- testMetric(t)
75
-}
76
-
77
-func BenchmarkMetric(b *testing.B) {
78
- for i := 0; i < b.N; i++ {
79
- testMetric(b)
80
- }
81
-}
82
-
83
-func TestCOWMetric(t *testing.T) {
84
- testMetric := Metric{
85
- "to_delete": "test1",
86
- "to_change": "test2",
87
- }
88
-
89
- scenarios := []struct {
90
- fn func(*COWMetric)
91
- out Metric
92
- }{
93
- {
94
- fn: func(cm *COWMetric) {
95
- cm.Delete("to_delete")
96
- },
97
- out: Metric{
98
- "to_change": "test2",
99
- },
100
- },
101
- {
102
- fn: func(cm *COWMetric) {
103
- cm.Set("to_change", "changed")
104
- },
105
- out: Metric{
106
- "to_delete": "test1",
107
- "to_change": "changed",
108
- },
109
- },
110
- }
111
-
112
- for i, s := range scenarios {
113
- orig := testMetric.Clone()
114
- cm := &COWMetric{
115
- Metric: orig,
116
- }
117
-
118
- s.fn(cm)
119
-
120
- // Test that the original metric was not modified.
121
- if !orig.Equal(testMetric) {
122
- t.Fatalf("%d. original metric changed; expected %v, got %v", i, testMetric, orig)
123
- }
124
-
125
- // Test that the new metric has the right changes.
126
- if !cm.Metric.Equal(s.out) {
127
- t.Fatalf("%d. copied metric doesn't contain expected changes; expected %v, got %v", i, s.out, cm.Metric)
128
- }
129
- }
130
-}
Godeps/_workspace/src/github.com/prometheus/client_golang/model/model.go
deleted
-15
@@ -1,15 +0,0 @@
1
-// Copyright 2013 The Prometheus Authors
2
-// Licensed under the Apache License, Version 2.0 (the "License");
3
-// you may not use this file except in compliance with the License.
4
-// You may obtain a copy of the License at
5
-//
6
-// http://www.apache.org/licenses/LICENSE-2.0
7
-//
8
-// Unless required by applicable law or agreed to in writing, software
9
-// distributed under the License is distributed on an "AS IS" BASIS,
10
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
-// See the License for the specific language governing permissions and
12
-// limitations under the License.
13
-
14
-// Package model contains core representation of Prometheus client primitives.
15
-package model
Godeps/_workspace/src/github.com/prometheus/client_golang/model/sample.go
deleted
-79
@@ -1,79 +0,0 @@
1
-// Copyright 2013 The Prometheus Authors
2
-// Licensed under the Apache License, Version 2.0 (the "License");
3
-// you may not use this file except in compliance with the License.
4
-// You may obtain a copy of the License at
5
-//
6
-// http://www.apache.org/licenses/LICENSE-2.0
7
-//
8
-// Unless required by applicable law or agreed to in writing, software
9
-// distributed under the License is distributed on an "AS IS" BASIS,
10
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
-// See the License for the specific language governing permissions and
12
-// limitations under the License.
13
-
14
-package model
15
-
16
-// Sample is a sample value with a timestamp and a metric.
17
-type Sample struct {
18
- Metric Metric
19
- Value SampleValue
20
- Timestamp Timestamp
21
-}
22
-
23
-// Equal compares first the metrics, then the timestamp, then the value.
24
-func (s *Sample) Equal(o *Sample) bool {
25
- if s == o {
26
- return true
27
- }
28
-
29
- if !s.Metric.Equal(o.Metric) {
30
- return false
31
- }
32
- if !s.Timestamp.Equal(o.Timestamp) {
33
- return false
34
- }
35
- if !s.Value.Equal(o.Value) {
36
- return false
37
- }
38
-
39
- return true
40
-}
41
-
42
-// Samples is a sortable Sample slice. It implements sort.Interface.
43
-type Samples []*Sample
44
-
45
-func (s Samples) Len() int {
46
- return len(s)
47
-}
48
-
49
-// Less compares first the metrics, then the timestamp.
50
-func (s Samples) Less(i, j int) bool {
51
- switch {
52
- case s[i].Metric.Before(s[j].Metric):
53
- return true
54
- case s[j].Metric.Before(s[i].Metric):
55
- return false
56
- case s[i].Timestamp.Before(s[j].Timestamp):
57
- return true
58
- default:
59
- return false
60
- }
61
-}
62
-
63
-func (s Samples) Swap(i, j int) {
64
- s[i], s[j] = s[j], s[i]
65
-}
66
-
67
-// Equal compares two sets of samples and returns true if they are equal.
68
-func (s Samples) Equal(o Samples) bool {
69
- if len(s) != len(o) {
70
- return false
71
- }
72
-
73
- for i, sample := range s {
74
- if !sample.Equal(o[i]) {
75
- return false
76
- }
77
- }
78
- return true
79
-}
Godeps/_workspace/src/github.com/prometheus/client_golang/model/sample_test.go
deleted
-114
@@ -1,114 +0,0 @@
1
-// Copyright 2013 The Prometheus Authors
2
-// Licensed under the Apache License, Version 2.0 (the "License");
3
-// you may not use this file except in compliance with the License.
4
-// You may obtain a copy of the License at
5
-//
6
-// http://www.apache.org/licenses/LICENSE-2.0
7
-//
8
-// Unless required by applicable law or agreed to in writing, software
9
-// distributed under the License is distributed on an "AS IS" BASIS,
10
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
-// See the License for the specific language governing permissions and
12
-// limitations under the License.
13
-
14
-package model
15
-
16
-import (
17
- "sort"
18
- "testing"
19
-)
20
-
21
-func TestSamplesSort(t *testing.T) {
22
- input := Samples{
23
- &Sample{
24
- Metric: Metric{
25
- MetricNameLabel: "A",
26
- },
27
- Timestamp: 1,
28
- },
29
- &Sample{
30
- Metric: Metric{
31
- MetricNameLabel: "A",
32
- },
33
- Timestamp: 2,
34
- },
35
- &Sample{
36
- Metric: Metric{
37
- MetricNameLabel: "C",
38
- },
39
- Timestamp: 1,
40
- },
41
- &Sample{
42
- Metric: Metric{
43
- MetricNameLabel: "C",
44
- },
45
- Timestamp: 2,
46
- },
47
- &Sample{
48
- Metric: Metric{
49
- MetricNameLabel: "B",
50
- },
51
- Timestamp: 1,
52
- },
53
- &Sample{
54
- Metric: Metric{
55
- MetricNameLabel: "B",
56
- },
57
- Timestamp: 2,
58
- },
59
- }
60
-
61
- expected := Samples{
62
- &Sample{
63
- Metric: Metric{
64
- MetricNameLabel: "A",
65
- },
66
- Timestamp: 1,
67
- },
68
- &Sample{
69
- Metric: Metric{
70
- MetricNameLabel: "A",
71
- },
72
- Timestamp: 2,
73
- },
74
- &Sample{
75
- Metric: Metric{
76
- MetricNameLabel: "B",
77
- },
78
- Timestamp: 1,
79
- },
80
- &Sample{
81
- Metric: Metric{
82
- MetricNameLabel: "B",
83
- },
84
- Timestamp: 2,
85
- },
86
- &Sample{
87
- Metric: Metric{
88
- MetricNameLabel: "C",
89
- },
90
- Timestamp: 1,
91
- },
92
- &Sample{
93
- Metric: Metric{
94
- MetricNameLabel: "C",
95
- },
96
- Timestamp: 2,
97
- },
98
- }
99
-
100
- sort.Sort(input)
101
-
102
- for i, actual := range input {
103
- actualFp := actual.Metric.Fingerprint()
104
- expectedFp := expected[i].Metric.Fingerprint()
105
-
106
- if !actualFp.Equal(expectedFp) {
107
- t.Fatalf("%d. Incorrect fingerprint. Got %s; want %s", i, actualFp.String(), expectedFp.String())
108
- }
109
-
110
- if actual.Timestamp != expected[i].Timestamp {
111
- t.Fatalf("%d. Incorrect timestamp. Got %s; want %s", i, actual.Timestamp, expected[i].Timestamp)
112
- }
113
- }
114
-}
Godeps/_workspace/src/github.com/prometheus/client_golang/model/samplevalue.go
deleted
-37
@@ -1,37 +0,0 @@
1
-// Copyright 2013 The Prometheus Authors
2
-// Licensed under the Apache License, Version 2.0 (the "License");
3
-// you may not use this file except in compliance with the License.
4
-// You may obtain a copy of the License at
5
-//
6
-// http://www.apache.org/licenses/LICENSE-2.0
7
-//
8
-// Unless required by applicable law or agreed to in writing, software
9
-// distributed under the License is distributed on an "AS IS" BASIS,
10
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
-// See the License for the specific language governing permissions and
12
-// limitations under the License.
13
-
14
-package model
15
-
16
-import (
17
- "fmt"
18
- "strconv"
19
-)
20
-
21
-// A SampleValue is a representation of a value for a given sample at a given
22
-// time.
23
-type SampleValue float64
24
-
25
-// Equal does a straight v==o.
26
-func (v SampleValue) Equal(o SampleValue) bool {
27
- return v == o
28
-}
29
-
30
-// MarshalJSON implements json.Marshaler.
31
-func (v SampleValue) MarshalJSON() ([]byte, error) {
32
- return []byte(fmt.Sprintf(`"%s"`, v)), nil
33
-}
34
-
35
-func (v SampleValue) String() string {
36
- return strconv.FormatFloat(float64(v), 'f', -1, 64)
37
-}
Godeps/_workspace/src/github.com/prometheus/client_golang/model/signature.go
deleted
-190
@@ -1,190 +0,0 @@
1
-// Copyright 2014 The Prometheus Authors
2
-// Licensed under the Apache License, Version 2.0 (the "License");
3
-// you may not use this file except in compliance with the License.
4
-// You may obtain a copy of the License at
5
-//
6
-// http://www.apache.org/licenses/LICENSE-2.0
7
-//
8
-// Unless required by applicable law or agreed to in writing, software
9
-// distributed under the License is distributed on an "AS IS" BASIS,
10
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
-// See the License for the specific language governing permissions and
12
-// limitations under the License.
13
-
14
-package model
15
-
16
-import (
17
- "bytes"
18
- "hash"
19
- "hash/fnv"
20
- "sort"
21
- "sync"
22
-)
23
-
24
-// SeparatorByte is a byte that cannot occur in valid UTF-8 sequences and is
25
-// used to separate label names, label values, and other strings from each other
26
-// when calculating their combined hash value (aka signature aka fingerprint).
27
-const SeparatorByte byte = 255
28
-
29
-var (
30
- // cache the signature of an empty label set.
31
- emptyLabelSignature = fnv.New64a().Sum64()
32
-
33
- hashAndBufPool sync.Pool
34
-)
35
-
36
-type hashAndBuf struct {
37
- h hash.Hash64
38
- b bytes.Buffer
39
-}
40
-
41
-func getHashAndBuf() *hashAndBuf {
42
- hb := hashAndBufPool.Get()
43
- if hb == nil {
44
- return &hashAndBuf{h: fnv.New64a()}
45
- }
46
- return hb.(*hashAndBuf)
47
-}
48
-
49
-func putHashAndBuf(hb *hashAndBuf) {
50
- hb.h.Reset()
51
- hb.b.Reset()
52
- hashAndBufPool.Put(hb)
53
-}
54
-
55
-// LabelsToSignature returns a quasi-unique signature (i.e., fingerprint) for a
56
-// given label set. (Collisions are possible but unlikely if the number of label
57
-// sets the function is applied to is small.)
58
-func LabelsToSignature(labels map[string]string) uint64 {
59
- if len(labels) == 0 {
60
- return emptyLabelSignature
61
- }
62
-
63
- labelNames := make([]string, 0, len(labels))
64
- for labelName := range labels {
65
- labelNames = append(labelNames, labelName)
66
- }
67
- sort.Strings(labelNames)
68
-
69
- hb := getHashAndBuf()
70
- defer putHashAndBuf(hb)
71
-
72
- for _, labelName := range labelNames {
73
- hb.b.WriteString(labelName)
74
- hb.b.WriteByte(SeparatorByte)
75
- hb.b.WriteString(labels[labelName])
76
- hb.b.WriteByte(SeparatorByte)
77
- hb.h.Write(hb.b.Bytes())
78
- hb.b.Reset()
79
- }
80
- return hb.h.Sum64()
81
-}
82
-
83
-// metricToFingerprint works exactly as LabelsToSignature but takes a Metric as
84
-// parameter (rather than a label map) and returns a Fingerprint.
85
-func metricToFingerprint(m Metric) Fingerprint {
86
- if len(m) == 0 {
87
- return Fingerprint(emptyLabelSignature)
88
- }
89
-
90
- labelNames := make(LabelNames, 0, len(m))
91
- for labelName := range m {
92
- labelNames = append(labelNames, labelName)
93
- }
94
- sort.Sort(labelNames)
95
-
96
- hb := getHashAndBuf()
97
- defer putHashAndBuf(hb)
98
-
99
- for _, labelName := range labelNames {
100
- hb.b.WriteString(string(labelName))
101
- hb.b.WriteByte(SeparatorByte)
102
- hb.b.WriteString(string(m[labelName]))
103
- hb.b.WriteByte(SeparatorByte)
104
- hb.h.Write(hb.b.Bytes())
105
- hb.b.Reset()
106
- }
107
- return Fingerprint(hb.h.Sum64())
108
-}
109
-
110
-// metricToFastFingerprint works similar to metricToFingerprint but uses a
111
-// faster and less allocation-heavy hash function, which is more susceptible to
112
-// create hash collisions. Therefore, collision detection should be applied.
113
-func metricToFastFingerprint(m Metric) Fingerprint {
114
- if len(m) == 0 {
115
- return Fingerprint(emptyLabelSignature)
116
- }
117
-
118
- var result uint64
119
- hb := getHashAndBuf()
120
- defer putHashAndBuf(hb)
121
-
122
- for labelName, labelValue := range m {
123
- hb.b.WriteString(string(labelName))
124
- hb.b.WriteByte(SeparatorByte)
125
- hb.b.WriteString(string(labelValue))
126
- hb.h.Write(hb.b.Bytes())
127
- result ^= hb.h.Sum64()
128
- hb.h.Reset()
129
- hb.b.Reset()
130
- }
131
- return Fingerprint(result)
132
-}
133
-
134
-// SignatureForLabels works like LabelsToSignature but takes a Metric as
135
-// parameter (rather than a label map) and only includes the labels with the
136
-// specified LabelNames into the signature calculation. The labels passed in
137
-// will be sorted by this function.
138
-func SignatureForLabels(m Metric, labels LabelNames) uint64 {
139
- if len(m) == 0 || len(labels) == 0 {
140
- return emptyLabelSignature
141
- }
142
-
143
- sort.Sort(labels)
144
-
145
- hb := getHashAndBuf()
146
- defer putHashAndBuf(hb)
147
-
148
- for _, label := range labels {
149
- hb.b.WriteString(string(label))
150
- hb.b.WriteByte(SeparatorByte)
151
- hb.b.WriteString(string(m[label]))
152
- hb.b.WriteByte(SeparatorByte)
153
- hb.h.Write(hb.b.Bytes())
154
- hb.b.Reset()
155
- }
156
- return hb.h.Sum64()
157
-}
158
-
159
-// SignatureWithoutLabels works like LabelsToSignature but takes a Metric as
160
-// parameter (rather than a label map) and excludes the labels with any of the
161
-// specified LabelNames from the signature calculation.
162
-func SignatureWithoutLabels(m Metric, labels map[LabelName]struct{}) uint64 {
163
- if len(m) == 0 {
164
- return emptyLabelSignature
165
- }
166
-
167
- labelNames := make(LabelNames, 0, len(m))
168
- for labelName := range m {
169
- if _, exclude := labels[labelName]; !exclude {
170
- labelNames = append(labelNames, labelName)
171
- }
172
- }
173
- if len(labelNames) == 0 {
174
- return emptyLabelSignature
175
- }
176
- sort.Sort(labelNames)
177
-
178
- hb := getHashAndBuf()
179
- defer putHashAndBuf(hb)
180
-
181
- for _, labelName := range labelNames {
182
- hb.b.WriteString(string(labelName))
183
- hb.b.WriteByte(SeparatorByte)
184
- hb.b.WriteString(string(m[labelName]))
185
- hb.b.WriteByte(SeparatorByte)
186
- hb.h.Write(hb.b.Bytes())
187
- hb.b.Reset()
188
- }
189
- return hb.h.Sum64()
190
-}
Godeps/_workspace/src/github.com/prometheus/client_golang/model/signature_test.go
deleted
-304
@@ -1,304 +0,0 @@
1
-// Copyright 2014 The Prometheus Authors
2
-// Licensed under the Apache License, Version 2.0 (the "License");
3
-// you may not use this file except in compliance with the License.
4
-// You may obtain a copy of the License at
5
-//
6
-// http://www.apache.org/licenses/LICENSE-2.0
7
-//
8
-// Unless required by applicable law or agreed to in writing, software
9
-// distributed under the License is distributed on an "AS IS" BASIS,
10
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
-// See the License for the specific language governing permissions and
12
-// limitations under the License.
13
-
14
-package model
15
-
16
-import (
17
- "runtime"
18
- "sync"
19
- "testing"
20
-)
21
-
22
-func TestLabelsToSignature(t *testing.T) {
23
- var scenarios = []struct {
24
- in map[string]string
25
- out uint64
26
- }{
27
- {
28
- in: map[string]string{},
29
- out: 14695981039346656037,
30
- },
31
- {
32
- in: map[string]string{"name": "garland, briggs", "fear": "love is not enough"},
33
- out: 5799056148416392346,
34
- },
35
- }
36
-
37
- for i, scenario := range scenarios {
38
- actual := LabelsToSignature(scenario.in)
39
-
40
- if actual != scenario.out {
41
- t.Errorf("%d. expected %d, got %d", i, scenario.out, actual)
42
- }
43
- }
44
-}
45
-
46
-func TestMetricToFingerprint(t *testing.T) {
47
- var scenarios = []struct {
48
- in Metric
49
- out Fingerprint
50
- }{
51
- {
52
- in: Metric{},
53
- out: 14695981039346656037,
54
- },
55
- {
56
- in: Metric{"name": "garland, briggs", "fear": "love is not enough"},
57
- out: 5799056148416392346,
58
- },
59
- }
60
-
61
- for i, scenario := range scenarios {
62
- actual := metricToFingerprint(scenario.in)
63
-
64
- if actual != scenario.out {
65
- t.Errorf("%d. expected %d, got %d", i, scenario.out, actual)
66
- }
67
- }
68
-}
69
-
70
-func TestMetricToFastFingerprint(t *testing.T) {
71
- var scenarios = []struct {
72
- in Metric
73
- out Fingerprint
74
- }{
75
- {
76
- in: Metric{},
77
- out: 14695981039346656037,
78
- },
79
- {
80
- in: Metric{"name": "garland, briggs", "fear": "love is not enough"},
81
- out: 12952432476264840823,
82
- },
83
- }
84
-
85
- for i, scenario := range scenarios {
86
- actual := metricToFastFingerprint(scenario.in)
87
-
88
- if actual != scenario.out {
89
- t.Errorf("%d. expected %d, got %d", i, scenario.out, actual)
90
- }
91
- }
92
-}
93
-
94
-func TestSignatureForLabels(t *testing.T) {
95
- var scenarios = []struct {
96
- in Metric
97
- labels LabelNames
98
- out uint64
99
- }{
100
- {
101
- in: Metric{},
102
- labels: nil,
103
- out: 14695981039346656037,
104
- },
105
- {
106
- in: Metric{"name": "garland, briggs", "fear": "love is not enough"},
107
- labels: LabelNames{"fear", "name"},
108
- out: 5799056148416392346,
109
- },
110
- {
111
- in: Metric{"name": "garland, briggs", "fear": "love is not enough", "foo": "bar"},
112
- labels: LabelNames{"fear", "name"},
113
- out: 5799056148416392346,
114
- },
115
- {
116
- in: Metric{"name": "garland, briggs", "fear": "love is not enough"},
117
- labels: LabelNames{},
118
- out: 14695981039346656037,
119
- },
120
- {
121
- in: Metric{"name": "garland, briggs", "fear": "love is not enough"},
122
- labels: nil,
123
- out: 14695981039346656037,
124
- },
125
- }
126
-
127
- for i, scenario := range scenarios {
128
- actual := SignatureForLabels(scenario.in, scenario.labels)
129
-
130
- if actual != scenario.out {
131
- t.Errorf("%d. expected %d, got %d", i, scenario.out, actual)
132
- }
133
- }
134
-}
135
-
136
-func TestSignatureWithoutLabels(t *testing.T) {
137
- var scenarios = []struct {
138
- in Metric
139
- labels map[LabelName]struct{}
140
- out uint64
141
- }{
142
- {
143
- in: Metric{},
144
- labels: nil,
145
- out: 14695981039346656037,
146
- },
147
- {
148
- in: Metric{"name": "garland, briggs", "fear": "love is not enough"},
149
- labels: map[LabelName]struct{}{"fear": struct{}{}, "name": struct{}{}},
150
- out: 14695981039346656037,
151
- },
152
- {
153
- in: Metric{"name": "garland, briggs", "fear": "love is not enough", "foo": "bar"},
154
- labels: map[LabelName]struct{}{"foo": struct{}{}},
155
- out: 5799056148416392346,
156
- },
157
- {
158
- in: Metric{"name": "garland, briggs", "fear": "love is not enough"},
159
- labels: map[LabelName]struct{}{},
160
- out: 5799056148416392346,
161
- },
162
- {
163
- in: Metric{"name": "garland, briggs", "fear": "love is not enough"},
164
- labels: nil,
165
- out: 5799056148416392346,
166
- },
167
- }
168
-
169
- for i, scenario := range scenarios {
170
- actual := SignatureWithoutLabels(scenario.in, scenario.labels)
171
-
172
- if actual != scenario.out {
173
- t.Errorf("%d. expected %d, got %d", i, scenario.out, actual)
174
- }
175
- }
176
-}
177
-
178
-func benchmarkLabelToSignature(b *testing.B, l map[string]string, e uint64) {
179
- for i := 0; i < b.N; i++ {
180
- if a := LabelsToSignature(l); a != e {
181
- b.Fatalf("expected signature of %d for %s, got %d", e, l, a)
182
- }
183
- }
184
-}
185
-
186
-func BenchmarkLabelToSignatureScalar(b *testing.B) {
187
- benchmarkLabelToSignature(b, nil, 14695981039346656037)
188
-}
189
-
190
-func BenchmarkLabelToSignatureSingle(b *testing.B) {
191
- benchmarkLabelToSignature(b, map[string]string{"first-label": "first-label-value"}, 5146282821936882169)
192
-}
193
-
194
-func BenchmarkLabelToSignatureDouble(b *testing.B) {
195
- benchmarkLabelToSignature(b, map[string]string{"first-label": "first-label-value", "second-label": "second-label-value"}, 3195800080984914717)
196
-}
197
-
198
-func BenchmarkLabelToSignatureTriple(b *testing.B) {
199
- benchmarkLabelToSignature(b, map[string]string{"first-label": "first-label-value", "second-label": "second-label-value", "third-label": "third-label-value"}, 13843036195897128121)
200
-}
201
-
202
-func benchmarkMetricToFingerprint(b *testing.B, m Metric, e Fingerprint) {
203
- for i := 0; i < b.N; i++ {
204
- if a := metricToFingerprint(m); a != e {
205
- b.Fatalf("expected signature of %d for %s, got %d", e, m, a)
206
- }
207
- }
208
-}
209
-
210
-func BenchmarkMetricToFingerprintScalar(b *testing.B) {
211
- benchmarkMetricToFingerprint(b, nil, 14695981039346656037)
212
-}
213
-
214
-func BenchmarkMetricToFingerprintSingle(b *testing.B) {
215
- benchmarkMetricToFingerprint(b, Metric{"first-label": "first-label-value"}, 5146282821936882169)
216
-}
217
-
218
-func BenchmarkMetricToFingerprintDouble(b *testing.B) {
219
- benchmarkMetricToFingerprint(b, Metric{"first-label": "first-label-value", "second-label": "second-label-value"}, 3195800080984914717)
220
-}
221
-
222
-func BenchmarkMetricToFingerprintTriple(b *testing.B) {
223
- benchmarkMetricToFingerprint(b, Metric{"first-label": "first-label-value", "second-label": "second-label-value", "third-label": "third-label-value"}, 13843036195897128121)
224
-}
225
-
226
-func benchmarkMetricToFastFingerprint(b *testing.B, m Metric, e Fingerprint) {
227
- for i := 0; i < b.N; i++ {
228
- if a := metricToFastFingerprint(m); a != e {
229
- b.Fatalf("expected signature of %d for %s, got %d", e, m, a)
230
- }
231
- }
232
-}
233
-
234
-func BenchmarkMetricToFastFingerprintScalar(b *testing.B) {
235
- benchmarkMetricToFastFingerprint(b, nil, 14695981039346656037)
236
-}
237
-
238
-func BenchmarkMetricToFastFingerprintSingle(b *testing.B) {
239
- benchmarkMetricToFastFingerprint(b, Metric{"first-label": "first-label-value"}, 5147259542624943964)
240
-}
241
-
242
-func BenchmarkMetricToFastFingerprintDouble(b *testing.B) {
243
- benchmarkMetricToFastFingerprint(b, Metric{"first-label": "first-label-value", "second-label": "second-label-value"}, 18269973311206963528)
244
-}
245
-
246
-func BenchmarkMetricToFastFingerprintTriple(b *testing.B) {
247
- benchmarkMetricToFastFingerprint(b, Metric{"first-label": "first-label-value", "second-label": "second-label-value", "third-label": "third-label-value"}, 15738406913934009676)
248
-}
249
-
250
-func TestEmptyLabelSignature(t *testing.T) {
251
- input := []map[string]string{nil, {}}
252
-
253
- var ms runtime.MemStats
254
- runtime.ReadMemStats(&ms)
255
-
256
- alloc := ms.Alloc
257
-
258
- for _, labels := range input {
259
- LabelsToSignature(labels)
260
- }
261
-
262
- runtime.ReadMemStats(&ms)
263
-
264
- if got := ms.Alloc; alloc != got {
265
- t.Fatal("expected LabelsToSignature with empty labels not to perform allocations")
266
- }
267
-}
268
-
269
-func benchmarkMetricToFastFingerprintConc(b *testing.B, m Metric, e Fingerprint, concLevel int) {
270
- var start, end sync.WaitGroup
271
- start.Add(1)
272
- end.Add(concLevel)
273
-
274
- for i := 0; i < concLevel; i++ {
275
- go func() {
276
- start.Wait()
277
- for j := b.N / concLevel; j >= 0; j-- {
278
- if a := metricToFastFingerprint(m); a != e {
279
- b.Fatalf("expected signature of %d for %s, got %d", e, m, a)
280
- }
281
- }
282
- end.Done()
283
- }()
284
- }
285
- b.ResetTimer()
286
- start.Done()
287
- end.Wait()
288
-}
289
-
290
-func BenchmarkMetricToFastFingerprintTripleConc1(b *testing.B) {
291
- benchmarkMetricToFastFingerprintConc(b, Metric{"first-label": "first-label-value", "second-label": "second-label-value", "third-label": "third-label-value"}, 15738406913934009676, 1)
292
-}
293
-
294
-func BenchmarkMetricToFastFingerprintTripleConc2(b *testing.B) {
295
- benchmarkMetricToFastFingerprintConc(b, Metric{"first-label": "first-label-value", "second-label": "second-label-value", "third-label": "third-label-value"}, 15738406913934009676, 2)
296
-}
297
-
298
-func BenchmarkMetricToFastFingerprintTripleConc4(b *testing.B) {
299
- benchmarkMetricToFastFingerprintConc(b, Metric{"first-label": "first-label-value", "second-label": "second-label-value", "third-label": "third-label-value"}, 15738406913934009676, 4)
300
-}
301
-
302
-func BenchmarkMetricToFastFingerprintTripleConc8(b *testing.B) {
303
- benchmarkMetricToFastFingerprintConc(b, Metric{"first-label": "first-label-value", "second-label": "second-label-value", "third-label": "third-label-value"}, 15738406913934009676, 8)
304
-}
Godeps/_workspace/src/github.com/prometheus/client_golang/model/timestamp.go
deleted
-116
@@ -1,116 +0,0 @@
1
-// Copyright 2013 The Prometheus Authors
2
-// Licensed under the Apache License, Version 2.0 (the "License");
3
-// you may not use this file except in compliance with the License.
4
-// You may obtain a copy of the License at
5
-//
6
-// http://www.apache.org/licenses/LICENSE-2.0
7
-//
8
-// Unless required by applicable law or agreed to in writing, software
9
-// distributed under the License is distributed on an "AS IS" BASIS,
10
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
-// See the License for the specific language governing permissions and
12
-// limitations under the License.
13
-
14
-package model
15
-
16
-import (
17
- "math"
18
- "strconv"
19
-
20
- native_time "time"
21
-)
22
-
23
-// Timestamp is the number of milliseconds since the epoch
24
-// (1970-01-01 00:00 UTC) excluding leap seconds.
25
-type Timestamp int64
26
-
27
-const (
28
- // MinimumTick is the minimum supported time resolution. This has to be
29
- // at least native_time.Second in order for the code below to work.
30
- MinimumTick = native_time.Millisecond
31
- // second is the timestamp duration equivalent to one second.
32
- second = int64(native_time.Second / MinimumTick)
33
- // The number of nanoseconds per minimum tick.
34
- nanosPerTick = int64(MinimumTick / native_time.Nanosecond)
35
-
36
- // Earliest is the earliest timestamp representable. Handy for
37
- // initializing a high watermark.
38
- Earliest = Timestamp(math.MinInt64)
39
- // Latest is the latest timestamp representable. Handy for initializing
40
- // a low watermark.
41
- Latest = Timestamp(math.MaxInt64)
42
-)
43
-
44
-// Equal reports whether two timestamps represent the same instant.
45
-func (t Timestamp) Equal(o Timestamp) bool {
46
- return t == o
47
-}
48
-
49
-// Before reports whether the timestamp t is before o.
50
-func (t Timestamp) Before(o Timestamp) bool {
51
- return t < o
52
-}
53
-
54
-// After reports whether the timestamp t is after o.
55
-func (t Timestamp) After(o Timestamp) bool {
56
- return t > o
57
-}
58
-
59
-// Add returns the Timestamp t + d.
60
-func (t Timestamp) Add(d native_time.Duration) Timestamp {
61
- return t + Timestamp(d/MinimumTick)
62
-}
63
-
64
-// Sub returns the Duration t - o.
65
-func (t Timestamp) Sub(o Timestamp) native_time.Duration {
66
- return native_time.Duration(t-o) * MinimumTick
67
-}
68
-
69
-// Time returns the time.Time representation of t.
70
-func (t Timestamp) Time() native_time.Time {
71
- return native_time.Unix(int64(t)/second, (int64(t)%second)*nanosPerTick)
72
-}
73
-
74
-// Unix returns t as a Unix time, the number of seconds elapsed
75
-// since January 1, 1970 UTC.
76
-func (t Timestamp) Unix() int64 {
77
- return int64(t) / second
78
-}
79
-
80
-// UnixNano returns t as a Unix time, the number of nanoseconds elapsed
81
-// since January 1, 1970 UTC.
82
-func (t Timestamp) UnixNano() int64 {
83
- return int64(t) * nanosPerTick
84
-}
85
-
86
-// String returns a string representation of the timestamp.
87
-func (t Timestamp) String() string {
88
- return strconv.FormatFloat(float64(t)/float64(second), 'f', -1, 64)
89
-}
90
-
91
-// MarshalJSON implements the json.Marshaler interface.
92
-func (t Timestamp) MarshalJSON() ([]byte, error) {
93
- return []byte(t.String()), nil
94
-}
95
-
96
-// Now returns the current time as a Timestamp.
97
-func Now() Timestamp {
98
- return TimestampFromTime(native_time.Now())
99
-}
100
-
101
-// TimestampFromTime returns the Timestamp equivalent to the time.Time t.
102
-func TimestampFromTime(t native_time.Time) Timestamp {
103
- return TimestampFromUnixNano(t.UnixNano())
104
-}
105
-
106
-// TimestampFromUnix returns the Timestamp equivalent to the Unix timestamp t
107
-// provided in seconds.
108
-func TimestampFromUnix(t int64) Timestamp {
109
- return Timestamp(t * second)
110
-}
111
-
112
-// TimestampFromUnixNano returns the Timestamp equivalent to the Unix timestamp
113
-// t provided in nanoseconds.
114
-func TimestampFromUnixNano(t int64) Timestamp {
115
- return Timestamp(t / nanosPerTick)
116
-}
Godeps/_workspace/src/github.com/prometheus/client_golang/model/timestamp_test.go
deleted
-86
@@ -1,86 +0,0 @@
1
-// Copyright 2013 The Prometheus Authors
2
-// Licensed under the Apache License, Version 2.0 (the "License");
3
-// you may not use this file except in compliance with the License.
4
-// You may obtain a copy of the License at
5
-//
6
-// http://www.apache.org/licenses/LICENSE-2.0
7
-//
8
-// Unless required by applicable law or agreed to in writing, software
9
-// distributed under the License is distributed on an "AS IS" BASIS,
10
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
-// See the License for the specific language governing permissions and
12
-// limitations under the License.
13
-
14
-package model
15
-
16
-import (
17
- "testing"
18
- native_time "time"
19
-)
20
-
21
-func TestComparators(t *testing.T) {
22
- t1a := TimestampFromUnix(0)
23
- t1b := TimestampFromUnix(0)
24
- t2 := TimestampFromUnix(2*second - 1)
25
-
26
- if !t1a.Equal(t1b) {
27
- t.Fatalf("Expected %s to be equal to %s", t1a, t1b)
28
- }
29
- if t1a.Equal(t2) {
30
- t.Fatalf("Expected %s to not be equal to %s", t1a, t2)
31
- }
32
-
33
- if !t1a.Before(t2) {
34
- t.Fatalf("Expected %s to be before %s", t1a, t2)
35
- }
36
- if t1a.Before(t1b) {
37
- t.Fatalf("Expected %s to not be before %s", t1a, t1b)
38
- }
39
-
40
- if !t2.After(t1a) {
41
- t.Fatalf("Expected %s to be after %s", t2, t1a)
42
- }
43
- if t1b.After(t1a) {
44
- t.Fatalf("Expected %s to not be after %s", t1b, t1a)
45
- }
46
-}
47
-
48
-func TestTimestampConversions(t *testing.T) {
49
- unixSecs := int64(1136239445)
50
- unixNsecs := int64(123456789)
51
- unixNano := unixSecs*1000000000 + unixNsecs
52
-
53
- t1 := native_time.Unix(unixSecs, unixNsecs-unixNsecs%nanosPerTick)
54
- t2 := native_time.Unix(unixSecs, unixNsecs)
55
-
56
- ts := TimestampFromUnixNano(unixNano)
57
- if !ts.Time().Equal(t1) {
58
- t.Fatalf("Expected %s, got %s", t1, ts.Time())
59
- }
60
-
61
- // Test available precision.
62
- ts = TimestampFromTime(t2)
63
- if !ts.Time().Equal(t1) {
64
- t.Fatalf("Expected %s, got %s", t1, ts.Time())
65
- }
66
-
67
- if ts.UnixNano() != unixNano-unixNano%nanosPerTick {
68
- t.Fatalf("Expected %d, got %d", unixNano, ts.UnixNano())
69
- }
70
-}
71
-
72
-func TestDuration(t *testing.T) {
73
- duration := native_time.Second + native_time.Minute + native_time.Hour
74
- goTime := native_time.Unix(1136239445, 0)
75
-
76
- ts := TimestampFromTime(goTime)
77
- if !goTime.Add(duration).Equal(ts.Add(duration).Time()) {
78
- t.Fatalf("Expected %s to be equal to %s", goTime.Add(duration), ts.Add(duration))
79
- }
80
-
81
- earlier := ts.Add(-duration)
82
- delta := ts.Sub(earlier)
83
- if delta != duration {
84
- t.Fatalf("Expected %s to be equal to %s", delta, duration)
85
- }
86
-}
Godeps/_workspace/src/github.com/prometheus/client_golang/prometheus/.gitignore
deleted
-1
@@ -1 +0,0 @@
1
-command-line-arguments.test
Godeps/_workspace/src/github.com/prometheus/client_golang/prometheus/README.md
deleted
-53
@@ -1,53 +0,0 @@
1
-# Overview
2
-This is the [Prometheus](http://www.prometheus.io) telemetric
3
-instrumentation client [Go](http://golang.org) client library. It
4
-enable authors to define process-space metrics for their servers and
5
-expose them through a web service interface for extraction,
6
-aggregation, and a whole slew of other post processing techniques.
7
-
8
-# Installing
9
- $ go get github.com/prometheus/client_golang/prometheus
10
-
11
-# Example
12
-```go
13
-package main
14
-
15
-import (
16
- "net/http"
17
-
18
- "github.com/prometheus/client_golang/prometheus"
19
-)
20
-
21
-var (
22
- indexed = prometheus.NewCounter(prometheus.CounterOpts{
23
- Namespace: "my_company",
24
- Subsystem: "indexer",
25
- Name: "documents_indexed",
26
- Help: "The number of documents indexed.",
27
- })
28
- size = prometheus.NewGauge(prometheus.GaugeOpts{
29
- Namespace: "my_company",
30
- Subsystem: "storage",
31
- Name: "documents_total_size_bytes",
32
- Help: "The total size of all documents in the storage.",
33
- })
34
-)
35
-
36
-func main() {
37
- http.Handle("/metrics", prometheus.Handler())
38
-
39
- indexed.Inc()
40
- size.Set(5)
41
-
42
- http.ListenAndServe(":8080", nil)
43
-}
44
-
45
-func init() {
46
- prometheus.MustRegister(indexed)
47
- prometheus.MustRegister(size)
48
-}
49
-```
50
-
51
-# Documentation
52
-
53
-[](https://godoc.org/github.com/prometheus/client_golang)
Godeps/_workspace/src/github.com/prometheus/client_golang/prometheus/benchmark_test.go
deleted
-159
@@ -1,159 +0,0 @@
1
-// Copyright 2014 The Prometheus Authors
2
-// Licensed under the Apache License, Version 2.0 (the "License");
3
-// you may not use this file except in compliance with the License.
4
-// You may obtain a copy of the License at
5
-//
6
-// http://www.apache.org/licenses/LICENSE-2.0
7
-//
8
-// Unless required by applicable law or agreed to in writing, software
9
-// distributed under the License is distributed on an "AS IS" BASIS,
10
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
-// See the License for the specific language governing permissions and
12
-// limitations under the License.
13
-
14
-package prometheus
15
-
16
-import (
17
- "testing"
18
-)
19
-
20
-func BenchmarkCounterWithLabelValues(b *testing.B) {
21
- m := NewCounterVec(
22
- CounterOpts{
23
- Name: "benchmark_counter",
24
- Help: "A counter to benchmark it.",
25
- },
26
- []string{"one", "two", "three"},
27
- )
28
- b.ReportAllocs()
29
- b.ResetTimer()
30
- for i := 0; i < b.N; i++ {
31
- m.WithLabelValues("eins", "zwei", "drei").Inc()
32
- }
33
-}
34
-
35
-func BenchmarkCounterWithMappedLabels(b *testing.B) {
36
- m := NewCounterVec(
37
- CounterOpts{
38
- Name: "benchmark_counter",
39
- Help: "A counter to benchmark it.",
40
- },
41
- []string{"one", "two", "three"},
42
- )
43
- b.ReportAllocs()
44
- b.ResetTimer()
45
- for i := 0; i < b.N; i++ {
46
- m.With(Labels{"two": "zwei", "one": "eins", "three": "drei"}).Inc()
47
- }
48
-}
49
-
50
-func BenchmarkCounterWithPreparedMappedLabels(b *testing.B) {
51
- m := NewCounterVec(
52
- CounterOpts{
53
- Name: "benchmark_counter",
54
- Help: "A counter to benchmark it.",
55
- },
56
- []string{"one", "two", "three"},
57
- )
58
- b.ReportAllocs()
59
- b.ResetTimer()
60
- labels := Labels{"two": "zwei", "one": "eins", "three": "drei"}
61
- for i := 0; i < b.N; i++ {
62
- m.With(labels).Inc()
63
- }
64
-}
65
-
66
-func BenchmarkCounterNoLabels(b *testing.B) {
67
- m := NewCounter(CounterOpts{
68
- Name: "benchmark_counter",
69
- Help: "A counter to benchmark it.",
70
- })
71
- b.ReportAllocs()
72
- b.ResetTimer()
73
- for i := 0; i < b.N; i++ {
74
- m.Inc()
75
- }
76
-}
77
-
78
-func BenchmarkGaugeWithLabelValues(b *testing.B) {
79
- m := NewGaugeVec(
80
- GaugeOpts{
81
- Name: "benchmark_gauge",
82
- Help: "A gauge to benchmark it.",
83
- },
84
- []string{"one", "two", "three"},
85
- )
86
- b.ReportAllocs()
87
- b.ResetTimer()
88
- for i := 0; i < b.N; i++ {
89
- m.WithLabelValues("eins", "zwei", "drei").Set(3.1415)
90
- }
91
-}
92
-
93
-func BenchmarkGaugeNoLabels(b *testing.B) {
94
- m := NewGauge(GaugeOpts{
95
- Name: "benchmark_gauge",
96
- Help: "A gauge to benchmark it.",
97
- })
98
- b.ReportAllocs()
99
- b.ResetTimer()
100
- for i := 0; i < b.N; i++ {
101
- m.Set(3.1415)
102
- }
103
-}
104
-
105
-func BenchmarkSummaryWithLabelValues(b *testing.B) {
106
- m := NewSummaryVec(
107
- SummaryOpts{
108
- Name: "benchmark_summary",
109
- Help: "A summary to benchmark it.",
110
- },
111
- []string{"one", "two", "three"},
112
- )
113
- b.ReportAllocs()
114
- b.ResetTimer()
115
- for i := 0; i < b.N; i++ {
116
- m.WithLabelValues("eins", "zwei", "drei").Observe(3.1415)
117
- }
118
-}
119
-
120
-func BenchmarkSummaryNoLabels(b *testing.B) {
121
- m := NewSummary(SummaryOpts{
122
- Name: "benchmark_summary",
123
- Help: "A summary to benchmark it.",
124
- },
125
- )
126
- b.ReportAllocs()
127
- b.ResetTimer()
128
- for i := 0; i < b.N; i++ {
129
- m.Observe(3.1415)
130
- }
131
-}
132
-
133
-func BenchmarkHistogramWithLabelValues(b *testing.B) {
134
- m := NewHistogramVec(
135
- HistogramOpts{
136
- Name: "benchmark_histogram",
137
- Help: "A histogram to benchmark it.",
138
- },
139
- []string{"one", "two", "three"},
140
- )
141
- b.ReportAllocs()
142
- b.ResetTimer()
143
- for i := 0; i < b.N; i++ {
144
- m.WithLabelValues("eins", "zwei", "drei").Observe(3.1415)
145
- }
146
-}
147
-
148
-func BenchmarkHistogramNoLabels(b *testing.B) {
149
- m := NewHistogram(HistogramOpts{
150
- Name: "benchmark_histogram",
151
- Help: "A histogram to benchmark it.",
152
- },
153
- )
154
- b.ReportAllocs()
155
- b.ResetTimer()
156
- for i := 0; i < b.N; i++ {
157
- m.Observe(3.1415)
158
- }
159
-}
Godeps/_workspace/src/github.com/prometheus/client_golang/prometheus/collector.go
deleted
-75
@@ -1,75 +0,0 @@
1
-// Copyright 2014 The Prometheus Authors
2
-// Licensed under the Apache License, Version 2.0 (the "License");
3
-// you may not use this file except in compliance with the License.
4
-// You may obtain a copy of the License at
5
-//
6
-// http://www.apache.org/licenses/LICENSE-2.0
7
-//
8
-// Unless required by applicable law or agreed to in writing, software
9
-// distributed under the License is distributed on an "AS IS" BASIS,
10
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
-// See the License for the specific language governing permissions and
12
-// limitations under the License.
13
-
14
-package prometheus
15
-
16
-// Collector is the interface implemented by anything that can be used by
17
-// Prometheus to collect metrics. A Collector has to be registered for
18
-// collection. See Register, MustRegister, RegisterOrGet, and MustRegisterOrGet.
19
-//
20
-// The stock metrics provided by this package (like Gauge, Counter, Summary) are
21
-// also Collectors (which only ever collect one metric, namely itself). An
22
-// implementer of Collector may, however, collect multiple metrics in a
23
-// coordinated fashion and/or create metrics on the fly. Examples for collectors
24
-// already implemented in this library are the metric vectors (i.e. collection
25
-// of multiple instances of the same Metric but with different label values)
26
-// like GaugeVec or SummaryVec, and the ExpvarCollector.
27
-type Collector interface {
28
- // Describe sends the super-set of all possible descriptors of metrics
29
- // collected by this Collector to the provided channel and returns once
30
- // the last descriptor has been sent. The sent descriptors fulfill the
31
- // consistency and uniqueness requirements described in the Desc
32
- // documentation. (It is valid if one and the same Collector sends
33
- // duplicate descriptors. Those duplicates are simply ignored. However,
34
- // two different Collectors must not send duplicate descriptors.) This
35
- // method idempotently sends the same descriptors throughout the
36
- // lifetime of the Collector. If a Collector encounters an error while
37
- // executing this method, it must send an invalid descriptor (created
38
- // with NewInvalidDesc) to signal the error to the registry.
39
- Describe(chan<- *Desc)
40
- // Collect is called by Prometheus when collecting metrics. The
41
- // implementation sends each collected metric via the provided channel
42
- // and returns once the last metric has been sent. The descriptor of
43
- // each sent metric is one of those returned by Describe. Returned
44
- // metrics that share the same descriptor must differ in their variable
45
- // label values. This method may be called concurrently and must
46
- // therefore be implemented in a concurrency safe way. Blocking occurs
47
- // at the expense of total performance of rendering all registered
48
- // metrics. Ideally, Collector implementations support concurrent
49
- // readers.
50
- Collect(chan<- Metric)
51
-}
52
-
53
-// SelfCollector implements Collector for a single Metric so that that the
54
-// Metric collects itself. Add it as an anonymous field to a struct that
55
-// implements Metric, and call Init with the Metric itself as an argument.
56
-type SelfCollector struct {
57
- self Metric
58
-}
59
-
60
-// Init provides the SelfCollector with a reference to the metric it is supposed
61
-// to collect. It is usually called within the factory function to create a
62
-// metric. See example.
63
-func (c *SelfCollector) Init(self Metric) {
64
- c.self = self
65
-}
66
-
67
-// Describe implements Collector.
68
-func (c *SelfCollector) Describe(ch chan<- *Desc) {
69
- ch <- c.self.Desc()
70
-}
71
-
72
-// Collect implements Collector.
73
-func (c *SelfCollector) Collect(ch chan<- Metric) {
74
- ch <- c.self
75
-}
Godeps/_workspace/src/github.com/prometheus/client_golang/prometheus/counter.go
deleted
-175
@@ -1,175 +0,0 @@
1
-// Copyright 2014 The Prometheus Authors
2
-// Licensed under the Apache License, Version 2.0 (the "License");
3
-// you may not use this file except in compliance with the License.
4
-// You may obtain a copy of the License at
5
-//
6
-// http://www.apache.org/licenses/LICENSE-2.0
7
-//
8
-// Unless required by applicable law or agreed to in writing, software
9
-// distributed under the License is distributed on an "AS IS" BASIS,
10
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
-// See the License for the specific language governing permissions and
12
-// limitations under the License.
13
-
14
-package prometheus
15
-
16
-import (
17
- "errors"
18
- "hash/fnv"
19
-)
20
-
21
-// Counter is a Metric that represents a single numerical value that only ever
22
-// goes up. That implies that it cannot be used to count items whose number can
23
-// also go down, e.g. the number of currently running goroutines. Those
24
-// "counters" are represented by Gauges.
25
-//
26
-// A Counter is typically used to count requests served, tasks completed, errors
27
-// occurred, etc.
28
-//
29
-// To create Counter instances, use NewCounter.
30
-type Counter interface {
31
- Metric
32
- Collector
33
-
34
- // Set is used to set the Counter to an arbitrary value. It is only used
35
- // if you have to transfer a value from an external counter into this
36
- // Prometheus metric. Do not use it for regular handling of a
37
- // Prometheus counter (as it can be used to break the contract of
38
- // monotonically increasing values).
39
- Set(float64)
40
- // Inc increments the counter by 1.
41
- Inc()
42
- // Add adds the given value to the counter. It panics if the value is <
43
- // 0.
44
- Add(float64)
45
-}
46
-
47
-// CounterOpts is an alias for Opts. See there for doc comments.
48
-type CounterOpts Opts
49
-
50
-// NewCounter creates a new Counter based on the provided CounterOpts.
51
-func NewCounter(opts CounterOpts) Counter {
52
- desc := NewDesc(
53
- BuildFQName(opts.Namespace, opts.Subsystem, opts.Name),
54
- opts.Help,
55
- nil,
56
- opts.ConstLabels,
57
- )
58
- result := &counter{value: value{desc: desc, valType: CounterValue, labelPairs: desc.constLabelPairs}}
59
- result.Init(result) // Init self-collection.
60
- return result
61
-}
62
-
63
-type counter struct {
64
- value
65
-}
66
-
67
-func (c *counter) Add(v float64) {
68
- if v < 0 {
69
- panic(errors.New("counter cannot decrease in value"))
70
- }
71
- c.value.Add(v)
72
-}
73
-
74
-// CounterVec is a Collector that bundles a set of Counters that all share the
75
-// same Desc, but have different values for their variable labels. This is used
76
-// if you want to count the same thing partitioned by various dimensions
77
-// (e.g. number of HTTP requests, partitioned by response code and
78
-// method). Create instances with NewCounterVec.
79
-//
80
-// CounterVec embeds MetricVec. See there for a full list of methods with
81
-// detailed documentation.
82
-type CounterVec struct {
83
- MetricVec
84
-}
85
-
86
-// NewCounterVec creates a new CounterVec based on the provided CounterOpts and
87
-// partitioned by the given label names. At least one label name must be
88
-// provided.
89
-func NewCounterVec(opts CounterOpts, labelNames []string) *CounterVec {
90
- desc := NewDesc(
91
- BuildFQName(opts.Namespace, opts.Subsystem, opts.Name),
92
- opts.Help,
93
- labelNames,
94
- opts.ConstLabels,
95
- )
96
- return &CounterVec{
97
- MetricVec: MetricVec{
98
- children: map[uint64]Metric{},
99
- desc: desc,
100
- hash: fnv.New64a(),
101
- newMetric: func(lvs ...string) Metric {
102
- result := &counter{value: value{
103
- desc: desc,
104
- valType: CounterValue,
105
- labelPairs: makeLabelPairs(desc, lvs),
106
- }}
107
- result.Init(result) // Init self-collection.
108
- return result
109
- },
110
- },
111
- }
112
-}
113
-
114
-// GetMetricWithLabelValues replaces the method of the same name in
115
-// MetricVec. The difference is that this method returns a Counter and not a
116
-// Metric so that no type conversion is required.
117
-func (m *CounterVec) GetMetricWithLabelValues(lvs ...string) (Counter, error) {
118
- metric, err := m.MetricVec.GetMetricWithLabelValues(lvs...)
119
- if metric != nil {
120
- return metric.(Counter), err
121
- }
122
- return nil, err
123
-}
124
-
125
-// GetMetricWith replaces the method of the same name in MetricVec. The
126
-// difference is that this method returns a Counter and not a Metric so that no
127
-// type conversion is required.
128
-func (m *CounterVec) GetMetricWith(labels Labels) (Counter, error) {
129
- metric, err := m.MetricVec.GetMetricWith(labels)
130
- if metric != nil {
131
- return metric.(Counter), err
132
- }
133
- return nil, err
134
-}
135
-
136
-// WithLabelValues works as GetMetricWithLabelValues, but panics where
137
-// GetMetricWithLabelValues would have returned an error. By not returning an
138
-// error, WithLabelValues allows shortcuts like
139
-// myVec.WithLabelValues("404", "GET").Add(42)
140
-func (m *CounterVec) WithLabelValues(lvs ...string) Counter {
141
- return m.MetricVec.WithLabelValues(lvs...).(Counter)
142
-}
143
-
144
-// With works as GetMetricWith, but panics where GetMetricWithLabels would have
145
-// returned an error. By not returning an error, With allows shortcuts like
146
-// myVec.With(Labels{"code": "404", "method": "GET"}).Add(42)
147
-func (m *CounterVec) With(labels Labels) Counter {
148
- return m.MetricVec.With(labels).(Counter)
149
-}
150
-
151
-// CounterFunc is a Counter whose value is determined at collect time by calling a
152
-// provided function.
153
-//
154
-// To create CounterFunc instances, use NewCounterFunc.
155
-type CounterFunc interface {
156
- Metric
157
- Collector
158
-}
159
-
160
-// NewCounterFunc creates a new CounterFunc based on the provided
161
-// CounterOpts. The value reported is determined by calling the given function
162
-// from within the Write method. Take into account that metric collection may
163
-// happen concurrently. If that results in concurrent calls to Write, like in
164
-// the case where a CounterFunc is directly registered with Prometheus, the
165
-// provided function must be concurrency-safe. The function should also honor
166
-// the contract for a Counter (values only go up, not down), but compliance will
167
-// not be checked.
168
-func NewCounterFunc(opts CounterOpts, function func() float64) CounterFunc {
169
- return newValueFunc(NewDesc(
170
- BuildFQName(opts.Namespace, opts.Subsystem, opts.Name),
171
- opts.Help,
172
- nil,
173
- opts.ConstLabels,
174
- ), CounterValue, function)
175
-}
Godeps/_workspace/src/github.com/prometheus/client_golang/prometheus/counter_test.go
deleted
-58
@@ -1,58 +0,0 @@
1
-// Copyright 2014 The Prometheus Authors
2
-// Licensed under the Apache License, Version 2.0 (the "License");
3
-// you may not use this file except in compliance with the License.
4
-// You may obtain a copy of the License at
5
-//
6
-// http://www.apache.org/licenses/LICENSE-2.0
7
-//
8
-// Unless required by applicable law or agreed to in writing, software
9
-// distributed under the License is distributed on an "AS IS" BASIS,
10
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
-// See the License for the specific language governing permissions and
12
-// limitations under the License.
13
-
14
-package prometheus
15
-
16
-import (
17
- "math"
18
- "testing"
19
-
20
- dto "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/prometheus/client_model/go"
21
-)
22
-
23
-func TestCounterAdd(t *testing.T) {
24
- counter := NewCounter(CounterOpts{
25
- Name: "test",
26
- Help: "test help",
27
- ConstLabels: Labels{"a": "1", "b": "2"},
28
- }).(*counter)
29
- counter.Inc()
30
- if expected, got := 1., math.Float64frombits(counter.valBits); expected != got {
31
- t.Errorf("Expected %f, got %f.", expected, got)
32
- }
33
- counter.Add(42)
34
- if expected, got := 43., math.Float64frombits(counter.valBits); expected != got {
35
- t.Errorf("Expected %f, got %f.", expected, got)
36
- }
37
-
38
- if expected, got := "counter cannot decrease in value", decreaseCounter(counter).Error(); expected != got {
39
- t.Errorf("Expected error %q, got %q.", expected, got)
40
- }
41
-
42
- m := &dto.Metric{}
43
- counter.Write(m)
44
-
45
- if expected, got := `label:<name:"a" value:"1" > label:<name:"b" value:"2" > counter:<value:43 > `, m.String(); expected != got {
46
- t.Errorf("expected %q, got %q", expected, got)
47
- }
48
-}
49
-
50
-func decreaseCounter(c *counter) (err error) {
51
- defer func() {
52
- if e := recover(); e != nil {
53
- err = e.(error)
54
- }
55
- }()
56
- c.Add(-1)
57
- return nil
58
-}
Godeps/_workspace/src/github.com/prometheus/client_golang/prometheus/desc.go
deleted
-198
@@ -1,198 +0,0 @@
1
-package prometheus
2
-
3
-import (
4
- "bytes"
5
- "errors"
6
- "fmt"
7
- "hash/fnv"
8
- "regexp"
9
- "sort"
10
- "strings"
11
-
12
- "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/golang/protobuf/proto"
13
-
14
- dto "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/prometheus/client_model/go"
15
-
16
- "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/prometheus/client_golang/model"
17
-)
18
-
19
-var (
20
- metricNameRE = regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_:]*$`)
21
-)
22
-
23
-// Labels represents a collection of label name -> value mappings. This type is
24
-// commonly used with the With(Labels) and GetMetricWith(Labels) methods of
25
-// metric vector Collectors, e.g.:
26
-// myVec.With(Labels{"code": "404", "method": "GET"}).Add(42)
27
-//
28
-// The other use-case is the specification of constant label pairs in Opts or to
29
-// create a Desc.
30
-type Labels map[string]string
31
-
32
-// Desc is the descriptor used by every Prometheus Metric. It is essentially
33
-// the immutable meta-data of a Metric. The normal Metric implementations
34
-// included in this package manage their Desc under the hood. Users only have to
35
-// deal with Desc if they use advanced features like the ExpvarCollector or
36
-// custom Collectors and Metrics.
37
-//
38
-// Descriptors registered with the same registry have to fulfill certain
39
-// consistency and uniqueness criteria if they share the same fully-qualified
40
-// name: They must have the same help string and the same label names (aka label
41
-// dimensions) in each, constLabels and variableLabels, but they must differ in
42
-// the values of the constLabels.
43
-//
44
-// Descriptors that share the same fully-qualified names and the same label
45
-// values of their constLabels are considered equal.
46
-//
47
-// Use NewDesc to create new Desc instances.
48
-type Desc struct {
49
- // fqName has been built from Namespace, Subsystem, and Name.
50
- fqName string
51
- // help provides some helpful information about this metric.
52
- help string
53
- // constLabelPairs contains precalculated DTO label pairs based on
54
- // the constant labels.
55
- constLabelPairs []*dto.LabelPair
56
- // VariableLabels contains names of labels for which the metric
57
- // maintains variable values.
58
- variableLabels []string
59
- // id is a hash of the values of the ConstLabels and fqName. This
60
- // must be unique among all registered descriptors and can therefore be
61
- // used as an identifier of the descriptor.
62
- id uint64
63
- // dimHash is a hash of the label names (preset and variable) and the
64
- // Help string. Each Desc with the same fqName must have the same
65
- // dimHash.
66
- dimHash uint64
67
- // err is an error that occured during construction. It is reported on
68
- // registration time.
69
- err error
70
-}
71
-
72
-// NewDesc allocates and initializes a new Desc. Errors are recorded in the Desc
73
-// and will be reported on registration time. variableLabels and constLabels can
74
-// be nil if no such labels should be set. fqName and help must not be empty.
75
-//
76
-// variableLabels only contain the label names. Their label values are variable
77
-// and therefore not part of the Desc. (They are managed within the Metric.)
78
-//
79
-// For constLabels, the label values are constant. Therefore, they are fully
80
-// specified in the Desc. See the Opts documentation for the implications of
81
-// constant labels.
82
-func NewDesc(fqName, help string, variableLabels []string, constLabels Labels) *Desc {
83
- d := &Desc{
84
- fqName: fqName,
85
- help: help,
86
- variableLabels: variableLabels,
87
- }
88
- if help == "" {
89
- d.err = errors.New("empty help string")
90
- return d
91
- }
92
- if !metricNameRE.MatchString(fqName) {
93
- d.err = fmt.Errorf("%q is not a valid metric name", fqName)
94
- return d
95
- }
96
- // labelValues contains the label values of const labels (in order of
97
- // their sorted label names) plus the fqName (at position 0).
98
- labelValues := make([]string, 1, len(constLabels)+1)
99
- labelValues[0] = fqName
100
- labelNames := make([]string, 0, len(constLabels)+len(variableLabels))
101
- labelNameSet := map[string]struct{}{}
102
- // First add only the const label names and sort them...
103
- for labelName := range constLabels {
104
- if !checkLabelName(labelName) {
105
- d.err = fmt.Errorf("%q is not a valid label name", labelName)
106
- return d
107
- }
108
- labelNames = append(labelNames, labelName)
109
- labelNameSet[labelName] = struct{}{}
110
- }
111
- sort.Strings(labelNames)
112
- // ... so that we can now add const label values in the order of their names.
113
- for _, labelName := range labelNames {
114
- labelValues = append(labelValues, constLabels[labelName])
115
- }
116
- // Now add the variable label names, but prefix them with something that
117
- // cannot be in a regular label name. That prevents matching the label
118
- // dimension with a different mix between preset and variable labels.
119
- for _, labelName := range variableLabels {
120
- if !checkLabelName(labelName) {
121
- d.err = fmt.Errorf("%q is not a valid label name", labelName)
122
- return d
123
- }
124
- labelNames = append(labelNames, "$"+labelName)
125
- labelNameSet[labelName] = struct{}{}
126
- }
127
- if len(labelNames) != len(labelNameSet) {
128
- d.err = errors.New("duplicate label names")
129
- return d
130
- }
131
- h := fnv.New64a()
132
- var b bytes.Buffer // To copy string contents into, avoiding []byte allocations.
133
- for _, val := range labelValues {
134
- b.Reset()
135
- b.WriteString(val)
136
- b.WriteByte(model.SeparatorByte)
137
- h.Write(b.Bytes())
138
- }
139
- d.id = h.Sum64()
140
- // Sort labelNames so that order doesn't matter for the hash.
141
- sort.Strings(labelNames)
142
- // Now hash together (in this order) the help string and the sorted
143
- // label names.
144
- h.Reset()
145
- b.Reset()
146
- b.WriteString(help)
147
- b.WriteByte(model.SeparatorByte)
148
- h.Write(b.Bytes())
149
- for _, labelName := range labelNames {
150
- b.Reset()
151
- b.WriteString(labelName)
152
- b.WriteByte(model.SeparatorByte)
153
- h.Write(b.Bytes())
154
- }
155
- d.dimHash = h.Sum64()
156
-
157
- d.constLabelPairs = make([]*dto.LabelPair, 0, len(constLabels))
158
- for n, v := range constLabels {
159
- d.constLabelPairs = append(d.constLabelPairs, &dto.LabelPair{
160
- Name: proto.String(n),
161
- Value: proto.String(v),
162
- })
163
- }
164
- sort.Sort(LabelPairSorter(d.constLabelPairs))
165
- return d
166
-}
167
-
168
-// NewInvalidDesc returns an invalid descriptor, i.e. a descriptor with the
169
-// provided error set. If a collector returning such a descriptor is registered,
170
-// registration will fail with the provided error. NewInvalidDesc can be used by
171
-// a Collector to signal inability to describe itself.
172
-func NewInvalidDesc(err error) *Desc {
173
- return &Desc{
174
- err: err,
175
- }
176
-}
177
-
178
-func (d *Desc) String() string {
179
- lpStrings := make([]string, 0, len(d.constLabelPairs))
180
- for _, lp := range d.constLabelPairs {
181
- lpStrings = append(
182
- lpStrings,
183
- fmt.Sprintf("%s=%q", lp.GetName(), lp.GetValue()),
184
- )
185
- }
186
- return fmt.Sprintf(
187
- "Desc{fqName: %q, help: %q, constLabels: {%s}, variableLabels: %v}",
188
- d.fqName,
189
- d.help,
190
- strings.Join(lpStrings, ","),
191
- d.variableLabels,
192
- )
193
-}
194
-
195
-func checkLabelName(l string) bool {
196
- return model.LabelNameRE.MatchString(l) &&
197
- !strings.HasPrefix(l, model.ReservedLabelPrefix)
198
-}
Godeps/_workspace/src/github.com/prometheus/client_golang/prometheus/doc.go
deleted
-109
@@ -1,109 +0,0 @@
1
-// Copyright 2014 The Prometheus Authors
2
-// Licensed under the Apache License, Version 2.0 (the "License");
3
-// you may not use this file except in compliance with the License.
4
-// You may obtain a copy of the License at
5
-//
6
-// http://www.apache.org/licenses/LICENSE-2.0
7
-//
8
-// Unless required by applicable law or agreed to in writing, software
9
-// distributed under the License is distributed on an "AS IS" BASIS,
10
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
-// See the License for the specific language governing permissions and
12
-// limitations under the License.
13
-
14
-// Package prometheus provides embeddable metric primitives for servers and
15
-// standardized exposition of telemetry through a web services interface.
16
-//
17
-// All exported functions and methods are safe to be used concurrently unless
18
-// specified otherwise.
19
-//
20
-// To expose metrics registered with the Prometheus registry, an HTTP server
21
-// needs to know about the Prometheus handler. The usual endpoint is "/metrics".
22
-//
23
-// http.Handle("/metrics", prometheus.Handler())
24
-//
25
-// As a starting point a very basic usage example:
26
-//
27
-// package main
28
-//
29
-// import (
30
-// "net/http"
31
-//
32
-// "github.com/prometheus/client_golang/prometheus"
33
-// )
34
-//
35
-// var (
36
-// cpuTemp = prometheus.NewGauge(prometheus.GaugeOpts{
37
-// Name: "cpu_temperature_celsius",
38
-// Help: "Current temperature of the CPU.",
39
-// })
40
-// hdFailures = prometheus.NewCounter(prometheus.CounterOpts{
41
-// Name: "hd_errors_total",
42
-// Help: "Number of hard-disk errors.",
43
-// })
44
-// )
45
-//
46
-// func init() {
47
-// prometheus.MustRegister(cpuTemp)
48
-// prometheus.MustRegister(hdFailures)
49
-// }
50
-//
51
-// func main() {
52
-// cpuTemp.Set(65.3)
53
-// hdFailures.Inc()
54
-//
55
-// http.Handle("/metrics", prometheus.Handler())
56
-// http.ListenAndServe(":8080", nil)
57
-// }
58
-//
59
-//
60
-// This is a complete program that exports two metrics, a Gauge and a Counter.
61
-// It also exports some stats about the HTTP usage of the /metrics
62
-// endpoint. (See the Handler function for more detail.)
63
-//
64
-// Two more advanced metric types are the Summary and Histogram.
65
-//
66
-// In addition to the fundamental metric types Gauge, Counter, Summary, and
67
-// Histogram, a very important part of the Prometheus data model is the
68
-// partitioning of samples along dimensions called labels, which results in
69
-// metric vectors. The fundamental types are GaugeVec, CounterVec, SummaryVec,
70
-// and HistogramVec.
71
-//
72
-// Those are all the parts needed for basic usage. Detailed documentation and
73
-// examples are provided below.
74
-//
75
-// Everything else this package offers is essentially for "power users" only. A
76
-// few pointers to "power user features":
77
-//
78
-// All the various ...Opts structs have a ConstLabels field for labels that
79
-// never change their value (which is only useful under special circumstances,
80
-// see documentation of the Opts type).
81
-//
82
-// The Untyped metric behaves like a Gauge, but signals the Prometheus server
83
-// not to assume anything about its type.
84
-//
85
-// Functions to fine-tune how the metric registry works: EnableCollectChecks,
86
-// PanicOnCollectError, Register, Unregister, SetMetricFamilyInjectionHook.
87
-//
88
-// For custom metric collection, there are two entry points: Custom Metric
89
-// implementations and custom Collector implementations. A Metric is the
90
-// fundamental unit in the Prometheus data model: a sample at a point in time
91
-// together with its meta-data (like its fully-qualified name and any number of
92
-// pairs of label name and label value) that knows how to marshal itself into a
93
-// data transfer object (aka DTO, implemented as a protocol buffer). A Collector
94
-// gets registered with the Prometheus registry and manages the collection of
95
-// one or more Metrics. Many parts of this package are building blocks for
96
-// Metrics and Collectors. Desc is the metric descriptor, actually used by all
97
-// metrics under the hood, and by Collectors to describe the Metrics to be
98
-// collected, but only to be dealt with by users if they implement their own
99
-// Metrics or Collectors. To create a Desc, the BuildFQName function will come
100
-// in handy. Other useful components for Metric and Collector implementation
101
-// include: LabelPairSorter to sort the DTO version of label pairs,
102
-// NewConstMetric and MustNewConstMetric to create "throw away" Metrics at
103
-// collection time, MetricVec to bundle custom Metrics into a metric vector
104
-// Collector, SelfCollector to make a custom Metric collect itself.
105
-//
106
-// A good example for a custom Collector is the ExpVarCollector included in this
107
-// package, which exports variables exported via the "expvar" package as
108
-// Prometheus metrics.
109
-package prometheus
Godeps/_workspace/src/github.com/prometheus/client_golang/prometheus/example_clustermanager_test.go
deleted
-130
@@ -1,130 +0,0 @@
1
-// Copyright 2014 The Prometheus Authors
2
-// Licensed under the Apache License, Version 2.0 (the "License");
3
-// you may not use this file except in compliance with the License.
4
-// You may obtain a copy of the License at
5
-//
6
-// http://www.apache.org/licenses/LICENSE-2.0
7
-//
8
-// Unless required by applicable law or agreed to in writing, software
9
-// distributed under the License is distributed on an "AS IS" BASIS,
10
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
-// See the License for the specific language governing permissions and
12
-// limitations under the License.
13
-
14
-package prometheus_test
15
-
16
-import (
17
- "sync"
18
-
19
- "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/prometheus/client_golang/prometheus"
20
-)
21
-
22
-// ClusterManager is an example for a system that might have been built without
23
-// Prometheus in mind. It models a central manager of jobs running in a
24
-// cluster. To turn it into something that collects Prometheus metrics, we
25
-// simply add the two methods required for the Collector interface.
26
-//
27
-// An additional challenge is that multiple instances of the ClusterManager are
28
-// run within the same binary, each in charge of a different zone. We need to
29
-// make use of ConstLabels to be able to register each ClusterManager instance
30
-// with Prometheus.
31
-type ClusterManager struct {
32
- Zone string
33
- OOMCount *prometheus.CounterVec
34
- RAMUsage *prometheus.GaugeVec
35
- mtx sync.Mutex // Protects OOMCount and RAMUsage.
36
- // ... many more fields
37
-}
38
-
39
-// ReallyExpensiveAssessmentOfTheSystemState is a mock for the data gathering a
40
-// real cluster manager would have to do. Since it may actually be really
41
-// expensive, it must only be called once per collection. This implementation,
42
-// obviously, only returns some made-up data.
43
-func (c *ClusterManager) ReallyExpensiveAssessmentOfTheSystemState() (
44
- oomCountByHost map[string]int, ramUsageByHost map[string]float64,
45
-) {
46
- // Just example fake data.
47
- oomCountByHost = map[string]int{
48
- "foo.example.org": 42,
49
- "bar.example.org": 2001,
50
- }
51
- ramUsageByHost = map[string]float64{
52
- "foo.example.org": 6.023e23,
53
- "bar.example.org": 3.14,
54
- }
55
- return
56
-}
57
-
58
-// Describe faces the interesting challenge that the two metric vectors that are
59
-// used in this example are already Collectors themselves. However, thanks to
60
-// the use of channels, it is really easy to "chain" Collectors. Here we simply
61
-// call the Describe methods of the two metric vectors.
62
-func (c *ClusterManager) Describe(ch chan<- *prometheus.Desc) {
63
- c.OOMCount.Describe(ch)
64
- c.RAMUsage.Describe(ch)
65
-}
66
-
67
-// Collect first triggers the ReallyExpensiveAssessmentOfTheSystemState. Then it
68
-// sets the retrieved values in the two metric vectors and then sends all their
69
-// metrics to the channel (again using a chaining technique as in the Describe
70
-// method). Since Collect could be called multiple times concurrently, that part
71
-// is protected by a mutex.
72
-func (c *ClusterManager) Collect(ch chan<- prometheus.Metric) {
73
- oomCountByHost, ramUsageByHost := c.ReallyExpensiveAssessmentOfTheSystemState()
74
- c.mtx.Lock()
75
- defer c.mtx.Unlock()
76
- for host, oomCount := range oomCountByHost {
77
- c.OOMCount.WithLabelValues(host).Set(float64(oomCount))
78
- }
79
- for host, ramUsage := range ramUsageByHost {
80
- c.RAMUsage.WithLabelValues(host).Set(ramUsage)
81
- }
82
- c.OOMCount.Collect(ch)
83
- c.RAMUsage.Collect(ch)
84
- // All metrics in OOMCount and RAMUsage are sent to the channel now. We
85
- // can safely reset the two metric vectors now, so that we can start
86
- // fresh in the next Collect cycle. (Imagine a host disappears from the
87
- // cluster. If we did not reset here, its Metric would stay in the
88
- // metric vectors forever.)
89
- c.OOMCount.Reset()
90
- c.RAMUsage.Reset()
91
-}
92
-
93
-// NewClusterManager creates the two metric vectors OOMCount and RAMUsage. Note
94
-// that the zone is set as a ConstLabel. (It's different in each instance of the
95
-// ClusterManager, but constant over the lifetime of an instance.) The reported
96
-// values are partitioned by host, which is therefore a variable label.
97
-func NewClusterManager(zone string) *ClusterManager {
98
- return &ClusterManager{
99
- Zone: zone,
100
- OOMCount: prometheus.NewCounterVec(
101
- prometheus.CounterOpts{
102
- Subsystem: "clustermanager",
103
- Name: "oom_count",
104
- Help: "number of OOM crashes",
105
- ConstLabels: prometheus.Labels{"zone": zone},
106
- },
107
- []string{"host"},
108
- ),
109
- RAMUsage: prometheus.NewGaugeVec(
110
- prometheus.GaugeOpts{
111
- Subsystem: "clustermanager",
112
- Name: "ram_usage_bytes",
113
- Help: "RAM usage as reported to the cluster manager",
114
- ConstLabels: prometheus.Labels{"zone": zone},
115
- },
116
- []string{"host"},
117
- ),
118
- }
119
-}
120
-
121
-func ExampleCollector_clustermanager() {
122
- workerDB := NewClusterManager("db")
123
- workerCA := NewClusterManager("ca")
124
- prometheus.MustRegister(workerDB)
125
- prometheus.MustRegister(workerCA)
126
-
127
- // Since we are dealing with custom Collector implementations, it might
128
- // be a good idea to enable the collect checks in the registry.
129
- prometheus.EnableCollectChecks(true)
130
-}
Godeps/_workspace/src/github.com/prometheus/client_golang/prometheus/example_memstats_test.go
deleted
-87
@@ -1,87 +0,0 @@
1
-// Copyright 2014 The Prometheus Authors
2
-// Licensed under the Apache License, Version 2.0 (the "License");
3
-// you may not use this file except in compliance with the License.
4
-// You may obtain a copy of the License at
5
-//
6
-// http://www.apache.org/licenses/LICENSE-2.0
7
-//
8
-// Unless required by applicable law or agreed to in writing, software
9
-// distributed under the License is distributed on an "AS IS" BASIS,
10
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
-// See the License for the specific language governing permissions and
12
-// limitations under the License.
13
-
14
-package prometheus_test
15
-
16
-import (
17
- "runtime"
18
-
19
- "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/prometheus/client_golang/prometheus"
20
-)
21
-
22
-var (
23
- allocDesc = prometheus.NewDesc(
24
- prometheus.BuildFQName("", "memstats", "alloc_bytes"),
25
- "bytes allocated and still in use",
26
- nil, nil,
27
- )
28
- totalAllocDesc = prometheus.NewDesc(
29
- prometheus.BuildFQName("", "memstats", "total_alloc_bytes"),
30
- "bytes allocated (even if freed)",
31
- nil, nil,
32
- )
33
- numGCDesc = prometheus.NewDesc(
34
- prometheus.BuildFQName("", "memstats", "num_gc_total"),
35
- "number of GCs run",
36
- nil, nil,
37
- )
38
-)
39
-
40
-// MemStatsCollector is an example for a custom Collector that solves the
41
-// problem of feeding into multiple metrics at the same time. The
42
-// runtime.ReadMemStats should happen only once, and then the results need to be
43
-// fed into a number of separate Metrics. In this example, only a few of the
44
-// values reported by ReadMemStats are used. For each, there is a Desc provided
45
-// as a var, so the MemStatsCollector itself needs nothing else in the
46
-// struct. Only the methods need to be implemented.
47
-type MemStatsCollector struct{}
48
-
49
-// Describe just sends the three Desc objects for the Metrics we intend to
50
-// collect.
51
-func (_ MemStatsCollector) Describe(ch chan<- *prometheus.Desc) {
52
- ch <- allocDesc
53
- ch <- totalAllocDesc
54
- ch <- numGCDesc
55
-}
56
-
57
-// Collect does the trick by calling ReadMemStats once and then constructing
58
-// three different Metrics on the fly.
59
-func (_ MemStatsCollector) Collect(ch chan<- prometheus.Metric) {
60
- var ms runtime.MemStats
61
- runtime.ReadMemStats(&ms)
62
- ch <- prometheus.MustNewConstMetric(
63
- allocDesc,
64
- prometheus.GaugeValue,
65
- float64(ms.Alloc),
66
- )
67
- ch <- prometheus.MustNewConstMetric(
68
- totalAllocDesc,
69
- prometheus.GaugeValue,
70
- float64(ms.TotalAlloc),
71
- )
72
- ch <- prometheus.MustNewConstMetric(
73
- numGCDesc,
74
- prometheus.CounterValue,
75
- float64(ms.NumGC),
76
- )
77
- // To avoid new allocations on each collection, you could also keep
78
- // metric objects around and return the same objects each time, just
79
- // with new values set.
80
-}
81
-
82
-func ExampleCollector_memstats() {
83
- prometheus.MustRegister(&MemStatsCollector{})
84
- // Since we are dealing with custom Collector implementations, it might
85
- // be a good idea to enable the collect checks in the registry.
86
- prometheus.EnableCollectChecks(true)
87
-}
Godeps/_workspace/src/github.com/prometheus/client_golang/prometheus/example_selfcollector_test.go
deleted
-69
@@ -1,69 +0,0 @@
1
-// Copyright 2014 The Prometheus Authors
2
-// Licensed under the Apache License, Version 2.0 (the "License");
3
-// you may not use this file except in compliance with the License.
4
-// You may obtain a copy of the License at
5
-//
6
-// http://www.apache.org/licenses/LICENSE-2.0
7
-//
8
-// Unless required by applicable law or agreed to in writing, software
9
-// distributed under the License is distributed on an "AS IS" BASIS,
10
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
-// See the License for the specific language governing permissions and
12
-// limitations under the License.
13
-
14
-package prometheus_test
15
-
16
-import (
17
- "runtime"
18
-
19
- "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/golang/protobuf/proto"
20
-
21
- dto "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/prometheus/client_model/go"
22
-
23
- "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/prometheus/client_golang/prometheus"
24
-)
25
-
26
-func NewCallbackMetric(desc *prometheus.Desc, callback func() float64) *CallbackMetric {
27
- result := &CallbackMetric{desc: desc, callback: callback}
28
- result.Init(result) // Initialize the SelfCollector.
29
- return result
30
-}
31
-
32
-// TODO: Come up with a better example.
33
-
34
-// CallbackMetric is an example for a user-defined Metric that exports the
35
-// result of a function call as a metric of type "untyped" without any
36
-// labels. It uses SelfCollector to turn the Metric into a Collector so that it
37
-// can be registered with Prometheus.
38
-//
39
-// Note that this example is pretty much academic as the prometheus package
40
-// already provides an UntypedFunc type.
41
-type CallbackMetric struct {
42
- prometheus.SelfCollector
43
-
44
- desc *prometheus.Desc
45
- callback func() float64
46
-}
47
-
48
-func (cm *CallbackMetric) Desc() *prometheus.Desc {
49
- return cm.desc
50
-}
51
-
52
-func (cm *CallbackMetric) Write(m *dto.Metric) error {
53
- m.Untyped = &dto.Untyped{Value: proto.Float64(cm.callback())}
54
- return nil
55
-}
56
-
57
-func ExampleSelfCollector() {
58
- m := NewCallbackMetric(
59
- prometheus.NewDesc(
60
- "runtime_goroutines_count",
61
- "Total number of goroutines that currently exist.",
62
- nil, nil, // No labels, these must be nil.
63
- ),
64
- func() float64 {
65
- return float64(runtime.NumGoroutine())
66
- },
67
- )
68
- prometheus.MustRegister(m)
69
-}
Godeps/_workspace/src/github.com/prometheus/client_golang/prometheus/examples_test.go
deleted
-626
@@ -1,626 +0,0 @@
1
-// Copyright 2014 The Prometheus Authors
2
-// Licensed under the Apache License, Version 2.0 (the "License");
3
-// you may not use this file except in compliance with the License.
4
-// You may obtain a copy of the License at
5
-//
6
-// http://www.apache.org/licenses/LICENSE-2.0
7
-//
8
-// Unless required by applicable law or agreed to in writing, software
9
-// distributed under the License is distributed on an "AS IS" BASIS,
10
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
-// See the License for the specific language governing permissions and
12
-// limitations under the License.
13
-
14
-package prometheus_test
15
-
16
-import (
17
- "flag"
18
- "fmt"
19
- "math"
20
- "net/http"
21
- "os"
22
- "runtime"
23
- "sort"
24
- "time"
25
-
26
- dto "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/prometheus/client_model/go"
27
-
28
- "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/golang/protobuf/proto"
29
-
30
- "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/prometheus/client_golang/prometheus"
31
-)
32
-
33
-func ExampleGauge() {
34
- opsQueued := prometheus.NewGauge(prometheus.GaugeOpts{
35
- Namespace: "our_company",
36
- Subsystem: "blob_storage",
37
- Name: "ops_queued",
38
- Help: "Number of blob storage operations waiting to be processed.",
39
- })
40
- prometheus.MustRegister(opsQueued)
41
-
42
- // 10 operations queued by the goroutine managing incoming requests.
43
- opsQueued.Add(10)
44
- // A worker goroutine has picked up a waiting operation.
45
- opsQueued.Dec()
46
- // And once more...
47
- opsQueued.Dec()
48
-}
49
-
50
-func ExampleGaugeVec() {
51
- binaryVersion := flag.String("binary_version", "debug", "Version of the binary: debug, canary, production.")
52
- flag.Parse()
53
-
54
- opsQueued := prometheus.NewGaugeVec(
55
- prometheus.GaugeOpts{
56
- Namespace: "our_company",
57
- Subsystem: "blob_storage",
58
- Name: "ops_queued",
59
- Help: "Number of blob storage operations waiting to be processed, partitioned by user and type.",
60
- ConstLabels: prometheus.Labels{"binary_version": *binaryVersion},
61
- },
62
- []string{
63
- // Which user has requested the operation?
64
- "user",
65
- // Of what type is the operation?
66
- "type",
67
- },
68
- )
69
- prometheus.MustRegister(opsQueued)
70
-
71
- // Increase a value using compact (but order-sensitive!) WithLabelValues().
72
- opsQueued.WithLabelValues("bob", "put").Add(4)
73
- // Increase a value with a map using WithLabels. More verbose, but order
74
- // doesn't matter anymore.
75
- opsQueued.With(prometheus.Labels{"type": "delete", "user": "alice"}).Inc()
76
-}
77
-
78
-func ExampleGaugeFunc() {
79
- if err := prometheus.Register(prometheus.NewGaugeFunc(
80
- prometheus.GaugeOpts{
81
- Subsystem: "runtime",
82
- Name: "goroutines_count",
83
- Help: "Number of goroutines that currently exist.",
84
- },
85
- func() float64 { return float64(runtime.NumGoroutine()) },
86
- )); err == nil {
87
- fmt.Println("GaugeFunc 'goroutines_count' registered.")
88
- }
89
- // Note that the count of goroutines is a gauge (and not a counter) as
90
- // it can go up and down.
91
-
92
- // Output:
93
- // GaugeFunc 'goroutines_count' registered.
94
-}
95
-
96
-func ExampleCounter() {
97
- pushCounter := prometheus.NewCounter(prometheus.CounterOpts{
98
- Name: "repository_pushes", // Note: No help string...
99
- })
100
- err := prometheus.Register(pushCounter) // ... so this will return an error.
101
- if err != nil {
102
- fmt.Println("Push counter couldn't be registered, no counting will happen:", err)
103
- return
104
- }
105
-
106
- // Try it once more, this time with a help string.
107
- pushCounter = prometheus.NewCounter(prometheus.CounterOpts{
108
- Name: "repository_pushes",
109
- Help: "Number of pushes to external repository.",
110
- })
111
- err = prometheus.Register(pushCounter)
112
- if err != nil {
113
- fmt.Println("Push counter couldn't be registered AGAIN, no counting will happen:", err)
114
- return
115
- }
116
-
117
- pushComplete := make(chan struct{})
118
- // TODO: Start a goroutine that performs repository pushes and reports
119
- // each completion via the channel.
120
- for _ = range pushComplete {
121
- pushCounter.Inc()
122
- }
123
- // Output:
124
- // Push counter couldn't be registered, no counting will happen: descriptor Desc{fqName: "repository_pushes", help: "", constLabels: {}, variableLabels: []} is invalid: empty help string
125
-}
126
-
127
-func ExampleCounterVec() {
128
- binaryVersion := flag.String("environment", "test", "Execution environment: test, staging, production.")
129
- flag.Parse()
130
-
131
- httpReqs := prometheus.NewCounterVec(
132
- prometheus.CounterOpts{
133
- Name: "http_requests_total",
134
- Help: "How many HTTP requests processed, partitioned by status code and HTTP method.",
135
- ConstLabels: prometheus.Labels{"env": *binaryVersion},
136
- },
137
- []string{"code", "method"},
138
- )
139
- prometheus.MustRegister(httpReqs)
140
-
141
- httpReqs.WithLabelValues("404", "POST").Add(42)
142
-
143
- // If you have to access the same set of labels very frequently, it
144
- // might be good to retrieve the metric only once and keep a handle to
145
- // it. But beware of deletion of that metric, see below!
146
- m := httpReqs.WithLabelValues("200", "GET")
147
- for i := 0; i < 1000000; i++ {
148
- m.Inc()
149
- }
150
- // Delete a metric from the vector. If you have previously kept a handle
151
- // to that metric (as above), future updates via that handle will go
152
- // unseen (even if you re-create a metric with the same label set
153
- // later).
154
- httpReqs.DeleteLabelValues("200", "GET")
155
- // Same thing with the more verbose Labels syntax.
156
- httpReqs.Delete(prometheus.Labels{"method": "GET", "code": "200"})
157
-}
158
-
159
-func ExampleInstrumentHandler() {
160
- // Handle the "/doc" endpoint with the standard http.FileServer handler.
161
- // By wrapping the handler with InstrumentHandler, request count,
162
- // request and response sizes, and request latency are automatically
163
- // exported to Prometheus, partitioned by HTTP status code and method
164
- // and by the handler name (here "fileserver").
165
- http.Handle("/doc", prometheus.InstrumentHandler(
166
- "fileserver", http.FileServer(http.Dir("/usr/share/doc")),
167
- ))
168
- // The Prometheus handler still has to be registered to handle the
169
- // "/metrics" endpoint. The handler returned by prometheus.Handler() is
170
- // already instrumented - with "prometheus" as the handler name. In this
171
- // example, we want the handler name to be "metrics", so we instrument
172
- // the uninstrumented Prometheus handler ourselves.
173
- http.Handle("/metrics", prometheus.InstrumentHandler(
174
- "metrics", prometheus.UninstrumentedHandler(),
175
- ))
176
-}
177
-
178
-func ExampleLabelPairSorter() {
179
- labelPairs := []*dto.LabelPair{
180
- &dto.LabelPair{Name: proto.String("status"), Value: proto.String("404")},
181
- &dto.LabelPair{Name: proto.String("method"), Value: proto.String("get")},
182
- }
183
-
184
- sort.Sort(prometheus.LabelPairSorter(labelPairs))
185
-
186
- fmt.Println(labelPairs)
187
- // Output:
188
- // [name:"method" value:"get" name:"status" value:"404" ]
189
-}
190
-
191
-func ExampleRegister() {
192
- // Imagine you have a worker pool and want to count the tasks completed.
193
- taskCounter := prometheus.NewCounter(prometheus.CounterOpts{
194
- Subsystem: "worker_pool",
195
- Name: "completed_tasks_total",
196
- Help: "Total number of tasks completed.",
197
- })
198
- // This will register fine.
199
- if err := prometheus.Register(taskCounter); err != nil {
200
- fmt.Println(err)
201
- } else {
202
- fmt.Println("taskCounter registered.")
203
- }
204
- // Don't forget to tell the HTTP server about the Prometheus handler.
205
- // (In a real program, you still need to start the HTTP server...)
206
- http.Handle("/metrics", prometheus.Handler())
207
-
208
- // Now you can start workers and give every one of them a pointer to
209
- // taskCounter and let it increment it whenever it completes a task.
210
- taskCounter.Inc() // This has to happen somewhere in the worker code.
211
-
212
- // But wait, you want to see how individual workers perform. So you need
213
- // a vector of counters, with one element for each worker.
214
- taskCounterVec := prometheus.NewCounterVec(
215
- prometheus.CounterOpts{
216
- Subsystem: "worker_pool",
217
- Name: "completed_tasks_total",
218
- Help: "Total number of tasks completed.",
219
- },
220
- []string{"worker_id"},
221
- )
222
-
223
- // Registering will fail because we already have a metric of that name.
224
- if err := prometheus.Register(taskCounterVec); err != nil {
225
- fmt.Println("taskCounterVec not registered:", err)
226
- } else {
227
- fmt.Println("taskCounterVec registered.")
228
- }
229
-
230
- // To fix, first unregister the old taskCounter.
231
- if prometheus.Unregister(taskCounter) {
232
- fmt.Println("taskCounter unregistered.")
233
- }
234
-
235
- // Try registering taskCounterVec again.
236
- if err := prometheus.Register(taskCounterVec); err != nil {
237
- fmt.Println("taskCounterVec not registered:", err)
238
- } else {
239
- fmt.Println("taskCounterVec registered.")
240
- }
241
- // Bummer! Still doesn't work.
242
-
243
- // Prometheus will not allow you to ever export metrics with
244
- // inconsistent help strings or label names. After unregistering, the
245
- // unregistered metrics will cease to show up in the /metrics HTTP
246
- // response, but the registry still remembers that those metrics had
247
- // been exported before. For this example, we will now choose a
248
- // different name. (In a real program, you would obviously not export
249
- // the obsolete metric in the first place.)
250
- taskCounterVec = prometheus.NewCounterVec(
251
- prometheus.CounterOpts{
252
- Subsystem: "worker_pool",
253
- Name: "completed_tasks_by_id",
254
- Help: "Total number of tasks completed.",
255
- },
256
- []string{"worker_id"},
257
- )
258
- if err := prometheus.Register(taskCounterVec); err != nil {
259
- fmt.Println("taskCounterVec not registered:", err)
260
- } else {
261
- fmt.Println("taskCounterVec registered.")
262
- }
263
- // Finally it worked!
264
-
265
- // The workers have to tell taskCounterVec their id to increment the
266
- // right element in the metric vector.
267
- taskCounterVec.WithLabelValues("42").Inc() // Code from worker 42.
268
-
269
- // Each worker could also keep a reference to their own counter element
270
- // around. Pick the counter at initialization time of the worker.
271
- myCounter := taskCounterVec.WithLabelValues("42") // From worker 42 initialization code.
272
- myCounter.Inc() // Somewhere in the code of that worker.
273
-
274
- // Note that something like WithLabelValues("42", "spurious arg") would
275
- // panic (because you have provided too many label values). If you want
276
- // to get an error instead, use GetMetricWithLabelValues(...) instead.
277
- notMyCounter, err := taskCounterVec.GetMetricWithLabelValues("42", "spurious arg")
278
- if err != nil {
279
- fmt.Println("Worker initialization failed:", err)
280
- }
281
- if notMyCounter == nil {
282
- fmt.Println("notMyCounter is nil.")
283
- }
284
-
285
- // A different (and somewhat tricky) approach is to use
286
- // ConstLabels. ConstLabels are pairs of label names and label values
287
- // that never change. You might ask what those labels are good for (and
288
- // rightfully so - if they never change, they could as well be part of
289
- // the metric name). There are essentially two use-cases: The first is
290
- // if labels are constant throughout the lifetime of a binary execution,
291
- // but they vary over time or between different instances of a running
292
- // binary. The second is what we have here: Each worker creates and
293
- // registers an own Counter instance where the only difference is in the
294
- // value of the ConstLabels. Those Counters can all be registered
295
- // because the different ConstLabel values guarantee that each worker
296
- // will increment a different Counter metric.
297
- counterOpts := prometheus.CounterOpts{
298
- Subsystem: "worker_pool",
299
- Name: "completed_tasks",
300
- Help: "Total number of tasks completed.",
301
- ConstLabels: prometheus.Labels{"worker_id": "42"},
302
- }
303
- taskCounterForWorker42 := prometheus.NewCounter(counterOpts)
304
- if err := prometheus.Register(taskCounterForWorker42); err != nil {
305
- fmt.Println("taskCounterVForWorker42 not registered:", err)
306
- } else {
307
- fmt.Println("taskCounterForWorker42 registered.")
308
- }
309
- // Obviously, in real code, taskCounterForWorker42 would be a member
310
- // variable of a worker struct, and the "42" would be retrieved with a
311
- // GetId() method or something. The Counter would be created and
312
- // registered in the initialization code of the worker.
313
-
314
- // For the creation of the next Counter, we can recycle
315
- // counterOpts. Just change the ConstLabels.
316
- counterOpts.ConstLabels = prometheus.Labels{"worker_id": "2001"}
317
- taskCounterForWorker2001 := prometheus.NewCounter(counterOpts)
318
- if err := prometheus.Register(taskCounterForWorker2001); err != nil {
319
- fmt.Println("taskCounterVForWorker2001 not registered:", err)
320
- } else {
321
- fmt.Println("taskCounterForWorker2001 registered.")
322
- }
323
-
324
- taskCounterForWorker2001.Inc()
325
- taskCounterForWorker42.Inc()
326
- taskCounterForWorker2001.Inc()
327
-
328
- // Yet another approach would be to turn the workers themselves into
329
- // Collectors and register them. See the Collector example for details.
330
-
331
- // Output:
332
- // taskCounter registered.
333
- // taskCounterVec not registered: a previously registered descriptor with the same fully-qualified name as Desc{fqName: "worker_pool_completed_tasks_total", help: "Total number of tasks completed.", constLabels: {}, variableLabels: [worker_id]} has different label names or a different help string
334
- // taskCounter unregistered.
335
- // taskCounterVec not registered: a previously registered descriptor with the same fully-qualified name as Desc{fqName: "worker_pool_completed_tasks_total", help: "Total number of tasks completed.", constLabels: {}, variableLabels: [worker_id]} has different label names or a different help string
336
- // taskCounterVec registered.
337
- // Worker initialization failed: inconsistent label cardinality
338
- // notMyCounter is nil.
339
- // taskCounterForWorker42 registered.
340
- // taskCounterForWorker2001 registered.
341
-}
342
-
343
-func ExampleSummary() {
344
- temps := prometheus.NewSummary(prometheus.SummaryOpts{
345
- Name: "pond_temperature_celsius",
346
- Help: "The temperature of the frog pond.", // Sorry, we can't measure how badly it smells.
347
- })
348
-
349
- // Simulate some observations.
350
- for i := 0; i < 1000; i++ {
351
- temps.Observe(30 + math.Floor(120*math.Sin(float64(i)*0.1))/10)
352
- }
353
-
354
- // Just for demonstration, let's check the state of the summary by
355
- // (ab)using its Write method (which is usually only used by Prometheus
356
- // internally).
357
- metric := &dto.Metric{}
358
- temps.Write(metric)
359
- fmt.Println(proto.MarshalTextString(metric))
360
-
361
- // Output:
362
- // summary: <
363
- // sample_count: 1000
364
- // sample_sum: 29969.50000000001
365
- // quantile: <
366
- // quantile: 0.5
367
- // value: 31.1
368
- // >
369
- // quantile: <
370
- // quantile: 0.9
371
- // value: 41.3
372
- // >
373
- // quantile: <
374
- // quantile: 0.99
375
- // value: 41.9
376
- // >
377
- // >
378
-}
379
-
380
-func ExampleSummaryVec() {
381
- temps := prometheus.NewSummaryVec(
382
- prometheus.SummaryOpts{
383
- Name: "pond_temperature_celsius",
384
- Help: "The temperature of the frog pond.", // Sorry, we can't measure how badly it smells.
385
- },
386
- []string{"species"},
387
- )
388
-
389
- // Simulate some observations.
390
- for i := 0; i < 1000; i++ {
391
- temps.WithLabelValues("litoria-caerulea").Observe(30 + math.Floor(120*math.Sin(float64(i)*0.1))/10)
392
- temps.WithLabelValues("lithobates-catesbeianus").Observe(32 + math.Floor(100*math.Cos(float64(i)*0.11))/10)
393
- }
394
-
395
- // Just for demonstration, let's check the state of the summary vector
396
- // by (ab)using its Collect method and the Write method of its elements
397
- // (which is usually only used by Prometheus internally - code like the
398
- // following will never appear in your own code).
399
- metricChan := make(chan prometheus.Metric)
400
- go func() {
401
- defer close(metricChan)
402
- temps.Collect(metricChan)
403
- }()
404
-
405
- metricStrings := []string{}
406
- for metric := range metricChan {
407
- dtoMetric := &dto.Metric{}
408
- metric.Write(dtoMetric)
409
- metricStrings = append(metricStrings, proto.MarshalTextString(dtoMetric))
410
- }
411
- sort.Strings(metricStrings) // For reproducible print order.
412
- fmt.Println(metricStrings)
413
-
414
- // Output:
415
- // [label: <
416
- // name: "species"
417
- // value: "lithobates-catesbeianus"
418
- // >
419
- // summary: <
420
- // sample_count: 1000
421
- // sample_sum: 31956.100000000017
422
- // quantile: <
423
- // quantile: 0.5
424
- // value: 32.4
425
- // >
426
- // quantile: <
427
- // quantile: 0.9
428
- // value: 41.4
429
- // >
430
- // quantile: <
431
- // quantile: 0.99
432
- // value: 41.9
433
- // >
434
- // >
435
- // label: <
436
- // name: "species"
437
- // value: "litoria-caerulea"
438
- // >
439
- // summary: <
440
- // sample_count: 1000
441
- // sample_sum: 29969.50000000001
442
- // quantile: <
443
- // quantile: 0.5
444
- // value: 31.1
445
- // >
446
- // quantile: <
447
- // quantile: 0.9
448
- // value: 41.3
449
- // >
450
- // quantile: <
451
- // quantile: 0.99
452
- // value: 41.9
453
- // >
454
- // >
455
- // ]
456
-}
457
-
458
-func ExampleConstSummary() {
459
- desc := prometheus.NewDesc(
460
- "http_request_duration_seconds",
461
- "A summary of the HTTP request durations.",
462
- []string{"code", "method"},
463
- prometheus.Labels{"owner": "example"},
464
- )
465
-
466
- // Create a constant summary from values we got from a 3rd party telemetry system.
467
- s := prometheus.MustNewConstSummary(
468
- desc,
469
- 4711, 403.34,
470
- map[float64]float64{0.5: 42.3, 0.9: 323.3},
471
- "200", "get",
472
- )
473
-
474
- // Just for demonstration, let's check the state of the summary by
475
- // (ab)using its Write method (which is usually only used by Prometheus
476
- // internally).
477
- metric := &dto.Metric{}
478
- s.Write(metric)
479
- fmt.Println(proto.MarshalTextString(metric))
480
-
481
- // Output:
482
- // label: <
483
- // name: "code"
484
- // value: "200"
485
- // >
486
- // label: <
487
- // name: "method"
488
- // value: "get"
489
- // >
490
- // label: <
491
- // name: "owner"
492
- // value: "example"
493
- // >
494
- // summary: <
495
- // sample_count: 4711
496
- // sample_sum: 403.34
497
- // quantile: <
498
- // quantile: 0.5
499
- // value: 42.3
500
- // >
501
- // quantile: <
502
- // quantile: 0.9
503
- // value: 323.3
504
- // >
505
- // >
506
-}
507
-
508
-func ExampleHistogram() {
509
- temps := prometheus.NewHistogram(prometheus.HistogramOpts{
510
- Name: "pond_temperature_celsius",
511
- Help: "The temperature of the frog pond.", // Sorry, we can't measure how badly it smells.
512
- Buckets: prometheus.LinearBuckets(20, 5, 5), // 5 buckets, each 5 centigrade wide.
513
- })
514
-
515
- // Simulate some observations.
516
- for i := 0; i < 1000; i++ {
517
- temps.Observe(30 + math.Floor(120*math.Sin(float64(i)*0.1))/10)
518
- }
519
-
520
- // Just for demonstration, let's check the state of the histogram by
521
- // (ab)using its Write method (which is usually only used by Prometheus
522
- // internally).
523
- metric := &dto.Metric{}
524
- temps.Write(metric)
525
- fmt.Println(proto.MarshalTextString(metric))
526
-
527
- // Output:
528
- // histogram: <
529
- // sample_count: 1000
530
- // sample_sum: 29969.50000000001
531
- // bucket: <
532
- // cumulative_count: 192
533
- // upper_bound: 20
534
- // >
535
- // bucket: <
536
- // cumulative_count: 366
537
- // upper_bound: 25
538
- // >
539
- // bucket: <
540
- // cumulative_count: 501
541
- // upper_bound: 30
542
- // >
543
- // bucket: <
544
- // cumulative_count: 638
545
- // upper_bound: 35
546
- // >
547
- // bucket: <
548
- // cumulative_count: 816
549
- // upper_bound: 40
550
- // >
551
- // >
552
-}
553
-
554
-func ExampleConstHistogram() {
555
- desc := prometheus.NewDesc(
556
- "http_request_duration_seconds",
557
- "A histogram of the HTTP request durations.",
558
- []string{"code", "method"},
559
- prometheus.Labels{"owner": "example"},
560
- )
561
-
562
- // Create a constant histogram from values we got from a 3rd party telemetry system.
563
- h := prometheus.MustNewConstHistogram(
564
- desc,
565
- 4711, 403.34,
566
- map[float64]uint64{25: 121, 50: 2403, 100: 3221, 200: 4233},
567
- "200", "get",
568
- )
569
-
570
- // Just for demonstration, let's check the state of the histogram by
571
- // (ab)using its Write method (which is usually only used by Prometheus
572
- // internally).
573
- metric := &dto.Metric{}
574
- h.Write(metric)
575
- fmt.Println(proto.MarshalTextString(metric))
576
-
577
- // Output:
578
- // label: <
579
- // name: "code"
580
- // value: "200"
581
- // >
582
- // label: <
583
- // name: "method"
584
- // value: "get"
585
- // >
586
- // label: <
587
- // name: "owner"
588
- // value: "example"
589
- // >
590
- // histogram: <
591
- // sample_count: 4711
592
- // sample_sum: 403.34
593
- // bucket: <
594
- // cumulative_count: 121
595
- // upper_bound: 25
596
- // >
597
- // bucket: <
598
- // cumulative_count: 2403
599
- // upper_bound: 50
600
- // >
601
- // bucket: <
602
- // cumulative_count: 3221
603
- // upper_bound: 100
604
- // >
605
- // bucket: <
606
- // cumulative_count: 4233
607
- // upper_bound: 200
608
- // >
609
- // >
610
-}
611
-
612
-func ExamplePushCollectors() {
613
- hostname, _ := os.Hostname()
614
- completionTime := prometheus.NewGauge(prometheus.GaugeOpts{
615
- Name: "db_backup_last_completion_time",
616
- Help: "The timestamp of the last succesful completion of a DB backup.",
617
- })
618
- completionTime.Set(float64(time.Now().Unix()))
619
- if err := prometheus.PushCollectors(
620
- "db_backup", hostname,
621
- "http://pushgateway:9091",
622
- completionTime,
623
- ); err != nil {
624
- fmt.Println("Could not push completion time to Pushgateway:", err)
625
- }
626
-}
Godeps/_workspace/src/github.com/prometheus/client_golang/prometheus/expvar.go
deleted
-119
@@ -1,119 +0,0 @@
1
-// Copyright 2014 The Prometheus Authors
2
-// Licensed under the Apache License, Version 2.0 (the "License");
3
-// you may not use this file except in compliance with the License.
4
-// You may obtain a copy of the License at
5
-//
6
-// http://www.apache.org/licenses/LICENSE-2.0
7
-//
8
-// Unless required by applicable law or agreed to in writing, software
9
-// distributed under the License is distributed on an "AS IS" BASIS,
10
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
-// See the License for the specific language governing permissions and
12
-// limitations under the License.
13
-
14
-package prometheus
15
-
16
-import (
17
- "encoding/json"
18
- "expvar"
19
-)
20
-
21
-// ExpvarCollector collects metrics from the expvar interface. It provides a
22
-// quick way to expose numeric values that are already exported via expvar as
23
-// Prometheus metrics. Note that the data models of expvar and Prometheus are
24
-// fundamentally different, and that the ExpvarCollector is inherently
25
-// slow. Thus, the ExpvarCollector is probably great for experiments and
26
-// prototying, but you should seriously consider a more direct implementation of
27
-// Prometheus metrics for monitoring production systems.
28
-//
29
-// Use NewExpvarCollector to create new instances.
30
-type ExpvarCollector struct {
31
- exports map[string]*Desc
32
-}
33
-
34
-// NewExpvarCollector returns a newly allocated ExpvarCollector that still has
35
-// to be registered with the Prometheus registry.
36
-//
37
-// The exports map has the following meaning:
38
-//
39
-// The keys in the map correspond to expvar keys, i.e. for every expvar key you
40
-// want to export as Prometheus metric, you need an entry in the exports
41
-// map. The descriptor mapped to each key describes how to export the expvar
42
-// value. It defines the name and the help string of the Prometheus metric
43
-// proxying the expvar value. The type will always be Untyped.
44
-//
45
-// For descriptors without variable labels, the expvar value must be a number or
46
-// a bool. The number is then directly exported as the Prometheus sample
47
-// value. (For a bool, 'false' translates to 0 and 'true' to 1). Expvar values
48
-// that are not numbers or bools are silently ignored.
49
-//
50
-// If the descriptor has one variable label, the expvar value must be an expvar
51
-// map. The keys in the expvar map become the various values of the one
52
-// Prometheus label. The values in the expvar map must be numbers or bools again
53
-// as above.
54
-//
55
-// For descriptors with more than one variable label, the expvar must be a
56
-// nested expvar map, i.e. where the values of the topmost map are maps again
57
-// etc. until a depth is reached that corresponds to the number of labels. The
58
-// leaves of that structure must be numbers or bools as above to serve as the
59
-// sample values.
60
-//
61
-// Anything that does not fit into the scheme above is silently ignored.
62
-func NewExpvarCollector(exports map[string]*Desc) *ExpvarCollector {
63
- return &ExpvarCollector{
64
- exports: exports,
65
- }
66
-}
67
-
68
-// Describe implements Collector.
69
-func (e *ExpvarCollector) Describe(ch chan<- *Desc) {
70
- for _, desc := range e.exports {
71
- ch <- desc
72
- }
73
-}
74
-
75
-// Collect implements Collector.
76
-func (e *ExpvarCollector) Collect(ch chan<- Metric) {
77
- for name, desc := range e.exports {
78
- var m Metric
79
- expVar := expvar.Get(name)
80
- if expVar == nil {
81
- continue
82
- }
83
- var v interface{}
84
- labels := make([]string, len(desc.variableLabels))
85
- if err := json.Unmarshal([]byte(expVar.String()), &v); err != nil {
86
- ch <- NewInvalidMetric(desc, err)
87
- continue
88
- }
89
- var processValue func(v interface{}, i int)
90
- processValue = func(v interface{}, i int) {
91
- if i >= len(labels) {
92
- copiedLabels := append(make([]string, 0, len(labels)), labels...)
93
- switch v := v.(type) {
94
- case float64:
95
- m = MustNewConstMetric(desc, UntypedValue, v, copiedLabels...)
96
- case bool:
97
- if v {
98
- m = MustNewConstMetric(desc, UntypedValue, 1, copiedLabels...)
99
- } else {
100
- m = MustNewConstMetric(desc, UntypedValue, 0, copiedLabels...)
101
- }
102
- default:
103
- return
104
- }
105
- ch <- m
106
- return
107
- }
108
- vm, ok := v.(map[string]interface{})
109
- if !ok {
110
- return
111
- }
112
- for lv, val := range vm {
113
- labels[i] = lv
114
- processValue(val, i+1)
115
- }
116
- }
117
- processValue(v, 0)
118
- }
119
-}
Godeps/_workspace/src/github.com/prometheus/client_golang/prometheus/expvar_test.go
deleted
-97
@@ -1,97 +0,0 @@
1
-// Copyright 2014 The Prometheus Authors
2
-// Licensed under the Apache License, Version 2.0 (the "License");
3
-// you may not use this file except in compliance with the License.
4
-// You may obtain a copy of the License at
5
-//
6
-// http://www.apache.org/licenses/LICENSE-2.0
7
-//
8
-// Unless required by applicable law or agreed to in writing, software
9
-// distributed under the License is distributed on an "AS IS" BASIS,
10
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
-// See the License for the specific language governing permissions and
12
-// limitations under the License.
13
-
14
-package prometheus_test
15
-
16
-import (
17
- "expvar"
18
- "fmt"
19
- "sort"
20
- "strings"
21
-
22
- dto "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/prometheus/client_model/go"
23
-
24
- "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/prometheus/client_golang/prometheus"
25
-)
26
-
27
-func ExampleExpvarCollector() {
28
- expvarCollector := prometheus.NewExpvarCollector(map[string]*prometheus.Desc{
29
- "memstats": prometheus.NewDesc(
30
- "expvar_memstats",
31
- "All numeric memstats as one metric family. Not a good role-model, actually... ;-)",
32
- []string{"type"}, nil,
33
- ),
34
- "lone-int": prometheus.NewDesc(
35
- "expvar_lone_int",
36
- "Just an expvar int as an example.",
37
- nil, nil,
38
- ),
39
- "http-request-map": prometheus.NewDesc(
40
- "expvar_http_request_total",
41
- "How many http requests processed, partitioned by status code and http method.",
42
- []string{"code", "method"}, nil,
43
- ),
44
- })
45
- prometheus.MustRegister(expvarCollector)
46
-
47
- // The Prometheus part is done here. But to show that this example is
48
- // doing anything, we have to manually export something via expvar. In
49
- // real-life use-cases, some library would already have exported via
50
- // expvar what we want to re-export as Prometheus metrics.
51
- expvar.NewInt("lone-int").Set(42)
52
- expvarMap := expvar.NewMap("http-request-map")
53
- var (
54
- expvarMap1, expvarMap2 expvar.Map
55
- expvarInt11, expvarInt12, expvarInt21, expvarInt22 expvar.Int
56
- )
57
- expvarMap1.Init()
58
- expvarMap2.Init()
59
- expvarInt11.Set(3)
60
- expvarInt12.Set(13)
61
- expvarInt21.Set(11)
62
- expvarInt22.Set(212)
63
- expvarMap1.Set("POST", &expvarInt11)
64
- expvarMap1.Set("GET", &expvarInt12)
65
- expvarMap2.Set("POST", &expvarInt21)
66
- expvarMap2.Set("GET", &expvarInt22)
67
- expvarMap.Set("404", &expvarMap1)
68
- expvarMap.Set("200", &expvarMap2)
69
- // Results in the following expvar map:
70
- // "http-request-count": {"200": {"POST": 11, "GET": 212}, "404": {"POST": 3, "GET": 13}}
71
-
72
- // Let's see what the scrape would yield, but exclude the memstats metrics.
73
- metricStrings := []string{}
74
- metric := dto.Metric{}
75
- metricChan := make(chan prometheus.Metric)
76
- go func() {
77
- expvarCollector.Collect(metricChan)
78
- close(metricChan)
79
- }()
80
- for m := range metricChan {
81
- if strings.Index(m.Desc().String(), "expvar_memstats") == -1 {
82
- metric.Reset()
83
- m.Write(&metric)
84
- metricStrings = append(metricStrings, metric.String())
85
- }
86
- }
87
- sort.Strings(metricStrings)
88
- for _, s := range metricStrings {
89
- fmt.Println(strings.TrimRight(s, " "))
90
- }
91
- // Output:
92
- // label:<name:"code" value:"200" > label:<name:"method" value:"GET" > untyped:<value:212 >
93
- // label:<name:"code" value:"200" > label:<name:"method" value:"POST" > untyped:<value:11 >
94
- // label:<name:"code" value:"404" > label:<name:"method" value:"GET" > untyped:<value:13 >
95
- // label:<name:"code" value:"404" > label:<name:"method" value:"POST" > untyped:<value:3 >
96
- // untyped:<value:42 >
97
-}
Godeps/_workspace/src/github.com/prometheus/client_golang/prometheus/gauge.go
deleted
-147
@@ -1,147 +0,0 @@
1
-// Copyright 2014 The Prometheus Authors
2
-// Licensed under the Apache License, Version 2.0 (the "License");
3
-// you may not use this file except in compliance with the License.
4
-// You may obtain a copy of the License at
5
-//
6
-// http://www.apache.org/licenses/LICENSE-2.0
7
-//
8
-// Unless required by applicable law or agreed to in writing, software
9
-// distributed under the License is distributed on an "AS IS" BASIS,
10
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
-// See the License for the specific language governing permissions and
12
-// limitations under the License.
13
-
14
-package prometheus
15
-
16
-import "hash/fnv"
17
-
18
-// Gauge is a Metric that represents a single numerical value that can
19
-// arbitrarily go up and down.
20
-//
21
-// A Gauge is typically used for measured values like temperatures or current
22
-// memory usage, but also "counts" that can go up and down, like the number of
23
-// running goroutines.
24
-//
25
-// To create Gauge instances, use NewGauge.
26
-type Gauge interface {
27
- Metric
28
- Collector
29
-
30
- // Set sets the Gauge to an arbitrary value.
31
- Set(float64)
32
- // Inc increments the Gauge by 1.
33
- Inc()
34
- // Dec decrements the Gauge by 1.
35
- Dec()
36
- // Add adds the given value to the Gauge. (The value can be
37
- // negative, resulting in a decrease of the Gauge.)
38
- Add(float64)
39
- // Sub subtracts the given value from the Gauge. (The value can be
40
- // negative, resulting in an increase of the Gauge.)
41
- Sub(float64)
42
-}
43
-
44
-// GaugeOpts is an alias for Opts. See there for doc comments.
45
-type GaugeOpts Opts
46
-
47
-// NewGauge creates a new Gauge based on the provided GaugeOpts.
48
-func NewGauge(opts GaugeOpts) Gauge {
49
- return newValue(NewDesc(
50
- BuildFQName(opts.Namespace, opts.Subsystem, opts.Name),
51
- opts.Help,
52
- nil,
53
- opts.ConstLabels,
54
- ), GaugeValue, 0)
55
-}
56
-
57
-// GaugeVec is a Collector that bundles a set of Gauges that all share the same
58
-// Desc, but have different values for their variable labels. This is used if
59
-// you want to count the same thing partitioned by various dimensions
60
-// (e.g. number of operations queued, partitioned by user and operation
61
-// type). Create instances with NewGaugeVec.
62
-type GaugeVec struct {
63
- MetricVec
64
-}
65
-
66
-// NewGaugeVec creates a new GaugeVec based on the provided GaugeOpts and
67
-// partitioned by the given label names. At least one label name must be
68
-// provided.
69
-func NewGaugeVec(opts GaugeOpts, labelNames []string) *GaugeVec {
70
- desc := NewDesc(
71
- BuildFQName(opts.Namespace, opts.Subsystem, opts.Name),
72
- opts.Help,
73
- labelNames,
74
- opts.ConstLabels,
75
- )
76
- return &GaugeVec{
77
- MetricVec: MetricVec{
78
- children: map[uint64]Metric{},
79
- desc: desc,
80
- hash: fnv.New64a(),
81
- newMetric: func(lvs ...string) Metric {
82
- return newValue(desc, GaugeValue, 0, lvs...)
83
- },
84
- },
85
- }
86
-}
87
-
88
-// GetMetricWithLabelValues replaces the method of the same name in
89
-// MetricVec. The difference is that this method returns a Gauge and not a
90
-// Metric so that no type conversion is required.
91
-func (m *GaugeVec) GetMetricWithLabelValues(lvs ...string) (Gauge, error) {
92
- metric, err := m.MetricVec.GetMetricWithLabelValues(lvs...)
93
- if metric != nil {
94
- return metric.(Gauge), err
95
- }
96
- return nil, err
97
-}
98
-
99
-// GetMetricWith replaces the method of the same name in MetricVec. The
100
-// difference is that this method returns a Gauge and not a Metric so that no
101
-// type conversion is required.
102
-func (m *GaugeVec) GetMetricWith(labels Labels) (Gauge, error) {
103
- metric, err := m.MetricVec.GetMetricWith(labels)
104
- if metric != nil {
105
- return metric.(Gauge), err
106
- }
107
- return nil, err
108
-}
109
-
110
-// WithLabelValues works as GetMetricWithLabelValues, but panics where
111
-// GetMetricWithLabelValues would have returned an error. By not returning an
112
-// error, WithLabelValues allows shortcuts like
113
-// myVec.WithLabelValues("404", "GET").Add(42)
114
-func (m *GaugeVec) WithLabelValues(lvs ...string) Gauge {
115
- return m.MetricVec.WithLabelValues(lvs...).(Gauge)
116
-}
117
-
118
-// With works as GetMetricWith, but panics where GetMetricWithLabels would have
119
-// returned an error. By not returning an error, With allows shortcuts like
120
-// myVec.With(Labels{"code": "404", "method": "GET"}).Add(42)
121
-func (m *GaugeVec) With(labels Labels) Gauge {
122
- return m.MetricVec.With(labels).(Gauge)
123
-}
124
-
125
-// GaugeFunc is a Gauge whose value is determined at collect time by calling a
126
-// provided function.
127
-//
128
-// To create GaugeFunc instances, use NewGaugeFunc.
129
-type GaugeFunc interface {
130
- Metric
131
- Collector
132
-}
133
-
134
-// NewGaugeFunc creates a new GaugeFunc based on the provided GaugeOpts. The
135
-// value reported is determined by calling the given function from within the
136
-// Write method. Take into account that metric collection may happen
137
-// concurrently. If that results in concurrent calls to Write, like in the case
138
-// where a GaugeFunc is directly registered with Prometheus, the provided
139
-// function must be concurrency-safe.
140
-func NewGaugeFunc(opts GaugeOpts, function func() float64) GaugeFunc {
141
- return newValueFunc(NewDesc(
142
- BuildFQName(opts.Namespace, opts.Subsystem, opts.Name),
143
- opts.Help,
144
- nil,
145
- opts.ConstLabels,
146
- ), GaugeValue, function)
147
-}
Godeps/_workspace/src/github.com/prometheus/client_golang/prometheus/gauge_test.go
deleted
-182
@@ -1,182 +0,0 @@
1
-// Copyright 2014 The Prometheus Authors
2
-// Licensed under the Apache License, Version 2.0 (the "License");
3
-// you may not use this file except in compliance with the License.
4
-// You may obtain a copy of the License at
5
-//
6
-// http://www.apache.org/licenses/LICENSE-2.0
7
-//
8
-// Unless required by applicable law or agreed to in writing, software
9
-// distributed under the License is distributed on an "AS IS" BASIS,
10
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
-// See the License for the specific language governing permissions and
12
-// limitations under the License.
13
-
14
-package prometheus
15
-
16
-import (
17
- "math"
18
- "math/rand"
19
- "sync"
20
- "testing"
21
- "testing/quick"
22
-
23
- dto "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/prometheus/client_model/go"
24
-)
25
-
26
-func listenGaugeStream(vals, result chan float64, done chan struct{}) {
27
- var sum float64
28
-outer:
29
- for {
30
- select {
31
- case <-done:
32
- close(vals)
33
- for v := range vals {
34
- sum += v
35
- }
36
- break outer
37
- case v := <-vals:
38
- sum += v
39
- }
40
- }
41
- result <- sum
42
- close(result)
43
-}
44
-
45
-func TestGaugeConcurrency(t *testing.T) {
46
- it := func(n uint32) bool {
47
- mutations := int(n % 10000)
48
- concLevel := int(n%15 + 1)
49
-
50
- var start, end sync.WaitGroup
51
- start.Add(1)
52
- end.Add(concLevel)
53
-
54
- sStream := make(chan float64, mutations*concLevel)
55
- result := make(chan float64)
56
- done := make(chan struct{})
57
-
58
- go listenGaugeStream(sStream, result, done)
59
- go func() {
60
- end.Wait()
61
- close(done)
62
- }()
63
-
64
- gge := NewGauge(GaugeOpts{
65
- Name: "test_gauge",
66
- Help: "no help can be found here",
67
- })
68
- for i := 0; i < concLevel; i++ {
69
- vals := make([]float64, mutations)
70
- for j := 0; j < mutations; j++ {
71
- vals[j] = rand.Float64() - 0.5
72
- }
73
-
74
- go func(vals []float64) {
75
- start.Wait()
76
- for _, v := range vals {
77
- sStream <- v
78
- gge.Add(v)
79
- }
80
- end.Done()
81
- }(vals)
82
- }
83
- start.Done()
84
-
85
- if expected, got := <-result, math.Float64frombits(gge.(*value).valBits); math.Abs(expected-got) > 0.000001 {
86
- t.Fatalf("expected approx. %f, got %f", expected, got)
87
- return false
88
- }
89
- return true
90
- }
91
-
92
- if err := quick.Check(it, nil); err != nil {
93
- t.Fatal(err)
94
- }
95
-}
96
-
97
-func TestGaugeVecConcurrency(t *testing.T) {
98
- it := func(n uint32) bool {
99
- mutations := int(n % 10000)
100
- concLevel := int(n%15 + 1)
101
- vecLength := int(n%5 + 1)
102
-
103
- var start, end sync.WaitGroup
104
- start.Add(1)
105
- end.Add(concLevel)
106
-
107
- sStreams := make([]chan float64, vecLength)
108
- results := make([]chan float64, vecLength)
109
- done := make(chan struct{})
110
-
111
- for i := 0; i < vecLength; i++ {
112
- sStreams[i] = make(chan float64, mutations*concLevel)
113
- results[i] = make(chan float64)
114
- go listenGaugeStream(sStreams[i], results[i], done)
115
- }
116
-
117
- go func() {
118
- end.Wait()
119
- close(done)
120
- }()
121
-
122
- gge := NewGaugeVec(
123
- GaugeOpts{
124
- Name: "test_gauge",
125
- Help: "no help can be found here",
126
- },
127
- []string{"label"},
128
- )
129
- for i := 0; i < concLevel; i++ {
130
- vals := make([]float64, mutations)
131
- pick := make([]int, mutations)
132
- for j := 0; j < mutations; j++ {
133
- vals[j] = rand.Float64() - 0.5
134
- pick[j] = rand.Intn(vecLength)
135
- }
136
-
137
- go func(vals []float64) {
138
- start.Wait()
139
- for i, v := range vals {
140
- sStreams[pick[i]] <- v
141
- gge.WithLabelValues(string('A' + pick[i])).Add(v)
142
- }
143
- end.Done()
144
- }(vals)
145
- }
146
- start.Done()
147
-
148
- for i := range sStreams {
149
- if expected, got := <-results[i], math.Float64frombits(gge.WithLabelValues(string('A'+i)).(*value).valBits); math.Abs(expected-got) > 0.000001 {
150
- t.Fatalf("expected approx. %f, got %f", expected, got)
151
- return false
152
- }
153
- }
154
- return true
155
- }
156
-
157
- if err := quick.Check(it, nil); err != nil {
158
- t.Fatal(err)
159
- }
160
-}
161
-
162
-func TestGaugeFunc(t *testing.T) {
163
- gf := NewGaugeFunc(
164
- GaugeOpts{
165
- Name: "test_name",
166
- Help: "test help",
167
- ConstLabels: Labels{"a": "1", "b": "2"},
168
- },
169
- func() float64 { return 3.1415 },
170
- )
171
-
172
- if expected, got := `Desc{fqName: "test_name", help: "test help", constLabels: {a="1",b="2"}, variableLabels: []}`, gf.Desc().String(); expected != got {
173
- t.Errorf("expected %q, got %q", expected, got)
174
- }
175
-
176
- m := &dto.Metric{}
177
- gf.Write(m)
178
-
179
- if expected, got := `label:<name:"a" value:"1" > label:<name:"b" value:"2" > gauge:<value:3.1415 > `, m.String(); expected != got {
180
- t.Errorf("expected %q, got %q", expected, got)
181
- }
182
-}
Godeps/_workspace/src/github.com/prometheus/client_golang/prometheus/go_collector.go
deleted
-50
@@ -1,50 +0,0 @@
1
-package prometheus
2
-
3
-import (
4
- "runtime"
5
- "runtime/debug"
6
- "time"
7
-)
8
-
9
-type goCollector struct {
10
- goroutines Gauge
11
- gcDesc *Desc
12
-}
13
-
14
-// NewGoCollector returns a collector which exports metrics about the current
15
-// go process.
16
-func NewGoCollector() *goCollector {
17
- return &goCollector{
18
- goroutines: NewGauge(GaugeOpts{
19
- Name: "go_goroutines",
20
- Help: "Number of goroutines that currently exist.",
21
- }),
22
- gcDesc: NewDesc(
23
- "go_gc_duration_seconds",
24
- "A summary of the GC invocation durations.",
25
- nil, nil),
26
- }
27
-}
28
-
29
-// Describe returns all descriptions of the collector.
30
-func (c *goCollector) Describe(ch chan<- *Desc) {
31
- ch <- c.goroutines.Desc()
32
- ch <- c.gcDesc
33
-}
34
-
35
-// Collect returns the current state of all metrics of the collector.
36
-func (c *goCollector) Collect(ch chan<- Metric) {
37
- c.goroutines.Set(float64(runtime.NumGoroutine()))
38
- ch <- c.goroutines
39
-
40
- var stats debug.GCStats
41
- stats.PauseQuantiles = make([]time.Duration, 5)
42
- debug.ReadGCStats(&stats)
43
-
44
- quantiles := make(map[float64]float64)
45
- for idx, pq := range stats.PauseQuantiles[1:] {
46
- quantiles[float64(idx+1)/float64(len(stats.PauseQuantiles)-1)] = pq.Seconds()
47
- }
48
- quantiles[0.0] = stats.PauseQuantiles[0].Seconds()
49
- ch <- MustNewConstSummary(c.gcDesc, uint64(stats.NumGC), float64(stats.PauseTotal.Seconds()), quantiles)
50
-}
Godeps/_workspace/src/github.com/prometheus/client_golang/prometheus/go_collector_test.go
deleted
-119
@@ -1,119 +0,0 @@
1
-package prometheus
2
-
3
-import (
4
- "runtime"
5
- "testing"
6
- "time"
7
-
8
- dto "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/prometheus/client_model/go"
9
-)
10
-
11
-func TestGoCollector(t *testing.T) {
12
- var (
13
- c = NewGoCollector()
14
- ch = make(chan Metric)
15
- waitc = make(chan struct{})
16
- closec = make(chan struct{})
17
- old = -1
18
- )
19
- defer close(closec)
20
-
21
- go func() {
22
- c.Collect(ch)
23
- go func(c <-chan struct{}) {
24
- <-c
25
- }(closec)
26
- <-waitc
27
- c.Collect(ch)
28
- }()
29
-
30
- for {
31
- select {
32
- case metric := <-ch:
33
- switch m := metric.(type) {
34
- // Attention, this also catches Counter...
35
- case Gauge:
36
- pb := &dto.Metric{}
37
- m.Write(pb)
38
- if pb.GetGauge() == nil {
39
- continue
40
- }
41
-
42
- if old == -1 {
43
- old = int(pb.GetGauge().GetValue())
44
- close(waitc)
45
- continue
46
- }
47
-
48
- if diff := int(pb.GetGauge().GetValue()) - old; diff != 1 {
49
- // TODO: This is flaky in highly concurrent situations.
50
- t.Errorf("want 1 new goroutine, got %d", diff)
51
- }
52
-
53
- return
54
- }
55
- case <-time.After(1 * time.Second):
56
- t.Fatalf("expected collect timed out")
57
- }
58
- }
59
-}
60
-
61
-func TestGCCollector(t *testing.T) {
62
- var (
63
- c = NewGoCollector()
64
- ch = make(chan Metric)
65
- waitc = make(chan struct{})
66
- closec = make(chan struct{})
67
- oldGC uint64
68
- oldPause float64
69
- )
70
- defer close(closec)
71
-
72
- go func() {
73
- c.Collect(ch)
74
- // force GC
75
- runtime.GC()
76
- <-waitc
77
- c.Collect(ch)
78
- }()
79
-
80
- first := true
81
- for {
82
- select {
83
- case metric := <-ch:
84
- switch m := metric.(type) {
85
- case *constSummary, *value:
86
- pb := &dto.Metric{}
87
- m.Write(pb)
88
- if pb.GetSummary() == nil {
89
- continue
90
- }
91
-
92
- if len(pb.GetSummary().Quantile) != 5 {
93
- t.Errorf("expected 4 buckets, got %d", len(pb.GetSummary().Quantile))
94
- }
95
- for idx, want := range []float64{0.0, 0.25, 0.5, 0.75, 1.0} {
96
- if *pb.GetSummary().Quantile[idx].Quantile != want {
97
- t.Errorf("bucket #%d is off, got %f, want %f", idx, *pb.GetSummary().Quantile[idx].Quantile, want)
98
- }
99
- }
100
- if first {
101
- first = false
102
- oldGC = *pb.GetSummary().SampleCount
103
- oldPause = *pb.GetSummary().SampleSum
104
- close(waitc)
105
- continue
106
- }
107
- if diff := *pb.GetSummary().SampleCount - oldGC; diff != 1 {
108
- t.Errorf("want 1 new garbage collection run, got %d", diff)
109
- }
110
- if diff := *pb.GetSummary().SampleSum - oldPause; diff <= 0 {
111
- t.Errorf("want moar pause, got %f", diff)
112
- }
113
- return
114
- }
115
- case <-time.After(1 * time.Second):
116
- t.Fatalf("expected collect timed out")
117
- }
118
- }
119
-}
Godeps/_workspace/src/github.com/prometheus/client_golang/prometheus/histogram.go
deleted
-447
@@ -1,447 +0,0 @@
1
-// Copyright 2015 The Prometheus Authors
2
-// Licensed under the Apache License, Version 2.0 (the "License");
3
-// you may not use this file except in compliance with the License.
4
-// You may obtain a copy of the License at
5
-//
6
-// http://www.apache.org/licenses/LICENSE-2.0
7
-//
8
-// Unless required by applicable law or agreed to in writing, software
9
-// distributed under the License is distributed on an "AS IS" BASIS,
10
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
-// See the License for the specific language governing permissions and
12
-// limitations under the License.
13
-
14
-package prometheus
15
-
16
-import (
17
- "fmt"
18
- "hash/fnv"
19
- "math"
20
- "sort"
21
- "sync/atomic"
22
-
23
- "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/golang/protobuf/proto"
24
-
25
- "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/prometheus/client_golang/model"
26
- dto "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/prometheus/client_model/go"
27
-)
28
-
29
-// A Histogram counts individual observations from an event or sample stream in
30
-// configurable buckets. Similar to a summary, it also provides a sum of
31
-// observations and an observation count.
32
-//
33
-// On the Prometheus server, quantiles can be calculated from a Histogram using
34
-// the histogram_quantile function in the query language.
35
-//
36
-// Note that Histograms, in contrast to Summaries, can be aggregated with the
37
-// Prometheus query language (see the documentation for detailed
38
-// procedures). However, Histograms require the user to pre-define suitable
39
-// buckets, and they are in general less accurate. The Observe method of a
40
-// Histogram has a very low performance overhead in comparison with the Observe
41
-// method of a Summary.
42
-//
43
-// To create Histogram instances, use NewHistogram.
44
-type Histogram interface {
45
- Metric
46
- Collector
47
-
48
- // Observe adds a single observation to the histogram.
49
- Observe(float64)
50
-}
51
-
52
-var (
53
- // DefBuckets are the default Histogram buckets. The default buckets are
54
- // tailored to broadly measure the response time (in seconds) of a
55
- // network service. Most likely, however, you will be required to define
56
- // buckets customized to your use case.
57
- DefBuckets = []float64{.005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10}
58
-
59
- errBucketLabelNotAllowed = fmt.Errorf(
60
- "%q is not allowed as label name in histograms", model.BucketLabel,
61
- )
62
-)
63
-
64
-// LinearBuckets creates 'count' buckets, each 'width' wide, where the lowest
65
-// bucket has an upper bound of 'start'. The final +Inf bucket is not counted
66
-// and not included in the returned slice. The returned slice is meant to be
67
-// used for the Buckets field of HistogramOpts.
68
-//
69
-// The function panics if 'count' is zero or negative.
70
-func LinearBuckets(start, width float64, count int) []float64 {
71
- if count < 1 {
72
- panic("LinearBuckets needs a positive count")
73
- }
74
- buckets := make([]float64, count)
75
- for i := range buckets {
76
- buckets[i] = start
77
- start += width
78
- }
79
- return buckets
80
-}
81
-
82
-// ExponentialBuckets creates 'count' buckets, where the lowest bucket has an
83
-// upper bound of 'start' and each following bucket's upper bound is 'factor'
84
-// times the previous bucket's upper bound. The final +Inf bucket is not counted
85
-// and not included in the returned slice. The returned slice is meant to be
86
-// used for the Buckets field of HistogramOpts.
87
-//
88
-// The function panics if 'count' is 0 or negative, if 'start' is 0 or negative,
89
-// or if 'factor' is less than or equal 1.
90
-func ExponentialBuckets(start, factor float64, count int) []float64 {
91
- if count < 1 {
92
- panic("ExponentialBuckets needs a positive count")
93
- }
94
- if start <= 0 {
95
- panic("ExponentialBuckets needs a positive start value")
96
- }
97
- if factor <= 1 {
98
- panic("ExponentialBuckets needs a factor greater than 1")
99
- }
100
- buckets := make([]float64, count)
101
- for i := range buckets {
102
- buckets[i] = start
103
- start *= factor
104
- }
105
- return buckets
106
-}
107
-
108
-// HistogramOpts bundles the options for creating a Histogram metric. It is
109
-// mandatory to set Name and Help to a non-empty string. All other fields are
110
-// optional and can safely be left at their zero value.
111
-type HistogramOpts struct {
112
- // Namespace, Subsystem, and Name are components of the fully-qualified
113
- // name of the Histogram (created by joining these components with
114
- // "_"). Only Name is mandatory, the others merely help structuring the
115
- // name. Note that the fully-qualified name of the Histogram must be a
116
- // valid Prometheus metric name.
117
- Namespace string
118
- Subsystem string
119
- Name string
120
-
121
- // Help provides information about this Histogram. Mandatory!
122
- //
123
- // Metrics with the same fully-qualified name must have the same Help
124
- // string.
125
- Help string
126
-
127
- // ConstLabels are used to attach fixed labels to this
128
- // Histogram. Histograms with the same fully-qualified name must have the
129
- // same label names in their ConstLabels.
130
- //
131
- // Note that in most cases, labels have a value that varies during the
132
- // lifetime of a process. Those labels are usually managed with a
133
- // HistogramVec. ConstLabels serve only special purposes. One is for the
134
- // special case where the value of a label does not change during the
135
- // lifetime of a process, e.g. if the revision of the running binary is
136
- // put into a label. Another, more advanced purpose is if more than one
137
- // Collector needs to collect Histograms with the same fully-qualified
138
- // name. In that case, those Summaries must differ in the values of
139
- // their ConstLabels. See the Collector examples.
140
- //
141
- // If the value of a label never changes (not even between binaries),
142
- // that label most likely should not be a label at all (but part of the
143
- // metric name).
144
- ConstLabels Labels
145
-
146
- // Buckets defines the buckets into which observations are counted. Each
147
- // element in the slice is the upper inclusive bound of a bucket. The
148
- // values must be sorted in strictly increasing order. There is no need
149
- // to add a highest bucket with +Inf bound, it will be added
150
- // implicitly. The default value is DefBuckets.
151
- Buckets []float64
152
-}
153
-
154
-// NewHistogram creates a new Histogram based on the provided HistogramOpts. It
155
-// panics if the buckets in HistogramOpts are not in strictly increasing order.
156
-func NewHistogram(opts HistogramOpts) Histogram {
157
- return newHistogram(
158
- NewDesc(
159
- BuildFQName(opts.Namespace, opts.Subsystem, opts.Name),
160
- opts.Help,
161
- nil,
162
- opts.ConstLabels,
163
- ),
164
- opts,
165
- )
166
-}
167
-
168
-func newHistogram(desc *Desc, opts HistogramOpts, labelValues ...string) Histogram {
169
- if len(desc.variableLabels) != len(labelValues) {
170
- panic(errInconsistentCardinality)
171
- }
172
-
173
- for _, n := range desc.variableLabels {
174
- if n == model.BucketLabel {
175
- panic(errBucketLabelNotAllowed)
176
- }
177
- }
178
- for _, lp := range desc.constLabelPairs {
179
- if lp.GetName() == model.BucketLabel {
180
- panic(errBucketLabelNotAllowed)
181
- }
182
- }
183
-
184
- if len(opts.Buckets) == 0 {
185
- opts.Buckets = DefBuckets
186
- }
187
-
188
- h := &histogram{
189
- desc: desc,
190
- upperBounds: opts.Buckets,
191
- labelPairs: makeLabelPairs(desc, labelValues),
192
- }
193
- for i, upperBound := range h.upperBounds {
194
- if i < len(h.upperBounds)-1 {
195
- if upperBound >= h.upperBounds[i+1] {
196
- panic(fmt.Errorf(
197
- "histogram buckets must be in increasing order: %f >= %f",
198
- upperBound, h.upperBounds[i+1],
199
- ))
200
- }
201
- } else {
202
- if math.IsInf(upperBound, +1) {
203
- // The +Inf bucket is implicit. Remove it here.
204
- h.upperBounds = h.upperBounds[:i]
205
- }
206
- }
207
- }
208
- // Finally we know the final length of h.upperBounds and can make counts.
209
- h.counts = make([]uint64, len(h.upperBounds))
210
-
211
- h.Init(h) // Init self-collection.
212
- return h
213
-}
214
-
215
-type histogram struct {
216
- // sumBits contains the bits of the float64 representing the sum of all
217
- // observations. sumBits and count have to go first in the struct to
218
- // guarantee alignment for atomic operations.
219
- // http://golang.org/pkg/sync/atomic/#pkg-note-BUG
220
- sumBits uint64
221
- count uint64
222
-
223
- SelfCollector
224
- // Note that there is no mutex required.
225
-
226
- desc *Desc
227
-
228
- upperBounds []float64
229
- counts []uint64
230
-
231
- labelPairs []*dto.LabelPair
232
-}
233
-
234
-func (h *histogram) Desc() *Desc {
235
- return h.desc
236
-}
237
-
238
-func (h *histogram) Observe(v float64) {
239
- // TODO(beorn7): For small numbers of buckets (<30), a linear search is
240
- // slightly faster than the binary search. If we really care, we could
241
- // switch from one search strategy to the other depending on the number
242
- // of buckets.
243
- //
244
- // Microbenchmarks (BenchmarkHistogramNoLabels):
245
- // 11 buckets: 38.3 ns/op linear - binary 48.7 ns/op
246
- // 100 buckets: 78.1 ns/op linear - binary 54.9 ns/op
247
- // 300 buckets: 154 ns/op linear - binary 61.6 ns/op
248
- i := sort.SearchFloat64s(h.upperBounds, v)
249
- if i < len(h.counts) {
250
- atomic.AddUint64(&h.counts[i], 1)
251
- }
252
- atomic.AddUint64(&h.count, 1)
253
- for {
254
- oldBits := atomic.LoadUint64(&h.sumBits)
255
- newBits := math.Float64bits(math.Float64frombits(oldBits) + v)
256
- if atomic.CompareAndSwapUint64(&h.sumBits, oldBits, newBits) {
257
- break
258
- }
259
- }
260
-}
261
-
262
-func (h *histogram) Write(out *dto.Metric) error {
263
- his := &dto.Histogram{}
264
- buckets := make([]*dto.Bucket, len(h.upperBounds))
265
-
266
- his.SampleSum = proto.Float64(math.Float64frombits(atomic.LoadUint64(&h.sumBits)))
267
- his.SampleCount = proto.Uint64(atomic.LoadUint64(&h.count))
268
- var count uint64
269
- for i, upperBound := range h.upperBounds {
270
- count += atomic.LoadUint64(&h.counts[i])
271
- buckets[i] = &dto.Bucket{
272
- CumulativeCount: proto.Uint64(count),
273
- UpperBound: proto.Float64(upperBound),
274
- }
275
- }
276
- his.Bucket = buckets
277
- out.Histogram = his
278
- out.Label = h.labelPairs
279
- return nil
280
-}
281
-
282
-// HistogramVec is a Collector that bundles a set of Histograms that all share the
283
-// same Desc, but have different values for their variable labels. This is used
284
-// if you want to count the same thing partitioned by various dimensions
285
-// (e.g. HTTP request latencies, partitioned by status code and method). Create
286
-// instances with NewHistogramVec.
287
-type HistogramVec struct {
288
- MetricVec
289
-}
290
-
291
-// NewHistogramVec creates a new HistogramVec based on the provided HistogramOpts and
292
-// partitioned by the given label names. At least one label name must be
293
-// provided.
294
-func NewHistogramVec(opts HistogramOpts, labelNames []string) *HistogramVec {
295
- desc := NewDesc(
296
- BuildFQName(opts.Namespace, opts.Subsystem, opts.Name),
297
- opts.Help,
298
- labelNames,
299
- opts.ConstLabels,
300
- )
301
- return &HistogramVec{
302
- MetricVec: MetricVec{
303
- children: map[uint64]Metric{},
304
- desc: desc,
305
- hash: fnv.New64a(),
306
- newMetric: func(lvs ...string) Metric {
307
- return newHistogram(desc, opts, lvs...)
308
- },
309
- },
310
- }
311
-}
312
-
313
-// GetMetricWithLabelValues replaces the method of the same name in
314
-// MetricVec. The difference is that this method returns a Histogram and not a
315
-// Metric so that no type conversion is required.
316
-func (m *HistogramVec) GetMetricWithLabelValues(lvs ...string) (Histogram, error) {
317
- metric, err := m.MetricVec.GetMetricWithLabelValues(lvs...)
318
- if metric != nil {
319
- return metric.(Histogram), err
320
- }
321
- return nil, err
322
-}
323
-
324
-// GetMetricWith replaces the method of the same name in MetricVec. The
325
-// difference is that this method returns a Histogram and not a Metric so that no
326
-// type conversion is required.
327
-func (m *HistogramVec) GetMetricWith(labels Labels) (Histogram, error) {
328
- metric, err := m.MetricVec.GetMetricWith(labels)
329
- if metric != nil {
330
- return metric.(Histogram), err
331
- }
332
- return nil, err
333
-}
334
-
335
-// WithLabelValues works as GetMetricWithLabelValues, but panics where
336
-// GetMetricWithLabelValues would have returned an error. By not returning an
337
-// error, WithLabelValues allows shortcuts like
338
-// myVec.WithLabelValues("404", "GET").Observe(42.21)
339
-func (m *HistogramVec) WithLabelValues(lvs ...string) Histogram {
340
- return m.MetricVec.WithLabelValues(lvs...).(Histogram)
341
-}
342
-
343
-// With works as GetMetricWith, but panics where GetMetricWithLabels would have
344
-// returned an error. By not returning an error, With allows shortcuts like
345
-// myVec.With(Labels{"code": "404", "method": "GET"}).Observe(42.21)
346
-func (m *HistogramVec) With(labels Labels) Histogram {
347
- return m.MetricVec.With(labels).(Histogram)
348
-}
349
-
350
-type constHistogram struct {
351
- desc *Desc
352
- count uint64
353
- sum float64
354
- buckets map[float64]uint64
355
- labelPairs []*dto.LabelPair
356
-}
357
-
358
-func (h *constHistogram) Desc() *Desc {
359
- return h.desc
360
-}
361
-
362
-func (h *constHistogram) Write(out *dto.Metric) error {
363
- his := &dto.Histogram{}
364
- buckets := make([]*dto.Bucket, 0, len(h.buckets))
365
-
366
- his.SampleCount = proto.Uint64(h.count)
367
- his.SampleSum = proto.Float64(h.sum)
368
-
369
- for upperBound, count := range h.buckets {
370
- buckets = append(buckets, &dto.Bucket{
371
- CumulativeCount: proto.Uint64(count),
372
- UpperBound: proto.Float64(upperBound),
373
- })
374
- }
375
-
376
- if len(buckets) > 0 {
377
- sort.Sort(buckSort(buckets))
378
- }
379
- his.Bucket = buckets
380
-
381
- out.Histogram = his
382
- out.Label = h.labelPairs
383
-
384
- return nil
385
-}
386
-
387
-// NewConstHistogram returns a metric representing a Prometheus histogram with
388
-// fixed values for the count, sum, and bucket counts. As those parameters
389
-// cannot be changed, the returned value does not implement the Histogram
390
-// interface (but only the Metric interface). Users of this package will not
391
-// have much use for it in regular operations. However, when implementing custom
392
-// Collectors, it is useful as a throw-away metric that is generated on the fly
393
-// to send it to Prometheus in the Collect method.
394
-//
395
-// buckets is a map of upper bounds to cumulative counts, excluding the +Inf
396
-// bucket.
397
-//
398
-// NewConstHistogram returns an error if the length of labelValues is not
399
-// consistent with the variable labels in Desc.
400
-func NewConstHistogram(
401
- desc *Desc,
402
- count uint64,
403
- sum float64,
404
- buckets map[float64]uint64,
405
- labelValues ...string,
406
-) (Metric, error) {
407
- if len(desc.variableLabels) != len(labelValues) {
408
- return nil, errInconsistentCardinality
409
- }
410
- return &constHistogram{
411
- desc: desc,
412
- count: count,
413
- sum: sum,
414
- buckets: buckets,
415
- labelPairs: makeLabelPairs(desc, labelValues),
416
- }, nil
417
-}
418
-
419
-// MustNewConstHistogram is a version of NewConstHistogram that panics where
420
-// NewConstMetric would have returned an error.
421
-func MustNewConstHistogram(
422
- desc *Desc,
423
- count uint64,
424
- sum float64,
425
- buckets map[float64]uint64,
426
- labelValues ...string,
427
-) Metric {
428
- m, err := NewConstHistogram(desc, count, sum, buckets, labelValues...)
429
- if err != nil {
430
- panic(err)
431
- }
432
- return m
433
-}
434
-
435
-type buckSort []*dto.Bucket
436
-
437
-func (s buckSort) Len() int {
438
- return len(s)
439
-}
440
-
441
-func (s buckSort) Swap(i, j int) {
442
- s[i], s[j] = s[j], s[i]
443
-}
444
-
445
-func (s buckSort) Less(i, j int) bool {
446
- return s[i].GetUpperBound() < s[j].GetUpperBound()
447
-}
Godeps/_workspace/src/github.com/prometheus/client_golang/prometheus/histogram_test.go
deleted
-318
@@ -1,318 +0,0 @@
1
-// Copyright 2015 The Prometheus Authors
2
-// Licensed under the Apache License, Version 2.0 (the "License");
3
-// you may not use this file except in compliance with the License.
4
-// You may obtain a copy of the License at
5
-//
6
-// http://www.apache.org/licenses/LICENSE-2.0
7
-//
8
-// Unless required by applicable law or agreed to in writing, software
9
-// distributed under the License is distributed on an "AS IS" BASIS,
10
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
-// See the License for the specific language governing permissions and
12
-// limitations under the License.
13
-
14
-package prometheus
15
-
16
-import (
17
- "math"
18
- "math/rand"
19
- "reflect"
20
- "sort"
21
- "sync"
22
- "testing"
23
- "testing/quick"
24
-
25
- dto "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/prometheus/client_model/go"
26
-)
27
-
28
-func benchmarkHistogramObserve(w int, b *testing.B) {
29
- b.StopTimer()
30
-
31
- wg := new(sync.WaitGroup)
32
- wg.Add(w)
33
-
34
- g := new(sync.WaitGroup)
35
- g.Add(1)
36
-
37
- s := NewHistogram(HistogramOpts{})
38
-
39
- for i := 0; i < w; i++ {
40
- go func() {
41
- g.Wait()
42
-
43
- for i := 0; i < b.N; i++ {
44
- s.Observe(float64(i))
45
- }
46
-
47
- wg.Done()
48
- }()
49
- }
50
-
51
- b.StartTimer()
52
- g.Done()
53
- wg.Wait()
54
-}
55
-
56
-func BenchmarkHistogramObserve1(b *testing.B) {
57
- benchmarkHistogramObserve(1, b)
58
-}
59
-
60
-func BenchmarkHistogramObserve2(b *testing.B) {
61
- benchmarkHistogramObserve(2, b)
62
-}
63
-
64
-func BenchmarkHistogramObserve4(b *testing.B) {
65
- benchmarkHistogramObserve(4, b)
66
-}
67
-
68
-func BenchmarkHistogramObserve8(b *testing.B) {
69
- benchmarkHistogramObserve(8, b)
70
-}
71
-
72
-func benchmarkHistogramWrite(w int, b *testing.B) {
73
- b.StopTimer()
74
-
75
- wg := new(sync.WaitGroup)
76
- wg.Add(w)
77
-
78
- g := new(sync.WaitGroup)
79
- g.Add(1)
80
-
81
- s := NewHistogram(HistogramOpts{})
82
-
83
- for i := 0; i < 1000000; i++ {
84
- s.Observe(float64(i))
85
- }
86
-
87
- for j := 0; j < w; j++ {
88
- outs := make([]dto.Metric, b.N)
89
-
90
- go func(o []dto.Metric) {
91
- g.Wait()
92
-
93
- for i := 0; i < b.N; i++ {
94
- s.Write(&o[i])
95
- }
96
-
97
- wg.Done()
98
- }(outs)
99
- }
100
-
101
- b.StartTimer()
102
- g.Done()
103
- wg.Wait()
104
-}
105
-
106
-func BenchmarkHistogramWrite1(b *testing.B) {
107
- benchmarkHistogramWrite(1, b)
108
-}
109
-
110
-func BenchmarkHistogramWrite2(b *testing.B) {
111
- benchmarkHistogramWrite(2, b)
112
-}
113
-
114
-func BenchmarkHistogramWrite4(b *testing.B) {
115
- benchmarkHistogramWrite(4, b)
116
-}
117
-
118
-func BenchmarkHistogramWrite8(b *testing.B) {
119
- benchmarkHistogramWrite(8, b)
120
-}
121
-
122
-// Intentionally adding +Inf here to test if that case is handled correctly.
123
-// Also, getCumulativeCounts depends on it.
124
-var testBuckets = []float64{-2, -1, -0.5, 0, 0.5, 1, 2, math.Inf(+1)}
125
-
126
-func TestHistogramConcurrency(t *testing.T) {
127
- rand.Seed(42)
128
-
129
- it := func(n uint32) bool {
130
- mutations := int(n%1e4 + 1e4)
131
- concLevel := int(n%5 + 1)
132
- total := mutations * concLevel
133
-
134
- var start, end sync.WaitGroup
135
- start.Add(1)
136
- end.Add(concLevel)
137
-
138
- sum := NewHistogram(HistogramOpts{
139
- Name: "test_histogram",
140
- Help: "helpless",
141
- Buckets: testBuckets,
142
- })
143
-
144
- allVars := make([]float64, total)
145
- var sampleSum float64
146
- for i := 0; i < concLevel; i++ {
147
- vals := make([]float64, mutations)
148
- for j := 0; j < mutations; j++ {
149
- v := rand.NormFloat64()
150
- vals[j] = v
151
- allVars[i*mutations+j] = v
152
- sampleSum += v
153
- }
154
-
155
- go func(vals []float64) {
156
- start.Wait()
157
- for _, v := range vals {
158
- sum.Observe(v)
159
- }
160
- end.Done()
161
- }(vals)
162
- }
163
- sort.Float64s(allVars)
164
- start.Done()
165
- end.Wait()
166
-
167
- m := &dto.Metric{}
168
- sum.Write(m)
169
- if got, want := int(*m.Histogram.SampleCount), total; got != want {
170
- t.Errorf("got sample count %d, want %d", got, want)
171
- }
172
- if got, want := *m.Histogram.SampleSum, sampleSum; math.Abs((got-want)/want) > 0.001 {
173
- t.Errorf("got sample sum %f, want %f", got, want)
174
- }
175
-
176
- wantCounts := getCumulativeCounts(allVars)
177
-
178
- if got, want := len(m.Histogram.Bucket), len(testBuckets)-1; got != want {
179
- t.Errorf("got %d buckets in protobuf, want %d", got, want)
180
- }
181
- for i, wantBound := range testBuckets {
182
- if i == len(testBuckets)-1 {
183
- break // No +Inf bucket in protobuf.
184
- }
185
- if gotBound := *m.Histogram.Bucket[i].UpperBound; gotBound != wantBound {
186
- t.Errorf("got bound %f, want %f", gotBound, wantBound)
187
- }
188
- if gotCount, wantCount := *m.Histogram.Bucket[i].CumulativeCount, wantCounts[i]; gotCount != wantCount {
189
- t.Errorf("got count %d, want %d", gotCount, wantCount)
190
- }
191
- }
192
- return true
193
- }
194
-
195
- if err := quick.Check(it, nil); err != nil {
196
- t.Error(err)
197
- }
198
-}
199
-
200
-func TestHistogramVecConcurrency(t *testing.T) {
201
- rand.Seed(42)
202
-
203
- objectives := make([]float64, 0, len(DefObjectives))
204
- for qu := range DefObjectives {
205
-
206
- objectives = append(objectives, qu)
207
- }
208
- sort.Float64s(objectives)
209
-
210
- it := func(n uint32) bool {
211
- mutations := int(n%1e4 + 1e4)
212
- concLevel := int(n%7 + 1)
213
- vecLength := int(n%3 + 1)
214
-
215
- var start, end sync.WaitGroup
216
- start.Add(1)
217
- end.Add(concLevel)
218
-
219
- his := NewHistogramVec(
220
- HistogramOpts{
221
- Name: "test_histogram",
222
- Help: "helpless",
223
- Buckets: []float64{-2, -1, -0.5, 0, 0.5, 1, 2, math.Inf(+1)},
224
- },
225
- []string{"label"},
226
- )
227
-
228
- allVars := make([][]float64, vecLength)
229
- sampleSums := make([]float64, vecLength)
230
- for i := 0; i < concLevel; i++ {
231
- vals := make([]float64, mutations)
232
- picks := make([]int, mutations)
233
- for j := 0; j < mutations; j++ {
234
- v := rand.NormFloat64()
235
- vals[j] = v
236
- pick := rand.Intn(vecLength)
237
- picks[j] = pick
238
- allVars[pick] = append(allVars[pick], v)
239
- sampleSums[pick] += v
240
- }
241
-
242
- go func(vals []float64) {
243
- start.Wait()
244
- for i, v := range vals {
245
- his.WithLabelValues(string('A' + picks[i])).Observe(v)
246
- }
247
- end.Done()
248
- }(vals)
249
- }
250
- for _, vars := range allVars {
251
- sort.Float64s(vars)
252
- }
253
- start.Done()
254
- end.Wait()
255
-
256
- for i := 0; i < vecLength; i++ {
257
- m := &dto.Metric{}
258
- s := his.WithLabelValues(string('A' + i))
259
- s.Write(m)
260
-
261
- if got, want := len(m.Histogram.Bucket), len(testBuckets)-1; got != want {
262
- t.Errorf("got %d buckets in protobuf, want %d", got, want)
263
- }
264
- if got, want := int(*m.Histogram.SampleCount), len(allVars[i]); got != want {
265
- t.Errorf("got sample count %d, want %d", got, want)
266
- }
267
- if got, want := *m.Histogram.SampleSum, sampleSums[i]; math.Abs((got-want)/want) > 0.001 {
268
- t.Errorf("got sample sum %f, want %f", got, want)
269
- }
270
-
271
- wantCounts := getCumulativeCounts(allVars[i])
272
-
273
- for j, wantBound := range testBuckets {
274
- if j == len(testBuckets)-1 {
275
- break // No +Inf bucket in protobuf.
276
- }
277
- if gotBound := *m.Histogram.Bucket[j].UpperBound; gotBound != wantBound {
278
- t.Errorf("got bound %f, want %f", gotBound, wantBound)
279
- }
280
- if gotCount, wantCount := *m.Histogram.Bucket[j].CumulativeCount, wantCounts[j]; gotCount != wantCount {
281
- t.Errorf("got count %d, want %d", gotCount, wantCount)
282
- }
283
- }
284
- }
285
- return true
286
- }
287
-
288
- if err := quick.Check(it, nil); err != nil {
289
- t.Error(err)
290
- }
291
-}
292
-
293
-func getCumulativeCounts(vars []float64) []uint64 {
294
- counts := make([]uint64, len(testBuckets))
295
- for _, v := range vars {
296
- for i := len(testBuckets) - 1; i >= 0; i-- {
297
- if v > testBuckets[i] {
298
- break
299
- }
300
- counts[i]++
301
- }
302
- }
303
- return counts
304
-}
305
-
306
-func TestBuckets(t *testing.T) {
307
- got := LinearBuckets(-15, 5, 6)
308
- want := []float64{-15, -10, -5, 0, 5, 10}
309
- if !reflect.DeepEqual(got, want) {
310
- t.Errorf("linear buckets: got %v, want %v", got, want)
311
- }
312
-
313
- got = ExponentialBuckets(100, 1.2, 3)
314
- want = []float64{100, 120, 144}
315
- if !reflect.DeepEqual(got, want) {
316
- t.Errorf("linear buckets: got %v, want %v", got, want)
317
- }
318
-}
Godeps/_workspace/src/github.com/prometheus/client_golang/prometheus/http.go
deleted
-361
@@ -1,361 +0,0 @@
1
-// Copyright 2014 The Prometheus Authors
2
-// Licensed under the Apache License, Version 2.0 (the "License");
3
-// you may not use this file except in compliance with the License.
4
-// You may obtain a copy of the License at
5
-//
6
-// http://www.apache.org/licenses/LICENSE-2.0
7
-//
8
-// Unless required by applicable law or agreed to in writing, software
9
-// distributed under the License is distributed on an "AS IS" BASIS,
10
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
-// See the License for the specific language governing permissions and
12
-// limitations under the License.
13
-
14
-package prometheus
15
-
16
-import (
17
- "bufio"
18
- "io"
19
- "net"
20
- "net/http"
21
- "strconv"
22
- "strings"
23
- "time"
24
-)
25
-
26
-var instLabels = []string{"method", "code"}
27
-
28
-type nower interface {
29
- Now() time.Time
30
-}
31
-
32
-type nowFunc func() time.Time
33
-
34
-func (n nowFunc) Now() time.Time {
35
- return n()
36
-}
37
-
38
-var now nower = nowFunc(func() time.Time {
39
- return time.Now()
40
-})
41
-
42
-func nowSeries(t ...time.Time) nower {
43
- return nowFunc(func() time.Time {
44
- defer func() {
45
- t = t[1:]
46
- }()
47
-
48
- return t[0]
49
- })
50
-}
51
-
52
-// InstrumentHandler wraps the given HTTP handler for instrumentation. It
53
-// registers four metric collectors (if not already done) and reports HTTP
54
-// metrics to the (newly or already) registered collectors: http_requests_total
55
-// (CounterVec), http_request_duration_microseconds (Summary),
56
-// http_request_size_bytes (Summary), http_response_size_bytes (Summary). Each
57
-// has a constant label named "handler" with the provided handlerName as
58
-// value. http_requests_total is a metric vector partitioned by HTTP method
59
-// (label name "method") and HTTP status code (label name "code").
60
-func InstrumentHandler(handlerName string, handler http.Handler) http.HandlerFunc {
61
- return InstrumentHandlerFunc(handlerName, handler.ServeHTTP)
62
-}
63
-
64
-// InstrumentHandlerFunc wraps the given function for instrumentation. It
65
-// otherwise works in the same way as InstrumentHandler.
66
-func InstrumentHandlerFunc(handlerName string, handlerFunc func(http.ResponseWriter, *http.Request)) http.HandlerFunc {
67
- return InstrumentHandlerFuncWithOpts(
68
- SummaryOpts{
69
- Subsystem: "http",
70
- ConstLabels: Labels{"handler": handlerName},
71
- },
72
- handlerFunc,
73
- )
74
-}
75
-
76
-// InstrumentHandlerWithOpts works like InstrumentHandler but provides more
77
-// flexibility (at the cost of a more complex call syntax). As
78
-// InstrumentHandler, this function registers four metric collectors, but it
79
-// uses the provided SummaryOpts to create them. However, the fields "Name" and
80
-// "Help" in the SummaryOpts are ignored. "Name" is replaced by
81
-// "requests_total", "request_duration_microseconds", "request_size_bytes", and
82
-// "response_size_bytes", respectively. "Help" is replaced by an appropriate
83
-// help string. The names of the variable labels of the http_requests_total
84
-// CounterVec are "method" (get, post, etc.), and "code" (HTTP status code).
85
-//
86
-// If InstrumentHandlerWithOpts is called as follows, it mimics exactly the
87
-// behavior of InstrumentHandler:
88
-//
89
-// prometheus.InstrumentHandlerWithOpts(
90
-// prometheus.SummaryOpts{
91
-// Subsystem: "http",
92
-// ConstLabels: prometheus.Labels{"handler": handlerName},
93
-// },
94
-// handler,
95
-// )
96
-//
97
-// Technical detail: "requests_total" is a CounterVec, not a SummaryVec, so it
98
-// cannot use SummaryOpts. Instead, a CounterOpts struct is created internally,
99
-// and all its fields are set to the equally named fields in the provided
100
-// SummaryOpts.
101
-func InstrumentHandlerWithOpts(opts SummaryOpts, handler http.Handler) http.HandlerFunc {
102
- return InstrumentHandlerFuncWithOpts(opts, handler.ServeHTTP)
103
-}
104
-
105
-// InstrumentHandlerFuncWithOpts works like InstrumentHandlerFunc but provides
106
-// more flexibility (at the cost of a more complex call syntax). See
107
-// InstrumentHandlerWithOpts for details how the provided SummaryOpts are used.
108
-func InstrumentHandlerFuncWithOpts(opts SummaryOpts, handlerFunc func(http.ResponseWriter, *http.Request)) http.HandlerFunc {
109
- reqCnt := NewCounterVec(
110
- CounterOpts{
111
- Namespace: opts.Namespace,
112
- Subsystem: opts.Subsystem,
113
- Name: "requests_total",
114
- Help: "Total number of HTTP requests made.",
115
- ConstLabels: opts.ConstLabels,
116
- },
117
- instLabels,
118
- )
119
-
120
- opts.Name = "request_duration_microseconds"
121
- opts.Help = "The HTTP request latencies in microseconds."
122
- reqDur := NewSummary(opts)
123
-
124
- opts.Name = "request_size_bytes"
125
- opts.Help = "The HTTP request sizes in bytes."
126
- reqSz := NewSummary(opts)
127
-
128
- opts.Name = "response_size_bytes"
129
- opts.Help = "The HTTP response sizes in bytes."
130
- resSz := NewSummary(opts)
131
-
132
- regReqCnt := MustRegisterOrGet(reqCnt).(*CounterVec)
133
- regReqDur := MustRegisterOrGet(reqDur).(Summary)
134
- regReqSz := MustRegisterOrGet(reqSz).(Summary)
135
- regResSz := MustRegisterOrGet(resSz).(Summary)
136
-
137
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
138
- now := time.Now()
139
-
140
- delegate := &responseWriterDelegator{ResponseWriter: w}
141
- out := make(chan int)
142
- urlLen := 0
143
- if r.URL != nil {
144
- urlLen = len(r.URL.String())
145
- }
146
- go computeApproximateRequestSize(r, out, urlLen)
147
-
148
- _, cn := w.(http.CloseNotifier)
149
- _, fl := w.(http.Flusher)
150
- _, hj := w.(http.Hijacker)
151
- _, rf := w.(io.ReaderFrom)
152
- var rw http.ResponseWriter
153
- if cn && fl && hj && rf {
154
- rw = &fancyResponseWriterDelegator{delegate}
155
- } else {
156
- rw = delegate
157
- }
158
- handlerFunc(rw, r)
159
-
160
- elapsed := float64(time.Since(now)) / float64(time.Microsecond)
161
-
162
- method := sanitizeMethod(r.Method)
163
- code := sanitizeCode(delegate.status)
164
- regReqCnt.WithLabelValues(method, code).Inc()
165
- regReqDur.Observe(elapsed)
166
- regResSz.Observe(float64(delegate.written))
167
- regReqSz.Observe(float64(<-out))
168
- })
169
-}
170
-
171
-func computeApproximateRequestSize(r *http.Request, out chan int, s int) {
172
- s += len(r.Method)
173
- s += len(r.Proto)
174
- for name, values := range r.Header {
175
- s += len(name)
176
- for _, value := range values {
177
- s += len(value)
178
- }
179
- }
180
- s += len(r.Host)
181
-
182
- // N.B. r.Form and r.MultipartForm are assumed to be included in r.URL.
183
-
184
- if r.ContentLength != -1 {
185
- s += int(r.ContentLength)
186
- }
187
- out <- s
188
-}
189
-
190
-type responseWriterDelegator struct {
191
- http.ResponseWriter
192
-
193
- handler, method string
194
- status int
195
- written int64
196
- wroteHeader bool
197
-}
198
-
199
-func (r *responseWriterDelegator) WriteHeader(code int) {
200
- r.status = code
201
- r.wroteHeader = true
202
- r.ResponseWriter.WriteHeader(code)
203
-}
204
-
205
-func (r *responseWriterDelegator) Write(b []byte) (int, error) {
206
- if !r.wroteHeader {
207
- r.WriteHeader(http.StatusOK)
208
- }
209
- n, err := r.ResponseWriter.Write(b)
210
- r.written += int64(n)
211
- return n, err
212
-}
213
-
214
-type fancyResponseWriterDelegator struct {
215
- *responseWriterDelegator
216
-}
217
-
218
-func (f *fancyResponseWriterDelegator) CloseNotify() <-chan bool {
219
- return f.ResponseWriter.(http.CloseNotifier).CloseNotify()
220
-}
221
-
222
-func (f *fancyResponseWriterDelegator) Flush() {
223
- f.ResponseWriter.(http.Flusher).Flush()
224
-}
225
-
226
-func (f *fancyResponseWriterDelegator) Hijack() (net.Conn, *bufio.ReadWriter, error) {
227
- return f.ResponseWriter.(http.Hijacker).Hijack()
228
-}
229
-
230
-func (f *fancyResponseWriterDelegator) ReadFrom(r io.Reader) (int64, error) {
231
- if !f.wroteHeader {
232
- f.WriteHeader(http.StatusOK)
233
- }
234
- n, err := f.ResponseWriter.(io.ReaderFrom).ReadFrom(r)
235
- f.written += n
236
- return n, err
237
-}
238
-
239
-func sanitizeMethod(m string) string {
240
- switch m {
241
- case "GET", "get":
242
- return "get"
243
- case "PUT", "put":
244
- return "put"
245
- case "HEAD", "head":
246
- return "head"
247
- case "POST", "post":
248
- return "post"
249
- case "DELETE", "delete":
250
- return "delete"
251
- case "CONNECT", "connect":
252
- return "connect"
253
- case "OPTIONS", "options":
254
- return "options"
255
- case "NOTIFY", "notify":
256
- return "notify"
257
- default:
258
- return strings.ToLower(m)
259
- }
260
-}
261
-
262
-func sanitizeCode(s int) string {
263
- switch s {
264
- case 100:
265
- return "100"
266
- case 101:
267
- return "101"
268
-
269
- case 200:
270
- return "200"
271
- case 201:
272
- return "201"
273
- case 202:
274
- return "202"
275
- case 203:
276
- return "203"
277
- case 204:
278
- return "204"
279
- case 205:
280
- return "205"
281
- case 206:
282
- return "206"
283
-
284
- case 300:
285
- return "300"
286
- case 301:
287
- return "301"
288
- case 302:
289
- return "302"
290
- case 304:
291
- return "304"
292
- case 305:
293
- return "305"
294
- case 307:
295
- return "307"
296
-
297
- case 400:
298
- return "400"
299
- case 401:
300
- return "401"
301
- case 402:
302
- return "402"
303
- case 403:
304
- return "403"
305
- case 404:
306
- return "404"
307
- case 405:
308
- return "405"
309
- case 406:
310
- return "406"
311
- case 407:
312
- return "407"
313
- case 408:
314
- return "408"
315
- case 409:
316
- return "409"
317
- case 410:
318
- return "410"
319
- case 411:
320
- return "411"
321
- case 412:
322
- return "412"
323
- case 413:
324
- return "413"
325
- case 414:
326
- return "414"
327
- case 415:
328
- return "415"
329
- case 416:
330
- return "416"
331
- case 417:
332
- return "417"
333
- case 418:
334
- return "418"
335
-
336
- case 500:
337
- return "500"
338
- case 501:
339
- return "501"
340
- case 502:
341
- return "502"
342
- case 503:
343
- return "503"
344
- case 504:
345
- return "504"
346
- case 505:
347
- return "505"
348
-
349
- case 428:
350
- return "428"
351
- case 429:
352
- return "429"
353
- case 431:
354
- return "431"
355
- case 511:
356
- return "511"
357
-
358
- default:
359
- return strconv.Itoa(s)
360
- }
361
-}
Godeps/_workspace/src/github.com/prometheus/client_golang/prometheus/http_test.go
deleted
-121
@@ -1,121 +0,0 @@
1
-// Copyright 2014 The Prometheus Authors
2
-// Licensed under the Apache License, Version 2.0 (the "License");
3
-// you may not use this file except in compliance with the License.
4
-// You may obtain a copy of the License at
5
-//
6
-// http://www.apache.org/licenses/LICENSE-2.0
7
-//
8
-// Unless required by applicable law or agreed to in writing, software
9
-// distributed under the License is distributed on an "AS IS" BASIS,
10
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
-// See the License for the specific language governing permissions and
12
-// limitations under the License.
13
-
14
-package prometheus
15
-
16
-import (
17
- "net/http"
18
- "net/http/httptest"
19
- "testing"
20
- "time"
21
-
22
- dto "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/prometheus/client_model/go"
23
-)
24
-
25
-type respBody string
26
-
27
-func (b respBody) ServeHTTP(w http.ResponseWriter, r *http.Request) {
28
- w.WriteHeader(http.StatusTeapot)
29
- w.Write([]byte(b))
30
-}
31
-
32
-func TestInstrumentHandler(t *testing.T) {
33
- defer func(n nower) {
34
- now = n.(nower)
35
- }(now)
36
-
37
- instant := time.Now()
38
- end := instant.Add(30 * time.Second)
39
- now = nowSeries(instant, end)
40
- respBody := respBody("Howdy there!")
41
-
42
- hndlr := InstrumentHandler("test-handler", respBody)
43
-
44
- opts := SummaryOpts{
45
- Subsystem: "http",
46
- ConstLabels: Labels{"handler": "test-handler"},
47
- }
48
-
49
- reqCnt := MustRegisterOrGet(NewCounterVec(
50
- CounterOpts{
51
- Namespace: opts.Namespace,
52
- Subsystem: opts.Subsystem,
53
- Name: "requests_total",
54
- Help: "Total number of HTTP requests made.",
55
- ConstLabels: opts.ConstLabels,
56
- },
57
- instLabels,
58
- )).(*CounterVec)
59
-
60
- opts.Name = "request_duration_microseconds"
61
- opts.Help = "The HTTP request latencies in microseconds."
62
- reqDur := MustRegisterOrGet(NewSummary(opts)).(Summary)
63
-
64
- opts.Name = "request_size_bytes"
65
- opts.Help = "The HTTP request sizes in bytes."
66
- MustRegisterOrGet(NewSummary(opts))
67
-
68
- opts.Name = "response_size_bytes"
69
- opts.Help = "The HTTP response sizes in bytes."
70
- MustRegisterOrGet(NewSummary(opts))
71
-
72
- reqCnt.Reset()
73
-
74
- resp := httptest.NewRecorder()
75
- req := &http.Request{
76
- Method: "GET",
77
- }
78
-
79
- hndlr.ServeHTTP(resp, req)
80
-
81
- if resp.Code != http.StatusTeapot {
82
- t.Fatalf("expected status %d, got %d", http.StatusTeapot, resp.Code)
83
- }
84
- if string(resp.Body.Bytes()) != "Howdy there!" {
85
- t.Fatalf("expected body %s, got %s", "Howdy there!", string(resp.Body.Bytes()))
86
- }
87
-
88
- out := &dto.Metric{}
89
- reqDur.Write(out)
90
- if want, got := "test-handler", out.Label[0].GetValue(); want != got {
91
- t.Errorf("want label value %q in reqDur, got %q", want, got)
92
- }
93
- if want, got := uint64(1), out.Summary.GetSampleCount(); want != got {
94
- t.Errorf("want sample count %d in reqDur, got %d", want, got)
95
- }
96
-
97
- out.Reset()
98
- if want, got := 1, len(reqCnt.children); want != got {
99
- t.Errorf("want %d children in reqCnt, got %d", want, got)
100
- }
101
- cnt, err := reqCnt.GetMetricWithLabelValues("get", "418")
102
- if err != nil {
103
- t.Fatal(err)
104
- }
105
- cnt.Write(out)
106
- if want, got := "418", out.Label[0].GetValue(); want != got {
107
- t.Errorf("want label value %q in reqCnt, got %q", want, got)
108
- }
109
- if want, got := "test-handler", out.Label[1].GetValue(); want != got {
110
- t.Errorf("want label value %q in reqCnt, got %q", want, got)
111
- }
112
- if want, got := "get", out.Label[2].GetValue(); want != got {
113
- t.Errorf("want label value %q in reqCnt, got %q", want, got)
114
- }
115
- if out.Counter == nil {
116
- t.Fatal("expected non-nil counter in reqCnt")
117
- }
118
- if want, got := 1., out.Counter.GetValue(); want != got {
119
- t.Errorf("want reqCnt of %f, got %f", want, got)
120
- }
121
-}
Godeps/_workspace/src/github.com/prometheus/client_golang/prometheus/metric.go
deleted
-164
@@ -1,164 +0,0 @@
1
-// Copyright 2014 The Prometheus Authors
2
-// Licensed under the Apache License, Version 2.0 (the "License");
3
-// you may not use this file except in compliance with the License.
4
-// You may obtain a copy of the License at
5
-//
6
-// http://www.apache.org/licenses/LICENSE-2.0
7
-//
8
-// Unless required by applicable law or agreed to in writing, software
9
-// distributed under the License is distributed on an "AS IS" BASIS,
10
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
-// See the License for the specific language governing permissions and
12
-// limitations under the License.
13
-
14
-package prometheus
15
-
16
-import (
17
- "strings"
18
-
19
- dto "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/prometheus/client_model/go"
20
-)
21
-
22
-// A Metric models a single sample value with its meta data being exported to
23
-// Prometheus. Implementers of Metric in this package inclued Gauge, Counter,
24
-// Untyped, and Summary. Users can implement their own Metric types, but that
25
-// should be rarely needed. See the example for SelfCollector, which is also an
26
-// example for a user-implemented Metric.
27
-type Metric interface {
28
- // Desc returns the descriptor for the Metric. This method idempotently
29
- // returns the same descriptor throughout the lifetime of the
30
- // Metric. The returned descriptor is immutable by contract. A Metric
31
- // unable to describe itself must return an invalid descriptor (created
32
- // with NewInvalidDesc).
33
- Desc() *Desc
34
- // Write encodes the Metric into a "Metric" Protocol Buffer data
35
- // transmission object.
36
- //
37
- // Implementers of custom Metric types must observe concurrency safety
38
- // as reads of this metric may occur at any time, and any blocking
39
- // occurs at the expense of total performance of rendering all
40
- // registered metrics. Ideally Metric implementations should support
41
- // concurrent readers.
42
- //
43
- // The Prometheus client library attempts to minimize memory allocations
44
- // and will provide a pre-existing reset dto.Metric pointer. Prometheus
45
- // may recycle the dto.Metric proto message, so Metric implementations
46
- // should just populate the provided dto.Metric and then should not keep
47
- // any reference to it.
48
- //
49
- // While populating dto.Metric, labels must be sorted lexicographically.
50
- // (Implementers may find LabelPairSorter useful for that.)
51
- Write(*dto.Metric) error
52
-}
53
-
54
-// Opts bundles the options for creating most Metric types. Each metric
55
-// implementation XXX has its own XXXOpts type, but in most cases, it is just be
56
-// an alias of this type (which might change when the requirement arises.)
57
-//
58
-// It is mandatory to set Name and Help to a non-empty string. All other fields
59
-// are optional and can safely be left at their zero value.
60
-type Opts struct {
61
- // Namespace, Subsystem, and Name are components of the fully-qualified
62
- // name of the Metric (created by joining these components with
63
- // "_"). Only Name is mandatory, the others merely help structuring the
64
- // name. Note that the fully-qualified name of the metric must be a
65
- // valid Prometheus metric name.
66
- Namespace string
67
- Subsystem string
68
- Name string
69
-
70
- // Help provides information about this metric. Mandatory!
71
- //
72
- // Metrics with the same fully-qualified name must have the same Help
73
- // string.
74
- Help string
75
-
76
- // ConstLabels are used to attach fixed labels to this metric. Metrics
77
- // with the same fully-qualified name must have the same label names in
78
- // their ConstLabels.
79
- //
80
- // Note that in most cases, labels have a value that varies during the
81
- // lifetime of a process. Those labels are usually managed with a metric
82
- // vector collector (like CounterVec, GaugeVec, UntypedVec). ConstLabels
83
- // serve only special purposes. One is for the special case where the
84
- // value of a label does not change during the lifetime of a process,
85
- // e.g. if the revision of the running binary is put into a
86
- // label. Another, more advanced purpose is if more than one Collector
87
- // needs to collect Metrics with the same fully-qualified name. In that
88
- // case, those Metrics must differ in the values of their
89
- // ConstLabels. See the Collector examples.
90
- //
91
- // If the value of a label never changes (not even between binaries),
92
- // that label most likely should not be a label at all (but part of the
93
- // metric name).
94
- ConstLabels Labels
95
-}
96
-
97
-// BuildFQName joins the given three name components by "_". Empty name
98
-// components are ignored. If the name parameter itself is empty, an empty
99
-// string is returned, no matter what. Metric implementations included in this
100
-// library use this function internally to generate the fully-qualified metric
101
-// name from the name component in their Opts. Users of the library will only
102
-// need this function if they implement their own Metric or instantiate a Desc
103
-// (with NewDesc) directly.
104
-func BuildFQName(namespace, subsystem, name string) string {
105
- if name == "" {
106
- return ""
107
- }
108
- switch {
109
- case namespace != "" && subsystem != "":
110
- return strings.Join([]string{namespace, subsystem, name}, "_")
111
- case namespace != "":
112
- return strings.Join([]string{namespace, name}, "_")
113
- case subsystem != "":
114
- return strings.Join([]string{subsystem, name}, "_")
115
- }
116
- return name
117
-}
118
-
119
-// LabelPairSorter implements sort.Interface. It is used to sort a slice of
120
-// dto.LabelPair pointers. This is useful for implementing the Write method of
121
-// custom metrics.
122
-type LabelPairSorter []*dto.LabelPair
123
-
124
-func (s LabelPairSorter) Len() int {
125
- return len(s)
126
-}
127
-
128
-func (s LabelPairSorter) Swap(i, j int) {
129
- s[i], s[j] = s[j], s[i]
130
-}
131
-
132
-func (s LabelPairSorter) Less(i, j int) bool {
133
- return s[i].GetName() < s[j].GetName()
134
-}
135
-
136
-type hashSorter []uint64
137
-
138
-func (s hashSorter) Len() int {
139
- return len(s)
140
-}
141
-
142
-func (s hashSorter) Swap(i, j int) {
143
- s[i], s[j] = s[j], s[i]
144
-}
145
-
146
-func (s hashSorter) Less(i, j int) bool {
147
- return s[i] < s[j]
148
-}
149
-
150
-type invalidMetric struct {
151
- desc *Desc
152
- err error
153
-}
154
-
155
-// NewInvalidMetric returns a metric whose Write method always returns the
156
-// provided error. It is useful if a Collector finds itself unable to collect
157
-// a metric and wishes to report an error to the registry.
158
-func NewInvalidMetric(desc *Desc, err error) Metric {
159
- return &invalidMetric{desc, err}
160
-}
161
-
162
-func (m *invalidMetric) Desc() *Desc { return m.desc }
163
-
164
-func (m *invalidMetric) Write(*dto.Metric) error { return m.err }
Godeps/_workspace/src/github.com/prometheus/client_golang/prometheus/metric_test.go
deleted
-35
@@ -1,35 +0,0 @@
1
-// Copyright 2014 The Prometheus Authors
2
-// Licensed under the Apache License, Version 2.0 (the "License");
3
-// you may not use this file except in compliance with the License.
4
-// You may obtain a copy of the License at
5
-//
6
-// http://www.apache.org/licenses/LICENSE-2.0
7
-//
8
-// Unless required by applicable law or agreed to in writing, software
9
-// distributed under the License is distributed on an "AS IS" BASIS,
10
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
-// See the License for the specific language governing permissions and
12
-// limitations under the License.
13
-
14
-package prometheus
15
-
16
-import "testing"
17
-
18
-func TestBuildFQName(t *testing.T) {
19
- scenarios := []struct{ namespace, subsystem, name, result string }{
20
- {"a", "b", "c", "a_b_c"},
21
- {"", "b", "c", "b_c"},
22
- {"a", "", "c", "a_c"},
23
- {"", "", "c", "c"},
24
- {"a", "b", "", ""},
25
- {"a", "", "", ""},
26
- {"", "b", "", ""},
27
- {" ", "", "", ""},
28
- }
29
-
30
- for i, s := range scenarios {
31
- if want, got := s.result, BuildFQName(s.namespace, s.subsystem, s.name); want != got {
32
- t.Errorf("%d. want %s, got %s", i, want, got)
33
- }
34
- }
35
-}
Godeps/_workspace/src/github.com/prometheus/client_golang/prometheus/process_collector.go
deleted
-142
@@ -1,142 +0,0 @@
1
-// Copyright 2015 The Prometheus Authors
2
-// Licensed under the Apache License, Version 2.0 (the "License");
3
-// you may not use this file except in compliance with the License.
4
-// You may obtain a copy of the License at
5
-//
6
-// http://www.apache.org/licenses/LICENSE-2.0
7
-//
8
-// Unless required by applicable law or agreed to in writing, software
9
-// distributed under the License is distributed on an "AS IS" BASIS,
10
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
-// See the License for the specific language governing permissions and
12
-// limitations under the License.
13
-
14
-package prometheus
15
-
16
-import "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/prometheus/procfs"
17
-
18
-type processCollector struct {
19
- pid int
20
- collectFn func(chan<- Metric)
21
- pidFn func() (int, error)
22
- cpuTotal Counter
23
- openFDs, maxFDs Gauge
24
- vsize, rss Gauge
25
- startTime Gauge
26
-}
27
-
28
-// NewProcessCollector returns a collector which exports the current state of
29
-// process metrics including cpu, memory and file descriptor usage as well as
30
-// the process start time for the given process id under the given namespace.
31
-func NewProcessCollector(pid int, namespace string) *processCollector {
32
- return NewProcessCollectorPIDFn(
33
- func() (int, error) { return pid, nil },
34
- namespace,
35
- )
36
-}
37
-
38
-// NewProcessCollectorPIDFn returns a collector which exports the current state
39
-// of process metrics including cpu, memory and file descriptor usage as well
40
-// as the process start time under the given namespace. The given pidFn is
41
-// called on each collect and is used to determine the process to export
42
-// metrics for.
43
-func NewProcessCollectorPIDFn(
44
- pidFn func() (int, error),
45
- namespace string,
46
-) *processCollector {
47
- c := processCollector{
48
- pidFn: pidFn,
49
- collectFn: func(chan<- Metric) {},
50
-
51
- cpuTotal: NewCounter(CounterOpts{
52
- Namespace: namespace,
53
- Name: "process_cpu_seconds_total",
54
- Help: "Total user and system CPU time spent in seconds.",
55
- }),
56
- openFDs: NewGauge(GaugeOpts{
57
- Namespace: namespace,
58
- Name: "process_open_fds",
59
- Help: "Number of open file descriptors.",
60
- }),
61
- maxFDs: NewGauge(GaugeOpts{
62
- Namespace: namespace,
63
- Name: "process_max_fds",
64
- Help: "Maximum number of open file descriptors.",
65
- }),
66
- vsize: NewGauge(GaugeOpts{
67
- Namespace: namespace,
68
- Name: "process_virtual_memory_bytes",
69
- Help: "Virtual memory size in bytes.",
70
- }),
71
- rss: NewGauge(GaugeOpts{
72
- Namespace: namespace,
73
- Name: "process_resident_memory_bytes",
74
- Help: "Resident memory size in bytes.",
75
- }),
76
- startTime: NewGauge(GaugeOpts{
77
- Namespace: namespace,
78
- Name: "process_start_time_seconds",
79
- Help: "Start time of the process since unix epoch in seconds.",
80
- }),
81
- }
82
-
83
- // Set up process metric collection if supported by the runtime.
84
- if _, err := procfs.NewStat(); err == nil {
85
- c.collectFn = c.processCollect
86
- }
87
-
88
- return &c
89
-}
90
-
91
-// Describe returns all descriptions of the collector.
92
-func (c *processCollector) Describe(ch chan<- *Desc) {
93
- ch <- c.cpuTotal.Desc()
94
- ch <- c.openFDs.Desc()
95
- ch <- c.maxFDs.Desc()
96
- ch <- c.vsize.Desc()
97
- ch <- c.rss.Desc()
98
- ch <- c.startTime.Desc()
99
-}
100
-
101
-// Collect returns the current state of all metrics of the collector.
102
-func (c *processCollector) Collect(ch chan<- Metric) {
103
- c.collectFn(ch)
104
-}
105
-
106
-// TODO(ts): Bring back error reporting by reverting 7faf9e7 as soon as the
107
-// client allows users to configure the error behavior.
108
-func (c *processCollector) processCollect(ch chan<- Metric) {
109
- pid, err := c.pidFn()
110
- if err != nil {
111
- return
112
- }
113
-
114
- p, err := procfs.NewProc(pid)
115
- if err != nil {
116
- return
117
- }
118
-
119
- if stat, err := p.NewStat(); err == nil {
120
- c.cpuTotal.Set(stat.CPUTime())
121
- ch <- c.cpuTotal
122
- c.vsize.Set(float64(stat.VirtualMemory()))
123
- ch <- c.vsize
124
- c.rss.Set(float64(stat.ResidentMemory()))
125
- ch <- c.rss
126
-
127
- if startTime, err := stat.StartTime(); err == nil {
128
- c.startTime.Set(startTime)
129
- ch <- c.startTime
130
- }
131
- }
132
-
133
- if fds, err := p.FileDescriptorsLen(); err == nil {
134
- c.openFDs.Set(float64(fds))
135
- ch <- c.openFDs
136
- }
137
-
138
- if limits, err := p.NewLimits(); err == nil {
139
- c.maxFDs.Set(float64(limits.OpenFiles))
140
- ch <- c.maxFDs
141
- }
142
-}
Godeps/_workspace/src/github.com/prometheus/client_golang/prometheus/process_collector_test.go
deleted
-54
@@ -1,54 +0,0 @@
1
-package prometheus
2
-
3
-import (
4
- "io/ioutil"
5
- "net/http"
6
- "net/http/httptest"
7
- "os"
8
- "regexp"
9
- "testing"
10
-
11
- "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/prometheus/procfs"
12
-)
13
-
14
-func TestProcessCollector(t *testing.T) {
15
- if _, err := procfs.Self(); err != nil {
16
- t.Skipf("skipping TestProcessCollector, procfs not available: %s", err)
17
- }
18
-
19
- registry := newRegistry()
20
- registry.Register(NewProcessCollector(os.Getpid(), ""))
21
- registry.Register(NewProcessCollectorPIDFn(
22
- func() (int, error) { return os.Getpid(), nil }, "foobar"))
23
-
24
- s := httptest.NewServer(InstrumentHandler("prometheus", registry))
25
- defer s.Close()
26
- r, err := http.Get(s.URL)
27
- if err != nil {
28
- t.Fatal(err)
29
- }
30
- defer r.Body.Close()
31
- body, err := ioutil.ReadAll(r.Body)
32
- if err != nil {
33
- t.Fatal(err)
34
- }
35
-
36
- for _, re := range []*regexp.Regexp{
37
- regexp.MustCompile("process_cpu_seconds_total [0-9]"),
38
- regexp.MustCompile("process_max_fds [0-9]{2,}"),
39
- regexp.MustCompile("process_open_fds [1-9]"),
40
- regexp.MustCompile("process_virtual_memory_bytes [1-9]"),
41
- regexp.MustCompile("process_resident_memory_bytes [1-9]"),
42
- regexp.MustCompile("process_start_time_seconds [0-9.]{10,}"),
43
- regexp.MustCompile("foobar_process_cpu_seconds_total [0-9]"),
44
- regexp.MustCompile("foobar_process_max_fds [0-9]{2,}"),
45
- regexp.MustCompile("foobar_process_open_fds [1-9]"),
46
- regexp.MustCompile("foobar_process_virtual_memory_bytes [1-9]"),
47
- regexp.MustCompile("foobar_process_resident_memory_bytes [1-9]"),
48
- regexp.MustCompile("foobar_process_start_time_seconds [0-9.]{10,}"),
49
- } {
50
- if !re.Match(body) {
51
- t.Errorf("want body to match %s\n%s", re, body)
52
- }
53
- }
54
-}
Godeps/_workspace/src/github.com/prometheus/client_golang/prometheus/push.go
deleted
-65
@@ -1,65 +0,0 @@
1
-// Copyright 2015 The Prometheus Authors
2
-// Licensed under the Apache License, Version 2.0 (the "License");
3
-// you may not use this file except in compliance with the License.
4
-// You may obtain a copy of the License at
5
-//
6
-// http://www.apache.org/licenses/LICENSE-2.0
7
-//
8
-// Unless required by applicable law or agreed to in writing, software
9
-// distributed under the License is distributed on an "AS IS" BASIS,
10
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
-// See the License for the specific language governing permissions and
12
-// limitations under the License.
13
-
14
-// Copyright (c) 2013, The Prometheus Authors
15
-// All rights reserved.
16
-//
17
-// Use of this source code is governed by a BSD-style license that can be found
18
-// in the LICENSE file.
19
-
20
-package prometheus
21
-
22
-// Push triggers a metric collection by the default registry and pushes all
23
-// collected metrics to the Pushgateway specified by addr. See the Pushgateway
24
-// documentation for detailed implications of the job and instance
25
-// parameter. instance can be left empty. You can use just host:port or ip:port
26
-// as url, in which case 'http://' is added automatically. You can also include
27
-// the schema in the URL. However, do not include the '/metrics/jobs/...' part.
28
-//
29
-// Note that all previously pushed metrics with the same job and instance will
30
-// be replaced with the metrics pushed by this call. (It uses HTTP method 'PUT'
31
-// to push to the Pushgateway.)
32
-func Push(job, instance, url string) error {
33
- return defRegistry.Push(job, instance, url, "PUT")
34
-}
35
-
36
-// PushAdd works like Push, but only previously pushed metrics with the same
37
-// name (and the same job and instance) will be replaced. (It uses HTTP method
38
-// 'POST' to push to the Pushgateway.)
39
-func PushAdd(job, instance, url string) error {
40
- return defRegistry.Push(job, instance, url, "POST")
41
-}
42
-
43
-// PushCollectors works like Push, but it does not collect from the default
44
-// registry. Instead, it collects from the provided collectors. It is a
45
-// convenient way to push only a few metrics.
46
-func PushCollectors(job, instance, url string, collectors ...Collector) error {
47
- return pushCollectors(job, instance, url, "PUT", collectors...)
48
-}
49
-
50
-// PushAddCollectors works like PushAdd, but it does not collect from the
51
-// default registry. Instead, it collects from the provided collectors. It is a
52
-// convenient way to push only a few metrics.
53
-func PushAddCollectors(job, instance, url string, collectors ...Collector) error {
54
- return pushCollectors(job, instance, url, "POST", collectors...)
55
-}
56
-
57
-func pushCollectors(job, instance, url, method string, collectors ...Collector) error {
58
- r := newRegistry()
59
- for _, collector := range collectors {
60
- if _, err := r.Register(collector); err != nil {
61
- return err
62
- }
63
- }
64
- return r.Push(job, instance, url, method)
65
-}
Godeps/_workspace/src/github.com/prometheus/client_golang/prometheus/registry.go
deleted
-760
@@ -1,760 +0,0 @@
1
-// Copyright 2014 The Prometheus Authors
2
-// Licensed under the Apache License, Version 2.0 (the "License");
3
-// you may not use this file except in compliance with the License.
4
-// You may obtain a copy of the License at
5
-//
6
-// http://www.apache.org/licenses/LICENSE-2.0
7
-//
8
-// Unless required by applicable law or agreed to in writing, software
9
-// distributed under the License is distributed on an "AS IS" BASIS,
10
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
-// See the License for the specific language governing permissions and
12
-// limitations under the License.
13
-
14
-// Copyright (c) 2013, The Prometheus Authors
15
-// All rights reserved.
16
-//
17
-// Use of this source code is governed by a BSD-style license that can be found
18
-// in the LICENSE file.
19
-
20
-package prometheus
21
-
22
-import (
23
- "bytes"
24
- "compress/gzip"
25
- "errors"
26
- "fmt"
27
- "hash/fnv"
28
- "io"
29
- "net/http"
30
- "net/url"
31
- "os"
32
- "sort"
33
- "strings"
34
- "sync"
35
-
36
- "github.com/ipfs/go-ipfs/Godeps/_workspace/src/bitbucket.org/ww/goautoneg"
37
- "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/golang/protobuf/proto"
38
-
39
- dto "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/prometheus/client_model/go"
40
-
41
- "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/prometheus/client_golang/model"
42
- "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/prometheus/client_golang/text"
43
-)
44
-
45
-var (
46
- defRegistry = newDefaultRegistry()
47
- errAlreadyReg = errors.New("duplicate metrics collector registration attempted")
48
-)
49
-
50
-// Constants relevant to the HTTP interface.
51
-const (
52
- // APIVersion is the version of the format of the exported data. This
53
- // will match this library's version, which subscribes to the Semantic
54
- // Versioning scheme.
55
- APIVersion = "0.0.4"
56
-
57
- // DelimitedTelemetryContentType is the content type set on telemetry
58
- // data responses in delimited protobuf format.
59
- DelimitedTelemetryContentType = `application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily; encoding=delimited`
60
- // TextTelemetryContentType is the content type set on telemetry data
61
- // responses in text format.
62
- TextTelemetryContentType = `text/plain; version=` + APIVersion
63
- // ProtoTextTelemetryContentType is the content type set on telemetry
64
- // data responses in protobuf text format. (Only used for debugging.)
65
- ProtoTextTelemetryContentType = `application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily; encoding=text`
66
- // ProtoCompactTextTelemetryContentType is the content type set on
67
- // telemetry data responses in protobuf compact text format. (Only used
68
- // for debugging.)
69
- ProtoCompactTextTelemetryContentType = `application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily; encoding=compact-text`
70
-
71
- // Constants for object pools.
72
- numBufs = 4
73
- numMetricFamilies = 1000
74
- numMetrics = 10000
75
-
76
- // Capacity for the channel to collect metrics and descriptors.
77
- capMetricChan = 1000
78
- capDescChan = 10
79
-
80
- contentTypeHeader = "Content-Type"
81
- contentLengthHeader = "Content-Length"
82
- contentEncodingHeader = "Content-Encoding"
83
-
84
- acceptEncodingHeader = "Accept-Encoding"
85
- acceptHeader = "Accept"
86
-)
87
-
88
-// Handler returns the HTTP handler for the global Prometheus registry. It is
89
-// already instrumented with InstrumentHandler (using "prometheus" as handler
90
-// name). Usually the handler is used to handle the "/metrics" endpoint.
91
-func Handler() http.Handler {
92
- return InstrumentHandler("prometheus", defRegistry)
93
-}
94
-
95
-// UninstrumentedHandler works in the same way as Handler, but the returned HTTP
96
-// handler is not instrumented. This is useful if no instrumentation is desired
97
-// (for whatever reason) or if the instrumentation has to happen with a
98
-// different handler name (or with a different instrumentation approach
99
-// altogether). See the InstrumentHandler example.
100
-func UninstrumentedHandler() http.Handler {
101
- return defRegistry
102
-}
103
-
104
-// Register registers a new Collector to be included in metrics collection. It
105
-// returns an error if the descriptors provided by the Collector are invalid or
106
-// if they - in combination with descriptors of already registered Collectors -
107
-// do not fulfill the consistency and uniqueness criteria described in the Desc
108
-// documentation.
109
-//
110
-// Do not register the same Collector multiple times concurrently. (Registering
111
-// the same Collector twice would result in an error anyway, but on top of that,
112
-// it is not safe to do so concurrently.)
113
-func Register(m Collector) error {
114
- _, err := defRegistry.Register(m)
115
- return err
116
-}
117
-
118
-// MustRegister works like Register but panics where Register would have
119
-// returned an error.
120
-func MustRegister(m Collector) {
121
- err := Register(m)
122
- if err != nil {
123
- panic(err)
124
- }
125
-}
126
-
127
-// RegisterOrGet works like Register but does not return an error if a Collector
128
-// is registered that equals a previously registered Collector. (Two Collectors
129
-// are considered equal if their Describe method yields the same set of
130
-// descriptors.) Instead, the previously registered Collector is returned (which
131
-// is helpful if the new and previously registered Collectors are equal but not
132
-// identical, i.e. not pointers to the same object).
133
-//
134
-// As for Register, it is still not safe to call RegisterOrGet with the same
135
-// Collector multiple times concurrently.
136
-func RegisterOrGet(m Collector) (Collector, error) {
137
- return defRegistry.RegisterOrGet(m)
138
-}
139
-
140
-// MustRegisterOrGet works like Register but panics where RegisterOrGet would
141
-// have returned an error.
142
-func MustRegisterOrGet(m Collector) Collector {
143
- existing, err := RegisterOrGet(m)
144
- if err != nil {
145
- panic(err)
146
- }
147
- return existing
148
-}
149
-
150
-// Unregister unregisters the Collector that equals the Collector passed in as
151
-// an argument. (Two Collectors are considered equal if their Describe method
152
-// yields the same set of descriptors.) The function returns whether a Collector
153
-// was unregistered.
154
-func Unregister(c Collector) bool {
155
- return defRegistry.Unregister(c)
156
-}
157
-
158
-// SetMetricFamilyInjectionHook sets a function that is called whenever metrics
159
-// are collected. The hook function must be set before metrics collection begins
160
-// (i.e. call SetMetricFamilyInjectionHook before setting the HTTP handler.) The
161
-// MetricFamily protobufs returned by the hook function are merged with the
162
-// metrics collected in the usual way.
163
-//
164
-// This is a way to directly inject MetricFamily protobufs managed and owned by
165
-// the caller. The caller has full responsibility. As no registration of the
166
-// injected metrics has happened, there is no descriptor to check against, and
167
-// there are no registration-time checks. If collect-time checks are disabled
168
-// (see function EnableCollectChecks), no sanity checks are performed on the
169
-// returned protobufs at all. If collect-checks are enabled, type and uniqueness
170
-// checks are performed, but no further consistency checks (which would require
171
-// knowledge of a metric descriptor).
172
-//
173
-// Sorting concerns: The caller is responsible for sorting the label pairs in
174
-// each metric. However, the order of metrics will be sorted by the registry as
175
-// it is required anyway after merging with the metric families collected
176
-// conventionally.
177
-//
178
-// The function must be callable at any time and concurrently.
179
-func SetMetricFamilyInjectionHook(hook func() []*dto.MetricFamily) {
180
- defRegistry.metricFamilyInjectionHook = hook
181
-}
182
-
183
-// PanicOnCollectError sets the behavior whether a panic is caused upon an error
184
-// while metrics are collected and served to the HTTP endpoint. By default, an
185
-// internal server error (status code 500) is served with an error message.
186
-func PanicOnCollectError(b bool) {
187
- defRegistry.panicOnCollectError = b
188
-}
189
-
190
-// EnableCollectChecks enables (or disables) additional consistency checks
191
-// during metrics collection. These additional checks are not enabled by default
192
-// because they inflict a performance penalty and the errors they check for can
193
-// only happen if the used Metric and Collector types have internal programming
194
-// errors. It can be helpful to enable these checks while working with custom
195
-// Collectors or Metrics whose correctness is not well established yet.
196
-func EnableCollectChecks(b bool) {
197
- defRegistry.collectChecksEnabled = b
198
-}
199
-
200
-// encoder is a function that writes a dto.MetricFamily to an io.Writer in a
201
-// certain encoding. It returns the number of bytes written and any error
202
-// encountered. Note that pbutil.WriteDelimited and pbutil.MetricFamilyToText
203
-// are encoders.
204
-type encoder func(io.Writer, *dto.MetricFamily) (int, error)
205
-
206
-type registry struct {
207
- mtx sync.RWMutex
208
- collectorsByID map[uint64]Collector // ID is a hash of the descIDs.
209
- descIDs map[uint64]struct{}
210
- dimHashesByName map[string]uint64
211
- bufPool chan *bytes.Buffer
212
- metricFamilyPool chan *dto.MetricFamily
213
- metricPool chan *dto.Metric
214
- metricFamilyInjectionHook func() []*dto.MetricFamily
215
-
216
- panicOnCollectError, collectChecksEnabled bool
217
-}
218
-
219
-func (r *registry) Register(c Collector) (Collector, error) {
220
- descChan := make(chan *Desc, capDescChan)
221
- go func() {
222
- c.Describe(descChan)
223
- close(descChan)
224
- }()
225
-
226
- newDescIDs := map[uint64]struct{}{}
227
- newDimHashesByName := map[string]uint64{}
228
- var collectorID uint64 // Just a sum of all desc IDs.
229
- var duplicateDescErr error
230
-
231
- r.mtx.Lock()
232
- defer r.mtx.Unlock()
233
- // Coduct various tests...
234
- for desc := range descChan {
235
-
236
- // Is the descriptor valid at all?
237
- if desc.err != nil {
238
- return c, fmt.Errorf("descriptor %s is invalid: %s", desc, desc.err)
239
- }
240
-
241
- // Is the descID unique?
242
- // (In other words: Is the fqName + constLabel combination unique?)
243
- if _, exists := r.descIDs[desc.id]; exists {
244
- duplicateDescErr = fmt.Errorf("descriptor %s already exists with the same fully-qualified name and const label values", desc)
245
- }
246
- // If it is not a duplicate desc in this collector, add it to
247
- // the collectorID. (We allow duplicate descs within the same
248
- // collector, but their existence must be a no-op.)
249
- if _, exists := newDescIDs[desc.id]; !exists {
250
- newDescIDs[desc.id] = struct{}{}
251
- collectorID += desc.id
252
- }
253
-
254
- // Are all the label names and the help string consistent with
255
- // previous descriptors of the same name?
256
- // First check existing descriptors...
257
- if dimHash, exists := r.dimHashesByName[desc.fqName]; exists {
258
- if dimHash != desc.dimHash {
259
- return nil, fmt.Errorf("a previously registered descriptor with the same fully-qualified name as %s has different label names or a different help string", desc)
260
- }
261
- } else {
262
- // ...then check the new descriptors already seen.
263
- if dimHash, exists := newDimHashesByName[desc.fqName]; exists {
264
- if dimHash != desc.dimHash {
265
- return nil, fmt.Errorf("descriptors reported by collector have inconsistent label names or help strings for the same fully-qualified name, offender is %s", desc)
266
- }
267
- } else {
268
- newDimHashesByName[desc.fqName] = desc.dimHash
269
- }
270
- }
271
- }
272
- // Did anything happen at all?
273
- if len(newDescIDs) == 0 {
274
- return nil, errors.New("collector has no descriptors")
275
- }
276
- if existing, exists := r.collectorsByID[collectorID]; exists {
277
- return existing, errAlreadyReg
278
- }
279
- // If the collectorID is new, but at least one of the descs existed
280
- // before, we are in trouble.
281
- if duplicateDescErr != nil {
282
- return nil, duplicateDescErr
283
- }
284
-
285
- // Only after all tests have passed, actually register.
286
- r.collectorsByID[collectorID] = c
287
- for hash := range newDescIDs {
288
- r.descIDs[hash] = struct{}{}
289
- }
290
- for name, dimHash := range newDimHashesByName {
291
- r.dimHashesByName[name] = dimHash
292
- }
293
- return c, nil
294
-}
295
-
296
-func (r *registry) RegisterOrGet(m Collector) (Collector, error) {
297
- existing, err := r.Register(m)
298
- if err != nil && err != errAlreadyReg {
299
- return nil, err
300
- }
301
- return existing, nil
302
-}
303
-
304
-func (r *registry) Unregister(c Collector) bool {
305
- descChan := make(chan *Desc, capDescChan)
306
- go func() {
307
- c.Describe(descChan)
308
- close(descChan)
309
- }()
310
-
311
- descIDs := map[uint64]struct{}{}
312
- var collectorID uint64 // Just a sum of the desc IDs.
313
- for desc := range descChan {
314
- if _, exists := descIDs[desc.id]; !exists {
315
- collectorID += desc.id
316
- descIDs[desc.id] = struct{}{}
317
- }
318
- }
319
-
320
- r.mtx.RLock()
321
- if _, exists := r.collectorsByID[collectorID]; !exists {
322
- r.mtx.RUnlock()
323
- return false
324
- }
325
- r.mtx.RUnlock()
326
-
327
- r.mtx.Lock()
328
- defer r.mtx.Unlock()
329
-
330
- delete(r.collectorsByID, collectorID)
331
- for id := range descIDs {
332
- delete(r.descIDs, id)
333
- }
334
- // dimHashesByName is left untouched as those must be consistent
335
- // throughout the lifetime of a program.
336
- return true
337
-}
338
-
339
-func (r *registry) Push(job, instance, pushURL, method string) error {
340
- if !strings.Contains(pushURL, "://") {
341
- pushURL = "http://" + pushURL
342
- }
343
- pushURL = fmt.Sprintf("%s/metrics/jobs/%s", pushURL, url.QueryEscape(job))
344
- if instance != "" {
345
- pushURL += "/instances/" + url.QueryEscape(instance)
346
- }
347
- buf := r.getBuf()
348
- defer r.giveBuf(buf)
349
- if _, err := r.writePB(buf, text.WriteProtoDelimited); err != nil {
350
- if r.panicOnCollectError {
351
- panic(err)
352
- }
353
- return err
354
- }
355
- req, err := http.NewRequest(method, pushURL, buf)
356
- if err != nil {
357
- return err
358
- }
359
- req.Header.Set(contentTypeHeader, DelimitedTelemetryContentType)
360
- resp, err := http.DefaultClient.Do(req)
361
- if err != nil {
362
- return err
363
- }
364
- defer resp.Body.Close()
365
- if resp.StatusCode != 202 {
366
- return fmt.Errorf("unexpected status code %d while pushing to %s", resp.StatusCode, pushURL)
367
- }
368
- return nil
369
-}
370
-
371
-func (r *registry) ServeHTTP(w http.ResponseWriter, req *http.Request) {
372
- enc, contentType := chooseEncoder(req)
373
- buf := r.getBuf()
374
- defer r.giveBuf(buf)
375
- writer, encoding := decorateWriter(req, buf)
376
- if _, err := r.writePB(writer, enc); err != nil {
377
- if r.panicOnCollectError {
378
- panic(err)
379
- }
380
- http.Error(w, "An error has occurred:\n\n"+err.Error(), http.StatusInternalServerError)
381
- return
382
- }
383
- if closer, ok := writer.(io.Closer); ok {
384
- closer.Close()
385
- }
386
- header := w.Header()
387
- header.Set(contentTypeHeader, contentType)
388
- header.Set(contentLengthHeader, fmt.Sprint(buf.Len()))
389
- if encoding != "" {
390
- header.Set(contentEncodingHeader, encoding)
391
- }
392
- w.Write(buf.Bytes())
393
-}
394
-
395
-func (r *registry) writePB(w io.Writer, writeEncoded encoder) (int, error) {
396
- var metricHashes map[uint64]struct{}
397
- if r.collectChecksEnabled {
398
- metricHashes = make(map[uint64]struct{})
399
- }
400
- metricChan := make(chan Metric, capMetricChan)
401
- wg := sync.WaitGroup{}
402
-
403
- r.mtx.RLock()
404
- metricFamiliesByName := make(map[string]*dto.MetricFamily, len(r.dimHashesByName))
405
-
406
- // Scatter.
407
- // (Collectors could be complex and slow, so we call them all at once.)
408
- wg.Add(len(r.collectorsByID))
409
- go func() {
410
- wg.Wait()
411
- close(metricChan)
412
- }()
413
- for _, collector := range r.collectorsByID {
414
- go func(collector Collector) {
415
- defer wg.Done()
416
- collector.Collect(metricChan)
417
- }(collector)
418
- }
419
- r.mtx.RUnlock()
420
-
421
- // Drain metricChan in case of premature return.
422
- defer func() {
423
- for _ = range metricChan {
424
- }
425
- }()
426
-
427
- // Gather.
428
- for metric := range metricChan {
429
- // This could be done concurrently, too, but it required locking
430
- // of metricFamiliesByName (and of metricHashes if checks are
431
- // enabled). Most likely not worth it.
432
- desc := metric.Desc()
433
- metricFamily, ok := metricFamiliesByName[desc.fqName]
434
- if !ok {
435
- metricFamily = r.getMetricFamily()
436
- defer r.giveMetricFamily(metricFamily)
437
- metricFamily.Name = proto.String(desc.fqName)
438
- metricFamily.Help = proto.String(desc.help)
439
- metricFamiliesByName[desc.fqName] = metricFamily
440
- }
441
- dtoMetric := r.getMetric()
442
- defer r.giveMetric(dtoMetric)
443
- if err := metric.Write(dtoMetric); err != nil {
444
- // TODO: Consider different means of error reporting so
445
- // that a single erroneous metric could be skipped
446
- // instead of blowing up the whole collection.
447
- return 0, fmt.Errorf("error collecting metric %v: %s", desc, err)
448
- }
449
- switch {
450
- case metricFamily.Type != nil:
451
- // Type already set. We are good.
452
- case dtoMetric.Gauge != nil:
453
- metricFamily.Type = dto.MetricType_GAUGE.Enum()
454
- case dtoMetric.Counter != nil:
455
- metricFamily.Type = dto.MetricType_COUNTER.Enum()
456
- case dtoMetric.Summary != nil:
457
- metricFamily.Type = dto.MetricType_SUMMARY.Enum()
458
- case dtoMetric.Untyped != nil:
459
- metricFamily.Type = dto.MetricType_UNTYPED.Enum()
460
- case dtoMetric.Histogram != nil:
461
- metricFamily.Type = dto.MetricType_HISTOGRAM.Enum()
462
- default:
463
- return 0, fmt.Errorf("empty metric collected: %s", dtoMetric)
464
- }
465
- if r.collectChecksEnabled {
466
- if err := r.checkConsistency(metricFamily, dtoMetric, desc, metricHashes); err != nil {
467
- return 0, err
468
- }
469
- }
470
- metricFamily.Metric = append(metricFamily.Metric, dtoMetric)
471
- }
472
-
473
- if r.metricFamilyInjectionHook != nil {
474
- for _, mf := range r.metricFamilyInjectionHook() {
475
- existingMF, exists := metricFamiliesByName[mf.GetName()]
476
- if !exists {
477
- metricFamiliesByName[mf.GetName()] = mf
478
- if r.collectChecksEnabled {
479
- for _, m := range mf.Metric {
480
- if err := r.checkConsistency(mf, m, nil, metricHashes); err != nil {
481
- return 0, err
482
- }
483
- }
484
- }
485
- continue
486
- }
487
- for _, m := range mf.Metric {
488
- if r.collectChecksEnabled {
489
- if err := r.checkConsistency(existingMF, m, nil, metricHashes); err != nil {
490
- return 0, err
491
- }
492
- }
493
- existingMF.Metric = append(existingMF.Metric, m)
494
- }
495
- }
496
- }
497
-
498
- // Now that MetricFamilies are all set, sort their Metrics
499
- // lexicographically by their label values.
500
- for _, mf := range metricFamiliesByName {
501
- sort.Sort(metricSorter(mf.Metric))
502
- }
503
-
504
- // Write out MetricFamilies sorted by their name.
505
- names := make([]string, 0, len(metricFamiliesByName))
506
- for name := range metricFamiliesByName {
507
- names = append(names, name)
508
- }
509
- sort.Strings(names)
510
-
511
- var written int
512
- for _, name := range names {
513
- w, err := writeEncoded(w, metricFamiliesByName[name])
514
- written += w
515
- if err != nil {
516
- return written, err
517
- }
518
- }
519
- return written, nil
520
-}
521
-
522
-func (r *registry) checkConsistency(metricFamily *dto.MetricFamily, dtoMetric *dto.Metric, desc *Desc, metricHashes map[uint64]struct{}) error {
523
-
524
- // Type consistency with metric family.
525
- if metricFamily.GetType() == dto.MetricType_GAUGE && dtoMetric.Gauge == nil ||
526
- metricFamily.GetType() == dto.MetricType_COUNTER && dtoMetric.Counter == nil ||
527
- metricFamily.GetType() == dto.MetricType_SUMMARY && dtoMetric.Summary == nil ||
528
- metricFamily.GetType() == dto.MetricType_HISTOGRAM && dtoMetric.Histogram == nil ||
529
- metricFamily.GetType() == dto.MetricType_UNTYPED && dtoMetric.Untyped == nil {
530
- return fmt.Errorf(
531
- "collected metric %s %s is not a %s",
532
- metricFamily.GetName(), dtoMetric, metricFamily.GetType(),
533
- )
534
- }
535
-
536
- // Is the metric unique (i.e. no other metric with the same name and the same label values)?
537
- h := fnv.New64a()
538
- var buf bytes.Buffer
539
- buf.WriteString(metricFamily.GetName())
540
- buf.WriteByte(model.SeparatorByte)
541
- h.Write(buf.Bytes())
542
- // Make sure label pairs are sorted. We depend on it for the consistency
543
- // check. Label pairs must be sorted by contract. But the point of this
544
- // method is to check for contract violations. So we better do the sort
545
- // now.
546
- sort.Sort(LabelPairSorter(dtoMetric.Label))
547
- for _, lp := range dtoMetric.Label {
548
- buf.Reset()
549
- buf.WriteString(lp.GetValue())
550
- buf.WriteByte(model.SeparatorByte)
551
- h.Write(buf.Bytes())
552
- }
553
- metricHash := h.Sum64()
554
- if _, exists := metricHashes[metricHash]; exists {
555
- return fmt.Errorf(
556
- "collected metric %s %s was collected before with the same name and label values",
557
- metricFamily.GetName(), dtoMetric,
558
- )
559
- }
560
- metricHashes[metricHash] = struct{}{}
561
-
562
- if desc == nil {
563
- return nil // Nothing left to check if we have no desc.
564
- }
565
-
566
- // Desc consistency with metric family.
567
- if metricFamily.GetName() != desc.fqName {
568
- return fmt.Errorf(
569
- "collected metric %s %s has name %q but should have %q",
570
- metricFamily.GetName(), dtoMetric, metricFamily.GetName(), desc.fqName,
571
- )
572
- }
573
- if metricFamily.GetHelp() != desc.help {
574
- return fmt.Errorf(
575
- "collected metric %s %s has help %q but should have %q",
576
- metricFamily.GetName(), dtoMetric, metricFamily.GetHelp(), desc.help,
577
- )
578
- }
579
-
580
- // Is the desc consistent with the content of the metric?
581
- lpsFromDesc := make([]*dto.LabelPair, 0, len(dtoMetric.Label))
582
- lpsFromDesc = append(lpsFromDesc, desc.constLabelPairs...)
583
- for _, l := range desc.variableLabels {
584
- lpsFromDesc = append(lpsFromDesc, &dto.LabelPair{
585
- Name: proto.String(l),
586
- })
587
- }
588
- if len(lpsFromDesc) != len(dtoMetric.Label) {
589
- return fmt.Errorf(
590
- "labels in collected metric %s %s are inconsistent with descriptor %s",
591
- metricFamily.GetName(), dtoMetric, desc,
592
- )
593
- }
594
- sort.Sort(LabelPairSorter(lpsFromDesc))
595
- for i, lpFromDesc := range lpsFromDesc {
596
- lpFromMetric := dtoMetric.Label[i]
597
- if lpFromDesc.GetName() != lpFromMetric.GetName() ||
598
- lpFromDesc.Value != nil && lpFromDesc.GetValue() != lpFromMetric.GetValue() {
599
- return fmt.Errorf(
600
- "labels in collected metric %s %s are inconsistent with descriptor %s",
601
- metricFamily.GetName(), dtoMetric, desc,
602
- )
603
- }
604
- }
605
-
606
- r.mtx.RLock() // Remaining checks need the read lock.
607
- defer r.mtx.RUnlock()
608
-
609
- // Is the desc registered?
610
- if _, exist := r.descIDs[desc.id]; !exist {
611
- return fmt.Errorf(
612
- "collected metric %s %s with unregistered descriptor %s",
613
- metricFamily.GetName(), dtoMetric, desc,
614
- )
615
- }
616
-
617
- return nil
618
-}
619
-
620
-func (r *registry) getBuf() *bytes.Buffer {
621
- select {
622
- case buf := <-r.bufPool:
623
- return buf
624
- default:
625
- return &bytes.Buffer{}
626
- }
627
-}
628
-
629
-func (r *registry) giveBuf(buf *bytes.Buffer) {
630
- buf.Reset()
631
- select {
632
- case r.bufPool <- buf:
633
- default:
634
- }
635
-}
636
-
637
-func (r *registry) getMetricFamily() *dto.MetricFamily {
638
- select {
639
- case mf := <-r.metricFamilyPool:
640
- return mf
641
- default:
642
- return &dto.MetricFamily{}
643
- }
644
-}
645
-
646
-func (r *registry) giveMetricFamily(mf *dto.MetricFamily) {
647
- mf.Reset()
648
- select {
649
- case r.metricFamilyPool <- mf:
650
- default:
651
- }
652
-}
653
-
654
-func (r *registry) getMetric() *dto.Metric {
655
- select {
656
- case m := <-r.metricPool:
657
- return m
658
- default:
659
- return &dto.Metric{}
660
- }
661
-}
662
-
663
-func (r *registry) giveMetric(m *dto.Metric) {
664
- m.Reset()
665
- select {
666
- case r.metricPool <- m:
667
- default:
668
- }
669
-}
670
-
671
-func newRegistry() *registry {
672
- return ®istry{
673
- collectorsByID: map[uint64]Collector{},
674
- descIDs: map[uint64]struct{}{},
675
- dimHashesByName: map[string]uint64{},
676
- bufPool: make(chan *bytes.Buffer, numBufs),
677
- metricFamilyPool: make(chan *dto.MetricFamily, numMetricFamilies),
678
- metricPool: make(chan *dto.Metric, numMetrics),
679
- }
680
-}
681
-
682
-func newDefaultRegistry() *registry {
683
- r := newRegistry()
684
- r.Register(NewProcessCollector(os.Getpid(), ""))
685
- r.Register(NewGoCollector())
686
- return r
687
-}
688
-
689
-func chooseEncoder(req *http.Request) (encoder, string) {
690
- accepts := goautoneg.ParseAccept(req.Header.Get(acceptHeader))
691
- for _, accept := range accepts {
692
- switch {
693
- case accept.Type == "application" &&
694
- accept.SubType == "vnd.google.protobuf" &&
695
- accept.Params["proto"] == "io.prometheus.client.MetricFamily":
696
- switch accept.Params["encoding"] {
697
- case "delimited":
698
- return text.WriteProtoDelimited, DelimitedTelemetryContentType
699
- case "text":
700
- return text.WriteProtoText, ProtoTextTelemetryContentType
701
- case "compact-text":
702
- return text.WriteProtoCompactText, ProtoCompactTextTelemetryContentType
703
- default:
704
- continue
705
- }
706
- case accept.Type == "text" &&
707
- accept.SubType == "plain" &&
708
- (accept.Params["version"] == "0.0.4" || accept.Params["version"] == ""):
709
- return text.MetricFamilyToText, TextTelemetryContentType
710
- default:
711
- continue
712
- }
713
- }
714
- return text.MetricFamilyToText, TextTelemetryContentType
715
-}
716
-
717
-// decorateWriter wraps a writer to handle gzip compression if requested. It
718
-// returns the decorated writer and the appropriate "Content-Encoding" header
719
-// (which is empty if no compression is enabled).
720
-func decorateWriter(request *http.Request, writer io.Writer) (io.Writer, string) {
721
- header := request.Header.Get(acceptEncodingHeader)
722
- parts := strings.Split(header, ",")
723
- for _, part := range parts {
724
- part := strings.TrimSpace(part)
725
- if part == "gzip" || strings.HasPrefix(part, "gzip;") {
726
- return gzip.NewWriter(writer), "gzip"
727
- }
728
- }
729
- return writer, ""
730
-}
731
-
732
-type metricSorter []*dto.Metric
733
-
734
-func (s metricSorter) Len() int {
735
- return len(s)
736
-}
737
-
738
-func (s metricSorter) Swap(i, j int) {
739
- s[i], s[j] = s[j], s[i]
740
-}
741
-
742
-func (s metricSorter) Less(i, j int) bool {
743
- if len(s[i].Label) != len(s[j].Label) {
744
- // This should not happen. The metrics are
745
- // inconsistent. However, we have to deal with the fact, as
746
- // people might use custom collectors or metric family injection
747
- // to create inconsistent metrics. So let's simply compare the
748
- // number of labels in this case. That will still yield
749
- // reproducible sorting.
750
- return len(s[i].Label) < len(s[j].Label)
751
- }
752
- for n, lp := range s[i].Label {
753
- vi := lp.GetValue()
754
- vj := s[j].Label[n].GetValue()
755
- if vi != vj {
756
- return vi < vj
757
- }
758
- }
759
- return true
760
-}
Godeps/_workspace/src/github.com/prometheus/client_golang/prometheus/registry_test.go
deleted
-535
@@ -1,535 +0,0 @@
1
-// Copyright 2014 The Prometheus Authors
2
-// Licensed under the Apache License, Version 2.0 (the "License");
3
-// you may not use this file except in compliance with the License.
4
-// You may obtain a copy of the License at
5
-//
6
-// http://www.apache.org/licenses/LICENSE-2.0
7
-//
8
-// Unless required by applicable law or agreed to in writing, software
9
-// distributed under the License is distributed on an "AS IS" BASIS,
10
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
-// See the License for the specific language governing permissions and
12
-// limitations under the License.
13
-
14
-// Copyright (c) 2013, The Prometheus Authors
15
-// All rights reserved.
16
-//
17
-// Use of this source code is governed by a BSD-style license that can be found
18
-// in the LICENSE file.
19
-
20
-package prometheus
21
-
22
-import (
23
- "bytes"
24
- "encoding/binary"
25
- "net/http"
26
- "testing"
27
-
28
- "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/golang/protobuf/proto"
29
- dto "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/prometheus/client_model/go"
30
-)
31
-
32
-type fakeResponseWriter struct {
33
- header http.Header
34
- body bytes.Buffer
35
-}
36
-
37
-func (r *fakeResponseWriter) Header() http.Header {
38
- return r.header
39
-}
40
-
41
-func (r *fakeResponseWriter) Write(d []byte) (l int, err error) {
42
- return r.body.Write(d)
43
-}
44
-
45
-func (r *fakeResponseWriter) WriteHeader(c int) {
46
-}
47
-
48
-func testHandler(t testing.TB) {
49
-
50
- metricVec := NewCounterVec(
51
- CounterOpts{
52
- Name: "name",
53
- Help: "docstring",
54
- ConstLabels: Labels{"constname": "constvalue"},
55
- },
56
- []string{"labelname"},
57
- )
58
-
59
- metricVec.WithLabelValues("val1").Inc()
60
- metricVec.WithLabelValues("val2").Inc()
61
-
62
- varintBuf := make([]byte, binary.MaxVarintLen32)
63
-
64
- externalMetricFamily := &dto.MetricFamily{
65
- Name: proto.String("externalname"),
66
- Help: proto.String("externaldocstring"),
67
- Type: dto.MetricType_COUNTER.Enum(),
68
- Metric: []*dto.Metric{
69
- {
70
- Label: []*dto.LabelPair{
71
- {
72
- Name: proto.String("externalconstname"),
73
- Value: proto.String("externalconstvalue"),
74
- },
75
- {
76
- Name: proto.String("externallabelname"),
77
- Value: proto.String("externalval1"),
78
- },
79
- },
80
- Counter: &dto.Counter{
81
- Value: proto.Float64(1),
82
- },
83
- },
84
- },
85
- }
86
- marshaledExternalMetricFamily, err := proto.Marshal(externalMetricFamily)
87
- if err != nil {
88
- t.Fatal(err)
89
- }
90
- var externalBuf bytes.Buffer
91
- l := binary.PutUvarint(varintBuf, uint64(len(marshaledExternalMetricFamily)))
92
- _, err = externalBuf.Write(varintBuf[:l])
93
- if err != nil {
94
- t.Fatal(err)
95
- }
96
- _, err = externalBuf.Write(marshaledExternalMetricFamily)
97
- if err != nil {
98
- t.Fatal(err)
99
- }
100
- externalMetricFamilyAsBytes := externalBuf.Bytes()
101
- externalMetricFamilyAsText := []byte(`# HELP externalname externaldocstring
102
-# TYPE externalname counter
103
-externalname{externalconstname="externalconstvalue",externallabelname="externalval1"} 1
104
-`)
105
- externalMetricFamilyAsProtoText := []byte(`name: "externalname"
106
-help: "externaldocstring"
107
-type: COUNTER
108
-metric: <
109
- label: <
110
- name: "externalconstname"
111
- value: "externalconstvalue"
112
- >
113
- label: <
114
- name: "externallabelname"
115
- value: "externalval1"
116
- >
117
- counter: <
118
- value: 1
119
- >
120
->
121
-
122
-`)
123
- externalMetricFamilyAsProtoCompactText := []byte(`name:"externalname" help:"externaldocstring" type:COUNTER metric:<label:<name:"externalconstname" value:"externalconstvalue" > label:<name:"externallabelname" value:"externalval1" > counter:<value:1 > >
124
-`)
125
-
126
- expectedMetricFamily := &dto.MetricFamily{
127
- Name: proto.String("name"),
128
- Help: proto.String("docstring"),
129
- Type: dto.MetricType_COUNTER.Enum(),
130
- Metric: []*dto.Metric{
131
- {
132
- Label: []*dto.LabelPair{
133
- {
134
- Name: proto.String("constname"),
135
- Value: proto.String("constvalue"),
136
- },
137
- {
138
- Name: proto.String("labelname"),
139
- Value: proto.String("val1"),
140
- },
141
- },
142
- Counter: &dto.Counter{
143
- Value: proto.Float64(1),
144
- },
145
- },
146
- {
147
- Label: []*dto.LabelPair{
148
- {
149
- Name: proto.String("constname"),
150
- Value: proto.String("constvalue"),
151
- },
152
- {
153
- Name: proto.String("labelname"),
154
- Value: proto.String("val2"),
155
- },
156
- },
157
- Counter: &dto.Counter{
158
- Value: proto.Float64(1),
159
- },
160
- },
161
- },
162
- }
163
- marshaledExpectedMetricFamily, err := proto.Marshal(expectedMetricFamily)
164
- if err != nil {
165
- t.Fatal(err)
166
- }
167
- var buf bytes.Buffer
168
- l = binary.PutUvarint(varintBuf, uint64(len(marshaledExpectedMetricFamily)))
169
- _, err = buf.Write(varintBuf[:l])
170
- if err != nil {
171
- t.Fatal(err)
172
- }
173
- _, err = buf.Write(marshaledExpectedMetricFamily)
174
- if err != nil {
175
- t.Fatal(err)
176
- }
177
- expectedMetricFamilyAsBytes := buf.Bytes()
178
- expectedMetricFamilyAsText := []byte(`# HELP name docstring
179
-# TYPE name counter
180
-name{constname="constvalue",labelname="val1"} 1
181
-name{constname="constvalue",labelname="val2"} 1
182
-`)
183
- expectedMetricFamilyAsProtoText := []byte(`name: "name"
184
-help: "docstring"
185
-type: COUNTER
186
-metric: <
187
- label: <
188
- name: "constname"
189
- value: "constvalue"
190
- >
191
- label: <
192
- name: "labelname"
193
- value: "val1"
194
- >
195
- counter: <
196
- value: 1
197
- >
198
->
199
-metric: <
200
- label: <
201
- name: "constname"
202
- value: "constvalue"
203
- >
204
- label: <
205
- name: "labelname"
206
- value: "val2"
207
- >
208
- counter: <
209
- value: 1
210
- >
211
->
212
-
213
-`)
214
- expectedMetricFamilyAsProtoCompactText := []byte(`name:"name" help:"docstring" type:COUNTER metric:<label:<name:"constname" value:"constvalue" > label:<name:"labelname" value:"val1" > counter:<value:1 > > metric:<label:<name:"constname" value:"constvalue" > label:<name:"labelname" value:"val2" > counter:<value:1 > >
215
-`)
216
-
217
- externalMetricFamilyWithSameName := &dto.MetricFamily{
218
- Name: proto.String("name"),
219
- Help: proto.String("inconsistent help string does not matter here"),
220
- Type: dto.MetricType_COUNTER.Enum(),
221
- Metric: []*dto.Metric{
222
- {
223
- Label: []*dto.LabelPair{
224
- {
225
- Name: proto.String("constname"),
226
- Value: proto.String("constvalue"),
227
- },
228
- {
229
- Name: proto.String("labelname"),
230
- Value: proto.String("different_val"),
231
- },
232
- },
233
- Counter: &dto.Counter{
234
- Value: proto.Float64(42),
235
- },
236
- },
237
- },
238
- }
239
-
240
- expectedMetricFamilyMergedWithExternalAsProtoCompactText := []byte(`name:"name" help:"docstring" type:COUNTER metric:<label:<name:"constname" value:"constvalue" > label:<name:"labelname" value:"different_val" > counter:<value:42 > > metric:<label:<name:"constname" value:"constvalue" > label:<name:"labelname" value:"val1" > counter:<value:1 > > metric:<label:<name:"constname" value:"constvalue" > label:<name:"labelname" value:"val2" > counter:<value:1 > >
241
-`)
242
-
243
- type output struct {
244
- headers map[string]string
245
- body []byte
246
- }
247
-
248
- var scenarios = []struct {
249
- headers map[string]string
250
- out output
251
- collector Collector
252
- externalMF []*dto.MetricFamily
253
- }{
254
- { // 0
255
- headers: map[string]string{
256
- "Accept": "foo/bar;q=0.2, dings/bums;q=0.8",
257
- },
258
- out: output{
259
- headers: map[string]string{
260
- "Content-Type": `text/plain; version=0.0.4`,
261
- },
262
- body: []byte{},
263
- },
264
- },
265
- { // 1
266
- headers: map[string]string{
267
- "Accept": "foo/bar;q=0.2, application/quark;q=0.8",
268
- },
269
- out: output{
270
- headers: map[string]string{
271
- "Content-Type": `text/plain; version=0.0.4`,
272
- },
273
- body: []byte{},
274
- },
275
- },
276
- { // 2
277
- headers: map[string]string{
278
- "Accept": "foo/bar;q=0.2, application/vnd.google.protobuf;proto=io.prometheus.client.MetricFamily;encoding=bla;q=0.8",
279
- },
280
- out: output{
281
- headers: map[string]string{
282
- "Content-Type": `text/plain; version=0.0.4`,
283
- },
284
- body: []byte{},
285
- },
286
- },
287
- { // 3
288
- headers: map[string]string{
289
- "Accept": "text/plain;q=0.2, application/vnd.google.protobuf;proto=io.prometheus.client.MetricFamily;encoding=delimited;q=0.8",
290
- },
291
- out: output{
292
- headers: map[string]string{
293
- "Content-Type": `application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily; encoding=delimited`,
294
- },
295
- body: []byte{},
296
- },
297
- },
298
- { // 4
299
- headers: map[string]string{
300
- "Accept": "application/json",
301
- },
302
- out: output{
303
- headers: map[string]string{
304
- "Content-Type": `text/plain; version=0.0.4`,
305
- },
306
- body: expectedMetricFamilyAsText,
307
- },
308
- collector: metricVec,
309
- },
310
- { // 5
311
- headers: map[string]string{
312
- "Accept": "application/vnd.google.protobuf;proto=io.prometheus.client.MetricFamily;encoding=delimited",
313
- },
314
- out: output{
315
- headers: map[string]string{
316
- "Content-Type": `application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily; encoding=delimited`,
317
- },
318
- body: expectedMetricFamilyAsBytes,
319
- },
320
- collector: metricVec,
321
- },
322
- { // 6
323
- headers: map[string]string{
324
- "Accept": "application/json",
325
- },
326
- out: output{
327
- headers: map[string]string{
328
- "Content-Type": `text/plain; version=0.0.4`,
329
- },
330
- body: externalMetricFamilyAsText,
331
- },
332
- externalMF: []*dto.MetricFamily{externalMetricFamily},
333
- },
334
- { // 7
335
- headers: map[string]string{
336
- "Accept": "application/vnd.google.protobuf;proto=io.prometheus.client.MetricFamily;encoding=delimited",
337
- },
338
- out: output{
339
- headers: map[string]string{
340
- "Content-Type": `application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily; encoding=delimited`,
341
- },
342
- body: externalMetricFamilyAsBytes,
343
- },
344
- externalMF: []*dto.MetricFamily{externalMetricFamily},
345
- },
346
- { // 8
347
- headers: map[string]string{
348
- "Accept": "application/vnd.google.protobuf;proto=io.prometheus.client.MetricFamily;encoding=delimited",
349
- },
350
- out: output{
351
- headers: map[string]string{
352
- "Content-Type": `application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily; encoding=delimited`,
353
- },
354
- body: bytes.Join(
355
- [][]byte{
356
- externalMetricFamilyAsBytes,
357
- expectedMetricFamilyAsBytes,
358
- },
359
- []byte{},
360
- ),
361
- },
362
- collector: metricVec,
363
- externalMF: []*dto.MetricFamily{externalMetricFamily},
364
- },
365
- { // 9
366
- headers: map[string]string{
367
- "Accept": "text/plain",
368
- },
369
- out: output{
370
- headers: map[string]string{
371
- "Content-Type": `text/plain; version=0.0.4`,
372
- },
373
- body: []byte{},
374
- },
375
- },
376
- { // 10
377
- headers: map[string]string{
378
- "Accept": "application/vnd.google.protobuf;proto=io.prometheus.client.MetricFamily;encoding=bla;q=0.2, text/plain;q=0.5",
379
- },
380
- out: output{
381
- headers: map[string]string{
382
- "Content-Type": `text/plain; version=0.0.4`,
383
- },
384
- body: expectedMetricFamilyAsText,
385
- },
386
- collector: metricVec,
387
- },
388
- { // 11
389
- headers: map[string]string{
390
- "Accept": "application/vnd.google.protobuf;proto=io.prometheus.client.MetricFamily;encoding=bla;q=0.2, text/plain;q=0.5;version=0.0.4",
391
- },
392
- out: output{
393
- headers: map[string]string{
394
- "Content-Type": `text/plain; version=0.0.4`,
395
- },
396
- body: bytes.Join(
397
- [][]byte{
398
- externalMetricFamilyAsText,
399
- expectedMetricFamilyAsText,
400
- },
401
- []byte{},
402
- ),
403
- },
404
- collector: metricVec,
405
- externalMF: []*dto.MetricFamily{externalMetricFamily},
406
- },
407
- { // 12
408
- headers: map[string]string{
409
- "Accept": "application/vnd.google.protobuf;proto=io.prometheus.client.MetricFamily;encoding=delimited;q=0.2, text/plain;q=0.5;version=0.0.2",
410
- },
411
- out: output{
412
- headers: map[string]string{
413
- "Content-Type": `application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily; encoding=delimited`,
414
- },
415
- body: bytes.Join(
416
- [][]byte{
417
- externalMetricFamilyAsBytes,
418
- expectedMetricFamilyAsBytes,
419
- },
420
- []byte{},
421
- ),
422
- },
423
- collector: metricVec,
424
- externalMF: []*dto.MetricFamily{externalMetricFamily},
425
- },
426
- { // 13
427
- headers: map[string]string{
428
- "Accept": "application/vnd.google.protobuf;proto=io.prometheus.client.MetricFamily;encoding=text;q=0.5, application/vnd.google.protobuf;proto=io.prometheus.client.MetricFamily;encoding=delimited;q=0.4",
429
- },
430
- out: output{
431
- headers: map[string]string{
432
- "Content-Type": `application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily; encoding=text`,
433
- },
434
- body: bytes.Join(
435
- [][]byte{
436
- externalMetricFamilyAsProtoText,
437
- expectedMetricFamilyAsProtoText,
438
- },
439
- []byte{},
440
- ),
441
- },
442
- collector: metricVec,
443
- externalMF: []*dto.MetricFamily{externalMetricFamily},
444
- },
445
- { // 14
446
- headers: map[string]string{
447
- "Accept": "application/vnd.google.protobuf;proto=io.prometheus.client.MetricFamily;encoding=compact-text",
448
- },
449
- out: output{
450
- headers: map[string]string{
451
- "Content-Type": `application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily; encoding=compact-text`,
452
- },
453
- body: bytes.Join(
454
- [][]byte{
455
- externalMetricFamilyAsProtoCompactText,
456
- expectedMetricFamilyAsProtoCompactText,
457
- },
458
- []byte{},
459
- ),
460
- },
461
- collector: metricVec,
462
- externalMF: []*dto.MetricFamily{externalMetricFamily},
463
- },
464
- { // 15
465
- headers: map[string]string{
466
- "Accept": "application/vnd.google.protobuf;proto=io.prometheus.client.MetricFamily;encoding=compact-text",
467
- },
468
- out: output{
469
- headers: map[string]string{
470
- "Content-Type": `application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily; encoding=compact-text`,
471
- },
472
- body: bytes.Join(
473
- [][]byte{
474
- externalMetricFamilyAsProtoCompactText,
475
- expectedMetricFamilyMergedWithExternalAsProtoCompactText,
476
- },
477
- []byte{},
478
- ),
479
- },
480
- collector: metricVec,
481
- externalMF: []*dto.MetricFamily{
482
- externalMetricFamily,
483
- externalMetricFamilyWithSameName,
484
- },
485
- },
486
- }
487
- for i, scenario := range scenarios {
488
- registry := newRegistry()
489
- registry.collectChecksEnabled = true
490
-
491
- if scenario.collector != nil {
492
- registry.Register(scenario.collector)
493
- }
494
- if scenario.externalMF != nil {
495
- registry.metricFamilyInjectionHook = func() []*dto.MetricFamily {
496
- return scenario.externalMF
497
- }
498
- }
499
- writer := &fakeResponseWriter{
500
- header: http.Header{},
501
- }
502
- handler := InstrumentHandler("prometheus", registry)
503
- request, _ := http.NewRequest("GET", "/", nil)
504
- for key, value := range scenario.headers {
505
- request.Header.Add(key, value)
506
- }
507
- handler(writer, request)
508
-
509
- for key, value := range scenario.out.headers {
510
- if writer.Header().Get(key) != value {
511
- t.Errorf(
512
- "%d. expected %q for header %q, got %q",
513
- i, value, key, writer.Header().Get(key),
514
- )
515
- }
516
- }
517
-
518
- if !bytes.Equal(scenario.out.body, writer.body.Bytes()) {
519
- t.Errorf(
520
- "%d. expected %q for body, got %q",
521
- i, scenario.out.body, writer.body.Bytes(),
522
- )
523
- }
524
- }
525
-}
526
-
527
-func TestHandler(t *testing.T) {
528
- testHandler(t)
529
-}
530
-
531
-func BenchmarkHandler(b *testing.B) {
532
- for i := 0; i < b.N; i++ {
533
- testHandler(b)
534
- }
535
-}
Godeps/_workspace/src/github.com/prometheus/client_golang/prometheus/summary.go
deleted
-536
@@ -1,536 +0,0 @@
1
-// Copyright 2014 The Prometheus Authors
2
-// Licensed under the Apache License, Version 2.0 (the "License");
3
-// you may not use this file except in compliance with the License.
4
-// You may obtain a copy of the License at
5
-//
6
-// http://www.apache.org/licenses/LICENSE-2.0
7
-//
8
-// Unless required by applicable law or agreed to in writing, software
9
-// distributed under the License is distributed on an "AS IS" BASIS,
10
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
-// See the License for the specific language governing permissions and
12
-// limitations under the License.
13
-
14
-package prometheus
15
-
16
-import (
17
- "fmt"
18
- "hash/fnv"
19
- "math"
20
- "sort"
21
- "sync"
22
- "time"
23
-
24
- "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/beorn7/perks/quantile"
25
- "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/golang/protobuf/proto"
26
-
27
- dto "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/prometheus/client_model/go"
28
-
29
- "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/prometheus/client_golang/model"
30
-)
31
-
32
-// A Summary captures individual observations from an event or sample stream and
33
-// summarizes them in a manner similar to traditional summary statistics: 1. sum
34
-// of observations, 2. observation count, 3. rank estimations.
35
-//
36
-// A typical use-case is the observation of request latencies. By default, a
37
-// Summary provides the median, the 90th and the 99th percentile of the latency
38
-// as rank estimations.
39
-//
40
-// Note that the rank estimations cannot be aggregated in a meaningful way with
41
-// the Prometheus query language (i.e. you cannot average or add them). If you
42
-// need aggregatable quantiles (e.g. you want the 99th percentile latency of all
43
-// queries served across all instances of a service), consider the Histogram
44
-// metric type. See the Prometheus documentation for more details.
45
-//
46
-// To create Summary instances, use NewSummary.
47
-type Summary interface {
48
- Metric
49
- Collector
50
-
51
- // Observe adds a single observation to the summary.
52
- Observe(float64)
53
-}
54
-
55
-var (
56
- // DefObjectives are the default Summary quantile values.
57
- DefObjectives = map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001}
58
-
59
- errQuantileLabelNotAllowed = fmt.Errorf(
60
- "%q is not allowed as label name in summaries", model.QuantileLabel,
61
- )
62
-)
63
-
64
-// Default values for SummaryOpts.
65
-const (
66
- // DefMaxAge is the default duration for which observations stay
67
- // relevant.
68
- DefMaxAge time.Duration = 10 * time.Minute
69
- // DefAgeBuckets is the default number of buckets used to calculate the
70
- // age of observations.
71
- DefAgeBuckets = 5
72
- // DefBufCap is the standard buffer size for collecting Summary observations.
73
- DefBufCap = 500
74
-)
75
-
76
-// SummaryOpts bundles the options for creating a Summary metric. It is
77
-// mandatory to set Name and Help to a non-empty string. All other fields are
78
-// optional and can safely be left at their zero value.
79
-type SummaryOpts struct {
80
- // Namespace, Subsystem, and Name are components of the fully-qualified
81
- // name of the Summary (created by joining these components with
82
- // "_"). Only Name is mandatory, the others merely help structuring the
83
- // name. Note that the fully-qualified name of the Summary must be a
84
- // valid Prometheus metric name.
85
- Namespace string
86
- Subsystem string
87
- Name string
88
-
89
- // Help provides information about this Summary. Mandatory!
90
- //
91
- // Metrics with the same fully-qualified name must have the same Help
92
- // string.
93
- Help string
94
-
95
- // ConstLabels are used to attach fixed labels to this
96
- // Summary. Summaries with the same fully-qualified name must have the
97
- // same label names in their ConstLabels.
98
- //
99
- // Note that in most cases, labels have a value that varies during the
100
- // lifetime of a process. Those labels are usually managed with a
101
- // SummaryVec. ConstLabels serve only special purposes. One is for the
102
- // special case where the value of a label does not change during the
103
- // lifetime of a process, e.g. if the revision of the running binary is
104
- // put into a label. Another, more advanced purpose is if more than one
105
- // Collector needs to collect Summaries with the same fully-qualified
106
- // name. In that case, those Summaries must differ in the values of
107
- // their ConstLabels. See the Collector examples.
108
- //
109
- // If the value of a label never changes (not even between binaries),
110
- // that label most likely should not be a label at all (but part of the
111
- // metric name).
112
- ConstLabels Labels
113
-
114
- // Objectives defines the quantile rank estimates with their respective
115
- // absolute error. The default value is DefObjectives.
116
- Objectives map[float64]float64
117
-
118
- // MaxAge defines the duration for which an observation stays relevant
119
- // for the summary. Must be positive. The default value is DefMaxAge.
120
- MaxAge time.Duration
121
-
122
- // AgeBuckets is the number of buckets used to exclude observations that
123
- // are older than MaxAge from the summary. A higher number has a
124
- // resource penalty, so only increase it if the higher resolution is
125
- // really required. For very high observation rates, you might want to
126
- // reduce the number of age buckets. With only one age bucket, you will
127
- // effectively see a complete reset of the summary each time MaxAge has
128
- // passed. The default value is DefAgeBuckets.
129
- AgeBuckets uint32
130
-
131
- // BufCap defines the default sample stream buffer size. The default
132
- // value of DefBufCap should suffice for most uses. If there is a need
133
- // to increase the value, a multiple of 500 is recommended (because that
134
- // is the internal buffer size of the underlying package
135
- // "github.com/bmizerany/perks/quantile").
136
- BufCap uint32
137
-}
138
-
139
-// TODO: Great fuck-up with the sliding-window decay algorithm... The Merge
140
-// method of perk/quantile is actually not working as advertised - and it might
141
-// be unfixable, as the underlying algorithm is apparently not capable of
142
-// merging summaries in the first place. To avoid using Merge, we are currently
143
-// adding observations to _each_ age bucket, i.e. the effort to add a sample is
144
-// essentially multiplied by the number of age buckets. When rotating age
145
-// buckets, we empty the previous head stream. On scrape time, we simply take
146
-// the quantiles from the head stream (no merging required). Result: More effort
147
-// on observation time, less effort on scrape time, which is exactly the
148
-// opposite of what we try to accomplish, but at least the results are correct.
149
-//
150
-// The quite elegant previous contraption to merge the age buckets efficiently
151
-// on scrape time (see code up commit 6b9530d72ea715f0ba612c0120e6e09fbf1d49d0)
152
-// can't be used anymore.
153
-
154
-// NewSummary creates a new Summary based on the provided SummaryOpts.
155
-func NewSummary(opts SummaryOpts) Summary {
156
- return newSummary(
157
- NewDesc(
158
- BuildFQName(opts.Namespace, opts.Subsystem, opts.Name),
159
- opts.Help,
160
- nil,
161
- opts.ConstLabels,
162
- ),
163
- opts,
164
- )
165
-}
166
-
167
-func newSummary(desc *Desc, opts SummaryOpts, labelValues ...string) Summary {
168
- if len(desc.variableLabels) != len(labelValues) {
169
- panic(errInconsistentCardinality)
170
- }
171
-
172
- for _, n := range desc.variableLabels {
173
- if n == model.QuantileLabel {
174
- panic(errQuantileLabelNotAllowed)
175
- }
176
- }
177
- for _, lp := range desc.constLabelPairs {
178
- if lp.GetName() == model.QuantileLabel {
179
- panic(errQuantileLabelNotAllowed)
180
- }
181
- }
182
-
183
- if len(opts.Objectives) == 0 {
184
- opts.Objectives = DefObjectives
185
- }
186
-
187
- if opts.MaxAge < 0 {
188
- panic(fmt.Errorf("illegal max age MaxAge=%v", opts.MaxAge))
189
- }
190
- if opts.MaxAge == 0 {
191
- opts.MaxAge = DefMaxAge
192
- }
193
-
194
- if opts.AgeBuckets == 0 {
195
- opts.AgeBuckets = DefAgeBuckets
196
- }
197
-
198
- if opts.BufCap == 0 {
199
- opts.BufCap = DefBufCap
200
- }
201
-
202
- s := &summary{
203
- desc: desc,
204
-
205
- objectives: opts.Objectives,
206
- sortedObjectives: make([]float64, 0, len(opts.Objectives)),
207
-
208
- labelPairs: makeLabelPairs(desc, labelValues),
209
-
210
- hotBuf: make([]float64, 0, opts.BufCap),
211
- coldBuf: make([]float64, 0, opts.BufCap),
212
- streamDuration: opts.MaxAge / time.Duration(opts.AgeBuckets),
213
- }
214
- s.headStreamExpTime = time.Now().Add(s.streamDuration)
215
- s.hotBufExpTime = s.headStreamExpTime
216
-
217
- for i := uint32(0); i < opts.AgeBuckets; i++ {
218
- s.streams = append(s.streams, s.newStream())
219
- }
220
- s.headStream = s.streams[0]
221
-
222
- for qu := range s.objectives {
223
- s.sortedObjectives = append(s.sortedObjectives, qu)
224
- }
225
- sort.Float64s(s.sortedObjectives)
226
-
227
- s.Init(s) // Init self-collection.
228
- return s
229
-}
230
-
231
-type summary struct {
232
- SelfCollector
233
-
234
- bufMtx sync.Mutex // Protects hotBuf and hotBufExpTime.
235
- mtx sync.Mutex // Protects every other moving part.
236
- // Lock bufMtx before mtx if both are needed.
237
-
238
- desc *Desc
239
-
240
- objectives map[float64]float64
241
- sortedObjectives []float64
242
-
243
- labelPairs []*dto.LabelPair
244
-
245
- sum float64
246
- cnt uint64
247
-
248
- hotBuf, coldBuf []float64
249
-
250
- streams []*quantile.Stream
251
- streamDuration time.Duration
252
- headStream *quantile.Stream
253
- headStreamIdx int
254
- headStreamExpTime, hotBufExpTime time.Time
255
-}
256
-
257
-func (s *summary) Desc() *Desc {
258
- return s.desc
259
-}
260
-
261
-func (s *summary) Observe(v float64) {
262
- s.bufMtx.Lock()
263
- defer s.bufMtx.Unlock()
264
-
265
- now := time.Now()
266
- if now.After(s.hotBufExpTime) {
267
- s.asyncFlush(now)
268
- }
269
- s.hotBuf = append(s.hotBuf, v)
270
- if len(s.hotBuf) == cap(s.hotBuf) {
271
- s.asyncFlush(now)
272
- }
273
-}
274
-
275
-func (s *summary) Write(out *dto.Metric) error {
276
- sum := &dto.Summary{}
277
- qs := make([]*dto.Quantile, 0, len(s.objectives))
278
-
279
- s.bufMtx.Lock()
280
- s.mtx.Lock()
281
- // Swap bufs even if hotBuf is empty to set new hotBufExpTime.
282
- s.swapBufs(time.Now())
283
- s.bufMtx.Unlock()
284
-
285
- s.flushColdBuf()
286
- sum.SampleCount = proto.Uint64(s.cnt)
287
- sum.SampleSum = proto.Float64(s.sum)
288
-
289
- for _, rank := range s.sortedObjectives {
290
- var q float64
291
- if s.headStream.Count() == 0 {
292
- q = math.NaN()
293
- } else {
294
- q = s.headStream.Query(rank)
295
- }
296
- qs = append(qs, &dto.Quantile{
297
- Quantile: proto.Float64(rank),
298
- Value: proto.Float64(q),
299
- })
300
- }
301
-
302
- s.mtx.Unlock()
303
-
304
- if len(qs) > 0 {
305
- sort.Sort(quantSort(qs))
306
- }
307
- sum.Quantile = qs
308
-
309
- out.Summary = sum
310
- out.Label = s.labelPairs
311
- return nil
312
-}
313
-
314
-func (s *summary) newStream() *quantile.Stream {
315
- return quantile.NewTargeted(s.objectives)
316
-}
317
-
318
-// asyncFlush needs bufMtx locked.
319
-func (s *summary) asyncFlush(now time.Time) {
320
- s.mtx.Lock()
321
- s.swapBufs(now)
322
-
323
- // Unblock the original goroutine that was responsible for the mutation
324
- // that triggered the compaction. But hold onto the global non-buffer
325
- // state mutex until the operation finishes.
326
- go func() {
327
- s.flushColdBuf()
328
- s.mtx.Unlock()
329
- }()
330
-}
331
-
332
-// rotateStreams needs mtx AND bufMtx locked.
333
-func (s *summary) maybeRotateStreams() {
334
- for !s.hotBufExpTime.Equal(s.headStreamExpTime) {
335
- s.headStream.Reset()
336
- s.headStreamIdx++
337
- if s.headStreamIdx >= len(s.streams) {
338
- s.headStreamIdx = 0
339
- }
340
- s.headStream = s.streams[s.headStreamIdx]
341
- s.headStreamExpTime = s.headStreamExpTime.Add(s.streamDuration)
342
- }
343
-}
344
-
345
-// flushColdBuf needs mtx locked.
346
-func (s *summary) flushColdBuf() {
347
- for _, v := range s.coldBuf {
348
- for _, stream := range s.streams {
349
- stream.Insert(v)
350
- }
351
- s.cnt++
352
- s.sum += v
353
- }
354
- s.coldBuf = s.coldBuf[0:0]
355
- s.maybeRotateStreams()
356
-}
357
-
358
-// swapBufs needs mtx AND bufMtx locked, coldBuf must be empty.
359
-func (s *summary) swapBufs(now time.Time) {
360
- if len(s.coldBuf) != 0 {
361
- panic("coldBuf is not empty")
362
- }
363
- s.hotBuf, s.coldBuf = s.coldBuf, s.hotBuf
364
- // hotBuf is now empty and gets new expiration set.
365
- for now.After(s.hotBufExpTime) {
366
- s.hotBufExpTime = s.hotBufExpTime.Add(s.streamDuration)
367
- }
368
-}
369
-
370
-type quantSort []*dto.Quantile
371
-
372
-func (s quantSort) Len() int {
373
- return len(s)
374
-}
375
-
376
-func (s quantSort) Swap(i, j int) {
377
- s[i], s[j] = s[j], s[i]
378
-}
379
-
380
-func (s quantSort) Less(i, j int) bool {
381
- return s[i].GetQuantile() < s[j].GetQuantile()
382
-}
383
-
384
-// SummaryVec is a Collector that bundles a set of Summaries that all share the
385
-// same Desc, but have different values for their variable labels. This is used
386
-// if you want to count the same thing partitioned by various dimensions
387
-// (e.g. HTTP request latencies, partitioned by status code and method). Create
388
-// instances with NewSummaryVec.
389
-type SummaryVec struct {
390
- MetricVec
391
-}
392
-
393
-// NewSummaryVec creates a new SummaryVec based on the provided SummaryOpts and
394
-// partitioned by the given label names. At least one label name must be
395
-// provided.
396
-func NewSummaryVec(opts SummaryOpts, labelNames []string) *SummaryVec {
397
- desc := NewDesc(
398
- BuildFQName(opts.Namespace, opts.Subsystem, opts.Name),
399
- opts.Help,
400
- labelNames,
401
- opts.ConstLabels,
402
- )
403
- return &SummaryVec{
404
- MetricVec: MetricVec{
405
- children: map[uint64]Metric{},
406
- desc: desc,
407
- hash: fnv.New64a(),
408
- newMetric: func(lvs ...string) Metric {
409
- return newSummary(desc, opts, lvs...)
410
- },
411
- },
412
- }
413
-}
414
-
415
-// GetMetricWithLabelValues replaces the method of the same name in
416
-// MetricVec. The difference is that this method returns a Summary and not a
417
-// Metric so that no type conversion is required.
418
-func (m *SummaryVec) GetMetricWithLabelValues(lvs ...string) (Summary, error) {
419
- metric, err := m.MetricVec.GetMetricWithLabelValues(lvs...)
420
- if metric != nil {
421
- return metric.(Summary), err
422
- }
423
- return nil, err
424
-}
425
-
426
-// GetMetricWith replaces the method of the same name in MetricVec. The
427
-// difference is that this method returns a Summary and not a Metric so that no
428
-// type conversion is required.
429
-func (m *SummaryVec) GetMetricWith(labels Labels) (Summary, error) {
430
- metric, err := m.MetricVec.GetMetricWith(labels)
431
- if metric != nil {
432
- return metric.(Summary), err
433
- }
434
- return nil, err
435
-}
436
-
437
-// WithLabelValues works as GetMetricWithLabelValues, but panics where
438
-// GetMetricWithLabelValues would have returned an error. By not returning an
439
-// error, WithLabelValues allows shortcuts like
440
-// myVec.WithLabelValues("404", "GET").Observe(42.21)
441
-func (m *SummaryVec) WithLabelValues(lvs ...string) Summary {
442
- return m.MetricVec.WithLabelValues(lvs...).(Summary)
443
-}
444
-
445
-// With works as GetMetricWith, but panics where GetMetricWithLabels would have
446
-// returned an error. By not returning an error, With allows shortcuts like
447
-// myVec.With(Labels{"code": "404", "method": "GET"}).Observe(42.21)
448
-func (m *SummaryVec) With(labels Labels) Summary {
449
- return m.MetricVec.With(labels).(Summary)
450
-}
451
-
452
-type constSummary struct {
453
- desc *Desc
454
- count uint64
455
- sum float64
456
- quantiles map[float64]float64
457
- labelPairs []*dto.LabelPair
458
-}
459
-
460
-func (s *constSummary) Desc() *Desc {
461
- return s.desc
462
-}
463
-
464
-func (s *constSummary) Write(out *dto.Metric) error {
465
- sum := &dto.Summary{}
466
- qs := make([]*dto.Quantile, 0, len(s.quantiles))
467
-
468
- sum.SampleCount = proto.Uint64(s.count)
469
- sum.SampleSum = proto.Float64(s.sum)
470
-
471
- for rank, q := range s.quantiles {
472
- qs = append(qs, &dto.Quantile{
473
- Quantile: proto.Float64(rank),
474
- Value: proto.Float64(q),
475
- })
476
- }
477
-
478
- if len(qs) > 0 {
479
- sort.Sort(quantSort(qs))
480
- }
481
- sum.Quantile = qs
482
-
483
- out.Summary = sum
484
- out.Label = s.labelPairs
485
-
486
- return nil
487
-}
488
-
489
-// NewConstSummary returns a metric representing a Prometheus summary with fixed
490
-// values for the count, sum, and quantiles. As those parameters cannot be
491
-// changed, the returned value does not implement the Summary interface (but
492
-// only the Metric interface). Users of this package will not have much use for
493
-// it in regular operations. However, when implementing custom Collectors, it is
494
-// useful as a throw-away metric that is generated on the fly to send it to
495
-// Prometheus in the Collect method.
496
-//
497
-// quantiles maps ranks to quantile values. For example, a median latency of
498
-// 0.23s and a 99th percentile latency of 0.56s would be expressed as:
499
-// map[float64]float64{0.5: 0.23, 0.99: 0.56}
500
-//
501
-// NewConstSummary returns an error if the length of labelValues is not
502
-// consistent with the variable labels in Desc.
503
-func NewConstSummary(
504
- desc *Desc,
505
- count uint64,
506
- sum float64,
507
- quantiles map[float64]float64,
508
- labelValues ...string,
509
-) (Metric, error) {
510
- if len(desc.variableLabels) != len(labelValues) {
511
- return nil, errInconsistentCardinality
512
- }
513
- return &constSummary{
514
- desc: desc,
515
- count: count,
516
- sum: sum,
517
- quantiles: quantiles,
518
- labelPairs: makeLabelPairs(desc, labelValues),
519
- }, nil
520
-}
521
-
522
-// MustNewConstSummary is a version of NewConstSummary that panics where
523
-// NewConstMetric would have returned an error.
524
-func MustNewConstSummary(
525
- desc *Desc,
526
- count uint64,
527
- sum float64,
528
- quantiles map[float64]float64,
529
- labelValues ...string,
530
-) Metric {
531
- m, err := NewConstSummary(desc, count, sum, quantiles, labelValues...)
532
- if err != nil {
533
- panic(err)
534
- }
535
- return m
536
-}
Godeps/_workspace/src/github.com/prometheus/client_golang/prometheus/summary_test.go
deleted
-347
@@ -1,347 +0,0 @@
1
-// Copyright 2014 The Prometheus Authors
2
-// Licensed under the Apache License, Version 2.0 (the "License");
3
-// you may not use this file except in compliance with the License.
4
-// You may obtain a copy of the License at
5
-//
6
-// http://www.apache.org/licenses/LICENSE-2.0
7
-//
8
-// Unless required by applicable law or agreed to in writing, software
9
-// distributed under the License is distributed on an "AS IS" BASIS,
10
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
-// See the License for the specific language governing permissions and
12
-// limitations under the License.
13
-
14
-package prometheus
15
-
16
-import (
17
- "math"
18
- "math/rand"
19
- "sort"
20
- "sync"
21
- "testing"
22
- "testing/quick"
23
- "time"
24
-
25
- dto "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/prometheus/client_model/go"
26
-)
27
-
28
-func benchmarkSummaryObserve(w int, b *testing.B) {
29
- b.StopTimer()
30
-
31
- wg := new(sync.WaitGroup)
32
- wg.Add(w)
33
-
34
- g := new(sync.WaitGroup)
35
- g.Add(1)
36
-
37
- s := NewSummary(SummaryOpts{})
38
-
39
- for i := 0; i < w; i++ {
40
- go func() {
41
- g.Wait()
42
-
43
- for i := 0; i < b.N; i++ {
44
- s.Observe(float64(i))
45
- }
46
-
47
- wg.Done()
48
- }()
49
- }
50
-
51
- b.StartTimer()
52
- g.Done()
53
- wg.Wait()
54
-}
55
-
56
-func BenchmarkSummaryObserve1(b *testing.B) {
57
- benchmarkSummaryObserve(1, b)
58
-}
59
-
60
-func BenchmarkSummaryObserve2(b *testing.B) {
61
- benchmarkSummaryObserve(2, b)
62
-}
63
-
64
-func BenchmarkSummaryObserve4(b *testing.B) {
65
- benchmarkSummaryObserve(4, b)
66
-}
67
-
68
-func BenchmarkSummaryObserve8(b *testing.B) {
69
- benchmarkSummaryObserve(8, b)
70
-}
71
-
72
-func benchmarkSummaryWrite(w int, b *testing.B) {
73
- b.StopTimer()
74
-
75
- wg := new(sync.WaitGroup)
76
- wg.Add(w)
77
-
78
- g := new(sync.WaitGroup)
79
- g.Add(1)
80
-
81
- s := NewSummary(SummaryOpts{})
82
-
83
- for i := 0; i < 1000000; i++ {
84
- s.Observe(float64(i))
85
- }
86
-
87
- for j := 0; j < w; j++ {
88
- outs := make([]dto.Metric, b.N)
89
-
90
- go func(o []dto.Metric) {
91
- g.Wait()
92
-
93
- for i := 0; i < b.N; i++ {
94
- s.Write(&o[i])
95
- }
96
-
97
- wg.Done()
98
- }(outs)
99
- }
100
-
101
- b.StartTimer()
102
- g.Done()
103
- wg.Wait()
104
-}
105
-
106
-func BenchmarkSummaryWrite1(b *testing.B) {
107
- benchmarkSummaryWrite(1, b)
108
-}
109
-
110
-func BenchmarkSummaryWrite2(b *testing.B) {
111
- benchmarkSummaryWrite(2, b)
112
-}
113
-
114
-func BenchmarkSummaryWrite4(b *testing.B) {
115
- benchmarkSummaryWrite(4, b)
116
-}
117
-
118
-func BenchmarkSummaryWrite8(b *testing.B) {
119
- benchmarkSummaryWrite(8, b)
120
-}
121
-
122
-func TestSummaryConcurrency(t *testing.T) {
123
- if testing.Short() {
124
- t.Skip("Skipping test in short mode.")
125
- }
126
-
127
- rand.Seed(42)
128
-
129
- it := func(n uint32) bool {
130
- mutations := int(n%1e4 + 1e4)
131
- concLevel := int(n%5 + 1)
132
- total := mutations * concLevel
133
-
134
- var start, end sync.WaitGroup
135
- start.Add(1)
136
- end.Add(concLevel)
137
-
138
- sum := NewSummary(SummaryOpts{
139
- Name: "test_summary",
140
- Help: "helpless",
141
- })
142
-
143
- allVars := make([]float64, total)
144
- var sampleSum float64
145
- for i := 0; i < concLevel; i++ {
146
- vals := make([]float64, mutations)
147
- for j := 0; j < mutations; j++ {
148
- v := rand.NormFloat64()
149
- vals[j] = v
150
- allVars[i*mutations+j] = v
151
- sampleSum += v
152
- }
153
-
154
- go func(vals []float64) {
155
- start.Wait()
156
- for _, v := range vals {
157
- sum.Observe(v)
158
- }
159
- end.Done()
160
- }(vals)
161
- }
162
- sort.Float64s(allVars)
163
- start.Done()
164
- end.Wait()
165
-
166
- m := &dto.Metric{}
167
- sum.Write(m)
168
- if got, want := int(*m.Summary.SampleCount), total; got != want {
169
- t.Errorf("got sample count %d, want %d", got, want)
170
- }
171
- if got, want := *m.Summary.SampleSum, sampleSum; math.Abs((got-want)/want) > 0.001 {
172
- t.Errorf("got sample sum %f, want %f", got, want)
173
- }
174
-
175
- objectives := make([]float64, 0, len(DefObjectives))
176
- for qu := range DefObjectives {
177
- objectives = append(objectives, qu)
178
- }
179
- sort.Float64s(objectives)
180
-
181
- for i, wantQ := range objectives {
182
- ε := DefObjectives[wantQ]
183
- gotQ := *m.Summary.Quantile[i].Quantile
184
- gotV := *m.Summary.Quantile[i].Value
185
- min, max := getBounds(allVars, wantQ, ε)
186
- if gotQ != wantQ {
187
- t.Errorf("got quantile %f, want %f", gotQ, wantQ)
188
- }
189
- if gotV < min || gotV > max {
190
- t.Errorf("got %f for quantile %f, want [%f,%f]", gotV, gotQ, min, max)
191
- }
192
- }
193
- return true
194
- }
195
-
196
- if err := quick.Check(it, nil); err != nil {
197
- t.Error(err)
198
- }
199
-}
200
-
201
-func TestSummaryVecConcurrency(t *testing.T) {
202
- if testing.Short() {
203
- t.Skip("Skipping test in short mode.")
204
- }
205
-
206
- rand.Seed(42)
207
-
208
- objectives := make([]float64, 0, len(DefObjectives))
209
- for qu := range DefObjectives {
210
-
211
- objectives = append(objectives, qu)
212
- }
213
- sort.Float64s(objectives)
214
-
215
- it := func(n uint32) bool {
216
- mutations := int(n%1e4 + 1e4)
217
- concLevel := int(n%7 + 1)
218
- vecLength := int(n%3 + 1)
219
-
220
- var start, end sync.WaitGroup
221
- start.Add(1)
222
- end.Add(concLevel)
223
-
224
- sum := NewSummaryVec(
225
- SummaryOpts{
226
- Name: "test_summary",
227
- Help: "helpless",
228
- },
229
- []string{"label"},
230
- )
231
-
232
- allVars := make([][]float64, vecLength)
233
- sampleSums := make([]float64, vecLength)
234
- for i := 0; i < concLevel; i++ {
235
- vals := make([]float64, mutations)
236
- picks := make([]int, mutations)
237
- for j := 0; j < mutations; j++ {
238
- v := rand.NormFloat64()
239
- vals[j] = v
240
- pick := rand.Intn(vecLength)
241
- picks[j] = pick
242
- allVars[pick] = append(allVars[pick], v)
243
- sampleSums[pick] += v
244
- }
245
-
246
- go func(vals []float64) {
247
- start.Wait()
248
- for i, v := range vals {
249
- sum.WithLabelValues(string('A' + picks[i])).Observe(v)
250
- }
251
- end.Done()
252
- }(vals)
253
- }
254
- for _, vars := range allVars {
255
- sort.Float64s(vars)
256
- }
257
- start.Done()
258
- end.Wait()
259
-
260
- for i := 0; i < vecLength; i++ {
261
- m := &dto.Metric{}
262
- s := sum.WithLabelValues(string('A' + i))
263
- s.Write(m)
264
- if got, want := int(*m.Summary.SampleCount), len(allVars[i]); got != want {
265
- t.Errorf("got sample count %d for label %c, want %d", got, 'A'+i, want)
266
- }
267
- if got, want := *m.Summary.SampleSum, sampleSums[i]; math.Abs((got-want)/want) > 0.001 {
268
- t.Errorf("got sample sum %f for label %c, want %f", got, 'A'+i, want)
269
- }
270
- for j, wantQ := range objectives {
271
- ε := DefObjectives[wantQ]
272
- gotQ := *m.Summary.Quantile[j].Quantile
273
- gotV := *m.Summary.Quantile[j].Value
274
- min, max := getBounds(allVars[i], wantQ, ε)
275
- if gotQ != wantQ {
276
- t.Errorf("got quantile %f for label %c, want %f", gotQ, 'A'+i, wantQ)
277
- }
278
- if gotV < min || gotV > max {
279
- t.Errorf("got %f for quantile %f for label %c, want [%f,%f]", gotV, gotQ, 'A'+i, min, max)
280
- }
281
- }
282
- }
283
- return true
284
- }
285
-
286
- if err := quick.Check(it, nil); err != nil {
287
- t.Error(err)
288
- }
289
-}
290
-
291
-func TestSummaryDecay(t *testing.T) {
292
- if testing.Short() {
293
- t.Skip("Skipping test in short mode.")
294
- // More because it depends on timing than because it is particularly long...
295
- }
296
-
297
- sum := NewSummary(SummaryOpts{
298
- Name: "test_summary",
299
- Help: "helpless",
300
- MaxAge: 100 * time.Millisecond,
301
- Objectives: map[float64]float64{0.1: 0.001},
302
- AgeBuckets: 10,
303
- })
304
-
305
- m := &dto.Metric{}
306
- i := 0
307
- tick := time.NewTicker(time.Millisecond)
308
- for _ = range tick.C {
309
- i++
310
- sum.Observe(float64(i))
311
- if i%10 == 0 {
312
- sum.Write(m)
313
- if got, want := *m.Summary.Quantile[0].Value, math.Max(float64(i)/10, float64(i-90)); math.Abs(got-want) > 20 {
314
- t.Errorf("%d. got %f, want %f", i, got, want)
315
- }
316
- m.Reset()
317
- }
318
- if i >= 1000 {
319
- break
320
- }
321
- }
322
- tick.Stop()
323
- // Wait for MaxAge without observations and make sure quantiles are NaN.
324
- time.Sleep(100 * time.Millisecond)
325
- sum.Write(m)
326
- if got := *m.Summary.Quantile[0].Value; !math.IsNaN(got) {
327
- t.Errorf("got %f, want NaN after expiration", got)
328
- }
329
-}
330
-
331
-func getBounds(vars []float64, q, ε float64) (min, max float64) {
332
- // TODO: This currently tolerates an error of up to 2*ε. The error must
333
- // be at most ε, but for some reason, it's sometimes slightly
334
- // higher. That's a bug.
335
- n := float64(len(vars))
336
- lower := int((q - 2*ε) * n)
337
- upper := int(math.Ceil((q + 2*ε) * n))
338
- min = vars[0]
339
- if lower > 1 {
340
- min = vars[lower-1]
341
- }
342
- max = vars[len(vars)-1]
343
- if upper < len(vars) {
344
- max = vars[upper-1]
345
- }
346
- return
347
-}
Godeps/_workspace/src/github.com/prometheus/client_golang/prometheus/untyped.go
deleted
-145
@@ -1,145 +0,0 @@
1
-// Copyright 2014 The Prometheus Authors
2
-// Licensed under the Apache License, Version 2.0 (the "License");
3
-// you may not use this file except in compliance with the License.
4
-// You may obtain a copy of the License at
5
-//
6
-// http://www.apache.org/licenses/LICENSE-2.0
7
-//
8
-// Unless required by applicable law or agreed to in writing, software
9
-// distributed under the License is distributed on an "AS IS" BASIS,
10
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
-// See the License for the specific language governing permissions and
12
-// limitations under the License.
13
-
14
-package prometheus
15
-
16
-import "hash/fnv"
17
-
18
-// Untyped is a Metric that represents a single numerical value that can
19
-// arbitrarily go up and down.
20
-//
21
-// An Untyped metric works the same as a Gauge. The only difference is that to
22
-// no type information is implied.
23
-//
24
-// To create Untyped instances, use NewUntyped.
25
-type Untyped interface {
26
- Metric
27
- Collector
28
-
29
- // Set sets the Untyped metric to an arbitrary value.
30
- Set(float64)
31
- // Inc increments the Untyped metric by 1.
32
- Inc()
33
- // Dec decrements the Untyped metric by 1.
34
- Dec()
35
- // Add adds the given value to the Untyped metric. (The value can be
36
- // negative, resulting in a decrease.)
37
- Add(float64)
38
- // Sub subtracts the given value from the Untyped metric. (The value can
39
- // be negative, resulting in an increase.)
40
- Sub(float64)
41
-}
42
-
43
-// UntypedOpts is an alias for Opts. See there for doc comments.
44
-type UntypedOpts Opts
45
-
46
-// NewUntyped creates a new Untyped metric from the provided UntypedOpts.
47
-func NewUntyped(opts UntypedOpts) Untyped {
48
- return newValue(NewDesc(
49
- BuildFQName(opts.Namespace, opts.Subsystem, opts.Name),
50
- opts.Help,
51
- nil,
52
- opts.ConstLabels,
53
- ), UntypedValue, 0)
54
-}
55
-
56
-// UntypedVec is a Collector that bundles a set of Untyped metrics that all
57
-// share the same Desc, but have different values for their variable
58
-// labels. This is used if you want to count the same thing partitioned by
59
-// various dimensions. Create instances with NewUntypedVec.
60
-type UntypedVec struct {
61
- MetricVec
62
-}
63
-
64
-// NewUntypedVec creates a new UntypedVec based on the provided UntypedOpts and
65
-// partitioned by the given label names. At least one label name must be
66
-// provided.
67
-func NewUntypedVec(opts UntypedOpts, labelNames []string) *UntypedVec {
68
- desc := NewDesc(
69
- BuildFQName(opts.Namespace, opts.Subsystem, opts.Name),
70
- opts.Help,
71
- labelNames,
72
- opts.ConstLabels,
73
- )
74
- return &UntypedVec{
75
- MetricVec: MetricVec{
76
- children: map[uint64]Metric{},
77
- desc: desc,
78
- hash: fnv.New64a(),
79
- newMetric: func(lvs ...string) Metric {
80
- return newValue(desc, UntypedValue, 0, lvs...)
81
- },
82
- },
83
- }
84
-}
85
-
86
-// GetMetricWithLabelValues replaces the method of the same name in
87
-// MetricVec. The difference is that this method returns an Untyped and not a
88
-// Metric so that no type conversion is required.
89
-func (m *UntypedVec) GetMetricWithLabelValues(lvs ...string) (Untyped, error) {
90
- metric, err := m.MetricVec.GetMetricWithLabelValues(lvs...)
91
- if metric != nil {
92
- return metric.(Untyped), err
93
- }
94
- return nil, err
95
-}
96
-
97
-// GetMetricWith replaces the method of the same name in MetricVec. The
98
-// difference is that this method returns an Untyped and not a Metric so that no
99
-// type conversion is required.
100
-func (m *UntypedVec) GetMetricWith(labels Labels) (Untyped, error) {
101
- metric, err := m.MetricVec.GetMetricWith(labels)
102
- if metric != nil {
103
- return metric.(Untyped), err
104
- }
105
- return nil, err
106
-}
107
-
108
-// WithLabelValues works as GetMetricWithLabelValues, but panics where
109
-// GetMetricWithLabelValues would have returned an error. By not returning an
110
-// error, WithLabelValues allows shortcuts like
111
-// myVec.WithLabelValues("404", "GET").Add(42)
112
-func (m *UntypedVec) WithLabelValues(lvs ...string) Untyped {
113
- return m.MetricVec.WithLabelValues(lvs...).(Untyped)
114
-}
115
-
116
-// With works as GetMetricWith, but panics where GetMetricWithLabels would have
117
-// returned an error. By not returning an error, With allows shortcuts like
118
-// myVec.With(Labels{"code": "404", "method": "GET"}).Add(42)
119
-func (m *UntypedVec) With(labels Labels) Untyped {
120
- return m.MetricVec.With(labels).(Untyped)
121
-}
122
-
123
-// UntypedFunc is an Untyped whose value is determined at collect time by
124
-// calling a provided function.
125
-//
126
-// To create UntypedFunc instances, use NewUntypedFunc.
127
-type UntypedFunc interface {
128
- Metric
129
- Collector
130
-}
131
-
132
-// NewUntypedFunc creates a new UntypedFunc based on the provided
133
-// UntypedOpts. The value reported is determined by calling the given function
134
-// from within the Write method. Take into account that metric collection may
135
-// happen concurrently. If that results in concurrent calls to Write, like in
136
-// the case where an UntypedFunc is directly registered with Prometheus, the
137
-// provided function must be concurrency-safe.
138
-func NewUntypedFunc(opts UntypedOpts, function func() float64) UntypedFunc {
139
- return newValueFunc(NewDesc(
140
- BuildFQName(opts.Namespace, opts.Subsystem, opts.Name),
141
- opts.Help,
142
- nil,
143
- opts.ConstLabels,
144
- ), UntypedValue, function)
145
-}
Godeps/_workspace/src/github.com/prometheus/client_golang/prometheus/value.go
deleted
-234
@@ -1,234 +0,0 @@
1
-// Copyright 2014 The Prometheus Authors
2
-// Licensed under the Apache License, Version 2.0 (the "License");
3
-// you may not use this file except in compliance with the License.
4
-// You may obtain a copy of the License at
5
-//
6
-// http://www.apache.org/licenses/LICENSE-2.0
7
-//
8
-// Unless required by applicable law or agreed to in writing, software
9
-// distributed under the License is distributed on an "AS IS" BASIS,
10
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
-// See the License for the specific language governing permissions and
12
-// limitations under the License.
13
-
14
-package prometheus
15
-
16
-import (
17
- "errors"
18
- "fmt"
19
- "math"
20
- "sort"
21
- "sync/atomic"
22
-
23
- dto "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/prometheus/client_model/go"
24
-
25
- "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/golang/protobuf/proto"
26
-)
27
-
28
-// ValueType is an enumeration of metric types that represent a simple value.
29
-type ValueType int
30
-
31
-// Possible values for the ValueType enum.
32
-const (
33
- _ ValueType = iota
34
- CounterValue
35
- GaugeValue
36
- UntypedValue
37
-)
38
-
39
-var errInconsistentCardinality = errors.New("inconsistent label cardinality")
40
-
41
-// value is a generic metric for simple values. It implements Metric, Collector,
42
-// Counter, Gauge, and Untyped. Its effective type is determined by
43
-// ValueType. This is a low-level building block used by the library to back the
44
-// implementations of Counter, Gauge, and Untyped.
45
-type value struct {
46
- // valBits containst the bits of the represented float64 value. It has
47
- // to go first in the struct to guarantee alignment for atomic
48
- // operations. http://golang.org/pkg/sync/atomic/#pkg-note-BUG
49
- valBits uint64
50
-
51
- SelfCollector
52
-
53
- desc *Desc
54
- valType ValueType
55
- labelPairs []*dto.LabelPair
56
-}
57
-
58
-// newValue returns a newly allocated value with the given Desc, ValueType,
59
-// sample value and label values. It panics if the number of label
60
-// values is different from the number of variable labels in Desc.
61
-func newValue(desc *Desc, valueType ValueType, val float64, labelValues ...string) *value {
62
- if len(labelValues) != len(desc.variableLabels) {
63
- panic(errInconsistentCardinality)
64
- }
65
- result := &value{
66
- desc: desc,
67
- valType: valueType,
68
- valBits: math.Float64bits(val),
69
- labelPairs: makeLabelPairs(desc, labelValues),
70
- }
71
- result.Init(result)
72
- return result
73
-}
74
-
75
-func (v *value) Desc() *Desc {
76
- return v.desc
77
-}
78
-
79
-func (v *value) Set(val float64) {
80
- atomic.StoreUint64(&v.valBits, math.Float64bits(val))
81
-}
82
-
83
-func (v *value) Inc() {
84
- v.Add(1)
85
-}
86
-
87
-func (v *value) Dec() {
88
- v.Add(-1)
89
-}
90
-
91
-func (v *value) Add(val float64) {
92
- for {
93
- oldBits := atomic.LoadUint64(&v.valBits)
94
- newBits := math.Float64bits(math.Float64frombits(oldBits) + val)
95
- if atomic.CompareAndSwapUint64(&v.valBits, oldBits, newBits) {
96
- return
97
- }
98
- }
99
-}
100
-
101
-func (v *value) Sub(val float64) {
102
- v.Add(val * -1)
103
-}
104
-
105
-func (v *value) Write(out *dto.Metric) error {
106
- val := math.Float64frombits(atomic.LoadUint64(&v.valBits))
107
- return populateMetric(v.valType, val, v.labelPairs, out)
108
-}
109
-
110
-// valueFunc is a generic metric for simple values retrieved on collect time
111
-// from a function. It implements Metric and Collector. Its effective type is
112
-// determined by ValueType. This is a low-level building block used by the
113
-// library to back the implementations of CounterFunc, GaugeFunc, and
114
-// UntypedFunc.
115
-type valueFunc struct {
116
- SelfCollector
117
-
118
- desc *Desc
119
- valType ValueType
120
- function func() float64
121
- labelPairs []*dto.LabelPair
122
-}
123
-
124
-// newValueFunc returns a newly allocated valueFunc with the given Desc and
125
-// ValueType. The value reported is determined by calling the given function
126
-// from within the Write method. Take into account that metric collection may
127
-// happen concurrently. If that results in concurrent calls to Write, like in
128
-// the case where a valueFunc is directly registered with Prometheus, the
129
-// provided function must be concurrency-safe.
130
-func newValueFunc(desc *Desc, valueType ValueType, function func() float64) *valueFunc {
131
- result := &valueFunc{
132
- desc: desc,
133
- valType: valueType,
134
- function: function,
135
- labelPairs: makeLabelPairs(desc, nil),
136
- }
137
- result.Init(result)
138
- return result
139
-}
140
-
141
-func (v *valueFunc) Desc() *Desc {
142
- return v.desc
143
-}
144
-
145
-func (v *valueFunc) Write(out *dto.Metric) error {
146
- return populateMetric(v.valType, v.function(), v.labelPairs, out)
147
-}
148
-
149
-// NewConstMetric returns a metric with one fixed value that cannot be
150
-// changed. Users of this package will not have much use for it in regular
151
-// operations. However, when implementing custom Collectors, it is useful as a
152
-// throw-away metric that is generated on the fly to send it to Prometheus in
153
-// the Collect method. NewConstMetric returns an error if the length of
154
-// labelValues is not consistent with the variable labels in Desc.
155
-func NewConstMetric(desc *Desc, valueType ValueType, value float64, labelValues ...string) (Metric, error) {
156
- if len(desc.variableLabels) != len(labelValues) {
157
- return nil, errInconsistentCardinality
158
- }
159
- return &constMetric{
160
- desc: desc,
161
- valType: valueType,
162
- val: value,
163
- labelPairs: makeLabelPairs(desc, labelValues),
164
- }, nil
165
-}
166
-
167
-// MustNewConstMetric is a version of NewConstMetric that panics where
168
-// NewConstMetric would have returned an error.
169
-func MustNewConstMetric(desc *Desc, valueType ValueType, value float64, labelValues ...string) Metric {
170
- m, err := NewConstMetric(desc, valueType, value, labelValues...)
171
- if err != nil {
172
- panic(err)
173
- }
174
- return m
175
-}
176
-
177
-type constMetric struct {
178
- desc *Desc
179
- valType ValueType
180
- val float64
181
- labelPairs []*dto.LabelPair
182
-}
183
-
184
-func (m *constMetric) Desc() *Desc {
185
- return m.desc
186
-}
187
-
188
-func (m *constMetric) Write(out *dto.Metric) error {
189
- return populateMetric(m.valType, m.val, m.labelPairs, out)
190
-}
191
-
192
-func populateMetric(
193
- t ValueType,
194
- v float64,
195
- labelPairs []*dto.LabelPair,
196
- m *dto.Metric,
197
-) error {
198
- m.Label = labelPairs
199
- switch t {
200
- case CounterValue:
201
- m.Counter = &dto.Counter{Value: proto.Float64(v)}
202
- case GaugeValue:
203
- m.Gauge = &dto.Gauge{Value: proto.Float64(v)}
204
- case UntypedValue:
205
- m.Untyped = &dto.Untyped{Value: proto.Float64(v)}
206
- default:
207
- return fmt.Errorf("encountered unknown type %v", t)
208
- }
209
- return nil
210
-}
211
-
212
-func makeLabelPairs(desc *Desc, labelValues []string) []*dto.LabelPair {
213
- totalLen := len(desc.variableLabels) + len(desc.constLabelPairs)
214
- if totalLen == 0 {
215
- // Super fast path.
216
- return nil
217
- }
218
- if len(desc.variableLabels) == 0 {
219
- // Moderately fast path.
220
- return desc.constLabelPairs
221
- }
222
- labelPairs := make([]*dto.LabelPair, 0, totalLen)
223
- for i, n := range desc.variableLabels {
224
- labelPairs = append(labelPairs, &dto.LabelPair{
225
- Name: proto.String(n),
226
- Value: proto.String(labelValues[i]),
227
- })
228
- }
229
- for _, lp := range desc.constLabelPairs {
230
- labelPairs = append(labelPairs, lp)
231
- }
232
- sort.Sort(LabelPairSorter(labelPairs))
233
- return labelPairs
234
-}
Godeps/_workspace/src/github.com/prometheus/client_golang/prometheus/vec.go
deleted
-241
@@ -1,241 +0,0 @@
1
-// Copyright 2014 The Prometheus Authors
2
-// Licensed under the Apache License, Version 2.0 (the "License");
3
-// you may not use this file except in compliance with the License.
4
-// You may obtain a copy of the License at
5
-//
6
-// http://www.apache.org/licenses/LICENSE-2.0
7
-//
8
-// Unless required by applicable law or agreed to in writing, software
9
-// distributed under the License is distributed on an "AS IS" BASIS,
10
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
-// See the License for the specific language governing permissions and
12
-// limitations under the License.
13
-
14
-package prometheus
15
-
16
-import (
17
- "bytes"
18
- "fmt"
19
- "hash"
20
- "sync"
21
-)
22
-
23
-// MetricVec is a Collector to bundle metrics of the same name that
24
-// differ in their label values. MetricVec is usually not used directly but as a
25
-// building block for implementations of vectors of a given metric
26
-// type. GaugeVec, CounterVec, SummaryVec, and UntypedVec are examples already
27
-// provided in this package.
28
-type MetricVec struct {
29
- mtx sync.RWMutex // Protects not only children, but also hash and buf.
30
- children map[uint64]Metric
31
- desc *Desc
32
-
33
- // hash is our own hash instance to avoid repeated allocations.
34
- hash hash.Hash64
35
- // buf is used to copy string contents into it for hashing,
36
- // again to avoid allocations.
37
- buf bytes.Buffer
38
-
39
- newMetric func(labelValues ...string) Metric
40
-}
41
-
42
-// Describe implements Collector. The length of the returned slice
43
-// is always one.
44
-func (m *MetricVec) Describe(ch chan<- *Desc) {
45
- ch <- m.desc
46
-}
47
-
48
-// Collect implements Collector.
49
-func (m *MetricVec) Collect(ch chan<- Metric) {
50
- m.mtx.RLock()
51
- defer m.mtx.RUnlock()
52
-
53
- for _, metric := range m.children {
54
- ch <- metric
55
- }
56
-}
57
-
58
-// GetMetricWithLabelValues returns the Metric for the given slice of label
59
-// values (same order as the VariableLabels in Desc). If that combination of
60
-// label values is accessed for the first time, a new Metric is created.
61
-// Keeping the Metric for later use is possible (and should be considered if
62
-// performance is critical), but keep in mind that Reset, DeleteLabelValues and
63
-// Delete can be used to delete the Metric from the MetricVec. In that case, the
64
-// Metric will still exist, but it will not be exported anymore, even if a
65
-// Metric with the same label values is created later. See also the CounterVec
66
-// example.
67
-//
68
-// An error is returned if the number of label values is not the same as the
69
-// number of VariableLabels in Desc.
70
-//
71
-// Note that for more than one label value, this method is prone to mistakes
72
-// caused by an incorrect order of arguments. Consider GetMetricWith(Labels) as
73
-// an alternative to avoid that type of mistake. For higher label numbers, the
74
-// latter has a much more readable (albeit more verbose) syntax, but it comes
75
-// with a performance overhead (for creating and processing the Labels map).
76
-// See also the GaugeVec example.
77
-func (m *MetricVec) GetMetricWithLabelValues(lvs ...string) (Metric, error) {
78
- m.mtx.Lock()
79
- defer m.mtx.Unlock()
80
-
81
- h, err := m.hashLabelValues(lvs)
82
- if err != nil {
83
- return nil, err
84
- }
85
- return m.getOrCreateMetric(h, lvs...), nil
86
-}
87
-
88
-// GetMetricWith returns the Metric for the given Labels map (the label names
89
-// must match those of the VariableLabels in Desc). If that label map is
90
-// accessed for the first time, a new Metric is created. Implications of keeping
91
-// the Metric are the same as for GetMetricWithLabelValues.
92
-//
93
-// An error is returned if the number and names of the Labels are inconsistent
94
-// with those of the VariableLabels in Desc.
95
-//
96
-// This method is used for the same purpose as
97
-// GetMetricWithLabelValues(...string). See there for pros and cons of the two
98
-// methods.
99
-func (m *MetricVec) GetMetricWith(labels Labels) (Metric, error) {
100
- m.mtx.Lock()
101
- defer m.mtx.Unlock()
102
-
103
- h, err := m.hashLabels(labels)
104
- if err != nil {
105
- return nil, err
106
- }
107
- lvs := make([]string, len(labels))
108
- for i, label := range m.desc.variableLabels {
109
- lvs[i] = labels[label]
110
- }
111
- return m.getOrCreateMetric(h, lvs...), nil
112
-}
113
-
114
-// WithLabelValues works as GetMetricWithLabelValues, but panics if an error
115
-// occurs. The method allows neat syntax like:
116
-// httpReqs.WithLabelValues("404", "POST").Inc()
117
-func (m *MetricVec) WithLabelValues(lvs ...string) Metric {
118
- metric, err := m.GetMetricWithLabelValues(lvs...)
119
- if err != nil {
120
- panic(err)
121
- }
122
- return metric
123
-}
124
-
125
-// With works as GetMetricWith, but panics if an error occurs. The method allows
126
-// neat syntax like:
127
-// httpReqs.With(Labels{"status":"404", "method":"POST"}).Inc()
128
-func (m *MetricVec) With(labels Labels) Metric {
129
- metric, err := m.GetMetricWith(labels)
130
- if err != nil {
131
- panic(err)
132
- }
133
- return metric
134
-}
135
-
136
-// DeleteLabelValues removes the metric where the variable labels are the same
137
-// as those passed in as labels (same order as the VariableLabels in Desc). It
138
-// returns true if a metric was deleted.
139
-//
140
-// It is not an error if the number of label values is not the same as the
141
-// number of VariableLabels in Desc. However, such inconsistent label count can
142
-// never match an actual Metric, so the method will always return false in that
143
-// case.
144
-//
145
-// Note that for more than one label value, this method is prone to mistakes
146
-// caused by an incorrect order of arguments. Consider Delete(Labels) as an
147
-// alternative to avoid that type of mistake. For higher label numbers, the
148
-// latter has a much more readable (albeit more verbose) syntax, but it comes
149
-// with a performance overhead (for creating and processing the Labels map).
150
-// See also the CounterVec example.
151
-func (m *MetricVec) DeleteLabelValues(lvs ...string) bool {
152
- m.mtx.Lock()
153
- defer m.mtx.Unlock()
154
-
155
- h, err := m.hashLabelValues(lvs)
156
- if err != nil {
157
- return false
158
- }
159
- if _, has := m.children[h]; !has {
160
- return false
161
- }
162
- delete(m.children, h)
163
- return true
164
-}
165
-
166
-// Delete deletes the metric where the variable labels are the same as those
167
-// passed in as labels. It returns true if a metric was deleted.
168
-//
169
-// It is not an error if the number and names of the Labels are inconsistent
170
-// with those of the VariableLabels in the Desc of the MetricVec. However, such
171
-// inconsistent Labels can never match an actual Metric, so the method will
172
-// always return false in that case.
173
-//
174
-// This method is used for the same purpose as DeleteLabelValues(...string). See
175
-// there for pros and cons of the two methods.
176
-func (m *MetricVec) Delete(labels Labels) bool {
177
- m.mtx.Lock()
178
- defer m.mtx.Unlock()
179
-
180
- h, err := m.hashLabels(labels)
181
- if err != nil {
182
- return false
183
- }
184
- if _, has := m.children[h]; !has {
185
- return false
186
- }
187
- delete(m.children, h)
188
- return true
189
-}
190
-
191
-// Reset deletes all metrics in this vector.
192
-func (m *MetricVec) Reset() {
193
- m.mtx.Lock()
194
- defer m.mtx.Unlock()
195
-
196
- for h := range m.children {
197
- delete(m.children, h)
198
- }
199
-}
200
-
201
-func (m *MetricVec) hashLabelValues(vals []string) (uint64, error) {
202
- if len(vals) != len(m.desc.variableLabels) {
203
- return 0, errInconsistentCardinality
204
- }
205
- m.hash.Reset()
206
- for _, val := range vals {
207
- m.buf.Reset()
208
- m.buf.WriteString(val)
209
- m.hash.Write(m.buf.Bytes())
210
- }
211
- return m.hash.Sum64(), nil
212
-}
213
-
214
-func (m *MetricVec) hashLabels(labels Labels) (uint64, error) {
215
- if len(labels) != len(m.desc.variableLabels) {
216
- return 0, errInconsistentCardinality
217
- }
218
- m.hash.Reset()
219
- for _, label := range m.desc.variableLabels {
220
- val, ok := labels[label]
221
- if !ok {
222
- return 0, fmt.Errorf("label name %q missing in label map", label)
223
- }
224
- m.buf.Reset()
225
- m.buf.WriteString(val)
226
- m.hash.Write(m.buf.Bytes())
227
- }
228
- return m.hash.Sum64(), nil
229
-}
230
-
231
-func (m *MetricVec) getOrCreateMetric(hash uint64, labelValues ...string) Metric {
232
- metric, ok := m.children[hash]
233
- if !ok {
234
- // Copy labelValues. Otherwise, they would be allocated even if we don't go
235
- // down this code path.
236
- copiedLabelValues := append(make([]string, 0, len(labelValues)), labelValues...)
237
- metric = m.newMetric(copiedLabelValues...)
238
- m.children[hash] = metric
239
- }
240
- return metric
241
-}
Godeps/_workspace/src/github.com/prometheus/client_golang/prometheus/vec_test.go
deleted
-91
@@ -1,91 +0,0 @@
1
-// Copyright 2014 The Prometheus Authors
2
-// Licensed under the Apache License, Version 2.0 (the "License");
3
-// you may not use this file except in compliance with the License.
4
-// You may obtain a copy of the License at
5
-//
6
-// http://www.apache.org/licenses/LICENSE-2.0
7
-//
8
-// Unless required by applicable law or agreed to in writing, software
9
-// distributed under the License is distributed on an "AS IS" BASIS,
10
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
-// See the License for the specific language governing permissions and
12
-// limitations under the License.
13
-
14
-package prometheus
15
-
16
-import (
17
- "hash/fnv"
18
- "testing"
19
-)
20
-
21
-func TestDelete(t *testing.T) {
22
- desc := NewDesc("test", "helpless", []string{"l1", "l2"}, nil)
23
- vec := MetricVec{
24
- children: map[uint64]Metric{},
25
- desc: desc,
26
- hash: fnv.New64a(),
27
- newMetric: func(lvs ...string) Metric {
28
- return newValue(desc, UntypedValue, 0, lvs...)
29
- },
30
- }
31
-
32
- if got, want := vec.Delete(Labels{"l1": "v1", "l2": "v2"}), false; got != want {
33
- t.Errorf("got %v, want %v", got, want)
34
- }
35
-
36
- vec.With(Labels{"l1": "v1", "l2": "v2"}).(Untyped).Set(42)
37
- if got, want := vec.Delete(Labels{"l1": "v1", "l2": "v2"}), true; got != want {
38
- t.Errorf("got %v, want %v", got, want)
39
- }
40
- if got, want := vec.Delete(Labels{"l1": "v1", "l2": "v2"}), false; got != want {
41
- t.Errorf("got %v, want %v", got, want)
42
- }
43
-
44
- vec.With(Labels{"l1": "v1", "l2": "v2"}).(Untyped).Set(42)
45
- if got, want := vec.Delete(Labels{"l2": "v2", "l1": "v1"}), true; got != want {
46
- t.Errorf("got %v, want %v", got, want)
47
- }
48
- if got, want := vec.Delete(Labels{"l2": "v2", "l1": "v1"}), false; got != want {
49
- t.Errorf("got %v, want %v", got, want)
50
- }
51
-
52
- vec.With(Labels{"l1": "v1", "l2": "v2"}).(Untyped).Set(42)
53
- if got, want := vec.Delete(Labels{"l2": "v1", "l1": "v2"}), false; got != want {
54
- t.Errorf("got %v, want %v", got, want)
55
- }
56
- if got, want := vec.Delete(Labels{"l1": "v1"}), false; got != want {
57
- t.Errorf("got %v, want %v", got, want)
58
- }
59
-}
60
-
61
-func TestDeleteLabelValues(t *testing.T) {
62
- desc := NewDesc("test", "helpless", []string{"l1", "l2"}, nil)
63
- vec := MetricVec{
64
- children: map[uint64]Metric{},
65
- desc: desc,
66
- hash: fnv.New64a(),
67
- newMetric: func(lvs ...string) Metric {
68
- return newValue(desc, UntypedValue, 0, lvs...)
69
- },
70
- }
71
-
72
- if got, want := vec.DeleteLabelValues("v1", "v2"), false; got != want {
73
- t.Errorf("got %v, want %v", got, want)
74
- }
75
-
76
- vec.With(Labels{"l1": "v1", "l2": "v2"}).(Untyped).Set(42)
77
- if got, want := vec.DeleteLabelValues("v1", "v2"), true; got != want {
78
- t.Errorf("got %v, want %v", got, want)
79
- }
80
- if got, want := vec.DeleteLabelValues("v1", "v2"), false; got != want {
81
- t.Errorf("got %v, want %v", got, want)
82
- }
83
-
84
- vec.With(Labels{"l1": "v1", "l2": "v2"}).(Untyped).Set(42)
85
- if got, want := vec.DeleteLabelValues("v2", "v1"), false; got != want {
86
- t.Errorf("got %v, want %v", got, want)
87
- }
88
- if got, want := vec.DeleteLabelValues("v1"), false; got != want {
89
- t.Errorf("got %v, want %v", got, want)
90
- }
91
-}
Godeps/_workspace/src/github.com/prometheus/client_golang/text/bench_test.go
deleted
-169
@@ -1,169 +0,0 @@
1
-// Copyright 2015 The Prometheus Authors
2
-// Licensed under the Apache License, Version 2.0 (the "License");
3
-// you may not use this file except in compliance with the License.
4
-// You may obtain a copy of the License at
5
-//
6
-// http://www.apache.org/licenses/LICENSE-2.0
7
-//
8
-// Unless required by applicable law or agreed to in writing, software
9
-// distributed under the License is distributed on an "AS IS" BASIS,
10
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
-// See the License for the specific language governing permissions and
12
-// limitations under the License.
13
-
14
-package text
15
-
16
-import (
17
- "bytes"
18
- "compress/gzip"
19
- "io"
20
- "io/ioutil"
21
- "testing"
22
-
23
- "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/matttproud/golang_protobuf_extensions/pbutil"
24
-
25
- dto "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/prometheus/client_model/go"
26
-)
27
-
28
-// Benchmarks to show how much penalty text format parsing actually inflicts.
29
-//
30
-// Example results on Linux 3.13.0, Intel(R) Core(TM) i7-4700MQ CPU @ 2.40GHz, go1.4.
31
-//
32
-// BenchmarkParseText 1000 1188535 ns/op 205085 B/op 6135 allocs/op
33
-// BenchmarkParseTextGzip 1000 1376567 ns/op 246224 B/op 6151 allocs/op
34
-// BenchmarkParseProto 10000 172790 ns/op 52258 B/op 1160 allocs/op
35
-// BenchmarkParseProtoGzip 5000 324021 ns/op 94931 B/op 1211 allocs/op
36
-// BenchmarkParseProtoMap 10000 187946 ns/op 58714 B/op 1203 allocs/op
37
-//
38
-// CONCLUSION: The overhead for the map is negligible. Text format needs ~5x more allocations.
39
-// Without compression, it needs ~7x longer, but with compression (the more relevant scenario),
40
-// the difference becomes less relevant, only ~4x.
41
-//
42
-// The test data contains 248 samples.
43
-//
44
-// BenchmarkProcessor002ParseOnly in the extraction package is not quite
45
-// comparable to the benchmarks here, but it gives an idea: JSON parsing is even
46
-// slower than text parsing and needs a comparable amount of allocs.
47
-
48
-// BenchmarkParseText benchmarks the parsing of a text-format scrape into metric
49
-// family DTOs.
50
-func BenchmarkParseText(b *testing.B) {
51
- b.StopTimer()
52
- data, err := ioutil.ReadFile("testdata/text")
53
- if err != nil {
54
- b.Fatal(err)
55
- }
56
- b.StartTimer()
57
-
58
- for i := 0; i < b.N; i++ {
59
- if _, err := parser.TextToMetricFamilies(bytes.NewReader(data)); err != nil {
60
- b.Fatal(err)
61
- }
62
- }
63
-}
64
-
65
-// BenchmarkParseTextGzip benchmarks the parsing of a gzipped text-format scrape
66
-// into metric family DTOs.
67
-func BenchmarkParseTextGzip(b *testing.B) {
68
- b.StopTimer()
69
- data, err := ioutil.ReadFile("testdata/text.gz")
70
- if err != nil {
71
- b.Fatal(err)
72
- }
73
- b.StartTimer()
74
-
75
- for i := 0; i < b.N; i++ {
76
- in, err := gzip.NewReader(bytes.NewReader(data))
77
- if err != nil {
78
- b.Fatal(err)
79
- }
80
- if _, err := parser.TextToMetricFamilies(in); err != nil {
81
- b.Fatal(err)
82
- }
83
- }
84
-}
85
-
86
-// BenchmarkParseProto benchmarks the parsing of a protobuf-format scrape into
87
-// metric family DTOs. Note that this does not build a map of metric families
88
-// (as the text version does), because it is not required for Prometheus
89
-// ingestion either. (However, it is required for the text-format parsing, as
90
-// the metric family might be sprinkled all over the text, while the
91
-// protobuf-format guarantees bundling at one place.)
92
-func BenchmarkParseProto(b *testing.B) {
93
- b.StopTimer()
94
- data, err := ioutil.ReadFile("testdata/protobuf")
95
- if err != nil {
96
- b.Fatal(err)
97
- }
98
- b.StartTimer()
99
-
100
- for i := 0; i < b.N; i++ {
101
- family := &dto.MetricFamily{}
102
- in := bytes.NewReader(data)
103
- for {
104
- family.Reset()
105
- if _, err := pbutil.ReadDelimited(in, family); err != nil {
106
- if err == io.EOF {
107
- break
108
- }
109
- b.Fatal(err)
110
- }
111
- }
112
- }
113
-}
114
-
115
-// BenchmarkParseProtoGzip is like BenchmarkParseProto above, but parses gzipped
116
-// protobuf format.
117
-func BenchmarkParseProtoGzip(b *testing.B) {
118
- b.StopTimer()
119
- data, err := ioutil.ReadFile("testdata/protobuf.gz")
120
- if err != nil {
121
- b.Fatal(err)
122
- }
123
- b.StartTimer()
124
-
125
- for i := 0; i < b.N; i++ {
126
- family := &dto.MetricFamily{}
127
- in, err := gzip.NewReader(bytes.NewReader(data))
128
- if err != nil {
129
- b.Fatal(err)
130
- }
131
- for {
132
- family.Reset()
133
- if _, err := pbutil.ReadDelimited(in, family); err != nil {
134
- if err == io.EOF {
135
- break
136
- }
137
- b.Fatal(err)
138
- }
139
- }
140
- }
141
-}
142
-
143
-// BenchmarkParseProtoMap is like BenchmarkParseProto but DOES put the parsed
144
-// metric family DTOs into a map. This is not happening during Prometheus
145
-// ingestion. It is just here to measure the overhead of that map creation and
146
-// separate it from the overhead of the text format parsing.
147
-func BenchmarkParseProtoMap(b *testing.B) {
148
- b.StopTimer()
149
- data, err := ioutil.ReadFile("testdata/protobuf")
150
- if err != nil {
151
- b.Fatal(err)
152
- }
153
- b.StartTimer()
154
-
155
- for i := 0; i < b.N; i++ {
156
- families := map[string]*dto.MetricFamily{}
157
- in := bytes.NewReader(data)
158
- for {
159
- family := &dto.MetricFamily{}
160
- if _, err := pbutil.ReadDelimited(in, family); err != nil {
161
- if err == io.EOF {
162
- break
163
- }
164
- b.Fatal(err)
165
- }
166
- families[family.GetName()] = family
167
- }
168
- }
169
-}
Godeps/_workspace/src/github.com/prometheus/client_golang/text/create.go
deleted
-315
@@ -1,315 +0,0 @@
1
-// Copyright 2014 The Prometheus Authors
2
-// Licensed under the Apache License, Version 2.0 (the "License");
3
-// you may not use this file except in compliance with the License.
4
-// You may obtain a copy of the License at
5
-//
6
-// http://www.apache.org/licenses/LICENSE-2.0
7
-//
8
-// Unless required by applicable law or agreed to in writing, software
9
-// distributed under the License is distributed on an "AS IS" BASIS,
10
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
-// See the License for the specific language governing permissions and
12
-// limitations under the License.
13
-
14
-// Package text contains helper functions to parse and create text-based
15
-// exchange formats. The package currently supports (only) version 0.0.4 of the
16
-// exchange format. Should other versions be supported in the future, some
17
-// versioning scheme has to be applied. Possibilities include separate packages
18
-// or separate functions. The best way depends on the nature of future changes,
19
-// which is the reason why no versioning scheme has been applied prematurely
20
-// here.
21
-package text
22
-
23
-import (
24
- "bytes"
25
- "fmt"
26
- "io"
27
- "math"
28
- "strings"
29
-
30
- "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/prometheus/client_golang/model"
31
- dto "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/prometheus/client_model/go"
32
-)
33
-
34
-// MetricFamilyToText converts a MetricFamily proto message into text format and
35
-// writes the resulting lines to 'out'. It returns the number of bytes written
36
-// and any error encountered. This function does not perform checks on the
37
-// content of the metric and label names, i.e. invalid metric or label names
38
-// will result in invalid text format output.
39
-// This method fulfills the type 'prometheus.encoder'.
40
-func MetricFamilyToText(out io.Writer, in *dto.MetricFamily) (int, error) {
41
- var written int
42
-
43
- // Fail-fast checks.
44
- if len(in.Metric) == 0 {
45
- return written, fmt.Errorf("MetricFamily has no metrics: %s", in)
46
- }
47
- name := in.GetName()
48
- if name == "" {
49
- return written, fmt.Errorf("MetricFamily has no name: %s", in)
50
- }
51
- if in.Type == nil {
52
- return written, fmt.Errorf("MetricFamily has no type: %s", in)
53
- }
54
-
55
- // Comments, first HELP, then TYPE.
56
- if in.Help != nil {
57
- n, err := fmt.Fprintf(
58
- out, "# HELP %s %s\n",
59
- name, escapeString(*in.Help, false),
60
- )
61
- written += n
62
- if err != nil {
63
- return written, err
64
- }
65
- }
66
- metricType := in.GetType()
67
- n, err := fmt.Fprintf(
68
- out, "# TYPE %s %s\n",
69
- name, strings.ToLower(metricType.String()),
70
- )
71
- written += n
72
- if err != nil {
73
- return written, err
74
- }
75
-
76
- // Finally the samples, one line for each.
77
- for _, metric := range in.Metric {
78
- switch metricType {
79
- case dto.MetricType_COUNTER:
80
- if metric.Counter == nil {
81
- return written, fmt.Errorf(
82
- "expected counter in metric %s %s", name, metric,
83
- )
84
- }
85
- n, err = writeSample(
86
- name, metric, "", "",
87
- metric.Counter.GetValue(),
88
- out,
89
- )
90
- case dto.MetricType_GAUGE:
91
- if metric.Gauge == nil {
92
- return written, fmt.Errorf(
93
- "expected gauge in metric %s %s", name, metric,
94
- )
95
- }
96
- n, err = writeSample(
97
- name, metric, "", "",
98
- metric.Gauge.GetValue(),
99
- out,
100
- )
101
- case dto.MetricType_UNTYPED:
102
- if metric.Untyped == nil {
103
- return written, fmt.Errorf(
104
- "expected untyped in metric %s %s", name, metric,
105
- )
106
- }
107
- n, err = writeSample(
108
- name, metric, "", "",
109
- metric.Untyped.GetValue(),
110
- out,
111
- )
112
- case dto.MetricType_SUMMARY:
113
- if metric.Summary == nil {
114
- return written, fmt.Errorf(
115
- "expected summary in metric %s %s", name, metric,
116
- )
117
- }
118
- for _, q := range metric.Summary.Quantile {
119
- n, err = writeSample(
120
- name, metric,
121
- model.QuantileLabel, fmt.Sprint(q.GetQuantile()),
122
- q.GetValue(),
123
- out,
124
- )
125
- written += n
126
- if err != nil {
127
- return written, err
128
- }
129
- }
130
- n, err = writeSample(
131
- name+"_sum", metric, "", "",
132
- metric.Summary.GetSampleSum(),
133
- out,
134
- )
135
- if err != nil {
136
- return written, err
137
- }
138
- written += n
139
- n, err = writeSample(
140
- name+"_count", metric, "", "",
141
- float64(metric.Summary.GetSampleCount()),
142
- out,
143
- )
144
- case dto.MetricType_HISTOGRAM:
145
- if metric.Histogram == nil {
146
- return written, fmt.Errorf(
147
- "expected histogram in metric %s %s", name, metric,
148
- )
149
- }
150
- infSeen := false
151
- for _, q := range metric.Histogram.Bucket {
152
- n, err = writeSample(
153
- name+"_bucket", metric,
154
- model.BucketLabel, fmt.Sprint(q.GetUpperBound()),
155
- float64(q.GetCumulativeCount()),
156
- out,
157
- )
158
- written += n
159
- if err != nil {
160
- return written, err
161
- }
162
- if math.IsInf(q.GetUpperBound(), +1) {
163
- infSeen = true
164
- }
165
- }
166
- if !infSeen {
167
- n, err = writeSample(
168
- name+"_bucket", metric,
169
- model.BucketLabel, "+Inf",
170
- float64(metric.Histogram.GetSampleCount()),
171
- out,
172
- )
173
- if err != nil {
174
- return written, err
175
- }
176
- written += n
177
- }
178
- n, err = writeSample(
179
- name+"_sum", metric, "", "",
180
- metric.Histogram.GetSampleSum(),
181
- out,
182
- )
183
- if err != nil {
184
- return written, err
185
- }
186
- written += n
187
- n, err = writeSample(
188
- name+"_count", metric, "", "",
189
- float64(metric.Histogram.GetSampleCount()),
190
- out,
191
- )
192
- default:
193
- return written, fmt.Errorf(
194
- "unexpected type in metric %s %s", name, metric,
195
- )
196
- }
197
- written += n
198
- if err != nil {
199
- return written, err
200
- }
201
- }
202
- return written, nil
203
-}
204
-
205
-// writeSample writes a single sample in text format to out, given the metric
206
-// name, the metric proto message itself, optionally an additional label name
207
-// and value (use empty strings if not required), and the value. The function
208
-// returns the number of bytes written and any error encountered.
209
-func writeSample(
210
- name string,
211
- metric *dto.Metric,
212
- additionalLabelName, additionalLabelValue string,
213
- value float64,
214
- out io.Writer,
215
-) (int, error) {
216
- var written int
217
- n, err := fmt.Fprint(out, name)
218
- written += n
219
- if err != nil {
220
- return written, err
221
- }
222
- n, err = labelPairsToText(
223
- metric.Label,
224
- additionalLabelName, additionalLabelValue,
225
- out,
226
- )
227
- written += n
228
- if err != nil {
229
- return written, err
230
- }
231
- n, err = fmt.Fprintf(out, " %v", value)
232
- written += n
233
- if err != nil {
234
- return written, err
235
- }
236
- if metric.TimestampMs != nil {
237
- n, err = fmt.Fprintf(out, " %v", *metric.TimestampMs)
238
- written += n
239
- if err != nil {
240
- return written, err
241
- }
242
- }
243
- n, err = out.Write([]byte{'\n'})
244
- written += n
245
- if err != nil {
246
- return written, err
247
- }
248
- return written, nil
249
-}
250
-
251
-// labelPairsToText converts a slice of LabelPair proto messages plus the
252
-// explicitly given additional label pair into text formatted as required by the
253
-// text format and writes it to 'out'. An empty slice in combination with an
254
-// empty string 'additionalLabelName' results in nothing being
255
-// written. Otherwise, the label pairs are written, escaped as required by the
256
-// text format, and enclosed in '{...}'. The function returns the number of
257
-// bytes written and any error encountered.
258
-func labelPairsToText(
259
- in []*dto.LabelPair,
260
- additionalLabelName, additionalLabelValue string,
261
- out io.Writer,
262
-) (int, error) {
263
- if len(in) == 0 && additionalLabelName == "" {
264
- return 0, nil
265
- }
266
- var written int
267
- separator := '{'
268
- for _, lp := range in {
269
- n, err := fmt.Fprintf(
270
- out, `%c%s="%s"`,
271
- separator, lp.GetName(), escapeString(lp.GetValue(), true),
272
- )
273
- written += n
274
- if err != nil {
275
- return written, err
276
- }
277
- separator = ','
278
- }
279
- if additionalLabelName != "" {
280
- n, err := fmt.Fprintf(
281
- out, `%c%s="%s"`,
282
- separator, additionalLabelName,
283
- escapeString(additionalLabelValue, true),
284
- )
285
- written += n
286
- if err != nil {
287
- return written, err
288
- }
289
- }
290
- n, err := out.Write([]byte{'}'})
291
- written += n
292
- if err != nil {
293
- return written, err
294
- }
295
- return written, nil
296
-}
297
-
298
-// escapeString replaces '\' by '\\', new line character by '\n', and - if
299
-// includeDoubleQuote is true - '"' by '\"'.
300
-func escapeString(v string, includeDoubleQuote bool) string {
301
- result := bytes.NewBuffer(make([]byte, 0, len(v)))
302
- for _, c := range v {
303
- switch {
304
- case c == '\\':
305
- result.WriteString(`\\`)
306
- case includeDoubleQuote && c == '"':
307
- result.WriteString(`\"`)
308
- case c == '\n':
309
- result.WriteString(`\n`)
310
- default:
311
- result.WriteRune(c)
312
- }
313
- }
314
- return result.String()
315
-}
Godeps/_workspace/src/github.com/prometheus/client_golang/text/create_test.go
deleted
-439
@@ -1,439 +0,0 @@
1
-// Copyright 2014 The Prometheus Authors
2
-// Licensed under the Apache License, Version 2.0 (the "License");
3
-// you may not use this file except in compliance with the License.
4
-// You may obtain a copy of the License at
5
-//
6
-// http://www.apache.org/licenses/LICENSE-2.0
7
-//
8
-// Unless required by applicable law or agreed to in writing, software
9
-// distributed under the License is distributed on an "AS IS" BASIS,
10
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
-// See the License for the specific language governing permissions and
12
-// limitations under the License.
13
-
14
-package text
15
-
16
-import (
17
- "bytes"
18
- "math"
19
- "strings"
20
- "testing"
21
-
22
- "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/golang/protobuf/proto"
23
- dto "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/prometheus/client_model/go"
24
-)
25
-
26
-func testCreate(t testing.TB) {
27
- var scenarios = []struct {
28
- in *dto.MetricFamily
29
- out string
30
- }{
31
- // 0: Counter, NaN as value, timestamp given.
32
- {
33
- in: &dto.MetricFamily{
34
- Name: proto.String("name"),
35
- Help: proto.String("two-line\n doc str\\ing"),
36
- Type: dto.MetricType_COUNTER.Enum(),
37
- Metric: []*dto.Metric{
38
- &dto.Metric{
39
- Label: []*dto.LabelPair{
40
- &dto.LabelPair{
41
- Name: proto.String("labelname"),
42
- Value: proto.String("val1"),
43
- },
44
- &dto.LabelPair{
45
- Name: proto.String("basename"),
46
- Value: proto.String("basevalue"),
47
- },
48
- },
49
- Counter: &dto.Counter{
50
- Value: proto.Float64(math.NaN()),
51
- },
52
- },
53
- &dto.Metric{
54
- Label: []*dto.LabelPair{
55
- &dto.LabelPair{
56
- Name: proto.String("labelname"),
57
- Value: proto.String("val2"),
58
- },
59
- &dto.LabelPair{
60
- Name: proto.String("basename"),
61
- Value: proto.String("basevalue"),
62
- },
63
- },
64
- Counter: &dto.Counter{
65
- Value: proto.Float64(.23),
66
- },
67
- TimestampMs: proto.Int64(1234567890),
68
- },
69
- },
70
- },
71
- out: `# HELP name two-line\n doc str\\ing
72
-# TYPE name counter
73
-name{labelname="val1",basename="basevalue"} NaN
74
-name{labelname="val2",basename="basevalue"} 0.23 1234567890
75
-`,
76
- },
77
- // 1: Gauge, some escaping required, +Inf as value, multi-byte characters in label values.
78
- {
79
- in: &dto.MetricFamily{
80
- Name: proto.String("gauge_name"),
81
- Help: proto.String("gauge\ndoc\nstr\"ing"),
82
- Type: dto.MetricType_GAUGE.Enum(),
83
- Metric: []*dto.Metric{
84
- &dto.Metric{
85
- Label: []*dto.LabelPair{
86
- &dto.LabelPair{
87
- Name: proto.String("name_1"),
88
- Value: proto.String("val with\nnew line"),
89
- },
90
- &dto.LabelPair{
91
- Name: proto.String("name_2"),
92
- Value: proto.String("val with \\backslash and \"quotes\""),
93
- },
94
- },
95
- Gauge: &dto.Gauge{
96
- Value: proto.Float64(math.Inf(+1)),
97
- },
98
- },
99
- &dto.Metric{
100
- Label: []*dto.LabelPair{
101
- &dto.LabelPair{
102
- Name: proto.String("name_1"),
103
- Value: proto.String("Björn"),
104
- },
105
- &dto.LabelPair{
106
- Name: proto.String("name_2"),
107
- Value: proto.String("佖佥"),
108
- },
109
- },
110
- Gauge: &dto.Gauge{
111
- Value: proto.Float64(3.14E42),
112
- },
113
- },
114
- },
115
- },
116
- out: `# HELP gauge_name gauge\ndoc\nstr"ing
117
-# TYPE gauge_name gauge
118
-gauge_name{name_1="val with\nnew line",name_2="val with \\backslash and \"quotes\""} +Inf
119
-gauge_name{name_1="Björn",name_2="佖佥"} 3.14e+42
120
-`,
121
- },
122
- // 2: Untyped, no help, one sample with no labels and -Inf as value, another sample with one label.
123
- {
124
- in: &dto.MetricFamily{
125
- Name: proto.String("untyped_name"),
126
- Type: dto.MetricType_UNTYPED.Enum(),
127
- Metric: []*dto.Metric{
128
- &dto.Metric{
129
- Untyped: &dto.Untyped{
130
- Value: proto.Float64(math.Inf(-1)),
131
- },
132
- },
133
- &dto.Metric{
134
- Label: []*dto.LabelPair{
135
- &dto.LabelPair{
136
- Name: proto.String("name_1"),
137
- Value: proto.String("value 1"),
138
- },
139
- },
140
- Untyped: &dto.Untyped{
141
- Value: proto.Float64(-1.23e-45),
142
- },
143
- },
144
- },
145
- },
146
- out: `# TYPE untyped_name untyped
147
-untyped_name -Inf
148
-untyped_name{name_1="value 1"} -1.23e-45
149
-`,
150
- },
151
- // 3: Summary.
152
- {
153
- in: &dto.MetricFamily{
154
- Name: proto.String("summary_name"),
155
- Help: proto.String("summary docstring"),
156
- Type: dto.MetricType_SUMMARY.Enum(),
157
- Metric: []*dto.Metric{
158
- &dto.Metric{
159
- Summary: &dto.Summary{
160
- SampleCount: proto.Uint64(42),
161
- SampleSum: proto.Float64(-3.4567),
162
- Quantile: []*dto.Quantile{
163
- &dto.Quantile{
164
- Quantile: proto.Float64(0.5),
165
- Value: proto.Float64(-1.23),
166
- },
167
- &dto.Quantile{
168
- Quantile: proto.Float64(0.9),
169
- Value: proto.Float64(.2342354),
170
- },
171
- &dto.Quantile{
172
- Quantile: proto.Float64(0.99),
173
- Value: proto.Float64(0),
174
- },
175
- },
176
- },
177
- },
178
- &dto.Metric{
179
- Label: []*dto.LabelPair{
180
- &dto.LabelPair{
181
- Name: proto.String("name_1"),
182
- Value: proto.String("value 1"),
183
- },
184
- &dto.LabelPair{
185
- Name: proto.String("name_2"),
186
- Value: proto.String("value 2"),
187
- },
188
- },
189
- Summary: &dto.Summary{
190
- SampleCount: proto.Uint64(4711),
191
- SampleSum: proto.Float64(2010.1971),
192
- Quantile: []*dto.Quantile{
193
- &dto.Quantile{
194
- Quantile: proto.Float64(0.5),
195
- Value: proto.Float64(1),
196
- },
197
- &dto.Quantile{
198
- Quantile: proto.Float64(0.9),
199
- Value: proto.Float64(2),
200
- },
201
- &dto.Quantile{
202
- Quantile: proto.Float64(0.99),
203
- Value: proto.Float64(3),
204
- },
205
- },
206
- },
207
- },
208
- },
209
- },
210
- out: `# HELP summary_name summary docstring
211
-# TYPE summary_name summary
212
-summary_name{quantile="0.5"} -1.23
213
-summary_name{quantile="0.9"} 0.2342354
214
-summary_name{quantile="0.99"} 0
215
-summary_name_sum -3.4567
216
-summary_name_count 42
217
-summary_name{name_1="value 1",name_2="value 2",quantile="0.5"} 1
218
-summary_name{name_1="value 1",name_2="value 2",quantile="0.9"} 2
219
-summary_name{name_1="value 1",name_2="value 2",quantile="0.99"} 3
220
-summary_name_sum{name_1="value 1",name_2="value 2"} 2010.1971
221
-summary_name_count{name_1="value 1",name_2="value 2"} 4711
222
-`,
223
- },
224
- // 4: Histogram
225
- {
226
- in: &dto.MetricFamily{
227
- Name: proto.String("request_duration_microseconds"),
228
- Help: proto.String("The response latency."),
229
- Type: dto.MetricType_HISTOGRAM.Enum(),
230
- Metric: []*dto.Metric{
231
- &dto.Metric{
232
- Histogram: &dto.Histogram{
233
- SampleCount: proto.Uint64(2693),
234
- SampleSum: proto.Float64(1756047.3),
235
- Bucket: []*dto.Bucket{
236
- &dto.Bucket{
237
- UpperBound: proto.Float64(100),
238
- CumulativeCount: proto.Uint64(123),
239
- },
240
- &dto.Bucket{
241
- UpperBound: proto.Float64(120),
242
- CumulativeCount: proto.Uint64(412),
243
- },
244
- &dto.Bucket{
245
- UpperBound: proto.Float64(144),
246
- CumulativeCount: proto.Uint64(592),
247
- },
248
- &dto.Bucket{
249
- UpperBound: proto.Float64(172.8),
250
- CumulativeCount: proto.Uint64(1524),
251
- },
252
- &dto.Bucket{
253
- UpperBound: proto.Float64(math.Inf(+1)),
254
- CumulativeCount: proto.Uint64(2693),
255
- },
256
- },
257
- },
258
- },
259
- },
260
- },
261
- out: `# HELP request_duration_microseconds The response latency.
262
-# TYPE request_duration_microseconds histogram
263
-request_duration_microseconds_bucket{le="100"} 123
264
-request_duration_microseconds_bucket{le="120"} 412
265
-request_duration_microseconds_bucket{le="144"} 592
266
-request_duration_microseconds_bucket{le="172.8"} 1524
267
-request_duration_microseconds_bucket{le="+Inf"} 2693
268
-request_duration_microseconds_sum 1.7560473e+06
269
-request_duration_microseconds_count 2693
270
-`,
271
- },
272
- // 5: Histogram with missing +Inf bucket.
273
- {
274
- in: &dto.MetricFamily{
275
- Name: proto.String("request_duration_microseconds"),
276
- Help: proto.String("The response latency."),
277
- Type: dto.MetricType_HISTOGRAM.Enum(),
278
- Metric: []*dto.Metric{
279
- &dto.Metric{
280
- Histogram: &dto.Histogram{
281
- SampleCount: proto.Uint64(2693),
282
- SampleSum: proto.Float64(1756047.3),
283
- Bucket: []*dto.Bucket{
284
- &dto.Bucket{
285
- UpperBound: proto.Float64(100),
286
- CumulativeCount: proto.Uint64(123),
287
- },
288
- &dto.Bucket{
289
- UpperBound: proto.Float64(120),
290
- CumulativeCount: proto.Uint64(412),
291
- },
292
- &dto.Bucket{
293
- UpperBound: proto.Float64(144),
294
- CumulativeCount: proto.Uint64(592),
295
- },
296
- &dto.Bucket{
297
- UpperBound: proto.Float64(172.8),
298
- CumulativeCount: proto.Uint64(1524),
299
- },
300
- },
301
- },
302
- },
303
- },
304
- },
305
- out: `# HELP request_duration_microseconds The response latency.
306
-# TYPE request_duration_microseconds histogram
307
-request_duration_microseconds_bucket{le="100"} 123
308
-request_duration_microseconds_bucket{le="120"} 412
309
-request_duration_microseconds_bucket{le="144"} 592
310
-request_duration_microseconds_bucket{le="172.8"} 1524
311
-request_duration_microseconds_bucket{le="+Inf"} 2693
312
-request_duration_microseconds_sum 1.7560473e+06
313
-request_duration_microseconds_count 2693
314
-`,
315
- },
316
- }
317
-
318
- for i, scenario := range scenarios {
319
- out := bytes.NewBuffer(make([]byte, 0, len(scenario.out)))
320
- n, err := MetricFamilyToText(out, scenario.in)
321
- if err != nil {
322
- t.Errorf("%d. error: %s", i, err)
323
- continue
324
- }
325
- if expected, got := len(scenario.out), n; expected != got {
326
- t.Errorf(
327
- "%d. expected %d bytes written, got %d",
328
- i, expected, got,
329
- )
330
- }
331
- if expected, got := scenario.out, out.String(); expected != got {
332
- t.Errorf(
333
- "%d. expected out=%q, got %q",
334
- i, expected, got,
335
- )
336
- }
337
- }
338
-
339
-}
340
-
341
-func TestCreate(t *testing.T) {
342
- testCreate(t)
343
-}
344
-
345
-func BenchmarkCreate(b *testing.B) {
346
- for i := 0; i < b.N; i++ {
347
- testCreate(b)
348
- }
349
-}
350
-
351
-func testCreateError(t testing.TB) {
352
- var scenarios = []struct {
353
- in *dto.MetricFamily
354
- err string
355
- }{
356
- // 0: No metric.
357
- {
358
- in: &dto.MetricFamily{
359
- Name: proto.String("name"),
360
- Help: proto.String("doc string"),
361
- Type: dto.MetricType_COUNTER.Enum(),
362
- Metric: []*dto.Metric{},
363
- },
364
- err: "MetricFamily has no metrics",
365
- },
366
- // 1: No metric name.
367
- {
368
- in: &dto.MetricFamily{
369
- Help: proto.String("doc string"),
370
- Type: dto.MetricType_UNTYPED.Enum(),
371
- Metric: []*dto.Metric{
372
- &dto.Metric{
373
- Untyped: &dto.Untyped{
374
- Value: proto.Float64(math.Inf(-1)),
375
- },
376
- },
377
- },
378
- },
379
- err: "MetricFamily has no name",
380
- },
381
- // 2: No metric type.
382
- {
383
- in: &dto.MetricFamily{
384
- Name: proto.String("name"),
385
- Help: proto.String("doc string"),
386
- Metric: []*dto.Metric{
387
- &dto.Metric{
388
- Untyped: &dto.Untyped{
389
- Value: proto.Float64(math.Inf(-1)),
390
- },
391
- },
392
- },
393
- },
394
- err: "MetricFamily has no type",
395
- },
396
- // 3: Wrong type.
397
- {
398
- in: &dto.MetricFamily{
399
- Name: proto.String("name"),
400
- Help: proto.String("doc string"),
401
- Type: dto.MetricType_COUNTER.Enum(),
402
- Metric: []*dto.Metric{
403
- &dto.Metric{
404
- Untyped: &dto.Untyped{
405
- Value: proto.Float64(math.Inf(-1)),
406
- },
407
- },
408
- },
409
- },
410
- err: "expected counter in metric",
411
- },
412
- }
413
-
414
- for i, scenario := range scenarios {
415
- var out bytes.Buffer
416
- _, err := MetricFamilyToText(&out, scenario.in)
417
- if err == nil {
418
- t.Errorf("%d. expected error, got nil", i)
419
- continue
420
- }
421
- if expected, got := scenario.err, err.Error(); strings.Index(got, expected) != 0 {
422
- t.Errorf(
423
- "%d. expected error starting with %q, got %q",
424
- i, expected, got,
425
- )
426
- }
427
- }
428
-
429
-}
430
-
431
-func TestCreateError(t *testing.T) {
432
- testCreateError(t)
433
-}
434
-
435
-func BenchmarkCreateError(b *testing.B) {
436
- for i := 0; i < b.N; i++ {
437
- testCreateError(b)
438
- }
439
-}
Godeps/_workspace/src/github.com/prometheus/client_golang/text/parse.go
deleted
-746
@@ -1,746 +0,0 @@
1
-// Copyright 2014 The Prometheus Authors
2
-// Licensed under the Apache License, Version 2.0 (the "License");
3
-// you may not use this file except in compliance with the License.
4
-// You may obtain a copy of the License at
5
-//
6
-// http://www.apache.org/licenses/LICENSE-2.0
7
-//
8
-// Unless required by applicable law or agreed to in writing, software
9
-// distributed under the License is distributed on an "AS IS" BASIS,
10
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
-// See the License for the specific language governing permissions and
12
-// limitations under the License.
13
-
14
-package text
15
-
16
-import (
17
- "bufio"
18
- "bytes"
19
- "fmt"
20
- "io"
21
- "math"
22
- "strconv"
23
- "strings"
24
-
25
- dto "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/prometheus/client_model/go"
26
-
27
- "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/golang/protobuf/proto"
28
- "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/prometheus/client_golang/model"
29
-)
30
-
31
-// A stateFn is a function that represents a state in a state machine. By
32
-// executing it, the state is progressed to the next state. The stateFn returns
33
-// another stateFn, which represents the new state. The end state is represented
34
-// by nil.
35
-type stateFn func() stateFn
36
-
37
-// ParseError signals errors while parsing the simple and flat text-based
38
-// exchange format.
39
-type ParseError struct {
40
- Line int
41
- Msg string
42
-}
43
-
44
-// Error implements the error interface.
45
-func (e ParseError) Error() string {
46
- return fmt.Sprintf("text format parsing error in line %d: %s", e.Line, e.Msg)
47
-}
48
-
49
-// Parser is used to parse the simple and flat text-based exchange format. Its
50
-// nil value is ready to use.
51
-type Parser struct {
52
- metricFamiliesByName map[string]*dto.MetricFamily
53
- buf *bufio.Reader // Where the parsed input is read through.
54
- err error // Most recent error.
55
- lineCount int // Tracks the line count for error messages.
56
- currentByte byte // The most recent byte read.
57
- currentToken bytes.Buffer // Re-used each time a token has to be gathered from multiple bytes.
58
- currentMF *dto.MetricFamily
59
- currentMetric *dto.Metric
60
- currentLabelPair *dto.LabelPair
61
-
62
- // The remaining member variables are only used for summaries/histograms.
63
- currentLabels map[string]string // All labels including '__name__' but excluding 'quantile'/'le'
64
- // Summary specific.
65
- summaries map[uint64]*dto.Metric // Key is created with LabelsToSignature.
66
- currentQuantile float64
67
- // Histogram specific.
68
- histograms map[uint64]*dto.Metric // Key is created with LabelsToSignature.
69
- currentBucket float64
70
- // These tell us if the currently processed line ends on '_count' or
71
- // '_sum' respectively and belong to a summary/histogram, representing the sample
72
- // count and sum of that summary/histogram.
73
- currentIsSummaryCount, currentIsSummarySum bool
74
- currentIsHistogramCount, currentIsHistogramSum bool
75
-}
76
-
77
-// TextToMetricFamilies reads 'in' as the simple and flat text-based exchange
78
-// format and creates MetricFamily proto messages. It returns the MetricFamily
79
-// proto messages in a map where the metric names are the keys, along with any
80
-// error encountered.
81
-//
82
-// If the input contains duplicate metrics (i.e. lines with the same metric name
83
-// and exactly the same label set), the resulting MetricFamily will contain
84
-// duplicate Metric proto messages. Similar is true for duplicate label
85
-// names. Checks for duplicates have to be performed separately, if required.
86
-// Also note that neither the metrics within each MetricFamily are sorted nor
87
-// the label pairs within each Metric. Sorting is not required for the most
88
-// frequent use of this method, which is sample ingestion in the Prometheus
89
-// server. However, for presentation purposes, you might want to sort the
90
-// metrics, and in some cases, you must sort the labels, e.g. for consumption by
91
-// the metric family injection hook of the Prometheus registry.
92
-//
93
-// Summaries and histograms are rather special beasts. You would probably not
94
-// use them in the simple text format anyway. This method can deal with
95
-// summaries and histograms if they are presented in exactly the way the
96
-// text.Create function creates them.
97
-//
98
-// This method must not be called concurrently. If you want to parse different
99
-// input concurrently, instantiate a separate Parser for each goroutine.
100
-func (p *Parser) TextToMetricFamilies(in io.Reader) (map[string]*dto.MetricFamily, error) {
101
- p.reset(in)
102
- for nextState := p.startOfLine; nextState != nil; nextState = nextState() {
103
- // Magic happens here...
104
- }
105
- // Get rid of empty metric families.
106
- for k, mf := range p.metricFamiliesByName {
107
- if len(mf.GetMetric()) == 0 {
108
- delete(p.metricFamiliesByName, k)
109
- }
110
- }
111
- return p.metricFamiliesByName, p.err
112
-}
113
-
114
-func (p *Parser) reset(in io.Reader) {
115
- p.metricFamiliesByName = map[string]*dto.MetricFamily{}
116
- if p.buf == nil {
117
- p.buf = bufio.NewReader(in)
118
- } else {
119
- p.buf.Reset(in)
120
- }
121
- p.err = nil
122
- p.lineCount = 0
123
- if p.summaries == nil || len(p.summaries) > 0 {
124
- p.summaries = map[uint64]*dto.Metric{}
125
- }
126
- if p.histograms == nil || len(p.histograms) > 0 {
127
- p.histograms = map[uint64]*dto.Metric{}
128
- }
129
- p.currentQuantile = math.NaN()
130
- p.currentBucket = math.NaN()
131
-}
132
-
133
-// startOfLine represents the state where the next byte read from p.buf is the
134
-// start of a line (or whitespace leading up to it).
135
-func (p *Parser) startOfLine() stateFn {
136
- p.lineCount++
137
- if p.skipBlankTab(); p.err != nil {
138
- // End of input reached. This is the only case where
139
- // that is not an error but a signal that we are done.
140
- p.err = nil
141
- return nil
142
- }
143
- switch p.currentByte {
144
- case '#':
145
- return p.startComment
146
- case '\n':
147
- return p.startOfLine // Empty line, start the next one.
148
- }
149
- return p.readingMetricName
150
-}
151
-
152
-// startComment represents the state where the next byte read from p.buf is the
153
-// start of a comment (or whitespace leading up to it).
154
-func (p *Parser) startComment() stateFn {
155
- if p.skipBlankTab(); p.err != nil {
156
- return nil // Unexpected end of input.
157
- }
158
- if p.currentByte == '\n' {
159
- return p.startOfLine
160
- }
161
- if p.readTokenUntilWhitespace(); p.err != nil {
162
- return nil // Unexpected end of input.
163
- }
164
- // If we have hit the end of line already, there is nothing left
165
- // to do. This is not considered a syntax error.
166
- if p.currentByte == '\n' {
167
- return p.startOfLine
168
- }
169
- keyword := p.currentToken.String()
170
- if keyword != "HELP" && keyword != "TYPE" {
171
- // Generic comment, ignore by fast forwarding to end of line.
172
- for p.currentByte != '\n' {
173
- if p.currentByte, p.err = p.buf.ReadByte(); p.err != nil {
174
- return nil // Unexpected end of input.
175
- }
176
- }
177
- return p.startOfLine
178
- }
179
- // There is something. Next has to be a metric name.
180
- if p.skipBlankTab(); p.err != nil {
181
- return nil // Unexpected end of input.
182
- }
183
- if p.readTokenAsMetricName(); p.err != nil {
184
- return nil // Unexpected end of input.
185
- }
186
- if p.currentByte == '\n' {
187
- // At the end of the line already.
188
- // Again, this is not considered a syntax error.
189
- return p.startOfLine
190
- }
191
- if !isBlankOrTab(p.currentByte) {
192
- p.parseError("invalid metric name in comment")
193
- return nil
194
- }
195
- p.setOrCreateCurrentMF()
196
- if p.skipBlankTab(); p.err != nil {
197
- return nil // Unexpected end of input.
198
- }
199
- if p.currentByte == '\n' {
200
- // At the end of the line already.
201
- // Again, this is not considered a syntax error.
202
- return p.startOfLine
203
- }
204
- switch keyword {
205
- case "HELP":
206
- return p.readingHelp
207
- case "TYPE":
208
- return p.readingType
209
- }
210
- panic(fmt.Sprintf("code error: unexpected keyword %q", keyword))
211
-}
212
-
213
-// readingMetricName represents the state where the last byte read (now in
214
-// p.currentByte) is the first byte of a metric name.
215
-func (p *Parser) readingMetricName() stateFn {
216
- if p.readTokenAsMetricName(); p.err != nil {
217
- return nil
218
- }
219
- if p.currentToken.Len() == 0 {
220
- p.parseError("invalid metric name")
221
- return nil
222
- }
223
- p.setOrCreateCurrentMF()
224
- // Now is the time to fix the type if it hasn't happened yet.
225
- if p.currentMF.Type == nil {
226
- p.currentMF.Type = dto.MetricType_UNTYPED.Enum()
227
- }
228
- p.currentMetric = &dto.Metric{}
229
- // Do not append the newly created currentMetric to
230
- // currentMF.Metric right now. First wait if this is a summary,
231
- // and the metric exists already, which we can only know after
232
- // having read all the labels.
233
- if p.skipBlankTabIfCurrentBlankTab(); p.err != nil {
234
- return nil // Unexpected end of input.
235
- }
236
- return p.readingLabels
237
-}
238
-
239
-// readingLabels represents the state where the last byte read (now in
240
-// p.currentByte) is either the first byte of the label set (i.e. a '{'), or the
241
-// first byte of the value (otherwise).
242
-func (p *Parser) readingLabels() stateFn {
243
- // Summaries/histograms are special. We have to reset the
244
- // currentLabels map, currentQuantile and currentBucket before starting to
245
- // read labels.
246
- if p.currentMF.GetType() == dto.MetricType_SUMMARY || p.currentMF.GetType() == dto.MetricType_HISTOGRAM {
247
- p.currentLabels = map[string]string{}
248
- p.currentLabels[string(model.MetricNameLabel)] = p.currentMF.GetName()
249
- p.currentQuantile = math.NaN()
250
- p.currentBucket = math.NaN()
251
- }
252
- if p.currentByte != '{' {
253
- return p.readingValue
254
- }
255
- return p.startLabelName
256
-}
257
-
258
-// startLabelName represents the state where the next byte read from p.buf is
259
-// the start of a label name (or whitespace leading up to it).
260
-func (p *Parser) startLabelName() stateFn {
261
- if p.skipBlankTab(); p.err != nil {
262
- return nil // Unexpected end of input.
263
- }
264
- if p.currentByte == '}' {
265
- if p.skipBlankTab(); p.err != nil {
266
- return nil // Unexpected end of input.
267
- }
268
- return p.readingValue
269
- }
270
- if p.readTokenAsLabelName(); p.err != nil {
271
- return nil // Unexpected end of input.
272
- }
273
- if p.currentToken.Len() == 0 {
274
- p.parseError(fmt.Sprintf("invalid label name for metric %q", p.currentMF.GetName()))
275
- return nil
276
- }
277
- p.currentLabelPair = &dto.LabelPair{Name: proto.String(p.currentToken.String())}
278
- if p.currentLabelPair.GetName() == string(model.MetricNameLabel) {
279
- p.parseError(fmt.Sprintf("label name %q is reserved", model.MetricNameLabel))
280
- return nil
281
- }
282
- // Special summary/histogram treatment. Don't add 'quantile' and 'le'
283
- // labels to 'real' labels.
284
- if !(p.currentMF.GetType() == dto.MetricType_SUMMARY && p.currentLabelPair.GetName() == model.QuantileLabel) &&
285
- !(p.currentMF.GetType() == dto.MetricType_HISTOGRAM && p.currentLabelPair.GetName() == model.BucketLabel) {
286
- p.currentMetric.Label = append(p.currentMetric.Label, p.currentLabelPair)
287
- }
288
- if p.skipBlankTabIfCurrentBlankTab(); p.err != nil {
289
- return nil // Unexpected end of input.
290
- }
291
- if p.currentByte != '=' {
292
- p.parseError(fmt.Sprintf("expected '=' after label name, found %q", p.currentByte))
293
- return nil
294
- }
295
- return p.startLabelValue
296
-}
297
-
298
-// startLabelValue represents the state where the next byte read from p.buf is
299
-// the start of a (quoted) label value (or whitespace leading up to it).
300
-func (p *Parser) startLabelValue() stateFn {
301
- if p.skipBlankTab(); p.err != nil {
302
- return nil // Unexpected end of input.
303
- }
304
- if p.currentByte != '"' {
305
- p.parseError(fmt.Sprintf("expected '\"' at start of label value, found %q", p.currentByte))
306
- return nil
307
- }
308
- if p.readTokenAsLabelValue(); p.err != nil {
309
- return nil
310
- }
311
- p.currentLabelPair.Value = proto.String(p.currentToken.String())
312
- // Special treatment of summaries:
313
- // - Quantile labels are special, will result in dto.Quantile later.
314
- // - Other labels have to be added to currentLabels for signature calculation.
315
- if p.currentMF.GetType() == dto.MetricType_SUMMARY {
316
- if p.currentLabelPair.GetName() == model.QuantileLabel {
317
- if p.currentQuantile, p.err = strconv.ParseFloat(p.currentLabelPair.GetValue(), 64); p.err != nil {
318
- // Create a more helpful error message.
319
- p.parseError(fmt.Sprintf("expected float as value for 'quantile' label, got %q", p.currentLabelPair.GetValue()))
320
- return nil
321
- }
322
- } else {
323
- p.currentLabels[p.currentLabelPair.GetName()] = p.currentLabelPair.GetValue()
324
- }
325
- }
326
- // Similar special treatment of histograms.
327
- if p.currentMF.GetType() == dto.MetricType_HISTOGRAM {
328
- if p.currentLabelPair.GetName() == model.BucketLabel {
329
- if p.currentBucket, p.err = strconv.ParseFloat(p.currentLabelPair.GetValue(), 64); p.err != nil {
330
- // Create a more helpful error message.
331
- p.parseError(fmt.Sprintf("expected float as value for 'le' label, got %q", p.currentLabelPair.GetValue()))
332
- return nil
333
- }
334
- } else {
335
- p.currentLabels[p.currentLabelPair.GetName()] = p.currentLabelPair.GetValue()
336
- }
337
- }
338
- if p.skipBlankTab(); p.err != nil {
339
- return nil // Unexpected end of input.
340
- }
341
- switch p.currentByte {
342
- case ',':
343
- return p.startLabelName
344
-
345
- case '}':
346
- if p.skipBlankTab(); p.err != nil {
347
- return nil // Unexpected end of input.
348
- }
349
- return p.readingValue
350
- default:
351
- p.parseError(fmt.Sprintf("unexpected end of label value %q", p.currentLabelPair.Value))
352
- return nil
353
- }
354
-}
355
-
356
-// readingValue represents the state where the last byte read (now in
357
-// p.currentByte) is the first byte of the sample value (i.e. a float).
358
-func (p *Parser) readingValue() stateFn {
359
- // When we are here, we have read all the labels, so for the
360
- // special case of a summary/histogram, we can finally find out
361
- // if the metric already exists.
362
- if p.currentMF.GetType() == dto.MetricType_SUMMARY {
363
- signature := model.LabelsToSignature(p.currentLabels)
364
- if summary := p.summaries[signature]; summary != nil {
365
- p.currentMetric = summary
366
- } else {
367
- p.summaries[signature] = p.currentMetric
368
- p.currentMF.Metric = append(p.currentMF.Metric, p.currentMetric)
369
- }
370
- } else if p.currentMF.GetType() == dto.MetricType_HISTOGRAM {
371
- signature := model.LabelsToSignature(p.currentLabels)
372
- if histogram := p.histograms[signature]; histogram != nil {
373
- p.currentMetric = histogram
374
- } else {
375
- p.histograms[signature] = p.currentMetric
376
- p.currentMF.Metric = append(p.currentMF.Metric, p.currentMetric)
377
- }
378
- } else {
379
- p.currentMF.Metric = append(p.currentMF.Metric, p.currentMetric)
380
- }
381
- if p.readTokenUntilWhitespace(); p.err != nil {
382
- return nil // Unexpected end of input.
383
- }
384
- value, err := strconv.ParseFloat(p.currentToken.String(), 64)
385
- if err != nil {
386
- // Create a more helpful error message.
387
- p.parseError(fmt.Sprintf("expected float as value, got %q", p.currentToken.String()))
388
- return nil
389
- }
390
- switch p.currentMF.GetType() {
391
- case dto.MetricType_COUNTER:
392
- p.currentMetric.Counter = &dto.Counter{Value: proto.Float64(value)}
393
- case dto.MetricType_GAUGE:
394
- p.currentMetric.Gauge = &dto.Gauge{Value: proto.Float64(value)}
395
- case dto.MetricType_UNTYPED:
396
- p.currentMetric.Untyped = &dto.Untyped{Value: proto.Float64(value)}
397
- case dto.MetricType_SUMMARY:
398
- // *sigh*
399
- if p.currentMetric.Summary == nil {
400
- p.currentMetric.Summary = &dto.Summary{}
401
- }
402
- switch {
403
- case p.currentIsSummaryCount:
404
- p.currentMetric.Summary.SampleCount = proto.Uint64(uint64(value))
405
- case p.currentIsSummarySum:
406
- p.currentMetric.Summary.SampleSum = proto.Float64(value)
407
- case !math.IsNaN(p.currentQuantile):
408
- p.currentMetric.Summary.Quantile = append(
409
- p.currentMetric.Summary.Quantile,
410
- &dto.Quantile{
411
- Quantile: proto.Float64(p.currentQuantile),
412
- Value: proto.Float64(value),
413
- },
414
- )
415
- }
416
- case dto.MetricType_HISTOGRAM:
417
- // *sigh*
418
- if p.currentMetric.Histogram == nil {
419
- p.currentMetric.Histogram = &dto.Histogram{}
420
- }
421
- switch {
422
- case p.currentIsHistogramCount:
423
- p.currentMetric.Histogram.SampleCount = proto.Uint64(uint64(value))
424
- case p.currentIsHistogramSum:
425
- p.currentMetric.Histogram.SampleSum = proto.Float64(value)
426
- case !math.IsNaN(p.currentBucket):
427
- p.currentMetric.Histogram.Bucket = append(
428
- p.currentMetric.Histogram.Bucket,
429
- &dto.Bucket{
430
- UpperBound: proto.Float64(p.currentBucket),
431
- CumulativeCount: proto.Uint64(uint64(value)),
432
- },
433
- )
434
- }
435
- default:
436
- p.err = fmt.Errorf("unexpected type for metric name %q", p.currentMF.GetName())
437
- }
438
- if p.currentByte == '\n' {
439
- return p.startOfLine
440
- }
441
- return p.startTimestamp
442
-}
443
-
444
-// startTimestamp represents the state where the next byte read from p.buf is
445
-// the start of the timestamp (or whitespace leading up to it).
446
-func (p *Parser) startTimestamp() stateFn {
447
- if p.skipBlankTab(); p.err != nil {
448
- return nil // Unexpected end of input.
449
- }
450
- if p.readTokenUntilWhitespace(); p.err != nil {
451
- return nil // Unexpected end of input.
452
- }
453
- timestamp, err := strconv.ParseInt(p.currentToken.String(), 10, 64)
454
- if err != nil {
455
- // Create a more helpful error message.
456
- p.parseError(fmt.Sprintf("expected integer as timestamp, got %q", p.currentToken.String()))
457
- return nil
458
- }
459
- p.currentMetric.TimestampMs = proto.Int64(timestamp)
460
- if p.readTokenUntilNewline(false); p.err != nil {
461
- return nil // Unexpected end of input.
462
- }
463
- if p.currentToken.Len() > 0 {
464
- p.parseError(fmt.Sprintf("spurious string after timestamp: %q", p.currentToken.String()))
465
- return nil
466
- }
467
- return p.startOfLine
468
-}
469
-
470
-// readingHelp represents the state where the last byte read (now in
471
-// p.currentByte) is the first byte of the docstring after 'HELP'.
472
-func (p *Parser) readingHelp() stateFn {
473
- if p.currentMF.Help != nil {
474
- p.parseError(fmt.Sprintf("second HELP line for metric name %q", p.currentMF.GetName()))
475
- return nil
476
- }
477
- // Rest of line is the docstring.
478
- if p.readTokenUntilNewline(true); p.err != nil {
479
- return nil // Unexpected end of input.
480
- }
481
- p.currentMF.Help = proto.String(p.currentToken.String())
482
- return p.startOfLine
483
-}
484
-
485
-// readingType represents the state where the last byte read (now in
486
-// p.currentByte) is the first byte of the type hint after 'HELP'.
487
-func (p *Parser) readingType() stateFn {
488
- if p.currentMF.Type != nil {
489
- p.parseError(fmt.Sprintf("second TYPE line for metric name %q, or TYPE reported after samples", p.currentMF.GetName()))
490
- return nil
491
- }
492
- // Rest of line is the type.
493
- if p.readTokenUntilNewline(false); p.err != nil {
494
- return nil // Unexpected end of input.
495
- }
496
- metricType, ok := dto.MetricType_value[strings.ToUpper(p.currentToken.String())]
497
- if !ok {
498
- p.parseError(fmt.Sprintf("unknown metric type %q", p.currentToken.String()))
499
- return nil
500
- }
501
- p.currentMF.Type = dto.MetricType(metricType).Enum()
502
- return p.startOfLine
503
-}
504
-
505
-// parseError sets p.err to a ParseError at the current line with the given
506
-// message.
507
-func (p *Parser) parseError(msg string) {
508
- p.err = ParseError{
509
- Line: p.lineCount,
510
- Msg: msg,
511
- }
512
-}
513
-
514
-// skipBlankTab reads (and discards) bytes from p.buf until it encounters a byte
515
-// that is neither ' ' nor '\t'. That byte is left in p.currentByte.
516
-func (p *Parser) skipBlankTab() {
517
- for {
518
- if p.currentByte, p.err = p.buf.ReadByte(); p.err != nil || !isBlankOrTab(p.currentByte) {
519
- return
520
- }
521
- }
522
-}
523
-
524
-// skipBlankTabIfCurrentBlankTab works exactly as skipBlankTab but doesn't do
525
-// anything if p.currentByte is neither ' ' nor '\t'.
526
-func (p *Parser) skipBlankTabIfCurrentBlankTab() {
527
- if isBlankOrTab(p.currentByte) {
528
- p.skipBlankTab()
529
- }
530
-}
531
-
532
-// readTokenUntilWhitespace copies bytes from p.buf into p.currentToken. The
533
-// first byte considered is the byte already read (now in p.currentByte). The
534
-// first whitespace byte encountered is still copied into p.currentByte, but not
535
-// into p.currentToken.
536
-func (p *Parser) readTokenUntilWhitespace() {
537
- p.currentToken.Reset()
538
- for p.err == nil && !isBlankOrTab(p.currentByte) && p.currentByte != '\n' {
539
- p.currentToken.WriteByte(p.currentByte)
540
- p.currentByte, p.err = p.buf.ReadByte()
541
- }
542
-}
543
-
544
-// readTokenUntilNewline copies bytes from p.buf into p.currentToken. The first
545
-// byte considered is the byte already read (now in p.currentByte). The first
546
-// newline byte encountered is still copied into p.currentByte, but not into
547
-// p.currentToken. If recognizeEscapeSequence is true, two escape sequences are
548
-// recognized: '\\' tranlates into '\', and '\n' into a line-feed character. All
549
-// other escape sequences are invalid and cause an error.
550
-func (p *Parser) readTokenUntilNewline(recognizeEscapeSequence bool) {
551
- p.currentToken.Reset()
552
- escaped := false
553
- for p.err == nil {
554
- if recognizeEscapeSequence && escaped {
555
- switch p.currentByte {
556
- case '\\':
557
- p.currentToken.WriteByte(p.currentByte)
558
- case 'n':
559
- p.currentToken.WriteByte('\n')
560
- default:
561
- p.parseError(fmt.Sprintf("invalid escape sequence '\\%c'", p.currentByte))
562
- return
563
- }
564
- escaped = false
565
- } else {
566
- switch p.currentByte {
567
- case '\n':
568
- return
569
- case '\\':
570
- escaped = true
571
- default:
572
- p.currentToken.WriteByte(p.currentByte)
573
- }
574
- }
575
- p.currentByte, p.err = p.buf.ReadByte()
576
- }
577
-}
578
-
579
-// readTokenAsMetricName copies a metric name from p.buf into p.currentToken.
580
-// The first byte considered is the byte already read (now in p.currentByte).
581
-// The first byte not part of a metric name is still copied into p.currentByte,
582
-// but not into p.currentToken.
583
-func (p *Parser) readTokenAsMetricName() {
584
- p.currentToken.Reset()
585
- if !isValidMetricNameStart(p.currentByte) {
586
- return
587
- }
588
- for {
589
- p.currentToken.WriteByte(p.currentByte)
590
- p.currentByte, p.err = p.buf.ReadByte()
591
- if p.err != nil || !isValidMetricNameContinuation(p.currentByte) {
592
- return
593
- }
594
- }
595
-}
596
-
597
-// readTokenAsLabelName copies a label name from p.buf into p.currentToken.
598
-// The first byte considered is the byte already read (now in p.currentByte).
599
-// The first byte not part of a label name is still copied into p.currentByte,
600
-// but not into p.currentToken.
601
-func (p *Parser) readTokenAsLabelName() {
602
- p.currentToken.Reset()
603
- if !isValidLabelNameStart(p.currentByte) {
604
- return
605
- }
606
- for {
607
- p.currentToken.WriteByte(p.currentByte)
608
- p.currentByte, p.err = p.buf.ReadByte()
609
- if p.err != nil || !isValidLabelNameContinuation(p.currentByte) {
610
- return
611
- }
612
- }
613
-}
614
-
615
-// readTokenAsLabelValue copies a label value from p.buf into p.currentToken.
616
-// In contrast to the other 'readTokenAs...' functions, which start with the
617
-// last read byte in p.currentByte, this method ignores p.currentByte and starts
618
-// with reading a new byte from p.buf. The first byte not part of a label value
619
-// is still copied into p.currentByte, but not into p.currentToken.
620
-func (p *Parser) readTokenAsLabelValue() {
621
- p.currentToken.Reset()
622
- escaped := false
623
- for {
624
- if p.currentByte, p.err = p.buf.ReadByte(); p.err != nil {
625
- return
626
- }
627
- if escaped {
628
- switch p.currentByte {
629
- case '"', '\\':
630
- p.currentToken.WriteByte(p.currentByte)
631
- case 'n':
632
- p.currentToken.WriteByte('\n')
633
- default:
634
- p.parseError(fmt.Sprintf("invalid escape sequence '\\%c'", p.currentByte))
635
- return
636
- }
637
- escaped = false
638
- continue
639
- }
640
- switch p.currentByte {
641
- case '"':
642
- return
643
- case '\n':
644
- p.parseError(fmt.Sprintf("label value %q contains unescaped new-line", p.currentToken.String()))
645
- return
646
- case '\\':
647
- escaped = true
648
- default:
649
- p.currentToken.WriteByte(p.currentByte)
650
- }
651
- }
652
-}
653
-
654
-func (p *Parser) setOrCreateCurrentMF() {
655
- p.currentIsSummaryCount = false
656
- p.currentIsSummarySum = false
657
- p.currentIsHistogramCount = false
658
- p.currentIsHistogramSum = false
659
- name := p.currentToken.String()
660
- if p.currentMF = p.metricFamiliesByName[name]; p.currentMF != nil {
661
- return
662
- }
663
- // Try out if this is a _sum or _count for a summary/histogram.
664
- summaryName := summaryMetricName(name)
665
- if p.currentMF = p.metricFamiliesByName[summaryName]; p.currentMF != nil {
666
- if p.currentMF.GetType() == dto.MetricType_SUMMARY {
667
- if isCount(name) {
668
- p.currentIsSummaryCount = true
669
- }
670
- if isSum(name) {
671
- p.currentIsSummarySum = true
672
- }
673
- return
674
- }
675
- }
676
- histogramName := histogramMetricName(name)
677
- if p.currentMF = p.metricFamiliesByName[histogramName]; p.currentMF != nil {
678
- if p.currentMF.GetType() == dto.MetricType_HISTOGRAM {
679
- if isCount(name) {
680
- p.currentIsHistogramCount = true
681
- }
682
- if isSum(name) {
683
- p.currentIsHistogramSum = true
684
- }
685
- return
686
- }
687
- }
688
- p.currentMF = &dto.MetricFamily{Name: proto.String(name)}
689
- p.metricFamiliesByName[name] = p.currentMF
690
-}
691
-
692
-func isValidLabelNameStart(b byte) bool {
693
- return (b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z') || b == '_'
694
-}
695
-
696
-func isValidLabelNameContinuation(b byte) bool {
697
- return isValidLabelNameStart(b) || (b >= '0' && b <= '9')
698
-}
699
-
700
-func isValidMetricNameStart(b byte) bool {
701
- return isValidLabelNameStart(b) || b == ':'
702
-}
703
-
704
-func isValidMetricNameContinuation(b byte) bool {
705
- return isValidLabelNameContinuation(b) || b == ':'
706
-}
707
-
708
-func isBlankOrTab(b byte) bool {
709
- return b == ' ' || b == '\t'
710
-}
711
-
712
-func isCount(name string) bool {
713
- return len(name) > 6 && name[len(name)-6:] == "_count"
714
-}
715
-
716
-func isSum(name string) bool {
717
- return len(name) > 4 && name[len(name)-4:] == "_sum"
718
-}
719
-
720
-func isBucket(name string) bool {
721
- return len(name) > 7 && name[len(name)-7:] == "_bucket"
722
-}
723
-
724
-func summaryMetricName(name string) string {
725
- switch {
726
- case isCount(name):
727
- return name[:len(name)-6]
728
- case isSum(name):
729
- return name[:len(name)-4]
730
- default:
731
- return name
732
- }
733
-}
734
-
735
-func histogramMetricName(name string) string {
736
- switch {
737
- case isCount(name):
738
- return name[:len(name)-6]
739
- case isSum(name):
740
- return name[:len(name)-4]
741
- case isBucket(name):
742
- return name[:len(name)-7]
743
- default:
744
- return name
745
- }
746
-}
Godeps/_workspace/src/github.com/prometheus/client_golang/text/parse_test.go
deleted
-588
@@ -1,588 +0,0 @@
1
-// Copyright 2014 The Prometheus Authors
2
-// Licensed under the Apache License, Version 2.0 (the "License");
3
-// you may not use this file except in compliance with the License.
4
-// You may obtain a copy of the License at
5
-//
6
-// http://www.apache.org/licenses/LICENSE-2.0
7
-//
8
-// Unless required by applicable law or agreed to in writing, software
9
-// distributed under the License is distributed on an "AS IS" BASIS,
10
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
-// See the License for the specific language governing permissions and
12
-// limitations under the License.
13
-
14
-package text
15
-
16
-import (
17
- "math"
18
- "strings"
19
- "testing"
20
-
21
- "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/golang/protobuf/proto"
22
- dto "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/prometheus/client_model/go"
23
-)
24
-
25
-var parser Parser
26
-
27
-func testParse(t testing.TB) {
28
- var scenarios = []struct {
29
- in string
30
- out []*dto.MetricFamily
31
- }{
32
- // 0: Empty lines as input.
33
- {
34
- in: `
35
-
36
-`,
37
- out: []*dto.MetricFamily{},
38
- },
39
- // 1: Minimal case.
40
- {
41
- in: `
42
-minimal_metric 1.234
43
-another_metric -3e3 103948
44
-# Even that:
45
-no_labels{} 3
46
-# HELP line for non-existing metric will be ignored.
47
-`,
48
- out: []*dto.MetricFamily{
49
- &dto.MetricFamily{
50
- Name: proto.String("minimal_metric"),
51
- Type: dto.MetricType_UNTYPED.Enum(),
52
- Metric: []*dto.Metric{
53
- &dto.Metric{
54
- Untyped: &dto.Untyped{
55
- Value: proto.Float64(1.234),
56
- },
57
- },
58
- },
59
- },
60
- &dto.MetricFamily{
61
- Name: proto.String("another_metric"),
62
- Type: dto.MetricType_UNTYPED.Enum(),
63
- Metric: []*dto.Metric{
64
- &dto.Metric{
65
- Untyped: &dto.Untyped{
66
- Value: proto.Float64(-3e3),
67
- },
68
- TimestampMs: proto.Int64(103948),
69
- },
70
- },
71
- },
72
- &dto.MetricFamily{
73
- Name: proto.String("no_labels"),
74
- Type: dto.MetricType_UNTYPED.Enum(),
75
- Metric: []*dto.Metric{
76
- &dto.Metric{
77
- Untyped: &dto.Untyped{
78
- Value: proto.Float64(3),
79
- },
80
- },
81
- },
82
- },
83
- },
84
- },
85
- // 2: Counters & gauges, docstrings, various whitespace, escape sequences.
86
- {
87
- in: `
88
-# A normal comment.
89
-#
90
-# TYPE name counter
91
-name{labelname="val1",basename="basevalue"} NaN
92
-name {labelname="val2",basename="base\"v\\al\nue"} 0.23 1234567890
93
-# HELP name two-line\n doc str\\ing
94
-
95
- # HELP name2 doc str"ing 2
96
- # TYPE name2 gauge
97
-name2{labelname="val2" ,basename = "basevalue2" } +Inf 54321
98
-name2{ labelname = "val1" , }-Inf
99
-`,
100
- out: []*dto.MetricFamily{
101
- &dto.MetricFamily{
102
- Name: proto.String("name"),
103
- Help: proto.String("two-line\n doc str\\ing"),
104
- Type: dto.MetricType_COUNTER.Enum(),
105
- Metric: []*dto.Metric{
106
- &dto.Metric{
107
- Label: []*dto.LabelPair{
108
- &dto.LabelPair{
109
- Name: proto.String("labelname"),
110
- Value: proto.String("val1"),
111
- },
112
- &dto.LabelPair{
113
- Name: proto.String("basename"),
114
- Value: proto.String("basevalue"),
115
- },
116
- },
117
- Counter: &dto.Counter{
118
- Value: proto.Float64(math.NaN()),
119
- },
120
- },
121
- &dto.Metric{
122
- Label: []*dto.LabelPair{
123
- &dto.LabelPair{
124
- Name: proto.String("labelname"),
125
- Value: proto.String("val2"),
126
- },
127
- &dto.LabelPair{
128
- Name: proto.String("basename"),
129
- Value: proto.String("base\"v\\al\nue"),
130
- },
131
- },
132
- Counter: &dto.Counter{
133
- Value: proto.Float64(.23),
134
- },
135
- TimestampMs: proto.Int64(1234567890),
136
- },
137
- },
138
- },
139
- &dto.MetricFamily{
140
- Name: proto.String("name2"),
141
- Help: proto.String("doc str\"ing 2"),
142
- Type: dto.MetricType_GAUGE.Enum(),
143
- Metric: []*dto.Metric{
144
- &dto.Metric{
145
- Label: []*dto.LabelPair{
146
- &dto.LabelPair{
147
- Name: proto.String("labelname"),
148
- Value: proto.String("val2"),
149
- },
150
- &dto.LabelPair{
151
- Name: proto.String("basename"),
152
- Value: proto.String("basevalue2"),
153
- },
154
- },
155
- Gauge: &dto.Gauge{
156
- Value: proto.Float64(math.Inf(+1)),
157
- },
158
- TimestampMs: proto.Int64(54321),
159
- },
160
- &dto.Metric{
161
- Label: []*dto.LabelPair{
162
- &dto.LabelPair{
163
- Name: proto.String("labelname"),
164
- Value: proto.String("val1"),
165
- },
166
- },
167
- Gauge: &dto.Gauge{
168
- Value: proto.Float64(math.Inf(-1)),
169
- },
170
- },
171
- },
172
- },
173
- },
174
- },
175
- // 3: The evil summary, mixed with other types and funny comments.
176
- {
177
- in: `
178
-# TYPE my_summary summary
179
-my_summary{n1="val1",quantile="0.5"} 110
180
-decoy -1 -2
181
-my_summary{n1="val1",quantile="0.9"} 140 1
182
-my_summary_count{n1="val1"} 42
183
-# Latest timestamp wins in case of a summary.
184
-my_summary_sum{n1="val1"} 4711 2
185
-fake_sum{n1="val1"} 2001
186
-# TYPE another_summary summary
187
-another_summary_count{n2="val2",n1="val1"} 20
188
-my_summary_count{n2="val2",n1="val1"} 5 5
189
-another_summary{n1="val1",n2="val2",quantile=".3"} -1.2
190
-my_summary_sum{n1="val2"} 08 15
191
-my_summary{n1="val3", quantile="0.2"} 4711
192
- my_summary{n1="val1",n2="val2",quantile="-12.34",} NaN
193
-# some
194
-# funny comments
195
-# HELP
196
-# HELP
197
-# HELP my_summary
198
-# HELP my_summary
199
-`,
200
- out: []*dto.MetricFamily{
201
- &dto.MetricFamily{
202
- Name: proto.String("fake_sum"),
203
- Type: dto.MetricType_UNTYPED.Enum(),
204
- Metric: []*dto.Metric{
205
- &dto.Metric{
206
- Label: []*dto.LabelPair{
207
- &dto.LabelPair{
208
- Name: proto.String("n1"),
209
- Value: proto.String("val1"),
210
- },
211
- },
212
- Untyped: &dto.Untyped{
213
- Value: proto.Float64(2001),
214
- },
215
- },
216
- },
217
- },
218
- &dto.MetricFamily{
219
- Name: proto.String("decoy"),
220
- Type: dto.MetricType_UNTYPED.Enum(),
221
- Metric: []*dto.Metric{
222
- &dto.Metric{
223
- Untyped: &dto.Untyped{
224
- Value: proto.Float64(-1),
225
- },
226
- TimestampMs: proto.Int64(-2),
227
- },
228
- },
229
- },
230
- &dto.MetricFamily{
231
- Name: proto.String("my_summary"),
232
- Type: dto.MetricType_SUMMARY.Enum(),
233
- Metric: []*dto.Metric{
234
- &dto.Metric{
235
- Label: []*dto.LabelPair{
236
- &dto.LabelPair{
237
- Name: proto.String("n1"),
238
- Value: proto.String("val1"),
239
- },
240
- },
241
- Summary: &dto.Summary{
242
- SampleCount: proto.Uint64(42),
243
- SampleSum: proto.Float64(4711),
244
- Quantile: []*dto.Quantile{
245
- &dto.Quantile{
246
- Quantile: proto.Float64(0.5),
247
- Value: proto.Float64(110),
248
- },
249
- &dto.Quantile{
250
- Quantile: proto.Float64(0.9),
251
- Value: proto.Float64(140),
252
- },
253
- },
254
- },
255
- TimestampMs: proto.Int64(2),
256
- },
257
- &dto.Metric{
258
- Label: []*dto.LabelPair{
259
- &dto.LabelPair{
260
- Name: proto.String("n2"),
261
- Value: proto.String("val2"),
262
- },
263
- &dto.LabelPair{
264
- Name: proto.String("n1"),
265
- Value: proto.String("val1"),
266
- },
267
- },
268
- Summary: &dto.Summary{
269
- SampleCount: proto.Uint64(5),
270
- Quantile: []*dto.Quantile{
271
- &dto.Quantile{
272
- Quantile: proto.Float64(-12.34),
273
- Value: proto.Float64(math.NaN()),
274
- },
275
- },
276
- },
277
- TimestampMs: proto.Int64(5),
278
- },
279
- &dto.Metric{
280
- Label: []*dto.LabelPair{
281
- &dto.LabelPair{
282
- Name: proto.String("n1"),
283
- Value: proto.String("val2"),
284
- },
285
- },
286
- Summary: &dto.Summary{
287
- SampleSum: proto.Float64(8),
288
- },
289
- TimestampMs: proto.Int64(15),
290
- },
291
- &dto.Metric{
292
- Label: []*dto.LabelPair{
293
- &dto.LabelPair{
294
- Name: proto.String("n1"),
295
- Value: proto.String("val3"),
296
- },
297
- },
298
- Summary: &dto.Summary{
299
- Quantile: []*dto.Quantile{
300
- &dto.Quantile{
301
- Quantile: proto.Float64(0.2),
302
- Value: proto.Float64(4711),
303
- },
304
- },
305
- },
306
- },
307
- },
308
- },
309
- &dto.MetricFamily{
310
- Name: proto.String("another_summary"),
311
- Type: dto.MetricType_SUMMARY.Enum(),
312
- Metric: []*dto.Metric{
313
- &dto.Metric{
314
- Label: []*dto.LabelPair{
315
- &dto.LabelPair{
316
- Name: proto.String("n2"),
317
- Value: proto.String("val2"),
318
- },
319
- &dto.LabelPair{
320
- Name: proto.String("n1"),
321
- Value: proto.String("val1"),
322
- },
323
- },
324
- Summary: &dto.Summary{
325
- SampleCount: proto.Uint64(20),
326
- Quantile: []*dto.Quantile{
327
- &dto.Quantile{
328
- Quantile: proto.Float64(0.3),
329
- Value: proto.Float64(-1.2),
330
- },
331
- },
332
- },
333
- },
334
- },
335
- },
336
- },
337
- },
338
- // 4: The histogram.
339
- {
340
- in: `
341
-# HELP request_duration_microseconds The response latency.
342
-# TYPE request_duration_microseconds histogram
343
-request_duration_microseconds_bucket{le="100"} 123
344
-request_duration_microseconds_bucket{le="120"} 412
345
-request_duration_microseconds_bucket{le="144"} 592
346
-request_duration_microseconds_bucket{le="172.8"} 1524
347
-request_duration_microseconds_bucket{le="+Inf"} 2693
348
-request_duration_microseconds_sum 1.7560473e+06
349
-request_duration_microseconds_count 2693
350
-`,
351
- out: []*dto.MetricFamily{
352
- {
353
- Name: proto.String("request_duration_microseconds"),
354
- Help: proto.String("The response latency."),
355
- Type: dto.MetricType_HISTOGRAM.Enum(),
356
- Metric: []*dto.Metric{
357
- &dto.Metric{
358
- Histogram: &dto.Histogram{
359
- SampleCount: proto.Uint64(2693),
360
- SampleSum: proto.Float64(1756047.3),
361
- Bucket: []*dto.Bucket{
362
- &dto.Bucket{
363
- UpperBound: proto.Float64(100),
364
- CumulativeCount: proto.Uint64(123),
365
- },
366
- &dto.Bucket{
367
- UpperBound: proto.Float64(120),
368
- CumulativeCount: proto.Uint64(412),
369
- },
370
- &dto.Bucket{
371
- UpperBound: proto.Float64(144),
372
- CumulativeCount: proto.Uint64(592),
373
- },
374
- &dto.Bucket{
375
- UpperBound: proto.Float64(172.8),
376
- CumulativeCount: proto.Uint64(1524),
377
- },
378
- &dto.Bucket{
379
- UpperBound: proto.Float64(math.Inf(+1)),
380
- CumulativeCount: proto.Uint64(2693),
381
- },
382
- },
383
- },
384
- },
385
- },
386
- },
387
- },
388
- },
389
- }
390
-
391
- for i, scenario := range scenarios {
392
- out, err := parser.TextToMetricFamilies(strings.NewReader(scenario.in))
393
- if err != nil {
394
- t.Errorf("%d. error: %s", i, err)
395
- continue
396
- }
397
- if expected, got := len(scenario.out), len(out); expected != got {
398
- t.Errorf(
399
- "%d. expected %d MetricFamilies, got %d",
400
- i, expected, got,
401
- )
402
- }
403
- for _, expected := range scenario.out {
404
- got, ok := out[expected.GetName()]
405
- if !ok {
406
- t.Errorf(
407
- "%d. expected MetricFamily %q, found none",
408
- i, expected.GetName(),
409
- )
410
- continue
411
- }
412
- if expected.String() != got.String() {
413
- t.Errorf(
414
- "%d. expected MetricFamily %s, got %s",
415
- i, expected, got,
416
- )
417
- }
418
- }
419
- }
420
-}
421
-
422
-func TestParse(t *testing.T) {
423
- testParse(t)
424
-}
425
-
426
-func BenchmarkParse(b *testing.B) {
427
- for i := 0; i < b.N; i++ {
428
- testParse(b)
429
- }
430
-}
431
-
432
-func testParseError(t testing.TB) {
433
- var scenarios = []struct {
434
- in string
435
- err string
436
- }{
437
- // 0: No new-line at end of input.
438
- {
439
- in: `bla 3.14`,
440
- err: "EOF",
441
- },
442
- // 1: Invalid escape sequence in label value.
443
- {
444
- in: `metric{label="\t"} 3.14`,
445
- err: "text format parsing error in line 1: invalid escape sequence",
446
- },
447
- // 2: Newline in label value.
448
- {
449
- in: `
450
-metric{label="new
451
-line"} 3.14
452
-`,
453
- err: `text format parsing error in line 2: label value "new" contains unescaped new-line`,
454
- },
455
- // 3:
456
- {
457
- in: `metric{@="bla"} 3.14`,
458
- err: "text format parsing error in line 1: invalid label name for metric",
459
- },
460
- // 4:
461
- {
462
- in: `metric{__name__="bla"} 3.14`,
463
- err: `text format parsing error in line 1: label name "__name__" is reserved`,
464
- },
465
- // 5:
466
- {
467
- in: `metric{label+="bla"} 3.14`,
468
- err: "text format parsing error in line 1: expected '=' after label name",
469
- },
470
- // 6:
471
- {
472
- in: `metric{label=bla} 3.14`,
473
- err: "text format parsing error in line 1: expected '\"' at start of label value",
474
- },
475
- // 7:
476
- {
477
- in: `
478
-# TYPE metric summary
479
-metric{quantile="bla"} 3.14
480
-`,
481
- err: "text format parsing error in line 3: expected float as value for 'quantile' label",
482
- },
483
- // 8:
484
- {
485
- in: `metric{label="bla"+} 3.14`,
486
- err: "text format parsing error in line 1: unexpected end of label value",
487
- },
488
- // 9:
489
- {
490
- in: `metric{label="bla"} 3.14 2.72
491
-`,
492
- err: "text format parsing error in line 1: expected integer as timestamp",
493
- },
494
- // 10:
495
- {
496
- in: `metric{label="bla"} 3.14 2 3
497
-`,
498
- err: "text format parsing error in line 1: spurious string after timestamp",
499
- },
500
- // 11:
501
- {
502
- in: `metric{label="bla"} blubb
503
-`,
504
- err: "text format parsing error in line 1: expected float as value",
505
- },
506
- // 12:
507
- {
508
- in: `
509
-# HELP metric one
510
-# HELP metric two
511
-`,
512
- err: "text format parsing error in line 3: second HELP line for metric name",
513
- },
514
- // 13:
515
- {
516
- in: `
517
-# TYPE metric counter
518
-# TYPE metric untyped
519
-`,
520
- err: `text format parsing error in line 3: second TYPE line for metric name "metric", or TYPE reported after samples`,
521
- },
522
- // 14:
523
- {
524
- in: `
525
-metric 4.12
526
-# TYPE metric counter
527
-`,
528
- err: `text format parsing error in line 3: second TYPE line for metric name "metric", or TYPE reported after samples`,
529
- },
530
- // 14:
531
- {
532
- in: `
533
-# TYPE metric bla
534
-`,
535
- err: "text format parsing error in line 2: unknown metric type",
536
- },
537
- // 15:
538
- {
539
- in: `
540
-# TYPE met-ric
541
-`,
542
- err: "text format parsing error in line 2: invalid metric name in comment",
543
- },
544
- // 16:
545
- {
546
- in: `@invalidmetric{label="bla"} 3.14 2`,
547
- err: "text format parsing error in line 1: invalid metric name",
548
- },
549
- // 17:
550
- {
551
- in: `{label="bla"} 3.14 2`,
552
- err: "text format parsing error in line 1: invalid metric name",
553
- },
554
- // 18:
555
- {
556
- in: `
557
-# TYPE metric histogram
558
-metric_bucket{le="bla"} 3.14
559
-`,
560
- err: "text format parsing error in line 3: expected float as value for 'le' label",
561
- },
562
- }
563
-
564
- for i, scenario := range scenarios {
565
- _, err := parser.TextToMetricFamilies(strings.NewReader(scenario.in))
566
- if err == nil {
567
- t.Errorf("%d. expected error, got nil", i)
568
- continue
569
- }
570
- if expected, got := scenario.err, err.Error(); strings.Index(got, expected) != 0 {
571
- t.Errorf(
572
- "%d. expected error starting with %q, got %q",
573
- i, expected, got,
574
- )
575
- }
576
- }
577
-
578
-}
579
-
580
-func TestParseError(t *testing.T) {
581
- testParseError(t)
582
-}
583
-
584
-func BenchmarkParseError(b *testing.B) {
585
- for i := 0; i < b.N; i++ {
586
- testParseError(b)
587
- }
588
-}
Godeps/_workspace/src/github.com/prometheus/client_golang/text/proto.go
deleted
-43
@@ -1,43 +0,0 @@
1
-// Copyright 2014 The Prometheus Authors
2
-// Licensed under the Apache License, Version 2.0 (the "License");
3
-// you may not use this file except in compliance with the License.
4
-// You may obtain a copy of the License at
5
-//
6
-// http://www.apache.org/licenses/LICENSE-2.0
7
-//
8
-// Unless required by applicable law or agreed to in writing, software
9
-// distributed under the License is distributed on an "AS IS" BASIS,
10
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
-// See the License for the specific language governing permissions and
12
-// limitations under the License.
13
-
14
-package text
15
-
16
-import (
17
- "fmt"
18
- "io"
19
-
20
- "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/golang/protobuf/proto"
21
- "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/matttproud/golang_protobuf_extensions/pbutil"
22
-
23
- dto "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/prometheus/client_model/go"
24
-)
25
-
26
-// WriteProtoDelimited writes the MetricFamily to the writer in delimited
27
-// protobuf format and returns the number of bytes written and any error
28
-// encountered.
29
-func WriteProtoDelimited(w io.Writer, p *dto.MetricFamily) (int, error) {
30
- return pbutil.WriteDelimited(w, p)
31
-}
32
-
33
-// WriteProtoText writes the MetricFamily to the writer in text format and
34
-// returns the number of bytes written and any error encountered.
35
-func WriteProtoText(w io.Writer, p *dto.MetricFamily) (int, error) {
36
- return fmt.Fprintf(w, "%s\n", proto.MarshalTextString(p))
37
-}
38
-
39
-// WriteProtoCompactText writes the MetricFamily to the writer in compact text
40
-// format and returns the number of bytes written and any error encountered.
41
-func WriteProtoCompactText(w io.Writer, p *dto.MetricFamily) (int, error) {
42
- return fmt.Fprintf(w, "%s\n", p)
43
-}
Godeps/_workspace/src/github.com/prometheus/client_golang/text/testdata/protobuf
Binary files a/Godeps/_workspace/src/github.com/prometheus/client_golang/text/testdata/protobuf and /dev/null differ
Godeps/_workspace/src/github.com/prometheus/client_golang/text/testdata/protobuf.gz
Binary files a/Godeps/_workspace/src/github.com/prometheus/client_golang/text/testdata/protobuf.gz and /dev/null differ
Godeps/_workspace/src/github.com/prometheus/client_golang/text/testdata/text
deleted
-322
@@ -1,322 +0,0 @@
1
-# HELP http_request_duration_microseconds The HTTP request latencies in microseconds.
2
-# TYPE http_request_duration_microseconds summary
3
-http_request_duration_microseconds{handler="/",quantile="0.5"} 0
4
-http_request_duration_microseconds{handler="/",quantile="0.9"} 0
5
-http_request_duration_microseconds{handler="/",quantile="0.99"} 0
6
-http_request_duration_microseconds_sum{handler="/"} 0
7
-http_request_duration_microseconds_count{handler="/"} 0
8
-http_request_duration_microseconds{handler="/alerts",quantile="0.5"} 0
9
-http_request_duration_microseconds{handler="/alerts",quantile="0.9"} 0
10
-http_request_duration_microseconds{handler="/alerts",quantile="0.99"} 0
11
-http_request_duration_microseconds_sum{handler="/alerts"} 0
12
-http_request_duration_microseconds_count{handler="/alerts"} 0
13
-http_request_duration_microseconds{handler="/api/metrics",quantile="0.5"} 0
14
-http_request_duration_microseconds{handler="/api/metrics",quantile="0.9"} 0
15
-http_request_duration_microseconds{handler="/api/metrics",quantile="0.99"} 0
16
-http_request_duration_microseconds_sum{handler="/api/metrics"} 0
17
-http_request_duration_microseconds_count{handler="/api/metrics"} 0
18
-http_request_duration_microseconds{handler="/api/query",quantile="0.5"} 0
19
-http_request_duration_microseconds{handler="/api/query",quantile="0.9"} 0
20
-http_request_duration_microseconds{handler="/api/query",quantile="0.99"} 0
21
-http_request_duration_microseconds_sum{handler="/api/query"} 0
22
-http_request_duration_microseconds_count{handler="/api/query"} 0
23
-http_request_duration_microseconds{handler="/api/query_range",quantile="0.5"} 0
24
-http_request_duration_microseconds{handler="/api/query_range",quantile="0.9"} 0
25
-http_request_duration_microseconds{handler="/api/query_range",quantile="0.99"} 0
26
-http_request_duration_microseconds_sum{handler="/api/query_range"} 0
27
-http_request_duration_microseconds_count{handler="/api/query_range"} 0
28
-http_request_duration_microseconds{handler="/api/targets",quantile="0.5"} 0
29
-http_request_duration_microseconds{handler="/api/targets",quantile="0.9"} 0
30
-http_request_duration_microseconds{handler="/api/targets",quantile="0.99"} 0
31
-http_request_duration_microseconds_sum{handler="/api/targets"} 0
32
-http_request_duration_microseconds_count{handler="/api/targets"} 0
33
-http_request_duration_microseconds{handler="/consoles/",quantile="0.5"} 0
34
-http_request_duration_microseconds{handler="/consoles/",quantile="0.9"} 0
35
-http_request_duration_microseconds{handler="/consoles/",quantile="0.99"} 0
36
-http_request_duration_microseconds_sum{handler="/consoles/"} 0
37
-http_request_duration_microseconds_count{handler="/consoles/"} 0
38
-http_request_duration_microseconds{handler="/graph",quantile="0.5"} 0
39
-http_request_duration_microseconds{handler="/graph",quantile="0.9"} 0
40
-http_request_duration_microseconds{handler="/graph",quantile="0.99"} 0
41
-http_request_duration_microseconds_sum{handler="/graph"} 0
42
-http_request_duration_microseconds_count{handler="/graph"} 0
43
-http_request_duration_microseconds{handler="/heap",quantile="0.5"} 0
44
-http_request_duration_microseconds{handler="/heap",quantile="0.9"} 0
45
-http_request_duration_microseconds{handler="/heap",quantile="0.99"} 0
46
-http_request_duration_microseconds_sum{handler="/heap"} 0
47
-http_request_duration_microseconds_count{handler="/heap"} 0
48
-http_request_duration_microseconds{handler="/static/",quantile="0.5"} 0
49
-http_request_duration_microseconds{handler="/static/",quantile="0.9"} 0
50
-http_request_duration_microseconds{handler="/static/",quantile="0.99"} 0
51
-http_request_duration_microseconds_sum{handler="/static/"} 0
52
-http_request_duration_microseconds_count{handler="/static/"} 0
53
-http_request_duration_microseconds{handler="prometheus",quantile="0.5"} 1307.275
54
-http_request_duration_microseconds{handler="prometheus",quantile="0.9"} 1858.632
55
-http_request_duration_microseconds{handler="prometheus",quantile="0.99"} 3087.384
56
-http_request_duration_microseconds_sum{handler="prometheus"} 179886.5000000001
57
-http_request_duration_microseconds_count{handler="prometheus"} 119
58
-# HELP http_request_size_bytes The HTTP request sizes in bytes.
59
-# TYPE http_request_size_bytes summary
60
-http_request_size_bytes{handler="/",quantile="0.5"} 0
61
-http_request_size_bytes{handler="/",quantile="0.9"} 0
62
-http_request_size_bytes{handler="/",quantile="0.99"} 0
63
-http_request_size_bytes_sum{handler="/"} 0
64
-http_request_size_bytes_count{handler="/"} 0
65
-http_request_size_bytes{handler="/alerts",quantile="0.5"} 0
66
-http_request_size_bytes{handler="/alerts",quantile="0.9"} 0
67
-http_request_size_bytes{handler="/alerts",quantile="0.99"} 0
68
-http_request_size_bytes_sum{handler="/alerts"} 0
69
-http_request_size_bytes_count{handler="/alerts"} 0
70
-http_request_size_bytes{handler="/api/metrics",quantile="0.5"} 0
71
-http_request_size_bytes{handler="/api/metrics",quantile="0.9"} 0
72
-http_request_size_bytes{handler="/api/metrics",quantile="0.99"} 0
73
-http_request_size_bytes_sum{handler="/api/metrics"} 0
74
-http_request_size_bytes_count{handler="/api/metrics"} 0
75
-http_request_size_bytes{handler="/api/query",quantile="0.5"} 0
76
-http_request_size_bytes{handler="/api/query",quantile="0.9"} 0
77
-http_request_size_bytes{handler="/api/query",quantile="0.99"} 0
78
-http_request_size_bytes_sum{handler="/api/query"} 0
79
-http_request_size_bytes_count{handler="/api/query"} 0
80
-http_request_size_bytes{handler="/api/query_range",quantile="0.5"} 0
81
-http_request_size_bytes{handler="/api/query_range",quantile="0.9"} 0
82
-http_request_size_bytes{handler="/api/query_range",quantile="0.99"} 0
83
-http_request_size_bytes_sum{handler="/api/query_range"} 0
84
-http_request_size_bytes_count{handler="/api/query_range"} 0
85
-http_request_size_bytes{handler="/api/targets",quantile="0.5"} 0
86
-http_request_size_bytes{handler="/api/targets",quantile="0.9"} 0
87
-http_request_size_bytes{handler="/api/targets",quantile="0.99"} 0
88
-http_request_size_bytes_sum{handler="/api/targets"} 0
89
-http_request_size_bytes_count{handler="/api/targets"} 0
90
-http_request_size_bytes{handler="/consoles/",quantile="0.5"} 0
91
-http_request_size_bytes{handler="/consoles/",quantile="0.9"} 0
92
-http_request_size_bytes{handler="/consoles/",quantile="0.99"} 0
93
-http_request_size_bytes_sum{handler="/consoles/"} 0
94
-http_request_size_bytes_count{handler="/consoles/"} 0
95
-http_request_size_bytes{handler="/graph",quantile="0.5"} 0
96
-http_request_size_bytes{handler="/graph",quantile="0.9"} 0
97
-http_request_size_bytes{handler="/graph",quantile="0.99"} 0
98
-http_request_size_bytes_sum{handler="/graph"} 0
99
-http_request_size_bytes_count{handler="/graph"} 0
100
-http_request_size_bytes{handler="/heap",quantile="0.5"} 0
101
-http_request_size_bytes{handler="/heap",quantile="0.9"} 0
102
-http_request_size_bytes{handler="/heap",quantile="0.99"} 0
103
-http_request_size_bytes_sum{handler="/heap"} 0
104
-http_request_size_bytes_count{handler="/heap"} 0
105
-http_request_size_bytes{handler="/static/",quantile="0.5"} 0
106
-http_request_size_bytes{handler="/static/",quantile="0.9"} 0
107
-http_request_size_bytes{handler="/static/",quantile="0.99"} 0
108
-http_request_size_bytes_sum{handler="/static/"} 0
109
-http_request_size_bytes_count{handler="/static/"} 0
110
-http_request_size_bytes{handler="prometheus",quantile="0.5"} 291
111
-http_request_size_bytes{handler="prometheus",quantile="0.9"} 291
112
-http_request_size_bytes{handler="prometheus",quantile="0.99"} 291
113
-http_request_size_bytes_sum{handler="prometheus"} 34488
114
-http_request_size_bytes_count{handler="prometheus"} 119
115
-# HELP http_requests_total Total number of HTTP requests made.
116
-# TYPE http_requests_total counter
117
-http_requests_total{code="200",handler="prometheus",method="get"} 119
118
-# HELP http_response_size_bytes The HTTP response sizes in bytes.
119
-# TYPE http_response_size_bytes summary
120
-http_response_size_bytes{handler="/",quantile="0.5"} 0
121
-http_response_size_bytes{handler="/",quantile="0.9"} 0
122
-http_response_size_bytes{handler="/",quantile="0.99"} 0
123
-http_response_size_bytes_sum{handler="/"} 0
124
-http_response_size_bytes_count{handler="/"} 0
125
-http_response_size_bytes{handler="/alerts",quantile="0.5"} 0
126
-http_response_size_bytes{handler="/alerts",quantile="0.9"} 0
127
-http_response_size_bytes{handler="/alerts",quantile="0.99"} 0
128
-http_response_size_bytes_sum{handler="/alerts"} 0
129
-http_response_size_bytes_count{handler="/alerts"} 0
130
-http_response_size_bytes{handler="/api/metrics",quantile="0.5"} 0
131
-http_response_size_bytes{handler="/api/metrics",quantile="0.9"} 0
132
-http_response_size_bytes{handler="/api/metrics",quantile="0.99"} 0
133
-http_response_size_bytes_sum{handler="/api/metrics"} 0
134
-http_response_size_bytes_count{handler="/api/metrics"} 0
135
-http_response_size_bytes{handler="/api/query",quantile="0.5"} 0
136
-http_response_size_bytes{handler="/api/query",quantile="0.9"} 0
137
-http_response_size_bytes{handler="/api/query",quantile="0.99"} 0
138
-http_response_size_bytes_sum{handler="/api/query"} 0
139
-http_response_size_bytes_count{handler="/api/query"} 0
140
-http_response_size_bytes{handler="/api/query_range",quantile="0.5"} 0
141
-http_response_size_bytes{handler="/api/query_range",quantile="0.9"} 0
142
-http_response_size_bytes{handler="/api/query_range",quantile="0.99"} 0
143
-http_response_size_bytes_sum{handler="/api/query_range"} 0
144
-http_response_size_bytes_count{handler="/api/query_range"} 0
145
-http_response_size_bytes{handler="/api/targets",quantile="0.5"} 0
146
-http_response_size_bytes{handler="/api/targets",quantile="0.9"} 0
147
-http_response_size_bytes{handler="/api/targets",quantile="0.99"} 0
148
-http_response_size_bytes_sum{handler="/api/targets"} 0
149
-http_response_size_bytes_count{handler="/api/targets"} 0
150
-http_response_size_bytes{handler="/consoles/",quantile="0.5"} 0
151
-http_response_size_bytes{handler="/consoles/",quantile="0.9"} 0
152
-http_response_size_bytes{handler="/consoles/",quantile="0.99"} 0
153
-http_response_size_bytes_sum{handler="/consoles/"} 0
154
-http_response_size_bytes_count{handler="/consoles/"} 0
155
-http_response_size_bytes{handler="/graph",quantile="0.5"} 0
156
-http_response_size_bytes{handler="/graph",quantile="0.9"} 0
157
-http_response_size_bytes{handler="/graph",quantile="0.99"} 0
158
-http_response_size_bytes_sum{handler="/graph"} 0
159
-http_response_size_bytes_count{handler="/graph"} 0
160
-http_response_size_bytes{handler="/heap",quantile="0.5"} 0
161
-http_response_size_bytes{handler="/heap",quantile="0.9"} 0
162
-http_response_size_bytes{handler="/heap",quantile="0.99"} 0
163
-http_response_size_bytes_sum{handler="/heap"} 0
164
-http_response_size_bytes_count{handler="/heap"} 0
165
-http_response_size_bytes{handler="/static/",quantile="0.5"} 0
166
-http_response_size_bytes{handler="/static/",quantile="0.9"} 0
167
-http_response_size_bytes{handler="/static/",quantile="0.99"} 0
168
-http_response_size_bytes_sum{handler="/static/"} 0
169
-http_response_size_bytes_count{handler="/static/"} 0
170
-http_response_size_bytes{handler="prometheus",quantile="0.5"} 2049
171
-http_response_size_bytes{handler="prometheus",quantile="0.9"} 2058
172
-http_response_size_bytes{handler="prometheus",quantile="0.99"} 2064
173
-http_response_size_bytes_sum{handler="prometheus"} 247001
174
-http_response_size_bytes_count{handler="prometheus"} 119
175
-# HELP process_cpu_seconds_total Total user and system CPU time spent in seconds.
176
-# TYPE process_cpu_seconds_total counter
177
-process_cpu_seconds_total 0.55
178
-# HELP go_goroutines Number of goroutines that currently exist.
179
-# TYPE go_goroutines gauge
180
-go_goroutines 70
181
-# HELP process_max_fds Maximum number of open file descriptors.
182
-# TYPE process_max_fds gauge
183
-process_max_fds 8192
184
-# HELP process_open_fds Number of open file descriptors.
185
-# TYPE process_open_fds gauge
186
-process_open_fds 29
187
-# HELP process_resident_memory_bytes Resident memory size in bytes.
188
-# TYPE process_resident_memory_bytes gauge
189
-process_resident_memory_bytes 5.3870592e+07
190
-# HELP process_start_time_seconds Start time of the process since unix epoch in seconds.
191
-# TYPE process_start_time_seconds gauge
192
-process_start_time_seconds 1.42236894836e+09
193
-# HELP process_virtual_memory_bytes Virtual memory size in bytes.
194
-# TYPE process_virtual_memory_bytes gauge
195
-process_virtual_memory_bytes 5.41478912e+08
196
-# HELP prometheus_dns_sd_lookup_failures_total The number of DNS-SD lookup failures.
197
-# TYPE prometheus_dns_sd_lookup_failures_total counter
198
-prometheus_dns_sd_lookup_failures_total 0
199
-# HELP prometheus_dns_sd_lookups_total The number of DNS-SD lookups.
200
-# TYPE prometheus_dns_sd_lookups_total counter
201
-prometheus_dns_sd_lookups_total 7
202
-# HELP prometheus_evaluator_duration_milliseconds The duration for all evaluations to execute.
203
-# TYPE prometheus_evaluator_duration_milliseconds summary
204
-prometheus_evaluator_duration_milliseconds{quantile="0.01"} 0
205
-prometheus_evaluator_duration_milliseconds{quantile="0.05"} 0
206
-prometheus_evaluator_duration_milliseconds{quantile="0.5"} 0
207
-prometheus_evaluator_duration_milliseconds{quantile="0.9"} 1
208
-prometheus_evaluator_duration_milliseconds{quantile="0.99"} 1
209
-prometheus_evaluator_duration_milliseconds_sum 12
210
-prometheus_evaluator_duration_milliseconds_count 23
211
-# HELP prometheus_local_storage_checkpoint_duration_milliseconds The duration (in milliseconds) it took to checkpoint in-memory metrics and head chunks.
212
-# TYPE prometheus_local_storage_checkpoint_duration_milliseconds gauge
213
-prometheus_local_storage_checkpoint_duration_milliseconds 0
214
-# HELP prometheus_local_storage_chunk_ops_total The total number of chunk operations by their type.
215
-# TYPE prometheus_local_storage_chunk_ops_total counter
216
-prometheus_local_storage_chunk_ops_total{type="create"} 598
217
-prometheus_local_storage_chunk_ops_total{type="persist"} 174
218
-prometheus_local_storage_chunk_ops_total{type="pin"} 920
219
-prometheus_local_storage_chunk_ops_total{type="transcode"} 415
220
-prometheus_local_storage_chunk_ops_total{type="unpin"} 920
221
-# HELP prometheus_local_storage_indexing_batch_latency_milliseconds Quantiles for batch indexing latencies in milliseconds.
222
-# TYPE prometheus_local_storage_indexing_batch_latency_milliseconds summary
223
-prometheus_local_storage_indexing_batch_latency_milliseconds{quantile="0.5"} 0
224
-prometheus_local_storage_indexing_batch_latency_milliseconds{quantile="0.9"} 0
225
-prometheus_local_storage_indexing_batch_latency_milliseconds{quantile="0.99"} 0
226
-prometheus_local_storage_indexing_batch_latency_milliseconds_sum 0
227
-prometheus_local_storage_indexing_batch_latency_milliseconds_count 1
228
-# HELP prometheus_local_storage_indexing_batch_sizes Quantiles for indexing batch sizes (number of metrics per batch).
229
-# TYPE prometheus_local_storage_indexing_batch_sizes summary
230
-prometheus_local_storage_indexing_batch_sizes{quantile="0.5"} 2
231
-prometheus_local_storage_indexing_batch_sizes{quantile="0.9"} 2
232
-prometheus_local_storage_indexing_batch_sizes{quantile="0.99"} 2
233
-prometheus_local_storage_indexing_batch_sizes_sum 2
234
-prometheus_local_storage_indexing_batch_sizes_count 1
235
-# HELP prometheus_local_storage_indexing_queue_capacity The capacity of the indexing queue.
236
-# TYPE prometheus_local_storage_indexing_queue_capacity gauge
237
-prometheus_local_storage_indexing_queue_capacity 16384
238
-# HELP prometheus_local_storage_indexing_queue_length The number of metrics waiting to be indexed.
239
-# TYPE prometheus_local_storage_indexing_queue_length gauge
240
-prometheus_local_storage_indexing_queue_length 0
241
-# HELP prometheus_local_storage_ingested_samples_total The total number of samples ingested.
242
-# TYPE prometheus_local_storage_ingested_samples_total counter
243
-prometheus_local_storage_ingested_samples_total 30473
244
-# HELP prometheus_local_storage_invalid_preload_requests_total The total number of preload requests referring to a non-existent series. This is an indication of outdated label indexes.
245
-# TYPE prometheus_local_storage_invalid_preload_requests_total counter
246
-prometheus_local_storage_invalid_preload_requests_total 0
247
-# HELP prometheus_local_storage_memory_chunkdescs The current number of chunk descriptors in memory.
248
-# TYPE prometheus_local_storage_memory_chunkdescs gauge
249
-prometheus_local_storage_memory_chunkdescs 1059
250
-# HELP prometheus_local_storage_memory_chunks The current number of chunks in memory, excluding cloned chunks (i.e. chunks without a descriptor).
251
-# TYPE prometheus_local_storage_memory_chunks gauge
252
-prometheus_local_storage_memory_chunks 1020
253
-# HELP prometheus_local_storage_memory_series The current number of series in memory.
254
-# TYPE prometheus_local_storage_memory_series gauge
255
-prometheus_local_storage_memory_series 424
256
-# HELP prometheus_local_storage_persist_latency_microseconds A summary of latencies for persisting each chunk.
257
-# TYPE prometheus_local_storage_persist_latency_microseconds summary
258
-prometheus_local_storage_persist_latency_microseconds{quantile="0.5"} 30.377
259
-prometheus_local_storage_persist_latency_microseconds{quantile="0.9"} 203.539
260
-prometheus_local_storage_persist_latency_microseconds{quantile="0.99"} 2626.463
261
-prometheus_local_storage_persist_latency_microseconds_sum 20424.415
262
-prometheus_local_storage_persist_latency_microseconds_count 174
263
-# HELP prometheus_local_storage_persist_queue_capacity The total capacity of the persist queue.
264
-# TYPE prometheus_local_storage_persist_queue_capacity gauge
265
-prometheus_local_storage_persist_queue_capacity 1024
266
-# HELP prometheus_local_storage_persist_queue_length The current number of chunks waiting in the persist queue.
267
-# TYPE prometheus_local_storage_persist_queue_length gauge
268
-prometheus_local_storage_persist_queue_length 0
269
-# HELP prometheus_local_storage_series_ops_total The total number of series operations by their type.
270
-# TYPE prometheus_local_storage_series_ops_total counter
271
-prometheus_local_storage_series_ops_total{type="create"} 2
272
-prometheus_local_storage_series_ops_total{type="maintenance_in_memory"} 11
273
-# HELP prometheus_notifications_latency_milliseconds Latency quantiles for sending alert notifications (not including dropped notifications).
274
-# TYPE prometheus_notifications_latency_milliseconds summary
275
-prometheus_notifications_latency_milliseconds{quantile="0.5"} 0
276
-prometheus_notifications_latency_milliseconds{quantile="0.9"} 0
277
-prometheus_notifications_latency_milliseconds{quantile="0.99"} 0
278
-prometheus_notifications_latency_milliseconds_sum 0
279
-prometheus_notifications_latency_milliseconds_count 0
280
-# HELP prometheus_notifications_queue_capacity The capacity of the alert notifications queue.
281
-# TYPE prometheus_notifications_queue_capacity gauge
282
-prometheus_notifications_queue_capacity 100
283
-# HELP prometheus_notifications_queue_length The number of alert notifications in the queue.
284
-# TYPE prometheus_notifications_queue_length gauge
285
-prometheus_notifications_queue_length 0
286
-# HELP prometheus_rule_evaluation_duration_milliseconds The duration for a rule to execute.
287
-# TYPE prometheus_rule_evaluation_duration_milliseconds summary
288
-prometheus_rule_evaluation_duration_milliseconds{rule_type="alerting",quantile="0.5"} 0
289
-prometheus_rule_evaluation_duration_milliseconds{rule_type="alerting",quantile="0.9"} 0
290
-prometheus_rule_evaluation_duration_milliseconds{rule_type="alerting",quantile="0.99"} 2
291
-prometheus_rule_evaluation_duration_milliseconds_sum{rule_type="alerting"} 12
292
-prometheus_rule_evaluation_duration_milliseconds_count{rule_type="alerting"} 115
293
-prometheus_rule_evaluation_duration_milliseconds{rule_type="recording",quantile="0.5"} 0
294
-prometheus_rule_evaluation_duration_milliseconds{rule_type="recording",quantile="0.9"} 0
295
-prometheus_rule_evaluation_duration_milliseconds{rule_type="recording",quantile="0.99"} 3
296
-prometheus_rule_evaluation_duration_milliseconds_sum{rule_type="recording"} 15
297
-prometheus_rule_evaluation_duration_milliseconds_count{rule_type="recording"} 115
298
-# HELP prometheus_rule_evaluation_failures_total The total number of rule evaluation failures.
299
-# TYPE prometheus_rule_evaluation_failures_total counter
300
-prometheus_rule_evaluation_failures_total 0
301
-# HELP prometheus_samples_queue_capacity Capacity of the queue for unwritten samples.
302
-# TYPE prometheus_samples_queue_capacity gauge
303
-prometheus_samples_queue_capacity 4096
304
-# HELP prometheus_samples_queue_length Current number of items in the queue for unwritten samples. Each item comprises all samples exposed by one target as one metric family (i.e. metrics of the same name).
305
-# TYPE prometheus_samples_queue_length gauge
306
-prometheus_samples_queue_length 0
307
-# HELP prometheus_target_interval_length_seconds Actual intervals between scrapes.
308
-# TYPE prometheus_target_interval_length_seconds summary
309
-prometheus_target_interval_length_seconds{interval="15s",quantile="0.01"} 14
310
-prometheus_target_interval_length_seconds{interval="15s",quantile="0.05"} 14
311
-prometheus_target_interval_length_seconds{interval="15s",quantile="0.5"} 15
312
-prometheus_target_interval_length_seconds{interval="15s",quantile="0.9"} 15
313
-prometheus_target_interval_length_seconds{interval="15s",quantile="0.99"} 15
314
-prometheus_target_interval_length_seconds_sum{interval="15s"} 175
315
-prometheus_target_interval_length_seconds_count{interval="15s"} 12
316
-prometheus_target_interval_length_seconds{interval="1s",quantile="0.01"} 0
317
-prometheus_target_interval_length_seconds{interval="1s",quantile="0.05"} 0
318
-prometheus_target_interval_length_seconds{interval="1s",quantile="0.5"} 0
319
-prometheus_target_interval_length_seconds{interval="1s",quantile="0.9"} 1
320
-prometheus_target_interval_length_seconds{interval="1s",quantile="0.99"} 1
321
-prometheus_target_interval_length_seconds_sum{interval="1s"} 55
322
-prometheus_target_interval_length_seconds_count{interval="1s"} 117
Godeps/_workspace/src/github.com/prometheus/client_golang/text/testdata/text.gz
Binary files a/Godeps/_workspace/src/github.com/prometheus/client_golang/text/testdata/text.gz and /dev/null differ
Godeps/_workspace/src/github.com/prometheus/client_model/go/metrics.pb.go
deleted
-364
@@ -1,364 +0,0 @@
1
-// Code generated by protoc-gen-go.
2
-// source: metrics.proto
3
-// DO NOT EDIT!
4
-
5
-/*
6
-Package io_prometheus_client is a generated protocol buffer package.
7
-
8
-It is generated from these files:
9
- metrics.proto
10
-
11
-It has these top-level messages:
12
- LabelPair
13
- Gauge
14
- Counter
15
- Quantile
16
- Summary
17
- Untyped
18
- Histogram
19
- Bucket
20
- Metric
21
- MetricFamily
22
-*/
23
-package io_prometheus_client
24
-
25
-import proto "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/golang/protobuf/proto"
26
-import math "math"
27
-
28
-// Reference imports to suppress errors if they are not otherwise used.
29
-var _ = proto.Marshal
30
-var _ = math.Inf
31
-
32
-type MetricType int32
33
-
34
-const (
35
- MetricType_COUNTER MetricType = 0
36
- MetricType_GAUGE MetricType = 1
37
- MetricType_SUMMARY MetricType = 2
38
- MetricType_UNTYPED MetricType = 3
39
- MetricType_HISTOGRAM MetricType = 4
40
-)
41
-
42
-var MetricType_name = map[int32]string{
43
- 0: "COUNTER",
44
- 1: "GAUGE",
45
- 2: "SUMMARY",
46
- 3: "UNTYPED",
47
- 4: "HISTOGRAM",
48
-}
49
-var MetricType_value = map[string]int32{
50
- "COUNTER": 0,
51
- "GAUGE": 1,
52
- "SUMMARY": 2,
53
- "UNTYPED": 3,
54
- "HISTOGRAM": 4,
55
-}
56
-
57
-func (x MetricType) Enum() *MetricType {
58
- p := new(MetricType)
59
- *p = x
60
- return p
61
-}
62
-func (x MetricType) String() string {
63
- return proto.EnumName(MetricType_name, int32(x))
64
-}
65
-func (x *MetricType) UnmarshalJSON(data []byte) error {
66
- value, err := proto.UnmarshalJSONEnum(MetricType_value, data, "MetricType")
67
- if err != nil {
68
- return err
69
- }
70
- *x = MetricType(value)
71
- return nil
72
-}
73
-
74
-type LabelPair struct {
75
- Name *string `protobuf:"bytes,1,opt,name=name" json:"name,omitempty"`
76
- Value *string `protobuf:"bytes,2,opt,name=value" json:"value,omitempty"`
77
- XXX_unrecognized []byte `json:"-"`
78
-}
79
-
80
-func (m *LabelPair) Reset() { *m = LabelPair{} }
81
-func (m *LabelPair) String() string { return proto.CompactTextString(m) }
82
-func (*LabelPair) ProtoMessage() {}
83
-
84
-func (m *LabelPair) GetName() string {
85
- if m != nil && m.Name != nil {
86
- return *m.Name
87
- }
88
- return ""
89
-}
90
-
91
-func (m *LabelPair) GetValue() string {
92
- if m != nil && m.Value != nil {
93
- return *m.Value
94
- }
95
- return ""
96
-}
97
-
98
-type Gauge struct {
99
- Value *float64 `protobuf:"fixed64,1,opt,name=value" json:"value,omitempty"`
100
- XXX_unrecognized []byte `json:"-"`
101
-}
102
-
103
-func (m *Gauge) Reset() { *m = Gauge{} }
104
-func (m *Gauge) String() string { return proto.CompactTextString(m) }
105
-func (*Gauge) ProtoMessage() {}
106
-
107
-func (m *Gauge) GetValue() float64 {
108
- if m != nil && m.Value != nil {
109
- return *m.Value
110
- }
111
- return 0
112
-}
113
-
114
-type Counter struct {
115
- Value *float64 `protobuf:"fixed64,1,opt,name=value" json:"value,omitempty"`
116
- XXX_unrecognized []byte `json:"-"`
117
-}
118
-
119
-func (m *Counter) Reset() { *m = Counter{} }
120
-func (m *Counter) String() string { return proto.CompactTextString(m) }
121
-func (*Counter) ProtoMessage() {}
122
-
123
-func (m *Counter) GetValue() float64 {
124
- if m != nil && m.Value != nil {
125
- return *m.Value
126
- }
127
- return 0
128
-}
129
-
130
-type Quantile struct {
131
- Quantile *float64 `protobuf:"fixed64,1,opt,name=quantile" json:"quantile,omitempty"`
132
- Value *float64 `protobuf:"fixed64,2,opt,name=value" json:"value,omitempty"`
133
- XXX_unrecognized []byte `json:"-"`
134
-}
135
-
136
-func (m *Quantile) Reset() { *m = Quantile{} }
137
-func (m *Quantile) String() string { return proto.CompactTextString(m) }
138
-func (*Quantile) ProtoMessage() {}
139
-
140
-func (m *Quantile) GetQuantile() float64 {
141
- if m != nil && m.Quantile != nil {
142
- return *m.Quantile
143
- }
144
- return 0
145
-}
146
-
147
-func (m *Quantile) GetValue() float64 {
148
- if m != nil && m.Value != nil {
149
- return *m.Value
150
- }
151
- return 0
152
-}
153
-
154
-type Summary struct {
155
- SampleCount *uint64 `protobuf:"varint,1,opt,name=sample_count" json:"sample_count,omitempty"`
156
- SampleSum *float64 `protobuf:"fixed64,2,opt,name=sample_sum" json:"sample_sum,omitempty"`
157
- Quantile []*Quantile `protobuf:"bytes,3,rep,name=quantile" json:"quantile,omitempty"`
158
- XXX_unrecognized []byte `json:"-"`
159
-}
160
-
161
-func (m *Summary) Reset() { *m = Summary{} }
162
-func (m *Summary) String() string { return proto.CompactTextString(m) }
163
-func (*Summary) ProtoMessage() {}
164
-
165
-func (m *Summary) GetSampleCount() uint64 {
166
- if m != nil && m.SampleCount != nil {
167
- return *m.SampleCount
168
- }
169
- return 0
170
-}
171
-
172
-func (m *Summary) GetSampleSum() float64 {
173
- if m != nil && m.SampleSum != nil {
174
- return *m.SampleSum
175
- }
176
- return 0
177
-}
178
-
179
-func (m *Summary) GetQuantile() []*Quantile {
180
- if m != nil {
181
- return m.Quantile
182
- }
183
- return nil
184
-}
185
-
186
-type Untyped struct {
187
- Value *float64 `protobuf:"fixed64,1,opt,name=value" json:"value,omitempty"`
188
- XXX_unrecognized []byte `json:"-"`
189
-}
190
-
191
-func (m *Untyped) Reset() { *m = Untyped{} }
192
-func (m *Untyped) String() string { return proto.CompactTextString(m) }
193
-func (*Untyped) ProtoMessage() {}
194
-
195
-func (m *Untyped) GetValue() float64 {
196
- if m != nil && m.Value != nil {
197
- return *m.Value
198
- }
199
- return 0
200
-}
201
-
202
-type Histogram struct {
203
- SampleCount *uint64 `protobuf:"varint,1,opt,name=sample_count" json:"sample_count,omitempty"`
204
- SampleSum *float64 `protobuf:"fixed64,2,opt,name=sample_sum" json:"sample_sum,omitempty"`
205
- Bucket []*Bucket `protobuf:"bytes,3,rep,name=bucket" json:"bucket,omitempty"`
206
- XXX_unrecognized []byte `json:"-"`
207
-}
208
-
209
-func (m *Histogram) Reset() { *m = Histogram{} }
210
-func (m *Histogram) String() string { return proto.CompactTextString(m) }
211
-func (*Histogram) ProtoMessage() {}
212
-
213
-func (m *Histogram) GetSampleCount() uint64 {
214
- if m != nil && m.SampleCount != nil {
215
- return *m.SampleCount
216
- }
217
- return 0
218
-}
219
-
220
-func (m *Histogram) GetSampleSum() float64 {
221
- if m != nil && m.SampleSum != nil {
222
- return *m.SampleSum
223
- }
224
- return 0
225
-}
226
-
227
-func (m *Histogram) GetBucket() []*Bucket {
228
- if m != nil {
229
- return m.Bucket
230
- }
231
- return nil
232
-}
233
-
234
-type Bucket struct {
235
- CumulativeCount *uint64 `protobuf:"varint,1,opt,name=cumulative_count" json:"cumulative_count,omitempty"`
236
- UpperBound *float64 `protobuf:"fixed64,2,opt,name=upper_bound" json:"upper_bound,omitempty"`
237
- XXX_unrecognized []byte `json:"-"`
238
-}
239
-
240
-func (m *Bucket) Reset() { *m = Bucket{} }
241
-func (m *Bucket) String() string { return proto.CompactTextString(m) }
242
-func (*Bucket) ProtoMessage() {}
243
-
244
-func (m *Bucket) GetCumulativeCount() uint64 {
245
- if m != nil && m.CumulativeCount != nil {
246
- return *m.CumulativeCount
247
- }
248
- return 0
249
-}
250
-
251
-func (m *Bucket) GetUpperBound() float64 {
252
- if m != nil && m.UpperBound != nil {
253
- return *m.UpperBound
254
- }
255
- return 0
256
-}
257
-
258
-type Metric struct {
259
- Label []*LabelPair `protobuf:"bytes,1,rep,name=label" json:"label,omitempty"`
260
- Gauge *Gauge `protobuf:"bytes,2,opt,name=gauge" json:"gauge,omitempty"`
261
- Counter *Counter `protobuf:"bytes,3,opt,name=counter" json:"counter,omitempty"`
262
- Summary *Summary `protobuf:"bytes,4,opt,name=summary" json:"summary,omitempty"`
263
- Untyped *Untyped `protobuf:"bytes,5,opt,name=untyped" json:"untyped,omitempty"`
264
- Histogram *Histogram `protobuf:"bytes,7,opt,name=histogram" json:"histogram,omitempty"`
265
- TimestampMs *int64 `protobuf:"varint,6,opt,name=timestamp_ms" json:"timestamp_ms,omitempty"`
266
- XXX_unrecognized []byte `json:"-"`
267
-}
268
-
269
-func (m *Metric) Reset() { *m = Metric{} }
270
-func (m *Metric) String() string { return proto.CompactTextString(m) }
271
-func (*Metric) ProtoMessage() {}
272
-
273
-func (m *Metric) GetLabel() []*LabelPair {
274
- if m != nil {
275
- return m.Label
276
- }
277
- return nil
278
-}
279
-
280
-func (m *Metric) GetGauge() *Gauge {
281
- if m != nil {
282
- return m.Gauge
283
- }
284
- return nil
285
-}
286
-
287
-func (m *Metric) GetCounter() *Counter {
288
- if m != nil {
289
- return m.Counter
290
- }
291
- return nil
292
-}
293
-
294
-func (m *Metric) GetSummary() *Summary {
295
- if m != nil {
296
- return m.Summary
297
- }
298
- return nil
299
-}
300
-
301
-func (m *Metric) GetUntyped() *Untyped {
302
- if m != nil {
303
- return m.Untyped
304
- }
305
- return nil
306
-}
307
-
308
-func (m *Metric) GetHistogram() *Histogram {
309
- if m != nil {
310
- return m.Histogram
311
- }
312
- return nil
313
-}
314
-
315
-func (m *Metric) GetTimestampMs() int64 {
316
- if m != nil && m.TimestampMs != nil {
317
- return *m.TimestampMs
318
- }
319
- return 0
320
-}
321
-
322
-type MetricFamily struct {
323
- Name *string `protobuf:"bytes,1,opt,name=name" json:"name,omitempty"`
324
- Help *string `protobuf:"bytes,2,opt,name=help" json:"help,omitempty"`
325
- Type *MetricType `protobuf:"varint,3,opt,name=type,enum=io.prometheus.client.MetricType" json:"type,omitempty"`
326
- Metric []*Metric `protobuf:"bytes,4,rep,name=metric" json:"metric,omitempty"`
327
- XXX_unrecognized []byte `json:"-"`
328
-}
329
-
330
-func (m *MetricFamily) Reset() { *m = MetricFamily{} }
331
-func (m *MetricFamily) String() string { return proto.CompactTextString(m) }
332
-func (*MetricFamily) ProtoMessage() {}
333
-
334
-func (m *MetricFamily) GetName() string {
335
- if m != nil && m.Name != nil {
336
- return *m.Name
337
- }
338
- return ""
339
-}
340
-
341
-func (m *MetricFamily) GetHelp() string {
342
- if m != nil && m.Help != nil {
343
- return *m.Help
344
- }
345
- return ""
346
-}
347
-
348
-func (m *MetricFamily) GetType() MetricType {
349
- if m != nil && m.Type != nil {
350
- return *m.Type
351
- }
352
- return MetricType_COUNTER
353
-}
354
-
355
-func (m *MetricFamily) GetMetric() []*Metric {
356
- if m != nil {
357
- return m.Metric
358
- }
359
- return nil
360
-}
361
-
362
-func init() {
363
- proto.RegisterEnum("io.prometheus.client.MetricType", MetricType_name, MetricType_value)
364
-}
Godeps/_workspace/src/github.com/prometheus/procfs/.travis.yml
deleted
-5
@@ -1,5 +0,0 @@
1
-language: go
2
-go:
3
- - 1.3
4
- - 1.4
5
- - tip
Godeps/_workspace/src/github.com/prometheus/procfs/AUTHORS.md
deleted
-12
@@ -1,12 +0,0 @@
1
-The Prometheus project was started by Matt T. Proud (emeritus) and
2
-Julius Volz in 2012.
3
-
4
-Maintainers of this repository:
5
-
6
-* Tobias Schmidt <ts@soundcloud.com>
7
-
8
-The following individuals have contributed code to this repository
9
-(listed in alphabetical order):
10
-
11
-* Ji-Hoon, Seol <jihoon.seol@gmail.com>
12
-* Tobias Schmidt <tobidt@gmail.com>
Godeps/_workspace/src/github.com/prometheus/procfs/CONTRIBUTING.md
deleted
-18
@@ -1,18 +0,0 @@
1
-# Contributing
2
-
3
-Prometheus uses GitHub to manage reviews of pull requests.
4
-
5
-* If you have a trivial fix or improvement, go ahead and create a pull
6
- request, addressing (with `@...`) one or more of the maintainers
7
- (see [AUTHORS.md](AUTHORS.md)) in the description of the pull request.
8
-
9
-* If you plan to do something more involved, first discuss your ideas
10
- on our [mailing list](https://groups.google.com/forum/?fromgroups#!forum/prometheus-developers).
11
- This will avoid unnecessary work and surely give you and us a good deal
12
- of inspiration.
13
-
14
-* Relevant coding style guidelines are the [Go Code Review
15
- Comments](https://code.google.com/p/go-wiki/wiki/CodeReviewComments)
16
- and the _Formatting and style_ section of Peter Bourgon's [Go: Best
17
- Practices for Production
18
- Environments](http://peter.bourgon.org/go-in-production/#formatting-and-style).
Godeps/_workspace/src/github.com/prometheus/procfs/LICENSE
deleted
-201
@@ -1,201 +0,0 @@
1
- Apache License
2
- Version 2.0, January 2004
3
- http://www.apache.org/licenses/
4
-
5
- TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
-
7
- 1. Definitions.
8
-
9
- "License" shall mean the terms and conditions for use, reproduction,
10
- and distribution as defined by Sections 1 through 9 of this document.
11
-
12
- "Licensor" shall mean the copyright owner or entity authorized by
13
- the copyright owner that is granting the License.
14
-
15
- "Legal Entity" shall mean the union of the acting entity and all
16
- other entities that control, are controlled by, or are under common
17
- control with that entity. For the purposes of this definition,
18
- "control" means (i) the power, direct or indirect, to cause the
19
- direction or management of such entity, whether by contract or
20
- otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
- outstanding shares, or (iii) beneficial ownership of such entity.
22
-
23
- "You" (or "Your") shall mean an individual or Legal Entity
24
- exercising permissions granted by this License.
25
-
26
- "Source" form shall mean the preferred form for making modifications,
27
- including but not limited to software source code, documentation
28
- source, and configuration files.
29
-
30
- "Object" form shall mean any form resulting from mechanical
31
- transformation or translation of a Source form, including but
32
- not limited to compiled object code, generated documentation,
33
- and conversions to other media types.
34
-
35
- "Work" shall mean the work of authorship, whether in Source or
36
- Object form, made available under the License, as indicated by a
37
- copyright notice that is included in or attached to the work
38
- (an example is provided in the Appendix below).
39
-
40
- "Derivative Works" shall mean any work, whether in Source or Object
41
- form, that is based on (or derived from) the Work and for which the
42
- editorial revisions, annotations, elaborations, or other modifications
43
- represent, as a whole, an original work of authorship. For the purposes
44
- of this License, Derivative Works shall not include works that remain
45
- separable from, or merely link (or bind by name) to the interfaces of,
46
- the Work and Derivative Works thereof.
47
-
48
- "Contribution" shall mean any work of authorship, including
49
- the original version of the Work and any modifications or additions
50
- to that Work or Derivative Works thereof, that is intentionally
51
- submitted to Licensor for inclusion in the Work by the copyright owner
52
- or by an individual or Legal Entity authorized to submit on behalf of
53
- the copyright owner. For the purposes of this definition, "submitted"
54
- means any form of electronic, verbal, or written communication sent
55
- to the Licensor or its representatives, including but not limited to
56
- communication on electronic mailing lists, source code control systems,
57
- and issue tracking systems that are managed by, or on behalf of, the
58
- Licensor for the purpose of discussing and improving the Work, but
59
- excluding communication that is conspicuously marked or otherwise
60
- designated in writing by the copyright owner as "Not a Contribution."
61
-
62
- "Contributor" shall mean Licensor and any individual or Legal Entity
63
- on behalf of whom a Contribution has been received by Licensor and
64
- subsequently incorporated within the Work.
65
-
66
- 2. Grant of Copyright License. Subject to the terms and conditions of
67
- this License, each Contributor hereby grants to You a perpetual,
68
- worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
- copyright license to reproduce, prepare Derivative Works of,
70
- publicly display, publicly perform, sublicense, and distribute the
71
- Work and such Derivative Works in Source or Object form.
72
-
73
- 3. Grant of Patent License. Subject to the terms and conditions of
74
- this License, each Contributor hereby grants to You a perpetual,
75
- worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
- (except as stated in this section) patent license to make, have made,
77
- use, offer to sell, sell, import, and otherwise transfer the Work,
78
- where such license applies only to those patent claims licensable
79
- by such Contributor that are necessarily infringed by their
80
- Contribution(s) alone or by combination of their Contribution(s)
81
- with the Work to which such Contribution(s) was submitted. If You
82
- institute patent litigation against any entity (including a
83
- cross-claim or counterclaim in a lawsuit) alleging that the Work
84
- or a Contribution incorporated within the Work constitutes direct
85
- or contributory patent infringement, then any patent licenses
86
- granted to You under this License for that Work shall terminate
87
- as of the date such litigation is filed.
88
-
89
- 4. Redistribution. You may reproduce and distribute copies of the
90
- Work or Derivative Works thereof in any medium, with or without
91
- modifications, and in Source or Object form, provided that You
92
- meet the following conditions:
93
-
94
- (a) You must give any other recipients of the Work or
95
- Derivative Works a copy of this License; and
96
-
97
- (b) You must cause any modified files to carry prominent notices
98
- stating that You changed the files; and
99
-
100
- (c) You must retain, in the Source form of any Derivative Works
101
- that You distribute, all copyright, patent, trademark, and
102
- attribution notices from the Source form of the Work,
103
- excluding those notices that do not pertain to any part of
104
- the Derivative Works; and
105
-
106
- (d) If the Work includes a "NOTICE" text file as part of its
107
- distribution, then any Derivative Works that You distribute must
108
- include a readable copy of the attribution notices contained
109
- within such NOTICE file, excluding those notices that do not
110
- pertain to any part of the Derivative Works, in at least one
111
- of the following places: within a NOTICE text file distributed
112
- as part of the Derivative Works; within the Source form or
113
- documentation, if provided along with the Derivative Works; or,
114
- within a display generated by the Derivative Works, if and
115
- wherever such third-party notices normally appear. The contents
116
- of the NOTICE file are for informational purposes only and
117
- do not modify the License. You may add Your own attribution
118
- notices within Derivative Works that You distribute, alongside
119
- or as an addendum to the NOTICE text from the Work, provided
120
- that such additional attribution notices cannot be construed
121
- as modifying the License.
122
-
123
- You may add Your own copyright statement to Your modifications and
124
- may provide additional or different license terms and conditions
125
- for use, reproduction, or distribution of Your modifications, or
126
- for any such Derivative Works as a whole, provided Your use,
127
- reproduction, and distribution of the Work otherwise complies with
128
- the conditions stated in this License.
129
-
130
- 5. Submission of Contributions. Unless You explicitly state otherwise,
131
- any Contribution intentionally submitted for inclusion in the Work
132
- by You to the Licensor shall be under the terms and conditions of
133
- this License, without any additional terms or conditions.
134
- Notwithstanding the above, nothing herein shall supersede or modify
135
- the terms of any separate license agreement you may have executed
136
- with Licensor regarding such Contributions.
137
-
138
- 6. Trademarks. This License does not grant permission to use the trade
139
- names, trademarks, service marks, or product names of the Licensor,
140
- except as required for reasonable and customary use in describing the
141
- origin of the Work and reproducing the content of the NOTICE file.
142
-
143
- 7. Disclaimer of Warranty. Unless required by applicable law or
144
- agreed to in writing, Licensor provides the Work (and each
145
- Contributor provides its Contributions) on an "AS IS" BASIS,
146
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
- implied, including, without limitation, any warranties or conditions
148
- of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
- PARTICULAR PURPOSE. You are solely responsible for determining the
150
- appropriateness of using or redistributing the Work and assume any
151
- risks associated with Your exercise of permissions under this License.
152
-
153
- 8. Limitation of Liability. In no event and under no legal theory,
154
- whether in tort (including negligence), contract, or otherwise,
155
- unless required by applicable law (such as deliberate and grossly
156
- negligent acts) or agreed to in writing, shall any Contributor be
157
- liable to You for damages, including any direct, indirect, special,
158
- incidental, or consequential damages of any character arising as a
159
- result of this License or out of the use or inability to use the
160
- Work (including but not limited to damages for loss of goodwill,
161
- work stoppage, computer failure or malfunction, or any and all
162
- other commercial damages or losses), even if such Contributor
163
- has been advised of the possibility of such damages.
164
-
165
- 9. Accepting Warranty or Additional Liability. While redistributing
166
- the Work or Derivative Works thereof, You may choose to offer,
167
- and charge a fee for, acceptance of support, warranty, indemnity,
168
- or other liability obligations and/or rights consistent with this
169
- License. However, in accepting such obligations, You may act only
170
- on Your own behalf and on Your sole responsibility, not on behalf
171
- of any other Contributor, and only if You agree to indemnify,
172
- defend, and hold each Contributor harmless for any liability
173
- incurred by, or claims asserted against, such Contributor by reason
174
- of your accepting any such warranty or additional liability.
175
-
176
- END OF TERMS AND CONDITIONS
177
-
178
- APPENDIX: How to apply the Apache License to your work.
179
-
180
- To apply the Apache License to your work, attach the following
181
- boilerplate notice, with the fields enclosed by brackets "[]"
182
- replaced with your own identifying information. (Don't include
183
- the brackets!) The text should be enclosed in the appropriate
184
- comment syntax for the file format. We also recommend that a
185
- file or class name and description of purpose be included on the
186
- same "printed page" as the copyright notice for easier
187
- identification within third-party archives.
188
-
189
- Copyright [yyyy] [name of copyright owner]
190
-
191
- Licensed under the Apache License, Version 2.0 (the "License");
192
- you may not use this file except in compliance with the License.
193
- You may obtain a copy of the License at
194
-
195
- http://www.apache.org/licenses/LICENSE-2.0
196
-
197
- Unless required by applicable law or agreed to in writing, software
198
- distributed under the License is distributed on an "AS IS" BASIS,
199
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
- See the License for the specific language governing permissions and
201
- limitations under the License.
Godeps/_workspace/src/github.com/prometheus/procfs/NOTICE
deleted
-7
@@ -1,7 +0,0 @@
1
-procfs provides functions to retrieve system, kernel and process
2
-metrics from the pseudo-filesystem proc.
3
-
4
-Copyright 2014-2015 The Prometheus Authors
5
-
6
-This product includes software developed at
7
-SoundCloud Ltd. (http://soundcloud.com/).
Godeps/_workspace/src/github.com/prometheus/procfs/README.md
deleted
-10
@@ -1,10 +0,0 @@
1
-# procfs
2
-
3
-This procfs package provides functions to retrieve system, kernel and process
4
-metrics from the pseudo-filesystem proc.
5
-
6
-*WARNING*: This package is a work in progress. Its API may still break in
7
-backwards-incompatible ways without warnings. Use it at your own risk.
8
-
9
-[](https://godoc.org/github.com/prometheus/procfs)
10
-[](https://travis-ci.org/prometheus/procfs)
Godeps/_workspace/src/github.com/prometheus/procfs/doc.go
deleted
-45
@@ -1,45 +0,0 @@
1
-// Copyright 2014 Prometheus Team
2
-// Licensed under the Apache License, Version 2.0 (the "License");
3
-// you may not use this file except in compliance with the License.
4
-// You may obtain a copy of the License at
5
-//
6
-// http://www.apache.org/licenses/LICENSE-2.0
7
-//
8
-// Unless required by applicable law or agreed to in writing, software
9
-// distributed under the License is distributed on an "AS IS" BASIS,
10
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
-// See the License for the specific language governing permissions and
12
-// limitations under the License.
13
-
14
-// Package procfs provides functions to retrieve system, kernel and process
15
-// metrics from the pseudo-filesystem proc.
16
-//
17
-// Example:
18
-//
19
-// package main
20
-//
21
-// import (
22
-// "fmt"
23
-// "log"
24
-//
25
-// "github.com/prometheus/procfs"
26
-// )
27
-//
28
-// func main() {
29
-// p, err := procfs.Self()
30
-// if err != nil {
31
-// log.Fatalf("could not get process: %s", err)
32
-// }
33
-//
34
-// stat, err := p.NewStat()
35
-// if err != nil {
36
-// log.Fatalf("could not get process stat: %s", err)
37
-// }
38
-//
39
-// fmt.Printf("command: %s\n", stat.Comm)
40
-// fmt.Printf("cpu time: %fs\n", stat.CPUTime())
41
-// fmt.Printf("vsize: %dB\n", stat.VirtualMemory())
42
-// fmt.Printf("rss: %dB\n", stat.ResidentMemory())
43
-// }
44
-//
45
-package procfs
Godeps/_workspace/src/github.com/prometheus/procfs/fixtures/26231/cmdline
Binary files a/Godeps/_workspace/src/github.com/prometheus/procfs/fixtures/26231/cmdline and /dev/null differ
Godeps/_workspace/src/github.com/prometheus/procfs/fixtures/26231/exe
deleted
-1
@@ -1 +0,0 @@
1
-/usr/bin/vim
\ No newline at end of file
Godeps/_workspace/src/github.com/prometheus/procfs/fixtures/26231/fd/0
deleted
-1
@@ -1 +0,0 @@
1
-../../symlinktargets/abc
\ No newline at end of file
Godeps/_workspace/src/github.com/prometheus/procfs/fixtures/26231/fd/1
deleted
-1
@@ -1 +0,0 @@
1
-../../symlinktargets/def
\ No newline at end of file
Godeps/_workspace/src/github.com/prometheus/procfs/fixtures/26231/fd/10
deleted
-1
@@ -1 +0,0 @@
1
-../../symlinktargets/xyz
\ No newline at end of file
Godeps/_workspace/src/github.com/prometheus/procfs/fixtures/26231/fd/2
deleted
-1
@@ -1 +0,0 @@
1
-../../symlinktargets/ghi
\ No newline at end of file
Godeps/_workspace/src/github.com/prometheus/procfs/fixtures/26231/fd/3
deleted
-1
@@ -1 +0,0 @@
1
-../../symlinktargets/uvw
\ No newline at end of file
Godeps/_workspace/src/github.com/prometheus/procfs/fixtures/26231/io
deleted
-7
@@ -1,7 +0,0 @@
1
-rchar: 750339
2
-wchar: 818609
3
-syscr: 7405
4
-syscw: 5245
5
-read_bytes: 1024
6
-write_bytes: 2048
7
-cancelled_write_bytes: -1024
Godeps/_workspace/src/github.com/prometheus/procfs/fixtures/26231/limits
deleted
-17
@@ -1,17 +0,0 @@
1
-Limit Soft Limit Hard Limit Units
2
-Max cpu time unlimited unlimited seconds
3
-Max file size unlimited unlimited bytes
4
-Max data size unlimited unlimited bytes
5
-Max stack size 8388608 unlimited bytes
6
-Max core file size 0 unlimited bytes
7
-Max resident set unlimited unlimited bytes
8
-Max processes 62898 62898 processes
9
-Max open files 2048 4096 files
10
-Max locked memory 65536 65536 bytes
11
-Max address space unlimited unlimited bytes
12
-Max file locks unlimited unlimited locks
13
-Max pending signals 62898 62898 signals
14
-Max msgqueue size 819200 819200 bytes
15
-Max nice priority 0 0
16
-Max realtime priority 0 0
17
-Max realtime timeout unlimited unlimited us
Godeps/_workspace/src/github.com/prometheus/procfs/fixtures/26231/stat
deleted
-1
@@ -1 +0,0 @@
1
-26231 (vim) R 5392 7446 5392 34835 7446 4218880 32533 309516 26 82 1677 44 158 99 20 0 1 0 82375 56274944 1981 18446744073709551615 4194304 6294284 140736914091744 140736914087944 139965136429984 0 0 12288 1870679807 0 0 0 17 0 0 0 31 0 0 8391624 8481048 16420864 140736914093252 140736914093279 140736914093279 140736914096107 0
Godeps/_workspace/src/github.com/prometheus/procfs/fixtures/26232/cmdline
Godeps/_workspace/src/github.com/prometheus/procfs/fixtures/26232/fd/0
deleted
-1
@@ -1 +0,0 @@
1
-../../symlinktargets/abc
\ No newline at end of file
Godeps/_workspace/src/github.com/prometheus/procfs/fixtures/26232/fd/1
deleted
-1
@@ -1 +0,0 @@
1
-../../symlinktargets/def
\ No newline at end of file
Godeps/_workspace/src/github.com/prometheus/procfs/fixtures/26232/fd/2
deleted
-1
@@ -1 +0,0 @@
1
-../../symlinktargets/ghi
\ No newline at end of file
Godeps/_workspace/src/github.com/prometheus/procfs/fixtures/26232/fd/3
deleted
-1
@@ -1 +0,0 @@
1
-../../symlinktargets/uvw
\ No newline at end of file
Godeps/_workspace/src/github.com/prometheus/procfs/fixtures/26232/fd/4
deleted
-1
@@ -1 +0,0 @@
1
-../../symlinktargets/xyz
\ No newline at end of file
Godeps/_workspace/src/github.com/prometheus/procfs/fixtures/26232/limits
deleted
-17
@@ -1,17 +0,0 @@
1
-Limit Soft Limit Hard Limit Units
2
-Max cpu time unlimited unlimited seconds
3
-Max file size unlimited unlimited bytes
4
-Max data size unlimited unlimited bytes
5
-Max stack size 8388608 unlimited bytes
6
-Max core file size 0 unlimited bytes
7
-Max resident set unlimited unlimited bytes
8
-Max processes 29436 29436 processes
9
-Max open files 1024 4096 files
10
-Max locked memory 65536 65536 bytes
11
-Max address space unlimited unlimited bytes
12
-Max file locks unlimited unlimited locks
13
-Max pending signals 29436 29436 signals
14
-Max msgqueue size 819200 819200 bytes
15
-Max nice priority 0 0
16
-Max realtime priority 0 0
17
-Max realtime timeout unlimited unlimited us
Godeps/_workspace/src/github.com/prometheus/procfs/fixtures/26232/stat
deleted
-1
@@ -1 +0,0 @@
1
-33 (ata_sff) S 2 0 0 0 -1 69238880 0 0 0 0 0 0 0 0 0 -20 1 0 5 0 0 18446744073709551615 0 0 0 0 0 0 0 2147483647 0 18446744073709551615 0 0 17 1 0 0 0 0 0 0 0 0 0 0 0 0 0
Godeps/_workspace/src/github.com/prometheus/procfs/fixtures/584/stat
deleted
-2
@@ -1,2 +0,0 @@
1
-1020 ((a b ) ( c d) ) R 28378 1020 28378 34842 1020 4218880 286 0 0 0 0 0 0 0 20 0 1 0 10839175 10395648 155 18446744073709551615 4194304 4238788 140736466511168 140736466511168 140609271124624 0 0 0 0 0 0 0 17 5 0 0 0 0 0 6336016 6337300 25579520 140736466515030 140736466515061 140736466515061 140736466518002 0
2
-#!/bin/cat /proc/self/stat
Godeps/_workspace/src/github.com/prometheus/procfs/fixtures/net/ip_vs
deleted
-14
@@ -1,14 +0,0 @@
1
-IP Virtual Server version 1.2.1 (size=4096)
2
-Prot LocalAddress:Port Scheduler Flags
3
- -> RemoteAddress:Port Forward Weight ActiveConn InActConn
4
-TCP C0A80016:0CEA wlc
5
- -> C0A85216:0CEA Tunnel 100 248 2
6
- -> C0A85318:0CEA Tunnel 100 248 2
7
- -> C0A85315:0CEA Tunnel 100 248 1
8
-TCP C0A80039:0CEA wlc
9
- -> C0A85416:0CEA Tunnel 0 0 0
10
- -> C0A85215:0CEA Tunnel 100 1499 0
11
- -> C0A83215:0CEA Tunnel 100 1498 0
12
-TCP C0A80037:0CEA wlc
13
- -> C0A8321A:0CEA Tunnel 0 0 0
14
- -> C0A83120:0CEA Tunnel 100 0 0
Godeps/_workspace/src/github.com/prometheus/procfs/fixtures/net/ip_vs_stats
deleted
-6
@@ -1,6 +0,0 @@
1
- Total Incoming Outgoing Incoming Outgoing
2
- Conns Packets Packets Bytes Bytes
3
- 16AA370 E33656E5 0 51D8C8883AB3 0
4
-
5
- Conns/s Pkts/s Pkts/s Bytes/s Bytes/s
6
- 4 1FB3C 0 1282A8F 0
Godeps/_workspace/src/github.com/prometheus/procfs/fixtures/stat
deleted
-16
@@ -1,16 +0,0 @@
1
-cpu 301854 612 111922 8979004 3552 2 3944 0 0 0
2
-cpu0 44490 19 21045 1087069 220 1 3410 0 0 0
3
-cpu1 47869 23 16474 1110787 591 0 46 0 0 0
4
-cpu2 46504 36 15916 1112321 441 0 326 0 0 0
5
-cpu3 47054 102 15683 1113230 533 0 60 0 0 0
6
-cpu4 28413 25 10776 1140321 217 0 8 0 0 0
7
-cpu5 29271 101 11586 1136270 672 0 30 0 0 0
8
-cpu6 29152 36 10276 1139721 319 0 29 0 0 0
9
-cpu7 29098 268 10164 1139282 555 0 31 0 0 0
10
-intr 8885917 17 0 0 0 0 0 0 0 1 79281 0 0 0 0 0 0 0 231237 0 0 0 0 250586 103 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 223424 190745 13 906 1283803 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
11
-ctxt 38014093
12
-btime 1418183276
13
-processes 26442
14
-procs_running 2
15
-procs_blocked 0
16
-softirq 5057579 250191 1481983 1647 211099 186066 0 1783454 622196 12499 508444
Godeps/_workspace/src/github.com/prometheus/procfs/fixtures/symlinktargets/README
deleted
-2
@@ -1,2 +0,0 @@
1
-This directory contains some empty files that are the symlinks the files in the "fd" directory point to.
2
-They are otherwise ignored by the tests
Godeps/_workspace/src/github.com/prometheus/procfs/fixtures/symlinktargets/abc
Godeps/_workspace/src/github.com/prometheus/procfs/fixtures/symlinktargets/def
Godeps/_workspace/src/github.com/prometheus/procfs/fixtures/symlinktargets/ghi
Godeps/_workspace/src/github.com/prometheus/procfs/fixtures/symlinktargets/uvw
Godeps/_workspace/src/github.com/prometheus/procfs/fixtures/symlinktargets/xyz
Godeps/_workspace/src/github.com/prometheus/procfs/fs.go
deleted
-40
@@ -1,40 +0,0 @@
1
-package procfs
2
-
3
-import (
4
- "fmt"
5
- "os"
6
- "path"
7
-)
8
-
9
-// FS represents the pseudo-filesystem proc, which provides an interface to
10
-// kernel data structures.
11
-type FS string
12
-
13
-// DefaultMountPoint is the common mount point of the proc filesystem.
14
-const DefaultMountPoint = "/proc"
15
-
16
-// NewFS returns a new FS mounted under the given mountPoint. It will error
17
-// if the mount point can't be read.
18
-func NewFS(mountPoint string) (FS, error) {
19
- info, err := os.Stat(mountPoint)
20
- if err != nil {
21
- return "", fmt.Errorf("could not read %s: %s", mountPoint, err)
22
- }
23
- if !info.IsDir() {
24
- return "", fmt.Errorf("mount point %s is not a directory", mountPoint)
25
- }
26
-
27
- return FS(mountPoint), nil
28
-}
29
-
30
-func (fs FS) stat(p string) (os.FileInfo, error) {
31
- return os.Stat(path.Join(string(fs), p))
32
-}
33
-
34
-func (fs FS) open(p string) (*os.File, error) {
35
- return os.Open(path.Join(string(fs), p))
36
-}
37
-
38
-func (fs FS) readlink(p string) (string, error) {
39
- return os.Readlink(path.Join(string(fs), p))
40
-}
Godeps/_workspace/src/github.com/prometheus/procfs/fs_test.go
deleted
-13
@@ -1,13 +0,0 @@
1
-package procfs
2
-
3
-import "testing"
4
-
5
-func TestNewFS(t *testing.T) {
6
- if _, err := NewFS("foobar"); err == nil {
7
- t.Error("want NewFS to fail for non-existing mount point")
8
- }
9
-
10
- if _, err := NewFS("procfs.go"); err == nil {
11
- t.Error("want NewFS to fail if mount point is not a directory")
12
- }
13
-}
Godeps/_workspace/src/github.com/prometheus/procfs/ipvs.go
deleted
-223
@@ -1,223 +0,0 @@
1
-package procfs
2
-
3
-import (
4
- "bufio"
5
- "encoding/hex"
6
- "errors"
7
- "fmt"
8
- "io"
9
- "io/ioutil"
10
- "net"
11
- "strconv"
12
- "strings"
13
-)
14
-
15
-// IPVSStats holds IPVS statistics, as exposed by the kernel in `/proc/net/ip_vs_stats`.
16
-type IPVSStats struct {
17
- // Total count of connections.
18
- Connections uint64
19
- // Total incoming packages processed.
20
- IncomingPackets uint64
21
- // Total outgoing packages processed.
22
- OutgoingPackets uint64
23
- // Total incoming traffic.
24
- IncomingBytes uint64
25
- // Total outgoing traffic.
26
- OutgoingBytes uint64
27
-}
28
-
29
-// IPVSBackendStatus holds current metrics of one virtual / real address pair.
30
-type IPVSBackendStatus struct {
31
- // The local (virtual) IP address.
32
- LocalAddress net.IP
33
- // The local (virtual) port.
34
- LocalPort uint16
35
- // The transport protocol (TCP, UDP).
36
- Proto string
37
- // The remote (real) IP address.
38
- RemoteAddress net.IP
39
- // The remote (real) port.
40
- RemotePort uint16
41
- // The current number of active connections for this virtual/real address pair.
42
- ActiveConn uint64
43
- // The current number of inactive connections for this virtual/real address pair.
44
- InactConn uint64
45
- // The current weight of this virtual/real address pair.
46
- Weight uint64
47
-}
48
-
49
-// NewIPVSStats reads the IPVS statistics.
50
-func NewIPVSStats() (IPVSStats, error) {
51
- fs, err := NewFS(DefaultMountPoint)
52
- if err != nil {
53
- return IPVSStats{}, err
54
- }
55
-
56
- return fs.NewIPVSStats()
57
-}
58
-
59
-// NewIPVSStats reads the IPVS statistics from the specified `proc` filesystem.
60
-func (fs FS) NewIPVSStats() (IPVSStats, error) {
61
- file, err := fs.open("net/ip_vs_stats")
62
- if err != nil {
63
- return IPVSStats{}, err
64
- }
65
- defer file.Close()
66
-
67
- return parseIPVSStats(file)
68
-}
69
-
70
-// parseIPVSStats performs the actual parsing of `ip_vs_stats`.
71
-func parseIPVSStats(file io.Reader) (IPVSStats, error) {
72
- var (
73
- statContent []byte
74
- statLines []string
75
- statFields []string
76
- stats IPVSStats
77
- )
78
-
79
- statContent, err := ioutil.ReadAll(file)
80
- if err != nil {
81
- return IPVSStats{}, err
82
- }
83
-
84
- statLines = strings.SplitN(string(statContent), "\n", 4)
85
- if len(statLines) != 4 {
86
- return IPVSStats{}, errors.New("ip_vs_stats corrupt: too short")
87
- }
88
-
89
- statFields = strings.Fields(statLines[2])
90
- if len(statFields) != 5 {
91
- return IPVSStats{}, errors.New("ip_vs_stats corrupt: unexpected number of fields")
92
- }
93
-
94
- stats.Connections, err = strconv.ParseUint(statFields[0], 16, 64)
95
- if err != nil {
96
- return IPVSStats{}, err
97
- }
98
- stats.IncomingPackets, err = strconv.ParseUint(statFields[1], 16, 64)
99
- if err != nil {
100
- return IPVSStats{}, err
101
- }
102
- stats.OutgoingPackets, err = strconv.ParseUint(statFields[2], 16, 64)
103
- if err != nil {
104
- return IPVSStats{}, err
105
- }
106
- stats.IncomingBytes, err = strconv.ParseUint(statFields[3], 16, 64)
107
- if err != nil {
108
- return IPVSStats{}, err
109
- }
110
- stats.OutgoingBytes, err = strconv.ParseUint(statFields[4], 16, 64)
111
- if err != nil {
112
- return IPVSStats{}, err
113
- }
114
-
115
- return stats, nil
116
-}
117
-
118
-// NewIPVSBackendStatus reads and returns the status of all (virtual,real) server pairs.
119
-func NewIPVSBackendStatus() ([]IPVSBackendStatus, error) {
120
- fs, err := NewFS(DefaultMountPoint)
121
- if err != nil {
122
- return []IPVSBackendStatus{}, err
123
- }
124
-
125
- return fs.NewIPVSBackendStatus()
126
-}
127
-
128
-// NewIPVSBackendStatus reads and returns the status of all (virtual,real) server pairs from the specified `proc` filesystem.
129
-func (fs FS) NewIPVSBackendStatus() ([]IPVSBackendStatus, error) {
130
- file, err := fs.open("net/ip_vs")
131
- if err != nil {
132
- return nil, err
133
- }
134
- defer file.Close()
135
-
136
- return parseIPVSBackendStatus(file)
137
-}
138
-
139
-func parseIPVSBackendStatus(file io.Reader) ([]IPVSBackendStatus, error) {
140
- var (
141
- status []IPVSBackendStatus
142
- scanner = bufio.NewScanner(file)
143
- proto string
144
- localAddress net.IP
145
- localPort uint16
146
- err error
147
- )
148
-
149
- for scanner.Scan() {
150
- fields := strings.Fields(string(scanner.Text()))
151
- if len(fields) == 0 {
152
- continue
153
- }
154
- switch {
155
- case fields[0] == "IP" || fields[0] == "Prot" || fields[1] == "RemoteAddress:Port":
156
- continue
157
- case fields[0] == "TCP" || fields[0] == "UDP":
158
- if len(fields) < 2 {
159
- continue
160
- }
161
- proto = fields[0]
162
- localAddress, localPort, err = parseIPPort(fields[1])
163
- if err != nil {
164
- return nil, err
165
- }
166
- case fields[0] == "->":
167
- if len(fields) < 6 {
168
- continue
169
- }
170
- remoteAddress, remotePort, err := parseIPPort(fields[1])
171
- if err != nil {
172
- return nil, err
173
- }
174
- weight, err := strconv.ParseUint(fields[3], 10, 64)
175
- if err != nil {
176
- return nil, err
177
- }
178
- activeConn, err := strconv.ParseUint(fields[4], 10, 64)
179
- if err != nil {
180
- return nil, err
181
- }
182
- inactConn, err := strconv.ParseUint(fields[5], 10, 64)
183
- if err != nil {
184
- return nil, err
185
- }
186
- status = append(status, IPVSBackendStatus{
187
- LocalAddress: localAddress,
188
- LocalPort: localPort,
189
- RemoteAddress: remoteAddress,
190
- RemotePort: remotePort,
191
- Proto: proto,
192
- Weight: weight,
193
- ActiveConn: activeConn,
194
- InactConn: inactConn,
195
- })
196
- }
197
- }
198
- return status, nil
199
-}
200
-
201
-func parseIPPort(s string) (net.IP, uint16, error) {
202
- tmp := strings.SplitN(s, ":", 2)
203
-
204
- if len(tmp) != 2 {
205
- return nil, 0, fmt.Errorf("invalid IP:Port: %s", s)
206
- }
207
-
208
- if len(tmp[0]) != 8 && len(tmp[0]) != 32 {
209
- return nil, 0, fmt.Errorf("invalid IP: %s", tmp[0])
210
- }
211
-
212
- ip, err := hex.DecodeString(tmp[0])
213
- if err != nil {
214
- return nil, 0, err
215
- }
216
-
217
- port, err := strconv.ParseUint(tmp[1], 16, 16)
218
- if err != nil {
219
- return nil, 0, err
220
- }
221
-
222
- return ip, uint16(port), nil
223
-}
Godeps/_workspace/src/github.com/prometheus/procfs/ipvs_test.go
deleted
-196
@@ -1,196 +0,0 @@
1
-package procfs
2
-
3
-import (
4
- "net"
5
- "testing"
6
-)
7
-
8
-var (
9
- expectedIPVSStats = IPVSStats{
10
- Connections: 23765872,
11
- IncomingPackets: 3811989221,
12
- OutgoingPackets: 0,
13
- IncomingBytes: 89991519156915,
14
- OutgoingBytes: 0,
15
- }
16
- expectedIPVSBackendStatuses = []IPVSBackendStatus{
17
- IPVSBackendStatus{
18
- LocalAddress: net.ParseIP("192.168.0.22"),
19
- LocalPort: 3306,
20
- RemoteAddress: net.ParseIP("192.168.82.22"),
21
- RemotePort: 3306,
22
- Proto: "TCP",
23
- Weight: 100,
24
- ActiveConn: 248,
25
- InactConn: 2,
26
- },
27
- IPVSBackendStatus{
28
- LocalAddress: net.ParseIP("192.168.0.22"),
29
- LocalPort: 3306,
30
- RemoteAddress: net.ParseIP("192.168.83.24"),
31
- RemotePort: 3306,
32
- Proto: "TCP",
33
- Weight: 100,
34
- ActiveConn: 248,
35
- InactConn: 2,
36
- },
37
- IPVSBackendStatus{
38
- LocalAddress: net.ParseIP("192.168.0.22"),
39
- LocalPort: 3306,
40
- RemoteAddress: net.ParseIP("192.168.83.21"),
41
- RemotePort: 3306,
42
- Proto: "TCP",
43
- Weight: 100,
44
- ActiveConn: 248,
45
- InactConn: 1,
46
- },
47
- IPVSBackendStatus{
48
- LocalAddress: net.ParseIP("192.168.0.57"),
49
- LocalPort: 3306,
50
- RemoteAddress: net.ParseIP("192.168.84.22"),
51
- RemotePort: 3306,
52
- Proto: "TCP",
53
- Weight: 0,
54
- ActiveConn: 0,
55
- InactConn: 0,
56
- },
57
- IPVSBackendStatus{
58
- LocalAddress: net.ParseIP("192.168.0.57"),
59
- LocalPort: 3306,
60
- RemoteAddress: net.ParseIP("192.168.82.21"),
61
- RemotePort: 3306,
62
- Proto: "TCP",
63
- Weight: 100,
64
- ActiveConn: 1499,
65
- InactConn: 0,
66
- },
67
- IPVSBackendStatus{
68
- LocalAddress: net.ParseIP("192.168.0.57"),
69
- LocalPort: 3306,
70
- RemoteAddress: net.ParseIP("192.168.50.21"),
71
- RemotePort: 3306,
72
- Proto: "TCP",
73
- Weight: 100,
74
- ActiveConn: 1498,
75
- InactConn: 0,
76
- },
77
- IPVSBackendStatus{
78
- LocalAddress: net.ParseIP("192.168.0.55"),
79
- LocalPort: 3306,
80
- RemoteAddress: net.ParseIP("192.168.50.26"),
81
- RemotePort: 3306,
82
- Proto: "TCP",
83
- Weight: 0,
84
- ActiveConn: 0,
85
- InactConn: 0,
86
- },
87
- IPVSBackendStatus{
88
- LocalAddress: net.ParseIP("192.168.0.55"),
89
- LocalPort: 3306,
90
- RemoteAddress: net.ParseIP("192.168.49.32"),
91
- RemotePort: 3306,
92
- Proto: "TCP",
93
- Weight: 100,
94
- ActiveConn: 0,
95
- InactConn: 0,
96
- },
97
- }
98
-)
99
-
100
-func TestIPVSStats(t *testing.T) {
101
- fs, err := NewFS("fixtures")
102
- if err != nil {
103
- t.Fatal(err)
104
- }
105
- stats, err := fs.NewIPVSStats()
106
- if err != nil {
107
- t.Fatal(err)
108
- }
109
-
110
- if stats != expectedIPVSStats {
111
- t.Errorf("want %+v, got %+v", expectedIPVSStats, stats)
112
- }
113
-}
114
-
115
-func TestParseIPPort(t *testing.T) {
116
- ip := net.ParseIP("192.168.0.22")
117
- port := uint16(3306)
118
-
119
- gotIP, gotPort, err := parseIPPort("C0A80016:0CEA")
120
- if err != nil {
121
- t.Fatal(err)
122
- }
123
- if !(gotIP.Equal(ip) && port == gotPort) {
124
- t.Errorf("want %s:%d, got %s:%d", ip, port, gotIP, gotPort)
125
- }
126
-}
127
-
128
-func TestParseIPPortInvalid(t *testing.T) {
129
- testcases := []string{
130
- "",
131
- "C0A80016",
132
- "C0A800:1234",
133
- "FOOBARBA:1234",
134
- "C0A80016:0CEA:1234",
135
- }
136
-
137
- for _, s := range testcases {
138
- ip, port, err := parseIPPort(s)
139
- if ip != nil || port != uint16(0) || err == nil {
140
- t.Errorf("Expected error for input %s, got ip = %s, port = %v, err = %v", s, ip, port, err)
141
- }
142
- }
143
-}
144
-
145
-func TestParseIPPortIPv6(t *testing.T) {
146
- ip := net.ParseIP("dead:beef::1")
147
- port := uint16(8080)
148
-
149
- gotIP, gotPort, err := parseIPPort("DEADBEEF000000000000000000000001:1F90")
150
- if err != nil {
151
- t.Fatal(err)
152
- }
153
- if !(gotIP.Equal(ip) && port == gotPort) {
154
- t.Errorf("want %s:%d, got %s:%d", ip, port, gotIP, gotPort)
155
- }
156
-
157
-}
158
-
159
-func TestIPVSBackendStatus(t *testing.T) {
160
- fs, err := NewFS("fixtures")
161
- if err != nil {
162
- t.Fatal(err)
163
- }
164
-
165
- backendStats, err := fs.NewIPVSBackendStatus()
166
- if err != nil {
167
- t.Fatal(err)
168
- }
169
-
170
- for idx, expect := range expectedIPVSBackendStatuses {
171
- if !backendStats[idx].LocalAddress.Equal(expect.LocalAddress) {
172
- t.Errorf("expected LocalAddress %s, got %s", expect.LocalAddress, backendStats[idx].LocalAddress)
173
- }
174
- if backendStats[idx].LocalPort != expect.LocalPort {
175
- t.Errorf("expected LocalPort %d, got %d", expect.LocalPort, backendStats[idx].LocalPort)
176
- }
177
- if !backendStats[idx].RemoteAddress.Equal(expect.RemoteAddress) {
178
- t.Errorf("expected RemoteAddress %s, got %s", expect.RemoteAddress, backendStats[idx].RemoteAddress)
179
- }
180
- if backendStats[idx].RemotePort != expect.RemotePort {
181
- t.Errorf("expected RemotePort %d, got %d", expect.RemotePort, backendStats[idx].RemotePort)
182
- }
183
- if backendStats[idx].Proto != expect.Proto {
184
- t.Errorf("expected Proto %s, got %s", expect.Proto, backendStats[idx].Proto)
185
- }
186
- if backendStats[idx].Weight != expect.Weight {
187
- t.Errorf("expected Weight %d, got %d", expect.Weight, backendStats[idx].Weight)
188
- }
189
- if backendStats[idx].ActiveConn != expect.ActiveConn {
190
- t.Errorf("expected ActiveConn %d, got %d", expect.ActiveConn, backendStats[idx].ActiveConn)
191
- }
192
- if backendStats[idx].InactConn != expect.InactConn {
193
- t.Errorf("expected InactConn %d, got %d", expect.InactConn, backendStats[idx].InactConn)
194
- }
195
- }
196
-}
Godeps/_workspace/src/github.com/prometheus/procfs/proc.go
deleted
-188
@@ -1,188 +0,0 @@
1
-package procfs
2
-
3
-import (
4
- "fmt"
5
- "io/ioutil"
6
- "os"
7
- "path"
8
- "strconv"
9
- "strings"
10
-)
11
-
12
-// Proc provides information about a running process.
13
-type Proc struct {
14
- // The process ID.
15
- PID int
16
-
17
- fs FS
18
-}
19
-
20
-// Procs represents a list of Proc structs.
21
-type Procs []Proc
22
-
23
-func (p Procs) Len() int { return len(p) }
24
-func (p Procs) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
25
-func (p Procs) Less(i, j int) bool { return p[i].PID < p[j].PID }
26
-
27
-// Self returns a process for the current process.
28
-func Self() (Proc, error) {
29
- return NewProc(os.Getpid())
30
-}
31
-
32
-// NewProc returns a process for the given pid under /proc.
33
-func NewProc(pid int) (Proc, error) {
34
- fs, err := NewFS(DefaultMountPoint)
35
- if err != nil {
36
- return Proc{}, err
37
- }
38
-
39
- return fs.NewProc(pid)
40
-}
41
-
42
-// AllProcs returns a list of all currently avaible processes under /proc.
43
-func AllProcs() (Procs, error) {
44
- fs, err := NewFS(DefaultMountPoint)
45
- if err != nil {
46
- return Procs{}, err
47
- }
48
-
49
- return fs.AllProcs()
50
-}
51
-
52
-// NewProc returns a process for the given pid.
53
-func (fs FS) NewProc(pid int) (Proc, error) {
54
- if _, err := fs.stat(strconv.Itoa(pid)); err != nil {
55
- return Proc{}, err
56
- }
57
-
58
- return Proc{PID: pid, fs: fs}, nil
59
-}
60
-
61
-// AllProcs returns a list of all currently avaible processes.
62
-func (fs FS) AllProcs() (Procs, error) {
63
- d, err := fs.open("")
64
- if err != nil {
65
- return Procs{}, err
66
- }
67
- defer d.Close()
68
-
69
- names, err := d.Readdirnames(-1)
70
- if err != nil {
71
- return Procs{}, fmt.Errorf("could not read %s: %s", d.Name(), err)
72
- }
73
-
74
- p := Procs{}
75
- for _, n := range names {
76
- pid, err := strconv.ParseInt(n, 10, 64)
77
- if err != nil {
78
- continue
79
- }
80
- p = append(p, Proc{PID: int(pid), fs: fs})
81
- }
82
-
83
- return p, nil
84
-}
85
-
86
-// CmdLine returns the command line of a process.
87
-func (p Proc) CmdLine() ([]string, error) {
88
- f, err := p.open("cmdline")
89
- if err != nil {
90
- return nil, err
91
- }
92
- defer f.Close()
93
-
94
- data, err := ioutil.ReadAll(f)
95
- if err != nil {
96
- return nil, err
97
- }
98
-
99
- if len(data) < 1 {
100
- return []string{}, nil
101
- }
102
-
103
- return strings.Split(string(data[:len(data)-1]), string(byte(0))), nil
104
-}
105
-
106
-// Executable returns the absolute path of the executable command of a process.
107
-func (p Proc) Executable() (string, error) {
108
- exe, err := p.readlink("exe")
109
-
110
- if os.IsNotExist(err) {
111
- return "", nil
112
- }
113
-
114
- return exe, err
115
-}
116
-
117
-// FileDescriptors returns the currently open file descriptors of a process.
118
-func (p Proc) FileDescriptors() ([]uintptr, error) {
119
- names, err := p.fileDescriptors()
120
- if err != nil {
121
- return nil, err
122
- }
123
-
124
- fds := make([]uintptr, len(names))
125
- for i, n := range names {
126
- fd, err := strconv.ParseInt(n, 10, 32)
127
- if err != nil {
128
- return nil, fmt.Errorf("could not parse fd %s: %s", n, err)
129
- }
130
- fds[i] = uintptr(fd)
131
- }
132
-
133
- return fds, nil
134
-}
135
-
136
-// FileDescriptorTargets returns the targets of all file descriptors of a process.
137
-// If a file descriptor is not a symlink to a file (like a socket), that value will be the empty string.
138
-func (p Proc) FileDescriptorTargets() ([]string, error) {
139
- names, err := p.fileDescriptors()
140
- if err != nil {
141
- return nil, err
142
- }
143
-
144
- targets := make([]string, len(names))
145
-
146
- for i, name := range names {
147
- target, err := p.readlink("fd/" + name)
148
- if err == nil {
149
- targets[i] = target
150
- }
151
- }
152
-
153
- return targets, nil
154
-}
155
-
156
-// FileDescriptorsLen returns the number of currently open file descriptors of
157
-// a process.
158
-func (p Proc) FileDescriptorsLen() (int, error) {
159
- fds, err := p.fileDescriptors()
160
- if err != nil {
161
- return 0, err
162
- }
163
-
164
- return len(fds), nil
165
-}
166
-
167
-func (p Proc) fileDescriptors() ([]string, error) {
168
- d, err := p.open("fd")
169
- if err != nil {
170
- return nil, err
171
- }
172
- defer d.Close()
173
-
174
- names, err := d.Readdirnames(-1)
175
- if err != nil {
176
- return nil, fmt.Errorf("could not read %s: %s", d.Name(), err)
177
- }
178
-
179
- return names, nil
180
-}
181
-
182
-func (p Proc) open(pa string) (*os.File, error) {
183
- return p.fs.open(path.Join(strconv.Itoa(p.PID), pa))
184
-}
185
-
186
-func (p Proc) readlink(pa string) (string, error) {
187
- return p.fs.readlink(path.Join(strconv.Itoa(p.PID), pa))
188
-}
Godeps/_workspace/src/github.com/prometheus/procfs/proc_io.go
deleted
-54
@@ -1,54 +0,0 @@
1
-package procfs
2
-
3
-import (
4
- "fmt"
5
- "io/ioutil"
6
-)
7
-
8
-// ProcIO models the content of /proc/<pid>/io.
9
-type ProcIO struct {
10
- // Chars read.
11
- RChar uint64
12
- // Chars written.
13
- WChar uint64
14
- // Read syscalls.
15
- SyscR uint64
16
- // Write syscalls.
17
- SyscW uint64
18
- // Bytes read.
19
- ReadBytes uint64
20
- // Bytes written.
21
- WriteBytes uint64
22
- // Bytes written, but taking into account truncation. See
23
- // Documentation/filesystems/proc.txt in the kernel sources for
24
- // detailed explanation.
25
- CancelledWriteBytes int64
26
-}
27
-
28
-// NewIO creates a new ProcIO instance from a given Proc instance.
29
-func (p Proc) NewIO() (ProcIO, error) {
30
- pio := ProcIO{}
31
-
32
- f, err := p.open("io")
33
- if err != nil {
34
- return pio, err
35
- }
36
- defer f.Close()
37
-
38
- data, err := ioutil.ReadAll(f)
39
- if err != nil {
40
- return pio, err
41
- }
42
-
43
- ioFormat := "rchar: %d\nwchar: %d\nsyscr: %d\nsyscw: %d\n" +
44
- "read_bytes: %d\nwrite_bytes: %d\n" +
45
- "cancelled_write_bytes: %d\n"
46
-
47
- _, err = fmt.Sscanf(string(data), ioFormat, &pio.RChar, &pio.WChar, &pio.SyscR,
48
- &pio.SyscW, &pio.ReadBytes, &pio.WriteBytes, &pio.CancelledWriteBytes)
49
- if err != nil {
50
- return pio, err
51
- }
52
-
53
- return pio, nil
54
-}
Godeps/_workspace/src/github.com/prometheus/procfs/proc_io_test.go
deleted
-49
@@ -1,49 +0,0 @@
1
-package procfs
2
-
3
-import "testing"
4
-
5
-func TestProcIO(t *testing.T) {
6
- fs, err := NewFS("fixtures")
7
- if err != nil {
8
- t.Fatal(err)
9
- }
10
-
11
- p, err := fs.NewProc(26231)
12
- if err != nil {
13
- t.Fatal(err)
14
- }
15
-
16
- s, err := p.NewIO()
17
- if err != nil {
18
- t.Fatal(err)
19
- }
20
-
21
- for _, test := range []struct {
22
- name string
23
- want uint64
24
- got uint64
25
- }{
26
- {name: "RChar", want: 750339, got: s.RChar},
27
- {name: "WChar", want: 818609, got: s.WChar},
28
- {name: "SyscR", want: 7405, got: s.SyscR},
29
- {name: "SyscW", want: 5245, got: s.SyscW},
30
- {name: "ReadBytes", want: 1024, got: s.ReadBytes},
31
- {name: "WriteBytes", want: 2048, got: s.WriteBytes},
32
- } {
33
- if test.want != test.got {
34
- t.Errorf("want %s %d, got %d", test.name, test.want, test.got)
35
- }
36
- }
37
-
38
- for _, test := range []struct {
39
- name string
40
- want int64
41
- got int64
42
- }{
43
- {name: "CancelledWriteBytes", want: -1024, got: s.CancelledWriteBytes},
44
- } {
45
- if test.want != test.got {
46
- t.Errorf("want %s %d, got %d", test.name, test.want, test.got)
47
- }
48
- }
49
-}
Godeps/_workspace/src/github.com/prometheus/procfs/proc_limits.go
deleted
-111
@@ -1,111 +0,0 @@
1
-package procfs
2
-
3
-import (
4
- "bufio"
5
- "fmt"
6
- "regexp"
7
- "strconv"
8
-)
9
-
10
-// ProcLimits represents the soft limits for each of the process's resource
11
-// limits.
12
-type ProcLimits struct {
13
- CPUTime int
14
- FileSize int
15
- DataSize int
16
- StackSize int
17
- CoreFileSize int
18
- ResidentSet int
19
- Processes int
20
- OpenFiles int
21
- LockedMemory int
22
- AddressSpace int
23
- FileLocks int
24
- PendingSignals int
25
- MsqqueueSize int
26
- NicePriority int
27
- RealtimePriority int
28
- RealtimeTimeout int
29
-}
30
-
31
-const (
32
- limitsFields = 3
33
- limitsUnlimited = "unlimited"
34
-)
35
-
36
-var (
37
- limitsDelimiter = regexp.MustCompile(" +")
38
-)
39
-
40
-// NewLimits returns the current soft limits of the process.
41
-func (p Proc) NewLimits() (ProcLimits, error) {
42
- f, err := p.open("limits")
43
- if err != nil {
44
- return ProcLimits{}, err
45
- }
46
- defer f.Close()
47
-
48
- var (
49
- l = ProcLimits{}
50
- s = bufio.NewScanner(f)
51
- )
52
- for s.Scan() {
53
- fields := limitsDelimiter.Split(s.Text(), limitsFields)
54
- if len(fields) != limitsFields {
55
- return ProcLimits{}, fmt.Errorf(
56
- "couldn't parse %s line %s", f.Name(), s.Text())
57
- }
58
-
59
- switch fields[0] {
60
- case "Max cpu time":
61
- l.CPUTime, err = parseInt(fields[1])
62
- case "Max file size":
63
- l.FileLocks, err = parseInt(fields[1])
64
- case "Max data size":
65
- l.DataSize, err = parseInt(fields[1])
66
- case "Max stack size":
67
- l.StackSize, err = parseInt(fields[1])
68
- case "Max core file size":
69
- l.CoreFileSize, err = parseInt(fields[1])
70
- case "Max resident set":
71
- l.ResidentSet, err = parseInt(fields[1])
72
- case "Max processes":
73
- l.Processes, err = parseInt(fields[1])
74
- case "Max open files":
75
- l.OpenFiles, err = parseInt(fields[1])
76
- case "Max locked memory":
77
- l.LockedMemory, err = parseInt(fields[1])
78
- case "Max address space":
79
- l.AddressSpace, err = parseInt(fields[1])
80
- case "Max file locks":
81
- l.FileLocks, err = parseInt(fields[1])
82
- case "Max pending signals":
83
- l.PendingSignals, err = parseInt(fields[1])
84
- case "Max msgqueue size":
85
- l.MsqqueueSize, err = parseInt(fields[1])
86
- case "Max nice priority":
87
- l.NicePriority, err = parseInt(fields[1])
88
- case "Max realtime priority":
89
- l.RealtimePriority, err = parseInt(fields[1])
90
- case "Max realtime timeout":
91
- l.RealtimeTimeout, err = parseInt(fields[1])
92
- }
93
-
94
- if err != nil {
95
- return ProcLimits{}, err
96
- }
97
- }
98
-
99
- return l, s.Err()
100
-}
101
-
102
-func parseInt(s string) (int, error) {
103
- if s == limitsUnlimited {
104
- return -1, nil
105
- }
106
- i, err := strconv.ParseInt(s, 10, 32)
107
- if err != nil {
108
- return 0, fmt.Errorf("couldn't parse value %s: %s", s, err)
109
- }
110
- return int(i), nil
111
-}
Godeps/_workspace/src/github.com/prometheus/procfs/proc_limits_test.go
deleted
-36
@@ -1,36 +0,0 @@
1
-package procfs
2
-
3
-import "testing"
4
-
5
-func TestNewLimits(t *testing.T) {
6
- fs, err := NewFS("fixtures")
7
- if err != nil {
8
- t.Fatal(err)
9
- }
10
-
11
- p, err := fs.NewProc(26231)
12
- if err != nil {
13
- t.Fatal(err)
14
- }
15
-
16
- l, err := p.NewLimits()
17
- if err != nil {
18
- t.Fatal(err)
19
- }
20
-
21
- for _, test := range []struct {
22
- name string
23
- want int
24
- got int
25
- }{
26
- {name: "cpu time", want: -1, got: l.CPUTime},
27
- {name: "open files", want: 2048, got: l.OpenFiles},
28
- {name: "msgqueue size", want: 819200, got: l.MsqqueueSize},
29
- {name: "nice priority", want: 0, got: l.NicePriority},
30
- {name: "address space", want: -1, got: l.AddressSpace},
31
- } {
32
- if test.want != test.got {
33
- t.Errorf("want %s %d, got %d", test.name, test.want, test.got)
34
- }
35
- }
36
-}
Godeps/_workspace/src/github.com/prometheus/procfs/proc_stat.go
deleted
-175
@@ -1,175 +0,0 @@
1
-package procfs
2
-
3
-import (
4
- "bytes"
5
- "fmt"
6
- "io/ioutil"
7
- "os"
8
-)
9
-
10
-// Originally, this USER_HZ value was dynamically retrieved via a sysconf call which
11
-// required cgo. However, that caused a lot of problems regarding
12
-// cross-compilation. Alternatives such as running a binary to determine the
13
-// value, or trying to derive it in some other way were all problematic.
14
-// After much research it was determined that USER_HZ is actually hardcoded to
15
-// 100 on all Go-supported platforms as of the time of this writing. This is
16
-// why we decided to hardcode it here as well. It is not impossible that there
17
-// could be systems with exceptions, but they should be very exotic edge cases,
18
-// and in that case, the worst outcome will be two misreported metrics.
19
-//
20
-// See also the following discussions:
21
-//
22
-// - https://github.com/prometheus/node_exporter/issues/52
23
-// - https://github.com/prometheus/procfs/pull/2
24
-// - http://stackoverflow.com/questions/17410841/how-does-user-hz-solve-the-jiffy-scaling-issue
25
-const userHZ = 100
26
-
27
-// ProcStat provides status information about the process,
28
-// read from /proc/[pid]/stat.
29
-type ProcStat struct {
30
- // The process ID.
31
- PID int
32
- // The filename of the executable.
33
- Comm string
34
- // The process state.
35
- State string
36
- // The PID of the parent of this process.
37
- PPID int
38
- // The process group ID of the process.
39
- PGRP int
40
- // The session ID of the process.
41
- Session int
42
- // The controlling terminal of the process.
43
- TTY int
44
- // The ID of the foreground process group of the controlling terminal of
45
- // the process.
46
- TPGID int
47
- // The kernel flags word of the process.
48
- Flags uint
49
- // The number of minor faults the process has made which have not required
50
- // loading a memory page from disk.
51
- MinFlt uint
52
- // The number of minor faults that the process's waited-for children have
53
- // made.
54
- CMinFlt uint
55
- // The number of major faults the process has made which have required
56
- // loading a memory page from disk.
57
- MajFlt uint
58
- // The number of major faults that the process's waited-for children have
59
- // made.
60
- CMajFlt uint
61
- // Amount of time that this process has been scheduled in user mode,
62
- // measured in clock ticks.
63
- UTime uint
64
- // Amount of time that this process has been scheduled in kernel mode,
65
- // measured in clock ticks.
66
- STime uint
67
- // Amount of time that this process's waited-for children have been
68
- // scheduled in user mode, measured in clock ticks.
69
- CUTime uint
70
- // Amount of time that this process's waited-for children have been
71
- // scheduled in kernel mode, measured in clock ticks.
72
- CSTime uint
73
- // For processes running a real-time scheduling policy, this is the negated
74
- // scheduling priority, minus one.
75
- Priority int
76
- // The nice value, a value in the range 19 (low priority) to -20 (high
77
- // priority).
78
- Nice int
79
- // Number of threads in this process.
80
- NumThreads int
81
- // The time the process started after system boot, the value is expressed
82
- // in clock ticks.
83
- Starttime uint64
84
- // Virtual memory size in bytes.
85
- VSize int
86
- // Resident set size in pages.
87
- RSS int
88
-
89
- fs FS
90
-}
91
-
92
-// NewStat returns the current status information of the process.
93
-func (p Proc) NewStat() (ProcStat, error) {
94
- f, err := p.open("stat")
95
- if err != nil {
96
- return ProcStat{}, err
97
- }
98
- defer f.Close()
99
-
100
- data, err := ioutil.ReadAll(f)
101
- if err != nil {
102
- return ProcStat{}, err
103
- }
104
-
105
- var (
106
- ignore int
107
-
108
- s = ProcStat{PID: p.PID, fs: p.fs}
109
- l = bytes.Index(data, []byte("("))
110
- r = bytes.LastIndex(data, []byte(")"))
111
- )
112
-
113
- if l < 0 || r < 0 {
114
- return ProcStat{}, fmt.Errorf(
115
- "unexpected format, couldn't extract comm: %s",
116
- data,
117
- )
118
- }
119
-
120
- s.Comm = string(data[l+1 : r])
121
- _, err = fmt.Fscan(
122
- bytes.NewBuffer(data[r+2:]),
123
- &s.State,
124
- &s.PPID,
125
- &s.PGRP,
126
- &s.Session,
127
- &s.TTY,
128
- &s.TPGID,
129
- &s.Flags,
130
- &s.MinFlt,
131
- &s.CMinFlt,
132
- &s.MajFlt,
133
- &s.CMajFlt,
134
- &s.UTime,
135
- &s.STime,
136
- &s.CUTime,
137
- &s.CSTime,
138
- &s.Priority,
139
- &s.Nice,
140
- &s.NumThreads,
141
- &ignore,
142
- &s.Starttime,
143
- &s.VSize,
144
- &s.RSS,
145
- )
146
- if err != nil {
147
- return ProcStat{}, err
148
- }
149
-
150
- return s, nil
151
-}
152
-
153
-// VirtualMemory returns the virtual memory size in bytes.
154
-func (s ProcStat) VirtualMemory() int {
155
- return s.VSize
156
-}
157
-
158
-// ResidentMemory returns the resident memory size in bytes.
159
-func (s ProcStat) ResidentMemory() int {
160
- return s.RSS * os.Getpagesize()
161
-}
162
-
163
-// StartTime returns the unix timestamp of the process in seconds.
164
-func (s ProcStat) StartTime() (float64, error) {
165
- stat, err := s.fs.NewStat()
166
- if err != nil {
167
- return 0, err
168
- }
169
- return float64(stat.BootTime) + (float64(s.Starttime) / userHZ), nil
170
-}
171
-
172
-// CPUTime returns the total CPU user and system time in seconds.
173
-func (s ProcStat) CPUTime() float64 {
174
- return float64(s.UTime+s.STime) / userHZ
175
-}
Godeps/_workspace/src/github.com/prometheus/procfs/proc_stat_test.go
deleted
-112
@@ -1,112 +0,0 @@
1
-package procfs
2
-
3
-import "testing"
4
-
5
-func TestProcStat(t *testing.T) {
6
- fs, err := NewFS("fixtures")
7
- if err != nil {
8
- t.Fatal(err)
9
- }
10
-
11
- p, err := fs.NewProc(26231)
12
- if err != nil {
13
- t.Fatal(err)
14
- }
15
-
16
- s, err := p.NewStat()
17
- if err != nil {
18
- t.Fatal(err)
19
- }
20
-
21
- for _, test := range []struct {
22
- name string
23
- want int
24
- got int
25
- }{
26
- {name: "pid", want: 26231, got: s.PID},
27
- {name: "user time", want: 1677, got: int(s.UTime)},
28
- {name: "system time", want: 44, got: int(s.STime)},
29
- {name: "start time", want: 82375, got: int(s.Starttime)},
30
- {name: "virtual memory size", want: 56274944, got: s.VSize},
31
- {name: "resident set size", want: 1981, got: s.RSS},
32
- } {
33
- if test.want != test.got {
34
- t.Errorf("want %s %d, got %d", test.name, test.want, test.got)
35
- }
36
- }
37
-}
38
-
39
-func TestProcStatComm(t *testing.T) {
40
- s1, err := testProcStat(26231)
41
- if err != nil {
42
- t.Fatal(err)
43
- }
44
- if want, got := "vim", s1.Comm; want != got {
45
- t.Errorf("want comm %s, got %s", want, got)
46
- }
47
-
48
- s2, err := testProcStat(584)
49
- if err != nil {
50
- t.Fatal(err)
51
- }
52
- if want, got := "(a b ) ( c d) ", s2.Comm; want != got {
53
- t.Errorf("want comm %s, got %s", want, got)
54
- }
55
-}
56
-
57
-func TestProcStatVirtualMemory(t *testing.T) {
58
- s, err := testProcStat(26231)
59
- if err != nil {
60
- t.Fatal(err)
61
- }
62
-
63
- if want, got := 56274944, s.VirtualMemory(); want != got {
64
- t.Errorf("want virtual memory %d, got %d", want, got)
65
- }
66
-}
67
-
68
-func TestProcStatResidentMemory(t *testing.T) {
69
- s, err := testProcStat(26231)
70
- if err != nil {
71
- t.Fatal(err)
72
- }
73
-
74
- if want, got := 1981*4096, s.ResidentMemory(); want != got {
75
- t.Errorf("want resident memory %d, got %d", want, got)
76
- }
77
-}
78
-
79
-func TestProcStatStartTime(t *testing.T) {
80
- s, err := testProcStat(26231)
81
- if err != nil {
82
- t.Fatal(err)
83
- }
84
-
85
- time, err := s.StartTime()
86
- if err != nil {
87
- t.Fatal(err)
88
- }
89
- if want, got := 1418184099.75, time; want != got {
90
- t.Errorf("want start time %f, got %f", want, got)
91
- }
92
-}
93
-
94
-func TestProcStatCPUTime(t *testing.T) {
95
- s, err := testProcStat(26231)
96
- if err != nil {
97
- t.Fatal(err)
98
- }
99
-
100
- if want, got := 17.21, s.CPUTime(); want != got {
101
- t.Errorf("want cpu time %f, got %f", want, got)
102
- }
103
-}
104
-
105
-func testProcStat(pid int) (ProcStat, error) {
106
- p, err := testProcess(pid)
107
- if err != nil {
108
- return ProcStat{}, err
109
- }
110
-
111
- return p.NewStat()
112
-}
Godeps/_workspace/src/github.com/prometheus/procfs/proc_test.go
deleted
-200
@@ -1,200 +0,0 @@
1
-package procfs
2
-
3
-import (
4
- "os"
5
- "reflect"
6
- "sort"
7
- "testing"
8
-)
9
-
10
-func TestSelf(t *testing.T) {
11
- p1, err := NewProc(os.Getpid())
12
- if err != nil {
13
- t.Fatal(err)
14
- }
15
- p2, err := Self()
16
- if err != nil {
17
- t.Fatal(err)
18
- }
19
-
20
- if !reflect.DeepEqual(p1, p2) {
21
- t.Errorf("want process %v to equal %v", p1, p2)
22
- }
23
-}
24
-
25
-func TestAllProcs(t *testing.T) {
26
- fs, err := NewFS("fixtures")
27
- if err != nil {
28
- t.Fatal(err)
29
- }
30
- procs, err := fs.AllProcs()
31
- if err != nil {
32
- t.Fatal(err)
33
- }
34
- sort.Sort(procs)
35
- for i, p := range []*Proc{{PID: 584}, {PID: 26231}} {
36
- if want, got := p.PID, procs[i].PID; want != got {
37
- t.Errorf("want processes %d, got %d", want, got)
38
- }
39
- }
40
-}
41
-
42
-func TestCmdLine(t *testing.T) {
43
- for _, tt := range []struct {
44
- process int
45
- want []string
46
- }{
47
- {process: 26231, want: []string{"vim", "test.go", "+10"}},
48
- {process: 26232, want: []string{}},
49
- } {
50
- p1, err := testProcess(tt.process)
51
- if err != nil {
52
- t.Fatal(err)
53
- }
54
- c1, err := p1.CmdLine()
55
- if err != nil {
56
- t.Fatal(err)
57
- }
58
- if !reflect.DeepEqual(tt.want, c1) {
59
- t.Errorf("want cmdline %v, got %v", tt.want, c1)
60
- }
61
- }
62
-}
63
-
64
-func TestExecutable(t *testing.T) {
65
- for _, tt := range []struct {
66
- process int
67
- want string
68
- }{
69
- {process: 26231, want: "/usr/bin/vim"},
70
- {process: 26232, want: ""},
71
- } {
72
- p, err := testProcess(tt.process)
73
- if err != nil {
74
- t.Fatal(err)
75
- }
76
- exe, err := p.Executable()
77
- if err != nil {
78
- t.Fatal(err)
79
- }
80
- if !reflect.DeepEqual(tt.want, exe) {
81
- t.Errorf("want absolute path to cmdline %v, got %v", tt.want, exe)
82
- }
83
- }
84
-}
85
-
86
-func TestFileDescriptors(t *testing.T) {
87
- p1, err := testProcess(26231)
88
- if err != nil {
89
- t.Fatal(err)
90
- }
91
- fds, err := p1.FileDescriptors()
92
- if err != nil {
93
- t.Fatal(err)
94
- }
95
- sort.Sort(byUintptr(fds))
96
- if want := []uintptr{0, 1, 2, 3, 10}; !reflect.DeepEqual(want, fds) {
97
- t.Errorf("want fds %v, got %v", want, fds)
98
- }
99
-
100
- p2, err := Self()
101
- if err != nil {
102
- t.Fatal(err)
103
- }
104
-
105
- fdsBefore, err := p2.FileDescriptors()
106
- if err != nil {
107
- t.Fatal(err)
108
- }
109
-
110
- s, err := os.Open("fixtures")
111
- if err != nil {
112
- t.Fatal(err)
113
- }
114
- defer s.Close()
115
-
116
- fdsAfter, err := p2.FileDescriptors()
117
- if err != nil {
118
- t.Fatal(err)
119
- }
120
-
121
- if len(fdsBefore)+1 != len(fdsAfter) {
122
- t.Errorf("want fds %v+1 to equal %v", fdsBefore, fdsAfter)
123
- }
124
-}
125
-
126
-func TestFileDescriptorTargets(t *testing.T) {
127
- p1, err := testProcess(26231)
128
- if err != nil {
129
- t.Fatal(err)
130
- }
131
- fds, err := p1.FileDescriptorTargets()
132
- if err != nil {
133
- t.Fatal(err)
134
- }
135
- sort.Strings(fds)
136
- var want = []string{
137
- "../../symlinktargets/abc",
138
- "../../symlinktargets/def",
139
- "../../symlinktargets/ghi",
140
- "../../symlinktargets/uvw",
141
- "../../symlinktargets/xyz",
142
- }
143
- if !reflect.DeepEqual(want, fds) {
144
- t.Errorf("want fds %v, got %v", want, fds)
145
- }
146
-
147
- p2, err := Self()
148
- if err != nil {
149
- t.Fatal(err)
150
- }
151
-
152
- fdsBefore, err := p2.FileDescriptors()
153
- if err != nil {
154
- t.Fatal(err)
155
- }
156
-
157
- s, err := os.Open("fixtures")
158
- if err != nil {
159
- t.Fatal(err)
160
- }
161
- defer s.Close()
162
-
163
- fdsAfter, err := p2.FileDescriptors()
164
- if err != nil {
165
- t.Fatal(err)
166
- }
167
-
168
- if len(fdsBefore)+1 != len(fdsAfter) {
169
- t.Errorf("want fds %v+1 to equal %v", fdsBefore, fdsAfter)
170
- }
171
-}
172
-
173
-func TestFileDescriptorsLen(t *testing.T) {
174
- p1, err := testProcess(26231)
175
- if err != nil {
176
- t.Fatal(err)
177
- }
178
- l, err := p1.FileDescriptorsLen()
179
- if err != nil {
180
- t.Fatal(err)
181
- }
182
- if want, got := 5, l; want != got {
183
- t.Errorf("want fds %d, got %d", want, got)
184
- }
185
-}
186
-
187
-func testProcess(pid int) (Proc, error) {
188
- fs, err := NewFS("fixtures")
189
- if err != nil {
190
- return Proc{}, err
191
- }
192
-
193
- return fs.NewProc(pid)
194
-}
195
-
196
-type byUintptr []uintptr
197
-
198
-func (a byUintptr) Len() int { return len(a) }
199
-func (a byUintptr) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
200
-func (a byUintptr) Less(i, j int) bool { return a[i] < a[j] }
Godeps/_workspace/src/github.com/prometheus/procfs/stat.go
deleted
-55
@@ -1,55 +0,0 @@
1
-package procfs
2
-
3
-import (
4
- "bufio"
5
- "fmt"
6
- "strconv"
7
- "strings"
8
-)
9
-
10
-// Stat represents kernel/system statistics.
11
-type Stat struct {
12
- // Boot time in seconds since the Epoch.
13
- BootTime int64
14
-}
15
-
16
-// NewStat returns kernel/system statistics read from /proc/stat.
17
-func NewStat() (Stat, error) {
18
- fs, err := NewFS(DefaultMountPoint)
19
- if err != nil {
20
- return Stat{}, err
21
- }
22
-
23
- return fs.NewStat()
24
-}
25
-
26
-// NewStat returns an information about current kernel/system statistics.
27
-func (fs FS) NewStat() (Stat, error) {
28
- f, err := fs.open("stat")
29
- if err != nil {
30
- return Stat{}, err
31
- }
32
- defer f.Close()
33
-
34
- s := bufio.NewScanner(f)
35
- for s.Scan() {
36
- line := s.Text()
37
- if !strings.HasPrefix(line, "btime") {
38
- continue
39
- }
40
- fields := strings.Fields(line)
41
- if len(fields) != 2 {
42
- return Stat{}, fmt.Errorf("couldn't parse %s line %s", f.Name(), line)
43
- }
44
- i, err := strconv.ParseInt(fields[1], 10, 32)
45
- if err != nil {
46
- return Stat{}, fmt.Errorf("couldn't parse %s: %s", fields[1], err)
47
- }
48
- return Stat{BootTime: i}, nil
49
- }
50
- if err := s.Err(); err != nil {
51
- return Stat{}, fmt.Errorf("couldn't parse %s: %s", f.Name(), err)
52
- }
53
-
54
- return Stat{}, fmt.Errorf("couldn't parse %s, missing btime", f.Name())
55
-}
Godeps/_workspace/src/github.com/prometheus/procfs/stat_test.go
deleted
-19
@@ -1,19 +0,0 @@
1
-package procfs
2
-
3
-import "testing"
4
-
5
-func TestStat(t *testing.T) {
6
- fs, err := NewFS("fixtures")
7
- if err != nil {
8
- t.Fatal(err)
9
- }
10
-
11
- s, err := fs.NewStat()
12
- if err != nil {
13
- t.Fatal(err)
14
- }
15
-
16
- if want, got := int64(1418183276), s.BootTime; want != got {
17
- t.Errorf("want boot time %d, got %d", want, got)
18
- }
19
-}