Skip to content

Commit

Permalink
Merge pull request #1079 from adamreeve/nullable_string_array
Browse files Browse the repository at this point in the history
Add nullable annotation to nullable string array parameters in internal methods
  • Loading branch information
badcel authored Jun 12, 2024
2 parents 9b11472 + c3b3927 commit 7545f0f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ public RenderableParameter Convert(GirModel.Parameter parameter)
private static RenderableParameter SizeBasedArray(GirModel.Parameter parameter)
{
var length = parameter.AnyTypeOrVarArgs.AsT0.AsT1.Length ?? throw new Exception("Length must not be null");
var typeName = Model.ArrayType.GetName(parameter.AnyTypeOrVarArgs.AsT0.AsT1) + Nullable.Render(parameter);

return new RenderableParameter(
Attribute: MarshalAs.UnmanagedLpArray(sizeParamIndex: length),
Direction: string.Empty,
NullableTypeName: Model.ArrayType.GetName(parameter.AnyTypeOrVarArgs.AsT0.AsT1),
NullableTypeName: typeName,
Name: Model.Parameter.GetName(parameter)
);
}
Expand Down
18 changes: 18 additions & 0 deletions src/Native/GirTestLib/girtest-string-array-tester.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,24 @@ gchar* girtest_string_array_tester_utf8_return_element_parameter_null_terminated
return g_strdup(data[position]);
}

/**
* girtest_string_array_tester_utf8_return_element_parameter_transfer_none_nullable_with_size:
* @data: (array length=data_size) (element-type utf8) (transfer none) (nullable): Array
* @data_size: The number of values in @data
* @position: The index to return
*
* Returns the string at the given position.
*
* Returns: (transfer full) (nullable): The string of the array from the given position
*/
gchar* girtest_string_array_tester_utf8_return_element_parameter_transfer_none_nullable_with_size(const gchar** data, int data_size, int position)
{
if(!data)
return NULL;

return g_strdup(data[position]);
}

/**
* girtest_string_array_tester_filename_return_element_parameter_null_terminated_transfer_none:
* @data: (array zero-terminated=1) (element-type filename) (transfer none): Array
Expand Down
1 change: 1 addition & 0 deletions src/Native/GirTestLib/girtest-string-array-tester.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ const char** girtest_string_array_tester_filename_return_transfer_none_nullable(
gchar* girtest_string_array_tester_utf8_return_element_parameter_null_terminated_transfer_none(const gchar** data, int position);
gchar* girtest_string_array_tester_filename_return_element_parameter_null_terminated_transfer_none(const gchar** data, int position);
gchar* girtest_string_array_tester_utf8_return_element_parameter_null_terminated_transfer_none_nullable(const gchar** data, int position);
gchar* girtest_string_array_tester_utf8_return_element_parameter_transfer_none_nullable_with_size(const gchar** data, int data_size, int position);
gchar* girtest_string_array_tester_filename_return_element_parameter_null_terminated_transfer_none_nullable(const gchar** data, int position);
G_END_DECLS
9 changes: 9 additions & 0 deletions src/Tests/Libs/GirTest-0.1.Tests/StringArrayTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ public void Utf8ParameterNullTerminatedStringArrayTransferNoneNullable()
StringArrayTester.Utf8ReturnElementParameterNullTerminatedTransferNoneNullable(null, 1).Should().BeNull();
}

[TestMethod]
public void Utf8ParameterTransferNoneNullableWithSize()
{
var array = new[] { "FOO", "BAR" };
StringArrayTester.Utf8ReturnElementParameterTransferNoneNullableWithSize(array, 2, 0).Should().Be(array[0]);
StringArrayTester.Utf8ReturnElementParameterTransferNoneNullableWithSize(array, 2, 1).Should().Be(array[1]);
StringArrayTester.Utf8ReturnElementParameterTransferNoneNullableWithSize(null, 0, 1).Should().BeNull();
}

[TestMethod]
public void PlatformReturnNullTerminatedStringArrayTransferNone()
{
Expand Down

0 comments on commit 7545f0f

Please sign in to comment.