MCPcopy Create free account
hub / github.com/bufbuild/buf / ImagesToCodeGeneratorRequests

Function ImagesToCodeGeneratorRequests

private/bufpkg/bufimage/bufimage.go:610–665  ·  view source on GitHub ↗

ImagesToCodeGeneratorRequests converts the Images to CodeGeneratorRequests. All non-imports are added as files to generate. If includeImports is set, all non-well-known-type imports are also added as files to generate. If includeImports is set, only one CodeGeneratorRequest will contain any given f

(
	images []Image,
	parameter string,
	compilerVersion *pluginpb.Version,
	includeImports bool,
	includeWellKnownTypes bool,
)

Source from the content-addressed store, hash-verified

608// If includeWellKnownTypes is set, well-known-type imports are also added as files to generate.
609// includeWellKnownTypes has no effect if includeImports is not set.
610func ImagesToCodeGeneratorRequests(
611 images []Image,
612 parameter string,
613 compilerVersion *pluginpb.Version,
614 includeImports bool,
615 includeWellKnownTypes bool,
616) ([]*pluginpb.CodeGeneratorRequest, error) {
617 requests := make([]*pluginpb.CodeGeneratorRequest, len(images))
618 // alreadyUsedPaths is a map of paths that have already been added to an image.
619 //
620 // We track this if includeImports is set, so that when we find an import, we can
621 // see if the import was already added to a CodeGeneratorRequest via another Image
622 // in the Image slice. If the import was already added, we do not add duplicates
623 // across CodeGeneratorRequests.
624 var alreadyUsedPaths map[string]struct{}
625 // nonImportPaths is a map of non-import paths.
626 //
627 // We track this if includeImports is set. If we find a non-import file in Image A
628 // and this file is an import in Image B, the file will have already been added to
629 // a CodeGeneratorRequest via Image A, so do not add the duplicate to any other
630 // CodeGeneratorRequest.
631 var nonImportPaths map[string]struct{}
632 if includeImports {
633 // We don't need to track these if includeImports is false, so we only populate
634 // the maps if includeImports is true. If includeImports is false, only non-imports
635 // will be added to each CodeGeneratorRequest, so figuring out whether or not
636 // we should add a given import to a given CodeGeneratorRequest is unnecessary.
637 //
638 // imageToCodeGeneratorRequest checks if these maps are nil before every access.
639 alreadyUsedPaths = make(map[string]struct{})
640 nonImportPaths = make(map[string]struct{})
641 for _, image := range images {
642 for _, imageFile := range image.Files() {
643 if !imageFile.IsImport() {
644 nonImportPaths[imageFile.Path()] = struct{}{}
645 }
646 }
647 }
648 }
649 for i, image := range images {
650 var err error
651 requests[i], err = imageToCodeGeneratorRequest(
652 image,
653 parameter,
654 compilerVersion,
655 includeImports,
656 includeWellKnownTypes,
657 alreadyUsedPaths,
658 nonImportPaths,
659 )
660 if err != nil {
661 return nil, err
662 }
663 }
664 return requests, nil
665}
666
667type newImageForProtoOptions struct {

Callers 3

executePluginFunction · 0.92
TestBasicFunction · 0.92
execLocalPluginMethod · 0.92

Calls 4

FilesMethod · 0.65
IsImportMethod · 0.65
PathMethod · 0.65

Tested by 1

TestBasicFunction · 0.74

Used in the wild real call sites across dependent graphs

searching dependent graphs…