|
8 | 8 | "regexp" |
9 | 9 | "strings" |
10 | 10 | "unicode/utf8" |
| 11 | + |
| 12 | + "github.com/duke-git/lancet/v2/strutil" |
| 13 | + "github.com/gobuffalo/flect" |
11 | 14 | ) |
12 | 15 |
|
13 | 16 | func applyUpdates(src string, kind string, grpcServiceName string, importPath string) (string, bool, error) { |
@@ -86,13 +89,16 @@ func addImportProto(src string, importPath string) (string, bool, error) { |
86 | 89 | } |
87 | 90 |
|
88 | 91 | func addRPCsToAPIServer(src string, kind string, grpcServiceName string) (string, bool, error) { |
| 92 | + pluralKind := flect.Pluralize(strutil.UpperFirst(strutil.CamelCase(kind))) |
| 93 | + |
89 | 94 | var ( |
90 | | - reServiceOpen = regexp.MustCompile(fmt.Sprintf(`(?m)^[ \t]*service[ \t]+%s[ \t]*\{`, grpcServiceName)) |
91 | | - reCreate = regexp.MustCompile(fmt.Sprintf(`(?m)^[ \t]*rpc[ \t]+Create%s[ \t]*\(`, kind)) |
92 | | - reUpdate = regexp.MustCompile(fmt.Sprintf(`(?m)^[ \t]*rpc[ \t]+Update%s[ \t]*\(`, kind)) |
93 | | - reDelete = regexp.MustCompile(fmt.Sprintf(`(?m)^[ \t]*rpc[ \t]+Delete%s[ \t]*\(`, kind)) |
94 | | - reGet = regexp.MustCompile(fmt.Sprintf(`(?m)^[ \t]*rpc[ \t]+Get%s[ \t]*\(`, kind)) |
95 | | - reList = regexp.MustCompile(fmt.Sprintf(`(?m)^[ \t]*rpc[ \t]+List%s[ \t]*\(`, kind)) |
| 95 | + reServiceOpen = regexp.MustCompile(fmt.Sprintf(`(?m)^[ \t]*service[ \t]+%s[ \t]*\{`, grpcServiceName)) |
| 96 | + reCreate = regexp.MustCompile(fmt.Sprintf(`(?m)^[ \t]*rpc[ \t]+Create%s[ \t]*\(`, kind)) |
| 97 | + reUpdate = regexp.MustCompile(fmt.Sprintf(`(?m)^[ \t]*rpc[ \t]+Update%s[ \t]*\(`, kind)) |
| 98 | + reDelete = regexp.MustCompile(fmt.Sprintf(`(?m)^[ \t]*rpc[ \t]+Delete%s[ \t]*\(`, kind)) |
| 99 | + reDeleteCollection = regexp.MustCompile(fmt.Sprintf(`(?m)^[ \t]*rpc[ \t]+Delete%s[ \t]*\(`, pluralKind)) |
| 100 | + reGet = regexp.MustCompile(fmt.Sprintf(`(?m)^[ \t]*rpc[ \t]+Get%s[ \t]*\(`, kind)) |
| 101 | + reList = regexp.MustCompile(fmt.Sprintf(`(?m)^[ \t]*rpc[ \t]+List%s[ \t]*\(`, kind)) |
96 | 102 | ) |
97 | 103 |
|
98 | 104 | loc := reServiceOpen.FindStringIndex(src) |
@@ -121,6 +127,7 @@ func addRPCsToAPIServer(src string, kind string, grpcServiceName string) (string |
121 | 127 | hasCreate := reCreate.FindStringIndex(body) != nil |
122 | 128 | hasUpdate := reUpdate.FindStringIndex(body) != nil |
123 | 129 | hasDelete := reDelete.FindStringIndex(body) != nil |
| 130 | + hasDeleteCollection := reDeleteCollection.FindStringIndex(body) != nil |
124 | 131 | hasGet := reGet.FindStringIndex(body) != nil |
125 | 132 | hasList := reList.FindStringIndex(body) != nil |
126 | 133 |
|
@@ -154,6 +161,11 @@ func addRPCsToAPIServer(src string, kind string, grpcServiceName string) (string |
154 | 161 | b.WriteString(fmt.Sprintf("rpc Delete%s(Delete%sRequest) returns (Delete%sResponse);\n", kind, kind, kind)) |
155 | 162 | needUpdate = true |
156 | 163 | } |
| 164 | + if !hasDeleteCollection { |
| 165 | + b.WriteString(insIndent) |
| 166 | + b.WriteString(fmt.Sprintf("rpc Delete%s(Delete%sRequest) returns (Delete%sResponse);\n", pluralKind, pluralKind, pluralKind)) |
| 167 | + needUpdate = true |
| 168 | + } |
157 | 169 | if !hasGet { |
158 | 170 | b.WriteString(insIndent) |
159 | 171 | b.WriteString(fmt.Sprintf("rpc Get%s(Get%sRequest) returns (Get%sResponse);\n", kind, kind, kind)) |
|
0 commit comments