Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor additions #18

Merged
merged 3 commits into from
Oct 27, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ build/
bld/
[Bb]in/
[Oo]bj/
nuget/tools/

# MSTest test Results
[Tt]est[Rr]esult*/
Expand Down
15 changes: 15 additions & 0 deletions Cvent.JsonSchema2Poco.Client/CommandLineSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ internal class CommandLineSettings : IGenerationConfig
public HashSet<Uri> Sources { get; private set; }
public string TargetDirectory { get; private set; }
public string Namespace { get; private set; }
public bool UsePartialClasses { get; private set; }
public bool UseLongsForInts { get; private set; }
public bool UseDoublesForFloats { get; private set; }
public bool RemoveOldOutput { get; private set; }

public CommandLineSettings()
Expand All @@ -36,6 +39,18 @@ public static CommandLineSettings ParseCommandLineParameters(IEnumerable<string>
{"n=|namespace=", "Namespace containing all of the generated classes", ns => settings.Namespace = ns},
{"s=|schemas=", "File path to the schema file", s => sources = s},
{"o=|output=", "Directory to save files", fn => settings.TargetDirectory = fn},
{
"l|long", "Indicates if longs should be used instead of ints for integers.",
fn => settings.UseLongsForInts = fn != null
},
{
"f|floats", "Indicates if doubles should be used instead of floats for numbers.",
fn => settings.UseDoublesForFloats = fn != null
},
{
"p|partial", "Flag to set if the classes generated will be partial.",
fn => settings.UsePartialClasses = fn != null
},
{
"r|remove", "Flag to remove files in output directory before generation.",
fn => settings.RemoveOldOutput = fn != null
Expand Down
3 changes: 2 additions & 1 deletion Cvent.JsonSchema2Poco.Core/Class/JsonSchemaClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static JsonSchemaClass CreateFromSchema(JsonSchema schema, string schemaN
return classBuilder.Build(schema, schemaName, schemaNamespace);
}

public CodeCompileUnit GetClassRepresentation()
public CodeCompileUnit GetClassRepresentation(bool usePartialClasses)
{
// DEFINE THE CONSTRUCTOR.
var constructor = new CodeConstructor();
Expand All @@ -47,6 +47,7 @@ public CodeCompileUnit GetClassRepresentation()
// DEFINE THE IN MEMORY CLASS REPRESENTATION OF THE SCHEMA.
var jsonClassRepresentation = new CodeTypeDeclaration();
jsonClassRepresentation.IsClass = true;
jsonClassRepresentation.IsPartial = usePartialClasses;
jsonClassRepresentation.Name = Name;
jsonClassRepresentation.Comments.Add(new CodeCommentStatement(Comment));
Fields.ForEach(x => jsonClassRepresentation.Members.Add(x));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,23 @@ namespace Cvent.JsonSchema2Poco.Class.Property.Types
/// </summary>
class IntegerPropertyType : JsonSchemaPropertyType
{
public static bool UseLongs { get; set; }

/// <see cref="JsonSchemaPropertyType"/>
public override CodeTypeReference GetType(JsonPropertyInfo info)
{
var required = info.Definition.Required.HasValue && info.Definition.Required.Value;
var defaultPresent = GetDefaultAssignment(info) != null;
if (UseLongs)
{
return required || defaultPresent
? new CodeTypeReference(typeof (long))
: new CodeTypeReference(typeof (long?));
}

return required || defaultPresent
? new CodeTypeReference(typeof(long))
: new CodeTypeReference(typeof(long?));
? new CodeTypeReference(typeof(int))
: new CodeTypeReference(typeof(int?));
}

/// <see cref="JsonSchemaPropertyType"/>
Expand All @@ -24,7 +33,9 @@ public override CodeAssignStatement GetDefaultAssignment(JsonPropertyInfo info)
if (info.Definition.Default == null || info.Definition.Default.Type != JTokenType.Integer)
return null;

return CreatePrimitiveDefaultAssignment(info.FieldName, info.Definition.Default.Value<long>());
return UseLongs
? CreatePrimitiveDefaultAssignment(info.FieldName, info.Definition.Default.Value<long>())
: CreatePrimitiveDefaultAssignment(info.FieldName, info.Definition.Default.Value<int>());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,22 @@ namespace Cvent.JsonSchema2Poco.Class.Property.Types
/// </summary>
class NumberPropertyType : JsonSchemaPropertyType
{
public static bool UseDoubles { get; set; }

/// <see cref="JsonSchemaPropertyType"/>
public override CodeTypeReference GetType(JsonPropertyInfo info)
{
var required = info.Definition.Required.HasValue && info.Definition.Required.Value;
var defaultPresent = GetDefaultAssignment(info) != null;
if (UseDoubles)
{
return required || defaultPresent
? new CodeTypeReference(typeof (double))
: new CodeTypeReference(typeof (double?));
}
return required || defaultPresent
? new CodeTypeReference(typeof(double))
: new CodeTypeReference(typeof(double?));
? new CodeTypeReference(typeof(float))
: new CodeTypeReference(typeof(float?));
}

/// <see cref="JsonSchemaPropertyType"/>
Expand All @@ -24,7 +32,9 @@ public override CodeAssignStatement GetDefaultAssignment(JsonPropertyInfo info)
if (info.Definition.Default == null || info.Definition.Default.Type != JTokenType.Float)
return null;

return CreatePrimitiveDefaultAssignment(info.FieldName, info.Definition.Default.Value<double>());
return UseDoubles
? CreatePrimitiveDefaultAssignment(info.FieldName, info.Definition.Default.Value<double>())
: CreatePrimitiveDefaultAssignment(info.FieldName, info.Definition.Default.Value<float>());
}
}
}
3 changes: 3 additions & 0 deletions Cvent.JsonSchema2Poco.Core/IGenerationConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ public interface IGenerationConfig
HashSet<Uri> Sources { get; }
string TargetDirectory { get; }
string Namespace { get; }
bool UseLongsForInts { get; }
bool UseDoublesForFloats { get; }
bool UsePartialClasses { get; }
bool RemoveOldOutput { get; }
}
}
12 changes: 11 additions & 1 deletion Cvent.JsonSchema2Poco.Core/JsonSchema2Poco.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.IO;
using System.Linq;
using System.Text;
using Cvent.JsonSchema2Poco.Class.Property.Types;
using Cvent.JsonSchema2Poco.Schema;
using Cvent.JsonSchema2Poco.Class;
using Microsoft.CSharp;
Expand Down Expand Up @@ -31,6 +32,9 @@ public static void Generate(IGenerationConfig configuration)
var schemas = new List<JsonSchema>();
configuration.Sources.ToList().ForEach(schemaUri => schemas.Add(JsonSchemaParser.Parse(schemaUri)));

// SET PROPERTY CONFIGS.
SetPropertyConfigs(configuration);

// CREATE THE CLASS REPRESENTATION OF THE SCHEMAS.
var schemaClassRepresentations = new List<JsonSchemaClass>();
schemas.ForEach(schema => schemaClassRepresentations.Add(JsonSchemaClass.CreateFromSchema(schema, null, configuration.Namespace)));
Expand All @@ -39,10 +43,16 @@ public static void Generate(IGenerationConfig configuration)
// GENERATE THE CLASSES.
foreach (var schemaClass in uniqueSchemas)
{
File.WriteAllText(Path.Combine(configuration.TargetDirectory, schemaClass.Name + ".cs"), GetCSharpClassText(schemaClass.GetClassRepresentation()));
File.WriteAllText(Path.Combine(configuration.TargetDirectory, schemaClass.Name + ".cs"), GetCSharpClassText(schemaClass.GetClassRepresentation(configuration.UsePartialClasses)));
}
}

private static void SetPropertyConfigs(IGenerationConfig configuration)
{
IntegerPropertyType.UseLongs = configuration.UseLongsForInts;
NumberPropertyType.UseDoubles = configuration.UseDoublesForFloats;
}

private static string GetCSharpClassText(CodeCompileUnit classRepresentation)
{
var stringBuilder = new StringBuilder();
Expand Down
12 changes: 12 additions & 0 deletions Cvent.JsonSchema2Poco.Core/Schema/JsonSchemaParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ public static JsonSchema Parse(Uri schemaUri)
private static JsonSchema Parse(Uri schemaUri, JsonSchemaResolver referenceSchemaResolver)
{
var schemaContent = GetJsonFromResource(schemaUri);
// RESOLVE THE ID OF THE SCHEMA TO AN ABSOLUTE URI.
var id = GetProperty(schemaContent, "id", JTokenType.String);
if (id != null)
{
var resolvedUri = new Uri(schemaUri, new Uri(id.Value<string>(), UriKind.RelativeOrAbsolute));
schemaContent["id"].Replace(JToken.Parse(string.Format(@"""{0}""", resolvedUri)));
}
else
{
// The Id was not present. Simply resolve as the URI of the parent schema.
schemaContent.Add("id", JToken.Parse(string.Format(@"""{0}""", schemaUri)));
}
ResolveReferenceSchemas(schemaContent, schemaUri, referenceSchemaResolver);
return JsonSchema.Parse(schemaContent.ToString(), referenceSchemaResolver);
}
Expand Down