Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: v3 .NET #144

Merged
merged 16 commits into from
Jun 8, 2023
Merged
Prev Previous commit
Next Next commit
Remove wrapper from Dotnet
  • Loading branch information
Meldiron committed Feb 7, 2023
commit c1e28a4630643b3be19e7e43f3a812b38a45fdc3
33 changes: 29 additions & 4 deletions runtimes/dotnet-6.0/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ To learn more about runtimes, visit [Structure](https://github.com/open-runtimes

```bash
mkdir dotnet-or && cd dotnet-or
printf "public async Task<RuntimeOutput> Main(RuntimeContext Context) => Context.Res.Json(new() {{ \"n\", new System.Random().NextDouble() }} );" > Index.cs
printf "namespace DotNetRuntime;\npublic class Handler {\n public async Task<RuntimeOutput> Main(RuntimeContext Context) => Context.Res.Json(new() {{ \"n\", new System.Random().NextDouble() }} );\n}" > Index.cs
```

2. Build the code:
Expand Down Expand Up @@ -67,17 +67,42 @@ You can also make changes to the example code and apply the changes with the `do

## Notes

- When writing function for this runtime, ensure it is named `Main`. An example of this is:
- When writing function for this runtime, ensure it is named `Main` and is inside `Handler` class. An example of this is:

```cs
public async Task<RuntimeOutput> Main(RuntimeContext Context) =>
Context.Res.Send("Hello Open Runtimes 👋");
namespace DotNetRuntime;

public class Handler {
public async Task<RuntimeOutput> Main(RuntimeContext Context) =>
Context.Res.Send("Hello Open Runtimes 👋");
}
```

- Your entrypoint code must start with `namespace DotNetRuntime;`.

- To handle dependencies, you need to have `csproj` file containing the `PackageReferences` you desire. Dependencies will be automatically cached and installed, so you don't need to include the `.nuget` folder in your function.

- The default entrypoint is `Index.cs`. If your entrypoint differs, make sure to configure it using `OPEN_RUNTIMES_ENTRYPOINT` environment variable, for instance, `OPEN_RUNTIMES_ENTRYPOINT=src/App.cs`.

- F# can be used in .NET runtime:

```fs
namespace DotNetRuntime

type Handler()=
[YOUR_CODE]
```

- Visual Basic can be used in .NET runtime:

```vb
Namespace DotNetRuntime
Public Class Handler
[YOUR_CODE]
End Class
End Namespace
```

## Authors

**Eldad Fux**
Expand Down
70 changes: 0 additions & 70 deletions runtimes/dotnet-6.0/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,6 @@ build() {
break
done


case ${OPEN_RUNTIMES_ENTRYPOINT##*.} in
cs) write_cs_wrapper ;;
fs) write_fs_wrapper ;;
vb) write_vb_wrapper ;;
esac

# Remove the user code file (copy)
rm "${OPEN_RUNTIMES_ENTRYPOINT}"

# Build the executable
cd /usr/local/src
dotnet publish DotNetRuntime.csproj -c Release
Expand All @@ -47,64 +37,4 @@ build() {
tar -czf /usr/code/code.tar.gz .
}

write_cs_wrapper() {
# Read user code and collect usings
USINGS=""
while read line; do
case "${line}" in using*)
USINGS="${USINGS}${line}
"
esac
done < "$OPEN_RUNTIMES_ENTRYPOINT"
CODE="$(sed /using*/d "$OPEN_RUNTIMES_ENTRYPOINT")"

# Wrap the user code in a class
echo "${USINGS}
namespace DotNetRuntime;
public class Wrapper {
${CODE}
}
" > Wrapper.cs
}

write_fs_wrapper() {
# Read user code and collect opens
OPENS=""
while read line; do
case "${line}" in open*)
OPENS="${OPENS}${line}
"
esac
done < "$OPEN_RUNTIMES_ENTRYPOINT"
CODE="$(sed /open*/d "$OPEN_RUNTIMES_ENTRYPOINT" | sed 's/^/ /')"
# Wrap the user code in a class
echo "namespace DotNetRuntime
${OPENS}
type Wrapper()=
${CODE}
" > Wrapper.fs

cat Wrapper.fs
}

write_vb_wrapper() {
# Read user code and collect imports
IMPORTS=""
while read line; do
case "${line}" in Imports*)
IMPORTS="${IMPORTS}${line}
"
esac
done < "$OPEN_RUNTIMES_ENTRYPOINT"
CODE="$(sed /using*/d "$OPEN_RUNTIMES_ENTRYPOINT")"

# Wrap the user code in a class
echo "${IMPORTS}
Namespace DotNetRuntime
Public Class Wrapper
${CODE}
End Class
End Namespace" > Wrapper.vb
}

build
38 changes: 21 additions & 17 deletions runtimes/dotnet-6.0/example/Index.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
using Newtonsoft.Json;
namespace DotNetRuntime;

static readonly HttpClient http = new();
using Newtonsoft.Json;

public async Task<RuntimeOutput> Main(RuntimeContext Context)
{
string id = "1";
public class Handler {
static readonly HttpClient http = new();

if (!(Context.Req.Body is String))
public async Task<RuntimeOutput> Main(RuntimeContext Context)
{
Dictionary<string, object> body = (Dictionary<string, object>) Context.Req.Body;
id = body.TryGetValue("id", out var value) ? (string) value : "1";
}
string id = "1";

var response = await http.GetStringAsync($"https://jsonplaceholder.typicode.com/todos/{id}");
var todo = JsonConvert.DeserializeObject<Dictionary<string, object>>(response, settings: null);
if (!(Context.Req.Body is String))
{
Dictionary<string, object> body = (Dictionary<string, object>) Context.Req.Body;
id = body.TryGetValue("id", out var value) ? (string) value : "1";
}

return Context.Res.Json(new()
{
{ "message", "Hello Open Runtimes 👋" },
{ "todo", todo }
});
}
var response = await http.GetStringAsync($"https://jsonplaceholder.typicode.com/todos/{id}");
var todo = JsonConvert.DeserializeObject<Dictionary<string, object>>(response, settings: null);

return Context.Res.Json(new()
{
{ "message", "Hello Open Runtimes 👋" },
{ "todo", todo }
});
}
}
4 changes: 2 additions & 2 deletions runtimes/dotnet-6.0/src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ static async Task<IResult> Execute(HttpRequest Request)

try
{
var CodeWrapper = new Wrapper();
Output = await CodeWrapper.Main(Context);
var CodeHandler = new Handler();
Output = await CodeHandler.Main(Context);
}
catch (Exception e)
{
Expand Down
Loading