Skip to content

Commit 1d8ad07

Browse files
authored
Merge pull request #11 from moorestech/feature/fix
requiredのコードの自動生成機能追加
2 parents 3951229 + 921ba55 commit 1d8ad07

7 files changed

Lines changed: 85 additions & 57 deletions

File tree

.claude/settings.local.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
{
2-
"enableAllProjectMcpServers": false,
32
"permissions": {
43
"allow": [
5-
"Bash(find:*)"
4+
"Bash(find:*)",
5+
"Bash(dotnet test:*)"
66
]
7-
}
7+
},
8+
"enableAllProjectMcpServers": false
89
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#if ENABLE_COMMAND_FORGE_GENERATOR
2+
namespace CommandForgeGenerator.Command
3+
{
4+
public partial class TextCommand : ICommandForgeCommand
5+
{
6+
public const string Type = "text";
7+
public readonly CommandId CommandId;
8+
9+
public readonly string Character;
10+
public readonly string? Body;
11+
public readonly string? VoiceId;
12+
13+
14+
public static TextCommand Create(int commandId, global::Newtonsoft.Json.Linq.JToken json)
15+
{
16+
17+
var Character = (string)json["character"];
18+
var Body = json["body"] == null ? null : (string)json["body"];
19+
var VoiceId = json["voiceId"] == null ? null : (string)json["voiceId"];
20+
21+
22+
return new TextCommand(commandId, Character, Body, VoiceId);
23+
}
24+
25+
public TextCommand(int commandId, string Character, string? Body, string? VoiceId)
26+
{
27+
CommandId = (CommandId)commandId;
28+
29+
this.Character = Character;
30+
this.Body = Body;
31+
this.VoiceId = VoiceId;
32+
33+
}
34+
}
35+
}
36+
#endif
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace CommandForgeGenerator.Command;
2+
3+
// 他の生成結果サンプルコマンドをコンパイルエラーにしないためのコード
4+
5+
public interface ICommandForgeCommand { }
6+
public enum CommandId{}
Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,9 @@
1+
using System.IO;
2+
13
namespace CommandForgeGenerator.Tests;
24

35
public class GenerateTestCode
46
{
5-
public const string TextCommand = """
6-
#if ENABLE_COMMAND_FORGE_GENERATOR
7-
namespace CommandForgeGenerator.Command
8-
{
9-
public partial class TextCommand : ICommandForgeCommand
10-
{
11-
public const string Type = "text";
12-
public readonly CommandId CommandId;
13-
14-
public readonly string Character;
15-
public readonly string Body;
16-
17-
18-
public static TextCommand Create(int commandId, global::Newtonsoft.Json.Linq.JToken json)
19-
{
20-
21-
var Character = (string)json["character"];
22-
var Body = (string)json["body"];
23-
24-
25-
return new TextCommand(commandId, Character, Body);
26-
}
27-
28-
public TextCommand(int commandId, string Character, string Body)
29-
{
30-
CommandId = (CommandId)commandId;
31-
32-
this.Character = Character;
33-
this.Body = Body;
34-
35-
}
36-
}
37-
}
38-
#endif
39-
""";
7+
// プロジェクトファイルに存在する GenerateSampleTextCommand.cs を取得する
8+
public static string TextCommandStr => File.ReadAllText("../../../GenerateSampleTextCommand.cs");
409
}

CommandForgeGenerator.Tests/Test.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public void GenerateTest()
6565
var codeFiles = CodeGenerator.Generate(commandsSchema);
6666

6767
Assert.Equal(16, codeFiles.Count);
68-
Assert.Equal(GenerateTestCode.TextCommand, codeFiles.FirstOrDefault(c => c.FileName == "TextCommand.g.cs").Code);
68+
Assert.Equal(GenerateTestCode.TextCommandStr, codeFiles.FirstOrDefault(c => c.FileName == "TextCommand.g.cs").Code);
6969

7070
#region Internal
7171

@@ -86,6 +86,10 @@ string GetSampleYaml()
8686
body:
8787
type: string
8888
multiline: true
89+
voiceId:
90+
type: string
91+
multiline: true
92+
required: false
8993
9094
- id: emote
9195
label: エモート

CommandForgeGenerator/CodeGenerate/CodeGenerator.cs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -156,39 +156,42 @@ private static string GenerateCreateMethodTempVariables(List<CommandProperty> co
156156
else
157157
{
158158
// nullable の場合はnullチェックを追加
159-
properties.AppendLine($"var {property.CodeProperty}Token = json[\"{property.Name}\"];");
160-
161-
if (property.Type is CommandPropertyType.CommandId)
159+
if (property.Type is CommandPropertyType.String)
160+
{
161+
properties.AppendLine($"var {property.CodeProperty} = json[\"{property.Name}\"] == null ? null : (string)json[\"{property.Name}\"];");
162+
}
163+
else if (property.Type is CommandPropertyType.CommandId)
162164
{
163-
properties.AppendLine($"{type} {property.CodeProperty} = {property.CodeProperty}Token?.Type == global::Newtonsoft.Json.Linq.JTokenType.Null ? null : (CommandId?)((int){property.CodeProperty}Token);");
165+
properties.AppendLine($"var {property.CodeProperty} = json[\"{property.Name}\"] == null ? null : (CommandId?)((int)json[\"{property.Name}\"]);");
164166
}
165167
else if (property.Type is CommandPropertyType.Vector2)
166168
{
167-
properties.AppendLine($"{type} {property.CodeProperty} = {property.CodeProperty}Token?.Type == global::Newtonsoft.Json.Linq.JTokenType.Null ? null : new global::UnityEngine.Vector2((float){property.CodeProperty}Token[0], (float){property.CodeProperty}Token[1]);");
169+
properties.AppendLine($"var {property.CodeProperty}Array = json[\"{property.Name}\"];");
170+
properties.AppendLine($"var {property.CodeProperty} = {property.CodeProperty}Array == null ? null : new global::UnityEngine.Vector2((float){property.CodeProperty}Array[0], (float){property.CodeProperty}Array[1]);");
168171
}
169172
else if (property.Type is CommandPropertyType.Vector3)
170173
{
171-
properties.AppendLine($"{type} {property.CodeProperty} = {property.CodeProperty}Token?.Type == global::Newtonsoft.Json.Linq.JTokenType.Null ? null : new global::UnityEngine.Vector3((float){property.CodeProperty}Token[0], (float){property.CodeProperty}Token[1], (float){property.CodeProperty}Token[2]);");
174+
properties.AppendLine($"var {property.CodeProperty}Array = json[\"{property.Name}\"];");
175+
properties.AppendLine($"var {property.CodeProperty} = {property.CodeProperty}Array == null ? null : new global::UnityEngine.Vector3((float){property.CodeProperty}Array[0], (float){property.CodeProperty}Array[1], (float){property.CodeProperty}Array[2]);");
172176
}
173177
else if (property.Type is CommandPropertyType.Vector4)
174178
{
175-
properties.AppendLine($"{type} {property.CodeProperty} = {property.CodeProperty}Token?.Type == global::Newtonsoft.Json.Linq.JTokenType.Null ? null : new global::UnityEngine.Vector4((float){property.CodeProperty}Token[0], (float){property.CodeProperty}Token[1], (float){property.CodeProperty}Token[2], (float){property.CodeProperty}Token[3]);");
179+
properties.AppendLine($"var {property.CodeProperty}Array = json[\"{property.Name}\"];");
180+
properties.AppendLine($"var {property.CodeProperty} = {property.CodeProperty}Array == null ? null : new global::UnityEngine.Vector4((float){property.CodeProperty}Array[0], (float){property.CodeProperty}Array[1], (float){property.CodeProperty}Array[2], (float){property.CodeProperty}Array[3]);");
176181
}
177182
else if (property.Type is CommandPropertyType.Vector2Int)
178183
{
179-
properties.AppendLine($"{type} {property.CodeProperty} = {property.CodeProperty}Token?.Type == global::Newtonsoft.Json.Linq.JTokenType.Null ? null : new global::UnityEngine.Vector2Int((int){property.CodeProperty}Token[0], (int){property.CodeProperty}Token[1]);");
184+
properties.AppendLine($"var {property.CodeProperty}Array = json[\"{property.Name}\"];");
185+
properties.AppendLine($"var {property.CodeProperty} = {property.CodeProperty}Array == null ? null : new global::UnityEngine.Vector2Int((int){property.CodeProperty}Array[0], (int){property.CodeProperty}Array[1]);");
180186
}
181187
else if (property.Type is CommandPropertyType.Vector3Int)
182188
{
183-
properties.AppendLine($"{type} {property.CodeProperty} = {property.CodeProperty}Token?.Type == global::Newtonsoft.Json.Linq.JTokenType.Null ? null : new global::UnityEngine.Vector3Int((int){property.CodeProperty}Token[0], (int){property.CodeProperty}Token[1], (int){property.CodeProperty}Token[2]);");
184-
}
185-
else if (property.Type is CommandPropertyType.String)
186-
{
187-
properties.AppendLine($"{type} {property.CodeProperty} = {property.CodeProperty}Token?.Type == global::Newtonsoft.Json.Linq.JTokenType.Null ? null : (string?){property.CodeProperty}Token;");
189+
properties.AppendLine($"var {property.CodeProperty}Array = json[\"{property.Name}\"];");
190+
properties.AppendLine($"var {property.CodeProperty} = {property.CodeProperty}Array == null ? null : new global::UnityEngine.Vector3Int((int){property.CodeProperty}Array[0], (int){property.CodeProperty}Array[1], (int){property.CodeProperty}Array[2]);");
188191
}
189192
else
190193
{
191-
properties.AppendLine($"{type} {property.CodeProperty} = {property.CodeProperty}Token?.Type == global::Newtonsoft.Json.Linq.JTokenType.Null ? null : ({type}){property.CodeProperty}Token;");
194+
properties.AppendLine($"var {property.CodeProperty} = json[\"{property.Name}\"] == null ? null : ({type})json[\"{property.Name}\"];");
192195
}
193196
}
194197
}

CommandForgeGenerator/Semantic/CommandSemanticsLoader.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,20 @@ CommandsSemantics ParseCommandsSchema(JsonObject root)
9090
_ => throw new Exception($"未知の property type \"{typeStr}\"")
9191
};
9292

93-
// required フィールドを読み取る
94-
var isRequired = true;
95-
if (propObj.Nodes.ContainsKey("required") && propObj["required"] is JsonBoolean requiredNode)
93+
// required フィールドを読み取る(デフォルトはfalse)
94+
var isRequired = false;
95+
if (propObj.Nodes.ContainsKey("required"))
9696
{
97-
isRequired = requiredNode.Literal;
97+
var requiredVal = propObj["required"];
98+
if (requiredVal is JsonBoolean requiredBool)
99+
{
100+
isRequired = requiredBool.Literal;
101+
}
102+
else if (requiredVal is JsonString requiredStr)
103+
{
104+
// YAMLが文字列としてbooleanを返す場合がある
105+
isRequired = requiredStr.Literal.ToLowerInvariant() == "true";
106+
}
98107
}
99108

100109
properties.Add(new CommandProperty(mappedType, propName, isRequired));

0 commit comments

Comments
 (0)