diff --git a/.gitignore b/.gitignore index 8f5b35e..688fe21 100644 --- a/.gitignore +++ b/.gitignore @@ -15,6 +15,7 @@ build/ bld/ [Bb]in/ [Oo]bj/ +nuget/tools/ # MSTest test Results [Tt]est[Rr]esult*/ diff --git a/Cvent.JsonSchema2Poco.Client/CommandLineSettings.cs b/Cvent.JsonSchema2Poco.Client/CommandLineSettings.cs index fd9c2db..55385cd 100644 --- a/Cvent.JsonSchema2Poco.Client/CommandLineSettings.cs +++ b/Cvent.JsonSchema2Poco.Client/CommandLineSettings.cs @@ -16,6 +16,9 @@ internal class CommandLineSettings : IGenerationConfig public HashSet 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() @@ -36,6 +39,18 @@ public static CommandLineSettings ParseCommandLineParameters(IEnumerable {"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 diff --git a/Cvent.JsonSchema2Poco.Core/Class/JsonSchemaClass.cs b/Cvent.JsonSchema2Poco.Core/Class/JsonSchemaClass.cs index 5946135..bd1a022 100644 --- a/Cvent.JsonSchema2Poco.Core/Class/JsonSchemaClass.cs +++ b/Cvent.JsonSchema2Poco.Core/Class/JsonSchemaClass.cs @@ -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(); @@ -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)); diff --git a/Cvent.JsonSchema2Poco.Core/Class/Property/Types/IntegerPropertyType.cs b/Cvent.JsonSchema2Poco.Core/Class/Property/Types/IntegerPropertyType.cs index 9326227..c06a184 100644 --- a/Cvent.JsonSchema2Poco.Core/Class/Property/Types/IntegerPropertyType.cs +++ b/Cvent.JsonSchema2Poco.Core/Class/Property/Types/IntegerPropertyType.cs @@ -8,14 +8,23 @@ namespace Cvent.JsonSchema2Poco.Class.Property.Types /// class IntegerPropertyType : JsonSchemaPropertyType { + public static bool UseLongs { get; set; } + /// 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?)); } /// @@ -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()); + return UseLongs + ? CreatePrimitiveDefaultAssignment(info.FieldName, info.Definition.Default.Value()) + : CreatePrimitiveDefaultAssignment(info.FieldName, info.Definition.Default.Value()); } } } diff --git a/Cvent.JsonSchema2Poco.Core/Class/Property/Types/NumberPropertyType.cs b/Cvent.JsonSchema2Poco.Core/Class/Property/Types/NumberPropertyType.cs index 90dbcfb..6c88555 100644 --- a/Cvent.JsonSchema2Poco.Core/Class/Property/Types/NumberPropertyType.cs +++ b/Cvent.JsonSchema2Poco.Core/Class/Property/Types/NumberPropertyType.cs @@ -8,14 +8,22 @@ namespace Cvent.JsonSchema2Poco.Class.Property.Types /// class NumberPropertyType : JsonSchemaPropertyType { + public static bool UseDoubles { get; set; } + /// 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?)); } /// @@ -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()); + return UseDoubles + ? CreatePrimitiveDefaultAssignment(info.FieldName, info.Definition.Default.Value()) + : CreatePrimitiveDefaultAssignment(info.FieldName, info.Definition.Default.Value()); } } } diff --git a/Cvent.JsonSchema2Poco.Core/IGenerationConfig.cs b/Cvent.JsonSchema2Poco.Core/IGenerationConfig.cs index 7108e59..8c1907a 100644 --- a/Cvent.JsonSchema2Poco.Core/IGenerationConfig.cs +++ b/Cvent.JsonSchema2Poco.Core/IGenerationConfig.cs @@ -8,6 +8,9 @@ public interface IGenerationConfig HashSet Sources { get; } string TargetDirectory { get; } string Namespace { get; } + bool UseLongsForInts { get; } + bool UseDoublesForFloats { get; } + bool UsePartialClasses { get; } bool RemoveOldOutput { get; } } } diff --git a/Cvent.JsonSchema2Poco.Core/JsonSchema2Poco.cs b/Cvent.JsonSchema2Poco.Core/JsonSchema2Poco.cs index 0dac7c9..158a0af 100644 --- a/Cvent.JsonSchema2Poco.Core/JsonSchema2Poco.cs +++ b/Cvent.JsonSchema2Poco.Core/JsonSchema2Poco.cs @@ -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; @@ -31,6 +32,9 @@ public static void Generate(IGenerationConfig configuration) var schemas = new List(); 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(); schemas.ForEach(schema => schemaClassRepresentations.Add(JsonSchemaClass.CreateFromSchema(schema, null, configuration.Namespace))); @@ -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(); diff --git a/Cvent.JsonSchema2Poco.Core/Schema/JsonSchemaParser.cs b/Cvent.JsonSchema2Poco.Core/Schema/JsonSchemaParser.cs index 53731a2..f50b643 100644 --- a/Cvent.JsonSchema2Poco.Core/Schema/JsonSchemaParser.cs +++ b/Cvent.JsonSchema2Poco.Core/Schema/JsonSchemaParser.cs @@ -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(), 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); }