Skip to content

Commit be03602

Browse files
committed
Fix build errors: add GetShape methods to SmithyModel and KebabCase to NamingUtils
1 parent c1201cc commit be03602

File tree

4 files changed

+50
-13
lines changed

4 files changed

+50
-13
lines changed

Smithy.CSharpGenerator/Generators/ResourceGenerator.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,33 +26,31 @@ public void GenerateResource(ResourceShape resourceShape, SmithyModel model, Str
2626
// Generate resource class
2727
inner.AppendLine($"public class {NamingUtils.PascalCase(resourceShape.Id)}");
2828
inner.AppendLine("{");
29-
30-
// Generate identifiers
29+
// Generate identifiers
3130
if (resourceShape.Identifiers != null)
3231
{
3332
inner.AppendLine(" // Resource identifiers");
3433
foreach (var identifier in resourceShape.Identifiers)
3534
{
3635
// Get target shape
37-
var targetShape = model.GetShape(identifier.Target);
36+
var targetShape = model.GetShape(identifier.Value);
3837
string typeName = _typeMapper.MapToType(targetShape);
3938

40-
inner.AppendLine($" public {typeName} {NamingUtils.PascalCase(identifier.Name)} {{ get; set; }}");
39+
inner.AppendLine($" public {typeName} {NamingUtils.PascalCase(identifier.Key)} {{ get; set; }}");
4140
}
4241
inner.AppendLine();
4342
}
44-
45-
// Generate properties
43+
// Generate properties
4644
if (resourceShape.Properties != null)
4745
{
4846
inner.AppendLine(" // Resource properties");
4947
foreach (var property in resourceShape.Properties)
5048
{
5149
// Get target shape
52-
var targetShape = model.GetShape(property.Target);
50+
var targetShape = model.GetShape(property.Value);
5351
string typeName = _typeMapper.MapToType(targetShape);
5452

55-
inner.AppendLine($" public {typeName} {NamingUtils.PascalCase(property.Name)} {{ get; set; }}");
53+
inner.AppendLine($" public {typeName} {NamingUtils.PascalCase(property.Key)} {{ get; set; }}");
5654
}
5755
inner.AppendLine();
5856
}

Smithy.CSharpGenerator/Utils/NamingUtils.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,29 @@ public static class NamingUtils
1616
sb.Append(part.Substring(1));
1717
}
1818
return sb.ToString();
19-
}
20-
21-
public static string KebabCase(string id)
19+
} public static string KebabCase(string id)
2220
{
2321
if (string.IsNullOrEmpty(id)) return id;
22+
23+
// Remove any namespace or version prefix (e.g., com.example#MyService -> MyService)
24+
int hashIndex = id.LastIndexOf('#');
25+
if (hashIndex >= 0 && hashIndex < id.Length - 1)
26+
{
27+
id = id.Substring(hashIndex + 1);
28+
}
29+
2430
var sb = new StringBuilder();
2531

2632
for (int i = 0; i < id.Length; i++)
2733
{
28-
if (i > 0 && char.IsUpper(id[i]))
34+
char c = id[i];
35+
36+
if (i > 0 && char.IsUpper(c))
2937
{
3038
sb.Append('-');
3139
}
32-
sb.Append(char.ToLower(id[i]));
40+
41+
sb.Append(char.ToLower(c));
3342
}
3443

3544
return sb.ToString();

Smithy.Cli/Program.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using System.Collections.Generic;
33
using Smithy.Model;
44
using Smithy.CSharpGenerator;
5+
using Smithy.Model.Parsers;
6+
using Smithy.Model.Validation;
57

68
// See https://aka.ms/new-console-template for more information
79
Console.WriteLine("Hello, World!");

Smithy.Model/SmithyModel.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System.Collections.Generic;
22

3+
using System.Linq;
4+
35
namespace Smithy.Model
46
{
57
public class SmithyModel
@@ -10,6 +12,32 @@ public class SmithyModel
1012
public Dictionary<string, object> Metadata { get; set; } = new();
1113
public List<string> Uses { get; set; } = new();
1214
public List<string> Applies { get; set; } = new();
15+
/// <summary>
16+
/// Get a shape by its ID from the model
17+
/// </summary>
18+
/// <param name="shapeId">The ID of the shape to retrieve</param>
19+
/// <returns>The shape with the specified ID, or null if not found</returns>
20+
public Shape? GetShape(string? shapeId)
21+
{
22+
if (string.IsNullOrEmpty(shapeId))
23+
return null;
24+
25+
return Shapes.FirstOrDefault(s => s.Id == shapeId);
26+
}
27+
28+
/// <summary>
29+
/// Get a shape by its ID from the model
30+
/// </summary>
31+
/// <typeparam name="T">The type of shape to retrieve</typeparam>
32+
/// <param name="shapeId">The ID of the shape to retrieve</param>
33+
/// <returns>The shape with the specified ID, or null if not found</returns>
34+
public T? GetShape<T>(string? shapeId) where T : Shape
35+
{
36+
if (string.IsNullOrEmpty(shapeId))
37+
return null;
38+
39+
return Shapes.OfType<T>().FirstOrDefault(s => s.Id == shapeId);
40+
}
1341
}
1442

1543
public interface ISmithyModelParser

0 commit comments

Comments
 (0)