Skip to content

Commit

Permalink
Follow Nexus changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincent Wilms committed Feb 4, 2025
1 parent f68764f commit e0e6be8
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 110 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## v2.0.0-beta.54 - 2025-02-04

- Follow Nexus changes

## v2.0.0-beta.53 - 2025-02-03

- Fix version
Expand Down
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<PackageVersion Include="BenchmarkDotNet" Version="0.14.0" />
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer" Version="5.1.0" />
<PackageVersion Include="Moq" Version="4.20.72" />
<PackageVersion Include="Nexus.Extensibility" Version="2.0.0-beta.48" />
<PackageVersion Include="Nexus.Extensibility" Version="2.0.0-beta.50" />
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="9.0.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageVersion Include="NSwag.AspNetCore" Version="14.1.0" />
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
apollo3zehn-package-management==1.0.0-b5
build
fastapi[standard]
nexus-extensibility==2.0.0-b48
nexus-extensibility==2.0.0-b50
pytest-asyncio
wheel
2 changes: 1 addition & 1 deletion src/Nexus.Sources.Remote/Remote.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public partial class Remote : IDataSource<RemoteSettings>, IUpgradableDataSource

private DataSourceContext<RemoteSettings> Context { get; set; } = default!;

public static async Task<JsonElement> UpgradeSourceConfigurationAsync(
public async Task<JsonElement> UpgradeSourceConfigurationAsync(
JsonElement configuration,
CancellationToken cancellationToken
)
Expand Down
2 changes: 1 addition & 1 deletion src/agent/python/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
apollo3zehn-package-management==1.0.0-b5
fastapi[standard]
nexus-extensibility==2.0.0-beta.48
nexus-extensibility==2.0.0-beta.50
66 changes: 11 additions & 55 deletions src/remoting/dotnet/Remoting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,18 @@ CancellationToken cancellationToken
throw new Exception("The connection must be initialized with a type before invoking other methods.");

var dataSourceType = _getDataSourceType(_sourceTypeName);
var configuration = @params[0];
var upgradedConfiguration = @params[0];

var upgradedConfiguration = await InternalUpgradeAllAsync(
dataSourceType,
@params[0]
);
if (dataSourceType.IsAssignableTo(dataSourceType))
{
var upgradableDataSource = (IUpgradableDataSource)Activator.CreateInstance(dataSourceType)!;
var timeoutTokenSource = new CancellationTokenSource(TimeSpan.FromMinutes(1));

upgradedConfiguration = await upgradableDataSource.UpgradeSourceConfigurationAsync(
@params[0],
timeoutTokenSource.Token
);
}

result = JsonSerializer.SerializeToNode(upgradedConfiguration, Utilities.JsonSerializerOptions);
}
Expand Down Expand Up @@ -450,56 +456,6 @@ private int ReadSize(NetworkStream currentStream)
var size = BitConverter.ToInt32(sizeBuffer);
return size;
}

/* Copy of Nexus.Services.UpgradeConfigurationService.InternalUpgradeAllAsync */
private static async Task<JsonElement> InternalUpgradeAllAsync(
Type sourceType,
JsonElement configuration
)
{
/* Collect potential types in the inheritance chain */
var upgradableDataSourceTypes = new List<Type>();
var currentType1 = sourceType;

while (!(currentType1 is null || currentType1 == typeof(object)))
{
var interfaceTypes = currentType1.GetInterfaces();

if (interfaceTypes.Contains(typeof(IUpgradableDataSource)))
upgradableDataSourceTypes.Add(currentType1);

currentType1 = currentType1.BaseType;
}

upgradableDataSourceTypes.Reverse();

/* Invoke UpgradeSourceConfigurationAsync */
var upgradedConfiguration = configuration;

foreach (var currentType2 in upgradableDataSourceTypes)
{
var timeoutTokenSource = new CancellationTokenSource(TimeSpan.FromMinutes(1));

var methodInfo = currentType2
.GetMethod(nameof(IUpgradableDataSource.UpgradeSourceConfigurationAsync), BindingFlags.Public | BindingFlags.Static)!;

/* This happens when base class implements IUpgradableDataSource and
* sub class does not reimplement it, which is fine.
*/
if (methodInfo is null)
continue;

upgradedConfiguration = await (Task<JsonElement>)methodInfo.Invoke(
default,
[
upgradedConfiguration,
timeoutTokenSource.Token
]
)!;
}

return upgradedConfiguration;
}
}

internal static class Utilities
Expand Down
39 changes: 4 additions & 35 deletions src/remoting/python/nexus_remoting/_remoting.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,11 @@ async def _process_invocation(self, request: dict[str, Any]) \
raise Exception("The connection must be initialized with a type before invoking other methods.")

data_source_type = self._get_data_source_type(self._source_type_name)
upgraded_configuration = params[0]

upgraded_configuration = await self.internal_upgrade_all(
data_source_type,
params[0]
)
if issubclass(data_source_type, IUpgradableDataSource):
upgradable_data_source = cast(IUpgradableDataSource, data_source_type())
upgraded_configuration = await upgradable_data_source.upgrade_source_configuration(params[0])

result = upgraded_configuration

Expand Down Expand Up @@ -327,37 +327,6 @@ async def _read_size(self, reader: asyncio.StreamReader) -> int:

return size

async def internal_upgrade_all(self, source_type: type, configuration: Any):

# Collect potential types in the inheritance chain
upgradable_data_source_types: list[type[IUpgradableDataSource]] = []
current_type = source_type

# The following implementation just follows the first base type
# which is not IDataSource or IUpgradableDataSource
while not (current_type is None):

if issubclass(current_type, IUpgradableDataSource) and IUpgradableDataSource in current_type.__bases__:
upgradable_data_source_types.append(current_type)

current_type = next(
(
base for base in current_type.__bases__ \
if base is not IDataSource and base is not IUpgradableDataSource
),
None
)

upgradable_data_source_types.reverse()

# Invoke upgrade_source_configuration
upgraded_configuration = configuration

for current_type in upgradable_data_source_types:
upgraded_configuration = await current_type.upgrade_source_configuration(configuration)

return upgraded_configuration

async def _send_to_server(message: Any, writer: asyncio.StreamWriter):

encoded = JsonEncoder.encode(message, _json_encoder_options)
Expand Down
2 changes: 1 addition & 1 deletion src/remoting/python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@
},
python_requires=">=3.10",
install_requires=[
"nexus-extensibility>=2.0.0b48"
"nexus-extensibility>=2.0.0b50"
]
)
2 changes: 1 addition & 1 deletion tests/Nexus.Sources.Remote.Tests/RemoteTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public async Task CanUpgradeSourceConfiguration(string language)
var configuration = CreateSettings(language);

// Act
var upgradedConfiguration = await Remote.UpgradeSourceConfigurationAsync(
var upgradedConfiguration = await new Remote().UpgradeSourceConfigurationAsync(
JsonSerializer.SerializeToElement(configuration, Utilities.JsonSerializerOptions),
CancellationToken.None
);
Expand Down
13 changes: 5 additions & 8 deletions tests/Nexus.Sources.Remote.Tests/dotnet/v1/Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@ string LogMessage

public class TestBase : IUpgradableDataSource
{
public static Task<JsonElement> UpgradeSourceConfigurationAsync(
public virtual Task<JsonElement> UpgradeSourceConfigurationAsync(
JsonElement configuration,
CancellationToken cancellationToken
)
{
var jsonNode = JsonSerializer.SerializeToNode(configuration)!;

jsonNode["foo"] = jsonNode["logMessage"]!.DeepClone();

var upgradedConfiguration = JsonSerializer.SerializeToElement(jsonNode, JsonSerializerOptions.Web);

return Task.FromResult(upgradedConfiguration);
Expand All @@ -32,22 +30,21 @@ CancellationToken cancellationToken
/* Note: Inherit from StructuredFileDataSource would be possible
* but collides with ReadAndModifyNexusData method
*/
public class Test : TestBase, IDataSource<TestSettings>, IUpgradableDataSource
public class Test : TestBase, IDataSource<TestSettings>
{
private DataSourceContext<TestSettings> _context = default!;

public static new Task<JsonElement> UpgradeSourceConfigurationAsync(
public override async Task<JsonElement> UpgradeSourceConfigurationAsync(
JsonElement configuration,
CancellationToken cancellationToken
)
{
configuration = await base.UpgradeSourceConfigurationAsync(configuration, cancellationToken);
var jsonNode = JsonSerializer.SerializeToNode(configuration)!;

jsonNode["bar"] = jsonNode["logMessage"]!.DeepClone();

var upgradedConfiguration = JsonSerializer.SerializeToElement(jsonNode, JsonSerializerOptions.Web);

return Task.FromResult(upgradedConfiguration);
return upgradedConfiguration;
}

public Task SetContextAsync(
Expand Down
11 changes: 6 additions & 5 deletions tests/Nexus.Sources.Remote.Tests/python/v1/src/foo/test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from abc import abstractmethod
import glob
import os
from dataclasses import dataclass
Expand All @@ -19,17 +20,17 @@ class TestSettings():

class TestBase(IUpgradableDataSource):

@staticmethod
async def upgrade_source_configuration(configuration: Any) -> Any:
@abstractmethod
async def upgrade_source_configuration(self, configuration: Any) -> Any:
configuration["foo"] = configuration["logMessage"]

class Test(TestBase, IDataSource[TestSettings], IUpgradableDataSource):
class Test(TestBase, IDataSource[TestSettings]):

_root: str

@staticmethod
async def upgrade_source_configuration(configuration: Any) -> Any:
async def upgrade_source_configuration(self, configuration: Any) -> Any:

await super().upgrade_source_configuration(configuration)
configuration["bar"] = configuration["logMessage"]

return configuration
Expand Down
2 changes: 1 addition & 1 deletion version.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"version": "2.0.0",
"suffix": "beta.53"
"suffix": "beta.54"
}

0 comments on commit e0e6be8

Please sign in to comment.