Skip to content

Commit

Permalink
Convert ProjectStorageType from an enum to a string for extensibility
Browse files Browse the repository at this point in the history
  • Loading branch information
lyonsil committed Sep 21, 2023
1 parent 8aa45ab commit d1816ad
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 60 deletions.
2 changes: 1 addition & 1 deletion c-sharp/Projects/ParatextProjectDataProviderFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ protected override ResponseToRequest GetProjectDataProviderID(
string projectStorageInterpreterId
)
{
if (_paratextPsi.StorageType.ToSerializedString() != projectStorageInterpreterId)
if (_paratextPsi.StorageType != projectStorageInterpreterId)
return ResponseToRequest.Failed(
$"Unexpected project storage interpreter requested: {projectStorageInterpreterId}"
);
Expand Down
13 changes: 4 additions & 9 deletions c-sharp/Projects/ProjectMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,7 @@ namespace Paranext.DataProvider.Projects;
/// </summary>
public class ProjectMetadata
{
public ProjectMetadata(
Guid id,
string name,
ProjectStorageType projectStorageType,
string projectType
)
public ProjectMetadata(Guid id, string name, string projectStorageType, string projectType)
{
ID = id;
Name = name;
Expand All @@ -31,7 +26,7 @@ string projectType
/// <summary>
/// Indicates how the project is persisted to storage
/// </summary>
public ProjectStorageType ProjectStorageType { get; }
public string ProjectStorageType { get; }

/// <summary>
/// Indicates what sort of project this is which implies its data shape (e.g., what data streams should be available)
Expand All @@ -54,11 +49,11 @@ protected bool Equals(ProjectMetadata other)

public override int GetHashCode()
{
return HashCode.Combine(ID, Name, (int)ProjectStorageType, ProjectType);
return HashCode.Combine(ID, Name, ProjectStorageType, ProjectType);
}

public override string ToString()
{
return $"[{Name} ({ID}): {ProjectStorageType.ToSerializedString()}, {ProjectType}]";
return $"[{Name} ({ID}): {ProjectStorageType}, {ProjectType}]";
}
}
8 changes: 4 additions & 4 deletions c-sharp/Projects/ProjectStorageInterpreter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ namespace Paranext.DataProvider.Projects;
internal abstract class ProjectStorageInterpreter : NetworkObjects.DataProvider
{
protected ProjectStorageInterpreter(
ProjectStorageType storageType,
string storageType,
IReadOnlyList<string> projectTypes,
PapiClient papiClient
)
// TODO: Register one network object per (storage type, project type) pair or come up with a better approach
: base($"platform.{storageType.ToSerializedString()}-{projectTypes[0]}-psi", papiClient)
: base($"platform.{storageType}-{projectTypes[0]}-psi", papiClient)
{
StorageType = storageType;
ProjectTypes = projectTypes;
Expand Down Expand Up @@ -64,13 +64,13 @@ private ResponseToRequest GetSupportedTypes()
return ResponseToRequest.Succeeded(
new JObject
{
["storageType"] = StorageType.ToSerializedString(),
["storageType"] = StorageType,
["projectTypes"] = JsonConvert.SerializeObject(ProjectTypes)
}.ToString()
);
}

public ProjectStorageType StorageType { get; }
public string StorageType { get; }
public IEnumerable<string> ProjectTypes { get; }

/// <summary>
Expand Down
49 changes: 4 additions & 45 deletions c-sharp/Projects/ProjectStorageType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,9 @@ namespace Paranext.DataProvider.Projects;
/// Structure of the data in persisted storage.
/// This is not related to the format of the data itself but the way it is stored
/// </summary>
public enum ProjectStorageType
public static class ProjectStorageType
{
Unknown,
ParatextFolders,
ParatextZipped
}

// Add an extension methods related to the enum
public static class ProjectStorageTypeExtensions
{
/// <summary>
/// Convert a ProjectStorageType into a value recognized by PAPI
/// </summary>
/// <param name="projectStorageType">Enum value</param>
/// <returns>String that represents the enum value that can be passed over the network</returns>
public static string ToSerializedString(this ProjectStorageType projectStorageType)
{
return projectStorageType switch
{
ProjectStorageType.Unknown => "unknown",
ProjectStorageType.ParatextFolders => "paratextFolders",
ProjectStorageType.ParatextZipped => "paratextZipped",
_ => throw new Exception("Unexpected project storage type: " + projectStorageType)
};
}

/// <summary>
/// Convert a string from PAPI into a ProjectStorageType enum value
/// </summary>
/// <param name="input">string representing a ProjectStorageType value</param>
/// <param name="output">ProjectStorageType enum value</param>
public static void FromSerializedString(this string input, out ProjectStorageType output)
{
switch (input)
{
case "paratextFolders":
output = ProjectStorageType.ParatextFolders;
return;
case "paratextZipped":
output = ProjectStorageType.ParatextZipped;
return;
default:
output = ProjectStorageType.Unknown;
return;
}
}
public const string Unknown = "unknown";
public const string ParatextFolders = "paratextFolders";
public const string ParatextZipped = "paratextZipped";
}
2 changes: 1 addition & 1 deletion c-sharp/Projects/ProjectType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace Paranext.DataProvider.Projects;
/// <summary>
/// Format of main (Project Data Provider, not extension) data in a project
/// </summary>
internal static class ProjectType
public static class ProjectType
{
public const string Paratext = "ParatextStandard";
}

0 comments on commit d1816ad

Please sign in to comment.