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
c34ed5c9
Commit
c34ed5c9
authored
Oct 07, 2015
by
Jon Skeet
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #846 from jskeet/tostring
Support ToString in RepeatedField and MapField.
parents
2842568f
9ed6d4da
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
106 additions
and
0 deletions
+106
-0
MapFieldTest.cs
csharp/src/Google.Protobuf.Test/Collections/MapFieldTest.cs
+14
-0
RepeatedFieldTest.cs
...src/Google.Protobuf.Test/Collections/RepeatedFieldTest.cs
+57
-0
MapField.cs
csharp/src/Google.Protobuf/Collections/MapField.cs
+19
-0
RepeatedField.cs
csharp/src/Google.Protobuf/Collections/RepeatedField.cs
+16
-0
JsonFormatter.cs
csharp/src/Google.Protobuf/JsonFormatter.cs
+0
-0
No files found.
csharp/src/Google.Protobuf.Test/Collections/MapFieldTest.cs
View file @
c34ed5c9
...
...
@@ -562,6 +562,20 @@ namespace Google.Protobuf.Collections
Assert
.
IsFalse
(
values
.
Contains
(
null
));
}
[
Test
]
public
void
ToString_StringToString
()
{
var
map
=
new
MapField
<
string
,
string
>
{
{
"foo"
,
"bar"
},
{
"x"
,
"y"
}
};
Assert
.
AreEqual
(
"{ \"foo\": \"bar\", \"x\": \"y\" }"
,
map
.
ToString
());
}
[
Test
]
public
void
ToString_UnsupportedKeyType
()
{
var
map
=
new
MapField
<
byte
,
string
>
{
{
10
,
"foo"
}
};
Assert
.
Throws
<
ArgumentException
>(()
=>
map
.
ToString
());
}
private
static
KeyValuePair
<
TKey
,
TValue
>
NewKeyValuePair
<
TKey
,
TValue
>(
TKey
key
,
TValue
value
)
{
return
new
KeyValuePair
<
TKey
,
TValue
>(
key
,
value
);
...
...
csharp/src/Google.Protobuf.Test/Collections/RepeatedFieldTest.cs
View file @
c34ed5c9
...
...
@@ -37,6 +37,7 @@ using System.IO;
using
System.Linq
;
using
System.Text
;
using
Google.Protobuf.TestProtos
;
using
Google.Protobuf.WellKnownTypes
;
using
NUnit.Framework
;
namespace
Google.Protobuf.Collections
...
...
@@ -599,5 +600,61 @@ namespace Google.Protobuf.Collections
list
.
Insert
(
1
,
"middle"
);
CollectionAssert
.
AreEqual
(
new
[]
{
"first"
,
"middle"
,
"second"
},
list
);
}
[
Test
]
public
void
ToString_Integers
()
{
var
list
=
new
RepeatedField
<
int
>
{
5
,
10
,
20
};
var
text
=
list
.
ToString
();
Assert
.
AreEqual
(
"[ 5, 10, 20 ]"
,
text
);
}
[
Test
]
public
void
ToString_Strings
()
{
var
list
=
new
RepeatedField
<
string
>
{
"x"
,
"y"
,
"z"
};
var
text
=
list
.
ToString
();
Assert
.
AreEqual
(
"[ \"x\", \"y\", \"z\" ]"
,
text
);
}
[
Test
]
public
void
ToString_Messages
()
{
var
list
=
new
RepeatedField
<
TestAllTypes
>
{
new
TestAllTypes
{
SingleDouble
=
1.5
},
new
TestAllTypes
{
SingleInt32
=
10
}
};
var
text
=
list
.
ToString
();
Assert
.
AreEqual
(
"[ { \"singleDouble\": 1.5 }, { \"singleInt32\": 10 } ]"
,
text
);
}
[
Test
]
public
void
ToString_Empty
()
{
var
list
=
new
RepeatedField
<
TestAllTypes
>
{
};
var
text
=
list
.
ToString
();
Assert
.
AreEqual
(
"[ ]"
,
text
);
}
[
Test
]
public
void
ToString_InvalidElementType
()
{
var
list
=
new
RepeatedField
<
decimal
>
{
15
m
};
Assert
.
Throws
<
ArgumentException
>(()
=>
list
.
ToString
());
}
[
Test
]
public
void
ToString_Timestamp
()
{
var
list
=
new
RepeatedField
<
Timestamp
>
{
Timestamp
.
FromDateTime
(
new
DateTime
(
2015
,
10
,
1
,
12
,
34
,
56
,
DateTimeKind
.
Utc
))
};
var
text
=
list
.
ToString
();
Assert
.
AreEqual
(
"[ \"2015-10-01T12:34:56Z\" ]"
,
text
);
}
[
Test
]
public
void
ToString_Struct
()
{
var
message
=
new
Struct
{
Fields
=
{
{
"foo"
,
new
Value
{
NumberValue
=
20
}
}
}
};
var
list
=
new
RepeatedField
<
Struct
>
{
message
};
var
text
=
list
.
ToString
();
Assert
.
AreEqual
(
text
,
"[ { \"foo\": 20 } ]"
,
message
.
ToString
());
}
}
}
csharp/src/Google.Protobuf/Collections/MapField.cs
View file @
c34ed5c9
...
...
@@ -35,6 +35,7 @@ using System;
using
System.Collections
;
using
System.Collections.Generic
;
using
System.Linq
;
using
System.Text
;
using
Google.Protobuf.Compatibility
;
namespace
Google.Protobuf.Collections
...
...
@@ -45,10 +46,17 @@ namespace Google.Protobuf.Collections
/// <typeparam name="TKey">Key type in the map. Must be a type supported by Protocol Buffer map keys.</typeparam>
/// <typeparam name="TValue">Value type in the map. Must be a type supported by Protocol Buffers.</typeparam>
/// <remarks>
/// <para>
/// This implementation preserves insertion order for simplicity of testing
/// code using maps fields. Overwriting an existing entry does not change the
/// position of that entry within the map. Equality is not order-sensitive.
/// For string keys, the equality comparison is provided by <see cref="StringComparer.Ordinal" />.
/// </para>
/// <para>
/// This implementation does not generally prohibit the use of key/value types which are not
/// supported by Protocol Buffers (e.g. using a key type of <code>byte</code>) but nor does it guarantee
/// that all operations will work in such cases.
/// </para>
/// </remarks>
public
sealed
class
MapField
<
TKey
,
TValue
>
:
IDeepCloneable
<
MapField
<
TKey
,
TValue
>>,
IDictionary
<
TKey
,
TValue
>,
IEquatable
<
MapField
<
TKey
,
TValue
>>,
IDictionary
{
...
...
@@ -482,6 +490,17 @@ namespace Google.Protobuf.Collections
return
size
;
}
/// <summary>
/// Returns a string representation of this repeated field, in the same
/// way as it would be represented by the default JSON formatter.
/// </summary>
public
override
string
ToString
()
{
var
builder
=
new
StringBuilder
();
JsonFormatter
.
Default
.
WriteDictionary
(
builder
,
this
);
return
builder
.
ToString
();
}
#
region
IDictionary
explicit
interface
implementation
void
IDictionary
.
Add
(
object
key
,
object
value
)
{
...
...
csharp/src/Google.Protobuf/Collections/RepeatedField.cs
View file @
c34ed5c9
...
...
@@ -33,6 +33,7 @@
using
System
;
using
System.Collections
;
using
System.Collections.Generic
;
using
System.Text
;
using
Google.Protobuf.Compatibility
;
namespace
Google.Protobuf.Collections
...
...
@@ -41,6 +42,10 @@ namespace Google.Protobuf.Collections
/// The contents of a repeated field: essentially, a collection with some extra
/// restrictions (no null values) and capabilities (deep cloning).
/// </summary>
/// <remarks>
/// This implementation does not generally prohibit the use of types which are not
/// supported by Protocol Buffers but nor does it guarantee that all operations will work in such cases.
/// </remarks>
/// <typeparam name="T">The element type of the repeated field.</typeparam>
public
sealed
class
RepeatedField
<
T
>
:
IList
<
T
>,
IList
,
IDeepCloneable
<
RepeatedField
<
T
>>,
IEquatable
<
RepeatedField
<
T
>>
{
...
...
@@ -464,6 +469,17 @@ namespace Google.Protobuf.Collections
array
[
count
]
=
default
(
T
);
}
/// <summary>
/// Returns a string representation of this repeated field, in the same
/// way as it would be represented by the default JSON formatter.
/// </summary>
public
override
string
ToString
()
{
var
builder
=
new
StringBuilder
();
JsonFormatter
.
Default
.
WriteList
(
builder
,
this
);
return
builder
.
ToString
();
}
/// <summary>
/// Gets or sets the item at the specified index.
/// </summary>
...
...
csharp/src/Google.Protobuf/JsonFormatter.cs
View file @
c34ed5c9
This diff is collapsed.
Click to expand it.
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