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
8bb0d728
Commit
8bb0d728
authored
Apr 07, 2014
by
Jon Skeet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add the ability to print a builder (not just a message),
and override ToString in AbstractBuilder. This fixes issue 73.
parent
8e04d10d
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
61 additions
and
0 deletions
+61
-0
MessageTest.cs
src/ProtocolBuffers.Test/MessageTest.cs
+7
-0
TextFormatTest.cs
src/ProtocolBuffers.Test/TextFormatTest.cs
+14
-0
AbstractBuilder.cs
src/ProtocolBuffers/AbstractBuilder.cs
+14
-0
TextFormat.cs
src/ProtocolBuffers/TextFormat.cs
+26
-0
No files found.
src/ProtocolBuffers.Test/MessageTest.cs
View file @
8bb0d728
...
@@ -141,6 +141,13 @@ namespace Google.ProtocolBuffers
...
@@ -141,6 +141,13 @@ namespace Google.ProtocolBuffers
Assert
.
IsTrue
(
builder
.
IsInitialized
);
Assert
.
IsTrue
(
builder
.
IsInitialized
);
}
}
[
TestMethod
]
public
void
UninitializedBuilderToString
()
{
TestRequired
.
Builder
builder
=
TestRequired
.
CreateBuilder
().
SetA
(
1
);
Assert
.
AreEqual
(
"a: 1\n"
,
builder
.
ToString
());
}
[
TestMethod
]
[
TestMethod
]
public
void
RequiredForeign
()
public
void
RequiredForeign
()
{
{
...
...
src/ProtocolBuffers.Test/TextFormatTest.cs
View file @
8bb0d728
...
@@ -99,6 +99,20 @@ namespace Google.ProtocolBuffers
...
@@ -99,6 +99,20 @@ namespace Google.ProtocolBuffers
});
});
}
}
/// <summary>
/// Tests that a builder prints the same way as a message.
/// </summary>
[
TestMethod
]
public
void
PrintBuilder
()
{
TestUtil
.
TestInMultipleCultures
(()
=>
{
string
messageText
=
TextFormat
.
PrintToString
(
TestUtil
.
GetAllSet
());
string
builderText
=
TextFormat
.
PrintToString
(
TestUtil
.
GetAllSet
().
ToBuilder
());
Assert
.
AreEqual
(
messageText
,
builderText
);
});
}
/// <summary>
/// <summary>
/// Print TestAllExtensions and compare with golden file.
/// Print TestAllExtensions and compare with golden file.
/// </summary>
/// </summary>
...
...
src/ProtocolBuffers/AbstractBuilder.cs
View file @
8bb0d728
...
@@ -249,5 +249,18 @@ namespace Google.ProtocolBuffers
...
@@ -249,5 +249,18 @@ namespace Google.ProtocolBuffers
}
}
#
endregion
#
endregion
/// <summary>
/// Converts this builder to a string using <see cref="TextFormat" />.
/// </summary>
/// <remarks>
/// This method is not sealed (in the way that it is in <see cref="AbstractMessage{TMessage, TBuilder}" />
/// as it was added after earlier releases; some other implementations may already be overriding the
/// method.
/// </remarks>
public
override
string
ToString
()
{
return
TextFormat
.
PrintToString
(
this
);
}
}
}
}
}
\ No newline at end of file
src/ProtocolBuffers/TextFormat.cs
View file @
8bb0d728
...
@@ -61,6 +61,16 @@ namespace Google.ProtocolBuffers
...
@@ -61,6 +61,16 @@ namespace Google.ProtocolBuffers
Print
(
message
,
generator
);
Print
(
message
,
generator
);
}
}
/// <summary>
/// Outputs a textual representation of the Protocol Message builder supplied into
/// the parameter output.
/// </summary>
public
static
void
Print
(
IBuilder
builder
,
TextWriter
output
)
{
TextGenerator
generator
=
new
TextGenerator
(
output
,
"\n"
);
Print
(
builder
,
generator
);
}
/// <summary>
/// <summary>
/// Outputs a textual representation of <paramref name="fields" /> to <paramref name="output"/>.
/// Outputs a textual representation of <paramref name="fields" /> to <paramref name="output"/>.
/// </summary>
/// </summary>
...
@@ -77,6 +87,13 @@ namespace Google.ProtocolBuffers
...
@@ -77,6 +87,13 @@ namespace Google.ProtocolBuffers
return
text
.
ToString
();
return
text
.
ToString
();
}
}
public
static
string
PrintToString
(
IBuilder
builder
)
{
StringWriter
text
=
new
StringWriter
();
Print
(
builder
,
text
);
return
text
.
ToString
();
}
public
static
string
PrintToString
(
UnknownFieldSet
fields
)
public
static
string
PrintToString
(
UnknownFieldSet
fields
)
{
{
StringWriter
text
=
new
StringWriter
();
StringWriter
text
=
new
StringWriter
();
...
@@ -93,6 +110,15 @@ namespace Google.ProtocolBuffers
...
@@ -93,6 +110,15 @@ namespace Google.ProtocolBuffers
PrintUnknownFields
(
message
.
UnknownFields
,
generator
);
PrintUnknownFields
(
message
.
UnknownFields
,
generator
);
}
}
private
static
void
Print
(
IBuilder
message
,
TextGenerator
generator
)
{
foreach
(
KeyValuePair
<
FieldDescriptor
,
object
>
entry
in
message
.
AllFields
)
{
PrintField
(
entry
.
Key
,
entry
.
Value
,
generator
);
}
PrintUnknownFields
(
message
.
UnknownFields
,
generator
);
}
internal
static
void
PrintField
(
FieldDescriptor
field
,
object
value
,
TextGenerator
generator
)
internal
static
void
PrintField
(
FieldDescriptor
field
,
object
value
,
TextGenerator
generator
)
{
{
if
(
field
.
IsRepeated
)
if
(
field
.
IsRepeated
)
...
...
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