Skip to content

Commit

Permalink
Make things more testable
Browse files Browse the repository at this point in the history
  • Loading branch information
nloum committed Nov 7, 2024
1 parent 2528d7c commit 7a55817
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 32 deletions.
31 changes: 31 additions & 0 deletions CodegenBot/CodegenBotImports.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Text.Json;
using System.Text.Json.Serialization.Metadata;
using Extism;

namespace CodegenBot;

public interface ICodegenBotImports
{
string GraphQL<T>(GraphQLRequest<T> request, JsonTypeInfo<GraphQLRequest<T>> jsonTypeInfo);
void Log(LogEvent logEvent);
}

public class CodegenBotImports : ICodegenBotImports
{
public string GraphQL<T>(GraphQLRequest<T> request, JsonTypeInfo<GraphQLRequest<T>> jsonTypeInfo)
{
var json = request.ToJsonString(jsonTypeInfo);
using var block = Pdk.Allocate(json);
var ptr = Imports.ExternGraphQL(block.Offset);
var response = MemoryBlock.Find(ptr).ReadString();
response = JsonUtility.EnsureTypeDiscriminatorPropertiesComeFirst(response);
return response;
}

public void Log(LogEvent logEvent)
{
var json = JsonSerializer.Serialize(logEvent, LogEventJsonContext.Default.LogEvent);
using var block = Pdk.Allocate(json);
Imports.ExternLog(block.Offset);
}
}
32 changes: 0 additions & 32 deletions CodegenBot/Imports.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using System;
using System.Runtime.InteropServices;
using System.Text.Json;
using System.Text.Json.Serialization.Metadata;
using CodegenBot;
using Extism;

namespace CodegenBot;

Expand All @@ -16,35 +13,6 @@ public class Imports
[DllImport("extism", EntryPoint = "cgb_graphql")]
public static extern ulong ExternGraphQL(ulong offset);

public static string GraphQL<T>(GraphQLRequest<T> request, JsonTypeInfo<GraphQLRequest<T>> jsonTypeInfo)
{
var json = request.ToJsonString(jsonTypeInfo);
using var block = Pdk.Allocate(json);
var ptr = ExternGraphQL(block.Offset);
var response = MemoryBlock.Find(ptr).ReadString();
response = JsonUtility.EnsureTypeDiscriminatorPropertiesComeFirst(response);
return response;
}

[DllImport("extism", EntryPoint = "cgb_log")]
public static extern void ExternLog(ulong offset);

public static void Log(LogEvent logEvent)
{
var json = JsonSerializer.Serialize(logEvent, LogEventJsonContext.Default.LogEvent);
using var block = Pdk.Allocate(json);
ExternLog(block.Offset);
}

[DllImport("extism", EntryPoint = "cgb_random")]
public static extern ulong ExternRandom(ulong offset);

public static byte[] GetRandomBytes(int numBytes)
{
var text = numBytes.ToString();
using var block = Pdk.Allocate(text);
var ptr = ExternRandom(block.Offset);
var response = MemoryBlock.Find(ptr).ReadBytes();
return response;
}
}

0 comments on commit 7a55817

Please sign in to comment.