Skip to content

Commit

Permalink
re-organizing code
Browse files Browse the repository at this point in the history
  • Loading branch information
ignatandrei committed May 9, 2020
1 parent ace64cd commit 5c3d9ab
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 136 deletions.
4 changes: 2 additions & 2 deletions src/NetCore2Blockly/NetCore2Blockly/ActionInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ private BindingSourceDefinition ConvertFromBindingSource(BindingSource bs)

};
}
Dictionary<string, (TypeArgument type, BindingSourceDefinition bs)> GetParameters(ApiParameterDescription[] parameterDescriptions)
Dictionary<string, (TypeArgumentBase type, BindingSourceDefinition bs)> GetParameters(ApiParameterDescription[] parameterDescriptions)
{
var desc = new Dictionary<string, (TypeArgument type, BindingSourceDefinition bs)>();
var desc = new Dictionary<string, (TypeArgumentBase type, BindingSourceDefinition bs)>();

if (parameterDescriptions?.Length == 0)
return desc;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static class ActionListExtensions
/// </summary>
/// <param name="list">The list.</param>
/// <returns></returns>
public static TypeArgument[] GetAllTypesWithNullBlocklyType(this List<ActionInfo> list)
public static TypeArgumentBase[] GetAllTypesWithNullBlocklyType(this List<ActionInfo> list)
{
return list

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ private static void MapJS(IApplicationBuilder app, string url,Func<GenerateBlock
{
app.Map(url, app =>
{
var blocklyFilesHostedService = app.ApplicationServices
.GetService<GenerateBlocklyFilesHostedService>();
var blocklyFilesHostedService =
app.
ApplicationServices
.GetService<GenerateBlocklyFilesHostedService>();
app.Run(async context =>
{
await GetBlocklyFilesHostedServices(context, content(blocklyFilesHostedService));
Expand Down
4 changes: 2 additions & 2 deletions src/NetCore2Blockly/NetCore2Blockly/IActionInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public abstract class ActionInfo
/// </summary>
public ActionInfo()
{
Params = new Dictionary<string, (TypeArgument type, BindingSourceDefinition bs)>();
Params = new Dictionary<string, (TypeArgumentBase type, BindingSourceDefinition bs)>();
}
/// <summary>
/// Gets or sets the name of the action.
Expand Down Expand Up @@ -77,7 +77,7 @@ public int CustomGetHashCode()
/// <value>
/// The parameters.
/// </value>
public Dictionary<string, (TypeArgument type, BindingSourceDefinition bs)> Params { get; set; }
public Dictionary<string, (TypeArgumentBase type, BindingSourceDefinition bs)> Params { get; set; }

/// <summary>
/// Gets a value indicating whether this instance has parameters.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class BlocklyDefinitionGenerator
/// </summary>
/// <param name="type">The type.</param>
/// <returns></returns>
public string GenerateBlocklyDefinition(TypeArgument type)
public string GenerateBlocklyDefinition(TypeArgumentBase type)
{

if (type.ConvertibleToBlocklyType())
Expand All @@ -32,7 +32,7 @@ public string GenerateBlocklyDefinition(TypeArgument type)
/// </summary>
/// <param name="type">The type.</param>
/// <returns></returns>
public string GenerateDefinitionString(TypeArgument type)
public string GenerateDefinitionString(TypeArgumentBase type)
{
if (type.IsEnum)
{
Expand Down Expand Up @@ -74,7 +74,7 @@ public string GenerateDefinitionString(TypeArgument type)
return definitionString;
}

private string GenerateDefinitionStringForEnum(TypeArgument type)
private string GenerateDefinitionStringForEnum(TypeArgumentBase type)
{
string typeName = type.TypeNameForBlockly;
if (!type.IsEnum)
Expand Down Expand Up @@ -117,7 +117,7 @@ private long ValueEnum(object o)
}
throw new ArgumentException("there is an enum that is not valid");
}
internal (string tooltip, string propsDef) GenerateTooltipAndPropDef(TypeArgument type)
internal (string tooltip, string propsDef) GenerateTooltipAndPropDef(TypeArgumentBase type)
{
string tooltip = $"{type.Name} with props:";
string propsDef = "";
Expand Down Expand Up @@ -147,7 +147,7 @@ private long ValueEnum(object o)
/// </summary>
/// <param name="type">The type.</param>
/// <returns></returns>
public string GenerateJSstring(TypeArgument type)
public string GenerateJSstring(TypeArgumentBase type)
{
var str = typeof(string).FullName;
if (type.IsEnum)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ public class BlocklyToolBoxJSGenerator
/// </summary>
/// <param name="types">The types.</param>
/// <returns></returns>
public string GenerateBlocklyToolBoxValue(TypeArgument[] types)
public string GenerateBlocklyToolBoxValue(TypeArgumentBase[] types)
{
string blockText = "";
var globalVars = "var glbVar=function(workspace){";
foreach (var type in types)
var sort = types.OrderBy(it => it.Name).ToArray();
foreach (var type in sort)
{

var typeName = type.TypeNameForBlockly;
Expand Down Expand Up @@ -63,7 +64,7 @@ public string GenerateBlocklyToolBoxValue(TypeArgument[] types)
/// <param name="blockText">The block text.</param>
/// <param name="type">The type.</param>
/// <returns></returns>
public string GenerateToolBoxCodeForAllPropertiesOfAType(string blockText, TypeArgument type)
public string GenerateToolBoxCodeForAllPropertiesOfAType(string blockText, TypeArgumentBase type)
{
var validProperties = type.GetProperties();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class JavascriptGenerator
/// </summary>
/// <param name="type">The type.</param>
/// <returns></returns>
public string GenerateBlocklyDefinition(TypeArgument type)
public string GenerateBlocklyDefinition(TypeArgumentBase type)
{
return _definitionGenerator.GenerateBlocklyDefinition(type);
}
Expand All @@ -40,7 +40,7 @@ public string GenerateBlocklyDefinition(TypeArgument type)
/// </summary>
/// <param name="types">The types.</param>
/// <returns></returns>
public string GenerateBlocklyToolBoxValue(TypeArgument[] types)
public string GenerateBlocklyToolBoxValue(TypeArgumentBase[] types)
{
return _toolBoxJSGenerator.GenerateBlocklyToolBoxValue(types);
}
Expand Down
22 changes: 22 additions & 0 deletions src/NetCore2Blockly/NetCore2Blockly/PropertyCSharp.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Collections;

namespace NetCore2Blockly
{
public class PropertyCSharp : PropertyBase
{
public override bool IsArray
{
get
{
var str = typeof(string).FullName;
var arr = typeof(IEnumerable);
return (!PropertyType.IsValueType)
&&
(PropertyType.FullName != str)
&&
arr.IsAssignableFrom((PropertyType as TypeToGenerateFromCSharp).t);
}
}
}

}
125 changes: 6 additions & 119 deletions src/NetCore2Blockly/NetCore2Blockly/TypeArgument.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Collections.Generic;
using System.Net.Mail;

namespace NetCore2Blockly
{
public abstract class TypeArgument
public abstract class TypeArgumentBase
{
protected TypeArgument(string id)
protected TypeArgumentBase(string id)
{
this.id = id;
this.Name = id;
Expand All @@ -29,129 +25,20 @@ protected TypeArgument(string id)

public abstract string TypeNameForBlockly { get; }

public abstract Property[] GetProperties();
public abstract PropertyBase[] GetProperties();

public abstract Dictionary<string, long> GetValuesForEnum();

public abstract bool IsValueType { get; }

}
public class ListProperties : List<Property>
{

}
public abstract class Property
public abstract class PropertyBase
{
public string Name { get; set; }

public TypeArgument PropertyType { get; set; }
public TypeArgumentBase PropertyType { get; set; }

public abstract bool IsArray { get; }
}

public class PropertyCSharp : Property
{
public override bool IsArray
{
get
{
var str = typeof(string).FullName;
var arr = typeof(IEnumerable);
return (!PropertyType.IsValueType)
&&
(PropertyType.FullName != str)
&&
arr.IsAssignableFrom((PropertyType as TypeToGenerateFromCSharp).t);
}
}
}


[DebuggerDisplay("Create {id}")]

public class TypeToGenerateFromCSharp: TypeArgument
{
internal readonly Type t;
private Property[] props;
public TypeToGenerateFromCSharp(Type t) : base(t.FullName)
{
this.t = t;
props =
t.GetProperties().Where(prop => prop.GetSetMethod() != null)
.Select(it => new PropertyCSharp() {
Name = it.Name,
PropertyType = new TypeToGenerateFromCSharp(it.PropertyType)
}).ToArray();
;

}
public override Property[] GetProperties()
{
return props;
}
public override string TranslateToBlocklyType()
{
return t.TranslateToBlocklyType();
}
public override bool ConvertibleToBlocklyType()
{
return t.ConvertibleToBlocklyType();
}
public override string TranslateToBlocklyBlocksType()
{
return t.TranslateToBlocklyBlocksType();
}
public override string TranslateToNewTypeName()
{
return t.TranslateToNewTypeName();
}

public override Dictionary<string, long> GetValuesForEnum()
{
if(!IsEnum)
throw new NotImplementedException();

var names = Enum.GetNames(t);
var opt = names.Select(it => new KeyValuePair<string, long>(it, ValueEnum(Enum.Parse(t, it))));
var ret = new Dictionary<string, long>(opt);
return ret;

}
private long ValueEnum(object o)
{
try
{
return (long)o;
}
catch
{

return (int)o;
}
throw new ArgumentException("there is an enum that is not valid");
}
public override bool IsEnum
{
get
{
return t.IsEnum;
}
}

public override string TypeNameForBlockly
{
get
{

var type = Nullable.GetUnderlyingType(t) ?? t;
return type.Name;
}
}

public override string FullName => t.FullName;

public override bool IsValueType => t.IsValueType;
}

}
Loading

0 comments on commit 5c3d9ab

Please sign in to comment.