Skip to content

Commit

Permalink
Generate single file per channel
Browse files Browse the repository at this point in the history
Signed-off-by: whitneygriffith <[email protected]>
  • Loading branch information
whitneygriffith committed Feb 19, 2024
1 parent ad441fe commit 63b3c88
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 32 deletions.
27 changes: 6 additions & 21 deletions cmd/protoc-gen-crd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func generate(request *plugin.CodeGeneratorRequest) (*plugin.CodeGeneratorRespon
log.Println("it is experimental: ", fd)
}
// Legacy channel will have all files that are in standard and experimental
log.Println("it is legacy: ", fd)
legacyChannelFilesToGen[fd] = struct{}{}
}

Expand All @@ -104,29 +105,13 @@ func generate(request *plugin.CodeGeneratorRequest) (*plugin.CodeGeneratorRespon
descriptionConfiguration,
enumAsIntOrString)

channelOutput := make(map[string]map[*protomodel.FileDescriptor]struct{})
channelOutput["kubernetes/legacy.gen.yaml"] = legacyChannelFilesToGen
channelOutput["kubernetes/standard.gen.yaml"] = standardChannelFilesToGen
channelOutput["kubernetes/exerimental.gen.yaml"] = experimentalChannelFilesToGen
for outputFileName, files := range channelOutput {
// TODO (whgriffi): fix the return to generate multiple files. At this time only the first file in the list is returned
return g.generateOutput(files, outputFileName)
}

return nil, nil
channels := make(map[string]map[*protomodel.FileDescriptor]struct{})
channels["kubernetes/legacy.gen.yaml"] = legacyChannelFilesToGen
channels["kubernetes/exerimental.gen.yaml"] = experimentalChannelFilesToGen
channels["kubernetes/standard.gen.yaml"] = standardChannelFilesToGen
return g.generateOutput(channels)
}

func main() {
// TODO(whgriffi): may need to loop through the various channels here to generate multiple files as files aren't outputted by just running g.generateOutput(files, outputFileName)
// The protocgen.Generate function is part of the protoc-gen-go plugin for the Protocol Buffers compiler (protoc).
// This function handles the communication with protoc, including reading the CodeGeneratorRequest from protoc and writing the CodeGeneratorResponse back to protoc.
// channels = make(map[string][]string) // map of channel name to acceptable API versions
// channels["legacy"] = []string{"v1alpha1", "v1beta1", "v1"}
// channels["standard"] = []string{"v1beta1", "v1"}
// channels["experimental"] = []string{"v1alpha1"}

// for _, channel := range channels {

// }
protocgen.Generate(generate)
}
27 changes: 16 additions & 11 deletions cmd/protoc-gen-crd/openapiGenerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,10 @@ func buildCustomSchemasByMessageName() map[string]*apiext.JSONSchemaProps {
return schemasByMessageName
}

func (g *openapiGenerator) generateOutput(filesToGen map[*protomodel.FileDescriptor]struct{}, fileName string) (*plugin.CodeGeneratorResponse, error) {
func (g *openapiGenerator) generateOutput(channels map[string]map[*protomodel.FileDescriptor]struct{}) (*plugin.CodeGeneratorResponse, error) {
response := plugin.CodeGeneratorResponse{}

g.generateSingleFileOutput(filesToGen, fileName, &response)
g.generateSingleFileOutputPerChannel(channels, &response)

return &response, nil
}
Expand All @@ -184,17 +184,22 @@ func (g *openapiGenerator) getFileContents(
}
}

func (g *openapiGenerator) generateSingleFileOutput(filesToGen map[*protomodel.FileDescriptor]struct{}, fileName string, response *plugin.CodeGeneratorResponse) {
messages := make(map[string]*protomodel.MessageDescriptor)
enums := make(map[string]*protomodel.EnumDescriptor)
descriptions := make(map[string]string)
func (g *openapiGenerator) generateSingleFileOutputPerChannel(channels map[string]map[*protomodel.FileDescriptor]struct{}, response *plugin.CodeGeneratorResponse) {
for channel := range channels {
messages := make(map[string]*protomodel.MessageDescriptor)
enums := make(map[string]*protomodel.EnumDescriptor)
descriptions := make(map[string]string)

for file := range filesToGen {
g.getFileContents(file, messages, enums, descriptions)
}
outputFileName := channel
filesToGen := channels[channel]

rf := g.generateFile(fileName, messages, enums, descriptions)
response.File = []*plugin.CodeGeneratorResponse_File{&rf}
log.Println("Generating response file for channel: ", channel)
for file := range filesToGen {
g.getFileContents(file, messages, enums, descriptions)
}
rf := g.generateFile(outputFileName, messages, enums, descriptions)
response.File = append(response.File, &rf)
}
}

const (
Expand Down

0 comments on commit 63b3c88

Please sign in to comment.