Skip to content

Commit

Permalink
fix build
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles Roddie committed Oct 10, 2023
1 parent 87cbc68 commit 93d430b
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 122 deletions.
86 changes: 68 additions & 18 deletions CBOR/PeterO/Cbor/CBORObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ public CBORObject this[int index]
/// <summary>Gets the value of a CBOR object by integer index in this
/// array or by CBOR object key in this map, or a default value if that
/// value is not found.</summary>
/// <param name='key'>An arbitrary object. If this is a CBOR map, this
/// <param name='cborkey'>An arbitrary CBORObject. If this is a CBOR map, this
/// parameter is converted to a CBOR object serving as the key to the
/// map or index to the array, and can be null. If this is a CBOR
/// array, the key must be an integer 0 or greater and less than the
Expand All @@ -569,26 +569,76 @@ public CBORObject this[int index]
/// or map. If this is a CBOR map, returns <c>null</c> (not
/// <c>CBORObject.Null</c> ) if an item with the given key doesn't
/// exist.</returns>
[RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")]
public CBORObject GetOrDefault(object key, CBORObject defaultValue) { // Suspect method. This is always used with a string key in the main library. But if the key is not a number it will return defaultValue.
public CBORObject GetOrDefault(CBORObject cborkey, CBORObject defaultValue) {
if (this.Type == CBORType.Array) {
int index;
if (key is int) {
index = (int)key;
} else {
var cborkey = CBORObject.FromObject(key);
if (!cborkey.IsNumber || !cborkey.AsNumber().CanFitInInt32()) {
if (!cborkey.IsNumber || !cborkey.AsNumber().CanFitInInt32()) {
return defaultValue;
}
index = cborkey.AsNumber().ToInt32Checked();
}
int index = cborkey.AsNumber().ToInt32Checked();
IList<CBORObject> list = this.AsList();
return (index < 0 || index >= list.Count) ? defaultValue :
list[index];
}
if (this.Type == CBORType.Map) {
IDictionary<CBORObject, CBORObject> map = this.AsMap();
var ckey = CBORObject.FromObject(key);
var ckey = cborkey;
return PropertyMap.GetOrDefault(map, ckey, defaultValue);
}
return defaultValue;
}

/// <summary>Gets the value of a CBOR object by integer index in this
/// array, or a default value if that
/// value is not found.</summary>
/// <param name='key'>An arbitrary object. If this is a CBOR map, this
/// parameter is converted to a CBOR object serving as the key to the
/// map or index to the array, and can be null. If this is a CBOR
/// array, the key must be an integer 0 or greater and less than the
/// size of the array, and may be any object convertible to a CBOR
/// integer.</param>
/// <param name='defaultValue'>A value to return if an item with the
/// given key doesn't exist, or if the CBOR object is an array and the
/// key is not an integer 0 or greater and less than the size of the
/// array.</param>
/// <returns>The CBOR object referred to by index or key in this array
/// or map. If this is a CBOR map, returns <c>null</c> (not
/// <c>CBORObject.Null</c> ) if an item with the given key doesn't
/// exist.</returns>
[RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")]
public CBORObject GetOrDefault(int key, CBORObject defaultValue) {
if (this.Type == CBORType.Array) {
int index = key;
IList<CBORObject> list = this.AsList();
return (index < 0 || index >= list.Count) ? defaultValue :
list[index];
}
if (this.Type == CBORType.Map) {
IDictionary<CBORObject, CBORObject> map = this.AsMap();
var ckey = FromInt(key);
return PropertyMap.GetOrDefault(map, ckey, defaultValue);
}
return defaultValue;
}

/// <summary>Gets the value of a CBOR object by string key in a map, or a default value if that
/// value is not found.</summary>
/// <param name='key'>An arbitrary string. If this is a CBOR map, this
/// parameter is converted to a CBOR object serving as the key to the
/// map or index to the array, and can be null. If this is a CBOR
/// array, defaultValue is returned.</param>
/// <param name='defaultValue'>A value to return if an item with the
/// given key doesn't exist, or if the CBOR object is an array.</param>
/// <returns>The CBOR object referred to by index or key in this array
/// or map. If this is a CBOR map, returns <c>null</c> (not
/// <c>CBORObject.Null</c> ) if an item with the given key doesn't
/// exist.</returns>
public CBORObject GetOrDefault(string key, CBORObject defaultValue) {
if (this.Type == CBORType.Array) {
return defaultValue;
}
if (this.Type == CBORType.Map) {
IDictionary<CBORObject, CBORObject> map = this.AsMap();
var ckey = FromString(key);
return PropertyMap.GetOrDefault(map, ckey, defaultValue);
}
return defaultValue;
Expand Down Expand Up @@ -4110,7 +4160,7 @@ public static void WriteJSON(object obj, Stream outputStream) {
/// value if the key doesn't exist.</para>
/// <para>NOTE: This method can't be used to add a tag to an existing
/// CBOR object. To create a CBOR object with a given tag, call the
/// <c>CBORObject.FromObjectAndTag</c> method and pass the CBOR object
/// <c>CBORObject.FromCBORObjectAndTag</c> method and pass the CBOR object
/// and the desired tag number to that method.</para></summary>
/// <param name='key'>An object representing the key, which will be
/// converted to a CBORObject. Can be null, in which case this value is
Expand Down Expand Up @@ -4163,7 +4213,7 @@ public CBORObject Add(object key, object valueOb) {
/// <para>NOTE: This method
/// can't be used to add a tag to an existing CBOR object. To create a
/// CBOR object with a given tag, call the
/// <c>CBORObject.FromObjectAndTag</c>
/// <c>CBORObject.FromCBORObjectAndTag</c>
/// method and pass the CBOR object
/// and the desired tag number to that method.</para>
/// </summary>
Expand All @@ -4178,7 +4228,7 @@ public CBORObject Add(object key, object valueOb) {
/// Note the chaining behavior made possible by this method.</para>
/// <code>CBORObject obj = CBORObject.NewArray() .Add(CBORObject.False)
/// .Add(CBORObject.FromObject(5)) .Add(CBORObject.FromObject("text
/// string")) .Add(CBORObject.FromObjectAndTag(9999, 1));</code>
/// string")) .Add(CBORObject.FromCBORObjectAndTag(9999, 1));</code>
/// .
/// </example>
public CBORObject Add(CBORObject obj) {
Expand All @@ -4194,7 +4244,7 @@ public CBORObject Add(CBORObject obj) {
/// the end of this array.</para>
/// <para>NOTE: This method can't be used
/// to add a tag to an existing CBOR object. To create a CBOR object
/// with a given tag, call the <c>CBORObject.FromObjectAndTag</c>
/// with a given tag, call the <c>CBORObject.FromCBORObjectAndTag</c>
/// method and pass the CBOR object and the desired tag number to that
/// method.</para>
/// </summary>
Expand All @@ -4210,7 +4260,7 @@ public CBORObject Add(CBORObject obj) {
/// CBOR objects, one of which has a custom CBOR tag, to that array.
/// Note the chaining behavior made possible by this method.</para>
/// <code>CBORObject obj = CBORObject.NewArray() .Add(CBORObject.False) .Add(5)
/// .Add("text string") .Add(CBORObject.FromObjectAndTag(9999, 1));</code>
/// .Add("text string") .Add(CBORObject.FromCBORObjectAndTag(9999, 1));</code>
/// .
/// </example>
[RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")]
Expand Down Expand Up @@ -5617,7 +5667,7 @@ public string ToJSONString() {
/// 3.4.5.3 of RFC 8949). A byte string will instead be converted to
/// traditional base64 without whitespace and with padding if it has
/// tag 22, or base16 for tag 23. (To create a CBOR object with a given
/// tag, call the <c>CBORObject.FromObjectAndTag</c>
/// tag, call the <c>CBORObject.FromCBORObjectAndTag</c>
/// method and pass
/// the CBOR object and the desired tag number to that method.)</item>
/// <item>Rational numbers will be converted to their exact form, if
Expand Down
18 changes: 9 additions & 9 deletions CBOR/docs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1904,7 +1904,7 @@
<para>NOTE: This method
can't be used to add a tag to an existing CBOR object. To create a
CBOR object with a given tag, call the
<c>CBORObject.FromObjectAndTag</c> method and pass the CBOR object
<c>CBORObject.FromCBORObjectAndTag</c> method and pass the CBOR object
and the desired tag number to that method.</para>
</summary>
<param name='obj'>The parameter <paramref name='obj'/> is a CBOR
Expand All @@ -1917,7 +1917,7 @@
Note the chaining behavior made possible by this method.</para>
<code>CBORObject obj = CBORObject.NewArray() .Add(CBORObject.False)
.Add(CBORObject.FromObject(5)) .Add(CBORObject.FromObject("text
string")) .Add(CBORObject.FromObjectAndTag(9999, 1));</code> .
string")) .Add(CBORObject.FromCBORObjectAndTag(9999, 1));</code> .
</example>

</doc>
Expand All @@ -1927,7 +1927,7 @@
the end of this array.</para>
<para>NOTE: This method can't be used
to add a tag to an existing CBOR object. To create a CBOR object
with a given tag, call the <c>CBORObject.FromObjectAndTag</c> method and pass the CBOR object and the desired tag number to that
with a given tag, call the <c>CBORObject.FromCBORObjectAndTag</c> method and pass the CBOR object and the desired tag number to that
method.</para>
</summary>
<param name='obj'>A CBOR object (or an object convertible to a CBOR
Expand All @@ -1940,7 +1940,7 @@
CBOR objects, one of which has a custom CBOR tag, to that array.
Note the chaining behavior made possible by this method.</para>
<code>CBORObject obj = CBORObject.NewArray() .Add(CBORObject.False) .Add(5)
.Add("text string") .Add(CBORObject.FromObjectAndTag(9999, 1));</code> .
.Add("text string") .Add(CBORObject.FromCBORObjectAndTag(9999, 1));</code> .
</example>

</doc>
Expand All @@ -1950,7 +1950,7 @@
value if the key doesn't exist.</para>
<para>NOTE: This method can't be used to add a tag to an existing
CBOR object. To create a CBOR object with a given tag, call the
<c>CBORObject.FromObjectAndTag</c> method and pass the CBOR object
<c>CBORObject.FromCBORObjectAndTag</c> method and pass the CBOR object
and the desired tag number to that method.</para></summary>
<param name='key'>An object representing the key, which will be
converted to a CBORObject. Can be null, in which case this value is
Expand Down Expand Up @@ -3871,7 +3871,7 @@
<returns>A CBORObject object.</returns>

</doc>
<doc name="M:PeterO.Cbor.CBORObject.FromObjectAndTag(System.Object,PeterO.Numbers.EInteger)">
<doc name="M:PeterO.Cbor.CBORObject.FromCBORObjectAndTag(System.Object,PeterO.Numbers.EInteger)">

<summary>Generates a CBOR object from an arbitrary object and gives
the resulting object a tag in addition to its existing tags (the
Expand Down Expand Up @@ -3901,7 +3901,7 @@
<exception cref='ArgumentNullException'>The parameter <paramref name='bigintTag'/> is null.</exception>

</doc>
<doc name="M:PeterO.Cbor.CBORObject.FromObjectAndTag(System.Object,System.Int32)">
<doc name="M:PeterO.Cbor.CBORObject.FromCBORObjectAndTag(System.Object,System.Int32)">

<summary>Generates a CBOR object from an arbitrary object and gives
the resulting object a tag in addition to its existing tags (the
Expand Down Expand Up @@ -3929,7 +3929,7 @@
<exception cref='ArgumentException'>The parameter <paramref name='smallTag'/> is less than 0.</exception>

</doc>
<doc name="M:PeterO.Cbor.CBORObject.FromObjectAndTag(System.Object,System.UInt64)">
<doc name="M:PeterO.Cbor.CBORObject.FromCBORObjectAndTag(System.Object,System.UInt64)">

<summary>Generates a CBOR object from an arbitrary object and gives
the resulting object a tag.</summary>
Expand Down Expand Up @@ -4770,7 +4770,7 @@
3.4.5.3 of RFC 8949). A byte string will instead be converted to
traditional base64 without whitespace and with padding if it has
tag 22, or base16 for tag 23. (To create a CBOR object with a given
tag, call the <c>CBORObject.FromObjectAndTag</c> method and pass
tag, call the <c>CBORObject.FromCBORObjectAndTag</c> method and pass
the CBOR object and the desired tag number to that method.)</item>
<item>Rational numbers will be converted to their exact form, if
possible, otherwise to a high-precision approximation. (The
Expand Down
Loading

0 comments on commit 93d430b

Please sign in to comment.