Skip to content

Commit

Permalink
Merge pull request #2122 from petrlicman/LINQreenum#2040
Browse files Browse the repository at this point in the history
LinQ reenumeration fixes
  • Loading branch information
petrlicman authored Nov 15, 2023
2 parents 58769ee + 4049f46 commit 0f5e1a7
Show file tree
Hide file tree
Showing 50 changed files with 222 additions and 213 deletions.
5 changes: 5 additions & 0 deletions Src/Witsml/Extensions/ListExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,10 @@ public static List<T> AsSingletonList<T>(this T item)
{
return new List<T> { item };
}

public static ICollection<T> AsCollection<T>(this IList<T> list)
{
return list;
}
}
}
2 changes: 1 addition & 1 deletion Src/WitsmlExplorer.Api/Configuration/ICredentials.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ public interface ICredentials
{
internal string UserId { get; }
internal string Password { get; }

public bool IsNullOrEmpty();
}
}
13 changes: 3 additions & 10 deletions Src/WitsmlExplorer.Api/Configuration/ServerCredentials.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@

namespace WitsmlExplorer.Api.Configuration
{
public class ServerCredentials : ICredentials, IEquatable<ServerCredentials>
public class ServerCredentials : BasicCredentials, ICredentials, IEquatable<ServerCredentials>
{
public ServerCredentials() { }

public ServerCredentials(string host, string userid, string password)
public ServerCredentials(string host, string userid, string password) : base(userid, password)
{
Host = new Uri(host);
UserId = userid;
Password = password;
}
public ServerCredentials(string host, ICredentials creds)
{
Expand All @@ -21,12 +19,7 @@ public ServerCredentials(string host, ICredentials creds)
Password = creds.Password;
}
public Uri Host { get; init; }
public string UserId { get; init; }
public string Password { get; init; }
public bool IsCredsNullOrEmpty()
{
return string.IsNullOrEmpty(UserId) || string.IsNullOrEmpty(Password);
}

public bool Equals(ServerCredentials other)
{
return (Host.EqualsIgnoreCase(other.Host)) &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace WitsmlExplorer.Api.Configuration
{
public interface IWitsmlSystemCredentials
{
public ServerCredentials[] WitsmlCreds { get; set; }
public IReadOnlyCollection<ServerCredentials> WitsmlCreds { get; set; }
}

/// <summary>
Expand All @@ -30,7 +30,7 @@ public interface IWitsmlSystemCredentials
/// </summary>
public class WitsmlSystemCredentials : IWitsmlSystemCredentials, IDisposable
{
public ServerCredentials[] WitsmlCreds { get; set; }
public IReadOnlyCollection<ServerCredentials> WitsmlCreds { get; set; }
private IDisposable _unregister;

public WitsmlSystemCredentials(IConfiguration configuration)
Expand All @@ -48,13 +48,13 @@ private void Bind(IConfiguration configuration)
{
ServerCredentials cred = new();
rule.Bind(cred);
if (!cred.IsCredsNullOrEmpty() && cred.Host != null)
if (!cred.IsNullOrEmpty() && cred.Host != null)
{
credsList.Add(cred);
}
}

WitsmlCreds = credsList.ToArray();
WitsmlCreds = credsList;
_unregister?.Dispose();
_unregister = configuration.GetReloadToken().RegisterChangeCallback((_) => Bind(configuration), null);
}
Expand Down
2 changes: 1 addition & 1 deletion Src/WitsmlExplorer.Api/HttpHandlers/JobHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private static bool IsAdminOrDeveloper(string token)
{
JwtSecurityTokenHandler handler = new();
JwtSecurityToken jwt = handler.ReadJwtToken(token);
IEnumerable<string> userRoles = jwt.Claims.Where(n => n.Type == "roles").Select(n => n.Value);
List<string> userRoles = jwt.Claims.Where(n => n.Type == "roles").Select(n => n.Value).ToList();
return userRoles.Contains(AuthorizationPolicyRoles.ADMIN) || userRoles.Contains(AuthorizationPolicyRoles.DEVELOPER);
}

Expand Down
2 changes: 1 addition & 1 deletion Src/WitsmlExplorer.Api/Jobs/AnalyzeGapJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public record AnalyzeGapJob : Job
/// <summary>
/// Array of mnemonics names
/// </summary>
public IEnumerable<string> Mnemonics { get; init; }
public ICollection<string> Mnemonics { get; init; }

/// <summary>
/// Size of the GAP for depth
Expand Down
2 changes: 1 addition & 1 deletion Src/WitsmlExplorer.Api/Jobs/BatchModifyWellJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace WitsmlExplorer.Api.Jobs
{
public record BatchModifyWellJob : Job
{
public IEnumerable<Well> Wells { get; init; }
public ICollection<Well> Wells { get; init; }

public override string Description()
{
Expand Down
4 changes: 2 additions & 2 deletions Src/WitsmlExplorer.Api/Jobs/DeleteCurveValuesJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ namespace WitsmlExplorer.Api.Jobs
public record DeleteCurveValuesJob : Job
{
public ObjectReference LogReference { get; init; }
public IEnumerable<string> Mnemonics { get; init; }
public IEnumerable<IndexRange> IndexRanges { get; init; }
public ICollection<string> Mnemonics { get; init; }
public ICollection<IndexRange> IndexRanges { get; init; }

public override string Description()
{
Expand Down
4 changes: 2 additions & 2 deletions Src/WitsmlExplorer.Api/Jobs/DeleteEmptyMnemonicsJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ namespace WitsmlExplorer.Api.Jobs
public record DeleteEmptyMnemonicsJob : Job
{

public IEnumerable<WellReference> Wells { get; init; }
public IEnumerable<WellboreReference> Wellbores { get; init; }
public ICollection<WellReference> Wells { get; init; }
public ICollection<WellboreReference> Wellbores { get; init; }
public double NullDepthValue { get; init; }
public DateTime NullTimeValue { get; init; }

Expand Down
6 changes: 3 additions & 3 deletions Src/WitsmlExplorer.Api/Jobs/ImportLogDataJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ namespace WitsmlExplorer.Api.Jobs
public record ImportLogDataJob : Job
{
public ObjectReference TargetLog { get; init; }
public IEnumerable<string> Mnemonics { get; init; }
public IEnumerable<string> Units { get; init; }
public IEnumerable<IEnumerable<string>> DataRows { get; init; }
public ICollection<string> Mnemonics { get; init; }
public ICollection<string> Units { get; init; }
public ICollection<ICollection<string>> DataRows { get; init; }

public override string Description()
{
Expand Down
6 changes: 3 additions & 3 deletions Src/WitsmlExplorer.Api/Jobs/MissingDataJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ namespace WitsmlExplorer.Api.Jobs
{
public record MissingDataJob : Job
{
public IEnumerable<WellReference> WellReferences { get; init; }
public IEnumerable<WellboreReference> WellboreReferences { get; init; }
public IEnumerable<MissingDataCheck> MissingDataChecks { get; init; }
public ICollection<WellReference> WellReferences { get; init; }
public ICollection<WellboreReference> WellboreReferences { get; init; }
public ICollection<MissingDataCheck> MissingDataChecks { get; init; }

public override string Description()
{
Expand Down
4 changes: 2 additions & 2 deletions Src/WitsmlExplorer.Api/Models/LogData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ public class LogData
public string StartIndex { get; init; }
public string EndIndex { get; init; }
public string Direction { get; set; }
public IEnumerable<CurveSpecification> CurveSpecifications { get; init; }
public IEnumerable<Dictionary<string, LogDataValue>> Data { get; init; }
public ICollection<CurveSpecification> CurveSpecifications { get; init; }
public ICollection<Dictionary<string, LogDataValue>> Data { get; init; }
}

public class CurveSpecification
Expand Down
2 changes: 1 addition & 1 deletion Src/WitsmlExplorer.Api/Models/MissingDataCheck.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ namespace WitsmlExplorer.Api.Models
public class MissingDataCheck
{
public EntityType ObjectType { get; set; }
public IEnumerable<string> Properties { get; set; }
public ICollection<string> Properties { get; set; }
}
}
28 changes: 15 additions & 13 deletions Src/WitsmlExplorer.Api/Query/ObjectQueries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ namespace WitsmlExplorer.Api.Query
{
public static class ObjectQueries
{
public static IEnumerable<WitsmlObjectOnWellbore> DeleteObjectsQuery(ObjectReferences toDelete)
public static IList<WitsmlObjectOnWellbore> DeleteObjectsQuery(ObjectReferences toDelete)
{
return IdsToObjects(toDelete.WellUid, toDelete.WellboreUid, toDelete.ObjectUids, toDelete.ObjectType);
}

public static IEnumerable<WitsmlObjectOnWellbore> IdsToObjects(string wellUid, string wellboreUid, string[] objectUids, EntityType type)
public static IList<WitsmlObjectOnWellbore> IdsToObjects(string wellUid, string wellboreUid, string[] objectUids, EntityType type)
{
return objectUids.Select((uid) =>
{
Expand All @@ -29,20 +29,19 @@ public static IEnumerable<WitsmlObjectOnWellbore> IdsToObjects(string wellUid, s
o.UidWellbore = wellboreUid;
o.UidWell = wellUid;
return o;
}
);
}).ToList();
}

public static IEnumerable<T> CopyObjectsQuery<T>(IEnumerable<T> objects, WitsmlWellbore targetWellbore) where T : WitsmlObjectOnWellbore
public static IList<T> CopyObjectsQuery<T>(IEnumerable<T> objects, WitsmlWellbore targetWellbore) where T : WitsmlObjectOnWellbore
{
return objects.Select((o) =>
return objects.Select(o =>
{
o.UidWell = targetWellbore.UidWell;
o.NameWell = targetWellbore.NameWell;
o.UidWellbore = targetWellbore.Uid;
o.NameWellbore = targetWellbore.Name;
return o;
});
}).ToList();
}

public static IWitsmlObjectList GetWitsmlObjectsByType(EntityType type)
Expand All @@ -53,16 +52,18 @@ public static IWitsmlObjectList GetWitsmlObjectsByType(EntityType type)
public static IWitsmlObjectList GetWitsmlObjectsWithParamByType(EntityType type, string objectProperty, string objectPropertyValue)
{
WitsmlObjectOnWellbore o = EntityTypeHelper.ToObjectOnWellbore(type);
o.UidWell = "";
o.UidWellbore = "";
o.Uid = "";
o.NameWell = "";
o.NameWellbore = "";
o.Name = "";
o.UidWell = string.Empty;
o.UidWellbore = string.Empty;
o.Uid = string.Empty;
o.NameWell = string.Empty;
o.NameWellbore = string.Empty;
o.Name = string.Empty;
if (objectProperty != null)
{
o = QueryHelper.AddPropertyToObject(o, objectProperty, objectPropertyValue);
};

// TODO: REMOVE CASTING!
return (IWitsmlObjectList)o.AsSingletonWitsmlList();
}

Expand Down Expand Up @@ -144,6 +145,7 @@ public static IEnumerable<string> GetComponentUids(WitsmlObjectOnWellbore object
/// <param name="componentType">The type of components copy.</param>
/// <param name="reference">Reference of the target object that will be used to initialize the query.</param>
/// <param name="uidsToCopy">A list of uids that will be used to filter the <paramref name="source"/> components.</param>
/// </summary>
public static WitsmlObjectOnWellbore CopyComponents(WitsmlObjectOnWellbore source, ComponentType componentType, ObjectReference reference, string[] uidsToCopy)
{
WitsmlObjectOnWellbore target = EntityTypeHelper.FromObjectReference(componentType.ToParentType(), reference);
Expand Down
2 changes: 1 addition & 1 deletion Src/WitsmlExplorer.Api/Repositories/CosmosRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public async Task<TDocument> GetDocumentAsync(TDocumentId id)
return await container.ReadItemAsync<TDocument>(id.ToString(), new PartitionKey(id.ToString()));
}

public async Task<IEnumerable<TDocument>> GetDocumentsAsync()
public async Task<ICollection<TDocument>> GetDocumentsAsync()
{
var container = _cosmosClient.GetContainer(_dbName, _containerId);
var queryDefinition = new QueryDefinition("select * from T");
Expand Down
2 changes: 1 addition & 1 deletion Src/WitsmlExplorer.Api/Repositories/IDocumentRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace WitsmlExplorer.Api.Repositories
public interface IDocumentRepository<TDocument, in TDocumentId>
{
Task<TDocument> GetDocumentAsync(TDocumentId id);
Task<IEnumerable<TDocument>> GetDocumentsAsync();
Task<ICollection<TDocument>> GetDocumentsAsync();
Task<TDocument> CreateDocumentAsync(TDocument document);
Task<TDocument> UpdateDocumentAsync(TDocumentId id, TDocument document);
Task DeleteDocumentAsync(TDocumentId id);
Expand Down
2 changes: 1 addition & 1 deletion Src/WitsmlExplorer.Api/Repositories/MongoRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public async Task<TDocument> GetDocumentAsync(TDocumentId id)
return documents.First();
}

public async Task<IEnumerable<TDocument>> GetDocumentsAsync()
public async Task<ICollection<TDocument>> GetDocumentsAsync()
{
var documents = await _collection.FindAsync(new BsonDocument());
return documents.ToList<TDocument>();
Expand Down
6 changes: 3 additions & 3 deletions Src/WitsmlExplorer.Api/Services/BhaRunService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace WitsmlExplorer.Api.Services
public interface IBhaRunService
{
Task<BhaRun> GetBhaRun(string wellUid, string wellboreUid, string bhaRunUid);
Task<IEnumerable<BhaRun>> GetBhaRuns(string wellUid, string wellboreUid);
Task<ICollection<BhaRun>> GetBhaRuns(string wellUid, string wellboreUid);
}

public class BhaRunService : WitsmlService, IBhaRunService
Expand All @@ -27,12 +27,12 @@ public async Task<BhaRun> GetBhaRun(string wellUid, string wellboreUid, string b
WitsmlBhaRuns result = await _witsmlClient.GetFromStoreAsync(query, new OptionsIn(ReturnElements.All));
return result.BhaRuns.Any() ? WitsmlToBhaRun(result.BhaRuns.First()) : null;
}
public async Task<IEnumerable<BhaRun>> GetBhaRuns(string wellUid, string wellboreUid)
public async Task<ICollection<BhaRun>> GetBhaRuns(string wellUid, string wellboreUid)
{
WitsmlBhaRuns witsmlBhaRun = BhaRunQueries.GetWitsmlBhaRun(wellUid, wellboreUid);
WitsmlBhaRuns result = await _witsmlClient.GetFromStoreAsync(witsmlBhaRun, new OptionsIn(ReturnElements.Requested));
return result.BhaRuns.Select(WitsmlToBhaRun
).OrderBy(bhaRun => bhaRun.Name);
).OrderBy(bhaRun => bhaRun.Name).ToList();
}

private static BhaRun WitsmlToBhaRun(WitsmlBhaRun bhaRun)
Expand Down
6 changes: 3 additions & 3 deletions Src/WitsmlExplorer.Api/Services/ChangeLogService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ namespace WitsmlExplorer.Api.Services
{
public interface IChangeLogService
{
Task<IEnumerable<ChangeLog>> GetChangeLogs(string wellUid, string wellboreUid);
Task<ICollection<ChangeLog>> GetChangeLogs(string wellUid, string wellboreUid);
}

public class ChangeLogService : WitsmlService, IChangeLogService
{
public ChangeLogService(IWitsmlClientProvider witsmlClientProvider) : base(witsmlClientProvider) { }

public async Task<IEnumerable<ChangeLog>> GetChangeLogs(string wellUid, string wellboreUid)
public async Task<ICollection<ChangeLog>> GetChangeLogs(string wellUid, string wellboreUid)
{
WitsmlChangeLogs witsmlChangeLog = new WitsmlChangeLog()
{
Expand All @@ -35,7 +35,7 @@ public async Task<IEnumerable<ChangeLog>> GetChangeLogs(string wellUid, string w
}
}.AsSingletonWitsmlList();
WitsmlChangeLogs result = await _witsmlClient.GetFromStoreAsync(witsmlChangeLog, new OptionsIn(ReturnElements.Requested));
return result.ChangeLogs.Select(WitsmlToChangeLog);
return result.ChangeLogs.Select(WitsmlToChangeLog).ToList();
}

private static ChangeLog WitsmlToChangeLog(WitsmlChangeLog changeLog)
Expand Down
Loading

0 comments on commit 0f5e1a7

Please sign in to comment.