Skip to content

Commit

Permalink
Bump warnings to level 2 verbosity, fix async method headers.
Browse files Browse the repository at this point in the history
  • Loading branch information
c272 committed Feb 26, 2020
1 parent 9f77c81 commit 561a450
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 26 deletions.
2 changes: 1 addition & 1 deletion ArgumentParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public ArgumentParser(string[] args)
{
for (int i=0; i<args.Length; i++)
{
if (!validArgs.Contains(args[i]))
if (args[i].Length == 0 || !validArgs.Contains(args[i].Substring(1)))
{
Logger.Exit("Invalid argument supplied '" + args[i] + "'.");
return;
Expand Down
12 changes: 8 additions & 4 deletions burrito-api/BurritoAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ public static int Run()
case "GET":
case "get":
//Figure out a data type from the API endpoint (if possible).
var classReturned = DataTypeCreator.DeriveFromRoute(schema.RootPath, route);
bool isList = false;
var classReturned = DataTypeCreator.DeriveFromRoute(schema.RootPath, route, ref isList);
if (classReturned == null) { break; }

//Add class.
Expand All @@ -130,13 +131,15 @@ public static int Run()
RouteParams = route.GetRouteVariables(),
XMLSummary = route.GetSummary(),
Name = route.GetMethodName(),
ReceivedDataType = classReturned
ReceivedDataType = classReturned,
ReturnsList = isList
});
break;
case "POST":
case "post":
//Figure out a data type it should receive.
var postClassReturned = DataTypeCreator.DeriveFromRoute(schema.RootPath, route, route.ExampleData);
bool returnsList = false;
var postClassReturned = DataTypeCreator.DeriveFromRoute(schema.RootPath, route, ref returnsList, route.ExampleData);
if (postClassReturned == null) { break; }

//Figure out the data type is should send.
Expand Down Expand Up @@ -166,7 +169,8 @@ public static int Run()
RouteParams = route.GetRouteVariables(),
SentDataType = postClass,
ReceivedDataType = postClassReturned,
Name = route.GetMethodName()
Name = route.GetMethodName(),
ReturnsList = returnsList
});
break;
}
Expand Down
29 changes: 28 additions & 1 deletion burrito-api/Data Structures/APIMethodModule.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;

namespace Burrito
{
Expand All @@ -24,6 +25,32 @@ public abstract class APIMethodModule

//List of variables in the route that need to be put as params.
public List<string> RouteParams { get; set; } = new List<string>();

//Whether this method returns a list or not.
public bool ReturnsList { get; set; }

/// <summary>
/// Clones this API method module.
/// </summary>
public APIMethodModule Clone()
{
return (APIMethodModule)this.MemberwiseClone();
}

/// <summary>
/// Gets a string representation of this method's return type.
/// </summary>
public string GetReturnType()
{
if (ReturnsList)
{
return "List<" + ReceivedDataType.Name + ">";
}
else
{
return ReceivedDataType.Name;
}
}
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions burrito-api/Data Structures/APISchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,9 @@ public bool Validate()
}

//Is the route relative URL even valid?
if (!Regex.IsMatch(RelativeURL, "^[A-Za-z_\\-\\.0-9\\?\\=%#@/\\{\\}]+$"))
if (!Regex.IsMatch(RelativeURL, "^[A-Za-z_\\-\\.0-9\\?\\=%#&@/\\{\\}]+$"))
{
Logger.Write("[ERR] - Invalid relative route provided ('" + RelativeURL + "'.", 1);
Logger.Write("[ERR] - Invalid relative route provided ('" + RelativeURL + "').", 1);
return false;
}

Expand Down
12 changes: 7 additions & 5 deletions burrito-api/Data Structures/ClassModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public string GenerateCode(string ns, params string[] extraIncludes)
}
catch (Exception e)
{
Logger.Write("[WARN] - Failed to beautify code, could not parse as C#: '" + e.Message + "'.", 1);
Logger.Write("[WARN] - Failed to beautify code, could not parse as C#: '" + e.Message + "'.", 2);
}
return code;
}
Expand All @@ -132,7 +132,9 @@ private void GenerateMethods(ref string code, List<APIMethodModule> methods)
//If generate async and sync is on, and async, generate the synchronous method first.
if (method.Async && BurritoAPI.GenerateAsyncAndSync)
{
GenerateMethods(ref code, new List<APIMethodModule>() { method });
var methodCopy = method.Clone();
methodCopy.Async = false;
GenerateMethods(ref code, new List<APIMethodModule>() { methodCopy });
}

//XML summary.
Expand All @@ -147,7 +149,7 @@ private void GenerateMethods(ref string code, List<APIMethodModule> methods)
}
else
{
code += "public static " + method.ReceivedDataType.Name + " ";
code += "public static " + method.GetReturnType() + " ";
}

//Open parameters.
Expand All @@ -162,7 +164,7 @@ private void GenerateMethods(ref string code, List<APIMethodModule> methods)
}
if (method.Async)
{
params_.Add("BurritoCore.APICallReturnDelegate<" + method.ReceivedDataType.Name + "> callback");
params_.Add("BurritoCore.APICallReturnDelegate<" + method.GetReturnType() + "> callback");
}

//Optional template params Templates are replaced by blank by default.
Expand Down Expand Up @@ -196,7 +198,7 @@ private void GenerateMethods(ref string code, List<APIMethodModule> methods)
{
code += "Async";
}
code += "<" + method.ReceivedDataType.Name + ">(";
code += "<" + method.GetReturnType() + ">(";

//Required URL parameter, post data and async.
code += "_globals.RootURL + $\"" + method.Route + "\", ";
Expand Down
4 changes: 2 additions & 2 deletions burrito-api/Data Structures/ProjectCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public void CompileToDLL(string generationPath)
}
catch
{
Logger.Write("[WARN] - Failed renaming packed (.dll.packed) to normal (.dll), insufficient permissions. Job still completed.", 1);
Logger.Write("[WARN] - Failed renaming packed (.dll.packed) to normal (.dll), insufficient permissions. Job still completed.", 2);
}
}

Expand Down Expand Up @@ -198,7 +198,7 @@ public void CompileToProject(string generationPath)
catch (Exception e)
{
if (dll == "System") { continue; } //ignore missing system.dll
Logger.Write("[WARN] - Failed to copy dependency DLL '" + dll + "'. Reference left.", 1);
Logger.Write("[WARN] - Failed to copy dependency DLL '" + dll + "'. Reference left.", 2);
}
}

Expand Down
51 changes: 40 additions & 11 deletions burrito-api/Object Generation/DataTypeCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class DataTypeCreator
/// <summary>
/// Derives an API data type from a given HTTP GET/POST route.
/// </summary>
public static ClassModule DeriveFromRoute(string baseURI, Route route, JObject postData=null)
public static ClassModule DeriveFromRoute(string baseURI, Route route, ref bool isList, JObject postData=null)
{
//Attempt to ping the route for a response.
string response;
Expand Down Expand Up @@ -47,16 +47,42 @@ public static ClassModule DeriveFromRoute(string baseURI, Route route, JObject p
JObject jobj;
try
{
//Is it an object?
jobj = JObject.Parse(response);
isList = false;

//Construct a class from it.
return FromJObject(jobj, route.ReturnedDataName);
}
catch
{
Logger.Write("[ERR] - Failed to derive data from route '" + route.RelativeURL + "', invalid JSON response.", 1);
return null;
}
//Maybe it's an array?
try
{
var jarr = JArray.Parse(response);

//Yes, set method return type as array.
isList = true;

//An array of what?
if (jarr.Count == 0)
{
Logger.Write("[WARN] - Cannot derive type from empty array, assuming as an empty. (route '" + route.RelativeURL + "')", 2);
return BurritoAPI.Project.Namespaces["@"].Find(x => x.Name == "_empty");
}

//Construct a class from it.
return FromJObject(jobj, route.ReturnedDataName);
//Deriving type.
var field = GenerateField(route.ReturnedDataName, "", jarr[0]);

//Return a dummy class with the right typename.
return new ClassModule(field.TypeName);
}
catch
{
Logger.Write("[ERR] - Failed to derive data from route '" + route.RelativeURL + "', invalid JSON response. (route '" + route.RelativeURL + "')", 1);
return null;
}
}
}

/// <summary>
Expand All @@ -70,12 +96,12 @@ public static ClassModule FromJObject(JObject jobj, string name)
//If the name or JObject is null, invalid.
if (jobj == null)
{
Logger.Write("[ERR] - Invalid JSON data given to create a class module from, no data.", 1);
Logger.Write("[ERR] - Invalid JSON data given to create a class module from, no data. ('" + name + "')", 1);
return null;
}
if (name == null)
{
Logger.Write("[ERR] - No name provided for creating returned data from a route.", 1);
Logger.Write("[ERR] - No name provided for creating returned data from a route. ('" + name + "')", 1);
return null;
}

Expand All @@ -88,6 +114,9 @@ public static ClassModule FromJObject(JObject jobj, string name)
return module;
}

/// <summary>
/// Generates a field to use in a generated class.
/// </summary>
private static Field GenerateField(string rootName, string name, JToken value)
{
switch (value.Type)
Expand All @@ -98,7 +127,7 @@ private static Field GenerateField(string rootName, string name, JToken value)
var arr = (JArray)value;
if (arr.Count == 0)
{
Logger.Log("[WARN] - Cannot generate a type from an empty array. Leaving an empty list type here.");
Logger.Log("[WARN] - Cannot generate a type from an empty array. Leaving an empty list type here. ('" + name + "')");
return new Field(name, ClassModule.Empty());
}

Expand Down Expand Up @@ -138,11 +167,11 @@ private static Field GenerateField(string rootName, string name, JToken value)
case JTokenType.String:
return new Field(name, "string");
case JTokenType.Null:
Logger.Write("[WARN] - Null type in JSON at property '" + name + "', assumed as an object.", 1);
Logger.Write("[WARN] - Null type in JSON at property '" + name + "', assumed as an object. ('" + name + "')", 2);
return new Field(name, "object ");

default:
Logger.Write("[WARN] - Unsupported type in JSON to create a class from: '" + value.Type.ToString() + "' for generated data class '" + name + "'. Skipped property.", 1);
Logger.Write("[WARN] - Unsupported type in JSON to create a class from: '" + value.Type.ToString() + "' for generated data class '" + name + "'. Skipped property.", 2);
return null;
}
}
Expand Down

0 comments on commit 561a450

Please sign in to comment.