Skip to content

Commit 98f817e

Browse files
author
Gaurav Saini
committed
Added JSON input and output
1 parent eb23f55 commit 98f817e

File tree

1 file changed

+66
-57
lines changed

1 file changed

+66
-57
lines changed

csharp/intro/README.md

Lines changed: 66 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -284,16 +284,16 @@ methods and async.
284284
using Fnproject.Fn.Fdk;
285285
286286
using System.Runtime.CompilerServices;
287-
[assembly:InternalsVisibleTo("Function.Tests")]
287+
[assembly:InternalsVisibleTo("Function.Tests")]
288288
namespace Function {
289-
class Greeter {
290-
public string greet(string input) {
291-
return string.Format("Hello {0}!",
292-
input.Length == 0 ? "World" : input.Trim());
293-
}
294-
295-
static void Main(string[] args) { Fdk.Handle(args[0]); }
296-
}
289+
class Greeter {
290+
public string greet(string input) {
291+
return string.Format("Hello {0}!",
292+
input.Length == 0 ? "World" : input.Trim());
293+
}
294+
295+
static void Main(string[] args) { Fdk.Handle(args[0]); }
296+
}
297297
}
298298
```
299299
@@ -313,21 +313,21 @@ using Function;
313313
using NUnit.Framework;
314314
315315
namespace Function.Tests {
316-
public class GreeterTest {
317-
[Test]
318-
public void TestGreetValid() {
319-
Greeter greeter = new Greeter();
320-
string response = greeter.greet("Dotnet");
321-
Assert.AreEqual("Hello Dotnet!", response);
322-
}
323-
324-
[Test]
325-
public void TestGreetEmpty() {
326-
Greeter greeter = new Greeter();
327-
string response = greeter.greet("");
328-
Assert.AreEqual("Hello World!", response);
329-
}
330-
}
316+
public class GreeterTest {
317+
[Test]
318+
public void TestGreetValid() {
319+
Greeter greeter = new Greeter();
320+
string response = greeter.greet("Dotnet");
321+
Assert.AreEqual("Hello Dotnet!", response);
322+
}
323+
324+
[Test]
325+
public void TestGreetEmpty() {
326+
Greeter greeter = new Greeter();
327+
string response = greeter.greet("");
328+
Assert.AreEqual("Hello World!", response);
329+
}
330+
}
331331
}
332332
```
333333
@@ -343,33 +343,42 @@ Building image fndemouser/dotnetfn:0.0.2 .......
343343
Function fndemouser/dotnetfn:0.0.2 built successfully.
344344
```
345345
346-
## Accepting JSON Input
346+
## Working with JSON
347347
348348
Let's convert this function to use JSON for its input and output.
349-
Replace the definition of `HelloFunction` with the following:
349+
Replace the content of `Program.cs` with the following:
350350
351351
```csharp
352352
using Fnproject.Fn.Fdk;
353353
using System;
354354
355355
using System.Runtime.CompilerServices;
356356
[assembly:InternalsVisibleTo("Function.Tests")] namespace Function {
357-
class Input {
358-
public string name {
359-
get;
360-
set;
361-
}
357+
class Input {
358+
public string name {
359+
get;
360+
set;
362361
}
362+
}
363363
364-
class Greeter {
365-
public string greet(Input input) {
366-
return string.Format("Hello {0}!", string.IsNullOrEmpty(input.name)
367-
? "World"
368-
: input.name.Trim());
369-
}
364+
class Output {
365+
public string message {
366+
get;
367+
set;
368+
}
370369
371-
static void Main(string[] args) { Fdk.Handle(args[0]); }
370+
public Output(string message) { this.message = message; }
371+
}
372+
373+
class Greeter {
374+
public Output greet(Input input) {
375+
return new Output(string.Format(
376+
"Hello {0}!",
377+
string.IsNullOrEmpty(input.name) ? "World" : input.name.Trim()));
372378
}
379+
380+
static void Main(string[] args) { Fdk.Handle(args[0]); }
381+
}
373382
}
374383
```
375384
@@ -380,24 +389,24 @@ using Function;
380389
using NUnit.Framework;
381390
382391
namespace Function.Tests {
383-
public class GreeterTest {
384-
[Test]
385-
public void TestGreetEmpty() {
386-
Greeter greeter = new Greeter();
387-
Input input = new Input();
388-
string response = greeter.greet(input);
389-
Assert.AreEqual("Hello World!", response);
390-
}
391-
392-
[Test]
393-
public void TestGreetValid() {
394-
Greeter greeter = new Greeter();
395-
Input input = new Input();
396-
input.name = "Dotnet";
397-
string response = greeter.greet(input);
398-
Assert.AreEqual("Hello Dotnet!", response);
399-
}
392+
public class GreeterTest {
393+
[Test]
394+
public void TestGreetEmpty() {
395+
Greeter greeter = new Greeter();
396+
Input input = new Input();
397+
Output output = greeter.greet(input);
398+
Assert.AreEqual("Hello World!", output.message);
400399
}
400+
401+
[Test]
402+
public void TestGreetValid() {
403+
Greeter greeter = new Greeter();
404+
Input input = new Input();
405+
input.name = "Dotnet";
406+
Output output = greeter.greet(input);
407+
Assert.AreEqual("Hello Dotnet!", output.message);
408+
}
409+
}
401410
}
402411
```
403412
@@ -454,13 +463,13 @@ Use `curl` to invoke the function:
454463
455464
![user input](images/userinput.png)
456465
>```sh
457-
> curl -X "POST" -H "Content-Type: application/json" http://localhost:8080/invoke/01G1ZZMSA7NG8G00GZJ0000002
466+
> curl -X "POST" -H "Content-Type: application/json" http://localhost:8080/invoke/01G1ZZMSA7NG8G00GZJ0000002 --data-raw '{"name":"Dotnet"}'
458467
>```
459468
460469
The result is now in a JSON format.
461470
462471
```
463-
Hello World!
472+
{"message":"Hello Dotnet!"}
464473
```
465474
466475
## Wrap Up

0 commit comments

Comments
 (0)