Skip to content

Commit 1246704

Browse files
author
Viktor Chernev
committed
Work on AWS.
1 parent efb8476 commit 1246704

File tree

11 files changed

+305
-1107
lines changed

11 files changed

+305
-1107
lines changed

DescribeTranspiler.AWS/Datnik.cs

Lines changed: 6 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using DescribeTranspiler;
22

33

4-
namespace DescribeCompiler.AWS
4+
namespace DescribeTranspiler.AWS
55
{
66
public class Datnik
77
{
@@ -21,7 +21,7 @@ public class Datnik
2121
public static string? logPassword;
2222

2323
/// <summary>
24-
/// Wether to try to decrypt encrypted files (.DENC)
24+
/// Wether to parse encrypted files
2525
/// </summary>
2626
public static bool parseEncryptedFiles;
2727

@@ -38,35 +38,11 @@ public class Datnik
3838

3939

4040
/// <summary>
41-
/// The input file or folder
41+
/// The input file name
4242
/// </summary>
43-
public static string? input;
43+
public static string? fileName;
4444

45-
/// <summary>
46-
/// Wether the input is a file or a folder
47-
/// </summary>
48-
public static bool isInputDir;
49-
50-
/// <summary>
51-
/// The output file or folder
52-
/// </summary>
53-
public static string? output;
54-
55-
/// <summary>
56-
/// Wether the output is a file or a folder
57-
/// </summary>
58-
public static bool isOutputDir;
59-
60-
/// <summary>
61-
/// When parsing a folder, weather to parse files in top directory only
62-
/// or parse files in child directories as well
63-
/// </summary>
64-
public static bool topOnly;
6545

66-
/// <summary>
67-
/// Weather to omit files that are not Describe source files (".DS")
68-
/// </summary>
69-
public static bool dsOnly;
7046

7147
/// <summary>
7248
/// Wether the output needs to be in a beautified format
@@ -100,40 +76,20 @@ public class Datnik
10076

10177

10278

103-
/// <summary>
104-
/// Path to an external log file
105-
/// </summary>
106-
public static string? logFilePath;
107-
108-
/// <summary>
109-
/// Weather to output logs to an external file
110-
/// </summary>
111-
public static bool logToFile;
112-
113-
114-
11579

11680
static Datnik()
11781
{
11882
inputPassword = null;
11983
outputPassword = null;
120-
parseEncryptedFiles = false;
12184
encryptOutput = false;
12285
encryptLog = false;
86+
parseEncryptedFiles = false;
12387

124-
input = null;
125-
output = null;
126-
isInputDir = false;
127-
isOutputDir = false;
128-
topOnly = false;
88+
fileName = null;
12989
isBeautified = false;
13090

13191
translatorName = "HTML_PAGE";
13292

133-
logFilePath = null;
134-
logToFile = false;
135-
136-
dsOnly = true;
13793
verbosity = LogVerbosity.Low;
13894
langVer = DescribeVersionNumber.Version10;
13995
requireSuccess = true;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<_LastSelectedProfileId>C:\Users\Viktor Chernev\Desktop\World-Describe\Language\DescribeCompiler\DescribeTranspiler.AWS\Properties\PublishProfiles\FolderProfile.pubxml</_LastSelectedProfileId>
5+
</PropertyGroup>
6+
</Project>

DescribeTranspiler.AWS/Function.cs

Lines changed: 131 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,144 @@
11
using Amazon.Lambda.APIGatewayEvents;
22
using Amazon.Lambda.Core;
33
using Newtonsoft.Json;
4+
using Newtonsoft.Json.Linq;
45
using System.Net;
6+
using System.Text;
57

68
// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
79
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]
8-
namespace DescribeCompiler.AWS;
10+
namespace DescribeTranspiler.AWS;
911

1012
public class Function
1113
{
12-
//https://www.youtube.com/watch?v=IHIJFVUQyFY - AWS LAMBDA For The .NET Developer
1314
public static readonly bool LOG_STACK_TRACES = true;
1415

1516

17+
/// <summary>
18+
/// Lambda function. Translate single Describe file to single JSON file.
19+
/// </summary>
20+
/// <param name="request">The Http Request object</param>
21+
/// <param name="context">The Aws Lambda function context</param>
22+
/// <returns>A response object</returns>
23+
public APIGatewayProxyResponse HTranspile_SingleDmlToSingleJson(APIGatewayProxyRequest request, ILambdaContext context)
24+
{
25+
try
26+
{
27+
// preset
28+
Messages.Log = "";
29+
Messages.printInitialMessage("Single DML to Single JSON");
30+
31+
// check
32+
if (request == null)
33+
{
34+
throw new Exception("APIGatewayProxyRequest object is NULL");
35+
}
36+
37+
// decode
38+
byte[] data = Convert.FromBase64String(request.Body);
39+
string decodedString = System.Text.Encoding.UTF8.GetString(data);
40+
Messages.printCmdJson(decodedString);
41+
42+
// read parameters
43+
JObject jsonObject = JObject.Parse(decodedString);
44+
string? command = (string?)jsonObject["command"];
45+
string? filename = (string?)jsonObject["filename"];
46+
string? code = (string?)jsonObject["code"];
47+
string? input_password = (string?)jsonObject["input_password"];
48+
string? output_password = (string?)jsonObject["output_password"];
49+
string? log_password = (string?)jsonObject["log_password"];
50+
string? language_version = (string?)jsonObject["language_version"] ?? "1.0";
51+
string? translator = (string?)jsonObject["translator"] ?? "JSON_PLAIN";
52+
string? beautify = (string?)jsonObject["beautify"] ?? "false";
53+
string? verbosity = (string?)jsonObject["verbosity"] ?? "low";
54+
55+
// check command
56+
if(command != "parse-file")
57+
{
58+
Messages.printCommandError(command, "parse-file");
59+
throw new Exception("Unexpected command");
60+
}
61+
62+
// check code
63+
if(string.IsNullOrEmpty(code) || string.IsNullOrWhiteSpace(code))
64+
{
65+
Messages.printArgumentError(code, "code", "Code is NULL or empty");
66+
throw new Exception("Invalid code");
67+
}
68+
69+
// decode code
70+
byte[] codedata = Convert.FromBase64String(code);
71+
code = System.Text.Encoding.UTF8.GetString(codedata);
72+
73+
// check code
74+
if (string.IsNullOrEmpty(code) || string.IsNullOrWhiteSpace(code))
75+
{
76+
Messages.printArgumentError(code, "code", "Code is NULL or empty after decoding");
77+
throw new Exception("Invalid code");
78+
}
79+
80+
// read args
81+
Arguments.readFilenameArgument(filename);
82+
Arguments.readTranslatorArgument(translator);
83+
Arguments.readBeautifyArgument(beautify);
84+
Arguments.readVerbosityArgument(verbosity);
85+
Arguments.readLanguageVersion(language_version);
86+
Arguments.readInputPasswordArgument(input_password);
87+
Arguments.readOutputPasswordArgument(output_password);
88+
Arguments.readLogPasswordArgument(log_password);
89+
90+
// parse
91+
string? json = MainFunctions.Compile(code);
92+
if (json != null) Messages.printCompilationSuccess();
93+
else
94+
{
95+
Messages.printFatalError("Parser returned null");
96+
throw new Exception("Parsing failed");
97+
}
98+
99+
// to base64
100+
byte[] byteArray = Encoding.UTF8.GetBytes(json);
101+
string base64Json = Convert.ToBase64String(byteArray);
102+
103+
// output
104+
OutputJson result = new OutputJson();
105+
if (string.IsNullOrEmpty(json)) result.Result = "Error";
106+
else result.Result = "Success";
107+
result.Command = command;
108+
result.Logs = Messages.Log;
109+
result.Json = base64Json;
110+
111+
// return
112+
var response = new APIGatewayProxyResponse
113+
{
114+
StatusCode = (int)HttpStatusCode.OK,
115+
Body = JsonConvert.SerializeObject(result),
116+
Headers = new Dictionary<string, string> { { "Content-Type", "application/json" }, { "Access-Control-Allow-Origin", "*" } }
117+
};
118+
return response;
119+
}
120+
catch (Exception ex)
121+
{
122+
var tres = new APIGatewayProxyResponse
123+
{
124+
StatusCode = (int)HttpStatusCode.OK,
125+
Body = "ERROR!" + ex.Message,
126+
Headers = new Dictionary<string, string> { { "Content-Type", "application/json" }, { "Access-Control-Allow-Origin", "*" } }
127+
};
128+
return tres;
129+
}
130+
}
131+
132+
133+
16134

17135
/// <summary>
18136
/// The main function - handles HTTP POST request invocation
19137
/// </summary>
20138
/// <param name="request">The Http Request object</param>
21139
/// <param name="context">The Aws Lambda function context</param>
22140
/// <returns>A response object</returns>
23-
public APIGatewayProxyResponse FunctionHandler(APIGatewayProxyRequest request, ILambdaContext context)
141+
public APIGatewayProxyResponse FunctionHandlerPOST(APIGatewayProxyRequest request, ILambdaContext context)
24142
{
25143
try
26144
{
@@ -31,7 +149,7 @@ public APIGatewayProxyResponse FunctionHandler(APIGatewayProxyRequest request, I
31149
//do something about clearing logs or putting a
32150
//separator line in logs when starting a new job in the CLI !!!
33151
Messages.Log = "";
34-
Messages.printLogo();
152+
Messages.printInitialMessage(null);
35153

36154
//check
37155
if (request == null)
@@ -134,7 +252,7 @@ public APIGatewayProxyResponse FunctionHandler(APIGatewayProxyRequest request, I
134252
{
135253
//output request object as JSON for testing
136254
//string sssss = JsonConvert.SerializeObject(request);
137-
255+
138256

139257
string json = multiparse(code, translator, verbosity);
140258
OutputJson result = new OutputJson();
@@ -243,7 +361,7 @@ public APIGatewayProxyResponse FunctionHandlerGET(APIGatewayProxyRequest request
243361
//do something about clearing logs or putting a
244362
//separator line in logs when starting a new job in the CLI !!!
245363
Messages.Log = "";
246-
Messages.printLogo();
364+
Messages.printInitialMessage(null);
247365
Messages.printCmdLineForGET(request);
248366

249367

@@ -348,9 +466,9 @@ public OutputJson FunctionHandlerMock(InputJson inputJson, ILambdaContext contex
348466
try
349467
{
350468
//preset
351-
Messages.printLogo();
469+
Messages.printInitialMessage(null);
352470
Messages.printCmdLine(inputJson);
353-
471+
354472
//read args
355473
if (inputJson.Command == null)
356474
{
@@ -410,7 +528,6 @@ public OutputJson FunctionHandlerMock(InputJson inputJson, ILambdaContext contex
410528
}
411529
}
412530

413-
414531
static string multiparse(string code, string translator, string verbosiy)
415532
{
416533
//read options
@@ -439,7 +556,7 @@ static string parse(string code, string translator, string verbosiy, string file
439556
if (Arguments.readTranslatorArgument(translator) == false) return null;
440557

441558
//Compile
442-
string result = MainFunctions.Compile(code, filename);
559+
string result = MainFunctions.Compile(code);
443560
if (result != null) Messages.printCompilationSuccess();
444561
else Messages.printFatalError("Parser returned null");
445562

@@ -449,11 +566,11 @@ static string parse(string code, string translator, string verbosiy, string file
449566
static string parse(InputJson inputJson)
450567
{
451568
//read options
452-
if (Arguments.readVerbosityArgument(inputJson) == false) return null;
453-
if (Arguments.readTranslatorArgument(inputJson) == false) return null;
569+
if (Arguments.readVerbosityArgument(inputJson.ToString()!) == false) return null;
570+
if (Arguments.readTranslatorArgument(inputJson.ToString()!) == false) return null;
454571

455572
//Compile
456-
string result = MainFunctions.Compile(inputJson.Code, inputJson.Filename);
573+
string result = MainFunctions.Compile(inputJson.Code);
457574
Messages.printCompilationSuccess();
458575

459576
//return
@@ -477,5 +594,4 @@ public class OutputJson
477594
public string? Result { get; set; }
478595
public string? Logs { get; set; }
479596
public string? Json { get; set; }
480-
}
481-
//search for "AWS Explorer"
597+
}

0 commit comments

Comments
 (0)