1
1
import {
2
- DescriptorProto ,
3
- EnumDescriptorProto ,
4
- EnumValueDescriptorProto ,
5
- FieldDescriptorProto ,
6
- FileDescriptorProto ,
7
- MethodDescriptorProto ,
8
- OneofDescriptorProto ,
9
- ServiceDescriptorProto ,
10
- } from 'google-protobuf/google/protobuf/descriptor_pb ' ;
2
+ IDescriptorProto ,
3
+ IEnumDescriptorProto ,
4
+ IEnumValueDescriptorProto ,
5
+ IFieldDescriptorProto ,
6
+ IFileDescriptorProto ,
7
+ IMethodDescriptorProto ,
8
+ IOneofDescriptorProto ,
9
+ IServiceDescriptorProto ,
10
+ } from 'protobufjs/ext/descriptor ' ;
11
11
12
12
/** A set of functions for operating on protobuf objects as we visit them in a traversal */
13
13
interface Visitor {
14
- field ?: ( fqn : string , file : FileDescriptorProto , field : FieldDescriptorProto ) => void ;
15
- extension ?: ( fqn : string , file : FileDescriptorProto , extension : FieldDescriptorProto ) => void ;
16
- oneOf ?: ( fqn : string , file : FileDescriptorProto , decl : OneofDescriptorProto ) => void ;
17
- message ?: ( fqn : string , file : FileDescriptorProto , msg : DescriptorProto ) => void ;
18
- enum ?: ( fqn : string , file : FileDescriptorProto , msg : EnumDescriptorProto ) => void ;
19
- enumValue ?: ( fqn : string , file : FileDescriptorProto , msg : EnumValueDescriptorProto ) => void ;
20
- service ?: ( fqn : string , file : FileDescriptorProto , msg : ServiceDescriptorProto ) => void ;
21
- method ?: ( fqn : string , file : FileDescriptorProto , method : MethodDescriptorProto ) => void ;
14
+ field ?: ( fqn : string , file : IFileDescriptorProto , field : IFieldDescriptorProto ) => void ;
15
+ extension ?: ( fqn : string , file : IFileDescriptorProto , extension : IFieldDescriptorProto ) => void ;
16
+ oneOf ?: ( fqn : string , file : IFileDescriptorProto , decl : IOneofDescriptorProto ) => void ;
17
+ message ?: ( fqn : string , file : IFileDescriptorProto , msg : IDescriptorProto ) => void ;
18
+ enum ?: ( fqn : string , file : IFileDescriptorProto , msg : IEnumDescriptorProto ) => void ;
19
+ enumValue ?: ( fqn : string , file : IFileDescriptorProto , msg : IEnumValueDescriptorProto ) => void ;
20
+ service ?: ( fqn : string , file : IFileDescriptorProto , msg : IServiceDescriptorProto ) => void ;
21
+ method ?: ( fqn : string , file : IFileDescriptorProto , method : IMethodDescriptorProto ) => void ;
22
22
}
23
23
24
24
/** Visit each node in a protobuf file and perform an operation on it
@@ -29,82 +29,81 @@ interface Visitor {
29
29
*
30
30
* @see Visitor for the interface to interact with the nodes
31
31
*/
32
- export const visit = ( file : FileDescriptorProto , visitor : Visitor ) : void => {
33
- const processField = ( prefix : string , file : FileDescriptorProto , field : FieldDescriptorProto ) => {
34
- const fqn = `${ prefix } .${ field . getName ( ) } ` ;
32
+ export const visit = ( file : IFileDescriptorProto , visitor : Visitor ) : void => {
33
+ const processField = ( prefix : string , file : IFileDescriptorProto , field : IFieldDescriptorProto ) => {
34
+ const fqn = `${ prefix } .${ field . name } ` ;
35
35
if ( visitor . field ) {
36
36
visitor . field ( fqn , file , field ) ;
37
37
}
38
38
} ;
39
39
40
40
const processExtension = (
41
41
prefix : string ,
42
- file : FileDescriptorProto ,
43
- ext : FieldDescriptorProto ,
42
+ file : IFileDescriptorProto ,
43
+ ext : IFieldDescriptorProto ,
44
44
) => {
45
- const fqn = `${ prefix } .${ ext . getName ( ) } ` ;
45
+ const fqn = `${ prefix } .${ ext . name } ` ;
46
46
if ( visitor . extension ) {
47
47
visitor . extension ( fqn , file , ext ) ;
48
48
}
49
49
} ;
50
50
51
- const processOneOf = ( prefix : string , file : FileDescriptorProto , decl : OneofDescriptorProto ) => {
52
- const fqn = `${ prefix } .${ decl . getName ( ) } ` ;
51
+ const processOneOf = ( prefix : string , file : IFileDescriptorProto , decl : IOneofDescriptorProto ) => {
52
+ const fqn = `${ prefix } .${ decl . name } ` ;
53
53
if ( visitor . oneOf ) {
54
54
visitor . oneOf ( fqn , file , decl ) ;
55
55
}
56
56
} ;
57
57
58
- const processEnum = ( prefix : string , file : FileDescriptorProto , decl : EnumDescriptorProto ) => {
59
- const fqn = `${ prefix } .${ decl . getName ( ) } ` ;
58
+ const processEnum = ( prefix : string , file : IFileDescriptorProto , decl : IEnumDescriptorProto ) => {
59
+ const fqn = `${ prefix } .${ decl . name } ` ;
60
60
61
61
if ( visitor . enum ) {
62
62
visitor . enum ( fqn , file , decl ) ;
63
63
}
64
64
65
- decl . getValueList ( ) . forEach ( ( value ) => {
66
- const valueFqn = `${ fqn } .${ value . getName ( ) } ` ;
65
+ decl . value ? .forEach ( ( value ) => {
66
+ const valueFqn = `${ fqn } .${ value . name } ` ;
67
67
if ( visitor . enumValue ) {
68
68
visitor . enumValue ( valueFqn , file , value ) ;
69
69
}
70
70
} ) ;
71
71
} ;
72
72
73
- const processMessage = ( prefix : string , file : FileDescriptorProto , msg : DescriptorProto ) => {
74
- const fqn = `${ prefix } .${ msg . getName ( ) } ` ;
73
+ const processMessage = ( prefix : string , file : IFileDescriptorProto , msg : IDescriptorProto ) => {
74
+ const fqn = `${ prefix } .${ msg . name } ` ;
75
75
if ( visitor . message ) {
76
76
visitor . message ( fqn , file , msg ) ;
77
77
}
78
78
79
- msg . getNestedTypeList ( ) . forEach ( ( type ) => processMessage ( fqn , file , type ) ) ;
80
- msg . getEnumTypeList ( ) . forEach ( ( type ) => processEnum ( fqn , file , type ) ) ;
81
- msg . getFieldList ( ) . forEach ( ( field ) => processField ( fqn , file , field ) ) ;
82
- msg . getOneofDeclList ( ) . forEach ( ( decl ) => processOneOf ( fqn , file , decl ) ) ;
83
- msg . getExtensionList ( ) . forEach ( ( ext ) => processExtension ( fqn , file , ext ) ) ;
79
+ msg . nestedType ? .forEach ( ( type ) => processMessage ( fqn , file , type ) ) ;
80
+ msg . enumType ? .forEach ( ( type ) => processEnum ( fqn , file , type ) ) ;
81
+ msg . field ? .forEach ( ( field ) => processField ( fqn , file , field ) ) ;
82
+ msg . oneofDecl ? .forEach ( ( decl ) => processOneOf ( fqn , file , decl ) ) ;
83
+ msg . extension ? .forEach ( ( ext ) => processExtension ( fqn , file , ext ) ) ;
84
84
} ;
85
85
86
86
const processService = (
87
87
prefix : string ,
88
- file : FileDescriptorProto ,
89
- service : ServiceDescriptorProto ,
88
+ file : IFileDescriptorProto ,
89
+ service : IServiceDescriptorProto ,
90
90
) => {
91
- const fqn = `${ prefix } .${ service . getName ( ) } ` ;
91
+ const fqn = `${ prefix } .${ service . name } ` ;
92
92
if ( visitor . service ) {
93
93
visitor . service ( fqn , file , service ) ;
94
94
}
95
95
96
- service . getMethodList ( ) . forEach ( ( method ) => {
97
- const methodFqn = `${ fqn } .${ method . getName ( ) } ` ;
96
+ service . method ? .forEach ( ( method ) => {
97
+ const methodFqn = `${ fqn } .${ method . name } ` ;
98
98
if ( visitor . method ) {
99
99
visitor . method ( methodFqn , file , method ) ;
100
100
}
101
101
} ) ;
102
102
} ;
103
103
104
- const packageName = file . getPackage ( ) || '' ;
105
- file . getEnumTypeList ( ) . forEach ( ( type ) => processEnum ( packageName , file , type ) ) ;
106
- file . getMessageTypeList ( ) . forEach ( ( type ) => processMessage ( packageName , file , type ) ) ;
107
- file . getServiceList ( ) . forEach ( ( service ) => processService ( packageName , file , service ) ) ;
108
-
109
- file . getExtensionList ( ) . forEach ( ( ext ) => processExtension ( packageName , file , ext ) ) ;
104
+ const packageName = file . package || '' ;
105
+ file . enumType ?. forEach ( ( type ) => processEnum ( packageName , file , type ) ) ;
106
+ file . messageType ?. forEach ( ( type ) => processMessage ( packageName , file , type ) ) ;
107
+ file . service ?. forEach ( ( service ) => processService ( packageName , file , service ) ) ;
108
+ file . extension ?. forEach ( ( ext ) => processExtension ( packageName , file , ext ) ) ;
110
109
} ;
0 commit comments