Skip to content

Commit

Permalink
Make FieldDescriptor.IsPacked work appropriately.
Browse files Browse the repository at this point in the history
This is a bit of a grotty hack, as we need to sort of fake proto2 field presence, but with only a proto3 version of the descriptor messages (a bit like oneof detection).
Should be okay, but will need to be careful of this if we ever implement proto2.
jskeet committed Aug 8, 2015
1 parent e58cdbd commit 547d8e8
Showing 3 changed files with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -213,6 +213,6 @@ public void FieldDescriptor_NotFound()
var descriptor = TestAllTypes.Descriptor;
Assert.Throws<KeyNotFoundException>(() => descriptor.Fields[999999].ToString());
Assert.Throws<KeyNotFoundException>(() => descriptor.Fields["not found"].ToString());
}
}
}
}
8 changes: 5 additions & 3 deletions csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs
Original file line number Diff line number Diff line change
@@ -168,14 +168,16 @@ public bool IsMap
get { return fieldType == FieldType.Message && messageType.Proto.Options != null && messageType.Proto.Options.MapEntry; }
}

// TODO(jonskeet): Check whether this is correct with proto3, where we default to packed...

/// <summary>
/// Returns <c>true</c> if this field is a packed, repeated field; <c>false</c> otherwise.
/// </summary>
public bool IsPacked
{
get { return Proto.Options != null && Proto.Options.Packed; }
// Note the || rather than && here - we're effectively defaulting to packed, because that *is*
// the default in proto3, which is all we support. We may give the wrong result for the protos
// within descriptor.proto, but that's okay, as they're never exposed and we don't use IsPacked
// within the runtime.
get { return Proto.Options == null || Proto.Options.Packed; }
}

/// <summary>
12 changes: 12 additions & 0 deletions csharp/src/Google.Protobuf/Reflection/PartialClasses.cs
Original file line number Diff line number Diff line change
@@ -44,4 +44,16 @@ partial void OnConstruction()
OneofIndex = -1;
}
}

internal partial class FieldOptions
{
// We can't tell the difference between "explicitly set to false" and "not set"
// in proto3, but we need to tell the difference for FieldDescriptor.IsPacked.
// This won't work if we ever need to support proto2, but at that point we'll be
// able to remove this hack and use field presence instead.
partial void OnConstruction()
{
Packed = true;
}
}
}

0 comments on commit 547d8e8

Please sign in to comment.