Skip to content

Commit 45b3103

Browse files
Fix/default fix (#301)
* make the Party class default to a real value when the is no name given. * clean up serialization * Clean up constructor and fix an issue with C test * fixed missing xml comments
1 parent 74f6ffb commit 45b3103

File tree

5 files changed

+77
-33
lines changed

5 files changed

+77
-33
lines changed

bindings/netstandard/ElectionGuard/ElectionGuard.Encryption.Tests/TestManifest.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ public void Test_Can_Construct_Internationalized_Text()
3434
Assert.That(actual.Value == "some words");
3535
}
3636

37+
[Test]
38+
public void Test_Can_Party()
39+
{
40+
// Act
41+
var party = new Party("new party");
42+
43+
// Assert
44+
Assert.IsNotNull(party.Name);
45+
}
46+
3747
[Test]
3848
public void Test_Can_Construct_Ballot_style()
3949
{

bindings/netstandard/ElectionGuard/ElectionGuard.Encryption/Election.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,16 @@ public unsafe CiphertextElectionContext(ulong numberOfGuardians,
220220
status.ThrowIfError();
221221
}
222222

223+
/// <summary>
224+
/// Makes a CiphertextElectionContext object.
225+
///
226+
/// <param name="numberOfGuardians"> The number of guardians necessary to generate the public key </param>
227+
/// <param name="quorum"> The quorum of guardians necessary to decrypt an election. Must be less than `number_of_guardians` </param>
228+
/// <param name="publicKey"> the public key of the election </param>
229+
/// <param name="commitmentHash"> the hash of the commitments the guardians make to each other </param>
230+
/// <param name="manifestHash"> the hash of the election metadata </param>
231+
/// <param name="config"> the context configuration</param>
232+
/// </summary>
223233
public unsafe CiphertextElectionContext(ulong numberOfGuardians,
224234
ulong quorum,
225235
ElementModP publicKey,
@@ -258,7 +268,17 @@ public unsafe CiphertextElectionContext(ulong numberOfGuardians,
258268
}
259269

260270

261-
271+
/// <summary>
272+
/// Makes a CiphertextElectionContext object.
273+
///
274+
/// <param name="numberOfGuardians"> The number of guardians necessary to generate the public key </param>
275+
/// <param name="quorum"> The quorum of guardians necessary to decrypt an election. Must be less than `number_of_guardians` </param>
276+
/// <param name="publicKey"> the public key of the election </param>
277+
/// <param name="commitmentHash"> the hash of the commitments the guardians make to each other </param>
278+
/// <param name="manifestHash"> the hash of the election metadata </param>
279+
/// <param name="config"> the context configuration</param>
280+
/// <param name="extendedData"> an unordered map of key value strings revelant to the consuming application </param>
281+
/// </summary>
262282
public unsafe CiphertextElectionContext(ulong numberOfGuardians,
263283
ulong quorum,
264284
ElementModP publicKey,

include/electionguard/manifest.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,8 @@ namespace electionguard
438438
explicit Party(const std::string &objectId, std::unique_ptr<InternationalizedText> name,
439439
const std::string &abbreviation, const std::string &color,
440440
const std::string &logoUri);
441+
explicit Party(const std::string &objectId, const std::string &abbreviation,
442+
const std::string &color, const std::string &logoUri);
441443
~Party();
442444

443445
Party &operator=(Party other);
@@ -1058,8 +1060,7 @@ namespace electionguard
10581060
static std::vector<std::unique_ptr<GeopoliticalUnit>>
10591061
copyGeopoliticalUnits(const Manifest &description);
10601062

1061-
static std::vector<std::unique_ptr<Candidate>>
1062-
copyCandidates(const Manifest &description);
1063+
static std::vector<std::unique_ptr<Candidate>> copyCandidates(const Manifest &description);
10631064

10641065
static std::vector<std::unique_ptr<BallotStyle>>
10651066
copyBallotStyles(const Manifest &description);

src/electionguard/manifest.cpp

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -700,19 +700,26 @@ namespace electionguard
700700
string color;
701701
string logoUri;
702702

703-
explicit Impl(const string &objectId) { this->object_id = objectId; }
703+
void init(const string &objectId)
704+
{
705+
this->object_id = objectId;
706+
vector<unique_ptr<Language>> text;
707+
this->name = make_unique<InternationalizedText>(move(text));
708+
}
709+
710+
explicit Impl(const string &objectId) { init(objectId); }
704711

705712
explicit Impl(const string &objectId, const string &abbreviation)
706713
: abbreviation(abbreviation)
707714
{
708-
this->object_id = objectId;
715+
init(objectId);
709716
}
710717

711718
explicit Impl(const string &objectId, const string &abbreviation, const string &color,
712719
const string &logoUri)
713720
: abbreviation(abbreviation), color(color), logoUri(logoUri)
714721
{
715-
this->object_id = objectId;
722+
init(objectId);
716723
}
717724

718725
explicit Impl(const string &objectId, unique_ptr<InternationalizedText> name,
@@ -764,6 +771,12 @@ namespace electionguard
764771
{
765772
}
766773

774+
Party::Party(const string &objectId, const string &abbreviation, const string &color,
775+
const string &logoUri)
776+
: pimpl(new Impl(objectId, abbreviation, color, logoUri))
777+
{
778+
}
779+
767780
Party::~Party() = default;
768781

769782
Party &Party::operator=(Party other)
@@ -1508,7 +1521,7 @@ namespace electionguard
15081521
if (name == nullptr) {
15091522
auto hash = hash_elems(
15101523
{electionScopeId, getElectionTypeString(type), timePointToIsoString(startDate),
1511-
timePointToIsoString(endDate), nullptr, geopoliticalUnitRefs, partyRefs,
1524+
timePointToIsoString(endDate), nullptr, nullptr, geopoliticalUnitRefs, partyRefs,
15121525
contestRefs, ballotStyleRefs});
15131526
Log::trace("Manifest:: NO NAME!", hash->toHex());
15141527
return hash;
@@ -1813,8 +1826,8 @@ namespace electionguard
18131826
vector<unique_ptr<ContestDescriptionWithPlaceholders>> contests,
18141827
vector<unique_ptr<BallotStyle>> ballotStyles, unique_ptr<ElementModQ> manifestHash)
18151828
: geopoliticalUnits(move(geopoliticalUnits)), candidates(move(candidates)),
1816-
contests(move(contests)),
1817-
ballotStyles(move(ballotStyles)), manifestHash(move(manifestHash))
1829+
contests(move(contests)), ballotStyles(move(ballotStyles)),
1830+
manifestHash(move(manifestHash))
18181831
{
18191832
}
18201833
};
@@ -1829,8 +1842,7 @@ namespace electionguard
18291842
vector<unique_ptr<ContestDescriptionWithPlaceholders>> contests,
18301843
vector<unique_ptr<BallotStyle>> ballotStyles, const ElementModQ &manifestHash)
18311844
: pimpl(new Impl(move(geopoliticalUnits), move(candidates), move(contests),
1832-
move(ballotStyles),
1833-
make_unique<ElementModQ>(manifestHash)))
1845+
move(ballotStyles), make_unique<ElementModQ>(manifestHash)))
18341846
{
18351847
}
18361848

src/electionguard/serialize.hpp

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,10 @@ namespace electionguard
6969
static unique_ptr<InternationalizedText> internationalizedTextFromJson(const json &j)
7070
{
7171
vector<unique_ptr<Language>> text;
72-
for (const auto &i : j["text"]) {
73-
text.push_back(languageFromJson(i));
72+
if (j.contains("text") && !j["text"].is_null()) {
73+
for (const auto &i : j["text"]) {
74+
text.push_back(languageFromJson(i));
75+
}
7476
}
7577
return make_unique<InternationalizedText>(move(text));
7678
}
@@ -212,24 +214,24 @@ namespace electionguard
212214
static unique_ptr<Party> partyFromJson(const json &j)
213215
{
214216
// TODO: other cases
215-
if (j.contains("name") && !j["name"].is_null()) {
216-
string abbreviation;
217-
string color;
218-
string logo_uri;
219-
if (!j["abbreviation"].is_null()) {
220-
abbreviation = j["abbreviation"].get<string>();
221-
}
222-
if (!j["color"].is_null()) {
223-
color = j["color"].get<string>();
224-
}
225-
if (!j["logo_uri"].is_null()) {
226-
logo_uri = j["logo_uri"].get<string>();
227-
}
228-
return make_unique<Party>(j["object_id"].get<string>(),
229-
internationalizedTextFromJson(j["name"]), abbreviation, color,
230-
logo_uri);
217+
string abbreviation;
218+
string color;
219+
string logo_uri;
220+
if (j.contains("abbreviation") && !j["abbreviation"].is_null()) {
221+
abbreviation = j["abbreviation"].get<string>();
231222
}
232-
return make_unique<Party>(j["object_id"].get<string>());
223+
if (j.contains("color") && !j["color"].is_null()) {
224+
color = j["color"].get<string>();
225+
}
226+
if (j.contains("logo_uri") && !j["logo_uri"].is_null()) {
227+
logo_uri = j["logo_uri"].get<string>();
228+
}
229+
auto name = j.contains("name") && !j["name"].is_null()
230+
? internationalizedTextFromJson(j["name"])
231+
: internationalizedTextFromJson(json("{}"));
232+
233+
return make_unique<Party>(j["object_id"].get<string>(), move(name), abbreviation, color,
234+
logo_uri);
233235
}
234236

235237
static json partiesToJson(const vector<reference_wrapper<Party>> &serializable)
@@ -280,7 +282,7 @@ namespace electionguard
280282
auto objectId = j["object_id"].get<string>();
281283
auto name = j.contains("name") && !j["name"].is_null()
282284
? internationalizedTextFromJson(j["name"])
283-
: nullptr;
285+
: internationalizedTextFromJson("{}");
284286
bool isWriteIn = j.contains("is_write_in") && !j["is_write_in"].is_null()
285287
? j["is_write_in"].get<bool>()
286288
: false;
@@ -289,8 +291,7 @@ namespace electionguard
289291
auto imageUri =
290292
j.contains("image_uri") && !j["image_uri"].is_null() ? j["image_uri"].get<string>() : "";
291293

292-
return make_unique<Candidate>(objectId, name != nullptr ? move(name) : nullptr, partyId,
293-
imageUri, isWriteIn);
294+
return make_unique<Candidate>(objectId, move(name), partyId, imageUri, isWriteIn);
294295
}
295296

296297
static json candidatesToJson(const vector<reference_wrapper<Candidate>> &serializable)

0 commit comments

Comments
 (0)