11using Amazon . Lambda . APIGatewayEvents ;
22using Amazon . Lambda . Core ;
33using Newtonsoft . Json ;
4+ using Newtonsoft . Json . Linq ;
45using 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
1012public 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