Skip to content

Commit

Permalink
Do not require baseconfig (#129)
Browse files Browse the repository at this point in the history
Instead use a generic type parameter
  • Loading branch information
einarmo authored Oct 14, 2021
1 parent 65e28f7 commit 9297bb3
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 17 deletions.
2 changes: 1 addition & 1 deletion ExampleExtractor/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using System.Collections.Generic;
using Cognite.Extractor.Common;

class MyExtractor : BaseExtractor
class MyExtractor : BaseExtractor<BaseConfig>
{
public MyExtractor(BaseConfig config, IServiceProvider provider, CogniteDestination destination)
: base(config, provider, destination)
Expand Down
4 changes: 2 additions & 2 deletions ExtractorUtils.Test/integration/ExtractorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class MyConfig : BaseConfig
public string Prefix { get; set; }
}

class TestExtractor : BaseExtractor
class TestExtractor : BaseExtractor<MyConfig>
{
public string TSId { get; private set; }
public List<string> CreatedEvents { get; private set; }
Expand Down Expand Up @@ -126,7 +126,7 @@ await Destination.CogniteClient.Events.DeleteAsync(new EventDelete
}
}

class NoCdfExtractor : BaseExtractor
class NoCdfExtractor : BaseExtractor<MyConfig>
{
public bool Started { get; private set; }
public int Iter { get; private set; }
Expand Down
12 changes: 7 additions & 5 deletions ExtractorUtils/BaseExtractor.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Cognite.Extensions;
using Cognite.Extractor.Common;
using Cognite.Extractor.Configuration;
using Cognite.Extractor.Utils;
using CogniteSdk;
using Microsoft.Extensions.Logging;
Expand All @@ -14,12 +15,13 @@ namespace Cognite.Extractor.Utils
/// <summary>
/// Base class for extractors writing timeseries, events or datapoints.
/// </summary>
public abstract class BaseExtractor : IDisposable, IAsyncDisposable
public abstract class BaseExtractor<TConfig> : IDisposable, IAsyncDisposable
where TConfig : VersionedConfig
{
/// <summary>
/// Configuration object
/// </summary>
protected BaseConfig Config { get; }
protected TConfig Config { get; }
/// <summary>
/// CDF destination
/// </summary>
Expand Down Expand Up @@ -56,7 +58,7 @@ public abstract class BaseExtractor : IDisposable, IAsyncDisposable
/// </summary>
protected ExtractionRun Run { get; }

private readonly ILogger<BaseExtractor> _logger;
private readonly ILogger<BaseExtractor<TConfig>> _logger;

/// <summary>
/// Constructor
Expand All @@ -66,7 +68,7 @@ public abstract class BaseExtractor : IDisposable, IAsyncDisposable
/// <param name="provider">Service provider</param>
/// <param name="run">Optional extraction run</param>
public BaseExtractor(
BaseConfig config,
TConfig config,
IServiceProvider provider,
CogniteDestination destination = null,
ExtractionRun run = null)
Expand All @@ -78,7 +80,7 @@ public BaseExtractor(
}
Provider = provider;
Run = run;
_logger = provider.GetService<ILogger<BaseExtractor>>();
_logger = provider.GetService<ILogger<BaseExtractor<TConfig>>>();
}

/// <summary>
Expand Down
10 changes: 5 additions & 5 deletions ExtractorUtils/ExtractorRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static async Task Run<TConfig, TExtractor>(
TConfig config = null,
bool requireDestination = true)
where TConfig : VersionedConfig
where TExtractor : BaseExtractor
where TExtractor : BaseExtractor<TConfig>
{
int waitRepeats = 1;

Expand Down Expand Up @@ -124,14 +124,14 @@ void CancelKeyPressHandler(object sender, ConsoleCancelEventArgs eArgs)
}

services.AddSingleton<TExtractor>();
services.AddSingleton<BaseExtractor>(prov => prov.GetRequiredService<TExtractor>());
services.AddSingleton<BaseExtractor<TConfig>>(prov => prov.GetRequiredService<TExtractor>());
DateTime startTime = DateTime.UtcNow;
ILogger<BaseExtractor> log;
ILogger<BaseExtractor<TConfig>> log;

var provider = services.BuildServiceProvider();
await using (provider.ConfigureAwait(false))
{
log = new NullLogger<BaseExtractor>();
log = new NullLogger<BaseExtractor<TConfig>>();
TExtractor extractor = null;
try
{
Expand All @@ -142,7 +142,7 @@ void CancelKeyPressHandler(object sender, ConsoleCancelEventArgs eArgs)
}
if (addLogger)
{
log = provider.GetRequiredService<ILogger<BaseExtractor>>();
log = provider.GetRequiredService<ILogger<BaseExtractor<TConfig>>>();
Serilog.Log.Logger = provider.GetRequiredService<Serilog.ILogger>();
}
extractor = provider.GetRequiredService<TExtractor>();
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ using Cognite.Extractor.Utils;
using Cognite.Extensions;
using CogniteSdk;
class MyExtractor : BaseExtractor
class MyExtractor : BaseExtractor<BaseConfig>
{
public MyExtractor(BaseConfig config, CogniteDestination destination)
: base(config, destination)
Expand Down Expand Up @@ -95,7 +95,7 @@ class Program
{
static void Main()
{
ExtractorRunner.Run<BaseConfig>(
ExtractorRunner.Run<BaseConfig, MyExtractor>(
"config.yml",
new[] { 1 },
"my-extractor",
Expand Down
4 changes: 2 additions & 2 deletions docfx_project/tutorials/base-extractor.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ using Cognite.Extractor.Utils;
using Cognite.Extensions;
using CogniteSdk;

class MyExtractor : BaseExtractor
class MyExtractor : BaseExtractor<BaseConfig>
{
public MyExtractor(BaseConfig config, CogniteDestination destination)
: base(config, destination)
Expand Down Expand Up @@ -49,7 +49,7 @@ class Program
{
static async Task Main()
{
await ExtractorRunner.Run<BaseConfig>(
await ExtractorRunner.Run<BaseConfig, MyExtractor>(
"config.yml",
new[] { 1 },
"my-extractor",
Expand Down

0 comments on commit 9297bb3

Please sign in to comment.