private static string Render(ITemplate template, DataConversionParameters parameters) { var defaultValueGenerator = new DefaultValueGenerator(parameters.Resolver); template.Initialize(new { Variable = parameters.Variable, Value = parameters.Value, HasDefaultValue = defaultValueGenerator.GetDefaultValue(parameters.Schema, parameters.TypeNameHint) != null, DefaultValue = defaultValueGenerator.GetDefaultValue(parameters.Schema, parameters.TypeNameHint), Type = parameters.Resolver.Resolve(parameters.Schema, parameters.IsPropertyNullable, parameters.TypeNameHint), IsNewableObject = IsNewableObject(parameters.Schema), IsDate = parameters.Settings.DateTimeType != TypeScriptDateTimeType.String && parameters.Schema.Format == JsonFormatStrings.DateTime, IsDictionary = parameters.Schema.IsDictionary, DictionaryValueType = parameters.Resolver.TryResolve(parameters.Schema.AdditionalPropertiesSchema, parameters.TypeNameHint) ?? "any", IsDictionaryValueNewableObject = parameters.Schema.AdditionalPropertiesSchema != null && IsNewableObject(parameters.Schema.AdditionalPropertiesSchema), IsDictionaryValueDate = parameters.Settings.DateTimeType != TypeScriptDateTimeType.String && parameters.Schema.AdditionalPropertiesSchema?.Format == JsonFormatStrings.DateTime, IsArray = parameters.Schema.Type.HasFlag(JsonObjectType.Array), ArrayItemType = parameters.Resolver.TryResolve(parameters.Schema.Item, parameters.TypeNameHint) ?? "any", IsArrayItemNewableObject = parameters.Schema.Item != null && IsNewableObject(parameters.Schema.Item), IsArrayItemDate = parameters.Settings.DateTimeType != TypeScriptDateTimeType.String && parameters.Schema.Item?.Format == JsonFormatStrings.DateTime, StringToDateCode = parameters.Settings.DateTimeType == TypeScriptDateTimeType.Date ? "new Date" : "moment", DateToStringCode = "toISOString()" }); return(template.Render()); }
/// <summary>Generates the code to convert a data object to the target class instances.</summary> /// <returns>The generated code.</returns> public static string RenderConvertToClassCode(DataConversionParameters parameters) { var model = CreateModel(parameters); var template = parameters.Settings.TemplateFactory.CreateTemplate("TypeScript", "ConvertToClass", model); return(template.Render()); }
private static object CreateModel(DataConversionParameters parameters) { var type = parameters.Resolver.Resolve(parameters.Schema, parameters.IsPropertyNullable, parameters.TypeNameHint); var defaultValueGenerator = new TypeScriptDefaultValueGenerator(parameters.Resolver); return(new { Variable = parameters.Variable, Value = parameters.Value, HasDefaultValue = defaultValueGenerator.GetDefaultValue(parameters.Schema, parameters.IsPropertyNullable, type, parameters.TypeNameHint) != null, DefaultValue = defaultValueGenerator.GetDefaultValue(parameters.Schema, parameters.IsPropertyNullable, type, parameters.TypeNameHint), Type = type, IsNewableObject = IsNewableObject(parameters.Schema), IsDate = IsDate(parameters.Schema.Format, parameters.Settings.DateTimeType), IsDictionary = parameters.Schema.IsDictionary, DictionaryValueType = parameters.Resolver.TryResolve(parameters.Schema.AdditionalPropertiesSchema, parameters.TypeNameHint) ?? "any", IsDictionaryValueNewableObject = parameters.Schema.AdditionalPropertiesSchema != null && IsNewableObject(parameters.Schema.AdditionalPropertiesSchema), IsDictionaryValueDate = IsDate(parameters.Schema.AdditionalPropertiesSchema?.ActualSchema?.Format, parameters.Settings.DateTimeType), IsArray = parameters.Schema.Type.HasFlag(JsonObjectType.Array), ArrayItemType = parameters.Resolver.TryResolve(parameters.Schema.Item, parameters.TypeNameHint) ?? "any", IsArrayItemNewableObject = parameters.Schema.Item != null && IsNewableObject(parameters.Schema.Item), IsArrayItemDate = IsDate(parameters.Schema.Item?.Format, parameters.Settings.DateTimeType), StringToDateCode = parameters.Settings.DateTimeType == TypeScriptDateTimeType.Date ? "new Date" : "moment", DateToStringCode = "toISOString()" }); }
private static string Render(ITemplate template, DataConversionParameters parameters) { template.Initialize(new { Variable = parameters.Variable, Value = parameters.Value, HasDefaultValue = PropertyModelBase.GetDefaultValue(parameters.Schema) != null, DefaultValue = PropertyModelBase.GetDefaultValue(parameters.Schema), Type = parameters.Resolver.Resolve(parameters.Schema, parameters.IsPropertyNullable, parameters.TypeNameHint), IsNewableObject = IsNewableObject(parameters.Schema), IsDate = parameters.Schema.Format == JsonFormatStrings.DateTime, IsDictionary = parameters.Schema.IsDictionary, DictionaryValueType = parameters.Resolver.TryResolve(parameters.Schema.AdditionalPropertiesSchema, parameters.TypeNameHint) ?? "any", IsDictionaryValueNewableObject = parameters.Schema.AdditionalPropertiesSchema != null && IsNewableObject(parameters.Schema.AdditionalPropertiesSchema), IsDictionaryValueDate = parameters.Schema.AdditionalPropertiesSchema?.Format == JsonFormatStrings.DateTime, IsArray = parameters.Schema.Type.HasFlag(JsonObjectType.Array), ArrayItemType = parameters.Resolver.TryResolve(parameters.Schema.Item, parameters.TypeNameHint) ?? "any", IsArrayItemNewableObject = parameters.Schema.Item != null && IsNewableObject(parameters.Schema.Item), IsArrayItemDate = parameters.Schema.Item?.Format == JsonFormatStrings.DateTime }); return(template.Render()); }
private static string GetStringToDateTime(DataConversionParameters parameters, JsonSchema typeSchema) { switch (parameters.Settings.DateTimeType) { case TypeScriptDateTimeType.Date: return("new Date"); case TypeScriptDateTimeType.MomentJS: case TypeScriptDateTimeType.OffsetMomentJS: if (typeSchema.Format == JsonFormatStrings.TimeSpan) { return("moment.duration"); } if (parameters.Settings.DateTimeType == TypeScriptDateTimeType.OffsetMomentJS) { return("moment.parseZone"); } return("moment"); case TypeScriptDateTimeType.String: return(""); case TypeScriptDateTimeType.Luxon: return("DateTime.fromISO"); case TypeScriptDateTimeType.DayJS: return("dayjs"); default: throw new ArgumentOutOfRangeException(); } }
private static bool IsNewableObject(JsonSchema schema, DataConversionParameters parameters) { if (schema.ActualTypeSchema.IsEnumeration) { return(false); } return(parameters.Resolver.GeneratesType(schema)); }
private static object CreateModel(DataConversionParameters parameters) { var type = parameters.Resolver.Resolve(parameters.Schema, parameters.IsPropertyNullable, parameters.TypeNameHint); var defaultValueGenerator = new TypeScriptDefaultValueGenerator(parameters.Resolver); var dictionaryValueType = parameters.Resolver.TryResolve(parameters.Schema.AdditionalPropertiesSchema, parameters.TypeNameHint) ?? "any"; var dictionaryValueDefaultValue = parameters.Schema.AdditionalPropertiesSchema != null ? defaultValueGenerator.GetDefaultValue(parameters.Schema.AdditionalPropertiesSchema, parameters.Schema.AdditionalPropertiesSchema.IsNullable(parameters.Settings.NullHandling), dictionaryValueType, parameters.TypeNameHint, parameters.Settings.GenerateDefaultValues) : null; return(new { NullValue = parameters.NullValue.ToString().ToLowerInvariant(), Variable = parameters.Variable, Value = parameters.Value, HasDefaultValue = defaultValueGenerator.GetDefaultValue(parameters.Schema, parameters.IsPropertyNullable, type, parameters.TypeNameHint, parameters.Settings.GenerateDefaultValues) != null, DefaultValue = defaultValueGenerator.GetDefaultValue(parameters.Schema, parameters.IsPropertyNullable, type, parameters.TypeNameHint, parameters.Settings.GenerateDefaultValues), Type = type, IsNewableObject = IsNewableObject(parameters.Schema), IsDate = IsDate(parameters.Schema.Format, parameters.Settings.DateTimeType), IsDictionary = parameters.Schema.IsDictionary, DictionaryValueType = dictionaryValueType, DictionaryValueDefaultValue = dictionaryValueDefaultValue, HasDictionaryValueDefaultValue = dictionaryValueDefaultValue != null, IsDictionaryValueNewableObject = parameters.Schema.AdditionalPropertiesSchema != null && IsNewableObject(parameters.Schema.AdditionalPropertiesSchema), IsDictionaryValueDate = IsDate(parameters.Schema.AdditionalPropertiesSchema?.ActualSchema?.Format, parameters.Settings.DateTimeType), IsDictionaryValueNewableArray = parameters.Schema.AdditionalPropertiesSchema?.ActualSchema?.Type.HasFlag(JsonObjectType.Array) == true && IsNewableObject(parameters.Schema.AdditionalPropertiesSchema.Item), DictionaryValueArrayItemType = parameters.Schema.AdditionalPropertiesSchema?.ActualSchema?.Type.HasFlag(JsonObjectType.Array) == true? parameters.Resolver.TryResolve(parameters.Schema.AdditionalPropertiesSchema.Item, "Anonymous") ?? "any" : "any", IsArray = parameters.Schema.Type.HasFlag(JsonObjectType.Array), ArrayItemType = parameters.Resolver.TryResolve(parameters.Schema.Item, parameters.TypeNameHint) ?? "any", IsArrayItemNewableObject = parameters.Schema.Item != null && IsNewableObject(parameters.Schema.Item), IsArrayItemDate = IsDate(parameters.Schema.Item?.Format, parameters.Settings.DateTimeType), StringToDateCode = parameters.Settings.DateTimeType == TypeScriptDateTimeType.Date ? "new Date" : "moment", DateToStringCode = "toISOString()", HandleReferences = parameters.Settings.HandleReferences }); }
private static string GetDateToString(DataConversionParameters parameters, JsonSchema typeSchema) { switch (parameters.Settings.DateTimeType) { case TypeScriptDateTimeType.Date: case TypeScriptDateTimeType.String: return(""); case TypeScriptDateTimeType.MomentJS: case TypeScriptDateTimeType.OffsetMomentJS: case TypeScriptDateTimeType.DayJS: return("format('YYYY-MM-DD')"); case TypeScriptDateTimeType.Luxon: return("toFormat('yyyy-MM-dd')"); default: throw new ArgumentOutOfRangeException(); } }
private static string GetDateTimeToString(DataConversionParameters parameters, JsonSchema typeSchema) { switch (parameters.Settings.DateTimeType) { case TypeScriptDateTimeType.Date: return("toISOString()"); case TypeScriptDateTimeType.MomentJS: case TypeScriptDateTimeType.OffsetMomentJS: if (typeSchema.Format == JsonFormatStrings.TimeSpan) { return("format('d.hh:mm:ss.SS', { trim: false })"); } if (parameters.Settings.DateTimeType == TypeScriptDateTimeType.OffsetMomentJS) { return("toISOString(true)"); } return("toISOString()"); case TypeScriptDateTimeType.String: return(""); case TypeScriptDateTimeType.Luxon: return("toString()"); case TypeScriptDateTimeType.DayJS: if (typeSchema.Format == JsonFormatStrings.TimeSpan) { return("format('d.hh:mm:ss.SSS')"); } return("toISOString()"); default: throw new ArgumentOutOfRangeException(); } }
private static object CreateModel(DataConversionParameters parameters) { var type = parameters.Resolver.Resolve(parameters.Schema, parameters.IsPropertyNullable, parameters.TypeNameHint); var valueGenerator = parameters.Settings.ValueGenerator; var typeSchema = parameters.Schema.ActualTypeSchema; var dictionaryValueType = parameters.Resolver.TryResolve(typeSchema.AdditionalPropertiesSchema, parameters.TypeNameHint) ?? "any"; var dictionaryValueDefaultValue = typeSchema.AdditionalPropertiesSchema != null ? valueGenerator.GetDefaultValue(typeSchema.AdditionalPropertiesSchema, typeSchema.AdditionalPropertiesSchema.IsNullable(parameters.Settings.SchemaType), dictionaryValueType, parameters.TypeNameHint, parameters.Settings.GenerateDefaultValues, parameters.Resolver) : null; return(new { NullValue = parameters.NullValue.ToString().ToLowerInvariant(), Variable = parameters.Variable, Value = parameters.Value, HasDefaultValue = valueGenerator.GetDefaultValue(parameters.Schema, parameters.IsPropertyNullable, type, parameters.TypeNameHint, parameters.Settings.GenerateDefaultValues, parameters.Resolver) != null, DefaultValue = valueGenerator.GetDefaultValue(parameters.Schema, parameters.IsPropertyNullable, type, parameters.TypeNameHint, parameters.Settings.GenerateDefaultValues, parameters.Resolver), Type = type, CheckNewableObject = parameters.CheckNewableObject, IsNewableObject = IsNewableObject(parameters.Schema, parameters), IsDate = IsDate(typeSchema.Format, parameters.Settings.DateTimeType), IsDateTime = IsDateTime(typeSchema.Format, parameters.Settings.DateTimeType), IsDictionary = typeSchema.IsDictionary, DictionaryValueType = dictionaryValueType, DictionaryValueDefaultValue = dictionaryValueDefaultValue, HasDictionaryValueDefaultValue = dictionaryValueDefaultValue != null, IsDictionaryValueNewableObject = typeSchema.AdditionalPropertiesSchema != null && IsNewableObject(typeSchema.AdditionalPropertiesSchema, parameters), IsDictionaryValueDate = IsDate(typeSchema.AdditionalPropertiesSchema?.ActualSchema?.Format, parameters.Settings.DateTimeType), IsDictionaryValueDateTime = IsDateTime(typeSchema.AdditionalPropertiesSchema?.ActualSchema?.Format, parameters.Settings.DateTimeType), IsDictionaryValueNewableArray = typeSchema.AdditionalPropertiesSchema?.ActualSchema?.IsArray == true && IsNewableObject(typeSchema.AdditionalPropertiesSchema.Item, parameters), DictionaryValueArrayItemType = typeSchema.AdditionalPropertiesSchema?.ActualSchema?.IsArray == true? parameters.Resolver.TryResolve(typeSchema.AdditionalPropertiesSchema.Item, "Anonymous") ?? "any" : "any", IsArray = typeSchema.IsArray, ArrayItemType = parameters.Resolver.TryResolve(typeSchema.Item, parameters.TypeNameHint) ?? "any", IsArrayItemNewableObject = typeSchema.Item != null && IsNewableObject(typeSchema.Item, parameters), IsArrayItemDate = IsDate(typeSchema.Item?.Format, parameters.Settings.DateTimeType), IsArrayItemDateTime = IsDateTime(typeSchema.Item?.Format, parameters.Settings.DateTimeType), RequiresStrictPropertyInitialization = parameters.Settings.TypeScriptVersion >= 2.7m, //StringToDateCode is used for date and date-time formats UseJsDate = parameters.Settings.DateTimeType == TypeScriptDateTimeType.Date, StringToDateCode = GetStringToDateTime(parameters, typeSchema), DateToStringCode = GetDateToString(parameters, typeSchema), DateTimeToStringCode = GetDateTimeToString(parameters, typeSchema), HandleReferences = parameters.Settings.HandleReferences }); }
/// <summary>Generates the code to convert a data object to the target class instances.</summary> /// <returns>The generated code.</returns> public static string RenderConvertToClassCode(DataConversionParameters parameters) { return(new ConvertToClassTemplate(CreateModel(parameters)).Render()); }
/// <summary>Generates the code to convert a data object to the target class instances.</summary> /// <returns>The generated code.</returns> public static string RenderConvertToClassCode(DataConversionParameters parameters) { return(Render(new ConvertToClassTemplate(), parameters)); }
/// <summary>Generates the code to convert a data object to the target class instances.</summary> /// <returns>The generated code.</returns> public static string RenderConvertToJavaScriptCode(DataConversionParameters parameters) { return(Render(new ConvertToJavaScriptTemplate(), parameters)); }