Skip to content

Commit 3f712e6

Browse files
committed
feat: add option for disabling generation of top level resources
1 parent 6203b49 commit 3f712e6

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ filename Name of the file to generate the code to.
3939
insertion_point If non-empty, indicates that the named file should already exist,
4040
and the content here is to be inserted into that file at a defined
4141
insertion point.
42+
43+
skip_file_resource_definitions
44+
If set to true, resource names will not be generated for resource definitions
45+
in the file scope. Default: false.
4246
```
4347
4448
---

internal/plugin/generate.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ func Generate(request *pluginpb.CodeGeneratorRequest) (*pluginpb.CodeGeneratorRe
4242
if _, ok := generate[descriptor.ParentFile]; !ok {
4343
return true
4444
}
45+
if opts.SkipFileResourceDefinitions && !isMessageResourceDescriptor(descriptor) {
46+
return true
47+
}
4548
dir := path.Dir(descriptor.ParentFile)
4649
packageResources[dir] = append(packageResources[dir], descriptor)
4750
return true
@@ -67,3 +70,7 @@ func Generate(request *pluginpb.CodeGeneratorRequest) (*pluginpb.CodeGeneratorRe
6770

6871
return &response, nil
6972
}
73+
74+
func isMessageResourceDescriptor(r *aipreflect.ResourceDescriptor) bool {
75+
return r.Message != ""
76+
}

internal/plugin/options.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ type Options struct {
1414
// use.
1515
// Defaults to `""` (ie. no insertion point)
1616
InsertionPoint string
17+
18+
// SkipFileResourceDefinitions disables generation of resource names
19+
// declared in the file scope.
20+
// Defaults to false.
21+
SkipFileResourceDefinitions bool
1722
}
1823

1924
func defaultOptions() Options {
@@ -46,9 +51,25 @@ func (o *Options) Unmarshal(s *string) error {
4651
o.InsertionPoint = value
4752
case "filename":
4853
o.Filename = value
54+
case "skip_file_resource_definitions":
55+
b, err := unmarshalBool(value)
56+
if err != nil {
57+
return fmt.Errorf("unmarshal skip_file_resource_definitions: %w", err)
58+
}
59+
o.SkipFileResourceDefinitions = b
4960
default:
5061
return fmt.Errorf("unknown option [%s]", opt)
5162
}
5263
}
5364
return nil
5465
}
66+
67+
func unmarshalBool(s string) (bool, error) {
68+
switch s {
69+
case "false":
70+
return false, nil
71+
case "true":
72+
return true, nil
73+
}
74+
return false, fmt.Errorf("invalid bool value '%s', expected one of 'true' or 'false'", s)
75+
}

0 commit comments

Comments
 (0)