Skip to content

Commit

Permalink
Merge pull request #227 from betwixt-labs/features/rpc
Browse files Browse the repository at this point in the history
features/rpc - typescript
  • Loading branch information
Andrew committed Apr 28, 2023
2 parents c5c2d00 + 0698c8d commit e212f02
Show file tree
Hide file tree
Showing 19 changed files with 437 additions and 278 deletions.
4 changes: 2 additions & 2 deletions .env
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION="2.5.1"
VERSION="2.5.2"
MAJOR=2
MINOR=5
PATCH=1
PATCH=2
2 changes: 1 addition & 1 deletion Compiler/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ private static async Task<BebopSchema> ParseAndValidateSchema(List<string> schem
if (result == Err) return Err;
var generator = makeGenerator(schema);
generator.WriteAuxiliaryFiles(outputFile.DirectoryName ?? string.Empty);
var compiled = generator.Compile(langVersion, !(_flags?.SkipGeneratedNotice ?? false));
var compiled = generator.Compile(langVersion, writeGeneratedNotice: !(_flags?.SkipGeneratedNotice ?? false));
await File.WriteAllTextAsync(outputFile.FullName, compiled);
return Ok;
}
Expand Down
37 changes: 31 additions & 6 deletions Core/Exceptions/Exceptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -275,21 +275,46 @@ public DuplicateServiceDiscriminatorException(Token discriminator, string servic
}

[Serializable]
class DuplicateServiceFunctionNameException : SpanException
class DuplicateServiceMethodNameException : SpanException
{
public DuplicateServiceFunctionNameException(ushort discriminator, string serviceName, string functionName, Span span)
public DuplicateServiceMethodNameException(uint discriminator, string serviceName, string functionName, Span span)
: base($"Index {discriminator} duplicates the function name '{functionName}' which can only be used once in service '{serviceName}'.", span, 130)
{ }
}

[Serializable]
class DuplicateArgumentName : SpanException
class DuplicateServiceMethodIdException : SpanException
{
public DuplicateArgumentName(Span span, string serviceName, string serviceIndex, string argumentName)
: base($"Index {serviceIndex} in service '{serviceName}' has duplicated argument name {argumentName}.", span, 131)
public DuplicateServiceMethodIdException(uint id, string serviceName, string methodName, Span span)
: base($"Index {id} duplicates the function name '{methodName}' which can only be used once in service '{serviceName}'.", span, 131)
{ }
}


[Serializable]
class InvalidServiceRequestTypeException : SpanException
{
public InvalidServiceRequestTypeException(string serviceName, string methodName, TypeBase type, Span span)
: base($"The request type of method '{methodName}' in service '{serviceName}' is '{type.AsString}' must be a message, struct, or union.", span, 132)
{ }
}
[Serializable]
class InvalidServiceReturnTypeException : SpanException
{
public InvalidServiceReturnTypeException(string serviceName, string methodName, TypeBase type, Span span)
: base($"The return type of method '{methodName}' in service '{serviceName}' is '{type.AsString}' but must be a message, struct, or union.", span, 133)
{ }
}

[Serializable]
class ServiceMethodIdCollisionException : SpanException
{
public ServiceMethodIdCollisionException(string serviceOneName, string methodOneName, string serviceTwoName, string methodTwoName, uint id, Span span)
: base($"The hashed ID of service '{serviceOneName}' and method '{methodOneName}' collides with the hashed ID of service '{serviceTwoName}' and '{methodTwoName}' (id: {id}).", span, 134)
{ }
}



[Serializable]
public class EnumZeroWarning : SpanException
{
Expand Down
3 changes: 2 additions & 1 deletion Core/Generators/BaseGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ protected BaseGenerator(BebopSchema schema)
/// Generate code for a Bebop schema.
/// </summary>
/// <param name="languageVersion">Determines a default language version the generated code will target.</param>
/// <param name="services">Determines which components of a service will be generated. default to both client and server.</param>
/// <param name="writeGeneratedNotice">Whether a generation notice should be written at the top of files. This is true by default.</param>
/// <returns>The generated code.</returns>
public abstract string Compile(Version? languageVersion, bool writeGeneratedNotice = true);
public abstract string Compile(Version? languageVersion, XrpcServices services = XrpcServices.Both, bool writeGeneratedNotice = true);

/// <summary>
/// Write auxiliary files to an output directory path.
Expand Down
4 changes: 3 additions & 1 deletion Core/Generators/CPlusPlus/CPlusPlusGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ private static string EscapeStringLiteral(string value)
/// Generate code for a Bebop schema.
/// </summary>
/// <returns>The generated code.</returns>
public override string Compile(Version? languageVersion, bool writeGeneratedNotice = true)
public override string Compile(Version? languageVersion, XrpcServices services = XrpcServices.Both, bool writeGeneratedNotice = true)
{
var builder = new StringBuilder();
if (writeGeneratedNotice)
Expand Down Expand Up @@ -500,6 +500,8 @@ public override string Compile(Version? languageVersion, bool writeGeneratedNoti
builder.AppendLine($"const {TypeName(cd.Value.Type)} {cd.Name} = {EmitLiteral(cd.Value)};");
builder.AppendLine("");
break;
case ServiceDefinition:
break;
default:
throw new InvalidOperationException($"unsupported definition {definition}");
}
Expand Down
6 changes: 5 additions & 1 deletion Core/Generators/CSharp/CSharpGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public CSharpGenerator(BebopSchema schema) : base(schema)
}


public override string Compile(Version? languageVersion, bool writeGeneratedNotice = true)
public override string Compile(Version? languageVersion, XrpcServices services = XrpcServices.Both, bool writeGeneratedNotice = true)
{
if (languageVersion is not null)
{
Expand Down Expand Up @@ -69,6 +69,10 @@ public override string Compile(Version? languageVersion, bool writeGeneratedNoti
{
continue;
}
if (definition is ServiceDefinition)
{
continue;
}
if (!string.IsNullOrWhiteSpace(definition.Documentation))
{
builder.AppendLine(FormatDocumentation(definition.Documentation, 0));
Expand Down
4 changes: 3 additions & 1 deletion Core/Generators/Dart/DartGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ private static string EscapeStringLiteral(string value)
/// Generate code for a Bebop schema.
/// </summary>
/// <returns>The generated code.</returns>
public override string Compile(Version? languageVersion, bool writeGeneratedNotice = true)
public override string Compile(Version? languageVersion, XrpcServices services = XrpcServices.Both, bool writeGeneratedNotice = true)
{
var builder = new StringBuilder();
builder.AppendLine("import 'dart:typed_data';");
Expand Down Expand Up @@ -394,6 +394,8 @@ public override string Compile(Version? languageVersion, bool writeGeneratedNoti
builder.AppendLine($"final {TypeName(cd.Value.Type)} {cd.Name} = {EmitLiteral(cd.Value)};");
builder.AppendLine("");
break;
case ServiceDefinition:
break;
default:
throw new InvalidOperationException($"unsupported definition {definition}");
}
Expand Down
4 changes: 3 additions & 1 deletion Core/Generators/Rust/RustGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class RustGenerator : BaseGenerator

public RustGenerator(BebopSchema schema) : base(schema) { }

public override string Compile(Version? languageVersion, bool writeGeneratedNotice = true)
public override string Compile(Version? languageVersion, XrpcServices services = XrpcServices.Both, bool writeGeneratedNotice = true)
{
// the main scope which is where we write the const definitions and the borrowed types (as these are the
// primary way to use bebop in Rust)
Expand Down Expand Up @@ -107,6 +107,8 @@ public override string Compile(Version? languageVersion, bool writeGeneratedNoti
WriteUnionDefinition(mainBuilder, ud, CodeRegion.Main);
WriteUnionDefinition(ownedBuilder, ud, CodeRegion.Owned);
break;
case ServiceDefinition:
break;
default:
throw new InvalidOperationException($"unsupported definition {definition.GetType()}");
}
Expand Down
28 changes: 28 additions & 0 deletions Core/Generators/ServiceGeneratorFlags.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
namespace Core.Generators
{
/// <summary>
/// An enum that defines which parts of a service are generated
/// </summary>
public enum XrpcServices
{
/// <summary>
/// Indicates no service code should be generated
/// </summary>
None = 0,
/// <summary>
/// Indicates only client service code should be generated
/// </summary>
Client = 1,
/// <summary>
/// Indicates only server service code should be generated
/// </summary>
Server = 2,
/// <summary>
/// Indicates both client and server service code should be generated
/// </summary>
Both = 3

}
}

0 comments on commit e212f02

Please sign in to comment.