Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
P
protobuf
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
protobuf
Commits
e4b129f3
Commit
e4b129f3
authored
Jul 19, 2016
by
Nikolai Vavilov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
restore old behavior for toObject
parent
db1b2a06
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
37 additions
and
13 deletions
+37
-13
message_test.js
js/message_test.js
+19
-3
js_generator.cc
src/google/protobuf/compiler/js/js_generator.cc
+18
-10
No files found.
js/message_test.js
View file @
e4b129f3
...
@@ -145,7 +145,11 @@ describe('Message test suite', function() {
...
@@ -145,7 +145,11 @@ describe('Message test suite', function() {
undefined
,
undefined
,
undefined
,
undefined
]);
undefined
,
undefined
,
undefined
,
undefined
]);
var
result
=
foo
.
toObject
();
var
result
=
foo
.
toObject
();
assertObjectEquals
({
assertObjectEquals
({
aNestedMessage
:
{},
aString
:
undefined
,
anOutOfOrderBool
:
undefined
,
aNestedMessage
:
{
anInt
:
undefined
},
// Note: JsPb converts undefined repeated fields to empty arrays.
// Note: JsPb converts undefined repeated fields to empty arrays.
aRepeatedMessageList
:
[],
aRepeatedMessageList
:
[],
aRepeatedStringList
:
[]
aRepeatedStringList
:
[]
...
@@ -180,7 +184,14 @@ describe('Message test suite', function() {
...
@@ -180,7 +184,14 @@ describe('Message test suite', function() {
var
response
=
new
proto
.
jspb
.
test
.
DefaultValues
();
var
response
=
new
proto
.
jspb
.
test
.
DefaultValues
();
// Test toObject
// Test toObject
var
expectedObject
=
{};
var
expectedObject
=
{
stringField
:
defaultString
,
boolField
:
true
,
intField
:
11
,
enumField
:
13
,
emptyField
:
''
,
bytesField
:
'bW9v'
};
assertObjectEquals
(
expectedObject
,
response
.
toObject
());
assertObjectEquals
(
expectedObject
,
response
.
toObject
());
...
@@ -748,7 +759,12 @@ describe('Message test suite', function() {
...
@@ -748,7 +759,12 @@ describe('Message test suite', function() {
assertObjectEquals
({
id
:
'g1'
,
someBoolList
:
[
true
,
false
]},
assertObjectEquals
({
id
:
'g1'
,
someBoolList
:
[
true
,
false
]},
groups
[
0
].
toObject
());
groups
[
0
].
toObject
());
assertObjectEquals
({
assertObjectEquals
({
repeatedGroupList
:
[{
id
:
'g1'
,
someBoolList
:
[
true
,
false
]}]
repeatedGroupList
:
[{
id
:
'g1'
,
someBoolList
:
[
true
,
false
]}],
requiredGroup
:
{
id
:
undefined
},
optionalGroup
:
undefined
,
requiredSimple
:
{
aRepeatedStringList
:
[],
aString
:
undefined
},
optionalSimple
:
undefined
,
id
:
undefined
},
group
.
toObject
());
},
group
.
toObject
());
var
group1
=
new
proto
.
jspb
.
test
.
TestGroup1
();
var
group1
=
new
proto
.
jspb
.
test
.
TestGroup1
();
group1
.
setGroup
(
someGroup
);
group1
.
setGroup
(
someGroup
);
...
...
src/google/protobuf/compiler/js/js_generator.cc
View file @
e4b129f3
...
@@ -1952,7 +1952,7 @@ void Generator::GenerateClassToObject(const GeneratorOptions& options,
...
@@ -1952,7 +1952,7 @@ void Generator::GenerateClassToObject(const GeneratorOptions& options,
" * @return {!Object}
\n
"
" * @return {!Object}
\n
"
" */
\n
"
" */
\n
"
"$classname$.toObject = function(includeInstance, msg) {
\n
"
"$classname$.toObject = function(includeInstance, msg) {
\n
"
" var f, obj = {
};
"
,
" var f, obj = {"
,
"classname"
,
GetPath
(
options
,
desc
));
"classname"
,
GetPath
(
options
,
desc
));
bool
first
=
true
;
bool
first
=
true
;
...
@@ -1963,16 +1963,20 @@ void Generator::GenerateClassToObject(const GeneratorOptions& options,
...
@@ -1963,16 +1963,20 @@ void Generator::GenerateClassToObject(const GeneratorOptions& options,
}
}
if
(
!
first
)
{
if
(
!
first
)
{
printer
->
Print
(
"
\n
"
);
printer
->
Print
(
"
,
\n
"
);
}
else
{
}
else
{
printer
->
Print
(
"
\n
\n
"
);
printer
->
Print
(
"
\n
"
);
first
=
false
;
first
=
false
;
}
}
GenerateClassFieldToObject
(
options
,
printer
,
field
);
GenerateClassFieldToObject
(
options
,
printer
,
field
);
}
}
printer
->
Print
(
"
\n\n
"
);
if
(
!
first
)
{
printer
->
Print
(
"
\n
};
\n\n
"
);
}
else
{
printer
->
Print
(
"
\n\n
};
\n\n
"
);
}
if
(
IsExtendable
(
desc
))
{
if
(
IsExtendable
(
desc
))
{
printer
->
Print
(
printer
->
Print
(
...
@@ -1999,12 +2003,7 @@ void Generator::GenerateClassToObject(const GeneratorOptions& options,
...
@@ -1999,12 +2003,7 @@ void Generator::GenerateClassToObject(const GeneratorOptions& options,
void
Generator
::
GenerateClassFieldToObject
(
const
GeneratorOptions
&
options
,
void
Generator
::
GenerateClassFieldToObject
(
const
GeneratorOptions
&
options
,
io
::
Printer
*
printer
,
io
::
Printer
*
printer
,
const
FieldDescriptor
*
field
)
const
{
const
FieldDescriptor
*
field
)
const
{
if
(
HasFieldPresence
(
field
))
{
printer
->
Print
(
"$fieldname$: "
,
printer
->
Print
(
"if (msg.has$name$()) "
,
"name"
,
JSGetterName
(
options
,
field
));
}
printer
->
Print
(
"obj.$fieldname$ = "
,
"fieldname"
,
JSObjectFieldName
(
options
,
field
));
"fieldname"
,
JSObjectFieldName
(
options
,
field
));
if
(
field
->
is_map
())
{
if
(
field
->
is_map
())
{
...
@@ -2034,12 +2033,21 @@ void Generator::GenerateClassFieldToObject(const GeneratorOptions& options,
...
@@ -2034,12 +2033,21 @@ void Generator::GenerateClassFieldToObject(const GeneratorOptions& options,
printer
->
Print
(
"msg.get$getter$()"
,
printer
->
Print
(
"msg.get$getter$()"
,
"getter"
,
JSGetterName
(
options
,
field
,
BYTES_B64
));
"getter"
,
JSGetterName
(
options
,
field
,
BYTES_B64
));
}
else
{
}
else
{
if
(
field
->
has_default_value
())
{
printer
->
Print
(
"!msg.has$name$() ? $defaultValue$ : "
,
"name"
,
JSGetterName
(
options
,
field
),
"defaultValue"
,
JSFieldDefault
(
field
));
}
if
(
field
->
cpp_type
()
==
FieldDescriptor
::
CPPTYPE_FLOAT
||
if
(
field
->
cpp_type
()
==
FieldDescriptor
::
CPPTYPE_FLOAT
||
field
->
cpp_type
()
==
FieldDescriptor
::
CPPTYPE_DOUBLE
)
{
field
->
cpp_type
()
==
FieldDescriptor
::
CPPTYPE_DOUBLE
)
{
if
(
field
->
is_repeated
())
{
if
(
field
->
is_repeated
())
{
printer
->
Print
(
"jspb.Message.getRepeatedFloatingPointField("
printer
->
Print
(
"jspb.Message.getRepeatedFloatingPointField("
"msg, $index$)"
,
"msg, $index$)"
,
"index"
,
JSFieldIndex
(
field
));
"index"
,
JSFieldIndex
(
field
));
}
else
if
(
field
->
is_optional
()
&&
!
field
->
has_default_value
())
{
printer
->
Print
(
"jspb.Message.getOptionalFloatingPointField("
"msg, $index$)"
,
"index"
,
JSFieldIndex
(
field
));
}
else
{
}
else
{
// Convert "NaN" to NaN.
// Convert "NaN" to NaN.
printer
->
Print
(
"+jspb.Message.getField(msg, $index$)"
,
printer
->
Print
(
"+jspb.Message.getField(msg, $index$)"
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment