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, )
| 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. |
| 610 | func 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 | |
| 667 | type newImageForProtoOptions struct { |
searching dependent graphs…