File tree 3 files changed +14
-1
lines changed
3 files changed +14
-1
lines changed Original file line number Diff line number Diff line change @@ -18,18 +18,20 @@ var Enum = require("./enum"),
18
18
* @ignore
19
19
*/
20
20
function genValuePartial_fromObject ( gen , field , fieldIndex , prop ) {
21
+ var defaultAlreadyEmitted = false ;
21
22
/* eslint-disable no-unexpected-multiline, block-scoped-var, no-redeclare */
22
23
if ( field . resolvedType ) {
23
24
if ( field . resolvedType instanceof Enum ) { gen
24
25
( "switch(d%s){" , prop ) ;
25
26
for ( var values = field . resolvedType . values , keys = Object . keys ( values ) , i = 0 ; i < keys . length ; ++ i ) {
26
27
// enum unknown values passthrough
27
- if ( values [ keys [ i ] ] === field . typeDefault ) { gen
28
+ if ( values [ keys [ i ] ] === field . typeDefault && ! defaultAlreadyEmitted ) { gen
28
29
( "default:" )
29
30
( "if(typeof(d%s)===\"number\"){m%s=d%s;break}" , prop , prop , prop ) ;
30
31
if ( ! field . repeated ) gen // fallback to default value only for
31
32
// arrays, to avoid leaving holes.
32
33
( "break" ) ; // for non-repeated fields, just ignore
34
+ defaultAlreadyEmitted = true ;
33
35
}
34
36
gen
35
37
( "case%j:" , keys [ i ] )
Original file line number Diff line number Diff line change @@ -65,6 +65,7 @@ tape.test("pbjs generates static code", function(test) {
65
65
value : 42 ,
66
66
} ,
67
67
regularField : "abc" ,
68
+ enumField : 0 ,
68
69
} ;
69
70
var obj1 = OneofContainer . toObject ( OneofContainer . fromObject ( obj ) ) ;
70
71
test . deepEqual ( obj , obj1 , "fromObject and toObject work for plain object" ) ;
@@ -76,6 +77,7 @@ tape.test("pbjs generates static code", function(test) {
76
77
instance . messageInOneof = new Message ( ) ;
77
78
instance . messageInOneof . value = 42 ;
78
79
instance . regularField = "abc" ;
80
+ instance . enumField = 0 ;
79
81
var instance1 = OneofContainerDynamic . toObject ( OneofContainerDynamic . fromObject ( instance ) ) ;
80
82
test . deepEqual ( instance , instance1 , "fromObject and toObject work for instance of the static type" ) ;
81
83
Original file line number Diff line number Diff line change @@ -4,10 +4,19 @@ message Message {
4
4
int32 value = 1 ;
5
5
}
6
6
7
+ enum Enum {
8
+ option allow_alias = true ;
9
+
10
+ UNSPECIFIED = 0 ;
11
+ DEFAULT = 0 ; // checking that an alias does not break things
12
+ SOMETHING = 1 ;
13
+ }
14
+
7
15
message OneofContainer {
8
16
oneof some_oneof {
9
17
string string_in_oneof = 1 ;
10
18
Message message_in_oneof = 2 ;
11
19
}
12
20
string regular_field = 3 ;
21
+ Enum enum_field = 4 ;
13
22
}
You can’t perform that action at this time.
0 commit comments