-
Notifications
You must be signed in to change notification settings - Fork 36
/
rules.go
1224 lines (1095 loc) · 34.9 KB
/
rules.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package protolock
import (
"fmt"
)
var (
// Rules provides a complete list of all funcs to be run to compare
// a set of Protolocks. This list should be updated as new RuleFunc's
// are added to this package.
Rules = []Rule{
{
Name: "NoUsingReservedFields",
Func: NoUsingReservedFields,
},
{
Name: "NoRemovingReservedFields",
Func: NoRemovingReservedFields,
},
{
Name: "NoRemovingFieldsWithoutReserve",
Func: NoRemovingFieldsWithoutReserve,
},
{
Name: "NoChangingFieldIDs",
Func: NoChangingFieldIDs,
},
{
Name: "NoChangingFieldTypes",
Func: NoChangingFieldTypes,
},
{
Name: "NoChangingFieldNames",
Func: NoChangingFieldNames,
},
{
Name: "NoRemovingRPCs",
Func: NoRemovingRPCs,
},
{
Name: "NoChangingRPCSignature",
Func: NoChangingRPCSignature,
},
{
Name: "NoMovingExistingFieldsIntoOrOutOfOneof",
Func: NoMovingExistingFieldsIntoOrOutOfOneof,
},
}
strict = true
debug = false
)
const nestedPrefix = "."
// SetStrict enables the user to toggle strict mode on and off.
func SetStrict(mode bool) {
strict = mode
}
// SetDebug enables the user to toggle debug mode on and off.
func SetDebug(status bool) {
debug = status
}
type Rule struct {
Name string
Func RuleFunc
}
// RuleFunc defines the common signature for a function which can compare
// Protolock states and determine if issues exist.
type RuleFunc func(current, updated Protolock) ([]Warning, bool)
// lockIDsMap:
// table of filepath -> message name -> reserved field ID -> times ID encountered
// i.e.
/*
["test.proto"] -> ["Test"] -> [1] -> 1
-> ["User"] -> [1] -> 1
[2] -> 1
[3] -> 1
-> ["Plan"] -> [1] -> 1
[2] -> 1
[3] -> 1
*/
type lockIDsMap map[Protopath]map[string]map[int]int
// lockNamesMap:
// table of filepath -> message name -> field name -> times name encountered (or the field ID)
// i.e.
/*
["test.proto"] -> ["Test"] -> ["field_one"] -> 1
-> ["User"] -> ["field_one"] -> 1
["field_two"] -> 1
["field_three"] -> 1
-> ["Plan"] -> ["field_one"] -> 1
["field_two"] -> 1
["field_three"] -> 1
# if mapping field name -> id,
-> ["Account"] -> ["field_one"] -> 1
-> ["field_two"] -> 2
-> ["field_three"] -> 3
*/
type lockNamesMap map[Protopath]map[string]map[string]int
// lockFieldMap:
// table of filepath -> message name -> field name -> field type
type lockFieldMap map[Protopath]map[string]map[string]Field
// lockEnumFieldMap:
// table of filepath -> message name -> field name -> enum field type
type lockEnumFieldMap map[Protopath]map[string]map[string]EnumField
// lockMapMap:
// table of filepath -> message name -> Map name -> Map type
type lockMapMap map[Protopath]map[string]map[string]Map
// lockRPCMap:
// table of filepath -> service name -> rpc name -> rpc type
type lockRPCMap map[Protopath]map[string]map[string]RPC
// lockFieldIDNameMap:
// table of filepath -> message name -> field ID -> field name
type lockFieldIDNameMap map[Protopath]map[string]map[int]string
func incMessageFields(reservedIDMap lockIDsMap, reservedNameMap lockNamesMap, filepath Protopath, prefix string, msg Message) {
name := prefix + msg.Name
for _, field := range msg.Fields {
if reservedIDMap[filepath][name] == nil {
reservedIDMap[filepath][name] = make(map[int]int)
}
if reservedNameMap[filepath][name] == nil {
reservedNameMap[filepath][name] = make(map[string]int)
}
reservedIDMap[filepath][name][field.ID]++
reservedNameMap[filepath][name][field.Name]++
}
for _, mp := range msg.Maps {
if reservedIDMap[filepath][name] == nil {
reservedIDMap[filepath][name] = make(map[int]int)
}
if reservedNameMap[filepath][name] == nil {
reservedNameMap[filepath][name] = make(map[string]int)
}
reservedIDMap[filepath][name][mp.Field.ID]++
reservedNameMap[filepath][name][mp.Field.Name]++
}
for _, m := range msg.Messages {
incMessageFields(
reservedIDMap, reservedNameMap,
filepath, name+nestedPrefix, m,
)
}
}
func incEnumFields(reservedIDMap lockIDsMap, reservedNameMap lockNamesMap, filepath Protopath, enum Enum) {
for _, field := range enum.EnumFields {
if reservedIDMap[filepath][enum.Name] == nil {
reservedIDMap[filepath][enum.Name] = make(map[int]int)
}
if reservedNameMap[filepath][enum.Name] == nil {
reservedNameMap[filepath][enum.Name] = make(map[string]int)
}
for id := range reservedIDMap[filepath][enum.Name] {
if field.Integer == id {
reservedIDMap[filepath][enum.Name][field.Integer]++
}
}
for name := range reservedNameMap[filepath][enum.Name] {
if field.Name == name {
reservedNameMap[filepath][enum.Name][field.Name]++
}
}
}
}
// NoUsingReservedFields compares the current vs. updated Protolock definitions
// and will return a list of warnings if any message's previously reserved fields
// or IDs are now being used as part of the same message.
func NoUsingReservedFields(cur, upd Protolock) ([]Warning, bool) {
reservedIDMap, reservedNameMap := getReservedFields(cur)
reservedEnumIDMap, reservedEnumNameMap := getReservedEnumFields(cur)
// add each messages field name/number to the existing list identified as
// reserved to analyze
for _, def := range upd.Definitions {
if reservedIDMap[def.Filepath] == nil {
reservedIDMap[def.Filepath] = make(map[string]map[int]int)
}
if reservedNameMap[def.Filepath] == nil {
reservedNameMap[def.Filepath] = make(map[string]map[string]int)
}
for _, msg := range def.Def.Messages {
incMessageFields(
reservedIDMap, reservedNameMap,
def.Filepath, "", msg,
)
}
if reservedEnumIDMap[def.Filepath] == nil {
reservedEnumIDMap[def.Filepath] = make(map[string]map[int]int)
}
if reservedEnumNameMap[def.Filepath] == nil {
reservedEnumNameMap[def.Filepath] = make(map[string]map[string]int)
}
for _, enum := range def.Def.Enums {
incEnumFields(
reservedEnumIDMap, reservedEnumNameMap,
def.Filepath, enum,
)
}
}
var warnings []Warning
// Find message conflicts (using reserved names or IDs)
// if the field ID was encountered more than once per message, then it
// is known to be a re-use of a reserved field and a warning should be
// returned for each occurrance
for path, m := range reservedIDMap {
for msgName, mm := range m {
for id, count := range mm {
if count > 1 {
msg := fmt.Sprintf(
`"%s" is re-using ID: %d, a reserved field number`,
msgName, id,
)
warnings = append(warnings, Warning{
Filepath: OSPath(path),
Message: msg,
})
}
}
}
}
// if the field name was encountered more than once per message, then it
// is known to be a re-use of a reserved field and a warning should be
// returned for each occurrance
for path, m := range reservedNameMap {
for msgName, mm := range m {
for name, count := range mm {
if count > 1 {
msg := fmt.Sprintf(
`"%s" is re-using name: "%s", a reserved field name`,
msgName, name,
)
warnings = append(warnings, Warning{
Filepath: OSPath(path),
Message: msg,
})
}
}
}
}
// Find enum conflicts (using reserved names or integers)
// if the enum value was encountered more than once per message, then it
// is known to be a re-use of a reserved field and a warning should be
// returned for each occurrance
for path, m := range reservedEnumIDMap {
for enumName, mm := range m {
for id, count := range mm {
if count > 1 {
msg := fmt.Sprintf(
`"%s" is re-using integer: %d, a reserved value`,
enumName, id,
)
warnings = append(warnings, Warning{
Filepath: OSPath(path),
Message: msg,
})
}
}
}
}
// if the enum name was encountered more than once per message, then it
// is known to be a re-use of a reserved field and a warning should be
// returned for each occurrance
for path, m := range reservedEnumNameMap {
for enumName, mm := range m {
for name, count := range mm {
if count > 1 {
msg := fmt.Sprintf(
`"%s" is re-using name: "%s", a reserved name`,
enumName, name,
)
warnings = append(warnings, Warning{
Filepath: OSPath(path),
Message: msg,
})
}
}
}
}
if warnings != nil {
return warnings, false
}
return nil, true
}
// NoRemovingReservedFields compares the current vs. updated Protolock definitions
// and will return a list of warnings if any reserved field has been removed. This
// rule is only enforced when strict mode is enabled.
func NoRemovingReservedFields(cur, upd Protolock) ([]Warning, bool) {
if !strict {
return nil, true
}
var warnings []Warning
// check that all reserved fields on current Protolock remain in the
// updated Protolock
// check all reserved fields on messages
curReservedIDMap, curReservedNameMap := getReservedFields(cur)
updReservedIDMap, updReservedNameMap := getReservedFields(upd)
for path, msgMap := range curReservedIDMap {
for msgName, idMap := range msgMap {
for id := range idMap {
if _, ok := updReservedIDMap[path][msgName][id]; !ok {
msg := fmt.Sprintf(
`"%s" is missing ID: %d, which had been reserved`,
msgName, id,
)
warnings = append(warnings, Warning{
Filepath: OSPath(path),
Message: msg,
})
}
}
}
}
for path, msgMap := range curReservedNameMap {
for msgName, nameMap := range msgMap {
for name := range nameMap {
if _, ok := updReservedNameMap[path][msgName][name]; !ok {
msg := fmt.Sprintf(
`"%s" is missing name: "%s", which had been reserved`,
msgName, name,
)
warnings = append(warnings, Warning{
Filepath: OSPath(path),
Message: msg,
})
}
}
}
}
// check all reserved fields on enums
curReservedEnumIDMap, curReservedEnumNameMap := getReservedEnumFields(cur)
updReservedEnumIDMap, updReservedEnumNameMap := getReservedEnumFields(upd)
for path, enumMap := range curReservedEnumIDMap {
for enumName, idMap := range enumMap {
for id := range idMap {
if _, ok := updReservedEnumIDMap[path][enumName][id]; !ok {
msg := fmt.Sprintf(
`"%s" is missing integer: %d, which had been reserved`,
enumName, id,
)
warnings = append(warnings, Warning{
Filepath: OSPath(path),
Message: msg,
})
}
}
}
}
for path, enumMap := range curReservedEnumNameMap {
for enumName, nameMap := range enumMap {
for name := range nameMap {
if _, ok := updReservedEnumNameMap[path][enumName][name]; !ok {
msg := fmt.Sprintf(
`"%s" is missing name: "%s", which had been reserved`,
enumName, name,
)
warnings = append(warnings, Warning{
Filepath: OSPath(path),
Message: msg,
})
}
}
}
}
if warnings != nil {
return warnings, false
}
return nil, true
}
// NoChangingFieldIDs compares the current vs. updated Protolock definitions and
// will return a list of warnings if any field ID number has been changed.
func NoChangingFieldIDs(cur, upd Protolock) ([]Warning, bool) {
var warnings []Warning
// check all non-reserved message fields
curNameIDMap := getNonReservedFields(cur)
updNameIDMap := getNonReservedFields(upd)
// check that all current Protolock names map to the same IDs as the
// updated Protolock
for path, msgMap := range curNameIDMap {
for msgName, fieldMap := range msgMap {
for fieldName, fieldID := range fieldMap {
updFieldID, ok := updNameIDMap[path][msgName][fieldName]
if ok {
if updFieldID != fieldID {
msg := fmt.Sprintf(
`"%s" field: "%s" has a different ID: %d, previously %d`,
msgName, fieldName, updFieldID, fieldID,
)
warnings = append(warnings, Warning{
Filepath: OSPath(path),
Message: msg,
})
}
}
}
}
}
// check all non-reserved enum fields
curEnumNameIDMap := getNonReservedEnumFields(cur)
updEnumNameIDMap := getNonReservedEnumFields(upd)
// check that all current Protolock names map to the same IDs as the
// updated Protolock
for path, enumMap := range curEnumNameIDMap {
for enumName, fieldMap := range enumMap {
for fieldName, fieldInteger := range fieldMap {
updFieldInteger, ok := updEnumNameIDMap[path][enumName][fieldName]
if ok {
if updFieldInteger != fieldInteger {
msg := fmt.Sprintf(
`"%s" field: "%s" has a different integer: %d, previously %d`,
enumName, fieldName, updFieldInteger, fieldInteger,
)
warnings = append(warnings, Warning{
Filepath: OSPath(path),
Message: msg,
})
}
}
}
}
}
if warnings != nil {
return warnings, false
}
return nil, true
}
// NoChangingFieldTypes compares the current vs. updated Protolock definitions and
// will return a list of warnings if any field type has been changed.
func NoChangingFieldTypes(cur, upd Protolock) ([]Warning, bool) {
curFieldMap := getFieldMap(cur)
updFieldMap := getFieldMap(upd)
curMapMap := getMapMap(cur)
updMapMap := getMapMap(upd)
var warnings []Warning
// check that the current Protolock message's field types are the same
// for each of the same message's fields in the updated Protolock
for path, msgMap := range curFieldMap {
for msgName, fieldMap := range msgMap {
for fieldName, field := range fieldMap {
updField, ok := updFieldMap[path][msgName][fieldName]
if ok {
if updField.Type != field.Type {
msg := fmt.Sprintf(
`"%s" field: "%s" has a different type: %s, previously %s`,
msgName, fieldName, updField.Type, field.Type,
)
warnings = append(warnings, Warning{
Filepath: OSPath(path),
Message: msg,
})
}
if updField.IsRepeated != field.IsRepeated {
msg := fmt.Sprintf(
`"%s" field: "%s" has a different "repeated" status: %t, previously %t`,
msgName, fieldName, updField.IsRepeated, field.IsRepeated,
)
warnings = append(warnings, Warning{
Filepath: OSPath(path),
Message: msg,
})
}
}
}
}
}
// check that the current Protolock message's map types are the same
// for each of the same message's maps in the updated Protolock
for path, msgMap := range curMapMap {
for msgName, mapMap := range msgMap {
for fieldName, mp := range mapMap {
updMap, ok := updMapMap[path][msgName][fieldName]
if ok {
if updMap.KeyType != mp.KeyType {
msg := fmt.Sprintf(
`"%s" field: "%s" has a different type: %s, previously %s`,
msgName, fieldName, updMap.KeyType, mp.KeyType,
)
warnings = append(warnings, Warning{
Filepath: OSPath(path),
Message: msg,
})
}
}
}
}
}
if warnings != nil {
return warnings, false
}
return nil, true
}
// NoChangingFieldNames compares the current vs. updated Protolock definitions and
// will return a list of warnings if any message's previous fields have been
// renamed. This rule is only enforced when strict mode is enabled.
func NoChangingFieldNames(cur, upd Protolock) ([]Warning, bool) {
if !strict {
return nil, true
}
var warnings []Warning
// check all field names of messages
curFieldMap := getFieldsIDName(cur)
updFieldMap := getFieldsIDName(upd)
// check that the current Protolock messages' field names are equal to
// their relative messages' field names in the updated Protolock
for path, msgMap := range curFieldMap {
for msgName, fieldMap := range msgMap {
for fieldID, fieldName := range fieldMap {
updFieldName, ok := updFieldMap[path][msgName][fieldID]
if ok {
if updFieldName != fieldName {
msg := fmt.Sprintf(
`"%s" field: "%s" ID: %d has an updated name, previously "%s"`,
msgName, updFieldName, fieldID, fieldName,
)
warnings = append(warnings, Warning{
Filepath: OSPath(path),
Message: msg,
})
}
}
}
}
}
// check all field names of enums
curEnumFieldMap := getEnumFieldsIDName(cur)
updEnumFieldMap := getEnumFieldsIDName(upd)
// check that the current Protolock enums' field names are equal to
// their relative enums' field names in the updated Protolock
for path, enumMap := range curEnumFieldMap {
for enumName, fieldMap := range enumMap {
for fieldInteger, fieldName := range fieldMap {
updFieldName, ok := updEnumFieldMap[path][enumName][fieldInteger]
if ok {
if updFieldName != fieldName {
msg := fmt.Sprintf(
`"%s" field: "%s" integer: %d has an updated name, previously "%s"`,
enumName, updFieldName, fieldInteger, fieldName,
)
warnings = append(warnings, Warning{
Filepath: OSPath(path),
Message: msg,
})
}
}
}
}
}
if warnings != nil {
return warnings, false
}
return nil, true
}
// NoRemovingRPCs compares the current vs. updated Protolock definitions and
// will return a list of warnings if any RPCs provided by a Service have been
// removed. This rule is only enforced when strict mode is enabled.
func NoRemovingRPCs(cur, upd Protolock) ([]Warning, bool) {
if !strict {
return nil, true
}
var warnings []Warning
// check that all current Protolock services' RPCs are still in the
// updated Protolock
curServices := getServicesRPCsMap(cur)
updServices := getServicesRPCsMap(upd)
for path, svcMap := range curServices {
for svcName, rpcMap := range svcMap {
for rpcName := range rpcMap {
_, ok := updServices[path][svcName][rpcName]
if !ok {
msg := fmt.Sprintf(
`"%s" is missing RPC: "%s", which should be available`,
svcName, rpcName,
)
warnings = append(warnings, Warning{
Filepath: OSPath(path),
Message: msg,
})
}
}
}
}
if warnings != nil {
return warnings, false
}
return nil, true
}
// NoRemovingFieldsWithoutReserve compares the current vs. updated Protolock
// definitions and will return a list of warnings if any field has been removed
// without a corresponding reservation of that field name or ID.
func NoRemovingFieldsWithoutReserve(cur, upd Protolock) ([]Warning, bool) {
var warnings []Warning
// check all message fields
curFieldMap := getFieldMap(cur)
updFieldMap := getFieldMap(upd)
// check that if a field name from the current Protolock is not retained
// in the updated Protolock, then the field's name and ID should become
// reserved within the parent message
for path, msgMap := range curFieldMap {
for msgName, fieldMap := range msgMap {
encounteredIDs := make(map[int]int)
for _, field := range updFieldMap[path][msgName] {
encounteredIDs[field.ID]++
}
for fieldName, field := range fieldMap {
_, ok := updFieldMap[path][msgName][fieldName]
if !ok {
// check that the field name and ID are
// both in the reserved fields for this
// message
resIDsMap, resNamesMap := getReservedFields(upd)
if _, ok := resNamesMap[path][msgName][field.Name]; !ok {
msg := fmt.Sprintf(
`"%s" field: "%s" has been removed, but is not reserved`,
msgName, field.Name,
)
warnings = append(warnings, Warning{
Filepath: OSPath(path),
Message: msg,
})
}
// check that the ID for this missing field is being re-used
// in which case will be caught by NoChangingFieldNames
if _, ok := encounteredIDs[field.ID]; ok {
continue
}
if _, ok := resIDsMap[path][msgName][field.ID]; !ok {
msg := fmt.Sprintf(
`"%s" ID: "%d" has been removed, but is not reserved`,
msgName, field.ID,
)
warnings = append(warnings, Warning{
Filepath: OSPath(path),
Message: msg,
})
}
}
}
}
}
// check all enum fields
curEnumFieldMap := getEnumFieldMap(cur)
updEnumFieldMap := getEnumFieldMap(upd)
// check that if a field name from the current Protolock is not retained
// in the updated Protolock, then the field's name and integer should
// become reserved within the parent enum
for path, enumMap := range curEnumFieldMap {
for enumName, fieldMap := range enumMap {
encounteredIDs := make(map[int]int)
for _, field := range updEnumFieldMap[path][enumName] {
encounteredIDs[field.Integer]++
}
for fieldName, field := range fieldMap {
_, ok := updEnumFieldMap[path][enumName][fieldName]
if !ok {
// check that the field name and ID are
// both in the reserved fields for this
// enum
resIDsMap, resNamesMap := getReservedEnumFields(upd)
if _, ok := resNamesMap[path][enumName][field.Name]; !ok {
msg := fmt.Sprintf(
`"%s" field: "%s" has been removed, but is not reserved`,
enumName, field.Name,
)
warnings = append(warnings, Warning{
Filepath: OSPath(path),
Message: msg,
})
}
// check that the integer for this missing field is being re-used
// in which case will be caught by NoChangingFieldNames
if _, ok := encounteredIDs[field.Integer]; ok {
continue
}
if _, ok := resIDsMap[path][enumName][field.Integer]; !ok {
msg := fmt.Sprintf(
`"%s" integer: "%d" has been removed, but is not reserved`,
enumName, field.Integer,
)
warnings = append(warnings, Warning{
Filepath: OSPath(path),
Message: msg,
})
}
}
}
}
}
if warnings != nil {
return warnings, false
}
return nil, true
}
// NoChangingRPCSignature compares the current vs. updated Protolock
// definitions and will return a list of warnings if any RPC signature has been
// changed while using the same name.
func NoChangingRPCSignature(cur, upd Protolock) ([]Warning, bool) {
var warnings []Warning
// check that no breaking changes to the signature of an RPC have been
// made between the current Protolock and the updated Protolock
curRPCMap := getRPCMap(cur)
updRPCMap := getRPCMap(upd)
for path, svcMap := range curRPCMap {
for svcName, rpcMap := range svcMap {
for rpcName, rpc := range rpcMap {
updRPC, ok := updRPCMap[path][svcName][rpcName]
if !ok {
continue
}
// check that stream option and type are the same
// for both the RPC's request and response
if rpc.InStreamed != updRPC.InStreamed {
msg := fmt.Sprintf(
`"%s" RPC: "%s" input stream identifier has changed, previously: %t`,
svcName, rpcName, rpc.InStreamed,
)
warnings = append(warnings, Warning{
Filepath: OSPath(path),
Message: msg,
})
}
if rpc.OutStreamed != updRPC.OutStreamed {
msg := fmt.Sprintf(
`"%s" RPC: "%s" output stream identifier has changed, previously: %t`,
svcName, rpcName, rpc.OutStreamed,
)
warnings = append(warnings, Warning{
Filepath: OSPath(path),
Message: msg,
})
}
if rpc.InType != updRPC.InType {
msg := fmt.Sprintf(
`"%s" RPC: "%s" input type has changed, previously: %s`,
svcName, rpcName, rpc.InType,
)
warnings = append(warnings, Warning{
Filepath: OSPath(path),
Message: msg,
})
}
if rpc.OutType != updRPC.OutType {
msg := fmt.Sprintf(
`"%s" RPC: "%s" output type has changed, previously: %s`,
svcName, rpcName, rpc.OutType,
)
warnings = append(warnings, Warning{
Filepath: OSPath(path),
Message: msg,
})
}
}
}
}
if warnings != nil {
return warnings, false
}
return nil, true
}
// Existing fields must not be moved into or out of a oneof. This is a backwards-incompatible change in the Go protobuf stubs.
// per https://google.aip.dev/180#moving-into-oneofs
func NoMovingExistingFieldsIntoOrOutOfOneof(cur, upd Protolock) ([]Warning, bool) {
var warnings []Warning
// check all message fields
curFieldMap := getFieldMap(cur)
updFieldMap := getFieldMap(upd)
// if a field name from the current Protolock has a OneofParent entry
// that differs from the updated Protolock, then a warning should be added
for path, msgMap := range curFieldMap {
for msgName, fieldMap := range msgMap {
for fieldName, field := range fieldMap {
updField, ok := updFieldMap[path][msgName][fieldName]
if ok && updField.OneofParent != field.OneofParent {
if len(updField.OneofParent) == 0 {
msg := fmt.Sprintf(
`"%s" was moved out of oneof "%s"`,
fieldName, field.OneofParent,
)
warnings = append(warnings, Warning{
Filepath: OSPath(path),
Message: msg,
})
} else if len(field.OneofParent) == 0 {
msg := fmt.Sprintf(
`"%s" was moved into oneof "%s"`,
fieldName, updField.OneofParent,
)
warnings = append(warnings, Warning{
Filepath: OSPath(path),
Message: msg,
})
} else {
msg := fmt.Sprintf(
`"%s" was moved from oneof "%s" into of oneof "%s"`,
fieldName, field.OneofParent, updField.OneofParent,
)
warnings = append(warnings, Warning{
Filepath: OSPath(path),
Message: msg,
})
}
}
}
}
}
if warnings != nil {
return warnings, false
}
return nil, true
}
func getReservedFieldsRecursive(reservedIDMap lockIDsMap, reservedNameMap lockNamesMap, filepath Protopath, prefix string, msg Message) {
msgName := prefix + msg.Name
for _, id := range msg.ReservedIDs {
if reservedIDMap[filepath][msgName] == nil {
reservedIDMap[filepath][msgName] = make(map[int]int)
}
reservedIDMap[filepath][msgName][id]++
}
for _, name := range msg.ReservedNames {
if reservedNameMap[filepath][msgName] == nil {
reservedNameMap[filepath][msgName] = make(map[string]int)
}
reservedNameMap[filepath][msgName][name]++
}
for _, msg := range msg.Messages {
// recursively call func, using parent message name and a '.' as prefix
getReservedFieldsRecursive(reservedIDMap, reservedNameMap, filepath, msgName+nestedPrefix, msg)
}
}
// getReservedFields gets all the reserved field numbers and names, and stashes
// them in a lockIDsMap and lockNamesMap to be checked against.
func getReservedFields(lock Protolock) (lockIDsMap, lockNamesMap) {
reservedIDMap := make(lockIDsMap)
reservedNameMap := make(lockNamesMap)
for _, def := range lock.Definitions {
if reservedIDMap[def.Filepath] == nil {
reservedIDMap[def.Filepath] = make(map[string]map[int]int)
}
if reservedNameMap[def.Filepath] == nil {
reservedNameMap[def.Filepath] = make(map[string]map[string]int)
}
for _, msg := range def.Def.Messages {
getReservedFieldsRecursive(reservedIDMap, reservedNameMap, def.Filepath, "", msg)
}
}
return reservedIDMap, reservedNameMap
}
func getReservedEnumFields(lock Protolock) (lockIDsMap, lockNamesMap) {
reservedIDMap := make(lockIDsMap)
reservedNameMap := make(lockNamesMap)
for _, def := range lock.Definitions {
if reservedIDMap[def.Filepath] == nil {
reservedIDMap[def.Filepath] = make(map[string]map[int]int)
}
if reservedNameMap[def.Filepath] == nil {
reservedNameMap[def.Filepath] = make(map[string]map[string]int)
}
for _, enum := range def.Def.Enums {
for _, id := range enum.ReservedIDs {
if reservedIDMap[def.Filepath][enum.Name] == nil {
reservedIDMap[def.Filepath][enum.Name] = make(map[int]int)
}
reservedIDMap[def.Filepath][enum.Name][id]++
}
for _, name := range enum.ReservedNames {
if reservedNameMap[def.Filepath][enum.Name] == nil {
reservedNameMap[def.Filepath][enum.Name] = make(map[string]int)
}
reservedNameMap[def.Filepath][enum.Name][name]++
}
}
}
return reservedIDMap, reservedNameMap
}
func getFieldIDNameRecursive(fieldIDNameMap lockFieldIDNameMap, filepath Protopath, prefix string, msg Message) {
msgName := prefix + msg.Name
for _, field := range msg.Fields {
if fieldIDNameMap[filepath][msgName] == nil {
fieldIDNameMap[filepath][msgName] = make(map[int]string)
}
fieldIDNameMap[filepath][msgName][field.ID] = field.Name
}
for _, mp := range msg.Maps {
if fieldIDNameMap[filepath][msgName] == nil {
fieldIDNameMap[filepath][msgName] = make(map[int]string)
}
fieldIDNameMap[filepath][msgName][mp.Field.ID] = mp.Field.Name
}
for _, nestedMsg := range msg.Messages {
getFieldIDNameRecursive(fieldIDNameMap, filepath, msgName+nestedPrefix, nestedMsg)
}
}
// getFieldsIDName gets all the fields mapped by the field ID to its name for
// all messages.
func getFieldsIDName(lock Protolock) lockFieldIDNameMap {
fieldIDNameMap := make(lockFieldIDNameMap)
for _, def := range lock.Definitions {
if fieldIDNameMap[def.Filepath] == nil {
fieldIDNameMap[def.Filepath] = make(map[string]map[int]string)
}
for _, msg := range def.Def.Messages {
getFieldIDNameRecursive(fieldIDNameMap, def.Filepath, "", msg)
}
}
return fieldIDNameMap
}
// getEnumFieldsIDName gets all the fields mapped by the field ID to its name