File tree 1 file changed +34
-1
lines changed
1 file changed +34
-1
lines changed Original file line number Diff line number Diff line change @@ -262,6 +262,34 @@ func (n *NamespaceGenerator) cc() *CFileGenerator {
262
262
return n .c .c
263
263
}
264
264
265
+ // Rename renames a file from src to dst. If src does not exist, then it will
266
+ // return an error.
267
+ func (n * NamespaceGenerator ) Rename (src , dst string ) error {
268
+ srcFile , ok := n .Files [src ]
269
+ if ! ok {
270
+ return fmt .Errorf ("file %s does not exist" , src )
271
+ }
272
+
273
+ _ , ok = n .Files [dst ]
274
+ if ok {
275
+ return fmt .Errorf ("file %s already exists" , dst )
276
+ }
277
+
278
+ switch f := srcFile .(type ) {
279
+ case * GoFileGenerator :
280
+ f .name = dst
281
+ case * CFileGenerator :
282
+ f .name = dst
283
+ default :
284
+ return fmt .Errorf ("cannot rename file of unsupported type %T" , f )
285
+ }
286
+
287
+ delete (n .Files , src )
288
+ n .Files [dst ] = srcFile
289
+
290
+ return nil
291
+ }
292
+
265
293
// Generate generates everything in the current namespace into files. The
266
294
// returned map maps the filename to the raw file content.
267
295
func (n * NamespaceGenerator ) Generate () (map [string ][]byte , error ) {
@@ -359,7 +387,12 @@ func (n *NamespaceGenerator) Generate() (map[string][]byte, error) {
359
387
}
360
388
}
361
389
362
- for _ , file := range n .Files {
390
+ for name , file := range n .Files {
391
+ if name != file .Name () {
392
+ panic (fmt .Errorf (
393
+ "file name mismatch: %s != %s (see NamespaceGenerator.Rename)" ,
394
+ name , file .Name ()))
395
+ }
363
396
if file .IsEmpty () {
364
397
continue
365
398
}
You can’t perform that action at this time.
0 commit comments