Skip to content

Commit

Permalink
feat: add option for disabling generation of top level resources
Browse files Browse the repository at this point in the history
  • Loading branch information
ericwenn committed Aug 9, 2022
1 parent 6203b49 commit 3f712e6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ filename Name of the file to generate the code to.
insertion_point If non-empty, indicates that the named file should already exist,
and the content here is to be inserted into that file at a defined
insertion point.

skip_file_resource_definitions
If set to true, resource names will not be generated for resource definitions
in the file scope. Default: false.
```
---
Expand Down
7 changes: 7 additions & 0 deletions internal/plugin/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ func Generate(request *pluginpb.CodeGeneratorRequest) (*pluginpb.CodeGeneratorRe
if _, ok := generate[descriptor.ParentFile]; !ok {
return true
}
if opts.SkipFileResourceDefinitions && !isMessageResourceDescriptor(descriptor) {
return true
}
dir := path.Dir(descriptor.ParentFile)
packageResources[dir] = append(packageResources[dir], descriptor)
return true
Expand All @@ -67,3 +70,7 @@ func Generate(request *pluginpb.CodeGeneratorRequest) (*pluginpb.CodeGeneratorRe

return &response, nil
}

func isMessageResourceDescriptor(r *aipreflect.ResourceDescriptor) bool {
return r.Message != ""
}
21 changes: 21 additions & 0 deletions internal/plugin/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ type Options struct {
// use.
// Defaults to `""` (ie. no insertion point)
InsertionPoint string

// SkipFileResourceDefinitions disables generation of resource names
// declared in the file scope.
// Defaults to false.
SkipFileResourceDefinitions bool
}

func defaultOptions() Options {
Expand Down Expand Up @@ -46,9 +51,25 @@ func (o *Options) Unmarshal(s *string) error {
o.InsertionPoint = value
case "filename":
o.Filename = value
case "skip_file_resource_definitions":
b, err := unmarshalBool(value)
if err != nil {
return fmt.Errorf("unmarshal skip_file_resource_definitions: %w", err)
}
o.SkipFileResourceDefinitions = b
default:
return fmt.Errorf("unknown option [%s]", opt)
}
}
return nil
}

func unmarshalBool(s string) (bool, error) {
switch s {
case "false":
return false, nil
case "true":
return true, nil
}
return false, fmt.Errorf("invalid bool value '%s', expected one of 'true' or 'false'", s)
}

0 comments on commit 3f712e6

Please sign in to comment.