Skip to content

Commit

Permalink
Add WithId method to Definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
LykaiosNZ committed Apr 26, 2024
1 parent c92d3b0 commit 6a32e18
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 13 deletions.
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ SpiceWeaver is a .NET Source Generator for [SpiceDB](https://github.com/authzed/
Add SpiceWeaver to your `.csproj` file:

```xml

<ItemGroup>
<PackageReference Include="SpiceWeaver" Version="0.1.0" PrivateAssets="all"/>
</ItemGroup>
Expand All @@ -21,7 +20,6 @@ installed.
Add your SpiceDB schema to your `.csproj` file as an Additional File and flag it as a schema file:

```xml

<ItemGroup>
<AdditionalFiles Include="schema.zed" SpiceWeaver_SchemaFile="true"/>
</ItemGroup>
Expand All @@ -33,7 +31,6 @@ If your schema has already been converted to a json file using **spice2json**, y
Generator using the `IsJson`file option:

```xml

<ItemGroup>
<AdditionalFiles Include="schema.json" SpiceWeaver_SchemaFile="true" SpiceWeaver_IsJson="true"/>
</ItemGroup>
Expand Down Expand Up @@ -69,11 +66,13 @@ namespace SpiceWeaver
public static class User
{
public const string Name = "user";
public static string WithId(string id) => $"user:{id}";
}

public static class Document
{
public const string Name = "document";
public static string WithId(string id) => $"document:{id}";
public static class Relations
{
public const string Viewer = "viewer";
Expand Down Expand Up @@ -105,7 +104,6 @@ Global options are set in the `.csproj` file directly in a `PropertyGroup` and t
schema files

```xml

<PropertyGroup>
<SpiceWeaver_Spice2JsonPath>path/to/spice2json</SpiceWeaver_Spice2JsonPath>
</PropertyGroup>
Expand All @@ -123,7 +121,6 @@ schema files
File options are per-file and are set on individual `AdditionalFiles` items

```xml

<ItemGroup>
<AdditionalFiles Include="schema.zed" SpiceWeaver_SchemaFile="true" SpiceWeaver_Namespace="MyNamespace"/>
</ItemGroup>
Expand Down
45 changes: 37 additions & 8 deletions src/SpiceWeaver/CodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,12 @@ public static string Generate(string @namespace, string className, string schema
try
{
var schema = JsonConvert.DeserializeObject<Schema>(schemaJson);

Debug.Assert(schema is not null);

return Generate(@namespace, className, schema!);
}
catch (JsonException e)
{
throw new SpiceWeaverException("Error deserializing schema from json", e);
}
catch (JsonException e) { throw new SpiceWeaverException("Error deserializing schema from json", e); }
}

public static string Generate(string @namespace, string className, Schema schema)
Expand All @@ -53,7 +50,7 @@ public static void Generate(string @namespace, string className, Schema schema,
{
throw new ArgumentException("Cannot be null or whitespace", nameof(className));
}

var syntax = GenerateSyntax(@namespace, className, schema);

syntax.NormalizeWhitespace().WriteTo(writer);
Expand All @@ -75,11 +72,13 @@ public static NamespaceDeclarationSyntax GenerateSyntax(string @namespace, strin
private static ClassDeclarationSyntax CreateDefinition(Definition definition)
{
var name = ConstStringField("Name", definition.Name);
var withIdMethod = WithIdMethod(definition.Name);

var relations = definition.Relations.Select(RelationField).ToArray();
var permissions = definition.Permissions.Select(PermissionField).ToArray();

var definitionClass = StaticClass(definition.Name.ToPascalCase())
.AddMembers(name);
.AddMembers(name, withIdMethod);

if (relations.Any())
{
Expand Down Expand Up @@ -113,15 +112,45 @@ private static MemberDeclarationSyntax ConstStringField(string name, string valu
.AddModifiers(Public, Const);

private static VariableDeclarationSyntax InitializedStringVariable(string name, string value) =>
VariableDeclaration(PredefinedType(Token(SyntaxKind.StringKeyword)))
VariableDeclaration(StringType)
.AddVariables(
VariableDeclarator(Identifier(name))
.WithInitializer(
EqualsValueClause(
LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(value))
)));

private static MethodDeclarationSyntax WithIdMethod(string resourceName)
{
const string idParameterIdentifier = "id";

return MethodDeclaration(StringType, Identifier("WithId"))
.AddModifiers(Public, Static)
.AddParameterListParameters(StringParameter(idParameterIdentifier))
.WithExpressionBody(
ArrowExpressionClause(ResourceIdInterpolationExpression(resourceName, idParameterIdentifier))
)
.WithSemicolonToken(SemiColon);
}

private static InterpolatedStringExpressionSyntax ResourceIdInterpolationExpression(string resourceName,
string idIdentifier) =>
InterpolatedStringExpression(Token(SyntaxKind.InterpolatedStringStartToken))
.AddContents(
InterpolatedStringText(
Token(
TriviaList(), SyntaxKind.InterpolatedStringTextToken, $"{resourceName}:", "", TriviaList()
)
), Interpolation(IdentifierName(idIdentifier))
);

private static ParameterSyntax StringParameter(string identifier) =>
Parameter(Identifier(identifier)).WithType(StringType);

private static PredefinedTypeSyntax StringType => PredefinedType(Token(SyntaxKind.StringKeyword));

private static SyntaxToken Const => Token(SyntaxKind.ConstKeyword);
private static SyntaxToken Public => Token(SyntaxKind.PublicKeyword);
private static SyntaxToken Static => Token(SyntaxKind.StaticKeyword);
private static SyntaxToken SemiColon => Token(SyntaxKind.SemicolonToken);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
public static class User
{
public const string Name = "user";
public static string WithId(string id) => $"user:{id}";
}

public static class Document
{
public const string Name = "document";
public static string WithId(string id) => $"document:{id}";
public static class Relations
{
public const string Viewer = "viewer";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
public static class User
{
public const string Name = "user";
public static string WithId(string id) => $"user:{id}";
}

public static class Document
{
public const string Name = "document";
public static string WithId(string id) => $"document:{id}";
public static class Relations
{
public const string Viewer = "viewer";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
public static class User
{
public const string Name = "user";
public static string WithId(string id) => $"user:{id}";
}

public static class Document
{
public const string Name = "document";
public static string WithId(string id) => $"document:{id}";
public static class Relations
{
public const string Viewer = "viewer";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
public static class User
{
public const string Name = "user";
public static string WithId(string id) => $"user:{id}";
}

public static class Document
{
public const string Name = "document";
public static string WithId(string id) => $"document:{id}";
public static class Relations
{
public const string Viewer = "viewer";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
public static class User
{
public const string Name = "user";
public static string WithId(string id) => $"user:{id}";
}

public static class Document
{
public const string Name = "document";
public static string WithId(string id) => $"document:{id}";
public static class Relations
{
public const string Viewer = "viewer";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
public static class User
{
public const string Name = "user";
public static string WithId(string id) => $"user:{id}";
}

public static class Document
{
public const string Name = "document";
public static string WithId(string id) => $"document:{id}";
public static class Relations
{
public const string Viewer = "viewer";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
public static class User
{
public const string Name = "user";
public static string WithId(string id) => $"user:{id}";
}

public static class Document
{
public const string Name = "document";
public static string WithId(string id) => $"document:{id}";
public static class Relations
{
public const string Viewer = "viewer";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
public static class User
{
public const string Name = "user";
public static string WithId(string id) => $"user:{id}";
}

public static class Document
{
public const string Name = "document";
public static string WithId(string id) => $"document:{id}";
public static class Relations
{
public const string Viewer = "viewer";
Expand Down

0 comments on commit 6a32e18

Please sign in to comment.